Skip to content

Commit

Permalink
feat(ext/kv): support key expiration in remote backend (#20688)
Browse files Browse the repository at this point in the history
This patch adds support for [key
expiration](https://docs.deno.com/kv/manual/key_expiration) in the
remote backend.
  • Loading branch information
losfair committed Sep 27, 2023
1 parent f0a022b commit f58a400
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
1 change: 1 addition & 0 deletions ext/kv/proto/datapath.proto
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 48,7 @@ message KvMutation {
bytes key = 1;
KvValue value = 2;
KvMutationType mutation_type = 3;
int64 expire_at_ms = 4;
}

message KvValue {
Expand Down
19 changes: 12 additions & 7 deletions ext/kv/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 230,7 @@ impl<P: RemoteDbHandlerPermissions> Database for RemoteDb<P> {
})
})
.collect::<anyhow::Result<_>>()?,
kv_mutations: write
.mutations
.into_iter()
.map(|x| encode_mutation(x.key, x.kind))
.collect(),
kv_mutations: write.mutations.into_iter().map(encode_mutation).collect(),
enqueues: vec![],
};

Expand Down Expand Up @@ -333,32 329,41 @@ fn encode_value(value: crate::Value) -> pb::KvValue {
}
}

fn encode_mutation(key: Vec<u8>, mutation: MutationKind) -> pb::KvMutation {
match mutation {
fn encode_mutation(m: crate::KvMutation) -> pb::KvMutation {
let key = m.key;
let expire_at_ms =
m.expire_at.and_then(|x| i64::try_from(x).ok()).unwrap_or(0);

match m.kind {
MutationKind::Set(x) => pb::KvMutation {
key,
value: Some(encode_value(x)),
mutation_type: pb::KvMutationType::MSet as _,
expire_at_ms,
},
MutationKind::Delete => pb::KvMutation {
key,
value: Some(encode_value(crate::Value::Bytes(vec![]))),
mutation_type: pb::KvMutationType::MClear as _,
expire_at_ms,
},
MutationKind::Max(x) => pb::KvMutation {
key,
value: Some(encode_value(x)),
mutation_type: pb::KvMutationType::MMax as _,
expire_at_ms,
},
MutationKind::Min(x) => pb::KvMutation {
key,
value: Some(encode_value(x)),
mutation_type: pb::KvMutationType::MMin as _,
expire_at_ms,
},
MutationKind::Sum(x) => pb::KvMutation {
key,
value: Some(encode_value(x)),
mutation_type: pb::KvMutationType::MSum as _,
expire_at_ms,
},
}
}
Expand Down

0 comments on commit f58a400

Please sign in to comment.