ctdb-common: Add traverse_update function to db_hash abstraction
authorAmitay Isaacs <amitay@gmail.com>
Fri, 17 Mar 2017 07:00:16 +0000 (18:00 +1100)
committerMartin Schwenke <martins@samba.org>
Wed, 5 Apr 2017 02:47:23 +0000 (04:47 +0200)
Signed-off-by: Amitay Isaacs <amitay@gmail.com>
Reviewed-by: Martin Schwenke <martin@meltin.net>
ctdb/common/db_hash.c
ctdb/common/db_hash.h

index 6a23337e516f42628e56c5b08d7c8085f91fefe6..8dd62c4ec4a8a3da9eb58a3f1770285cb27ca041 100644 (file)
@@ -266,3 +266,30 @@ int db_hash_traverse(struct db_hash_context *dh,
 
        return ret;
 }
+
+int db_hash_traverse_update(struct db_hash_context *dh,
+                           db_hash_record_parser_fn parser,
+                           void *private_data, int *count)
+{
+       struct db_hash_traverse_state state;
+       int ret;
+
+       if (dh == NULL || parser == NULL) {
+               return EINVAL;
+       }
+
+       state.parser = parser;
+       state.private_data = private_data;
+
+       ret = tdb_traverse(dh->db, db_hash_traverse_parser, &state);
+       if (ret == -1) {
+               ret = db_hash_map_tdb_error(dh);
+       } else {
+               if (count != NULL) {
+                       *count = ret;
+               }
+               ret = 0;
+       }
+
+       return ret;
+}
index 8f0e50bb4cf9406e9cf4f94120120795a94fcecc..67e2b85796198a6f918ccca2584c8f7dd7a19e43 100644 (file)
@@ -142,9 +142,9 @@ int db_hash_fetch(struct db_hash_context *dh, uint8_t *keybuf, size_t keylen,
 int db_hash_exists(struct db_hash_context *dh, uint8_t *keybuf, size_t keylen);
 
 /**
- * @brief Traverse the database
+ * @brief Traverse the database without modification
  *
- * The parser function should non-zero value to stop traverse.
+ * The parser function should return non-zero value to stop traverse.
  *
  * @param[in] dh The tdb hash table context
  * @param[in] parser Function called for each record
@@ -156,4 +156,19 @@ int db_hash_traverse(struct db_hash_context *dh,
                     db_hash_record_parser_fn parser, void *private_data,
                     int *count);
 
+/**
+ * @brief Traverse the database for modifications
+ *
+ * The parser function should return non-zero value to stop traverse.
+ *
+ * @param[in] dh The tdb hash table context
+ * @param[in] parser Function called for each record
+ * @param[in] private_data Private data to parser function
+ * @param[out] count Number of records traversed
+ * @return 0 on success, errno on failure
+ */
+int db_hash_traverse_update(struct db_hash_context *dh,
+                           db_hash_record_parser_fn parser,
+                           void *private_data, int *count);
+
 #endif /* __CTDB_DB_HASH_H__ */