Skip to content

Commit ee14c9f

Browse files
committed
faster fetching space and index by name
1 parent b2e3988 commit ee14c9f

File tree

1 file changed

+18
-33
lines changed

1 file changed

+18
-33
lines changed

Diff for: tarantool/schema.py

+18-33
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ def __init__(self, array, space):
1919
self.parts.append((array[5+1+i*2], array[5+2+i*2]))
2020
self.space = space
2121
self.space.indexes[self.iid] = self
22+
if self.name:
23+
self.space.indexes[self.name] = self
2224

2325
def flush(self):
2426
del self.space.indexes[self.iid]
27+
if self.name:
28+
del self.space.indexes[self.name]
2529

2630
class SchemaSpace(object):
2731
def __init__(self, array, schema):
@@ -31,44 +35,24 @@ def __init__(self, array, schema):
3135
self.indexes = {}
3236
self.schema = schema
3337
self.schema[self.sid] = self
38+
if self.name:
39+
self.schema[self.name] = self
3440

3541
def flush(self):
3642
del self.schema[self.sid]
43+
if self.name:
44+
del self.schema[self.name]
3745

3846
class Schema(object):
3947
def __init__(self, con):
4048
self.schema = {}
4149
self.con = con
4250

43-
def find_local_space(self, space):
44-
if isinstance(space, basestring):
45-
for _, val in self.schema.iteritems():
46-
if val.name == space:
47-
return val
48-
return None
51+
def get_space(self, space):
4952
try:
5053
return self.schema[space]
5154
except KeyError:
52-
return None
53-
54-
def find_local_index(self, space, index):
55-
space = self.find_local_space(space)
56-
if space is None:
57-
return None
58-
if isinstance(index, basestring):
59-
for _, val in space.indexes.iteritems():
60-
if val.name == index:
61-
return val
62-
return None
63-
try:
64-
return space.indexes[index]
65-
except KeyError:
66-
return None
67-
68-
def get_space(self, space):
69-
_space = self.find_local_space(space)
70-
if _space is not None:
71-
return _space
55+
pass
7256
_index = (const.INDEX_SPACE_NAME if isinstance(space, basestring) else const.INDEX_SPACE_PRIMARY)
7357

7458
array = self.con.select(const.SPACE_SPACE, space, index=_index)
@@ -82,21 +66,22 @@ def get_space(self, space):
8266
return SchemaSpace(array, self.schema)
8367

8468
def get_index(self, space, index):
85-
_index = self.find_local_index(space, index)
86-
if _index is not None:
87-
return _index
88-
space = self.get_space(space)
69+
_space = self.get_space(space)
70+
try:
71+
return _space.indexes[index]
72+
except KeyError:
73+
pass
8974
_index = (const.INDEX_INDEX_NAME if isinstance(index, basestring) else const.INDEX_INDEX_PRIMARY)
9075

91-
array = self.con.select(const.SPACE_INDEX, [space.sid, index], index=_index)
76+
array = self.con.select(const.SPACE_INDEX, [_space.sid, index], index=_index)
9277
if len(array) > 1:
9378
raise SchemaError('Some strange output from server: \n'+array)
9479
elif len(array) == 0 or not len(array[0]):
9580
temp_name = ('name' if isinstance(index, basestring) else 'id')
9681
raise SchemaError('There\'s no index with {2} \'{0}\' '
97-
'in space \'{1}\''.format(index, space.name, temp_name))
82+
'in space \'{1}\''.format(index, _space.name, temp_name))
9883
array = array[0]
99-
return SchemaIndex(array, space)
84+
return SchemaIndex(array, _space)
10085

10186
def flush(self):
10287
self.schema.clear()

0 commit comments

Comments
 (0)