Skip to content

WIP change to use resources #28

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

Closed
wants to merge 1 commit into from
Closed
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
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>;
}
8 changes: 4 additions & 4 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<borrow<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, 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>;
}
46 changes: 23 additions & 23 deletions wit/caching.wit
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,28 @@ interface cache {

// The `get` operation returns the value passed by a previous `set` for the
// same key within the given TTL or none if there is no such value.
get: func(k: key) -> future-get-result;
get: func(k: key) -> borrow<future-get-result>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to return an owned future-get-result resource here for consuming?


// This block defines a special resource type used by `get` to emulate
// `future<result<option<incoming-value>,error>>`. In the return value
// 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 {
get: func() -> option<result<option<borrow<incoming-value>>, error>>;
listen: 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;
exists: func(k: key) -> borrow<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 {
get: func() -> option<result<bool, error>>;
listen: 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: outgoing-value, TTL-ms: option<u32>) -> borrow<future-result>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment as above


// 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 {
get: func() -> option<result<_, error>>;
listen: 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 {
get: func() -> option<result<get-or-set-entry, error>>;
listen: 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 {
fill: func(v: outgoing-value, 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;
delete: func(k: key) -> borrow<future-result>;
}
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: borrow<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);
on-delete: func(bucket: borrow<bucket>, key: key);
}
8 changes: 4 additions & 4 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: 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>;
}
33 changes: 17 additions & 16 deletions wit/types.wit
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ interface types {
/// 7. Azure Cosmos DB calls a collection of key-value pairs a container
///
/// 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: 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.
Expand All @@ -32,14 +31,17 @@ interface types {
/// A value is the data stored in a key-value pair. The value can be of any type
/// 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 {
constructor();

from-list: static func(list<u8>) -> outgoing-value;
from-stream: static func(input-stream) -> outgoing-value;

write-body-sync: func(value: outgoing-value-body-sync) -> result<_, error>;
write-body-async: func() -> result<outgoing-value-body-async, error>;
}
type outgoing-value-body-async = output-stream;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More work is needed here to support streams correctly

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 @@ -49,12 +51,11 @@ interface types {
/// value as a list of bytes.
/// 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;
resource incoming-value {
consume-sync: func() -> result<incoming-value-sync-body, error>;
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>;
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;
}