Skip to content

Commit 0bce169

Browse files
vigooMossaka
andauthored
Using resources (#33)
* Using resources * update markdowns Signed-off-by: Jiaxiao Zhou (Mossaka) <[email protected]> --------- Signed-off-by: Jiaxiao Zhou (Mossaka) <[email protected]> Co-authored-by: Jiaxiao Zhou (Mossaka) <[email protected]>
1 parent 3b7e098 commit 0bce169

10 files changed

+171
-221
lines changed

keyvalue-handle-watch.md

+59-84
Large diffs are not rendered by default.

keyvalue.md

+56-81
Large diffs are not rendered by default.

wit/atomic.wit

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ interface atomic {
1010
/// with the value set to the given delta.
1111
///
1212
/// If any other error occurs, it returns an error.
13-
increment: func(bucket: bucket, key: key, delta: u64) -> result<u64, error>;
13+
increment: func(bucket: borrow<bucket>, key: key, delta: u64) -> result<u64, error>;
1414

1515
/// Atomically compare and swap the value associated with the key in the bucket.
1616
/// It returns a boolean indicating if the swap was successful.
1717
///
1818
/// If the key does not exist in the bucket, it returns an error.
19-
compare-and-swap: func(bucket: bucket, key: key, old: u64, new: u64) -> result<bool, error>;
19+
compare-and-swap: func(bucket: borrow<bucket>, key: key, old: u64, new: u64) -> result<bool, error>;
2020
}

wit/batch.wit

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@ interface batch {
77
/// incoming-values that can be consumed to get the values.
88
///
99
/// If any of the keys do not exist in the bucket, it returns an error.
10-
get-many: func(bucket: bucket, keys: keys) -> result<list<incoming-value>, error>;
10+
get-many: func(bucket: borrow<bucket>, keys: keys) -> result<list<incoming-value>, error>;
1111

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

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

2222
/// Delete the key-value pairs associated with the keys in the bucket.
2323
///
2424
/// If any of the keys do not exist in the bucket, it skips the key.
2525
/// If any other error occurs, it returns an error.
26-
delete-many: func(bucket: bucket, keys: keys) -> result<_, error>;
27-
}
26+
delete-many: func(bucket: borrow<bucket>, keys: keys) -> result<_, error>;
27+
}

wit/caching.wit

+21-21
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,21 @@ interface cache {
2323
// of the `get` method, the outer `option` returns `none` when the pollable
2424
// is not yet ready and the inner `option` returns `none` when the
2525
// requested key wasn't present.
26-
type future-get-result = u32;
27-
drop-future-get-result: func(f: future-get-result);
28-
future-get-result-get: func(f: future-get-result) -> option<result<option<incoming-value>, error>>;
29-
listen-to-future-get-result: func(f: future-get-result) -> pollable;
26+
resource future-get-result {
27+
future-get-result-get: func() -> option<result<option<incoming-value>, error>>;
28+
listen-to-future-get-result: func() -> pollable;
29+
}
3030

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

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

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

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

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

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

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

9292
// The `delete` operation removes any value with the given key from the
9393
// cache. Like all cache operations, `delete` is weakly ordered and thus
9494
// concurrent `get` calls may still see deleted keys for a period of time.
9595
// Additionally, due to weak ordering, concurrent `set` calls for the same
9696
// key may or may not get deleted.
9797
delete: func(k: key) -> future-result;
98-
}
98+
}

wit/error.wit

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ interface wasi-cloud-error {
44
/// of the error. In the future, this will be extended to provide more information
55
/// about the error.
66
// Soon: switch to `resource error { ... }`
7-
type error = u32;
8-
drop-error: func(error: error);
9-
trace: func(error: error) -> string;
7+
resource error {
8+
trace: func() -> string;
9+
}
1010
}

wit/handle-watch.wit

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ interface handle-watch {
55

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

1010
/// Handle the delete event for the given bucket and key.
1111
on-delete: func(bucket: bucket, key: key);
12-
}
12+
}

wit/readwrite.wit

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@ interface readwrite {
77
/// that can be consumed to get the value.
88
///
99
/// If the key does not exist in the bucket, it returns an error.
10-
get: func(bucket: bucket, key: key) -> result<incoming-value, error>;
10+
get: func(bucket: borrow<bucket>, key: key) -> result<incoming-value, error>;
1111

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

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

2424
/// Check if the key exists in the bucket.
25-
exists: func(bucket: bucket, key: key) -> result<bool, error>;
26-
}
25+
exists: func(bucket: borrow<bucket>, key: key) -> result<bool, error>;
26+
}

wit/types.wit

+17-17
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
interface types {
33
/// A bucket is a collection of key-value pairs. Each key-value pair is stored
44
/// as a entry in the bucket, and the bucket itself acts as a collection of all
5-
/// these entries.
5+
/// these entries.
66
///
77
/// It is worth noting that the exact terminology for bucket in key-value stores
88
/// can very depending on the specific implementation. For example,
@@ -16,10 +16,9 @@ interface types {
1616
///
1717
/// In this interface, we use the term `bucket` to refer to a collection of key-value
1818
// Soon: switch to `resource bucket { ... }`
19-
type bucket = u32;
20-
drop-bucket: func(bucket: bucket);
21-
open-bucket: func(name: string) -> result<bucket, error>;
22-
19+
resource bucket {
20+
open-bucket: static func(name: string) -> result<bucket, error>;
21+
}
2322
/// A key is a unique identifier for a value in a bucket. The key is used to
2423
/// retrieve the value from the bucket.
2524
type key = string;
@@ -33,13 +32,14 @@ interface types {
3332
/// that can be represented in a byte array. It provides a way to write the value
3433
/// to the output-stream defined in the `wasi-io` interface.
3534
// Soon: switch to `resource value { ... }`
36-
type outgoing-value = u32;
35+
resource outgoing-value {
36+
new-outgoing-value: static func() -> outgoing-value;
37+
outgoing-value-write-body-async: func() -> result<outgoing-value-body-async, error>;
38+
outgoing-value-write-body-sync: func(value: outgoing-value-body-sync) -> result<_, error>;
39+
}
3740
type outgoing-value-body-async = output-stream;
3841
type outgoing-value-body-sync = list<u8>;
39-
drop-outgoing-value: func(outgoing-value: outgoing-value);
40-
new-outgoing-value: func() -> outgoing-value;
41-
outgoing-value-write-body-async: func(outgoing-value: outgoing-value) -> result<outgoing-value-body-async, error>;
42-
outgoing-value-write-body-sync: func(outgoing-value: outgoing-value, value: outgoing-value-body-sync) -> result<_, error>;
42+
4343

4444
/// A incoming-value is a wrapper around a value. It provides a way to read the value
4545
/// from the input-stream defined in the `wasi-io` interface.
@@ -50,11 +50,11 @@ interface types {
5050
/// 2. `incoming-value-consume-async` consumes the value asynchronously and returns the
5151
/// value as an input-stream.
5252
// Soon: switch to `resource incoming-value { ... }`
53-
type incoming-value = u32;
54-
type incoming-value-async-body = input-stream;
55-
type incoming-value-sync-body = list<u8>;
56-
drop-incoming-value: func(incoming-value: incoming-value);
57-
incoming-value-consume-sync: func(incoming-value: incoming-value) -> result<incoming-value-sync-body, error>;
58-
incoming-value-consume-async: func(incoming-value: incoming-value) -> result<incoming-value-async-body, error>;
59-
size: func(incoming-value: incoming-value) -> u64;
53+
resource incoming-value {
54+
incoming-value-consume-sync: func() -> result<incoming-value-sync-body, error>;
55+
incoming-value-consume-async: func() -> result<incoming-value-async-body, error>;
56+
size: func() -> u64;
57+
}
58+
type incoming-value-async-body = input-stream;
59+
type incoming-value-sync-body = list<u8>;
6060
}

wit/world.wit

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ world keyvalue {
99
world keyvalue-handle-watch {
1010
include keyvalue;
1111
export handle-watch;
12-
}
12+
}

0 commit comments

Comments
 (0)