def _AllocateIds(self, references): conn = self._GetConnection() try: full_keys = [] for key in references: datastore_stub_util.CheckAppId(self._trusted, self._app_id, key.app()) prefix = self._GetTablePrefix(key) last_element = key.path().element_list()[-1] if last_element.id() or last_element.has_name(): for el in key.path().element_list(): if el.id(): count, id_space = datastore_stub_util.IdToCounter( el.id()) table, _ = self.__id_counter_tables[id_space] self.__AdvanceIdCounter(conn, prefix, count, table) else: count, _ = self.__AllocateIdsFromBlock( conn, prefix, 1, self.__id_map_scattered, 'ScatteredIdCounters') last_element.set_id( datastore_stub_util.ToScatteredId(count)) full_keys.append(key) return full_keys finally: self._ReleaseConnection(conn)
def _AllocateIds(self, reference, size=1, max_id=None): conn = self._GetConnection() try: datastore_stub_util.CheckAppId(reference.app(), self._trusted, self._app_id) datastore_stub_util.Check(not (size and max_id), 'Both size and max cannot be set.') prefix = self._GetTablePrefix(reference) if size: datastore_stub_util.Check(size > 0, 'Size must be greater than 0.') next_id, block_size = self.__id_map.get(prefix, (0, 0)) if not block_size: block_size = (size / 1000 + 1) * 1000 c = conn.execute( 'SELECT next_id FROM IdSeq WHERE prefix = ? LIMIT 1', (prefix, )) next_id = c.fetchone()[0] c = conn.execute( 'UPDATE IdSeq SET next_id = next_id + ? WHERE prefix = ?', (block_size, prefix)) assert c.rowcount == 1 if size > block_size: c = conn.execute( 'SELECT next_id FROM IdSeq WHERE prefix = ? LIMIT 1', (prefix, )) start = c.fetchone()[0] c = conn.execute( 'UPDATE IdSeq SET next_id = next_id + ? WHERE prefix = ?', (size, prefix)) assert c.rowcount == 1 else: start = next_id next_id += size block_size -= size self.__id_map[prefix] = (next_id, block_size) end = start + size - 1 else: datastore_stub_util.Check( max_id >= 0, 'Max must be greater than or equal to 0.') c = conn.execute( 'SELECT next_id FROM IdSeq WHERE prefix = ? LIMIT 1', (prefix, )) start = c.fetchone()[0] if max_id and max_id >= start: c = conn.execute( 'UPDATE IdSeq SET next_id = ? WHERE prefix = ?', (max_id + 1, prefix)) assert c.rowcount == 1 end = max(max_id, start - 1) return (long(start), long(end)) finally: self._ReleaseConnection(conn)
def _AllocateSequentialIds(self, reference, size=1, max_id=None): conn = self._GetConnection() try: datastore_stub_util.CheckAppId(self._trusted, self._app_id, reference.app()) datastore_stub_util.Check(not (size and max_id), 'Both size and max cannot be set.') prefix = self._GetTablePrefix(reference) if size: start, end = self.__AllocateIdsFromBlock( conn, prefix, size, self.__id_map_sequential, 'IdSeq') else: start, end = self.__AdvanceIdCounter(conn, prefix, max_id, 'IdSeq') return long(start), long(end) finally: self._ReleaseConnection(conn)