Skip to content
This repository has been archived by the owner on Jun 10, 2024. It is now read-only.

Commit

Permalink
Updating in-memory keyvalue to wasmcloud:keyvalue contract (#11)
Browse files Browse the repository at this point in the history
* replaced all references to wascc-codec::keyvalue to interface

* addressed clippy warnings, removed unused variables
  • Loading branch information
brooksmtownsend authored Feb 8, 2021
1 parent 62a041e commit 7ad5bd6
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 123 deletions.
12 changes: 7 additions & 5 deletions keyvalue-provider/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "keyvalue"
version = "0.3.0"
authors = ["Kevin Hoffman <[email protected]>"]
name = "inmemory-keyvalue"
version = "0.4.0"
authors = ["wasmCloud Team"]
edition = "2018"

[lib]
Expand All @@ -12,6 +12,8 @@ crate-type = ["cdylib", "rlib"]
static_plugin = []

[dependencies]
wascc-codec = { version = "0.7.0", path = "../../wascc-codec" }
wascc-codec = "0.9.0"
log = "0.4.8"
env_logger = "0.7.1"
env_logger = "0.7.1"
actor-keyvalue = { git = "https://github.com/wasmcloud/actor-interfaces", branch = "main"}
actor-core = { git = "https://github.com/wasmcloud/actor-interfaces", branch = "main"}
2 changes: 1 addition & 1 deletion keyvalue-provider/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Key-Value Capability Provider

This is a tutorial capability provider. It supplies an implementation for the `wascc:keyvalue` capability ID and can be used as a testing alternative for the **Redis** capability provider. The usual caveats should apply - this provider's data only lasts as long as the host runtime, so it should only be used for isolated testing and experimentation, and obviously won't provide distributed cached data in production.
This is a tutorial capability provider. It supplies an implementation for the `wasmcloud:keyvalue` capability ID and can be used as a testing alternative for the **Redis** capability provider. The usual caveats should apply - this provider's data only lasts as long as the host runtime, so it should only be used for isolated testing and experimentation, and obviously won't provide distributed cached data in production.

This provider was created using the `new-provider-template` cargo generation template as a starter.
31 changes: 18 additions & 13 deletions keyvalue-provider/src/kv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl KeyValueStore {
}
}

pub fn incr(&mut self, key: &str, value: i32) -> Result<i32, Box<dyn Error>> {
pub fn incr(&mut self, key: &str, value: i32) -> Result<i32, Box<dyn Error + Send + Sync>> {
let mut orig = 0;
self.items
.entry(key.to_string())
Expand All @@ -35,16 +35,16 @@ impl KeyValueStore {
Ok(orig + value)
}

pub fn del(&mut self, key: &str) -> Result<(), Box<dyn Error>> {
pub fn del(&mut self, key: &str) -> Result<(), Box<dyn Error + Send + Sync>> {
self.items.remove(key);
Ok(())
}

pub fn exists(&self, key: &str) -> Result<bool, Box<dyn Error>> {
pub fn exists(&self, key: &str) -> Result<bool, Box<dyn Error + Send + Sync>> {
Ok(self.items.contains_key(key))
}

pub fn get(&self, key: &str) -> Result<String, Box<dyn Error>> {
pub fn get(&self, key: &str) -> Result<String, Box<dyn Error + Send + Sync>> {
self.items.get(key).map_or_else(
|| Err("No such key".into()),
|v| {
Expand All @@ -57,7 +57,12 @@ impl KeyValueStore {
)
}

pub fn lrange(&self, key: &str, start: i32, stop: i32) -> Result<Vec<String>, Box<dyn Error>> {
pub fn lrange(
&self,
key: &str,
start: i32,
stop: i32,
) -> Result<Vec<String>, Box<dyn Error + Send + Sync>> {
let start = start.max(0);
self.items.get(key).map_or_else(
|| Ok(vec![]),
Expand All @@ -72,7 +77,7 @@ impl KeyValueStore {
)
}

pub fn lpush(&mut self, key: &str, value: String) -> Result<i32, Box<dyn Error>> {
pub fn lpush(&mut self, key: &str, value: String) -> Result<i32, Box<dyn Error + Send + Sync>> {
let mut len = 1;
self.items
.entry(key.to_string())
Expand All @@ -89,7 +94,7 @@ impl KeyValueStore {
Ok(len as _)
}

pub fn set(&mut self, key: &str, value: String) -> Result<(), Box<dyn Error>> {
pub fn set(&mut self, key: &str, value: String) -> Result<(), Box<dyn Error + Send + Sync>> {
self.items
.entry(key.to_string())
.and_modify(|v| {
Expand All @@ -101,7 +106,7 @@ impl KeyValueStore {
Ok(())
}

pub fn lrem(&mut self, key: &str, value: String) -> Result<i32, Box<dyn Error>> {
pub fn lrem(&mut self, key: &str, value: String) -> Result<i32, Box<dyn Error + Send + Sync>> {
let mut len: i32 = 0;
self.items.entry(key.to_string()).and_modify(|v| {
if let KeyValueItem::List(ref l) = v {
Expand All @@ -117,7 +122,7 @@ impl KeyValueStore {
Ok(len)
}

pub fn sadd(&mut self, key: &str, value: String) -> Result<i32, Box<dyn Error>> {
pub fn sadd(&mut self, key: &str, value: String) -> Result<i32, Box<dyn Error + Send + Sync>> {
let mut len: i32 = 1;
self.items
.entry(key.to_string())
Expand All @@ -131,7 +136,7 @@ impl KeyValueStore {
Ok(len)
}

pub fn srem(&mut self, key: &str, value: String) -> Result<i32, Box<dyn Error>> {
pub fn srem(&mut self, key: &str, value: String) -> Result<i32, Box<dyn Error + Send + Sync>> {
let mut len: i32 = 0;
self.items
.entry(key.to_string())
Expand All @@ -145,7 +150,7 @@ impl KeyValueStore {
Ok(len)
}

pub fn sunion(&self, keys: Vec<String>) -> Result<Vec<String>, Box<dyn Error>> {
pub fn sunion(&self, keys: Vec<String>) -> Result<Vec<String>, Box<dyn Error + Send + Sync>> {
let union = self
.items
.iter()
Expand All @@ -165,7 +170,7 @@ impl KeyValueStore {
Ok(union.iter().cloned().collect())
}

pub fn sinter(&self, keys: Vec<String>) -> Result<Vec<String>, Box<dyn Error>> {
pub fn sinter(&self, keys: Vec<String>) -> Result<Vec<String>, Box<dyn Error + Send + Sync>> {
let sets: Vec<HashSet<String>> = self
.items
.iter()
Expand All @@ -188,7 +193,7 @@ impl KeyValueStore {
Ok(inter.cloned().collect())
}

pub fn smembers(&self, key: String) -> Result<Vec<String>, Box<dyn Error>> {
pub fn smembers(&self, key: String) -> Result<Vec<String>, Box<dyn Error + Send + Sync>> {
self.items.get(&key).map_or_else(
|| Ok(vec![]),
|v| {
Expand Down
Loading

0 comments on commit 7ad5bd6

Please sign in to comment.