Skip to content

Commit 90bd149

Browse files
committed
impl: backends independent from filesystems
- New `KvsBackend` API - independent from filesystem. - Make `JsonBackend` explicitly default, allow others. - Add `JsonBackendBuilder`. - This is required to allow for defaults override without setters. - E.g., `snapshot_max_count` can be set, but shouldn't change after init. - Update tests.
1 parent c0db2e5 commit 90bd149

File tree

19 files changed

+1044
-737
lines changed

19 files changed

+1044
-737
lines changed

src/rust/rust_kvs/examples/basic.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ use std::collections::HashMap;
88
use tempfile::tempdir;
99

1010
fn main() -> Result<(), ErrorCode> {
11-
// Temporary directory.
11+
// Temporary directory and common backend.
1212
let dir = tempdir()?;
13-
let dir_string = dir.path().to_string_lossy().to_string();
13+
let dir_path = dir.path().to_path_buf();
14+
let backend = Box::new(JsonBackendBuilder::new().working_dir(dir_path).build());
1415

1516
// Instance ID for KVS object instances.
1617
let instance_id = InstanceId(0);
@@ -20,7 +21,7 @@ fn main() -> Result<(), ErrorCode> {
2021
// `kvs_load` is explicitly set to `KvsLoad::Optional`, but this is the default value.
2122
// KVS files are not required.
2223
let builder = KvsBuilder::new(instance_id)
23-
.dir(dir_string.clone())
24+
.backend(backend.clone())
2425
.kvs_load(KvsLoad::Optional);
2526
let kvs = builder.build()?;
2627

@@ -65,7 +66,7 @@ fn main() -> Result<(), ErrorCode> {
6566
// Build KVS instance for given instance ID and temporary directory.
6667
// `kvs_load` is set to `KvsLoad::Required` - KVS files must already exist from previous KVS instance.
6768
let builder = KvsBuilder::new(instance_id)
68-
.dir(dir_string)
69+
.backend(backend)
6970
.kvs_load(KvsLoad::Required);
7071
let kvs = builder.build()?;
7172

src/rust/rust_kvs/examples/defaults.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ fn create_defaults_file(dir_path: PathBuf, instance_id: InstanceId) -> Result<()
3131
}
3232

3333
fn main() -> Result<(), ErrorCode> {
34-
// Temporary directory.
34+
// Temporary directory and common backend.
3535
let dir = tempdir()?;
36-
let dir_string = dir.path().to_string_lossy().to_string();
36+
let dir_path = dir.path().to_path_buf();
37+
let backend = Box::new(JsonBackendBuilder::new().working_dir(dir_path).build());
3738

3839
// Instance ID for KVS object instances.
3940
let instance_id = InstanceId(0);
@@ -44,7 +45,7 @@ fn main() -> Result<(), ErrorCode> {
4445
// Build KVS instance for given instance ID and temporary directory.
4546
// `defaults` is set to `KvsDefaults::Required` - defaults are required.
4647
let builder = KvsBuilder::new(instance_id)
47-
.dir(dir_string)
48+
.backend(backend.clone())
4849
.defaults(KvsDefaults::Required);
4950
let kvs = builder.build()?;
5051

src/rust/rust_kvs/examples/snapshots.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ use rust_kvs::prelude::*;
66
use tempfile::tempdir;
77

88
fn main() -> Result<(), ErrorCode> {
9-
// Temporary directory.
9+
// Temporary directory and common backend.
1010
let dir = tempdir()?;
11-
let dir_string = dir.path().to_string_lossy().to_string();
11+
let dir_path = dir.path().to_path_buf();
12+
let backend = Box::new(JsonBackendBuilder::new().working_dir(dir_path).build());
1213

1314
// Instance ID for KVS object instances.
1415
let instance_id = InstanceId(0);
@@ -17,7 +18,7 @@ fn main() -> Result<(), ErrorCode> {
1718
println!("-> `snapshot_count` and `snapshot_max_count` usage");
1819

1920
// Build KVS instance for given instance ID and temporary directory.
20-
let builder = KvsBuilder::new(instance_id).dir(dir_string.clone());
21+
let builder = KvsBuilder::new(instance_id).backend(backend.clone());
2122
let kvs = builder.build()?;
2223

2324
let max_count = kvs.snapshot_max_count() as u32;
@@ -38,7 +39,7 @@ fn main() -> Result<(), ErrorCode> {
3839
println!("-> `snapshot_restore` usage");
3940

4041
// Build KVS instance for given instance ID and temporary directory.
41-
let builder = KvsBuilder::new(instance_id).dir(dir_string.clone());
42+
let builder = KvsBuilder::new(instance_id).backend(backend.clone());
4243
let kvs = builder.build()?;
4344

4445
let max_count = kvs.snapshot_max_count() as u32;

0 commit comments

Comments
 (0)