Skip to content

Commit

Permalink
Using resources (#33)
Browse files Browse the repository at this point in the history
* Using resources

* update markdowns

Signed-off-by: Jiaxiao Zhou (Mossaka) <duibao55328@gmail.com>

---------

Signed-off-by: Jiaxiao Zhou (Mossaka) <duibao55328@gmail.com>
Co-authored-by: Jiaxiao Zhou (Mossaka) <duibao55328@gmail.com>
  • Loading branch information
vigoo and Mossaka committed Jan 5, 2024
1 parent 3b7e098 commit 0bce169
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 221 deletions.
143 changes: 59 additions & 84 deletions keyvalue-handle-watch.md

Large diffs are not rendered by default.

137 changes: 56 additions & 81 deletions keyvalue.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions wit/atomic.wit
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ interface atomic {
/// with the value set to the given delta.
///
/// If any other error occurs, it returns an error.
increment: func(bucket: bucket, key: key, delta: u64) -> result<u64, error>;
increment: func(bucket: borrow<bucket>, key: key, delta: u64) -> result<u64, error>;

/// Atomically compare and swap the value associated with the key in the bucket.
/// It returns a boolean indicating if the swap was successful.
///
/// If the key does not exist in the bucket, it returns an error.
compare-and-swap: func(bucket: bucket, key: key, old: u64, new: u64) -> result<bool, error>;
compare-and-swap: func(bucket: borrow<bucket>, key: key, old: u64, new: u64) -> result<bool, error>;
}
10 changes: 5 additions & 5 deletions wit/batch.wit
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ interface batch {
/// incoming-values that can be consumed to get the values.
///
/// If any of the keys do not exist in the bucket, it returns an error.
get-many: func(bucket: bucket, keys: keys) -> result<list<incoming-value>, error>;
get-many: func(bucket: borrow<bucket>, keys: keys) -> result<list<incoming-value>, error>;

/// Get all the keys in the bucket. It returns a list of keys.
get-keys: func(bucket: bucket) -> keys;
get-keys: func(bucket: borrow<bucket>) -> keys;

/// Set the values associated with the keys in the bucket. If the key already
/// exists in the bucket, it overwrites the value.
///
/// If any of the keys do not exist in the bucket, it creates a new key-value pair.
/// If any other error occurs, it returns an error.
set-many: func(bucket: bucket, key-values: list<tuple<key, outgoing-value>>) -> result<_, error>;
set-many: func(bucket: borrow<bucket>, key-values: list<tuple<key, borrow<outgoing-value>>>) -> result<_, error>;

/// Delete the key-value pairs associated with the keys in the bucket.
///
/// If any of the keys do not exist in the bucket, it skips the key.
/// If any other error occurs, it returns an error.
delete-many: func(bucket: bucket, keys: keys) -> result<_, error>;
}
delete-many: func(bucket: borrow<bucket>, keys: keys) -> result<_, error>;
}
42 changes: 21 additions & 21 deletions wit/caching.wit
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ interface cache {
// of the `get` method, the outer `option` returns `none` when the pollable
// is not yet ready and the inner `option` returns `none` when the
// requested key wasn't present.
type future-get-result = u32;
drop-future-get-result: func(f: future-get-result);
future-get-result-get: func(f: future-get-result) -> option<result<option<incoming-value>, error>>;
listen-to-future-get-result: func(f: future-get-result) -> pollable;
resource future-get-result {
future-get-result-get: func() -> option<result<option<incoming-value>, error>>;
listen-to-future-get-result: func() -> pollable;
}

// The `exists` operation returns whether a value was previously `set` for
// the given key within the TTL.
exists: func(k: key) -> future-exists-result;

// This block defines a special resource type used by `exists` to emulate
// `future<result<bool,error>>`.
type future-exists-result = u32;
drop-future-exists-result: func(f: future-exists-result);
future-exists-result-get: func(f: future-exists-result) -> option<result<bool, error>>;
listen-to-future-exists-result: func(f: future-exists-result) -> pollable;
resource future-exists-result {
future-exists-result-get: func() -> option<result<bool, error>>;
listen-to-future-exists-result: func() -> pollable;
}

// The `set` operation sets the given value for the given key for the given
// time-to-live (TTL) duration, if supplied, specified in milliseconds. If
Expand All @@ -46,14 +46,14 @@ interface cache {
// value is updated in-place. In the common case of computing and caching a
// value if the given key is not already in the cache, consider using
// `get-or-set` (below) intead of separate `get` and `set` operations.
set: func(k: key, v: outgoing-value, TTL-ms: option<u32>) -> future-result;
set: func(k: key, v: borrow<outgoing-value>, TTL-ms: option<u32>) -> future-result;

// This block defines a special resource type used by `set` and `delete` to
// emulate `future<result<_,error>>`.
type future-result = u32;
drop-future-result: func(f: future-result);
future-result-get: func(f: future-result) -> option<result<_, error>>;
listen-to-future-result: func(f: future-result) -> pollable;
resource future-result {
future-result-get: func() -> option<result<_, error>>;
listen-to-future-result: func() -> pollable;
}

// The `get-or-set` operation asynchronously returns one of two cases
// enumerated by `get-or-set-entry`: in the `occupied` case, the given key
Expand All @@ -73,10 +73,10 @@ interface cache {

// This block defines a special resource type used by `get-or-set` to
// emulate `future<result<get-or-set-entry,error>>`.
type future-get-or-set-result = u32;
drop-future-get-or-set-result: func(f: future-get-or-set-result);
future-get-or-set-result-get: func(f: future-get-or-set-result) -> option<result<get-or-set-entry, error>>;
listen-to-future-get-or-set-result: func(f: future-get-or-set-result) -> pollable;
resource future-get-or-set-result {
future-get-or-set-result-get: func() -> option<result<get-or-set-entry, error>>;
listen-to-future-get-or-set-result: func() -> pollable;
}

// The following block defines the `vacancy` resource type. (When resource
// types are added, the `u32` type aliases can be replaced by proper
Expand All @@ -85,14 +85,14 @@ interface cache {
// indicate an error that prevents calling `fill`. An implementation MAY
// have a timeout that drops a vacancy that hasn't been filled in order
// to unblock other waiting `get-or-set` callers.
type vacancy = u32;
drop-vacancy: func(v: vacancy);
vacancy-fill: func(v: vacancy, TTL-ms: option<u32>) -> outgoing-value;
resource vacancy {
vacancy-fill: func(TTL-ms: option<u32>) -> outgoing-value;
}

// The `delete` operation removes any value with the given key from the
// cache. Like all cache operations, `delete` is weakly ordered and thus
// concurrent `get` calls may still see deleted keys for a period of time.
// Additionally, due to weak ordering, concurrent `set` calls for the same
// key may or may not get deleted.
delete: func(k: key) -> future-result;
}
}
6 changes: 3 additions & 3 deletions wit/error.wit
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ interface wasi-cloud-error {
/// of the error. In the future, this will be extended to provide more information
/// about the error.
// Soon: switch to `resource error { ... }`
type error = u32;
drop-error: func(error: error);
trace: func(error: error) -> string;
resource error {
trace: func() -> string;
}
}
4 changes: 2 additions & 2 deletions wit/handle-watch.wit
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ interface handle-watch {

/// Handle the set event for the given bucket and key.
/// It returns a incoming-value that can be consumed to get the value.
on-set: func(bucket: bucket, key: key, incoming-value: incoming-value);
on-set: func(bucket: bucket, key: key, incoming-value: borrow<incoming-value>);

/// Handle the delete event for the given bucket and key.
on-delete: func(bucket: bucket, key: key);
}
}
10 changes: 5 additions & 5 deletions wit/readwrite.wit
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ interface readwrite {
/// that can be consumed to get the value.
///
/// If the key does not exist in the bucket, it returns an error.
get: func(bucket: bucket, key: key) -> result<incoming-value, error>;
get: func(bucket: borrow<bucket>, key: key) -> result<incoming-value, error>;

/// Set the value associated with the key in the bucket. If the key already
/// exists in the bucket, it overwrites the value.
///
/// If the key does not exist in the bucket, it creates a new key-value pair.
/// If any other error occurs, it returns an error.
set: func(bucket: bucket, key: key, outgoing-value: outgoing-value) -> result<_, error>;
set: func(bucket: borrow<bucket>, key: key, outgoing-value: borrow<outgoing-value>) -> result<_, error>;

/// Delete the key-value pair associated with the key in the bucket.
///
/// If the key does not exist in the bucket, it returns an error.
delete: func(bucket: bucket, key: key) -> result<_, error>;
delete: func(bucket: borrow<bucket>, key: key) -> result<_, error>;

/// Check if the key exists in the bucket.
exists: func(bucket: bucket, key: key) -> result<bool, error>;
}
exists: func(bucket: borrow<bucket>, key: key) -> result<bool, error>;
}
34 changes: 17 additions & 17 deletions wit/types.wit
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
interface types {
/// A bucket is a collection of key-value pairs. Each key-value pair is stored
/// as a entry in the bucket, and the bucket itself acts as a collection of all
/// these entries.
/// these entries.
///
/// It is worth noting that the exact terminology for bucket in key-value stores
/// can very depending on the specific implementation. For example,
Expand All @@ -16,10 +16,9 @@ interface types {
///
/// In this interface, we use the term `bucket` to refer to a collection of key-value
// Soon: switch to `resource bucket { ... }`
type bucket = u32;
drop-bucket: func(bucket: bucket);
open-bucket: func(name: string) -> result<bucket, error>;

resource bucket {
open-bucket: static func(name: string) -> result<bucket, error>;
}
/// A key is a unique identifier for a value in a bucket. The key is used to
/// retrieve the value from the bucket.
type key = string;
Expand All @@ -33,13 +32,14 @@ interface types {
/// that can be represented in a byte array. It provides a way to write the value
/// to the output-stream defined in the `wasi-io` interface.
// Soon: switch to `resource value { ... }`
type outgoing-value = u32;
resource outgoing-value {
new-outgoing-value: static func() -> outgoing-value;
outgoing-value-write-body-async: func() -> result<outgoing-value-body-async, error>;
outgoing-value-write-body-sync: func(value: outgoing-value-body-sync) -> result<_, error>;
}
type outgoing-value-body-async = output-stream;
type outgoing-value-body-sync = list<u8>;
drop-outgoing-value: func(outgoing-value: outgoing-value);
new-outgoing-value: func() -> outgoing-value;
outgoing-value-write-body-async: func(outgoing-value: outgoing-value) -> result<outgoing-value-body-async, error>;
outgoing-value-write-body-sync: func(outgoing-value: outgoing-value, value: outgoing-value-body-sync) -> result<_, error>;


/// A incoming-value is a wrapper around a value. It provides a way to read the value
/// from the input-stream defined in the `wasi-io` interface.
Expand All @@ -50,11 +50,11 @@ interface types {
/// 2. `incoming-value-consume-async` consumes the value asynchronously and returns the
/// value as an input-stream.
// Soon: switch to `resource incoming-value { ... }`
type incoming-value = u32;
type incoming-value-async-body = input-stream;
type incoming-value-sync-body = list<u8>;
drop-incoming-value: func(incoming-value: incoming-value);
incoming-value-consume-sync: func(incoming-value: incoming-value) -> result<incoming-value-sync-body, error>;
incoming-value-consume-async: func(incoming-value: incoming-value) -> result<incoming-value-async-body, error>;
size: func(incoming-value: incoming-value) -> u64;
resource incoming-value {
incoming-value-consume-sync: func() -> result<incoming-value-sync-body, error>;
incoming-value-consume-async: func() -> result<incoming-value-async-body, error>;
size: func() -> u64;
}
type incoming-value-async-body = input-stream;
type incoming-value-sync-body = list<u8>;
}
2 changes: 1 addition & 1 deletion wit/world.wit
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ world keyvalue {
world keyvalue-handle-watch {
include keyvalue;
export handle-watch;
}
}

0 comments on commit 0bce169

Please sign in to comment.