Skip to content
Merged
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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "wasi-nn"]
path = wasi-nn
url = https://github.com/WebAssembly/wasi-nn.git
[submodule "wit"]
path = wit
url = https://github.com/G-Core/FastEdge-wit.git
6 changes: 3 additions & 3 deletions examples/key-value/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ fn main(req: Request<Body>) -> Result<Response<Body>> {
}

body.write_all(b"zrange()\n")?;
match store.zrange("myset", 0.0, 100.0) {
match store.zrange_by_score("myset", 0.0, 100.0) {
Ok(values) => {
for value in values {
for (value, score) in values {
body.write_all(b"get_by_range=")?;
body.extend(value);
body.write_all(b"\n")?;
body.write_all(format!(", {}\n", score).as_bytes())?;
}
}
Err(error) => {
Expand Down
29 changes: 23 additions & 6 deletions src/proxywasm/key_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,17 @@ impl Store {
}
}

/// Get the values associated with the specified `key` stored in sorted set
/// Get all the elements with score from the sorted set at `key` with a f64 score between min and max
/// (including elements with score equal to min or max). The elements are considered to be ordered from low to high
/// scores.
///
/// Returns empty `Vec` if the key does not exist or min and max are out of index.
pub fn zrange(&self, key: &str, min: f64, max: f64) -> Result<Vec<Vec<u8>>, Error> {
/// Returns empty `Vec` if the key does not exist or min and max are out of score.
pub fn zrange_by_score(&self, key: &str, min: f64, max: f64) -> Result<Vec<(Vec<u8>, f64)>, Error> {
let mut return_data: *mut u8 = null_mut();
let mut return_size: usize = 0;

unsafe {
match super::proxy_kv_store_zrange(
match super::proxy_kv_store_zrange_by_score(
self.handle,
key.as_ptr(),
key.len(),
Expand All @@ -142,9 +144,24 @@ impl Store {
if !return_data.is_null() {
let data = Vec::from_raw_parts(return_data, return_size, return_size);

let data: Vec<Vec<u8>> = utils::deserialize_list(&data)
let data: Vec<(Vec<u8>, f64)> = utils::deserialize_list(&data)
.into_iter()
.map(|v| v.to_vec())
.map(|v| {
let mut value = v.to_vec();
let sz = size_of::<f64>();
Copy link

Copilot AI Nov 3, 2025

Choose a reason for hiding this comment

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

Missing import for size_of. Add use std::mem::size_of; to the imports section at the top of the file.

Copilot uses AI. Check for mistakes.
if value.len() > sz {
let npos = value.len() - sz;
let score = value.split_off(npos);
let score = f64::from_le_bytes(
<[u8; 8]>::try_from(&score[0..sz]).expect("Failed to convert score bytes to f64: expected 8 bytes"),
);
(value, score)
} else {
// return an empty vector and 0.0 score if deserialization fails
// empty key should never happen
(vec![], 0.0)
}
})
.collect();
Ok(data)
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/proxywasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ extern "C" {
return_value_size: *mut usize,
) -> u32;

fn proxy_kv_store_zrange(
fn proxy_kv_store_zrange_by_score(
handle: u32,
key_data: *const u8,
key_size: usize,
Expand Down
1 change: 1 addition & 0 deletions wit
Submodule wit added at 561aa9
6 changes: 0 additions & 6 deletions wit/dictionary.wit

This file was deleted.

5 changes: 0 additions & 5 deletions wit/http-client.wit

This file was deleted.

4 changes: 0 additions & 4 deletions wit/http-handler.wit

This file was deleted.

38 changes: 0 additions & 38 deletions wit/http.wit

This file was deleted.

54 changes: 0 additions & 54 deletions wit/key-value.wit

This file was deleted.

20 changes: 0 additions & 20 deletions wit/secret.wit

This file was deleted.

12 changes: 0 additions & 12 deletions wit/world.wit

This file was deleted.