piskvorky / sqlitedict

Persistent dict, backed by sqlite3 and pickle, multithread-safe.
Apache License 2.0
1.17k stars 131 forks source link

Feature Proposal: Key Prefix Scan #154

Open robbitt07 opened 2 years ago

robbitt07 commented 2 years ago

Looking for similar prefix scan functionality like what is available in LevelDB or BadgerDB. Basically looking for the option to scan a set of prefixes.

    def prefix_iteritems(self, prefix: str):
        GET_ITEMS = 'SELECT key, value FROM "%s" WHERE key LIKE ? ORDER BY rowid' % self.tablename
        for key, value in self.conn.select(GET_ITEMS, (f"{prefix}%",)):
            yield key, self.decode(value)

Keys

"p:60606"
"p:64112"
"p:90210"

Query

db.prefix_iteritems("p:6")
>>> ("p:60606", "p:64112", )
db.prefix_iteritems("p:64")
>>> ("p:64112", )