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 @@
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <err.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));
layout->filename = filename;
layout->num_elems = 0;
layout->elem = NULL;
return layout;
......@@ -302,5 +304,19 @@ struct tdb_context *tdb_layout_get(struct tdb_layout *layout)
/* Write tailer. */
((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;
}
......@@ -2,7 +2,7 @@
#define TDB2_TEST_LAYOUT_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,
unsigned int zone_bits,
bool fill_prev);
......@@ -61,6 +61,7 @@ union tdb_layout_elem {
};
struct tdb_layout {
const char *filename;
unsigned int num_elems;
union tdb_layout_elem *elem;
};
......
......@@ -37,7 +37,7 @@ int main(int argc, char *argv[])
key.dsize = 5;
/* 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 = tdb_layout_get(layout);
len = layout->elem[1].free.len;
......@@ -57,7 +57,7 @@ int main(int argc, char *argv[])
tdb_close(tdb);
/* 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_free(layout, 1024);
tdb_layout_add_used(layout, key, data, 6);
......@@ -78,7 +78,7 @@ int main(int argc, char *argv[])
tdb_close(tdb);
/* 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_free(layout, 1024);
tdb = tdb_layout_get(layout);
......@@ -101,7 +101,7 @@ int main(int argc, char *argv[])
tdb_close(tdb);
/* 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_free(layout, 1024);
tdb_layout_add_free(layout, 512);
......@@ -125,7 +125,7 @@ int main(int argc, char *argv[])
tdb_close(tdb);
/* 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_free(layout, 1024);
tdb_layout_add_free(layout, 512);
......@@ -151,7 +151,7 @@ int main(int argc, char *argv[])
tdb_close(tdb);
/* 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, true);
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