From 439719732c636e3f4b0802a2bd97c5bea78e9d16 Mon Sep 17 00:00:00 2001 From: Tpt Date: Mon, 10 Sep 2018 14:42:45 +0200 Subject: [PATCH] Adds method to get the size of a Graph or a Dataset --- Cargo.toml | 1 + src/store/mod.rs | 8 ++++++++ src/store/store.rs | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 0e791141..2cd5faf6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,7 @@ lazy_static = "1" rocksdb = "0.10" url = "1" uuid = { version = "0.6", features = ["v4"] } +bzip2 = "0.3" byteorder = "1" [build-dependencies] diff --git a/src/store/mod.rs b/src/store/mod.rs index adec5b03..bd3fa28b 100644 --- a/src/store/mod.rs +++ b/src/store/mod.rs @@ -57,6 +57,10 @@ pub trait Graph { fn insert(&self, triple: &Triple) -> Result<()>; fn remove(&self, triple: &Triple) -> Result<()>; + + fn len(&self) -> Result; + + fn is_empty(&self) -> Result; } pub trait NamedGraph: Graph { @@ -128,4 +132,8 @@ pub trait Dataset { fn insert(&self, quad: &Quad) -> Result<()>; fn remove(&self, quad: &Quad) -> Result<()>; + + fn len(&self) -> Result; + + fn is_empty(&self) -> Result; } diff --git a/src/store/store.rs b/src/store/store.rs index 7d4b37ce..280d1610 100644 --- a/src/store/store.rs +++ b/src/store/store.rs @@ -266,6 +266,14 @@ impl Dataset for StoreDataset { fn remove(&self, quad: &Quad) -> Result<()> { self.store.remove(&self.store.encoder().encode_quad(quad)?) } + + fn len(&self) -> Result { + Ok(self.store.quads()?.count()) + } + + fn is_empty(&self) -> Result { + Ok(self.store.quads()?.any(|_| true)) + } } pub struct StoreNamedGraph { @@ -403,6 +411,17 @@ impl Graph for StoreNamedGraph { .encode_triple_in_graph(triple, self.encoded_name.clone())?, ) } + + fn len(&self) -> Result { + Ok(self.store.quads_for_graph(&self.encoded_name)?.count()) + } + + fn is_empty(&self) -> Result { + Ok(self + .store + .quads_for_graph(&self.encoded_name)? + .any(|_| true)) + } } impl NamedGraph for StoreNamedGraph { @@ -530,6 +549,14 @@ impl Graph for StoreDefaultGraph { self.store .remove(&self.store.encoder().encode_triple_in_graph(triple, None)?) } + + fn len(&self) -> Result { + Ok(self.store.quads_for_graph(&None)?.count()) + } + + fn is_empty(&self) -> Result { + Ok(self.store.quads_for_graph(&None)?.any(|_| true)) + } } pub struct StoreUnionGraph { @@ -650,6 +677,14 @@ impl Graph for StoreUnionGraph { fn remove(&self, triple: &Triple) -> Result<()> { unimplemented!() } + + fn len(&self) -> Result { + Ok(self.store.quads()?.count()) + } + + fn is_empty(&self) -> Result { + Ok(self.store.quads()?.any(|_| true)) + } } pub struct DelegatingBytesStore<'a, S: 'a + BytesStore + Sized>(&'a S);