WIP: Implementing sqlite objects storage #13

Draft
lee.miller wants to merge 11 commits from lee.miller/MiNode:sqlite into v0.3
2 changed files with 5 additions and 4 deletions
Showing only changes of commit 0059b77b58 - Show all commits

View File

@ -63,4 +63,3 @@ outgoing_connections = 8
connection_limit = 250
objects = {}
objects_lock = threading.Lock()

View File

@ -2,6 +2,7 @@
import os
import sqlite3
import threading
import time
from . import shared, structure
@ -12,6 +13,7 @@ sqlite3.threadsafety = 3
class Inventory():
"""sqlite inventory"""
def __init__(self):
self._lock = threading.Lock()
self._db = sqlite3.connect(
os.path.join(shared.data_directory, 'objects.dat'),
check_same_thread=False
@ -52,7 +54,7 @@ class Inventory():
def cleanup(self):
"""Remove expired objects"""
with shared.objects_lock:
with self._lock:
self._db.execute(
'DELETE FROM objects WHERE expires < ?',
(int(time.time()) - 3 * 3600,)
@ -121,13 +123,13 @@ class Inventory():
return item
def __delitem__(self, vector):
with shared.objects_lock: # KeyError
with self._lock: # KeyError
self._db.execute('DELETE FROM objects WHERE vector = ?', (vector,))
self._db.commit()
self.rowid = len(self)
def __setitem__(self, vector, obj):
with shared.objects_lock:
with self._lock:
cur = self._db.execute(
'INSERT INTO objects VALUES (?,?,?,?,?,?,?,?)', (
vector, obj.expires_time, obj.object_type, obj.version,