-
Notifications
You must be signed in to change notification settings - Fork 581
/
chunkstore.py
359 lines (287 loc) · 11.7 KB
/
chunkstore.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import logging
import pymongo
import hashlib
from bson.binary import Binary
from pandas import concat, DataFrame, Series
from ..decorators import mongo_retry
from .._util import indent
from ..serialization.numpy_arrays import FrametoArraySerializer, DATA, VALUES, COLUMNS, TYPE
from .date_chunker import DateChunker, START, END
from ..exceptions import NoDataFoundException
logger = logging.getLogger(__name__)
CHUNK_STORE_TYPE = 'ChunkStoreV1'
SYMBOL = 'sy'
SHA = 'sh'
CHUNK_SIZE = 'cs'
CHUNK_COUNT = 'cc'
APPEND_COUNT = 'ac'
ROWS = 'r'
class ChunkStore(object):
@classmethod
def initialize_library(cls, arctic_lib, **kwargs):
ChunkStore(arctic_lib)._ensure_index()
@mongo_retry
def _ensure_index(self):
self._symbols.create_index([(SYMBOL, pymongo.ASCENDING)],
unique=True,
background=True)
self._collection.create_index([(SYMBOL, pymongo.HASHED)],
background=True)
self._collection.create_index([(SYMBOL, pymongo.ASCENDING),
(SHA, pymongo.ASCENDING)],
unique=True,
background=True)
self._collection.create_index([(SYMBOL, pymongo.ASCENDING),
(START, pymongo.ASCENDING),
(END, pymongo.ASCENDING)],
unique=True, background=True)
@mongo_retry
def __init__(self, arctic_lib, chunker=DateChunker(), serializer=FrametoArraySerializer()):
self.chunker = chunker
self.serializer = serializer
self._arctic_lib = arctic_lib
# Do we allow reading from secondaries
self._allow_secondary = self._arctic_lib.arctic._allow_secondary
# The default collection
self._collection = arctic_lib.get_top_level_collection()
self._symbols = self._collection.symbols
def __getstate__(self):
return {'arctic_lib': self._arctic_lib}
def __setstate__(self, state):
return ChunkStore.__init__(self, state['arctic_lib'])
def __str__(self):
return """<%s at %s>\n%s""" % (self.__class__.__name__, hex(id(self)),
indent(str(self._arctic_lib), 4))
def __repr__(self):
return str(self)
def _checksum(self, doc):
"""
Checksum the passed in dictionary
"""
sha = hashlib.sha1()
sha.update(self.chunker.chunk_to_str(doc[START]).encode('ascii'))
sha.update(self.chunker.chunk_to_str(doc[END]).encode('ascii'))
for k in doc[DATA][COLUMNS]:
sha.update(doc[DATA][DATA][k][VALUES])
return Binary(sha.digest())
def delete(self, symbol, chunk_range=None):
"""
Delete all chunks for a symbol, or optionally, chunks within a range
Parameters
----------
symbol : str
symbol name for the item
chunk_range: range object
a date range to delete
"""
if chunk_range:
# read out chunks that fall within the range and filter out
# data within the range
df = self.read(symbol, chunk_range=chunk_range, filter_data=False)
df = self.chunker.exclude(df, chunk_range)
# remove chunks, and update any remaining data
query = {SYMBOL: symbol}
query.update(self.chunker.to_mongo(chunk_range))
self._collection.delete_many(query)
self.update(symbol, df)
else:
query = {SYMBOL: symbol}
self._collection.delete_many(query)
self._collection.symbols.delete_many(query)
def list_symbols(self):
"""
Returns all symbols in the library
Returns
-------
list of str
"""
return self._symbols.distinct(SYMBOL)
def _get_symbol_info(self, symbol):
return self._symbols.find_one({SYMBOL: symbol})
def rename(self, from_symbol, to_symbol):
"""
Rename a symbol
Parameters
----------
from_symbol: str
the existing symbol that will be renamed
to_symbol: str
the new symbol name
"""
sym = self._get_symbol_info(from_symbol)
if not sym:
raise NoDataFoundException('No data found for %s' % (from_symbol))
if self._get_symbol_info(to_symbol) is not None:
raise Exception('Symbol %s already exists' % (to_symbol))
mongo_retry(self._collection.update_many)({SYMBOL: from_symbol},
{'$set': {SYMBOL: to_symbol}})
mongo_retry(self._symbols.update_one)({SYMBOL: from_symbol},
{'$set': {SYMBOL: to_symbol}})
def read(self, symbol, chunk_range=None, columns=None, filter_data=True):
"""
Reads data for a given symbol from the database.
Parameters
----------
symbol: str
the symbol to retrieve
chunk_range: object
corresponding range object for the specified chunker (for
DateChunker it is a DateRange object)
columns: list of str
subset of columns to read back (index will always be included, if
one exists)
filter: boolean
perform chunk level filtering on the data (see filter in _chunker)
only applicable when chunk_range is specified
Returns
-------
DataFrame or Series
"""
sym = self._get_symbol_info(symbol)
if not sym:
raise NoDataFoundException('No data found for %s' % (symbol))
spec = {SYMBOL: symbol,
}
if chunk_range:
spec.update(self.chunker.to_mongo(chunk_range))
segments = []
for x in self._collection.find(spec, sort=[(START, pymongo.ASCENDING)],):
segments.append(x[DATA])
data = self.serializer.deserialize(segments, columns)
if not filter_data or chunk_range is None:
return data
return self.chunker.filter(data, chunk_range)
def write(self, symbol, item, chunk_size):
"""
Writes data from item to symbol in the database
Parameters
----------
symbol: str
the symbol that will be used to reference the written data
item: Dataframe or Series
the data to write the database
chunk_size: ?
A chunk size that is understood by the specified chunker
"""
if not isinstance(item, (DataFrame, Series)):
raise Exception("Can only chunk DataFrames and Series")
previous_shas = []
doc = {}
doc[SYMBOL] = symbol
doc[CHUNK_SIZE] = chunk_size
doc[ROWS] = len(item)
doc[TYPE] = 'dataframe' if isinstance(item, DataFrame) else 'series'
sym = self._get_symbol_info(symbol)
if sym:
previous_shas = set([Binary(x[SHA]) for x in self._collection.find({SYMBOL: symbol},
projection={SHA: True, '_id': False},
)])
op = False
bulk = self._collection.initialize_unordered_bulk_op()
chunk_count = 0
for start, end, record in self.chunker.to_chunks(item, chunk_size):
chunk_count += 1
data = self.serializer.serialize(record)
doc[COLUMNS] = data[COLUMNS]
chunk = {DATA: data}
chunk[START] = start
chunk[END] = end
chunk[SYMBOL] = symbol
chunk[SHA] = self._checksum(chunk)
if chunk[SHA] not in previous_shas:
op = True
bulk.find({SYMBOL: symbol, START: start, END: end},
).upsert().update_one({'$set': chunk})
else:
# already exists, dont need to update in mongo
previous_shas.remove(chunk[SHA])
if op:
bulk.execute()
doc[CHUNK_COUNT] = chunk_count
doc[APPEND_COUNT] = 0
if previous_shas:
mongo_retry(self._collection.delete_many)({SYMBOL: symbol, SHA: {'$in': list(previous_shas)}})
mongo_retry(self._symbols.update_one)({SYMBOL: symbol},
{'$set': doc},
upsert=True)
def __concat(self, a, b):
return concat([a, b]).sort_index()
def __take_new(self, a, b):
return a
def __update(self, symbol, item, combine_method=None):
if not isinstance(item, (DataFrame, Series)):
raise Exception("Can only chunk DataFrames and Series")
sym = self._get_symbol_info(symbol)
if not sym:
raise NoDataFoundException("Symbol does not exist.")
if sym[TYPE] == 'series' and not isinstance(item, Series):
raise Exception("Cannot combine Series and DataFrame")
if sym[TYPE] == 'dataframe' and not isinstance(item, DataFrame):
raise Exception("Cannot combine DataFrame and Series")
bulk = self._collection.initialize_unordered_bulk_op()
op = False
for start, end, record in self.chunker.to_chunks(item, sym[CHUNK_SIZE]):
# read out matching chunks
df = self.read(symbol, chunk_range=self.chunker.to_range(start, end), filter_data=False)
# assuming they exist, update them and store the original chunk
# range for later use
if not df.empty:
record = combine_method(record, df)
if record is None or record.equals(df):
continue
sym[APPEND_COUNT] += len(record)
sym[ROWS] += len(record) - len(df)
new_chunk = False
else:
new_chunk = True
sym[CHUNK_COUNT] += 1
sym[ROWS] += len(record)
data = self.serializer.serialize(record)
op = True
chunk = {DATA: data}
chunk[TYPE] = 'dataframe' if isinstance(record, DataFrame) else 'series'
chunk[START] = start
chunk[END] = end
sha = self._checksum(chunk)
chunk[SHA] = sha
if new_chunk:
# new chunk
bulk.find({SYMBOL: symbol, SHA: sha}
).upsert().update_one({'$set': chunk})
else:
bulk.find({SYMBOL: symbol, START: start, END: end}
).update_one({'$set': chunk})
if op:
bulk.execute()
self._symbols.replace_one({SYMBOL: symbol}, sym)
def append(self, symbol, item):
"""
Appends data from item to symbol's data in the database.
Is not idempotent
Parameters
----------
symbol: str
the symbol for the given item in the DB
item: DataFrame or Series
the data to append
"""
self.__update(symbol, item, combine_method=self.__concat)
def update(self, symbol, item):
"""
Overwrites data in DB with data in item for the given symbol.
Is idempotent
Parameters
----------
symbol: str
the symbol for the given item in the DB
item: DataFrame or Series
the data to update
"""
self.__update(symbol, item, combine_method=self.__take_new)
def get_info(self, symbol):
sym = self._get_symbol_info(symbol)
ret = {}
ret['chunk_count'] = sym[CHUNK_COUNT]
ret['rows'] = sym[ROWS]
ret['col_names'] = sym[COLUMNS]
return ret