More efficient garbage collection of cache.

Iterates backwards from first entry of this generation, and forward from
last entry.
This commit is contained in:
Bill Thiede 2020-02-17 14:54:39 -08:00
parent 914e30365e
commit b801954599

View File

@ -11,6 +11,8 @@ use log::info;
use log::warn;
use photos::schemas::Album;
use photos::schemas::MediaItem;
use rocksdb::Direction;
use rocksdb::IteratorMode;
use rocksdb::DB;
// Used to ensure DB is invalidated after schema changes.
@ -47,7 +49,16 @@ impl Library {
}
fn gc(generation: &str, db: &DB) -> Result<(), rocksdb::Error> {
let gen = format!("{}/", generation);
for (k, _v) in db.iterator(rocksdb::IteratorMode::Start) {
// '0' is the next character after '/', so iterator's starting there would be after the
// last `gen` entry.
let next_gen = format!("{}0", generation);
for (k, _v) in db.iterator(IteratorMode::From(gen.as_bytes(), Direction::Reverse)) {
if !k.starts_with(gen.as_bytes()) {
info!("deleting stale key: {}", String::from_utf8_lossy(&k));
db.delete(k)?;
}
}
for (k, _v) in db.iterator(IteratorMode::From(next_gen.as_bytes(), Direction::Forward)) {
if !k.starts_with(gen.as_bytes()) {
info!("deleting stale key: {}", String::from_utf8_lossy(&k));
db.delete(k)?;