X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=apt_p2p_Khashmir%2Fdb.py;h=a7962566cc5a07eb99c0744b65eb81389f8b45bc;hb=aa0f99580b06354d1affbef134d5763b534026b6;hp=47e974cf62122bab33cd4b4a4868a92e873bf822;hpb=7b1167d8ce780312d3689c9309c7e9c64060c085;p=quix0rs-apt-p2p.git diff --git a/apt_p2p_Khashmir/db.py b/apt_p2p_Khashmir/db.py index 47e974c..a796256 100644 --- a/apt_p2p_Khashmir/db.py +++ b/apt_p2p_Khashmir/db.py @@ -148,6 +148,24 @@ class DB: c.execute("DELETE FROM kv WHERE last_refresh < ?", (t, )) self.conn.commit() + def keyStats(self): + """Count the total number of keys and values in the database. + @rtype: (C{int}, C{int}) + @return: the number of distinct keys and total values in the database + """ + c = self.conn.cursor() + c.execute("SELECT COUNT(value) as num_values FROM kv") + values = 0 + row = c.fetchone() + if row: + values = row[0] + c.execute("SELECT COUNT(key) as num_keys FROM (SELECT DISTINCT key FROM kv)") + keys = 0 + row = c.fetchone() + if row: + keys = row[0] + return keys, values + class TestDB(unittest.TestCase): """Tests for the khashmir database.""" @@ -164,6 +182,7 @@ class TestDB(unittest.TestCase): def test_Value(self): self.store.storeValue(self.key, self.key) + self.failUnlessEqual(self.store.countValues(self.key), 1) val = self.store.retrieveValues(self.key) self.failUnlessEqual(len(val), 1) self.failUnlessEqual(val[0], self.key)