Adds is_empty and len to MemoryStore

pull/41/head
Tpt 4 years ago
parent 79038afe44
commit be76dcb51d
  1. 5
      js/src/store.rs
  2. 17
      js/test/store.js
  3. 18
      lib/src/store/memory.rs

@ -52,6 +52,11 @@ impl JsMemoryStore {
.contains(&self.from_js.to_quad(quad)?.try_into()?)) .contains(&self.from_js.to_quad(quad)?.try_into()?))
} }
#[wasm_bindgen(getter=size)]
pub fn size(&self) -> usize {
self.store.len()
}
#[wasm_bindgen(js_name = match)] #[wasm_bindgen(js_name = match)]
pub fn match_quads( pub fn match_quads(
&self, &self,

@ -29,11 +29,18 @@ describe('MemoryStore', function() {
}); });
}); });
describe('#size()', function() {
it('A store with one quad should have 1 for size', function() {
const store = new MemoryStore([dataFactory.triple(ex, ex, ex)]);
assert.strictEqual(1, store.size);
});
});
describe('#match_quads()', function() { describe('#match_quads()', function() {
it('blank pattern should return all quads', function() { it('blank pattern should return all quads', function() {
const store = new MemoryStore([dataFactory.triple(ex, ex, ex)]); const store = new MemoryStore([dataFactory.triple(ex, ex, ex)]);
const results = store.match(); const results = store.match();
assert.equal(1, results.length); assert.strictEqual(1, results.length);
assert(dataFactory.triple(ex, ex, ex).equals(results[0])); assert(dataFactory.triple(ex, ex, ex).equals(results[0]));
}); });
}); });
@ -41,25 +48,25 @@ describe('MemoryStore', function() {
describe('#query()', function() { describe('#query()', function() {
it('ASK true', function() { it('ASK true', function() {
const store = new MemoryStore([dataFactory.triple(ex, ex, ex)]); const store = new MemoryStore([dataFactory.triple(ex, ex, ex)]);
assert.equal(true, store.query("ASK { ?s ?s ?s }")); assert.strictEqual(true, store.query("ASK { ?s ?s ?s }"));
}); });
it('ASK false', function() { it('ASK false', function() {
const store = new MemoryStore(); const store = new MemoryStore();
assert.equal(false, store.query("ASK { FILTER(false)}")); assert.strictEqual(false, store.query("ASK { FILTER(false)}"));
}); });
it('CONSTRUCT', function() { it('CONSTRUCT', function() {
const store = new MemoryStore([dataFactory.triple(ex, ex, ex)]); const store = new MemoryStore([dataFactory.triple(ex, ex, ex)]);
const results = store.query("CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o }"); const results = store.query("CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o }");
assert.equal(1, results.length); assert.strictEqual(1, results.length);
assert(dataFactory.triple(ex, ex, ex).equals(results[0])); assert(dataFactory.triple(ex, ex, ex).equals(results[0]));
}); });
it('SELECT', function() { it('SELECT', function() {
const store = new MemoryStore([dataFactory.triple(ex, ex, ex)]); const store = new MemoryStore([dataFactory.triple(ex, ex, ex)]);
const results = store.query("SELECT ?s WHERE { ?s ?p ?o }"); const results = store.query("SELECT ?s WHERE { ?s ?p ?o }");
assert.equal(1, results.length); assert.strictEqual(1, results.length);
assert(ex.equals(results[0].get("s"))); assert(ex.equals(results[0].get("s")));
}); });
}); });

@ -170,6 +170,24 @@ impl MemoryStore {
self.contains_encoded(&quad) self.contains_encoded(&quad)
} }
/// Returns the number of quads in the store
pub fn len(&self) -> usize {
self.indexes()
.spog
.values()
.map(|v| {
v.values()
.map(|v| v.values().map(|v| v.len()).sum::<usize>())
.sum::<usize>()
})
.sum()
}
/// Returns if the store is empty
pub fn is_empty(&self) -> bool {
self.indexes().spog.is_empty()
}
/// Executes a transaction. /// Executes a transaction.
/// ///
/// The transaction is executed if the given closure returns `Ok`. /// The transaction is executed if the given closure returns `Ok`.

Loading…
Cancel
Save