Browse Source

Move txn handling back into store.

scossu 1 year ago
parent
commit
74b6533fe9
5 changed files with 78 additions and 88 deletions
  1. 0 55
      include/graph.h
  2. 47 0
      include/store.h
  3. 0 22
      src/graph.c
  4. 22 0
      src/store.c
  5. 9 11
      test/test_graph.c

+ 0 - 55
include/graph.h

@@ -180,61 +180,6 @@ bool
 LSUP_graph_contains (const LSUP_Graph *gr, const LSUP_Triple *spo);
 
 
-/** @brief Begin a transaction.
- *
- * If the underlying store supports it, begin a transaction.
- *
- * Note that the transaction affects all graphs sharing the same store, and it
- * may be closed using a different graph as long as this is backed by the same
- * store. Only one transaction may be opened at a time for the store.
- *
- * The transaction must be either committed with #LSUP_graph_commit() or
- * rolled back with #LSUP_graph_abort().
- *
- * TODO Deprecate in favor of direct access to the store handle.
- *
- * @param[in] gr Graph handle.
- *
- * @param[in] flags Unused for now, use 0. TODO
- *
- * @param[out] txn Address to be populated with the new transaction handle.
- *
- * @return LSUP_OK on success; LSUP_VALUE_ERR if the graph store does not
- *  support transactions; LSUP_TXN_ERR if the store has already an uncommitted
- *  transaction; <0 on other errors from the underlying store.
- */
-LSUP_rc
-LSUP_graph_begin (LSUP_Graph *gr, int flags, void **txn);
-
-
-/** @brief Commit a transaction.
- *
- * If the underlying store supports it, commit an open transaction. In case of
- * error, the transaction is left open and it is advisable to roll it back with
- * #LSUP_graph_abort().
- *
- * TODO Deprecate in favor of direct access to the store handle.
- *
- * @param[in] gr Graph handle.
- *
- * @return LSUP_OK if the transaction was committed successfully; LSUP_NOACTION
- *  if NULL was passed; LSUP_TXN_ERR on error.
- */
-LSUP_rc LSUP_graph_commit (LSUP_Graph *gr, void *txn);
-
-
-/** @brief Abort (roll back) a transaction.
- *
- * If the underlying store supports it, abort an open transaction and abandon
- * all changes.
- *
- * TODO Deprecate in favor of direct access to the store handle.
- *
- * @param[in] gr Graph handle.
- */
-void LSUP_graph_abort (LSUP_Graph *gr, void *txn);
-
-
 /** @brief Initialize an iterator to add triples.
  *
  * @param[in] txn Transaction handle. It may be NULL. If not NULL, its handle

+ 47 - 0
include/store.h

@@ -104,4 +104,51 @@ LSUP_store_new (
 void
 LSUP_store_free (LSUP_Store *store);
 
+
+/** @brief Begin a transaction.
+ *
+ * If the store supports it, begin a transaction. Only one transaction may be
+ * opened at a time.
+ *
+ * The transaction must be either committed with #LSUP_store_commit() or
+ * rolled back with #LSUP_store_abort().
+ *
+ * @param[in] store Store handle.
+ *
+ * @param[in] flags Unused for now, use 0. TODO
+ *
+ * @param[out] txn Address to be populated with the new transaction handle.
+ *
+ * @return LSUP_OK on success; LSUP_VALUE_ERR if the store does not
+ *  support transactions; LSUP_TXN_ERR if the store has already an uncommitted
+ *  transaction; <0 on other errors.
+ */
+LSUP_rc
+LSUP_store_begin (LSUP_Store *store, int flags, void **txn);
+
+
+/** @brief Commit a transaction.
+ *
+ * If the store supports it, commit an open transaction. In case of
+ * error, the transaction is left open and it is advisable to roll it back with
+ * #LSUP_graph_abort().
+ *
+ * @param[in] store Store handle.
+ *
+ * @return LSUP_OK if the transaction was committed successfully; LSUP_NOACTION
+ *  if NULL was passed; LSUP_TXN_ERR on error.
+ */
+LSUP_rc
+LSUP_store_commit (LSUP_Store *store, void *txn);
+
+
+/** @brief Abort (roll back) a transaction.
+ *
+ * If the store supports it, abort an open transaction and abandon all changes.
+ *
+ * @param[in] store Store handle.
+ */
+void
+LSUP_store_abort (LSUP_Store *store, void *txn);
+
 #endif  /* LSUP_STORE_H */

+ 0 - 22
src/graph.c

@@ -469,28 +469,6 @@ LSUP_graph_contains (const LSUP_Graph *gr, const LSUP_Triple *spo)
 }
 
 
-LSUP_rc
-LSUP_graph_begin (LSUP_Graph *gr, int flags, void **txn) {
-    if (!(gr->store->sif->features & LSUP_STORE_TXN)) return LSUP_VALUE_ERR;
-
-    return gr->store->sif->txn_begin_fn (gr->store->data, flags, txn);
-}
-
-
-LSUP_rc
-LSUP_graph_commit (LSUP_Graph *gr, void *txn)
-{
-    if (!(gr->store->sif->features & LSUP_STORE_TXN)) return LSUP_VALUE_ERR;
-
-    return gr->store->sif->txn_commit_fn (txn);
-}
-
-
-void
-LSUP_graph_abort (LSUP_Graph *gr, void *txn)
-{ return gr->store->sif->txn_abort_fn (txn); }
-
-
 LSUP_LinkMap *
 LSUP_graph_connections (
         const LSUP_Graph *gr, LSUP_Term *t, LSUP_LinkType type)

+ 22 - 0
src/store.c

@@ -58,3 +58,25 @@ LSUP_store_free (LSUP_Store *store)
     store->sif->free_fn (store->data);
     free (store);
 }
+
+
+LSUP_rc
+LSUP_store_begin (LSUP_Store *store, int flags, void **txn) {
+    if (!(store->sif->features & LSUP_STORE_TXN)) return LSUP_VALUE_ERR;
+
+    return store->sif->txn_begin_fn (store->data, flags, txn);
+}
+
+
+LSUP_rc
+LSUP_store_commit (LSUP_Store *store, void *txn)
+{
+    if (!(store->sif->features & LSUP_STORE_TXN)) return LSUP_VALUE_ERR;
+
+    return store->sif->txn_commit_fn (txn);
+}
+
+
+void
+LSUP_store_abort (LSUP_Store *store, void *txn)
+{ return store->sif->txn_abort_fn (txn); }

+ 9 - 11
test/test_graph.c

@@ -252,29 +252,27 @@ _graph_txn (LSUP_StoreType type)
     LSUP_Triple *trp = create_triples();
 
     LSUP_Graph *gr;
-    LSUP_Store *store;
-    if (type == LSUP_STORE_HTABLE) {
-        gr = LSUP_graph_new (NULL, NULL, NULL);
-    } else {
-        store = LSUP_store_new (type, NULL, 0);
-        gr = LSUP_graph_new (store, NULL, NULL);
-    }
+    LSUP_Store *store =
+        type == LSUP_STORE_HTABLE ? NULL
+        : LSUP_store_new (type, NULL, 0);
+
+    gr = LSUP_graph_new (store, NULL, NULL);
 
     void *txn;
     size_t ct;
 
-    EXPECT_PASS (LSUP_graph_begin (gr, 0, &txn));
+    EXPECT_PASS (LSUP_store_begin (store, 0, &txn));
     EXPECT_PASS (LSUP_graph_add_txn (txn, gr, trp, &ct));
-    LSUP_graph_abort (gr, txn);
+    LSUP_store_abort (store, txn);
 
     // NOTE that ct reports the count before the txn was aborted. This is
     // intentional.
     EXPECT_INT_EQ (ct, 8);
     EXPECT_INT_EQ (LSUP_graph_size (gr), 0);
 
-    EXPECT_PASS (LSUP_graph_begin (gr, 0, &txn));
+    EXPECT_PASS (LSUP_store_begin (store, 0, &txn));
     EXPECT_PASS (LSUP_graph_add_txn (txn, gr, trp, &ct));
-    LSUP_graph_commit (gr, txn);
+    LSUP_store_commit (store, txn);
 
     EXPECT_INT_EQ (ct, 8);
     EXPECT_INT_EQ (LSUP_graph_size (gr), 8);