Skip to content

Commit c00a533

Browse files
author
Artjoms Iskovs
committed
Added a test for when all interesting chunks start inside the range and fixed a corner case
1 parent 87947ab commit c00a533

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

arctic/tickstore/tickstore.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ def _mongo_date_range_query(self, symbol, date_range):
166166
if date_range.start:
167167
assert date_range.start.tzinfo
168168
start = date_range.start
169+
170+
# If all chunks start inside of the range, we default to capping to our
171+
# range so that we don't fetch any chunks from the beginning of time
172+
start_range['$gte'] = start
173+
169174
match = self._symbol_query(symbol)
170175
match.update({'s': {'$lte': start}})
171176

@@ -186,18 +191,12 @@ def _mongo_date_range_query(self, symbol, date_range):
186191
try:
187192
for candidate in result:
188193
chunk = self._collection.find_one({'s': candidate['start'], 'sy': candidate['_id']}, {'e': 1})
189-
if chunk['e'].replace(tzinfo=mktz('UTC')) > start:
190-
first_dt = candidate['start'].replace(tzinfo=mktz('UTC'))
194+
if chunk['e'].replace(tzinfo=mktz('UTC')) >= start:
195+
start_range['$gte'] = candidate['start'].replace(tzinfo=mktz('UTC'))
191196
break
192197
except StopIteration:
193198
pass
194199

195-
if first_dt:
196-
start_range['$gte'] = first_dt
197-
else:
198-
# If all chunks start inside of the range, make sure
199-
# we don't fetch everything
200-
start_range['$gte'] = start
201200

202201
# Find the end bound
203202
if date_range.end:

tests/integration/tickstore/test_ts_read.py

+43
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,49 @@ def test_read_spanning_chunks(tickstore_lib):
453453
{'s': {'$gte': dt(2013, 6, 1, 12, 30, tzinfo=mktz('UTC')), '$lte': dt(2013, 6, 1, 15, 0, tzinfo=mktz('UTC'))}}
454454

455455

456+
def test_read_inside_range(tickstore_lib):
457+
SYM1_DATA = [
458+
{'a': 1.,
459+
'b': 2.,
460+
'index': dt(2013, 6, 1, 0, 00, tzinfo=mktz('UTC'))
461+
},
462+
{'a': 3.,
463+
'b': 4.,
464+
'index': dt(2013, 6, 1, 1, 00, tzinfo=mktz('UTC'))
465+
},
466+
# Chunk boundary here
467+
{'a': 5.,
468+
'b': 6.,
469+
'index': dt(2013, 6, 1, 14, 00, tzinfo=mktz('UTC'))
470+
}
471+
]
472+
SYM2_DATA = [
473+
{'a': 7.,
474+
'b': 8.,
475+
'index': dt(2013, 6, 1, 12, 30, tzinfo=mktz('UTC'))
476+
},
477+
{'a': 9.,
478+
'b': 10.,
479+
'index': dt(2013, 6, 1, 13, 30, tzinfo=mktz('UTC'))
480+
},
481+
# Chunk boundary here
482+
{'a': 11.,
483+
'b': 12.,
484+
'index': dt(2013, 6, 1, 14, 30, tzinfo=mktz('UTC'))
485+
}
486+
]
487+
tickstore_lib._chunk_size = 2
488+
tickstore_lib.write('SYM1', SYM1_DATA)
489+
tickstore_lib.write('SYM2', SYM2_DATA)
490+
491+
# If there are no chunks spanning the range, we still cap the start range so that we don't
492+
# fetch SYM1's 0am--1am chunk
493+
assert tickstore_lib._mongo_date_range_query(
494+
['SYM1', 'SYM2'],
495+
date_range=DateRange(dt(2013, 6, 1, 10, 0, tzinfo=mktz('UTC')),
496+
dt(2013, 6, 1, 15, 0, tzinfo=mktz('UTC')))) == \
497+
{'s': {'$gte': dt(2013, 6, 1, 10, 0, tzinfo=mktz('UTC')), '$lte': dt(2013, 6, 1, 15, 0, tzinfo=mktz('UTC'))}}
498+
456499
def test_read_longs(tickstore_lib):
457500
DUMMY_DATA = [
458501
{'a': 1,

0 commit comments

Comments
 (0)