@@ -98,6 +98,7 @@ def delete(self, symbol, chunk_range=None):
98
98
# read out chunks that fall within the range and filter out
99
99
# data within the range
100
100
df = self .read (symbol , chunk_range = chunk_range , filter_data = False )
101
+ row_adjust = len (df )
101
102
df = self .chunker .exclude (df , chunk_range )
102
103
103
104
# remove chunks, and update any remaining data
@@ -106,6 +107,12 @@ def delete(self, symbol, chunk_range=None):
106
107
self ._collection .delete_many (query )
107
108
self .update (symbol , df )
108
109
110
+ # update symbol metadata (rows and chunk count)
111
+ sym = self ._get_symbol_info (symbol )
112
+ sym [ROWS ] -= row_adjust
113
+ sym [CHUNK_COUNT ] = self ._collection .count ({SYMBOL : symbol })
114
+ self ._symbols .replace_one ({SYMBOL : symbol }, sym )
115
+
109
116
else :
110
117
query = {SYMBOL : symbol }
111
118
self ._collection .delete_many (query )
@@ -264,7 +271,7 @@ def __concat(self, a, b):
264
271
def __take_new (self , a , b ):
265
272
return a
266
273
267
- def __update (self , symbol , item , combine_method = None ):
274
+ def __update (self , symbol , item , combine_method = None , chunk_range = None ):
268
275
if not isinstance (item , (DataFrame , Series )):
269
276
raise Exception ("Can only chunk DataFrames and Series" )
270
277
@@ -277,11 +284,16 @@ def __update(self, symbol, item, combine_method=None):
277
284
if sym [TYPE ] == 'dataframe' and not isinstance (item , DataFrame ):
278
285
raise Exception ("Cannot combine DataFrame and Series" )
279
286
287
+ if chunk_range :
288
+ self .delete (symbol , chunk_range )
289
+ sym = self ._get_symbol_info (symbol )
290
+
280
291
bulk = self ._collection .initialize_unordered_bulk_op ()
281
292
op = False
282
293
for start , end , record in self .chunker .to_chunks (item , sym [CHUNK_SIZE ]):
283
294
# read out matching chunks
284
295
df = self .read (symbol , chunk_range = self .chunker .to_range (start , end ), filter_data = False )
296
+
285
297
# assuming they exist, update them and store the original chunk
286
298
# range for later use
287
299
if not df .empty :
@@ -334,7 +346,7 @@ def append(self, symbol, item):
334
346
"""
335
347
self .__update (symbol , item , combine_method = self .__concat )
336
348
337
- def update (self , symbol , item ):
349
+ def update (self , symbol , item , chunk_range = None ):
338
350
"""
339
351
Overwrites data in DB with data in item for the given symbol.
340
352
@@ -346,9 +358,19 @@ def update(self, symbol, item):
346
358
the symbol for the given item in the DB
347
359
item: DataFrame or Series
348
360
the data to update
361
+ chunk_range: None, or a range object
362
+ If a range is specified, it will clear/delete the data within the
363
+ range and overwrite it with the data in item. This allows the user
364
+ to update with data that might only be a subset of the
365
+ original data.
349
366
"""
350
367
351
- self .__update (symbol , item , combine_method = self .__take_new )
368
+ if chunk_range :
369
+ if self .chunker .filter (item , chunk_range ).empty :
370
+ raise Exception ('Range must be inclusive of data' )
371
+ self .__update (symbol , item , combine_method = self .__concat , chunk_range = chunk_range )
372
+ else :
373
+ self .__update (symbol , item , combine_method = self .__take_new , chunk_range = chunk_range )
352
374
353
375
def get_info (self , symbol ):
354
376
sym = self ._get_symbol_info (symbol )
0 commit comments