From 0e00e8209a8e72a25f7cf081eedf0e5140dcf527 Mon Sep 17 00:00:00 2001 From: Tpt Date: Mon, 13 Mar 2023 20:04:10 +0100 Subject: [PATCH] Python: adds Store.extend --- python/src/store.rs | 22 ++++++++++++++++++++++ python/tests/test_store.py | 5 +++++ 2 files changed, 27 insertions(+) diff --git a/python/src/store.rs b/python/src/store.rs index 011aae21..997f176d 100644 --- a/python/src/store.rs +++ b/python/src/store.rs @@ -130,6 +130,28 @@ impl PyStore { }) } + /// Adds atomically a set of quads to this store. + /// + /// :param quads: the quads to add. + /// :type quad: iterable(Quad) + /// :rtype: None + /// :raises IOError: if an I/O error happens during the quad insertion. + /// + /// >>> store = Store() + /// >>> store.extend([Quad(NamedNode('http://example.com'), NamedNode('http://example.com/p'), Literal('1'), NamedNode('http://example.com/g'))]) + /// >>> list(store) + /// [ predicate= object=> graph_name=>] + fn extend(&self, quads: &PyAny, py: Python<'_>) -> PyResult<()> { + let quads = quads + .iter()? + .map(|q| q?.extract()) + .collect::>>()?; + py.allow_threads(|| { + self.inner.extend(quads).map_err(map_storage_error)?; + Ok(()) + }) + } + /// Removes a quad from the store. /// /// :param quad: the quad to remove. diff --git a/python/tests/test_store.py b/python/tests/test_store.py index f0aeb0f2..9b736bb6 100644 --- a/python/tests/test_store.py +++ b/python/tests/test_store.py @@ -23,6 +23,11 @@ class TestStore(unittest.TestCase): store.add(Quad(foo, bar, triple)) self.assertEqual(len(store), 4) + def test_extend(self) -> None: + store = Store() + store.extend((Quad(foo, bar, baz), Quad(foo, bar, baz, graph), Quad(foo, bar, baz, DefaultGraph()))) + self.assertEqual(len(store), 2) + def test_remove(self) -> None: store = Store() store.add(Quad(foo, bar, baz))