Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache size to avoid delays on big db files #125

Open
twall opened this issue Dec 2, 2020 · 3 comments
Open

Cache size to avoid delays on big db files #125

twall opened this issue Dec 2, 2020 · 3 comments
Labels

Comments

@twall
Copy link

twall commented Dec 2, 2020

I use mostly read-only dbs, so the time spent sizing them is wasted. Even in the case of writable dbs, caching the last known size and timestamp and comparing against the db file mtime can be of use.

Something like this:

@property
def _cached_size(self):
    tablename = "meta"
    GET_ITEM = f'SELECT value FROM "{tablename}" WHERE key = ?'
    size = self._select_one(GET_ITEM, ("size",))
    ts = self._select_one(GET_ITEM, ("ts",))
    fts = os.path.getmtime(self.filename)
    return ts > fts and size

@_cached_size.setter
def _cached_size(self, size):
    tablename = "meta"
    MAKE_TABLE = f'CREATE TABLE IF NOT EXISTS "{tablename}" (key TEXT PRIMARY KEY, value BLOB)'
    self.conn.execute(MAKE_TABLE)
    ADD_ITEM = f'REPLACE INTO "{tablename}" (key, value) VALUES (?,?)'
    self.conn.execute(ADD_ITEM, ("size", self.encode(size)))
    ADD_ITEM = f'REPLACE INTO "{tablename}" (key, value) VALUES (?,?)'
    # Since file modtime will be updated _after_ writing the value, add some slack
    delay = 0.5
    while True:
        ts = time.time() + delay
        self.conn.execute(ADD_ITEM, ("ts", self.encode(ts)))
        self.conn.commit()
        if os.path.getmtime(self.filename) < ts:
            break
        delay += 0.5
@piskvorky
Copy link
Owner

piskvorky commented Dec 2, 2020

Interesting. Can you expand the motivation? In I use mostly read-only dbs, so the time spent sizing them is wasted., what exactly is wasted, how much of it, how does it affect you, how do you expect the fix will affect others?

Adding an extra meta table to help with caching is possible, but it's additional complexity so needs to be well motivated (and documented).

@twall
Copy link
Author

twall commented Dec 2, 2020 via email

@twall
Copy link
Author

twall commented Dec 2, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants