]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb2/private.h
tdb2: new tests, and new fixes.
[ccan] / ccan / tdb2 / private.h
index 1fe1563549c58617c11518bd422e048f28f299da..d7695cc41d8d06536b247a1d2290d3d51f79d467 100644 (file)
@@ -83,10 +83,10 @@ typedef uint64_t tdb_off_t;
 /* Hash chain locks. */
 #define TDB_HASH_LOCK_START 2
 
-/* We start wih 256 hash buckets, 10 free buckets.  A 1k-sized zone. */
+/* We start wih 256 hash buckets, 10 free buckets.  A 4k-sized zone. */
 #define INITIAL_HASH_BITS 8
 #define INITIAL_FREE_BUCKETS 10
-#define INITIAL_ZONE_BITS 10
+#define INITIAL_ZONE_BITS 12
 
 #if !HAVE_BSWAP_64
 static inline uint64_t bswap_64(uint64_t x)
@@ -136,7 +136,7 @@ static inline uint64_t rec_extra_padding(const struct tdb_used_record *r)
 
 static inline uint64_t rec_hash(const struct tdb_used_record *r)
 {
-       return ((r->magic_and_meta >> 32) & ((1ULL << 11) - 1)) << (64 - 11);
+       return ((r->magic_and_meta >> 32) & ((1ULL << 11) - 1));
 }
 
 static inline uint16_t rec_magic(const struct tdb_used_record *r)
@@ -223,6 +223,9 @@ struct tdb_context {
        tdb_hashfn_t khash;
        void *hash_priv;
 
+       /* Set if we are in a transaction. */
+       struct tdb_transaction *transaction;
+       
        /* What zone of the tdb to use, for spreading load. */
        uint64_t last_zone; 
 
@@ -234,9 +237,6 @@ struct tdb_context {
        uint64_t num_lockrecs;
        struct tdb_lock_type *lockrecs;
 
-       /* Set if we are in a transaction. */
-       struct tdb_transaction *transaction;
-       
        /* Single list of all TDBs, to avoid multiple opens. */
        struct tdb_context *next;
        dev_t device;   
@@ -247,22 +247,25 @@ struct tdb_methods {
        int (*read)(struct tdb_context *, tdb_off_t, void *, tdb_len_t);
        int (*write)(struct tdb_context *, tdb_off_t, const void *, tdb_len_t);
        int (*oob)(struct tdb_context *, tdb_off_t, bool);
-       int (*expand_file)(struct tdb_context *, tdb_len_t, tdb_len_t);
+       int (*expand_file)(struct tdb_context *, tdb_len_t);
 };
 
 /*
   internal prototypes
 */
 /* tdb.c: */
-/* Returns true if header changed. */
-bool update_header(struct tdb_context *tdb);
+/* Returns true if header changed (and updates it). */
+bool header_changed(struct tdb_context *tdb);
+
+/* Commit header to disk. */
+int write_header(struct tdb_context *tdb);
 
 /* Hash random memory. */
 uint64_t tdb_hash(struct tdb_context *tdb, const void *ptr, size_t len);
 
 
 /* free.c: */
-uint64_t random_free_zone(struct tdb_context *tdb);
+void tdb_zone_init(struct tdb_context *tdb);
 
 /* If this fails, try tdb_expand. */
 tdb_off_t alloc(struct tdb_context *tdb, size_t keylen, size_t datalen,
@@ -293,20 +296,13 @@ void *tdb_convert(const struct tdb_context *tdb, void *buf, tdb_len_t size);
 void tdb_munmap(struct tdb_context *tdb);
 void tdb_mmap(struct tdb_context *tdb);
 
-/* Hand data to a function, direct if possible */
-int tdb_parse_data(struct tdb_context *tdb, TDB_DATA key,
-                  tdb_off_t offset, tdb_len_t len,
-                  int (*parser)(TDB_DATA key, TDB_DATA data,
-                                void *private_data),
-                  void *private_data);
-
 /* Either make a copy into pad and return that, or return ptr into mmap.
  * Converts endian (ie. will use pad in that case). */
 void *tdb_get(struct tdb_context *tdb, tdb_off_t off, void *pad, size_t len);
 
 /* Either alloc a copy, or give direct access.  Release frees or noop. */
 const void *tdb_access_read(struct tdb_context *tdb,
-                           tdb_off_t off, tdb_len_t len);
+                           tdb_off_t off, tdb_len_t len, bool convert);
 void tdb_access_release(struct tdb_context *tdb, const void *p);
 
 /* Convenience routine to get an offset. */
@@ -328,13 +324,15 @@ tdb_off_t tdb_find_zero_off(struct tdb_context *tdb, tdb_off_t off,
 
 /* Even on files, we can get partial writes due to signals. */
 bool tdb_pwrite_all(int fd, const void *buf, size_t len, tdb_off_t off);
+bool tdb_pread_all(int fd, void *buf, size_t len, tdb_off_t off);
+bool tdb_read_all(int fd, void *buf, size_t len);
 
 /* Allocate and make a copy of some offset. */
 void *tdb_alloc_read(struct tdb_context *tdb, tdb_off_t offset, tdb_len_t len);
 
-/* Munges record and writes it */
+/* Writes a converted copy of a record. */
 int tdb_write_convert(struct tdb_context *tdb, tdb_off_t off,
-                     void *rec, size_t len);
+                     const void *rec, size_t len);
 
 /* Reads record and converts it */
 int tdb_read_convert(struct tdb_context *tdb, tdb_off_t off,
@@ -344,9 +342,11 @@ int tdb_read_convert(struct tdb_context *tdb, tdb_off_t off,
 uint64_t hash_record(struct tdb_context *tdb, tdb_off_t off);
 
 /* lock.c: */
+void tdb_lock_init(struct tdb_context *tdb);
+
 /* Lock/unlock a particular hash list. */
-int tdb_lock_list(struct tdb_context *tdb, tdb_off_t list,
-                 int ltype, enum tdb_lock_flags waitflag);
+tdb_off_t tdb_lock_list(struct tdb_context *tdb, uint64_t hash,
+                       int ltype, enum tdb_lock_flags waitflag);
 int tdb_unlock_list(struct tdb_context *tdb, tdb_off_t list, int ltype);
 
 /* Lock/unlock a particular free list. */