2023-03-22 00:13:24 -04:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
2023-08-22 01:56:00 -04:00
|
|
|
pub mod dynamic;
|
2023-03-22 00:13:24 -04:00
|
|
|
mod interface;
|
2023-08-22 01:56:00 -04:00
|
|
|
pub mod remote;
|
2023-03-22 00:13:24 -04:00
|
|
|
pub mod sqlite;
|
2023-10-30 11:45:45 -04:00
|
|
|
mod time;
|
2023-03-22 00:13:24 -04:00
|
|
|
|
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::num::NonZeroU32;
|
|
|
|
use std::rc::Rc;
|
2023-10-31 07:13:57 -04:00
|
|
|
use std::time::Duration;
|
2023-03-22 00:13:24 -04:00
|
|
|
|
2023-10-26 12:39:04 -04:00
|
|
|
use base64::prelude::BASE64_URL_SAFE;
|
|
|
|
use base64::Engine;
|
2023-10-31 07:13:57 -04:00
|
|
|
use chrono::DateTime;
|
|
|
|
use chrono::Utc;
|
2023-03-22 00:13:24 -04:00
|
|
|
use deno_core::anyhow::Context;
|
2023-09-26 23:06:57 -04:00
|
|
|
use deno_core::error::get_custom_error_class;
|
2023-03-22 00:13:24 -04:00
|
|
|
use deno_core::error::type_error;
|
|
|
|
use deno_core::error::AnyError;
|
2023-12-05 08:21:46 -05:00
|
|
|
use deno_core::futures::StreamExt;
|
2023-09-14 17:05:18 -04:00
|
|
|
use deno_core::op2;
|
2023-03-22 00:13:24 -04:00
|
|
|
use deno_core::serde_v8::AnyValue;
|
|
|
|
use deno_core::serde_v8::BigInt;
|
2023-12-05 08:21:46 -05:00
|
|
|
use deno_core::AsyncRefCell;
|
2023-03-22 00:13:24 -04:00
|
|
|
use deno_core::ByteString;
|
2023-12-05 08:21:46 -05:00
|
|
|
use deno_core::CancelFuture;
|
|
|
|
use deno_core::CancelHandle;
|
2023-06-22 17:37:56 -04:00
|
|
|
use deno_core::JsBuffer;
|
2023-03-22 00:13:24 -04:00
|
|
|
use deno_core::OpState;
|
2023-12-05 08:21:46 -05:00
|
|
|
use deno_core::RcRef;
|
2023-03-22 00:13:24 -04:00
|
|
|
use deno_core::Resource;
|
|
|
|
use deno_core::ResourceId;
|
2023-06-22 17:37:56 -04:00
|
|
|
use deno_core::ToJsBuffer;
|
2023-10-31 07:13:57 -04:00
|
|
|
use denokv_proto::decode_key;
|
|
|
|
use denokv_proto::encode_key;
|
|
|
|
use denokv_proto::AtomicWrite;
|
|
|
|
use denokv_proto::Check;
|
|
|
|
use denokv_proto::Consistency;
|
|
|
|
use denokv_proto::Database;
|
|
|
|
use denokv_proto::Enqueue;
|
|
|
|
use denokv_proto::Key;
|
|
|
|
use denokv_proto::KeyPart;
|
|
|
|
use denokv_proto::KvEntry;
|
|
|
|
use denokv_proto::KvValue;
|
|
|
|
use denokv_proto::Mutation;
|
|
|
|
use denokv_proto::MutationKind;
|
|
|
|
use denokv_proto::QueueMessageHandle;
|
|
|
|
use denokv_proto::ReadRange;
|
|
|
|
use denokv_proto::SnapshotReadOptions;
|
2023-12-05 08:21:46 -05:00
|
|
|
use denokv_proto::WatchKeyOutput;
|
|
|
|
use denokv_proto::WatchStream;
|
2023-10-31 07:13:57 -04:00
|
|
|
use log::debug;
|
2023-03-22 00:13:24 -04:00
|
|
|
use serde::Deserialize;
|
|
|
|
use serde::Serialize;
|
2023-10-31 07:13:57 -04:00
|
|
|
use time::utc_now;
|
2023-03-22 00:13:24 -04:00
|
|
|
|
|
|
|
pub use crate::interface::*;
|
|
|
|
|
2023-10-12 11:55:50 -04:00
|
|
|
pub const UNSTABLE_FEATURE_NAME: &str = "kv";
|
|
|
|
|
2023-03-25 03:29:36 -04:00
|
|
|
const MAX_WRITE_KEY_SIZE_BYTES: usize = 2048;
|
|
|
|
// range selectors can contain 0x00 or 0xff suffixes
|
|
|
|
const MAX_READ_KEY_SIZE_BYTES: usize = MAX_WRITE_KEY_SIZE_BYTES + 1;
|
|
|
|
const MAX_VALUE_SIZE_BYTES: usize = 65536;
|
|
|
|
const MAX_READ_RANGES: usize = 10;
|
|
|
|
const MAX_READ_ENTRIES: usize = 1000;
|
2023-11-02 14:57:11 -04:00
|
|
|
const MAX_CHECKS: usize = 100;
|
2023-08-26 21:26:09 -04:00
|
|
|
const MAX_MUTATIONS: usize = 1000;
|
2023-12-05 08:21:46 -05:00
|
|
|
const MAX_WATCHED_KEYS: usize = 10;
|
2023-09-07 03:07:04 -04:00
|
|
|
const MAX_TOTAL_MUTATION_SIZE_BYTES: usize = 800 * 1024;
|
|
|
|
const MAX_TOTAL_KEY_SIZE_BYTES: usize = 80 * 1024;
|
2023-03-25 03:29:36 -04:00
|
|
|
|
2023-03-22 00:13:24 -04:00
|
|
|
deno_core::extension!(deno_kv,
|
2023-11-01 15:26:12 -04:00
|
|
|
deps = [ deno_console, deno_web ],
|
2023-03-22 00:13:24 -04:00
|
|
|
parameters = [ DBH: DatabaseHandler ],
|
|
|
|
ops = [
|
|
|
|
op_kv_database_open<DBH>,
|
|
|
|
op_kv_snapshot_read<DBH>,
|
|
|
|
op_kv_atomic_write<DBH>,
|
|
|
|
op_kv_encode_cursor,
|
2023-06-13 20:49:57 -04:00
|
|
|
op_kv_dequeue_next_message<DBH>,
|
|
|
|
op_kv_finish_dequeued_message<DBH>,
|
2023-12-05 08:21:46 -05:00
|
|
|
op_kv_watch<DBH>,
|
|
|
|
op_kv_watch_next,
|
2023-03-22 00:13:24 -04:00
|
|
|
],
|
|
|
|
esm = [ "01_db.ts" ],
|
|
|
|
options = {
|
|
|
|
handler: DBH,
|
|
|
|
},
|
|
|
|
state = |state, options| {
|
|
|
|
state.put(Rc::new(options.handler));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
struct DatabaseResource<DB: Database + 'static> {
|
2023-12-05 08:21:46 -05:00
|
|
|
db: DB,
|
|
|
|
cancel_handle: Rc<CancelHandle>,
|
2023-03-22 00:13:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<DB: Database + 'static> Resource for DatabaseResource<DB> {
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"database".into()
|
|
|
|
}
|
2023-06-13 20:49:57 -04:00
|
|
|
|
|
|
|
fn close(self: Rc<Self>) {
|
|
|
|
self.db.close();
|
2023-12-05 08:21:46 -05:00
|
|
|
self.cancel_handle.cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct DatabaseWatcherResource {
|
|
|
|
stream: AsyncRefCell<WatchStream>,
|
|
|
|
db_cancel_handle: Rc<CancelHandle>,
|
|
|
|
cancel_handle: Rc<CancelHandle>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Resource for DatabaseWatcherResource {
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"databaseWatcher".into()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn close(self: Rc<Self>) {
|
|
|
|
self.cancel_handle.cancel()
|
2023-06-13 20:49:57 -04:00
|
|
|
}
|
2023-03-22 00:13:24 -04:00
|
|
|
}
|
|
|
|
|
2023-09-14 17:05:18 -04:00
|
|
|
#[op2(async)]
|
|
|
|
#[smi]
|
2023-03-22 00:13:24 -04:00
|
|
|
async fn op_kv_database_open<DBH>(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
2023-09-14 17:05:18 -04:00
|
|
|
#[string] path: Option<String>,
|
2023-03-22 00:13:24 -04:00
|
|
|
) -> Result<ResourceId, AnyError>
|
|
|
|
where
|
|
|
|
DBH: DatabaseHandler + 'static,
|
|
|
|
{
|
|
|
|
let handler = {
|
|
|
|
let state = state.borrow();
|
2023-10-12 11:55:50 -04:00
|
|
|
// TODO(bartlomieju): replace with `state.feature_checker.check_or_exit`
|
|
|
|
// once we phase out `check_or_exit_with_legacy_fallback`
|
2023-03-22 15:23:36 -04:00
|
|
|
state
|
2023-10-04 15:42:17 -04:00
|
|
|
.feature_checker
|
2023-10-12 11:55:50 -04:00
|
|
|
.check_or_exit_with_legacy_fallback(UNSTABLE_FEATURE_NAME, "Deno.openKv");
|
2023-03-22 00:13:24 -04:00
|
|
|
state.borrow::<Rc<DBH>>().clone()
|
|
|
|
};
|
|
|
|
let db = handler.open(state.clone(), path).await?;
|
2023-12-05 08:21:46 -05:00
|
|
|
let rid = state.borrow_mut().resource_table.add(DatabaseResource {
|
|
|
|
db,
|
|
|
|
cancel_handle: CancelHandle::new_rc(),
|
|
|
|
});
|
2023-03-22 00:13:24 -04:00
|
|
|
Ok(rid)
|
|
|
|
}
|
|
|
|
|
|
|
|
type KvKey = Vec<AnyValue>;
|
|
|
|
|
2023-10-31 07:13:57 -04:00
|
|
|
fn key_part_from_v8(value: AnyValue) -> KeyPart {
|
|
|
|
match value {
|
|
|
|
AnyValue::Bool(false) => KeyPart::False,
|
|
|
|
AnyValue::Bool(true) => KeyPart::True,
|
|
|
|
AnyValue::Number(n) => KeyPart::Float(n),
|
|
|
|
AnyValue::BigInt(n) => KeyPart::Int(n),
|
|
|
|
AnyValue::String(s) => KeyPart::String(s),
|
|
|
|
AnyValue::V8Buffer(buf) => KeyPart::Bytes(buf.to_vec()),
|
|
|
|
AnyValue::RustBuffer(_) => unreachable!(),
|
2023-03-22 00:13:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-31 07:13:57 -04:00
|
|
|
fn key_part_to_v8(value: KeyPart) -> AnyValue {
|
|
|
|
match value {
|
|
|
|
KeyPart::False => AnyValue::Bool(false),
|
|
|
|
KeyPart::True => AnyValue::Bool(true),
|
|
|
|
KeyPart::Float(n) => AnyValue::Number(n),
|
|
|
|
KeyPart::Int(n) => AnyValue::BigInt(n),
|
|
|
|
KeyPart::String(s) => AnyValue::String(s),
|
|
|
|
KeyPart::Bytes(buf) => AnyValue::RustBuffer(buf.into()),
|
2023-03-22 00:13:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-22 17:37:56 -04:00
|
|
|
#[derive(Debug, Deserialize)]
|
2023-03-22 00:13:24 -04:00
|
|
|
#[serde(tag = "kind", content = "value", rename_all = "snake_case")]
|
2023-06-22 17:37:56 -04:00
|
|
|
enum FromV8Value {
|
|
|
|
V8(JsBuffer),
|
|
|
|
Bytes(JsBuffer),
|
2023-03-22 00:13:24 -04:00
|
|
|
U64(BigInt),
|
|
|
|
}
|
|
|
|
|
2023-06-22 17:37:56 -04:00
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
#[serde(tag = "kind", content = "value", rename_all = "snake_case")]
|
|
|
|
enum ToV8Value {
|
|
|
|
V8(ToJsBuffer),
|
|
|
|
Bytes(ToJsBuffer),
|
|
|
|
U64(BigInt),
|
|
|
|
}
|
|
|
|
|
2023-10-31 07:13:57 -04:00
|
|
|
impl TryFrom<FromV8Value> for KvValue {
|
2023-03-22 00:13:24 -04:00
|
|
|
type Error = AnyError;
|
2023-06-22 17:37:56 -04:00
|
|
|
fn try_from(value: FromV8Value) -> Result<Self, AnyError> {
|
2023-03-22 00:13:24 -04:00
|
|
|
Ok(match value {
|
2023-10-31 07:13:57 -04:00
|
|
|
FromV8Value::V8(buf) => KvValue::V8(buf.to_vec()),
|
|
|
|
FromV8Value::Bytes(buf) => KvValue::Bytes(buf.to_vec()),
|
2023-06-22 17:37:56 -04:00
|
|
|
FromV8Value::U64(n) => {
|
2023-10-31 07:13:57 -04:00
|
|
|
KvValue::U64(num_bigint::BigInt::from(n).try_into()?)
|
2023-06-22 17:37:56 -04:00
|
|
|
}
|
2023-03-22 00:13:24 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-31 07:13:57 -04:00
|
|
|
impl From<KvValue> for ToV8Value {
|
|
|
|
fn from(value: KvValue) -> Self {
|
2023-03-22 00:13:24 -04:00
|
|
|
match value {
|
2023-10-31 07:13:57 -04:00
|
|
|
KvValue::V8(buf) => ToV8Value::V8(buf.into()),
|
|
|
|
KvValue::Bytes(buf) => ToV8Value::Bytes(buf.into()),
|
|
|
|
KvValue::U64(n) => ToV8Value::U64(num_bigint::BigInt::from(n).into()),
|
2023-03-22 00:13:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-22 17:37:56 -04:00
|
|
|
#[derive(Serialize)]
|
|
|
|
struct ToV8KvEntry {
|
2023-03-22 00:13:24 -04:00
|
|
|
key: KvKey,
|
2023-06-22 17:37:56 -04:00
|
|
|
value: ToV8Value,
|
2023-03-22 00:13:24 -04:00
|
|
|
versionstamp: ByteString,
|
|
|
|
}
|
|
|
|
|
2023-06-22 17:37:56 -04:00
|
|
|
impl TryFrom<KvEntry> for ToV8KvEntry {
|
2023-03-22 00:13:24 -04:00
|
|
|
type Error = AnyError;
|
|
|
|
fn try_from(entry: KvEntry) -> Result<Self, AnyError> {
|
2023-06-22 17:37:56 -04:00
|
|
|
Ok(ToV8KvEntry {
|
2023-03-22 00:13:24 -04:00
|
|
|
key: decode_key(&entry.key)?
|
|
|
|
.0
|
|
|
|
.into_iter()
|
2023-10-31 07:13:57 -04:00
|
|
|
.map(key_part_to_v8)
|
2023-03-22 00:13:24 -04:00
|
|
|
.collect(),
|
|
|
|
value: entry.value.into(),
|
|
|
|
versionstamp: hex::encode(entry.versionstamp).into(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
enum V8Consistency {
|
|
|
|
Strong,
|
|
|
|
Eventual,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<V8Consistency> for Consistency {
|
|
|
|
fn from(value: V8Consistency) -> Self {
|
|
|
|
match value {
|
|
|
|
V8Consistency::Strong => Consistency::Strong,
|
|
|
|
V8Consistency::Eventual => Consistency::Eventual,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// (prefix, start, end, limit, reverse, cursor)
|
|
|
|
type SnapshotReadRange = (
|
|
|
|
Option<KvKey>,
|
|
|
|
Option<KvKey>,
|
|
|
|
Option<KvKey>,
|
|
|
|
u32,
|
|
|
|
bool,
|
|
|
|
Option<ByteString>,
|
|
|
|
);
|
|
|
|
|
2023-09-14 17:05:18 -04:00
|
|
|
#[op2(async)]
|
|
|
|
#[serde]
|
2023-03-22 00:13:24 -04:00
|
|
|
async fn op_kv_snapshot_read<DBH>(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
2023-09-14 17:05:18 -04:00
|
|
|
#[smi] rid: ResourceId,
|
|
|
|
#[serde] ranges: Vec<SnapshotReadRange>,
|
|
|
|
#[serde] consistency: V8Consistency,
|
2023-06-22 17:37:56 -04:00
|
|
|
) -> Result<Vec<Vec<ToV8KvEntry>>, AnyError>
|
2023-03-22 00:13:24 -04:00
|
|
|
where
|
|
|
|
DBH: DatabaseHandler + 'static,
|
|
|
|
{
|
|
|
|
let db = {
|
|
|
|
let state = state.borrow();
|
|
|
|
let resource =
|
|
|
|
state.resource_table.get::<DatabaseResource<DBH::DB>>(rid)?;
|
|
|
|
resource.db.clone()
|
|
|
|
};
|
2023-03-25 03:29:36 -04:00
|
|
|
|
|
|
|
if ranges.len() > MAX_READ_RANGES {
|
|
|
|
return Err(type_error(format!(
|
|
|
|
"too many ranges (max {})",
|
|
|
|
MAX_READ_RANGES
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut total_entries = 0usize;
|
|
|
|
|
2023-03-22 00:13:24 -04:00
|
|
|
let read_ranges = ranges
|
|
|
|
.into_iter()
|
|
|
|
.map(|(prefix, start, end, limit, reverse, cursor)| {
|
|
|
|
let selector = RawSelector::from_tuple(prefix, start, end)?;
|
|
|
|
|
|
|
|
let (start, end) =
|
|
|
|
decode_selector_and_cursor(&selector, reverse, cursor.as_ref())?;
|
2023-03-25 03:29:36 -04:00
|
|
|
check_read_key_size(&start)?;
|
|
|
|
check_read_key_size(&end)?;
|
|
|
|
|
|
|
|
total_entries += limit as usize;
|
2023-03-22 00:13:24 -04:00
|
|
|
Ok(ReadRange {
|
|
|
|
start,
|
|
|
|
end,
|
|
|
|
limit: NonZeroU32::new(limit)
|
|
|
|
.with_context(|| "limit must be greater than 0")?,
|
|
|
|
reverse,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.collect::<Result<Vec<_>, AnyError>>()?;
|
2023-03-25 03:29:36 -04:00
|
|
|
|
|
|
|
if total_entries > MAX_READ_ENTRIES {
|
|
|
|
return Err(type_error(format!(
|
|
|
|
"too many entries (max {})",
|
|
|
|
MAX_READ_ENTRIES
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
2023-03-22 00:13:24 -04:00
|
|
|
let opts = SnapshotReadOptions {
|
|
|
|
consistency: consistency.into(),
|
|
|
|
};
|
2023-10-31 07:13:57 -04:00
|
|
|
let output_ranges = db.snapshot_read(read_ranges, opts).await?;
|
2023-03-22 00:13:24 -04:00
|
|
|
let output_ranges = output_ranges
|
|
|
|
.into_iter()
|
|
|
|
.map(|x| {
|
|
|
|
x.entries
|
|
|
|
.into_iter()
|
|
|
|
.map(TryInto::try_into)
|
|
|
|
.collect::<Result<Vec<_>, AnyError>>()
|
|
|
|
})
|
|
|
|
.collect::<Result<Vec<_>, AnyError>>()?;
|
|
|
|
Ok(output_ranges)
|
|
|
|
}
|
|
|
|
|
2023-06-13 20:49:57 -04:00
|
|
|
struct QueueMessageResource<QPH: QueueMessageHandle + 'static> {
|
|
|
|
handle: QPH,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<QMH: QueueMessageHandle + 'static> Resource for QueueMessageResource<QMH> {
|
|
|
|
fn name(&self) -> Cow<str> {
|
2023-10-31 07:13:57 -04:00
|
|
|
"queueMessage".into()
|
2023-06-13 20:49:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-14 17:05:18 -04:00
|
|
|
#[op2(async)]
|
|
|
|
#[serde]
|
2023-06-13 20:49:57 -04:00
|
|
|
async fn op_kv_dequeue_next_message<DBH>(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
2023-09-14 17:05:18 -04:00
|
|
|
#[smi] rid: ResourceId,
|
2023-09-26 23:06:57 -04:00
|
|
|
) -> Result<Option<(ToJsBuffer, ResourceId)>, AnyError>
|
2023-06-13 20:49:57 -04:00
|
|
|
where
|
|
|
|
DBH: DatabaseHandler + 'static,
|
|
|
|
{
|
|
|
|
let db = {
|
|
|
|
let state = state.borrow();
|
|
|
|
let resource =
|
2023-09-26 23:06:57 -04:00
|
|
|
match state.resource_table.get::<DatabaseResource<DBH::DB>>(rid) {
|
|
|
|
Ok(resource) => resource,
|
|
|
|
Err(err) => {
|
|
|
|
if get_custom_error_class(&err) == Some("BadResource") {
|
|
|
|
return Ok(None);
|
|
|
|
} else {
|
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2023-06-13 20:49:57 -04:00
|
|
|
resource.db.clone()
|
|
|
|
};
|
|
|
|
|
2023-10-31 07:13:57 -04:00
|
|
|
let Some(mut handle) = db.dequeue_next_message().await? else {
|
2023-09-26 23:06:57 -04:00
|
|
|
return Ok(None);
|
|
|
|
};
|
2023-06-13 20:49:57 -04:00
|
|
|
let payload = handle.take_payload().await?.into();
|
|
|
|
let handle_rid = {
|
|
|
|
let mut state = state.borrow_mut();
|
|
|
|
state.resource_table.add(QueueMessageResource { handle })
|
|
|
|
};
|
2023-09-26 23:06:57 -04:00
|
|
|
Ok(Some((payload, handle_rid)))
|
2023-06-13 20:49:57 -04:00
|
|
|
}
|
|
|
|
|
2023-12-05 08:21:46 -05:00
|
|
|
#[op2]
|
|
|
|
#[smi]
|
|
|
|
fn op_kv_watch<DBH>(
|
|
|
|
state: &mut OpState,
|
|
|
|
#[smi] rid: ResourceId,
|
|
|
|
#[serde] keys: Vec<KvKey>,
|
|
|
|
) -> Result<ResourceId, AnyError>
|
|
|
|
where
|
|
|
|
DBH: DatabaseHandler + 'static,
|
|
|
|
{
|
|
|
|
let resource = state.resource_table.get::<DatabaseResource<DBH::DB>>(rid)?;
|
|
|
|
|
|
|
|
if keys.len() > MAX_WATCHED_KEYS {
|
|
|
|
return Err(type_error(format!(
|
|
|
|
"too many keys (max {})",
|
|
|
|
MAX_WATCHED_KEYS
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
|
|
|
let keys: Vec<Vec<u8>> = keys
|
|
|
|
.into_iter()
|
|
|
|
.map(encode_v8_key)
|
|
|
|
.collect::<std::io::Result<_>>()?;
|
|
|
|
|
|
|
|
for k in &keys {
|
|
|
|
check_read_key_size(k)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let stream = resource.db.watch(keys);
|
|
|
|
|
|
|
|
let rid = state.resource_table.add(DatabaseWatcherResource {
|
|
|
|
stream: AsyncRefCell::new(stream),
|
|
|
|
db_cancel_handle: resource.cancel_handle.clone(),
|
|
|
|
cancel_handle: CancelHandle::new_rc(),
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(rid)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase", untagged)]
|
|
|
|
enum WatchEntry {
|
|
|
|
Changed(Option<ToV8KvEntry>),
|
|
|
|
Unchanged,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[op2(async)]
|
|
|
|
#[serde]
|
|
|
|
async fn op_kv_watch_next(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
|
|
|
#[smi] rid: ResourceId,
|
|
|
|
) -> Result<Option<Vec<WatchEntry>>, AnyError> {
|
|
|
|
let resource = {
|
|
|
|
let state = state.borrow();
|
|
|
|
let resource = state.resource_table.get::<DatabaseWatcherResource>(rid)?;
|
|
|
|
resource.clone()
|
|
|
|
};
|
|
|
|
|
|
|
|
let db_cancel_handle = resource.db_cancel_handle.clone();
|
|
|
|
let cancel_handle = resource.cancel_handle.clone();
|
|
|
|
let stream = RcRef::map(resource, |r| &r.stream)
|
|
|
|
.borrow_mut()
|
|
|
|
.or_cancel(db_cancel_handle)
|
|
|
|
.or_cancel(cancel_handle)
|
|
|
|
.await;
|
|
|
|
let Ok(Ok(mut stream)) = stream else {
|
|
|
|
return Ok(None);
|
|
|
|
};
|
|
|
|
|
|
|
|
// doesn't need a cancel handle because the stream ends when the database
|
|
|
|
// connection is closed
|
|
|
|
let Some(res) = stream.next().await else {
|
|
|
|
return Ok(None);
|
|
|
|
};
|
|
|
|
|
|
|
|
let entries = res?;
|
|
|
|
let entries = entries
|
|
|
|
.into_iter()
|
|
|
|
.map(|entry| {
|
|
|
|
Ok(match entry {
|
|
|
|
WatchKeyOutput::Changed { entry } => {
|
|
|
|
WatchEntry::Changed(entry.map(TryInto::try_into).transpose()?)
|
|
|
|
}
|
|
|
|
WatchKeyOutput::Unchanged => WatchEntry::Unchanged,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.collect::<Result<_, anyhow::Error>>()?;
|
|
|
|
|
|
|
|
Ok(Some(entries))
|
|
|
|
}
|
|
|
|
|
2023-09-14 17:05:18 -04:00
|
|
|
#[op2(async)]
|
2023-06-13 20:49:57 -04:00
|
|
|
async fn op_kv_finish_dequeued_message<DBH>(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
2023-09-14 17:05:18 -04:00
|
|
|
#[smi] handle_rid: ResourceId,
|
2023-06-13 20:49:57 -04:00
|
|
|
success: bool,
|
|
|
|
) -> Result<(), AnyError>
|
|
|
|
where
|
|
|
|
DBH: DatabaseHandler + 'static,
|
|
|
|
{
|
|
|
|
let handle = {
|
|
|
|
let mut state = state.borrow_mut();
|
|
|
|
let handle = state
|
|
|
|
.resource_table
|
|
|
|
.take::<QueueMessageResource<<<DBH>::DB as Database>::QMH>>(handle_rid)
|
|
|
|
.map_err(|_| type_error("Queue message not found"))?;
|
|
|
|
Rc::try_unwrap(handle)
|
|
|
|
.map_err(|_| type_error("Queue message not found"))?
|
|
|
|
.handle
|
|
|
|
};
|
2023-10-31 07:13:57 -04:00
|
|
|
// if we fail to finish the message, there is not much we can do and the
|
|
|
|
// message will be retried anyway, so we just ignore the error
|
|
|
|
if let Err(err) = handle.finish(success).await {
|
|
|
|
debug!("Failed to finish dequeued message: {}", err);
|
|
|
|
};
|
|
|
|
Ok(())
|
2023-06-13 20:49:57 -04:00
|
|
|
}
|
|
|
|
|
2023-03-22 00:13:24 -04:00
|
|
|
type V8KvCheck = (KvKey, Option<ByteString>);
|
|
|
|
|
2023-10-31 07:13:57 -04:00
|
|
|
fn check_from_v8(value: V8KvCheck) -> Result<Check, AnyError> {
|
|
|
|
let versionstamp = match value.1 {
|
|
|
|
Some(data) => {
|
|
|
|
let mut out = [0u8; 10];
|
|
|
|
hex::decode_to_slice(data, &mut out)
|
|
|
|
.map_err(|_| type_error("invalid versionstamp"))?;
|
|
|
|
Some(out)
|
|
|
|
}
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
Ok(Check {
|
|
|
|
key: encode_v8_key(value.0)?,
|
|
|
|
versionstamp,
|
|
|
|
})
|
2023-03-22 00:13:24 -04:00
|
|
|
}
|
|
|
|
|
2023-08-18 05:34:16 -04:00
|
|
|
type V8KvMutation = (KvKey, String, Option<FromV8Value>, Option<u64>);
|
2023-03-22 00:13:24 -04:00
|
|
|
|
2023-10-31 07:13:57 -04:00
|
|
|
fn mutation_from_v8(
|
|
|
|
(value, current_timstamp): (V8KvMutation, DateTime<Utc>),
|
|
|
|
) -> Result<Mutation, AnyError> {
|
|
|
|
let key = encode_v8_key(value.0)?;
|
|
|
|
let kind = match (value.1.as_str(), value.2) {
|
|
|
|
("set", Some(value)) => MutationKind::Set(value.try_into()?),
|
|
|
|
("delete", None) => MutationKind::Delete,
|
|
|
|
("sum", Some(value)) => MutationKind::Sum(value.try_into()?),
|
|
|
|
("min", Some(value)) => MutationKind::Min(value.try_into()?),
|
|
|
|
("max", Some(value)) => MutationKind::Max(value.try_into()?),
|
2023-12-13 11:58:20 -05:00
|
|
|
("setSuffixVersionstampedKey", Some(value)) => {
|
|
|
|
MutationKind::SetSuffixVersionstampedKey(value.try_into()?)
|
|
|
|
}
|
2023-10-31 07:13:57 -04:00
|
|
|
(op, Some(_)) => {
|
|
|
|
return Err(type_error(format!("invalid mutation '{op}' with value")))
|
|
|
|
}
|
|
|
|
(op, None) => {
|
|
|
|
return Err(type_error(format!("invalid mutation '{op}' without value")))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(Mutation {
|
|
|
|
key,
|
|
|
|
kind,
|
|
|
|
expire_at: value
|
|
|
|
.3
|
|
|
|
.map(|expire_in| current_timstamp + Duration::from_millis(expire_in)),
|
|
|
|
})
|
2023-03-22 00:13:24 -04:00
|
|
|
}
|
|
|
|
|
2023-06-22 17:37:56 -04:00
|
|
|
type V8Enqueue = (JsBuffer, u64, Vec<KvKey>, Option<Vec<u32>>);
|
2023-03-22 00:13:24 -04:00
|
|
|
|
2023-10-31 07:13:57 -04:00
|
|
|
fn enqueue_from_v8(
|
|
|
|
value: V8Enqueue,
|
|
|
|
current_timestamp: DateTime<Utc>,
|
|
|
|
) -> Result<Enqueue, AnyError> {
|
|
|
|
Ok(Enqueue {
|
|
|
|
payload: value.0.to_vec(),
|
|
|
|
deadline: current_timestamp
|
|
|
|
+ chrono::Duration::milliseconds(value.1 as i64),
|
|
|
|
keys_if_undelivered: value
|
|
|
|
.2
|
|
|
|
.into_iter()
|
|
|
|
.map(encode_v8_key)
|
|
|
|
.collect::<std::io::Result<_>>()?,
|
|
|
|
backoff_schedule: value.3,
|
|
|
|
})
|
2023-03-22 00:13:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn encode_v8_key(key: KvKey) -> Result<Vec<u8>, std::io::Error> {
|
2023-10-31 07:13:57 -04:00
|
|
|
encode_key(&Key(key.into_iter().map(key_part_from_v8).collect()))
|
2023-03-22 00:13:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
enum RawSelector {
|
|
|
|
Prefixed {
|
|
|
|
prefix: Vec<u8>,
|
|
|
|
start: Option<Vec<u8>>,
|
|
|
|
end: Option<Vec<u8>>,
|
|
|
|
},
|
|
|
|
Range {
|
|
|
|
start: Vec<u8>,
|
|
|
|
end: Vec<u8>,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RawSelector {
|
|
|
|
fn from_tuple(
|
|
|
|
prefix: Option<KvKey>,
|
|
|
|
start: Option<KvKey>,
|
|
|
|
end: Option<KvKey>,
|
|
|
|
) -> Result<Self, AnyError> {
|
|
|
|
let prefix = prefix.map(encode_v8_key).transpose()?;
|
|
|
|
let start = start.map(encode_v8_key).transpose()?;
|
|
|
|
let end = end.map(encode_v8_key).transpose()?;
|
|
|
|
|
|
|
|
match (prefix, start, end) {
|
|
|
|
(Some(prefix), None, None) => Ok(Self::Prefixed {
|
|
|
|
prefix,
|
|
|
|
start: None,
|
|
|
|
end: None,
|
|
|
|
}),
|
|
|
|
(Some(prefix), Some(start), None) => Ok(Self::Prefixed {
|
|
|
|
prefix,
|
|
|
|
start: Some(start),
|
|
|
|
end: None,
|
|
|
|
}),
|
|
|
|
(Some(prefix), None, Some(end)) => Ok(Self::Prefixed {
|
|
|
|
prefix,
|
|
|
|
start: None,
|
|
|
|
end: Some(end),
|
|
|
|
}),
|
|
|
|
(None, Some(start), Some(end)) => Ok(Self::Range { start, end }),
|
|
|
|
(None, Some(start), None) => {
|
|
|
|
let end = start.iter().copied().chain(Some(0)).collect();
|
|
|
|
Ok(Self::Range { start, end })
|
|
|
|
}
|
|
|
|
_ => Err(type_error("invalid range")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn start(&self) -> Option<&[u8]> {
|
|
|
|
match self {
|
|
|
|
Self::Prefixed { start, .. } => start.as_deref(),
|
|
|
|
Self::Range { start, .. } => Some(start),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end(&self) -> Option<&[u8]> {
|
|
|
|
match self {
|
|
|
|
Self::Prefixed { end, .. } => end.as_deref(),
|
|
|
|
Self::Range { end, .. } => Some(end),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn common_prefix(&self) -> &[u8] {
|
|
|
|
match self {
|
|
|
|
Self::Prefixed { prefix, .. } => prefix,
|
|
|
|
Self::Range { start, end } => common_prefix_for_bytes(start, end),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn range_start_key(&self) -> Vec<u8> {
|
|
|
|
match self {
|
|
|
|
Self::Prefixed {
|
|
|
|
start: Some(start), ..
|
|
|
|
} => start.clone(),
|
|
|
|
Self::Range { start, .. } => start.clone(),
|
|
|
|
Self::Prefixed { prefix, .. } => {
|
|
|
|
prefix.iter().copied().chain(Some(0)).collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn range_end_key(&self) -> Vec<u8> {
|
|
|
|
match self {
|
|
|
|
Self::Prefixed { end: Some(end), .. } => end.clone(),
|
|
|
|
Self::Range { end, .. } => end.clone(),
|
|
|
|
Self::Prefixed { prefix, .. } => {
|
|
|
|
prefix.iter().copied().chain(Some(0xff)).collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn common_prefix_for_bytes<'a>(a: &'a [u8], b: &'a [u8]) -> &'a [u8] {
|
|
|
|
let mut i = 0;
|
|
|
|
while i < a.len() && i < b.len() && a[i] == b[i] {
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
&a[..i]
|
|
|
|
}
|
|
|
|
|
|
|
|
fn encode_cursor(
|
|
|
|
selector: &RawSelector,
|
|
|
|
boundary_key: &[u8],
|
|
|
|
) -> Result<String, AnyError> {
|
|
|
|
let common_prefix = selector.common_prefix();
|
|
|
|
if !boundary_key.starts_with(common_prefix) {
|
|
|
|
return Err(type_error("invalid boundary key"));
|
|
|
|
}
|
2023-10-26 12:39:04 -04:00
|
|
|
Ok(BASE64_URL_SAFE.encode(&boundary_key[common_prefix.len()..]))
|
2023-03-22 00:13:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn decode_selector_and_cursor(
|
|
|
|
selector: &RawSelector,
|
|
|
|
reverse: bool,
|
|
|
|
cursor: Option<&ByteString>,
|
|
|
|
) -> Result<(Vec<u8>, Vec<u8>), AnyError> {
|
|
|
|
let Some(cursor) = cursor else {
|
|
|
|
return Ok((selector.range_start_key(), selector.range_end_key()));
|
|
|
|
};
|
|
|
|
|
|
|
|
let common_prefix = selector.common_prefix();
|
2023-10-26 12:39:04 -04:00
|
|
|
let cursor = BASE64_URL_SAFE
|
|
|
|
.decode(cursor)
|
2023-03-22 00:13:24 -04:00
|
|
|
.map_err(|_| type_error("invalid cursor"))?;
|
|
|
|
|
|
|
|
let first_key: Vec<u8>;
|
|
|
|
let last_key: Vec<u8>;
|
|
|
|
|
|
|
|
if reverse {
|
|
|
|
first_key = selector.range_start_key();
|
|
|
|
last_key = common_prefix
|
|
|
|
.iter()
|
|
|
|
.copied()
|
|
|
|
.chain(cursor.iter().copied())
|
|
|
|
.collect();
|
|
|
|
} else {
|
|
|
|
first_key = common_prefix
|
|
|
|
.iter()
|
|
|
|
.copied()
|
|
|
|
.chain(cursor.iter().copied())
|
|
|
|
.chain(Some(0))
|
|
|
|
.collect();
|
|
|
|
last_key = selector.range_end_key();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Defend against out-of-bounds reading
|
|
|
|
if let Some(start) = selector.start() {
|
|
|
|
if &first_key[..] < start {
|
|
|
|
return Err(type_error("cursor out of bounds"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(end) = selector.end() {
|
|
|
|
if &last_key[..] > end {
|
|
|
|
return Err(type_error("cursor out of bounds"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok((first_key, last_key))
|
|
|
|
}
|
|
|
|
|
2023-09-14 17:05:18 -04:00
|
|
|
#[op2(async)]
|
|
|
|
#[string]
|
2023-03-22 00:13:24 -04:00
|
|
|
async fn op_kv_atomic_write<DBH>(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
2023-09-14 17:05:18 -04:00
|
|
|
#[smi] rid: ResourceId,
|
|
|
|
#[serde] checks: Vec<V8KvCheck>,
|
|
|
|
#[serde] mutations: Vec<V8KvMutation>,
|
|
|
|
#[serde] enqueues: Vec<V8Enqueue>,
|
2023-03-30 14:57:21 -04:00
|
|
|
) -> Result<Option<String>, AnyError>
|
2023-03-22 00:13:24 -04:00
|
|
|
where
|
|
|
|
DBH: DatabaseHandler + 'static,
|
|
|
|
{
|
2023-10-31 07:13:57 -04:00
|
|
|
let current_timestamp = utc_now();
|
2023-03-22 00:13:24 -04:00
|
|
|
let db = {
|
|
|
|
let state = state.borrow();
|
|
|
|
let resource =
|
|
|
|
state.resource_table.get::<DatabaseResource<DBH::DB>>(rid)?;
|
|
|
|
resource.db.clone()
|
|
|
|
};
|
|
|
|
|
2023-03-25 03:29:36 -04:00
|
|
|
if checks.len() > MAX_CHECKS {
|
|
|
|
return Err(type_error(format!("too many checks (max {})", MAX_CHECKS)));
|
|
|
|
}
|
|
|
|
|
|
|
|
if mutations.len() + enqueues.len() > MAX_MUTATIONS {
|
|
|
|
return Err(type_error(format!(
|
|
|
|
"too many mutations (max {})",
|
|
|
|
MAX_MUTATIONS
|
|
|
|
)));
|
2023-03-22 16:53:16 -04:00
|
|
|
}
|
|
|
|
|
2023-03-22 00:13:24 -04:00
|
|
|
let checks = checks
|
|
|
|
.into_iter()
|
2023-10-31 07:13:57 -04:00
|
|
|
.map(check_from_v8)
|
|
|
|
.collect::<Result<Vec<Check>, AnyError>>()
|
2023-03-22 00:13:24 -04:00
|
|
|
.with_context(|| "invalid check")?;
|
|
|
|
let mutations = mutations
|
|
|
|
.into_iter()
|
2023-10-31 07:13:57 -04:00
|
|
|
.map(|mutation| mutation_from_v8((mutation, current_timestamp)))
|
|
|
|
.collect::<Result<Vec<Mutation>, AnyError>>()
|
2023-03-22 00:13:24 -04:00
|
|
|
.with_context(|| "invalid mutation")?;
|
|
|
|
let enqueues = enqueues
|
|
|
|
.into_iter()
|
2023-10-31 07:13:57 -04:00
|
|
|
.map(|e| enqueue_from_v8(e, current_timestamp))
|
2023-03-25 03:29:36 -04:00
|
|
|
.collect::<Result<Vec<Enqueue>, AnyError>>()
|
2023-03-22 00:13:24 -04:00
|
|
|
.with_context(|| "invalid enqueue")?;
|
|
|
|
|
2023-08-26 21:26:09 -04:00
|
|
|
let mut total_payload_size = 0usize;
|
2023-09-07 03:07:04 -04:00
|
|
|
let mut total_key_size = 0usize;
|
2023-08-26 21:26:09 -04:00
|
|
|
|
2023-03-25 03:29:36 -04:00
|
|
|
for key in checks
|
|
|
|
.iter()
|
|
|
|
.map(|c| &c.key)
|
|
|
|
.chain(mutations.iter().map(|m| &m.key))
|
|
|
|
{
|
|
|
|
if key.is_empty() {
|
|
|
|
return Err(type_error("key cannot be empty"));
|
|
|
|
}
|
|
|
|
|
2023-11-02 14:57:11 -04:00
|
|
|
total_payload_size += check_write_key_size(key)?;
|
2023-03-25 03:29:36 -04:00
|
|
|
}
|
|
|
|
|
2023-11-02 14:57:11 -04:00
|
|
|
for (key, value) in mutations
|
|
|
|
.iter()
|
|
|
|
.flat_map(|m| m.kind.value().map(|x| (&m.key, x)))
|
|
|
|
{
|
|
|
|
let key_size = check_write_key_size(key)?;
|
|
|
|
total_payload_size += check_value_size(value)? + key_size;
|
|
|
|
total_key_size += key_size;
|
2023-03-25 03:29:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for enqueue in &enqueues {
|
2023-08-26 21:26:09 -04:00
|
|
|
total_payload_size += check_enqueue_payload_size(&enqueue.payload)?;
|
2023-12-13 01:51:23 -05:00
|
|
|
if let Some(schedule) = enqueue.backoff_schedule.as_ref() {
|
|
|
|
total_payload_size += 4 * schedule.len();
|
|
|
|
}
|
2023-08-26 21:26:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if total_payload_size > MAX_TOTAL_MUTATION_SIZE_BYTES {
|
|
|
|
return Err(type_error(format!(
|
|
|
|
"total mutation size too large (max {} bytes)",
|
|
|
|
MAX_TOTAL_MUTATION_SIZE_BYTES
|
|
|
|
)));
|
2023-03-25 03:29:36 -04:00
|
|
|
}
|
|
|
|
|
2023-09-07 03:07:04 -04:00
|
|
|
if total_key_size > MAX_TOTAL_KEY_SIZE_BYTES {
|
|
|
|
return Err(type_error(format!(
|
|
|
|
"total key size too large (max {} bytes)",
|
|
|
|
MAX_TOTAL_KEY_SIZE_BYTES
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
2023-03-22 00:13:24 -04:00
|
|
|
let atomic_write = AtomicWrite {
|
|
|
|
checks,
|
|
|
|
mutations,
|
|
|
|
enqueues,
|
|
|
|
};
|
|
|
|
|
2023-10-31 07:13:57 -04:00
|
|
|
let result = db.atomic_write(atomic_write).await?;
|
2023-03-22 00:13:24 -04:00
|
|
|
|
2023-03-30 14:57:21 -04:00
|
|
|
Ok(result.map(|res| hex::encode(res.versionstamp)))
|
2023-03-22 00:13:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// (prefix, start, end)
|
|
|
|
type EncodeCursorRangeSelector = (Option<KvKey>, Option<KvKey>, Option<KvKey>);
|
|
|
|
|
2023-09-14 17:05:18 -04:00
|
|
|
#[op2]
|
|
|
|
#[string]
|
2023-03-22 00:13:24 -04:00
|
|
|
fn op_kv_encode_cursor(
|
2023-09-14 17:05:18 -04:00
|
|
|
#[serde] (prefix, start, end): EncodeCursorRangeSelector,
|
|
|
|
#[serde] boundary_key: KvKey,
|
2023-03-22 00:13:24 -04:00
|
|
|
) -> Result<String, AnyError> {
|
|
|
|
let selector = RawSelector::from_tuple(prefix, start, end)?;
|
|
|
|
let boundary_key = encode_v8_key(boundary_key)?;
|
|
|
|
let cursor = encode_cursor(&selector, &boundary_key)?;
|
|
|
|
Ok(cursor)
|
|
|
|
}
|
2023-03-25 03:29:36 -04:00
|
|
|
|
|
|
|
fn check_read_key_size(key: &[u8]) -> Result<(), AnyError> {
|
|
|
|
if key.len() > MAX_READ_KEY_SIZE_BYTES {
|
|
|
|
Err(type_error(format!(
|
|
|
|
"key too large for read (max {} bytes)",
|
|
|
|
MAX_READ_KEY_SIZE_BYTES
|
|
|
|
)))
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-26 21:26:09 -04:00
|
|
|
fn check_write_key_size(key: &[u8]) -> Result<usize, AnyError> {
|
2023-03-25 03:29:36 -04:00
|
|
|
if key.len() > MAX_WRITE_KEY_SIZE_BYTES {
|
|
|
|
Err(type_error(format!(
|
|
|
|
"key too large for write (max {} bytes)",
|
|
|
|
MAX_WRITE_KEY_SIZE_BYTES
|
|
|
|
)))
|
|
|
|
} else {
|
2023-08-26 21:26:09 -04:00
|
|
|
Ok(key.len())
|
2023-03-25 03:29:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-31 07:13:57 -04:00
|
|
|
fn check_value_size(value: &KvValue) -> Result<usize, AnyError> {
|
2023-03-25 03:29:36 -04:00
|
|
|
let payload = match value {
|
2023-10-31 07:13:57 -04:00
|
|
|
KvValue::Bytes(x) => x,
|
|
|
|
KvValue::V8(x) => x,
|
|
|
|
KvValue::U64(_) => return Ok(8),
|
2023-03-25 03:29:36 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
if payload.len() > MAX_VALUE_SIZE_BYTES {
|
|
|
|
Err(type_error(format!(
|
|
|
|
"value too large (max {} bytes)",
|
|
|
|
MAX_VALUE_SIZE_BYTES
|
|
|
|
)))
|
|
|
|
} else {
|
2023-08-26 21:26:09 -04:00
|
|
|
Ok(payload.len())
|
2023-03-25 03:29:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-26 21:26:09 -04:00
|
|
|
fn check_enqueue_payload_size(payload: &[u8]) -> Result<usize, AnyError> {
|
2023-03-25 03:29:36 -04:00
|
|
|
if payload.len() > MAX_VALUE_SIZE_BYTES {
|
|
|
|
Err(type_error(format!(
|
|
|
|
"enqueue payload too large (max {} bytes)",
|
|
|
|
MAX_VALUE_SIZE_BYTES
|
|
|
|
)))
|
|
|
|
} else {
|
2023-08-26 21:26:09 -04:00
|
|
|
Ok(payload.len())
|
2023-03-25 03:29:36 -04:00
|
|
|
}
|
|
|
|
}
|