From 4a34f7ccacbb3206c894fd4a0a3262a7157d124e Mon Sep 17 00:00:00 2001 From: Lee Miller Date: Sun, 8 Sep 2024 23:56:29 +0300 Subject: [PATCH] Handle sqlite3 exceptions in __setitem__ instead of checking presence --- minode/connection.py | 2 +- minode/sql.py | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/minode/connection.py b/minode/connection.py index c18a619..115eef5 100644 --- a/minode/connection.py +++ b/minode/connection.py @@ -477,7 +477,7 @@ class Connection(ConnectionBase): logging.debug('%s:%s -> %s', self.host_print, self.port, obj) self.vectors_requested.pop(obj.vector, None) self.vectors_to_get.discard(obj.vector) - if obj.is_valid() and obj.vector not in shared.objects: + if obj.is_valid(): shared.objects[obj.vector] = obj if ( obj.object_type == shared.i2p_dest_obj_type diff --git a/minode/sql.py b/minode/sql.py index 7ca22ac..0035608 100644 --- a/minode/sql.py +++ b/minode/sql.py @@ -126,11 +126,14 @@ class Inventory(): def __setitem__(self, vector, obj): with shared.objects_lock: - cur = self._db.execute( - 'INSERT INTO objects VALUES (?,?,?,?,?,?,?,?)', ( - vector, obj.expires_time, obj.object_type, obj.version, - obj.stream_number, obj.tag, obj.data, obj.offset - )) + try: + cur = self._db.execute( + 'INSERT INTO objects VALUES (?,?,?,?,?,?,?,?)', ( + vector, obj.expires_time, obj.object_type, obj.version, + obj.stream_number, obj.tag, obj.data, obj.offset + )) + except (sqlite3.DatabaseError, sqlite3.IntegrityError): + return self._db.commit() self.rowid = cur.lastrowid