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

feat: KV Connect protocol spec #6

Merged
merged 2 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ This repository contains two crates:

These crates are used by the `deno_kv` crate in the Deno repository to provide a
JavaScript API for interacting with Deno KV.

The Deno KV Connect protocol used for communication between Deno and a remote KV
database is defined in [`/proto/kv-connect.md`](./proto/kv-connect.md).
8 changes: 8 additions & 0 deletions proto/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# denokv_proto

The `denokv_proto` crate provides foundational types and structures related to
Deno KV protocol.

It contains the protobuf definitions for the KV Connect protocol, as well as the
[KV Connect specification](./kv-connect.md). It also contains a `Database` trait
that can be implemented to provide a Deno KV compatible database.
4 changes: 2 additions & 2 deletions proto/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ use std::io;
use std::path::PathBuf;

fn main() -> io::Result<()> {
println!("cargo:rerun-if-changed=./datapath.proto");
println!("cargo:rerun-if-changed=./schema/datapath.proto");

let descriptor_path =
PathBuf::from(env::var("OUT_DIR").unwrap()).join("proto_descriptor.bin");

prost_build::Config::new()
.file_descriptor_set_path(&descriptor_path)
.compile_well_known_types()
.compile_protos(&["datapath.proto"], &["protobuf/"])?;
.compile_protos(&["schema/datapath.proto"], &["protobuf/"])?;

Ok(())
}
97 changes: 0 additions & 97 deletions proto/datapath.proto

This file was deleted.

46 changes: 23 additions & 23 deletions proto/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ type Versionstamp = [u8; 10];
/// A key-value entry with a versionstamp.
pub struct KvEntry {
pub key: Vec<u8>,
pub value: Value,
pub value: KvValue,
pub versionstamp: Versionstamp,
}

Expand All @@ -194,7 +194,7 @@ pub struct KvEntry {
///
/// - **Bytes**: an arbitrary byte array.
/// - **U64**: a 64-bit unsigned integer.
pub enum Value {
pub enum KvValue {
V8(Vec<u8>),
Bytes(Vec<u8>),
U64(u64),
Expand All @@ -219,15 +219,15 @@ pub enum Value {
/// `mutations` field. The order of checks is not specified, and is also not
/// important because this ordering is un-observable.
pub struct AtomicWrite {
pub checks: Vec<KvCheck>,
pub mutations: Vec<KvMutation>,
pub checks: Vec<Check>,
pub mutations: Vec<Mutation>,
pub enqueues: Vec<Enqueue>,
}

/// A request to perform a check on a key in the database. The check is not
/// performed on the value of the key, but rather on the versionstamp of the
/// key.
pub struct KvCheck {
pub struct Check {
pub key: Vec<u8>,
pub versionstamp: Option<Versionstamp>,
}
Expand All @@ -237,7 +237,7 @@ pub struct KvCheck {
///
/// The type of mutation is specified by the `kind` field. The action performed
/// by each mutation kind is specified in the docs for [MutationKind].
pub struct KvMutation {
pub struct Mutation {
pub key: Vec<u8>,
pub kind: MutationKind,
pub expire_at: Option<DateTime<Utc>>,
Expand Down Expand Up @@ -305,15 +305,15 @@ pub struct Enqueue {
/// the key does not exist in the database, then the value specified in the
/// mutation is used as the new value of the key.
pub enum MutationKind {
Set(Value),
Set(KvValue),
Delete,
Sum(Value),
Min(Value),
Max(Value),
Sum(KvValue),
Min(KvValue),
Max(KvValue),
}

impl MutationKind {
pub fn value(&self) -> Option<&Value> {
pub fn value(&self) -> Option<&KvValue> {
match self {
MutationKind::Set(value) => Some(value),
MutationKind::Sum(value) => Some(value),
Expand Down Expand Up @@ -358,37 +358,37 @@ pub const VALUE_ENCODING_LE64: i64 = 2;
pub const VALUE_ENCODING_BYTES: i64 = 3;

/// Decode a value, returning None if the encoding is not understood.
pub fn decode_value(value: Vec<u8>, encoding: i64) -> Option<Value> {
pub fn decode_value(value: Vec<u8>, encoding: i64) -> Option<KvValue> {
let value = match encoding {
VALUE_ENCODING_V8 => Value::V8(value),
VALUE_ENCODING_BYTES => Value::Bytes(value),
VALUE_ENCODING_V8 => KvValue::V8(value),
VALUE_ENCODING_BYTES => KvValue::Bytes(value),
VALUE_ENCODING_LE64 => {
let mut buf = [0; 8];
buf.copy_from_slice(&value);
Value::U64(u64::from_le_bytes(buf))
KvValue::U64(u64::from_le_bytes(buf))
}
_ => return None,
};
Some(value)
}

pub fn encode_value(value: &Value) -> (Cow<'_, [u8]>, i64) {
pub fn encode_value(value: &KvValue) -> (Cow<'_, [u8]>, i64) {
match value {
Value::V8(value) => (Cow::Borrowed(value), VALUE_ENCODING_V8),
Value::Bytes(value) => (Cow::Borrowed(value), VALUE_ENCODING_BYTES),
Value::U64(value) => {
KvValue::V8(value) => (Cow::Borrowed(value), VALUE_ENCODING_V8),
KvValue::Bytes(value) => (Cow::Borrowed(value), VALUE_ENCODING_BYTES),
KvValue::U64(value) => {
let mut buf = [0; 8];
buf.copy_from_slice(&value.to_le_bytes());
(Cow::Owned(buf.to_vec()), VALUE_ENCODING_LE64)
}
}
}

pub fn encode_value_owned(value: Value) -> (Vec<u8>, i64) {
pub fn encode_value_owned(value: KvValue) -> (Vec<u8>, i64) {
match value {
Value::V8(value) => (value, VALUE_ENCODING_V8),
Value::Bytes(value) => (value, VALUE_ENCODING_BYTES),
Value::U64(value) => {
KvValue::V8(value) => (value, VALUE_ENCODING_V8),
KvValue::Bytes(value) => (value, VALUE_ENCODING_BYTES),
KvValue::U64(value) => {
let mut buf = [0; 8];
buf.copy_from_slice(&value.to_le_bytes());
(buf.to_vec(), VALUE_ENCODING_LE64)
Expand Down
Loading