Files
@ 8bb6a904d50b
Branch filter:
Location: Morevna/src/db.py - annotation
8bb6a904d50b
1.0 KiB
text/x-python
client too can now use saved hash tree
95bfc9d72133 95bfc9d72133 95bfc9d72133 95bfc9d72133 95bfc9d72133 6f31f20feb06 95bfc9d72133 95bfc9d72133 95bfc9d72133 95bfc9d72133 95bfc9d72133 95bfc9d72133 95bfc9d72133 95bfc9d72133 95bfc9d72133 95bfc9d72133 95bfc9d72133 95bfc9d72133 95bfc9d72133 95bfc9d72133 95bfc9d72133 95bfc9d72133 95bfc9d72133 95bfc9d72133 95bfc9d72133 95bfc9d72133 95bfc9d72133 95bfc9d72133 95bfc9d72133 95bfc9d72133 95bfc9d72133 95bfc9d72133 95bfc9d72133 95bfc9d72133 95bfc9d72133 95bfc9d72133 95bfc9d72133 95bfc9d72133 95bfc9d72133 | import sqlite3
import time
COMMIT_TIME=2 # s
class DB:
def __init__(self,filename):
self.filename=filename
self.connection=sqlite3.connect(self.filename)
self.connection.row_factory=sqlite3.Row
self.commitTime=time.time()
def setup(self):
# !! err if exists
self.connection.execute("""create table `hashes` (
`sector_id` integer primary key,
`sector_hash` blob not null,
`updated` integer
)""")
self.connection.commit()
## Inserts hash into the DB.
#
# @param sectorHash bytes object containing the hash
def insertHash(self,sectorId,sectorHash):
# !! if not connection
res=self.connection.execute("""insert or replace into `hashes` (`sector_id`, `sector_hash`, `updated`)
values (?, ?, strftime('%s','now'))""", (sectorId,sectorHash))
t=time.time()
if t-self.commitTime > COMMIT_TIME:
self.connection.commit()
self.commitTime=t
return res
def listHashes(self):
# !! if not connection
return self.connection.execute("""select * from `hashes` order by `sector_id` asc""")
|