Skip to content

Commit

Permalink
Support and test for non-sharded db.
Browse files Browse the repository at this point in the history
  • Loading branch information
shrutip committed Jan 31, 2014
1 parent 8ab0dd9 commit 68b51e9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 30 deletions.
42 changes: 28 additions & 14 deletions py/vtdb/keyrange.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,38 @@
# bind_vars for distrubuting the workload of streaming queries.


# Keyrange that spans the entire space, used
# for unsharded database.
NON_PARTIAL_KEYRANGE = ""
MIN_KEY = ''
MAX_KEY = ''

KIT_UNSET = ""
KIT_UINT64 = "uint64"
KIT_BYTES = "bytes"


class KeyRange(object):
kr_min = None
kr_max = None
Start = None
End = None

def __init__(self, kr):
if isinstance(kr, str):
kr = kr.split('-')
if kr == NON_PARTIAL_KEYRANGE:
self.Start = ""
self.End = ""
return
else:
kr = kr.split('-')
if not isinstance(kr, tuple) and not isinstance(kr, list) or len(kr) != 2:
raise dbexceptions.ProgrammingError("keyrange must be a list or tuple or a '-' separated str %s" % keyrange)
self.kr_min = kr[0].strip()
self.kr_max = kr[1].strip()
self.Start = kr[0].strip()
self.End = kr[1].strip()

def __str__(self):
return '%s-%s' % (self.kr_min, self.kr_max)
if self.Start == MIN_KEY and self.End == MAX_KEY:
return NON_PARTIAL_KEYRANGE
return '%s-%s' % (self.Start, self.End)


class StreamingTaskMap(object):
Expand Down Expand Up @@ -70,17 +88,13 @@ def _true_int_kr_value(kr_value):
return int(kr_value, base=16)


MIN_KEY = ''
MAX_KEY = ''

KIT_UNSET = ""
KIT_UINT64 = "uint64"
KIT_BYTES = "bytes"


# Compute the where clause and bind_vars for a given keyrange.
def create_where_clause_for_keyrange(keyrange, keyspace_col_name='keyspace_id', keyspace_col_type=KIT_UINT64):
if isinstance(keyrange, str):
# If the keyrange is for unsharded db, there is no
# where clause to add to or bind_vars to add to.
if keyrange == NON_PARTIAL_KEYRANGE:
return "", {}
keyrange = keyrange.split('-')

if not isinstance(keyrange, tuple) and not isinstance(keyrange, list) or len(keyrange) != 2:
Expand Down
39 changes: 23 additions & 16 deletions test/keyrange_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@
# and where clauses for streaming queries.

pkid_pack = struct.Struct('!Q').pack
int_shard_kid_map = {('-10'):[1, 100, 1000, 100000, 527875958493693904, 626750931627689502, 345387386794260318, 332484755310826578],
('10-20'):[1842642426274125671, 1326307661227634652, 1761124146422844620, 1661669973250483744],
('20-30'):[3361397649937244239, 3303511690915522723, 2444880764308344533, 2973657788686139039],
('30-40'):[3821005920507858605, 4575089859165626432, 3607090456016432961, 3979558375123453425],
('40-50'):[5129057445097465905, 5464969577815708398, 5190676584475132364, 5762096070688827561],
('50-60'):[6419540613918919447, 6867152356089593986, 6601838130703675400, 6132605084892127391],
('60-70'):[7251511061270371980, 7395364497868053835, 7814586147633440734, 7968977924086033834],
('70-80'):[8653665459643609079, 8419099072545971426, 9020726671664230611, 9064594986161620444],
('80-90'):[9767889778372766922, 9742070682920810358, 10296850775085416642, 9537430901666854108],
('90-a0'):[10440455099304929791, 11454183276974683945, 11185910247776122031, 10460396697869122981],
('a0-b0'):[11935085245138597119, 12115696589214223782, 12639360876311033978, 12548906240535188165],
('b0-c0'):[13379616110062597001, 12826553979133932576, 13288572810772383281, 13471801046560785347],
('c0-d0'):[14394342688314745188, 14639660031570920207, 14646353412066152016, 14186650213447467187],
('d0-e0'):[15397348460895960623, 16014223083986915239, 15058390871463382185, 15811857963302932363],
('e0-f0'):[17275711019497396001, 16979796627403646478, 16635982235308289704, 16906674090344806032],
('f0-'):[18229242992218358675, 17623451135465171527, 18333015752598164958, 17775908119782706671],
int_shard_kid_map = {'-10':[1, 100, 1000, 100000, 527875958493693904, 626750931627689502, 345387386794260318, 332484755310826578],
'10-20':[1842642426274125671, 1326307661227634652, 1761124146422844620, 1661669973250483744],
'20-30':[3361397649937244239, 3303511690915522723, 2444880764308344533, 2973657788686139039],
'30-40':[3821005920507858605, 4575089859165626432, 3607090456016432961, 3979558375123453425],
'40-50':[5129057445097465905, 5464969577815708398, 5190676584475132364, 5762096070688827561],
'50-60':[6419540613918919447, 6867152356089593986, 6601838130703675400, 6132605084892127391],
'60-70':[7251511061270371980, 7395364497868053835, 7814586147633440734, 7968977924086033834],
'70-80':[8653665459643609079, 8419099072545971426, 9020726671664230611, 9064594986161620444],
'80-90':[9767889778372766922, 9742070682920810358, 10296850775085416642, 9537430901666854108],
'90-a0':[10440455099304929791, 11454183276974683945, 11185910247776122031, 10460396697869122981],
'a0-b0':[11935085245138597119, 12115696589214223782, 12639360876311033978, 12548906240535188165],
'b0-c0':[13379616110062597001, 12826553979133932576, 13288572810772383281, 13471801046560785347],
'c0-d0':[14394342688314745188, 14639660031570920207, 14646353412066152016, 14186650213447467187],
'd0-e0':[15397348460895960623, 16014223083986915239, 15058390871463382185, 15811857963302932363],
'e0-f0':[17275711019497396001, 16979796627403646478, 16635982235308289704, 16906674090344806032],
'f0-':[18229242992218358675, 17623451135465171527, 18333015752598164958, 17775908119782706671],
}

# str_shard_kid_map is derived from int_shard_kid_map
Expand Down Expand Up @@ -113,5 +113,12 @@ def test_bind_values_for_str_keyspace(self):
self.assertGreaterEqual(keyspace_id.encode('hex'), bind_vars['keyspace_id0'])
self.assertLess(keyspace_id.encode('hex'), bind_vars['keyspace_id1'])

def test_bind_values_for_unsharded_keyspace(self):
stm = keyrange.create_streaming_task_map(1, 1)
self.assertEqual(len(stm.keyrange_list), 1)
where_clause, bind_vars = keyrange.create_where_clause_for_keyrange(stm.keyrange_list[0])
self.assertEqual(where_clause, "")
self.assertEqual(bind_vars, {})

if __name__ == '__main__':
utils.main()

0 comments on commit 68b51e9

Please sign in to comment.