tdb: use tdb_freelist_merge_adjacent in tdb_freelist_size()
authorMichael Adam <obnox@samba.org>
Wed, 11 Jun 2014 15:26:51 +0000 (17:26 +0200)
committerVolker Lendecke <vl@samba.org>
Thu, 26 Jun 2014 08:00:11 +0000 (10:00 +0200)
So that we automatically defragment the free list when freelist_size is called
(unless the database is read only).

Signed-off-by: Michael Adam <obnox@samba.org>
Reviewed-by: Volker Lendecke <vl@samba.org>
tdb/common/freelist.c

index 89ea371cbb54dfde2e03903fb0a6019b62a4c617..18b5bf1b67b6b18f1ae92449942f968d6d3fda1d 100644 (file)
@@ -670,10 +670,10 @@ done:
        return ret;
 }
 
-/*
-   return the size of the freelist - used to decide if we should repack
-*/
-_PUBLIC_ int tdb_freelist_size(struct tdb_context *tdb)
+/**
+ * return the size of the freelist - no merging done
+ */
+static int tdb_freelist_size_no_merge(struct tdb_context *tdb)
 {
        tdb_off_t ptr;
        int count=0;
@@ -690,3 +690,28 @@ _PUBLIC_ int tdb_freelist_size(struct tdb_context *tdb)
        tdb_unlock(tdb, -1, F_RDLCK);
        return count;
 }
+
+/**
+ * return the size of the freelist - used to decide if we should repack
+ *
+ * As a side effect, adjacent records are merged unless the
+ * database is read-only, in order to reduce the fragmentation
+ * without repacking.
+ */
+_PUBLIC_ int tdb_freelist_size(struct tdb_context *tdb)
+{
+
+       int count = 0;
+
+       if (tdb->read_only) {
+               count = tdb_freelist_size_no_merge(tdb);
+       } else {
+               int ret;
+               ret = tdb_freelist_merge_adjacent(tdb, &count, NULL);
+               if (ret != 0) {
+                       return -1;
+               }
+       }
+
+       return count;
+}