pub struct Mace { /* private fields */ }Expand description
The main entry point for the Mace storage engine.
Implementations§
Source§impl Mace
impl Mace
Sourcepub fn new(opt: ParsedOptions) -> Result<Self, OpCode>
pub fn new(opt: ParsedOptions) -> Result<Self, OpCode>
Creates a new Mace instance with the given options.
Examples found in repository?
8fn main() -> Result<(), OpCode> {
9 let path = std::env::temp_dir().join(format!("mace_observer_{}", std::process::id()));
10 let _ = std::fs::remove_dir_all(&path);
11
12 let observer = Arc::new(InMemoryObserver::new(256));
13 let mut opt = Options::new(&path);
14 opt.observer = observer.clone();
15
16 let db = Mace::new(opt.validate()?)?;
17 let bucket = db.new_bucket("observe", BucketOptions::default())?;
18
19 let tx = bucket.begin()?;
20 tx.put("k1", "v1")?;
21
22 let r = tx.put("k1", "v2");
23 assert_eq!(r.err(), Some(OpCode::AbortTx));
24 drop(tx);
25
26 let tx = bucket.begin()?;
27 tx.put("k2", "v2")?;
28 tx.commit()?;
29
30 let snapshot = observer.snapshot();
31 print_snapshot(&snapshot);
32 Ok(())
33}More examples
3fn main() -> Result<(), OpCode> {
4 let path = std::env::temp_dir().join("mace");
5 let _ = std::fs::remove_dir_all(&path);
6 let opt = Options::new(path).validate()?;
7 let db = Mace::new(opt)?;
8 let bucket = db.new_bucket("test", BucketOptions::default())?;
9
10 // start a read-write transaction
11 let kv = bucket.begin()?;
12 kv.put("foo", "bar")?;
13 kv.put("fool", "+1s")?;
14 kv.put("foolish", "elder")?;
15
16 // can't create two identical keys
17 let r = kv.put("foolish", "114514").err();
18 assert_eq!(r.unwrap(), OpCode::AbortTx);
19
20 // use `update` for exist key or use `upsert` when unsure
21 let r = kv.update("foolish", "114514");
22 assert!(r.is_ok());
23
24 let r = kv.get("foo")?;
25 assert_eq!(r.slice(), "bar".as_bytes());
26 kv.del("foolish")?;
27 kv.commit()?;
28
29 // rollback
30 let kv = bucket.begin()?;
31 kv.put("mo", "ha")?;
32 drop(kv);
33
34 // start a read-only transaction
35 let view = bucket.view()?;
36 let r = view.get("foo")?;
37 assert_eq!(r.slice(), "bar".as_bytes());
38 let r = view.get("mo");
39 assert_eq!(r.err().unwrap(), OpCode::NotFound);
40
41 // prefix scan
42 let r = view.get("foolish");
43 assert!(r.is_err() && r.err().unwrap() == OpCode::NotFound);
44 let iter = view.seek("foo");
45 assert_eq!(iter.count(), 2);
46
47 Ok(())
48}Sourcepub fn new_bucket<S: AsRef<str>>(
&self,
name: S,
opt: BucketOptions,
) -> Result<Bucket, OpCode>
pub fn new_bucket<S: AsRef<str>>( &self, name: S, opt: BucketOptions, ) -> Result<Bucket, OpCode>
Creates a bucket with the given name. NOTE: name must be less than 32 bytes.
Examples found in repository?
8fn main() -> Result<(), OpCode> {
9 let path = std::env::temp_dir().join(format!("mace_observer_{}", std::process::id()));
10 let _ = std::fs::remove_dir_all(&path);
11
12 let observer = Arc::new(InMemoryObserver::new(256));
13 let mut opt = Options::new(&path);
14 opt.observer = observer.clone();
15
16 let db = Mace::new(opt.validate()?)?;
17 let bucket = db.new_bucket("observe", BucketOptions::default())?;
18
19 let tx = bucket.begin()?;
20 tx.put("k1", "v1")?;
21
22 let r = tx.put("k1", "v2");
23 assert_eq!(r.err(), Some(OpCode::AbortTx));
24 drop(tx);
25
26 let tx = bucket.begin()?;
27 tx.put("k2", "v2")?;
28 tx.commit()?;
29
30 let snapshot = observer.snapshot();
31 print_snapshot(&snapshot);
32 Ok(())
33}More examples
3fn main() -> Result<(), OpCode> {
4 let path = std::env::temp_dir().join("mace");
5 let _ = std::fs::remove_dir_all(&path);
6 let opt = Options::new(path).validate()?;
7 let db = Mace::new(opt)?;
8 let bucket = db.new_bucket("test", BucketOptions::default())?;
9
10 // start a read-write transaction
11 let kv = bucket.begin()?;
12 kv.put("foo", "bar")?;
13 kv.put("fool", "+1s")?;
14 kv.put("foolish", "elder")?;
15
16 // can't create two identical keys
17 let r = kv.put("foolish", "114514").err();
18 assert_eq!(r.unwrap(), OpCode::AbortTx);
19
20 // use `update` for exist key or use `upsert` when unsure
21 let r = kv.update("foolish", "114514");
22 assert!(r.is_ok());
23
24 let r = kv.get("foo")?;
25 assert_eq!(r.slice(), "bar".as_bytes());
26 kv.del("foolish")?;
27 kv.commit()?;
28
29 // rollback
30 let kv = bucket.begin()?;
31 kv.put("mo", "ha")?;
32 drop(kv);
33
34 // start a read-only transaction
35 let view = bucket.view()?;
36 let r = view.get("foo")?;
37 assert_eq!(r.slice(), "bar".as_bytes());
38 let r = view.get("mo");
39 assert_eq!(r.err().unwrap(), OpCode::NotFound);
40
41 // prefix scan
42 let r = view.get("foolish");
43 assert!(r.is_err() && r.err().unwrap() == OpCode::NotFound);
44 let iter = view.seek("foo");
45 assert_eq!(iter.count(), 2);
46
47 Ok(())
48}Sourcepub fn get_bucket<S: AsRef<str>>(&self, name: S) -> Result<Bucket, OpCode>
pub fn get_bucket<S: AsRef<str>>(&self, name: S) -> Result<Bucket, OpCode>
Gets an existing bucket with the given name. NOTE: name must be less than 32 bytes.
Sourcepub fn update_bucket_opt<S: AsRef<str>>(
&self,
name: S,
opt: BucketOptions,
) -> Result<(), OpCode>
pub fn update_bucket_opt<S: AsRef<str>>( &self, name: S, opt: BucketOptions, ) -> Result<(), OpCode>
Updates the persisted bucket-scoped options of an existing bucket
Returns OpCode::Again if the bucket is currently loaded
Returns OpCode::Invalid if the requested BucketOptions conflict with
persisted compatibility-sensitive bucket options
Sourcepub fn active_buckets(&self) -> Vec<String>
pub fn active_buckets(&self) -> Vec<String>
Returns a list of all active bucket names.
Sourcepub fn drop_bucket<S: AsRef<str>>(&self, name: S) -> Result<(), OpCode>
pub fn drop_bucket<S: AsRef<str>>(&self, name: S) -> Result<(), OpCode>
Manually unloads a bucket to release memory.
Sourcepub fn del_bucket<S: AsRef<str>>(&self, name: S) -> Result<(), OpCode>
pub fn del_bucket<S: AsRef<str>>(&self, name: S) -> Result<(), OpCode>
Deletes a bucket and all its data.
Sourcepub fn vacuum_bucket<S: AsRef<str>>(
&self,
name: S,
) -> Result<VacuumStats, OpCode>
pub fn vacuum_bucket<S: AsRef<str>>( &self, name: S, ) -> Result<VacuumStats, OpCode>
Vacuums a bucket by scavenging and compacting its pages
Sourcepub fn is_bucket_vacuuming<S: AsRef<str>>(
&self,
name: S,
) -> Result<bool, OpCode>
pub fn is_bucket_vacuuming<S: AsRef<str>>( &self, name: S, ) -> Result<bool, OpCode>
Returns whether bucket vacuum is currently running
Sourcepub fn vacuum_meta(&self) -> Result<MetaVacuumStats, OpCode>
pub fn vacuum_meta(&self) -> Result<MetaVacuumStats, OpCode>
Vacuums metadata by compacting the manifest btree
Sourcepub fn disable_gc(&self)
pub fn disable_gc(&self)
Disables garbage collection.
Sourcepub fn data_gc_count(&self) -> u64
pub fn data_gc_count(&self) -> u64
Returns the number of data garbage collection cycles performed.
Sourcepub fn blob_gc_count(&self) -> u64
pub fn blob_gc_count(&self) -> u64
Returns the number of blob garbage collection cycles performed.
Sourcepub fn nr_buckets(&self) -> u64
pub fn nr_buckets(&self) -> u64
Returns the total number of buckets, including active and pending deletion ones.