forked from man-group/arctic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tickstore.py
721 lines (638 loc) · 28.4 KB
/
tickstore.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
from __future__ import print_function
import logging
from bson.binary import Binary
import copy
from datetime import datetime as dt, timedelta
import lz4
import numpy as np
import pandas as pd
from pandas.core.frame import _arrays_to_mgr
import pymongo
from pymongo import ReadPreference
from pymongo.errors import OperationFailure
from six import iteritems, string_types
from ..date import DateRange, to_pandas_closed_closed, mktz, datetime_to_ms, ms_to_datetime, CLOSED_CLOSED, to_dt
from ..decorators import mongo_retry
from ..exceptions import OverlappingDataException, NoDataFoundException, UnorderedDataException, UnhandledDtypeException, ArcticException
from .._util import indent
logger = logging.getLogger(__name__)
# Example-Schema:
# --------------
# {ID: ObjectId('52b1d39eed5066ab5e87a56d'),
# SYMBOL: u'symbol'
# INDEX: Binary('...', 0),
# IMAGE_DOC: { IMAGE: {
# 'ASK': 10.
# ...
# }
# 's': <sequence_no>
# 't': DateTime(...)
# }
# COLUMNS: {
# 'ACT_FLAG1': {
# DATA: Binary('...', 0),
# DTYPE: u'U1',
# ROWMASK: Binary('...', 0)},
# 'ACVOL_1': {
# DATA: Binary('...', 0),
# DTYPE: u'float64',
# ROWMASK: Binary('...', 0)},
# ...
# }
# START: DateTime(...),
# END: DateTime(...),
# END_SEQ: 31553879L,
# SEGMENT: 1386933906826L,
# SHA: 1386933906826L,
# VERSION: 3,
# }
TICK_STORE_TYPE = 'TickStoreV3'
ID = '_id'
SYMBOL = 'sy'
INDEX = 'i'
START = 's'
END = 'e'
START_SEQ = 'sS'
END_SEQ = 'eS'
SEGMENT = 'se'
SHA = 'sh'
IMAGE_DOC = 'im'
IMAGE = 'i'
COLUMNS = 'cs'
DATA = 'd'
DTYPE = 't'
IMAGE_TIME = 't'
ROWMASK = 'm'
COUNT = 'c'
VERSION = 'v'
CHUNK_VERSION_NUMBER = 3
class TickStore(object):
@classmethod
def initialize_library(cls, arctic_lib, **kwargs):
TickStore(arctic_lib)._ensure_index()
@mongo_retry
def _ensure_index(self):
collection = self._collection
collection.create_index([(SYMBOL, pymongo.ASCENDING),
(START, pymongo.ASCENDING)], background=True)
collection.create_index([(START, pymongo.ASCENDING)], background=True)
def __init__(self, arctic_lib, chunk_size=100000):
"""
Parameters
----------
arctic_lib : TickStore
Arctic Library
chunk_size : int
Number of ticks to store in a document before splitting to another document.
if the library was obtained through get_library then set with: self._chuck_size = 10000
"""
self._arctic_lib = arctic_lib
# Do we allow reading from secondaries
self._allow_secondary = self._arctic_lib.arctic._allow_secondary
# The default collections
self._collection = arctic_lib.get_top_level_collection()
self._chunk_size = chunk_size
def __getstate__(self):
return {'arctic_lib': self._arctic_lib}
def __setstate__(self, state):
return TickStore.__init__(self, state['arctic_lib'])
def __str__(self):
return """<%s at %s>
%s""" % (self.__class__.__name__, hex(id(self)), indent(str(self._arctic_lib), 4))
def __repr__(self):
return str(self)
def delete(self, symbol, date_range=None):
"""
Delete all chunks for a symbol.
Which are, for the moment, fully contained in the passed in
date_range.
Parameters
----------
symbol : `str`
symbol name for the item
date_range : `date.DateRange`
DateRange to delete ticks in
"""
query = {SYMBOL: symbol}
date_range = to_pandas_closed_closed(date_range)
if date_range is not None:
assert date_range.start and date_range.end
query[START] = {'$gte': date_range.start}
query[END] = {'$lte': date_range.end}
return self._collection.delete_many(query)
def list_symbols(self, date_range=None):
return self._collection.distinct(SYMBOL)
def _mongo_date_range_query(self, symbol, date_range):
# Handle date_range
if not date_range:
date_range = DateRange()
# We're assuming CLOSED_CLOSED on these Mongo queries
assert date_range.interval == CLOSED_CLOSED
# Since we only index on the start of the chunk,
# we do a pre-flight aggregate query to find the point where the
# earliest relevant chunk starts.
start_range = {}
first_dt = last_dt = None
if date_range.start:
assert date_range.start.tzinfo
start = date_range.start
# If all chunks start inside of the range, we default to capping to our
# range so that we don't fetch any chunks from the beginning of time
start_range['$gte'] = start
match = self._symbol_query(symbol)
match.update({'s': {'$lte': start}})
result = self._collection.aggregate([
# Only look at the symbols we are interested in and chunks that
# start before our start datetime
{'$match': match},
# Throw away everything but the start of every chunk and the symbol
{'$project': {'_id': 0, 's': 1, 'sy': 1}},
# For every symbol, get the latest chunk start (that is still before
# our sought start)
{'$group': {'_id': '$sy', 'start': {'$max': '$s'}}},
{'$sort': {'start': 1}},
])
# Now we need to get the earliest start of the chunk that still spans the start point.
# Since we got them sorted by start, we just need to fetch their ends as well and stop
# when we've seen the first such chunk
try:
for candidate in result:
chunk = self._collection.find_one({'s': candidate['start'], 'sy': candidate['_id']}, {'e': 1})
if chunk['e'].replace(tzinfo=mktz('UTC')) >= start:
start_range['$gte'] = candidate['start'].replace(tzinfo=mktz('UTC'))
break
except StopIteration:
pass
# Find the end bound
if date_range.end:
# If we have an end, we are only interested in the chunks that start before the end.
assert date_range.end.tzinfo
last_dt = date_range.end
else:
logger.info("No end provided. Loading a month for: {}:{}".format(symbol, first_dt))
if not first_dt:
first_doc = self._collection.find_one(self._symbol_query(symbol),
projection={START: 1, ID: 0},
sort=[(START, pymongo.ASCENDING)])
if not first_doc:
raise NoDataFoundException()
first_dt = first_doc[START]
last_dt = first_dt + timedelta(days=30)
if last_dt:
start_range['$lte'] = last_dt
# Return chunks in the specified range
if not start_range:
return {}
return {START: start_range}
def _symbol_query(self, symbol):
if isinstance(symbol, string_types):
query = {SYMBOL: symbol}
elif symbol is not None:
query = {SYMBOL: {'$in': symbol}}
else:
query = {}
return query
def _read_preference(self, allow_secondary):
""" Return the mongo read preference given an 'allow_secondary' argument
"""
allow_secondary = self._allow_secondary if allow_secondary is None else allow_secondary
return ReadPreference.NEAREST if allow_secondary else ReadPreference.PRIMARY
def read(self, symbol, date_range=None, columns=None, include_images=False, allow_secondary=None,
_target_tick_count=0):
"""
Read data for the named symbol. Returns a VersionedItem object with
a data and metdata element (as passed into write).
Parameters
----------
symbol : `str`
symbol name for the item
date_range : `date.DateRange`
Returns ticks in the specified DateRange
columns : `list` of `str`
Columns (fields) to return from the tickstore
include_images : `bool`
Should images (/snapshots) be included in the read
allow_secondary : `bool` or `None`
Override the default behavior for allowing reads from secondary members of a cluster:
`None` : use the settings from the top-level `Arctic` object used to query this version store.
`True` : allow reads from secondary members
`False` : only allow reads from primary members
Returns
-------
pandas.DataFrame of data
"""
perf_start = dt.now()
rtn = {}
column_set = set()
multiple_symbols = not isinstance(symbol, string_types)
date_range = to_pandas_closed_closed(date_range)
query = self._symbol_query(symbol)
query.update(self._mongo_date_range_query(symbol, date_range))
if columns:
projection = dict([(SYMBOL, 1),
(INDEX, 1),
(START, 1),
(VERSION, 1),
(IMAGE_DOC, 1)] +
[(COLUMNS + '.%s' % c, 1) for c in columns])
column_set.update([c for c in columns if c != 'SYMBOL'])
else:
projection = dict([(SYMBOL, 1),
(INDEX, 1),
(START, 1),
(VERSION, 1),
(COLUMNS, 1),
(IMAGE_DOC, 1)])
column_dtypes = {}
ticks_read = 0
data_coll = self._collection.with_options(read_preference=self._read_preference(allow_secondary))
for b in data_coll.find(query, projection=projection).sort([(START, pymongo.ASCENDING)],):
data = self._read_bucket(b, column_set, column_dtypes,
multiple_symbols or (columns is not None and 'SYMBOL' in columns),
include_images, columns)
for k, v in iteritems(data):
try:
rtn[k].append(v)
except KeyError:
rtn[k] = [v]
# For testing
ticks_read += len(data[INDEX])
if _target_tick_count and ticks_read > _target_tick_count:
break
if not rtn:
raise NoDataFoundException("No Data found for {} in range: {}".format(symbol, date_range))
rtn = self._pad_and_fix_dtypes(rtn, column_dtypes)
index = pd.to_datetime(np.concatenate(rtn[INDEX]), utc=True, unit='ms')
if columns is None:
columns = [x for x in rtn.keys() if x not in (INDEX, 'SYMBOL')]
if multiple_symbols and 'SYMBOL' not in columns:
columns = ['SYMBOL', ] + columns
if len(index) > 0:
arrays = [np.concatenate(rtn[k]) for k in columns]
else:
arrays = [[] for k in columns]
if multiple_symbols:
sort = np.argsort(index, kind='mergesort')
index = index[sort]
arrays = [a[sort] for a in arrays]
t = (dt.now() - perf_start).total_seconds()
logger.info("Got data in %s secs, creating DataFrame..." % t)
mgr = _arrays_to_mgr(arrays, columns, index, columns, dtype=None)
rtn = pd.DataFrame(mgr)
# Present data in the user's default TimeZone
rtn.index.tz = mktz()
t = (dt.now() - perf_start).total_seconds()
ticks = len(rtn)
rate = int(ticks / t) if t != 0 else float("nan")
logger.info("%d rows in %s secs: %s ticks/sec" % (ticks, t, rate))
if not rtn.index.is_monotonic:
logger.error("TimeSeries data is out of order, sorting!")
rtn = rtn.sort_index(kind='mergesort')
if date_range:
# FIXME: support DateRange.interval...
rtn = rtn.ix[date_range.start:date_range.end]
return rtn
def _pad_and_fix_dtypes(self, cols, column_dtypes):
# Pad out Nones with empty arrays of appropriate dtypes
rtn = {}
index = cols[INDEX]
full_length = len(index)
for k, v in iteritems(cols):
if k != INDEX and k != 'SYMBOL':
col_len = len(v)
if col_len < full_length:
v = ([None, ] * (full_length - col_len)) + v
assert len(v) == full_length
for i, arr in enumerate(v):
if arr is None:
# Replace Nones with appropriate-length empty arrays
v[i] = self._empty(len(index[i]), column_dtypes.get(k))
else:
# Promote to appropriate dtype only if we can safely cast all the values
# This avoids the case with strings where None is cast as 'None'.
# Casting the object to a string is not worthwhile anyway as Pandas changes the
# dtype back to objectS
if (i == 0 or v[i].dtype != v[i - 1].dtype) and np.can_cast(v[i].dtype, column_dtypes[k],
casting='safe'):
v[i] = v[i].astype(column_dtypes[k], casting='safe')
rtn[k] = v
return rtn
def _set_or_promote_dtype(self, column_dtypes, c, dtype):
existing_dtype = column_dtypes.get(c)
if existing_dtype is None or existing_dtype != dtype:
# Promote ints to floats - as we can't easily represent NaNs
if np.issubdtype(dtype, int):
dtype = np.dtype('f8')
column_dtypes[c] = np.promote_types(column_dtypes.get(c, dtype), dtype)
def _prepend_image(self, document, im, rtn_length, column_dtypes, column_set, columns):
image = im[IMAGE]
first_dt = im[IMAGE_TIME]
if not first_dt.tzinfo:
first_dt = first_dt.replace(tzinfo=mktz('UTC'))
document[INDEX] = np.insert(document[INDEX], 0, np.uint64(datetime_to_ms(first_dt)))
for field in image:
if field == INDEX:
continue
if columns and field not in columns:
continue
if field not in document or document[field] is None:
col_dtype = np.dtype(str if isinstance(image[field], string_types) else 'f8')
document[field] = self._empty(rtn_length, dtype=col_dtype)
column_dtypes[field] = col_dtype
column_set.add(field)
val = image[field]
document[field] = np.insert(document[field], 0, document[field].dtype.type(val))
# Now insert rows for fields in document that are not in the image
for field in set(document).difference(set(image)):
if field == INDEX:
continue
logger.debug("Field %s is missing from image!" % field)
if document[field] is not None:
val = np.nan
document[field] = np.insert(document[field], 0, document[field].dtype.type(val))
return document
def _read_bucket(self, doc, column_set, column_dtypes, include_symbol, include_images, columns):
rtn = {}
if doc[VERSION] != 3:
raise ArcticException("Unhandled document version: %s" % doc[VERSION])
rtn[INDEX] = np.cumsum(np.fromstring(lz4.decompress(doc[INDEX]), dtype='uint64'))
doc_length = len(rtn[INDEX])
column_set.update(doc[COLUMNS].keys())
# get the mask for the columns we're about to load
union_mask = np.zeros((doc_length + 7) // 8, dtype='uint8')
for c in column_set:
try:
coldata = doc[COLUMNS][c]
mask = np.fromstring(lz4.decompress(coldata[ROWMASK]), dtype='uint8')
union_mask = union_mask | mask
except KeyError:
rtn[c] = None
union_mask = np.unpackbits(union_mask)[:doc_length].astype('bool')
rtn_length = np.sum(union_mask)
rtn[INDEX] = rtn[INDEX][union_mask]
if include_symbol:
rtn['SYMBOL'] = [doc[SYMBOL], ] * rtn_length
# Unpack each requested column in turn
for c in column_set:
try:
coldata = doc[COLUMNS][c]
dtype = np.dtype(coldata[DTYPE])
values = np.fromstring(lz4.decompress(coldata[DATA]), dtype=dtype)
self._set_or_promote_dtype(column_dtypes, c, dtype)
rtn[c] = self._empty(rtn_length, dtype=column_dtypes[c])
rowmask = np.unpackbits(np.fromstring(lz4.decompress(coldata[ROWMASK]),
dtype='uint8'))[:doc_length].astype('bool')
rowmask = rowmask[union_mask]
rtn[c][rowmask] = values
except KeyError:
rtn[c] = None
if include_images and doc.get(IMAGE_DOC, {}).get(IMAGE, {}):
rtn = self._prepend_image(rtn, doc[IMAGE_DOC], rtn_length, column_dtypes, column_set, columns)
return rtn
def _empty(self, length, dtype):
if dtype is not None and dtype == np.float64:
rtn = np.empty(length, dtype)
rtn[:] = np.nan
return rtn
else:
return np.empty(length, dtype=np.object_)
def stats(self):
"""
Return storage statistics about the library
Returns
-------
dictionary of storage stats
"""
res = {}
db = self._collection.database
conn = db.connection
res['sharding'] = {}
try:
sharding = conn.config.databases.find_one({'_id': db.name})
if sharding:
res['sharding'].update(sharding)
res['sharding']['collections'] = list(conn.config.collections.find(
{'_id': {'$regex': '^' + db.name + r"\..*"}}))
except OperationFailure:
# Access denied
pass
res['dbstats'] = db.command('dbstats')
res['chunks'] = db.command('collstats', self._collection.name)
res['totals'] = {'count': res['chunks']['count'],
'size': res['chunks']['size'],
}
return res
def _assert_nonoverlapping_data(self, symbol, start, end):
#
# Imagine we're trying to insert a tick bucket like:
# |S------ New-B -------------- E|
# |---- 1 ----| |----- 2 -----| |----- 3 -----|
#
# S = New-B Start
# E = New-B End
# New-B overlaps with existing buckets 1,2,3
#
# All we need to do is find the bucket who's start is immediately before (E)
# If that document's end is > S, then we know it overlaps
# with this bucket.
doc = self._collection.find_one({SYMBOL: symbol,
START: {'$lt': end}
},
projection={START: 1,
END: 1,
'_id': 0},
sort=[(START, pymongo.DESCENDING)])
if doc:
if not doc[END].tzinfo:
doc[END] = doc[END].replace(tzinfo=mktz('UTC'))
if doc[END] > start:
raise OverlappingDataException("Document already exists with start:{} end:{} in the range of our start:{} end:{}".format(
doc[START], doc[END], start, end))
def write(self, symbol, data, initial_image=None):
"""
Writes a list of market data events.
Parameters
----------
symbol : `str`
symbol name for the item
data : list of dicts or a pandas.DataFrame
List of ticks to store to the tick-store.
if a list of dicts, each dict must contain a 'index' datetime
if a pandas.DataFrame the index must be a Timestamp that can be converted to a datetime.
Index names will not be preserved.
initial_image : dict
Dict of the initial image at the start of the document. If this contains a 'index' entry it is
assumed to be the time of the timestamp of the index
"""
pandas = False
# Check for overlapping data
if isinstance(data, list):
start = data[0]['index']
end = data[-1]['index']
elif isinstance(data, pd.DataFrame):
start = data.index[0].to_pydatetime()
end = data.index[-1].to_pydatetime()
pandas = True
else:
raise UnhandledDtypeException("Can't persist type %s to tickstore" % type(data))
self._assert_nonoverlapping_data(symbol, to_dt(start), to_dt(end))
if pandas:
buckets = self._pandas_to_buckets(data, symbol, initial_image)
else:
buckets = self._to_buckets(data, symbol, initial_image)
self._write(buckets)
def _write(self, buckets):
start = dt.now()
mongo_retry(self._collection.insert_many)(buckets)
t = (dt.now() - start).total_seconds()
ticks = len(buckets) * self._chunk_size
rate = int(ticks / t) if t != 0 else float("nan")
logger.debug("%d buckets in %s: approx %s ticks/sec" % (len(buckets), t, rate))
def _pandas_to_buckets(self, x, symbol, initial_image):
rtn = []
for i in range(0, len(x), self._chunk_size):
bucket, initial_image = TickStore._pandas_to_bucket(x[i:i + self._chunk_size], symbol, initial_image)
rtn.append(bucket)
return rtn
def _to_buckets(self, x, symbol, initial_image):
rtn = []
for i in range(0, len(x), self._chunk_size):
bucket, initial_image = TickStore._to_bucket(x[i:i + self._chunk_size], symbol, initial_image)
rtn.append(bucket)
return rtn
@staticmethod
def _to_ms(date):
if isinstance(date, dt):
if not date.tzinfo:
logger.warning('WARNING: treating naive datetime as UTC in write path')
return datetime_to_ms(date)
return date
@staticmethod
def _str_dtype(dtype):
"""
Represent dtypes without byte order, as earlier Java tickstore code doesn't support explicit byte order.
"""
assert dtype.byteorder != '>'
if (dtype.kind) == 'i':
assert dtype.itemsize == 8
return 'int64'
elif (dtype.kind) == 'f':
assert dtype.itemsize == 8
return 'float64'
elif (dtype.kind) == 'U':
return 'U%d' % (dtype.itemsize / 4)
else:
raise UnhandledDtypeException("Bad dtype '%s'" % dtype)
@staticmethod
def _ensure_supported_dtypes(array):
# We only support these types for now, as we need to read them in Java
if (array.dtype.kind) == 'i':
array = array.astype('<i8')
elif (array.dtype.kind) == 'f':
array = array.astype('<f8')
elif (array.dtype.kind) in ('U', 'S'):
array = array.astype(np.unicode_)
else:
raise UnhandledDtypeException("Unsupported dtype '%s' - only int64, float64 and U are supported" % array.dtype)
# Everything is little endian in tickstore
if array.dtype.byteorder != '<':
array = array.astype(array.dtype.newbyteorder('<'))
return array
@staticmethod
def _pandas_compute_final_image(df, image, end):
# Compute the final image with forward fill of df applied to the image
final_image = copy.copy(image)
last_values = df.ffill().tail(1).to_dict()
last_dict = {i: list(a.values())[0] for i, a in last_values.items()}
final_image.update(last_dict)
final_image['index'] = end
return final_image
@staticmethod
def _pandas_to_bucket(df, symbol, initial_image):
rtn = {SYMBOL: symbol, VERSION: CHUNK_VERSION_NUMBER, COLUMNS: {}, COUNT: len(df)}
end = to_dt(df.index[-1].to_pydatetime())
if initial_image :
if 'index' in initial_image:
start = min(to_dt(df.index[0].to_pydatetime()), initial_image['index'])
else:
start = to_dt(df.index[0].to_pydatetime())
image_start = initial_image.get('index', start)
image = {k: v for k, v in initial_image.items() if k != 'index'}
rtn[IMAGE_DOC] = {IMAGE_TIME: image_start, IMAGE: initial_image}
final_image = TickStore._pandas_compute_final_image(df, initial_image, end)
else:
start = to_dt(df.index[0].to_pydatetime())
final_image = {}
rtn[END] = end
rtn[START] = start
logger.warning("NB treating all values as 'exists' - no longer sparse")
rowmask = Binary(lz4.compressHC(np.packbits(np.ones(len(df), dtype='uint8'))))
index_name = df.index.names[0] or "index"
recs = df.to_records(convert_datetime64=False)
for col in df:
array = TickStore._ensure_supported_dtypes(recs[col])
col_data = {}
col_data[DATA] = Binary(lz4.compressHC(array.tostring()))
col_data[ROWMASK] = rowmask
col_data[DTYPE] = TickStore._str_dtype(array.dtype)
rtn[COLUMNS][col] = col_data
rtn[INDEX] = Binary(lz4.compressHC(np.concatenate(([recs[index_name][0].astype('datetime64[ms]').view('uint64')],
np.diff(recs[index_name].astype('datetime64[ms]').view('uint64')))).tostring()))
return rtn, final_image
@staticmethod
def _to_bucket(ticks, symbol, initial_image):
rtn = {SYMBOL: symbol, VERSION: CHUNK_VERSION_NUMBER, COLUMNS: {}, COUNT: len(ticks)}
data = {}
rowmask = {}
start = to_dt(ticks[0]['index'])
end = to_dt(ticks[-1]['index'])
final_image = copy.copy(initial_image) if initial_image else {}
for i, t in enumerate(ticks):
if initial_image:
final_image.update(t)
for k, v in iteritems(t):
try:
if k != 'index':
rowmask[k][i] = 1
else:
v = TickStore._to_ms(v)
if data[k][-1] > v:
raise UnorderedDataException("Timestamps out-of-order: %s > %s" % (
ms_to_datetime(data[k][-1]), t))
data[k].append(v)
except KeyError:
if k != 'index':
rowmask[k] = np.zeros(len(ticks), dtype='uint8')
rowmask[k][i] = 1
data[k] = [v]
rowmask = dict([(k, Binary(lz4.compressHC(np.packbits(v).tostring())))
for k, v in iteritems(rowmask)])
for k, v in iteritems(data):
if k != 'index':
v = np.array(v)
v = TickStore._ensure_supported_dtypes(v)
rtn[COLUMNS][k] = {DATA: Binary(lz4.compressHC(v.tostring())),
DTYPE: TickStore._str_dtype(v.dtype),
ROWMASK: rowmask[k]}
if initial_image:
image_start = initial_image.get('index', start)
if image_start > start:
raise UnorderedDataException("Image timestamp is after first tick: %s > %s" % (
image_start, start))
start = min(start, image_start)
rtn[IMAGE_DOC] = {IMAGE_TIME: image_start, IMAGE: initial_image}
rtn[END] = end
rtn[START] = start
rtn[INDEX] = Binary(lz4.compressHC(np.concatenate(([data['index'][0]], np.diff(data['index']))).tostring()))
return rtn, final_image
def max_date(self, symbol):
"""
Return the maximum datetime stored for a particular symbol
Parameters
----------
symbol : `str`
symbol name for the item
"""
res = self._collection.find_one({SYMBOL: symbol}, projection={ID: 0, END: 1},
sort=[(START, pymongo.DESCENDING)])
return res[END]