1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
/*
NAME
gtconv - test GDBM database conversion to extended format.
SYNOPSIS
gtconv [-v]
DESCRIPTION
When converting a traditional GDBM database to extended (numsync)
format, the size of the master av_table shrinks. Consequently,
if it is full or nearly full, the entries near its end that
don't fit into the new size are returned to the per-bucket
available pools using _gdbm_free (see _gdbm_convert_to_numsync).
This test program verifies that all main av_table entries are
preserved during format upgrade.
Operation:
1) Create a database with the minimal possible block size, to ensure
the mimimal size of the av_table array
2) Set the GDBM_SETCENTFREE option, so all released entries are
returned to the main av_table.
3) Populate the database with a sufficient number of entries.
4) Keep deleting entries until main av_table becomes full.
5) Save a copy of all avail_elems (both master and per-block),
sorted by av_adr.
6) Convert the database to the GDBM_NUMSYNC format.
7) Get a copy of all avail_elems similar to (5)
8) Compare arrays obtained in 5 and 7.
The array obtained in step 7 is normally one entry longer than the
one from step 5. The comparison in 8 ignores such extra entries.
OPTIONS
-v Verbosely print what's being done.
Repeated twice, dumps listings of av_table arrays received in
steps 5 and 7.
EXIT CODE
0 success
1 failure
2 usage error
77 unable to fill the av_table in step 4.
LICENSE
This file is part of GDBM test suite.
Copyright (C) 2021-2024 Free Software Foundation, Inc.
GDBM is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GDBM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GDBM. If not, see <http://www.gnu.org/licenses/>.
*/
#include "autoconf.h"
#include "gdbmdefs.h"
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
char dbname[] = "a.db";
#define DATASIZE (4*IGNORE_SIZE)
static int
avail_counter (avail_block *blk, off_t off, void *closure)
{
int *np = closure;
*np += blk->count;
return 0;
}
static int
avail_saver (avail_block *blk, off_t off, void *closure)
{
avail_block *ab = closure;
memcpy (ab->av_table + ab->count, blk->av_table,
blk->count * sizeof (blk->av_table[0]));
ab->count += blk->count;
return 0;
}
static int
av_table_cmp (const void *a, const void *b)
{
const avail_elem *ea = a;
const avail_elem *eb = b;
if (ea->av_adr < eb->av_adr)
return -1;
else if (ea->av_adr > eb->av_adr)
return 1;
return 0;
}
static avail_block *
collect_avail (GDBM_FILE dbf)
{
int av_count;
avail_block *ab;
/* Compute the number of entries in avail block */
av_count = 0;
gdbm_avail_traverse (dbf, avail_counter, &av_count);
/* Allocate temporary storage */
ab = malloc (sizeof (ab[0]) + (av_count - 1) * sizeof (ab->av_table[0]));
assert (ab != NULL);
/* Save the avail table */
ab->size = av_count;
ab->count = 0;
gdbm_avail_traverse (dbf, avail_saver, ab);
/* Sort it */
qsort (ab->av_table, ab->count, sizeof (ab->av_table[0]), av_table_cmp);
return ab;
}
static void
dump_avail (avail_block *ab, char const *title, FILE *fp)
{
int i;
unsigned long total = 0;
fprintf (fp, "%s\n", title);
for (i = 0; i < ab->count; i++)
{
total += ab->av_table[i].av_size;
fprintf (fp, "% 4d %6lu\n", ab->av_table[i].av_size,
(unsigned long) ab->av_table[i].av_adr);
}
fprintf (fp, "total = %lu\n", total);
}
int
main (int argc, char **argv)
{
int avcount;
GDBM_FILE dbf;
datum key, content;
char data[DATASIZE];
int nkeys;
int *keys;
int i, n;
avail_block *av_saved, *av_new;
int verbose = 0;
int rc;
while ((i = getopt (argc, argv, "v")) != EOF)
{
switch (i)
{
case 'v':
verbose++;
break;
default:
return 2;
}
}
/* Make sure we create new database */
unlink (dbname);
/* Create the database */
if (verbose)
printf ("creating database\n");
dbf = gdbm_open (dbname, GDBM_MIN_BLOCK_SIZE, GDBM_NEWDB, 0644, NULL);
if (!dbf)
{
fprintf (stderr, "gdbm_open: %s\n", gdbm_strerror (gdbm_errno));
return 1;
}
int t = 1;
if (gdbm_setopt (dbf, GDBM_SETCENTFREE, &t, sizeof (t)) == -1)
{
fprintf (stderr, "gdbm_setopt: %s\n", gdbm_strerror (gdbm_errno));
return 1;
}
avcount = dbf->avail->size;
if (verbose)
printf ("main av_table capacity: %d\n", avcount);
/* Initialize keys */
nkeys = 2*avcount;
keys = calloc (nkeys, sizeof (keys));
assert (keys != NULL);
for (i = 0; i < nkeys; i++)
{
keys[i] = i+1;
}
/* Initialize content */
for (i = 0; i < DATASIZE; i++)
data[i] = i+1;
content.dsize = DATASIZE;
content.dptr = data;
/* Populate the database */
if (verbose)
printf ("populating database (%d keys)\n", nkeys);
key.dsize = sizeof (keys[0]);
for (i = 0; i < nkeys; i++)
{
key.dptr = (char*) &keys[i];
if (gdbm_store (dbf, key, content, 0) != 0)
{
fprintf (stderr, "%d: item not inserted: %s\n",
i, gdbm_db_strerror (dbf));
gdbm_close (dbf);
return 1;
}
}
/* Delete all keys */
if (verbose)
printf ("deleting keys\n");
i = 0;
while (dbf->avail->count < dbf->avail->size)
{
if (i == nkeys)
{
if (verbose)
printf ("failed to fill av_table\n");
gdbm_close (dbf);
return 77;
}
key.dptr = (char*) &keys[i];
if (gdbm_delete (dbf, key))
{
fprintf (stderr, "%d: gdbm_delete: %s\n",
i, gdbm_db_strerror (dbf));
gdbm_close (dbf);
return 1;
}
i++;
}
if (verbose)
printf ("main av_table elements: %d\n", dbf->avail->count);
av_saved = collect_avail (dbf);
if (verbose)
printf ("total number of avail_elem entries used: %d\n", av_saved->count);
if (verbose > 1)
dump_avail (av_saved, "av_saved", stdout);
/* Upgrade the database */
if (verbose)
printf ("converting database\n");
if (gdbm_convert (dbf, GDBM_NUMSYNC))
{
fprintf (stderr, "gdbm_convert: %s\n", gdbm_db_strerror (dbf));
gdbm_close (dbf);
return 1;
}
if (verbose)
printf ("main av_table elements: %d / %d\n", dbf->avail->count, dbf->avail->size);
av_new = collect_avail (dbf);
if (verbose)
printf ("total number of avail_elem entries used: %d\n", av_new->count);
if (verbose > 1)
dump_avail (av_new, "av_new", stdout);
n = (av_saved->count < av_new->count) ? av_saved->count : av_new->count;
rc = 0;
for (i = 0; i < n; i++)
{
if (!(av_saved->av_table[i].av_adr == av_new->av_table[i].av_adr &&
av_saved->av_table[i].av_size == av_new->av_table[i].av_size))
{
fprintf (stderr, "element %d differs\n", i);
rc = 1;
break;
}
}
if (rc)
{
dump_avail (av_saved, "av_saved", stderr);
dump_avail (av_new, "av_new", stderr);
}
free (av_saved);
free (av_new);
free (keys);
gdbm_close (dbf);
return rc;
}
|