// A generic keyvalue interface for WASI.
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.
	///
	/// It is worth noting that the exact terminology for bucket in key-value stores
	/// can very depending on the specific implementation. For example,
	/// 1. Amazon DynamoDB calls a collection of key-value pairs a table
	/// 2. Redis has hashes, sets, and sorted sets as different types of collections
	/// 3. Cassandra calls a collection of key-value pairs a column family
	/// 4. MongoDB calls a collection of key-value pairs a collection
	/// 5. Riak calls a collection of key-value pairs a bucket
	/// 6. Memcached calls a collection of key-value pairs a slab
	/// 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 { ... }`
	resource bucket {
		/// Opens a bucket with the given name.
		///
		/// If any error occurs, including if the bucket does not exist, it returns an `Err(error)`.
		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;

	use wasi:io/streams@0.2.3.{input-stream, output-stream};
	use wasi-keyvalue-error.{ error };
	/// 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 { ... }`
	resource outgoing-value {
		new-outgoing-value: static func() -> outgoing-value;
		/// Writes the value to the output-stream asynchronously.
		/// If any other error occurs, it returns an `Err(error)`.
		outgoing-value-write-body-async: func() -> result<outgoing-value-body-async, error>;
		/// Writes the value to the output-stream synchronously.
		/// If any other error occurs, it returns an `Err(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>;

	/// 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.
	///
	/// The incoming-value provides two ways to consume the value:
	/// 1. `incoming-value-consume-sync` consumes the value synchronously and returns the
	///    value as a `list<u8>`.
	/// 2. `incoming-value-consume-async` consumes the value asynchronously and returns the
	///    value as an `input-stream`.
	/// In addition, it provides a `incoming-value-size` function to get the size of the value.
	/// This is useful when the value is large and the caller wants to allocate a buffer of
	/// the right size to consume the value.
	// Soon: switch to `resource incoming-value { ... }`
	resource incoming-value {
		/// Consumes the value synchronously and returns the value as a list of bytes.
		/// If any other error occurs, it returns an `Err(error)`.
    	incoming-value-consume-sync: func() -> result<incoming-value-sync-body, error>;
		/// Consumes the value asynchronously and returns the value as an `input-stream`.
		/// If any other error occurs, it returns an `Err(error)`.
	    incoming-value-consume-async: func() -> result<incoming-value-async-body, error>;
		/// The size of the value in bytes.
		/// If the size is unknown or unavailable, this function returns an `Err(error)`.
	    incoming-value-size: func() -> result<u64, error>;
	}
    type incoming-value-async-body = input-stream;
    type incoming-value-sync-body = list<u8>;
}
