Commit 9071df66 authored by Rusty Russell's avatar Rusty Russell

tdb2: extend test/layout to be able to place in file.

This was for lockcheck, but that didn't work very well.  Seems like a useful
addition nonetheless.
parent d71f8b0b
...@@ -3,11 +3,13 @@ ...@@ -3,11 +3,13 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include <err.h>
#include "logging.h" #include "logging.h"
struct tdb_layout *new_tdb_layout(void) struct tdb_layout *new_tdb_layout(const char *filename)
{ {
struct tdb_layout *layout = malloc(sizeof(*layout)); struct tdb_layout *layout = malloc(sizeof(*layout));
layout->filename = filename;
layout->num_elems = 0; layout->num_elems = 0;
layout->elem = NULL; layout->elem = NULL;
return layout; return layout;
...@@ -302,5 +304,19 @@ struct tdb_context *tdb_layout_get(struct tdb_layout *layout) ...@@ -302,5 +304,19 @@ struct tdb_context *tdb_layout_get(struct tdb_layout *layout)
/* Write tailer. */ /* Write tailer. */
((uint8_t *)tdb->map_ptr)[tdb->map_size-1] = last_zone->zone_bits; ((uint8_t *)tdb->map_ptr)[tdb->map_size-1] = last_zone->zone_bits;
/* Get physical if they asked for it. */
if (layout->filename) {
int fd = open(layout->filename, O_WRONLY|O_TRUNC|O_CREAT,
0600);
if (fd < 0)
err(1, "opening %s for writing", layout->filename);
write(fd, tdb->map_ptr, tdb->map_size);
close(fd);
tdb_close(tdb);
/* NOMMAP is for lockcheck. */
tdb = tdb_open(layout->filename, TDB_NOMMAP, O_RDWR, 0,
&tap_log_attr);
}
return tdb; return tdb;
} }
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
#define TDB2_TEST_LAYOUT_H #define TDB2_TEST_LAYOUT_H
#include <ccan/tdb2/private.h> #include <ccan/tdb2/private.h>
struct tdb_layout *new_tdb_layout(void); struct tdb_layout *new_tdb_layout(const char *filename);
void tdb_layout_add_zone(struct tdb_layout *layout, void tdb_layout_add_zone(struct tdb_layout *layout,
unsigned int zone_bits, unsigned int zone_bits,
bool fill_prev); bool fill_prev);
...@@ -61,6 +61,7 @@ union tdb_layout_elem { ...@@ -61,6 +61,7 @@ union tdb_layout_elem {
}; };
struct tdb_layout { struct tdb_layout {
const char *filename;
unsigned int num_elems; unsigned int num_elems;
union tdb_layout_elem *elem; union tdb_layout_elem *elem;
}; };
......
...@@ -37,7 +37,7 @@ int main(int argc, char *argv[]) ...@@ -37,7 +37,7 @@ int main(int argc, char *argv[])
key.dsize = 5; key.dsize = 5;
/* No coalescing can be done due to EOF */ /* No coalescing can be done due to EOF */
layout = new_tdb_layout(); layout = new_tdb_layout(NULL);
tdb_layout_add_zone(layout, zone_bits, false); tdb_layout_add_zone(layout, zone_bits, false);
tdb = tdb_layout_get(layout); tdb = tdb_layout_get(layout);
len = layout->elem[1].free.len; len = layout->elem[1].free.len;
...@@ -57,7 +57,7 @@ int main(int argc, char *argv[]) ...@@ -57,7 +57,7 @@ int main(int argc, char *argv[])
tdb_close(tdb); tdb_close(tdb);
/* No coalescing can be done due to used record */ /* No coalescing can be done due to used record */
layout = new_tdb_layout(); layout = new_tdb_layout(NULL);
tdb_layout_add_zone(layout, zone_bits, false); tdb_layout_add_zone(layout, zone_bits, false);
tdb_layout_add_free(layout, 1024); tdb_layout_add_free(layout, 1024);
tdb_layout_add_used(layout, key, data, 6); tdb_layout_add_used(layout, key, data, 6);
...@@ -78,7 +78,7 @@ int main(int argc, char *argv[]) ...@@ -78,7 +78,7 @@ int main(int argc, char *argv[])
tdb_close(tdb); tdb_close(tdb);
/* Coalescing can be done due to two free records, then EOF */ /* Coalescing can be done due to two free records, then EOF */
layout = new_tdb_layout(); layout = new_tdb_layout(NULL);
tdb_layout_add_zone(layout, zone_bits, false); tdb_layout_add_zone(layout, zone_bits, false);
tdb_layout_add_free(layout, 1024); tdb_layout_add_free(layout, 1024);
tdb = tdb_layout_get(layout); tdb = tdb_layout_get(layout);
...@@ -101,7 +101,7 @@ int main(int argc, char *argv[]) ...@@ -101,7 +101,7 @@ int main(int argc, char *argv[])
tdb_close(tdb); tdb_close(tdb);
/* Coalescing can be done due to two free records, then data */ /* Coalescing can be done due to two free records, then data */
layout = new_tdb_layout(); layout = new_tdb_layout(NULL);
tdb_layout_add_zone(layout, zone_bits, false); tdb_layout_add_zone(layout, zone_bits, false);
tdb_layout_add_free(layout, 1024); tdb_layout_add_free(layout, 1024);
tdb_layout_add_free(layout, 512); tdb_layout_add_free(layout, 512);
...@@ -125,7 +125,7 @@ int main(int argc, char *argv[]) ...@@ -125,7 +125,7 @@ int main(int argc, char *argv[])
tdb_close(tdb); tdb_close(tdb);
/* Coalescing can be done due to three free records, then EOF */ /* Coalescing can be done due to three free records, then EOF */
layout = new_tdb_layout(); layout = new_tdb_layout(NULL);
tdb_layout_add_zone(layout, zone_bits, false); tdb_layout_add_zone(layout, zone_bits, false);
tdb_layout_add_free(layout, 1024); tdb_layout_add_free(layout, 1024);
tdb_layout_add_free(layout, 512); tdb_layout_add_free(layout, 512);
...@@ -151,7 +151,7 @@ int main(int argc, char *argv[]) ...@@ -151,7 +151,7 @@ int main(int argc, char *argv[])
tdb_close(tdb); tdb_close(tdb);
/* Coalescing across two zones isn't possible. */ /* Coalescing across two zones isn't possible. */
layout = new_tdb_layout(); layout = new_tdb_layout(NULL);
tdb_layout_add_zone(layout, zone_bits, false); tdb_layout_add_zone(layout, zone_bits, false);
tdb_layout_add_zone(layout, zone_bits, true); tdb_layout_add_zone(layout, zone_bits, true);
tdb = tdb_layout_get(layout); tdb = tdb_layout_get(layout);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment