def scan_entries(self, start, end): """Retrieve log entries. Args: start: index of the first entry to retrieve. end: index of the last entry to retrieve. Yields: client_pb2.EntryResponse protos Raises: KeyError: an entry with a sequence number in the range does not exist. """ with self.__mgr.get_connection() as conn: cursor = conn.cursor() next_id = start for row in cursor.execute( "SELECT id, entry FROM entries WHERE id " "BETWEEN ? and ? ORDER BY id ASC", (start, end)): if row["id"] != next_id: raise database.KeyError("No such entry: %d" % next_id) entry = client_pb2.EntryResponse() entry.ParseFromString(str(row["entry"])) yield entry next_id += 1 if next_id != end + 1: raise database.KeyError("No such entry: %d" % next_id)
def create_storage(self, log_server): """Create a SQLiteTempDB object pointing to the temporary storage of a given log server. If the temporary storage does not exist, creates one. Args: log_server: the server name. """ with self.__mgr.get_connection() as conn: cursor = conn.cursor() try: database_name = self.__get_db_name(cursor, log_server) except database.KeyError: try: cursor.execute( "INSERT INTO database_mapping(server_name) " "VALUES (?)", (log_server, )) database_name = self.__database_id_to_name( cursor.lastrowid) except sqlite3.IntegrityError as e: raise database.KeyError("Failed to create a table mapping " "for server %s: is a concurrent " "factory running?\n%s" % (log_server, e)) return SQLiteTempDB( sqlitecon.SQLiteConnectionManager(self.__database_dir + "/" + database_name))
def __get_db_name(self, cursor, log_server): cursor.execute("SELECT id from database_mapping WHERE server_name = ?", (log_server, )) row = cursor.fetchone() if row is None: raise database.KeyError("No database for log server %s" % log_server) return self.__database_id_to_name(row["id"])
def _get_log_id(self, conn, log_server): res = conn.execute("SELECT id FROM logs WHERE log_server = ?", (log_server,)) try: log_id = res.next() except StopIteration: raise database.KeyError("Unknown log server: %s", log_server) return log_id[0]
def store_entries(self, entries): """Batch store log entries. Args: entries: an iterable of (entry_number, client_pb2.EntryResponse) tuples """ with self.__mgr.get_connection() as conn: cursor = conn.cursor() serialized_entries = map( lambda x: (x[0], sqlite3.Binary(x[1].SerializeToString())), entries) try: cursor.executemany( "INSERT OR REPLACE INTO entries(id, entry) VALUES " "(?, ?)", serialized_entries) except sqlite3.IntegrityError as e: raise database.KeyError("Failed to insert entries: an entry " "with the given sequence number " "already exists\n%s" % e)
def scan_entries(self, first, last): raise database.KeyError("boom!")