def get_index(self, space, index): _space = yield from self.get_space(space) try: return _space.indexes[index] except KeyError: pass if not self.con.connected: yield from self.con.connect() with (yield from self.con.lock): if index in _space.indexes: return _space.indexes[index] _index = (tarantool.const.INDEX_INDEX_NAME if isinstance(index, str) else tarantool.const.INDEX_INDEX_PRIMARY) array = yield from self.con.select(tarantool.const.SPACE_INDEX, [_space.sid, index], index=_index) if len(array) > 1: raise SchemaError('Some strange output from server: \n' + array) if len(array) == 0 or not len(array[0]): temp_name = ('name' if isinstance(index, str) else 'id') raise SchemaError( "There's no index with {2} '{0}' in space '{1}'".format( index, _space.name, temp_name)) array = array[0] return SchemaIndex(array, _space)
def get_space(self, space): try: return self.schema[space] except KeyError: pass if not self.con.connected: yield from self.con.connect() with (yield from self.con.lock): if space in self.schema: return self.schema[space] _index = (tarantool.const.INDEX_SPACE_NAME if isinstance(space, str) else tarantool.const.INDEX_SPACE_PRIMARY) array = yield from self.con.select(tarantool.const.SPACE_SPACE, space, index=_index) if len(array) > 1: raise SchemaError('Some strange output from server: \n' + array) if len(array) == 0 or not len(array[0]): temp_name = ('name' if isinstance(space, str) else 'id') raise SchemaError( "There's no space with {1} '{0}'".format(space, temp_name)) array = array[0] return SchemaSpace(array, self.schema)
def fetch_space(self, space): space_row = self.fetch_space_from(space) if len(space_row) > 1: # We have selected more than one space, it's strange raise SchemaError('Some strange output from server: \n' + str(space_row)) elif len(space_row) == 0 or not len(space_row[0]): # We can't find space with this name or id temp_name = 'name' if isinstance(space, six.string_types) else 'id' errmsg = "There's no space with {1} '{0}'".format(space, temp_name) raise SchemaError(errmsg) space_row = space_row[0] return SchemaSpace(space_row, self.schema)
def fetch_index_from(self, space, index): _index = None if isinstance(index, six.string_types): _index = const.INDEX_INDEX_NAME else: _index = const.INDEX_INDEX_PRIMARY _key_tuple = None if space is None and index is None: _key_tuple = () elif space is not None and index is None: _key_tuple = (space) elif space is not None and index is not None: _key_tuple = (space, index) else: raise SchemaError("Bad arguments for schema resolving") index_row = None try: # Try to fetch from '_vindex' index_row = self.con.select(const.SPACE_VINDEX, _key_tuple, index=_index) except DatabaseError as e: # if space can't be found, then user is using old version of # tarantool, try again with '_index' if e.args[0] != 36: raise if index_row is None: # Try to fetch from '_index' index_row = self.con.select(const.SPACE_INDEX, _key_tuple, index=_index) return index_row
def __init__(self, index_row, space): self.iid = index_row[1] self.name = index_row[2] self.name = to_unicode(index_row[2]) self.index = index_row[3] self.unique = index_row[4] self.parts = [] try: parts_raw = to_unicode_recursive(index_row[5], 3) except RecursionError as e: errmsg = 'Unexpected index parts structure: ' + str(e) raise SchemaError(errmsg) if isinstance(parts_raw, (list, tuple)): for val in parts_raw: if isinstance(val, dict): self.parts.append((val['field'], val['type'])) else: self.parts.append((val[0], val[1])) else: for i in range(parts_raw): self.parts.append((to_unicode(index_row[5 + 1 + i * 2]), to_unicode(index_row[5 + 2 + i * 2]))) self.space = space self.space.indexes[self.iid] = self if self.name: self.space.indexes[self.name] = self
def get_space(self, space): try: return self.schema[space] except KeyError: pass _index = (const.INDEX_SPACE_NAME if isinstance(space, basestring) else const.INDEX_SPACE_PRIMARY) array = self.con.select(const.SPACE_SPACE, space, index=_index) if len(array) > 1: raise SchemaError('Some strange output from server: \n' + array) elif len(array) == 0 or not len(array[0]): temp_name = ('name' if isinstance(space, basestring) else 'id') raise SchemaError("There's no space with {1} '{0}'".format( space, temp_name)) array = array[0] return SchemaSpace(array, self.schema)
def fetch_index(self, space_object, index): index_row = self.fetch_index_from(space_object.sid, index) if len(index_row) > 1: # We have selected more than one index, it's strange raise SchemaError('Some strange output from server: \n' + str(index_row)) elif len(index_row) == 0 or not len(index_row[0]): # We can't find index with this name or id temp_name = 'name' if isinstance(index, six.string_types) else 'id' errmsg = ("There's no index with {2} '{0}'" " in space '{1}'").format(index, space_object.name, temp_name) raise SchemaError(errmsg) index_row = index_row[0] return SchemaIndex(index_row, space_object)
def get_index(self, space, index): _space = self.get_space(space) try: return _space.indexes[index] except KeyError: pass _index = (const.INDEX_INDEX_NAME if isinstance(index, basestring) else const.INDEX_INDEX_PRIMARY) array = self.con.select(const.SPACE_INDEX, [_space.sid, index], index=_index) if len(array) > 1: raise SchemaError('Some strange output from server: \n' + array) elif len(array) == 0 or not len(array[0]): temp_name = ('name' if isinstance(index, basestring) else 'id') raise SchemaError( "There's no index with {2} '{0}' in space '{1}'".format( index, _space.name, temp_name)) array = array[0] return SchemaIndex(array, _space)
def get_field(self, space, field): _space = self.get_space(space) try: return _space.format[field] except: tp = 'name' if isinstance(field, string_types) else 'id' errmsg = "There's no field with {2} '{0}' in space '{1}'".format( field, _space.name, tp) raise SchemaError(errmsg) return field
def __init__(self, space_row, schema): self.sid = space_row[0] self.arity = space_row[1] self.name = to_unicode(space_row[2]) self.indexes = {} self.schema = schema self.schema[self.sid] = self if self.name: self.schema[self.name] = self self.format = dict() try: format_raw = to_unicode_recursive(space_row[6], 3) except RecursionError as e: errmsg = 'Unexpected space format structure: ' + str(e) raise SchemaError(errmsg) for part_id, part in enumerate(format_raw): part['id'] = part_id self.format[part['name']] = part self.format[part_id] = part