]> git.ozlabs.org Git - ccan/commitdiff
tdb2: copy tdb1's changed expansion logic.
authorRusty Russell <rusty@rustcorp.com.au>
Sat, 28 Jan 2012 04:14:43 +0000 (14:44 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Sat, 28 Jan 2012 04:14:43 +0000 (14:44 +1030)
TDB2 uses the same expansion logic as TDB1, which got factored out
recently.  So update TDB2 to match.

ccan/tdb2/free.c
ccan/tdb2/private.h
ccan/tdb2/transaction.c

index 03ca5a4f9471c17920bb9b1d44b7953e2f560eea..e693fe828ae3bebf801c653184256ace07e9d23e 100644 (file)
@@ -876,10 +876,38 @@ enum TDB_ERROR set_header(struct tdb_context *tdb,
        return TDB_SUCCESS;
 }
 
        return TDB_SUCCESS;
 }
 
+/* You need 'size', this tells you how much you should expand by. */
+tdb_off_t tdb_expand_adjust(tdb_off_t map_size, tdb_off_t size)
+{
+       tdb_off_t new_size, top_size;
+
+       /* limit size in order to avoid using up huge amounts of memory for
+        * in memory tdbs if an oddball huge record creeps in */
+       if (size > 100 * 1024) {
+               top_size = map_size + size * 2;
+       } else {
+               top_size = map_size + size * 100;
+       }
+
+       /* always make room for at least top_size more records, and at
+          least 25% more space. if the DB is smaller than 100MiB,
+          otherwise grow it by 10% only. */
+       if (map_size > 100 * 1024 * 1024) {
+               new_size = map_size * 1.10;
+       } else {
+               new_size = map_size * 1.25;
+       }
+
+       /* Round the database up to a multiple of the page size */
+       if (new_size < top_size)
+               new_size = top_size;
+       return new_size - map_size;
+}
+
 /* Expand the database. */
 static enum TDB_ERROR tdb_expand(struct tdb_context *tdb, tdb_len_t size)
 {
 /* Expand the database. */
 static enum TDB_ERROR tdb_expand(struct tdb_context *tdb, tdb_len_t size)
 {
-       uint64_t old_size, rec_size, map_size;
+       uint64_t old_size;
        tdb_len_t wanted;
        enum TDB_ERROR ecode;
 
        tdb_len_t wanted;
        enum TDB_ERROR ecode;
 
@@ -904,29 +932,8 @@ static enum TDB_ERROR tdb_expand(struct tdb_context *tdb, tdb_len_t size)
                return TDB_SUCCESS;
        }
 
                return TDB_SUCCESS;
        }
 
-       /* limit size in order to avoid using up huge amounts of memory for
-        * in memory tdbs if an oddball huge record creeps in */
-       if (size > 100 * 1024) {
-               rec_size = size * 2;
-       } else {
-               rec_size = size * 100;
-       }
-
-       /* always make room for at least rec_size more records, and at
-          least 25% more space. if the DB is smaller than 100MiB,
-          otherwise grow it by 10% only. */
-       if (old_size > 100 * 1024 * 1024) {
-               map_size = old_size / 10;
-       } else {
-               map_size = old_size / 4;
-       }
-
-       if (map_size > rec_size) {
-               wanted = map_size;
-       } else {
-               wanted = rec_size;
-       }
-
+       /* Overallocate. */
+       wanted = tdb_expand_adjust(old_size, size);
        /* We need room for the record header too. */
        wanted = adjust_size(0, sizeof(struct tdb_used_record) + wanted);
 
        /* We need room for the record header too. */
        wanted = adjust_size(0, sizeof(struct tdb_used_record) + wanted);
 
index da1f0a2c77c0995da5f4bc7f70b381c4dbeb2d56..ba7de3be5e2939892bda7b9a5bd6fbf579588eee 100644 (file)
@@ -425,6 +425,9 @@ tdb_off_t bucket_off(tdb_off_t ftable_off, unsigned bucket);
 /* Used by tdb_summary */
 tdb_off_t dead_space(struct tdb_context *tdb, tdb_off_t off);
 
 /* Used by tdb_summary */
 tdb_off_t dead_space(struct tdb_context *tdb, tdb_off_t off);
 
+/* Adjust expansion, used by create_recovery_area */
+tdb_off_t tdb_expand_adjust(tdb_off_t map_size, tdb_off_t size);
+
 /* io.c: */
 /* Initialize tdb->methods. */
 void tdb_io_init(struct tdb_context *tdb);
 /* io.c: */
 /* Initialize tdb->methods. */
 void tdb_io_init(struct tdb_context *tdb);
index 70e664fc2d60e5fe36bda442607fbdfba04677ff..dd94510c027343d1c6aac0e6ec0d6e3d28ffeaab 100644 (file)
@@ -836,10 +836,13 @@ static tdb_off_t create_recovery_area(struct tdb_context *tdb,
 
        /* round up to a multiple of page size. Overallocate, since each
         * such allocation forces us to expand the file. */
 
        /* round up to a multiple of page size. Overallocate, since each
         * such allocation forces us to expand the file. */
-       rec->max_len
-               = (((sizeof(*rec) + rec_length + rec_length / 2)
-                   + PAGESIZE-1) & ~(PAGESIZE-1))
+       rec->max_len = tdb_expand_adjust(tdb->file->map_size, rec_length);
+
+       /* Round up to a page. */
+       rec->max_len = ((sizeof(*rec) + rec->max_len + PAGESIZE-1)
+                       & ~(PAGESIZE-1))
                - sizeof(*rec);
                - sizeof(*rec);
+
        off = tdb->file->map_size;
 
        /* Restore ->map_size before calling underlying expand_file.
        off = tdb->file->map_size;
 
        /* Restore ->map_size before calling underlying expand_file.