Skip to content

Commit 193b2ef

Browse files
committed
Check key existence in the SQLite key-value store with a lightweight SELECT ... LIMIT 1 instead of fetching the whole value.
Signed-off-by: Zhiwei Liang <zhiwei.liang@zliang.me>
1 parent f8a4c2a commit 193b2ef

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

crates/key-value-spin/src/store.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,15 @@ impl Store for SqliteStore {
159159
}
160160

161161
async fn exists(&self, key: &str) -> Result<bool, Error> {
162-
Ok(self.get(key, usize::MAX).await?.is_some())
162+
task::block_in_place(|| {
163+
self.connection
164+
.lock()
165+
.unwrap()
166+
.prepare_cached("SELECT 1 FROM spin_key_value WHERE store=$1 AND key=$2 LIMIT 1")
167+
.map_err(log_error)?
168+
.exists([&self.name, key])
169+
.map_err(log_error)
170+
})
163171
}
164172

165173
async fn get_keys(&self, max_result_bytes: usize) -> Result<Vec<String>, Error> {

0 commit comments

Comments
 (0)