Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:remove support for kyotocabinet and add support for mmap-size and cache-size for all executables
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:c8eb4e153d28858af31afad1a1ac06190df5236e
User & Date: brandon 2016-06-12 17:00:01
Context
2016-06-12
17:49
add support for --block-size check-in: 3025c4b3b6 user: brandon tags: trunk
17:00
remove support for kyotocabinet and add support for mmap-size and cache-size for all executables check-in: c8eb4e153d user: brandon tags: trunk
16:23
add zdbi program check-in: 98ef403a3f user: brandon tags: trunk
Changes

Changes to configure.ac.

1
2
3
4
5
6
7
8
9
10
..
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
dnl configure.ac

dnl Copyright (C) 2013 Brandon Invergo <brandon@invergo.net>

dnl Author: Brandon Invergo <brandon@invergo.net>

dnl This program is free software; you can redistribute it and/or
dnl modify it under the terms of the GNU General Public License
dnl as published by the Free Software Foundation; either version 3
dnl of the License, or (at your option) any later version.
................................................................................

AM_MISSING_PROG(HELP2MAN, help2man, $missing_dir)
AC_PROG_RANLIB
AC_PROG_AWK
AC_FUNC_ALLOCA

gl_INIT
AC_ARG_WITH([kyotocabinet],
            [AS_HELP_STRING([--with-kyotocabinet],
                            [enable Kyoto Cabinet as the DBM backend instead of GDBM])],
            [], [with_kyotocabinet=no])

LIBKYOTOCABINET=
LIBGDBM=
AS_IF([test "x$with_kyotocabinet" != xno],
      [AC_SUBST([WITH_KYOTOCABINET], ["1"])]
      [AC_CHECK_LIB([kyotocabinet], [kcdbopen],
                    [AC_SUBST([LIBKYOTOCABINET], ["-lkyotocabinet"])
                     AC_DEFINE([HAVE_KYOTOCABINET], [1],
                     [Define if you have libkyotocabiet])],
                    [AC_MSG_FAILURE(
                     [--with-kyotocabinet was given, but test for libkyotocabinet failed])])]
      [AC_CHECK_HEADER([kclangc.h], [],
                       [AC_MSG_ERROR(
                        [--with-kyotocabinet was given, but kclangc.h was not found])])],
      [AC_CHECK_LIB([gdbm], [gdbm_open],
                    [AC_SUBST([LIBGDBM], ["-lgdbm"])
                     AC_DEFINE([HAVE_GDBM], [1],
                     [Define if you have libgdbm])],
                    [AC_MSG_ERROR([--with-gdbm was given, but libgdbm was not found!])])]
      [AC_CHECK_HEADER([gdbm.h], [],
                       [AC_MSG_ERROR([gdbm.h was not found!])])])

AC_CONFIG_FILES([tests/system_tests.test Makefile
                 psf/source_control.sh psf/PSF-src-sw psf/PSF-src-dist
                 psf/PSF-bin psf/bin_control.sh src/Makefile
                 doc/Makefile lib/Makefile], [chmod +x tests/system_tests.test])
AC_REQUIRE_AUX_FILE([tap-driver.sh])
AC_OUTPUT


|







 







<
<
<
<
<
<

<
<
<
<
<
<
<
<
<
<
<
|
|
<
|
|
|
|







1
2
3
4
5
6
7
8
9
10
..
28
29
30
31
32
33
34






35











36
37

38
39
40
41
42
43
44
45
46
47
48
dnl configure.ac

dnl Copyright (C) 2013, 2016 Brandon Invergo <brandon@invergo.net>

dnl Author: Brandon Invergo <brandon@invergo.net>

dnl This program is free software; you can redistribute it and/or
dnl modify it under the terms of the GNU General Public License
dnl as published by the Free Software Foundation; either version 3
dnl of the License, or (at your option) any later version.
................................................................................

AM_MISSING_PROG(HELP2MAN, help2man, $missing_dir)
AC_PROG_RANLIB
AC_PROG_AWK
AC_FUNC_ALLOCA

gl_INIT






LIBGDBM=











AC_CHECK_LIB([gdbm], [gdbm_open],
              [AC_SUBST([LIBGDBM], ["-lgdbm"])

               AC_DEFINE([HAVE_GDBM], [1], [Define if you have libgdbm])],
                         [AC_MSG_ERROR([--with-gdbm was given, but libgdbm was not found!])])
AC_CHECK_HEADER([gdbm.h], [],
                 [AC_MSG_ERROR([gdbm.h was not found!])])

AC_CONFIG_FILES([tests/system_tests.test Makefile
                 psf/source_control.sh psf/PSF-src-sw psf/PSF-src-dist
                 psf/PSF-bin psf/bin_control.sh src/Makefile
                 doc/Makefile lib/Makefile], [chmod +x tests/system_tests.test])
AC_REQUIRE_AUX_FILE([tap-driver.sh])
AC_OUTPUT

Changes to src/Makefile.am.

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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 this program. If not, see <http://www.gnu.org/licenses/>.


LDADD = $(LIBINTL) $(LIBGDBM) $(LIBKYOTOCABINET)	\
	$(top_srcdir)/lib/libzeptodb.a
zdbc_LDADD = $(LDADD)
zdbf_LDADD = $(LDADD)
zdbr_LDADD = $(LDADD)
zdbs_LDADD = $(LDADD)
zdbi_LDADD = $(LDADD)

AM_CPPFLAGS = -I$(top_builddir)/lib -I$(top_srcdir)/lib -I$(srcdir)







<
|







14
15
16
17
18
19
20

21
22
23
24
25
26
27
28
# 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 this program. If not, see <http://www.gnu.org/licenses/>.



LDADD = $(LIBINTL) $(LIBGDBM) $(top_srcdir)/lib/libzeptodb.a
zdbc_LDADD = $(LDADD)
zdbf_LDADD = $(LDADD)
zdbr_LDADD = $(LDADD)
zdbs_LDADD = $(LDADD)
zdbi_LDADD = $(LDADD)

AM_CPPFLAGS = -I$(top_builddir)/lib -I$(top_srcdir)/lib -I$(srcdir)

Changes to src/zdb.c.

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
..
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
...
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
...
286
287
288
289
290
291
292
293
294
295

   You should have received a copy of the GNU General Public License
   along with zeptodb.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "zdb.h"

void *

zdb_create (char *dbfile, size_t mmap_size, size_t num_buckets,
            bool verbose, int mode)
{
  void *db;
  if (verbose)
    printf ("Creating database %s\n", dbfile);
#ifdef HAVE_KYOTOCABINET
  char mmap_str[32];
  char buckets_str[32];
  char db_str[512] = "";
  if (strlen (dbfile) > sizeof (db_str) - sizeof (buckets_str) - sizeof (mmap_str))
    error (EXIT_FAILURE, errno, "Filename too long");
  strncat (db_str, dbfile, strlen (dbfile));
  snprintf (mmap_str, sizeof(mmap_str), "#msiz=%lu", mmap_size);
  if (strlen (dbfile) + strlen (mmap_str) < sizeof (db_str))
    strncat (db_str, mmap_str, 32);
  else
    error (EXIT_FAILURE, errno, "Filename too long");
  snprintf (buckets_str, sizeof(buckets_str), "#bnum=%lu", num_buckets);
  if (strlen (dbfile) + strlen (mmap_str) < sizeof (db_str))
    strncat (db_str, buckets_str, 32);
  else
    error (EXIT_FAILURE, errno, "Filename too long");
  db = zdb_open (db_str, mode, false);
#else
  db = zdb_open (dbfile, mode, false);
  GDBM_FILE database = (GDBM_FILE)db;
  gdbm_setopt (database, GDBM_SETMAXMAPSIZE, &mmap_size, sizeof (size_t));
  gdbm_setopt (database, GDBM_SETCACHESIZE, &num_buckets, sizeof (size_t));
#endif
  return db;
}

void *
zdb_open (char *dbfile, int mode, bool verbose)
{
  void *db;
  if (verbose)
    printf ("Opening database %s\n", dbfile);
#ifdef HAVE_KYOTOCABINET
  KCDB *database;
  int32_t res;
  database = kcdbnew ();
  int kc_mode = 0;
  if ((mode & ZDB_READER) == ZDB_READER)
    kc_mode ^= KCOREADER;
  else if ((mode & ZDB_WRITER) == ZDB_WRITER)
    kc_mode ^= KCOWRITER;
  else if ((mode & ZDB_CREATOR) == ZDB_CREATOR)
    kc_mode ^= KCOCREATE;
  if ((mode & ZDB_SYNC) == ZDB_SYNC)
    kc_mode ^= KCOAUTOSYNC;
  if ((mode & ZDB_NOLOCK) == ZDB_NOLOCK)
    kc_mode ^= KCONOLOCK;
  res = kcdbopen (database, dbfile, KCOREADER);
  if (!res)
    error (EXIT_FAILURE, errno, "Failed to open database %s", dbfile);
  db = database;
#else
  GDBM_FILE database;
  int gdbm_mode = 0;
  if ((mode & ZDB_READER) == ZDB_READER)
    gdbm_mode ^= GDBM_READER;
  else if ((mode & ZDB_WRITER) == ZDB_WRITER)
    gdbm_mode ^= GDBM_WRITER;
  else if ((mode & ZDB_CREATOR) == ZDB_CREATOR)
................................................................................
    gdbm_mode ^= GDBM_NOLOCK;
  if ((mode & ZDB_NOMMAP) == ZDB_NOMMAP)
    gdbm_mode ^= GDBM_NOMMAP;
  database = gdbm_open (dbfile, 512, gdbm_mode,
                        S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, NULL);
  if (!database)
    error (EXIT_FAILURE, errno, "Failed to open database %s", dbfile);
  db = (void *)database;
#endif
  return db;
}

int
zdb_close (void *db, bool verbose)
{
  if (verbose)
    printf ("Closing database\n");
#ifdef HAVE_KYOTOCABINET
  KCDB *database = (KCDB *)db;
  if (!kcdbclose (database))
    return 1;
  kcdbdel (database);
#else
  GDBM_FILE database = (GDBM_FILE)db;
  gdbm_close (database);
#endif
  return 0;
}

int
zdb_store (void *db, char *key_str, size_t key_size, char *value_str,
           size_t value_size, bool verbose)
{
  if (verbose)
    printf ("Storing %s|%s                             \r", key_str, value_str);
#ifdef HAVE_KYOTOCABINET
  KCDB *database = (KCDB *)db;
  if (!kcdbset (database, key_str, key_size, value_str, value_size))
    return 1;
#else
  GDBM_FILE database = (GDBM_FILE)db;
  datum key, value;
  value.dptr = value_str;
  value.dsize = value_size;
  key.dptr = key_str;
  key.dsize = key_size;
  if (gdbm_store (database, key, value, GDBM_REPLACE))
    return 1;
#endif
  return 0;
}

void
printrec (char *key, char *value, char *delim)
{
  if (delim)
    printf ("%s%s%s\n", key, delim, value);
  else
    printf ("%s\n", value);
}

int
zdb_fetch (void *db, char *key_str, size_t key_size, char *delim)
{
  char *value_str;
#ifdef HAVE_KYOTOCABINET
  KCDB *database = (KCDB *)db;
  size_t value_size;
  value_str = kcdbget (database, key_str, key_size, &value_size);
#else
  GDBM_FILE database = (GDBM_FILE)db;
  datum key, value;
  key.dptr = key_str;
  key.dsize = key_size;
  value = gdbm_fetch (database, key);
  if (value.dptr == NULL)
    return 1;
  value_str = strndup (value.dptr, value.dsize);
  free (value.dptr);
#endif
  if (!value_str)
    return 1;
  printrec (key_str, value_str, delim);
#ifdef HAVE_KYOTOCABINET
  kcfree (value_str);
#else
  free (value_str);
#endif
  return 0;
}

int
zdb_fetchall (void *db, char *delim)
{
  char *key_str;
#ifdef HAVE_KYOTOCABINET
  KCDB *database = (KCDB *)db;
  KCCUR *cur;
  const char *value_str;
  size_t key_size, value_size;
  cur = kcdbcursor (database);
  kccurjump (cur);
  while ((key_str = kccurget(cur, &key_size, &value_str, &value_size, 1)) != NULL)
    {
      printrec (key_str, (char *) value_str, delim);
      kcfree (key_str);
    }
  kccurdel (cur);
#else
  char *value_str;
  datum key, nextkey, value;
  GDBM_FILE database = (GDBM_FILE)db;
  key = gdbm_firstkey (database);
  while (key.dptr)
    {
      value = gdbm_fetch (database, key);
      if (value.dptr == NULL)
        error (0, errno, "Key with non-existent value: %s", key.dptr);
      value_str = strndup (value.dptr, value.dsize);
................................................................................
      free (value.dptr);
      free (key_str);
      free (value_str);
      nextkey = gdbm_nextkey (database, key);
      free (key.dptr);
      key = nextkey;
    }
#endif
  return (0);
}

int
zdb_remove (void *db, char *key_str, size_t key_size, bool verbose)
{
  if (verbose)
    printf ("Removing %s\r", key_str);
#ifdef HAVE_KYOTOCABINET
  KCDB *database = (KCDB *)db;
  if (!kcdbremove (database, key_str, key_size))
    return 1;
#else
  GDBM_FILE database = (GDBM_FILE)db;
  datum key;
  key.dptr = key_str;
  key.dsize = key_size;
  if (gdbm_delete (database, key))
    return 1;
#endif
  return 0;
}

int
zdb_reorganize (void *db)
{
#ifdef HAVE_KYOTOCABINET
  return 0;
#else
  GDBM_FILE database = (GDBM_FILE)db;
  if (gdbm_reorganize (database))
    return 1;
  return 0;
#endif
}

void
zdb_print_info (void *db)
{
#ifdef HAVE_KYOTOCABINET
  KCDB *database = (KCDB *)db;
  int64_t num_records;
  char *status;
  num_records = kcdbcount (database);
  status = kcdbstatus (database);
  printf ("Number of records:\t%ld\n", num_records);
  printf ("Internal status:\n%s\n", status);
  free (status);
#else
  GDBM_FILE database = (GDBM_FILE)db;
  size_t cache_size;
  size_t mmap_size_out;
  int mmap;
  gdbm_count_t num_records;
  gdbm_setopt (database, GDBM_GETCACHESIZE, &cache_size, sizeof (size_t));
  gdbm_setopt (database, GDBM_GETMAXMAPSIZE, &mmap_size_out, sizeof (size_t));
  gdbm_setopt (database, GDBM_GETMMAP, &mmap, sizeof (int));
................................................................................
      printf ("Memory-map size:\t%zd\n", mmap_size_out);
    }
  else
    {
      puts ("Memory-map enabled:\tfalse");
    }
  printf ("Number of records:\t%lld\n", num_records);
#endif
  return;
}







<
>
|
|

<

<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<

<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







 







|
|
|



|



<
<
<
<
<
<
<

<




|




<
<
<
<
<
<







<













|


<
<
<
<
<
<








<



<
<
<

<




|


<
<
<
<
<
<
<
<
<
<
<
<
<
<


<







 







<




|



<
<
<
<
<
<





<




|

<
<
<
<



<



|

<
<
<
<
<
<
<
<
<
<
<







 







<


16
17
18
19
20
21
22

23
24
25
26

27

































28




















29
30
31
32
33
34
35
..
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
...
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
...
167
168
169
170
171
172
173

174
175

   You should have received a copy of the GNU General Public License
   along with zeptodb.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "zdb.h"


GDBM_FILE
zdb_open (char *dbfile, int mode, size_t mmap_size, size_t cache_size,
          bool verbose)
{

  if (verbose)

































    printf ("Opening database %s\n", dbfile);




















  GDBM_FILE database;
  int gdbm_mode = 0;
  if ((mode & ZDB_READER) == ZDB_READER)
    gdbm_mode ^= GDBM_READER;
  else if ((mode & ZDB_WRITER) == ZDB_WRITER)
    gdbm_mode ^= GDBM_WRITER;
  else if ((mode & ZDB_CREATOR) == ZDB_CREATOR)
................................................................................
    gdbm_mode ^= GDBM_NOLOCK;
  if ((mode & ZDB_NOMMAP) == ZDB_NOMMAP)
    gdbm_mode ^= GDBM_NOMMAP;
  database = gdbm_open (dbfile, 512, gdbm_mode,
                        S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, NULL);
  if (!database)
    error (EXIT_FAILURE, errno, "Failed to open database %s", dbfile);
  gdbm_setopt (database, GDBM_SETMAXMAPSIZE, &mmap_size, sizeof (size_t));
  gdbm_setopt (database, GDBM_SETCACHESIZE, &cache_size, sizeof (size_t));
  return database;
}

int
zdb_close (GDBM_FILE database, bool verbose)
{
  if (verbose)
    printf ("Closing database\n");







  gdbm_close (database);

  return 0;
}

int
zdb_store (GDBM_FILE database, char *key_str, size_t key_size, char *value_str,
           size_t value_size, bool verbose)
{
  if (verbose)
    printf ("Storing %s|%s                             \r", key_str, value_str);






  datum key, value;
  value.dptr = value_str;
  value.dsize = value_size;
  key.dptr = key_str;
  key.dsize = key_size;
  if (gdbm_store (database, key, value, GDBM_REPLACE))
    return 1;

  return 0;
}

void
printrec (char *key, char *value, char *delim)
{
  if (delim)
    printf ("%s%s%s\n", key, delim, value);
  else
    printf ("%s\n", value);
}

int
zdb_fetch (GDBM_FILE database, char *key_str, size_t key_size, char *delim)
{
  char *value_str;






  datum key, value;
  key.dptr = key_str;
  key.dsize = key_size;
  value = gdbm_fetch (database, key);
  if (value.dptr == NULL)
    return 1;
  value_str = strndup (value.dptr, value.dsize);
  free (value.dptr);

  if (!value_str)
    return 1;
  printrec (key_str, value_str, delim);



  free (value_str);

  return 0;
}

int
zdb_fetchall (GDBM_FILE database, char *delim)
{
  char *key_str;














  char *value_str;
  datum key, nextkey, value;

  key = gdbm_firstkey (database);
  while (key.dptr)
    {
      value = gdbm_fetch (database, key);
      if (value.dptr == NULL)
        error (0, errno, "Key with non-existent value: %s", key.dptr);
      value_str = strndup (value.dptr, value.dsize);
................................................................................
      free (value.dptr);
      free (key_str);
      free (value_str);
      nextkey = gdbm_nextkey (database, key);
      free (key.dptr);
      key = nextkey;
    }

  return (0);
}

int
zdb_remove (GDBM_FILE database, char *key_str, size_t key_size, bool verbose)
{
  if (verbose)
    printf ("Removing %s\r", key_str);






  datum key;
  key.dptr = key_str;
  key.dsize = key_size;
  if (gdbm_delete (database, key))
    return 1;

  return 0;
}

int
zdb_reorganize (GDBM_FILE database)
{




  if (gdbm_reorganize (database))
    return 1;
  return 0;

}

void
zdb_print_info (GDBM_FILE database)
{











  size_t cache_size;
  size_t mmap_size_out;
  int mmap;
  gdbm_count_t num_records;
  gdbm_setopt (database, GDBM_GETCACHESIZE, &cache_size, sizeof (size_t));
  gdbm_setopt (database, GDBM_GETMAXMAPSIZE, &mmap_size_out, sizeof (size_t));
  gdbm_setopt (database, GDBM_GETMMAP, &mmap, sizeof (int));
................................................................................
      printf ("Memory-map size:\t%zd\n", mmap_size_out);
    }
  else
    {
      puts ("Memory-map enabled:\tfalse");
    }
  printf ("Number of records:\t%lld\n", num_records);

  return;
}

Changes to src/zdb.h.

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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "error.h"
#include <errno.h>
#include <sys/stat.h>
#include <stdbool.h>

#ifdef HAVE_KYOTOCABINET
#include <kclangc.h>
#else
#include <gdbm.h>
#endif

#define ZDB_READER 1<<0
#define ZDB_WRITER 1<<1
#define ZDB_CREATOR 1<<2
#define ZDB_SYNC 1<<3
#define ZDB_NOLOCK 1<<4
#define ZDB_NOMMAP 1<<5

void *zdb_create (char *, size_t, size_t, bool, int);
void *zdb_open (char *, int, bool);
int zdb_close (void *, bool);
int zdb_store (void *, char *, size_t, char *, size_t, bool);
void printrec (char *, char *, char *);
int zdb_fetch (void *, char *, size_t, char *);
int zdb_fetchall (void *, char *);
int zdb_remove (void *, char *, size_t, bool);
int zdb_reorganize (void *);
void zdb_print_info (void *db);

#endif







<
<
<
<

<








<
|
|
|

|
|
|
|
|


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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "error.h"
#include <errno.h>
#include <sys/stat.h>
#include <stdbool.h>




#include <gdbm.h>


#define ZDB_READER 1<<0
#define ZDB_WRITER 1<<1
#define ZDB_CREATOR 1<<2
#define ZDB_SYNC 1<<3
#define ZDB_NOLOCK 1<<4
#define ZDB_NOMMAP 1<<5


GDBM_FILE zdb_open (char *, int, size_t, size_t, bool);
int zdb_close (GDBM_FILE , bool);
int zdb_store (GDBM_FILE , char *, size_t, char *, size_t, bool);
void printrec (char *, char *, char *);
int zdb_fetch (GDBM_FILE , char *, size_t, char *);
int zdb_fetchall (GDBM_FILE , char *);
int zdb_remove (GDBM_FILE , char *, size_t, bool);
int zdb_reorganize (GDBM_FILE );
void zdb_print_info (GDBM_FILE db);

#endif

Changes to src/zdbc.c.

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
..
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
...
144
145
146
147
148
149
150
151

152
153
154
155
156
157
158
159
160
161
162
163
164
165
...
168
169
170
171
172
173
174
175
176
177
178
179
180
181

182
183
184
185
186
187
188
189
190
static char doc[] =
  "zdbc -- a tool for creating a DBM database";
static char args_doc[] = "DATABASE";

static struct argp_option options[] = {
  {"mmap-size",   (int)'m', "NUM", 0,
   "The size (in bytes) of the memory-mapped region to use (default=1024)", 0},
  {"num-buckets", (int)'b', "NUM", 0,
   "The number of buckets to use (default=100)", 0},




  {"verbose", (int)'v', (const char *)NULL, 0,
   "Print extra information.", 0},
  {"sync", (int)'s', (const char *)NULL, 0,
   "Automatically synchronize all database operations to the disk", 0},
  {"no-lock", (int)'l', (const char *)NULL, 0,
   "Do not perform any locking of the database file", 0},
#ifdef HAVE_GDBM
  {"no-mmap", (int)'n', (const char *)NULL, 0,
   "Do not memory-map the database file", 0},
#endif
  {(const char *)NULL, 0, (const char *)NULL, 0, (const char *)NULL, 0}
};

struct arguments
{
  char *args[1];
  size_t mmap_size;
  size_t num_buckets;
  bool sync;
  bool no_lock;
#ifdef HAVE_GDBM
  bool no_mmap;
#endif
  bool verbose;

};

static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
  struct arguments *arguments = state->input;
  unsigned long int size;
  switch (key)
    {
    case 'b':
      size = arg ? strtoul (arg, NULL, 10) : 100UL;
      if (size > SIZE_MAX)
        {
          arguments->num_buckets = SIZE_MAX;
        }
      else
        {
          arguments->num_buckets = (size_t)size;
        }
      break;
    case 'l':
      arguments->no_lock = true;
      break;
    case 'm':
      size = arg ? strtoul (arg, NULL, 10) : 131072UL;
................................................................................
          arguments->mmap_size = SIZE_MAX;
        }
      else
        {
          arguments->mmap_size = (size_t)size;
        }
      break;
#ifdef HAVE_GDBM
    case 'n':
      arguments->no_mmap = true;
      break;
#endif
    case 's':
      arguments->sync = true;
      break;
    case 'v':
      arguments->verbose = true;
      break;
    case ARGP_KEY_ARG:
................................................................................
int
main (int argc, char **argv)
{
  struct arguments arguments;
  int mode = ZDB_CREATOR;
  error_t argp_result;

  arguments.verbose = false;

  arguments.sync = false;
  arguments.no_lock = false;
#ifdef HAVE_GDBM
  arguments.no_mmap = false;
#endif
  arguments.mmap_size = 1024;
  arguments.num_buckets = 10;

  argp_result = argp_parse (&argp, argc, argv, 0, 0, &arguments);
  if (argp_result)
    error (EXIT_FAILURE, argp_result, "Failed to parse arguments");

  if (signal (SIGINT, termination_handler) == SIG_IGN)
    signal (SIGINT, SIG_IGN);
................................................................................
  if (signal (SIGTERM, termination_handler) == SIG_IGN)
    signal (SIGTERM, SIG_IGN);

  if (arguments.sync)
    mode ^= ZDB_SYNC;
  if (arguments.no_lock)
    mode ^= ZDB_NOLOCK;
#ifdef HAVE_GDBM
  if (arguments.no_mmap)
    mode ^= ZDB_NOMMAP;
#endif

  /* open the database */
  database = zdb_create (arguments.args[0], arguments.mmap_size,

                         arguments.num_buckets,  arguments.verbose, mode);
  if (!database)
    error (EXIT_FAILURE, errno,
           "Failed to create database %s", arguments.args[0]);
  else if (zdb_close (database, arguments.verbose) != 0)
    error (EXIT_FAILURE, errno,
           "Failed to close database %s", arguments.args[0]);
  exit (EXIT_SUCCESS);
}







|
|
>
>
>
>




<
<
<
<
<
<







|
|

<
<
<

>









|
|


|



|







 







<



<







 







|
>
|

<
|
<
|
<







 







<


<


<
>
|








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
..
93
94
95
96
97
98
99

100
101
102

103
104
105
106
107
108
109
...
138
139
140
141
142
143
144
145
146
147
148

149

150

151
152
153
154
155
156
157
...
160
161
162
163
164
165
166

167
168

169
170

171
172
173
174
175
176
177
178
179
180
static char doc[] =
  "zdbc -- a tool for creating a DBM database";
static char args_doc[] = "DATABASE";

static struct argp_option options[] = {
  {"mmap-size",   (int)'m', "NUM", 0,
   "The size (in bytes) of the memory-mapped region to use (default=1024)", 0},
  {"cache-size", (int)'c', "NUM", 0,
   "The bucket cache size to use (default=128)", 0},
  {"no-mmap", (int)'n', (const char *)NULL, 0,
   "Do not memory-map the database file", 0},
  {"no-lock", (int)'l', (const char *)NULL, 0,
   "Do not perform any locking of the database file", 0},
  {"verbose", (int)'v', (const char *)NULL, 0,
   "Print extra information.", 0},
  {"sync", (int)'s', (const char *)NULL, 0,
   "Automatically synchronize all database operations to the disk", 0},






  {(const char *)NULL, 0, (const char *)NULL, 0, (const char *)NULL, 0}
};

struct arguments
{
  char *args[1];
  size_t mmap_size;
  size_t cache_size;
  bool no_mmap;
  bool no_lock;



  bool verbose;
  bool sync;
};

static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
  struct arguments *arguments = state->input;
  unsigned long int size;
  switch (key)
    {
    case 'c':
      size = arg ? strtoul (arg, NULL, 10) : 128UL;
      if (size > SIZE_MAX)
        {
          arguments->cache_size = SIZE_MAX;
        }
      else
        {
          arguments->cache_size = (size_t)size;
        }
      break;
    case 'l':
      arguments->no_lock = true;
      break;
    case 'm':
      size = arg ? strtoul (arg, NULL, 10) : 131072UL;
................................................................................
          arguments->mmap_size = SIZE_MAX;
        }
      else
        {
          arguments->mmap_size = (size_t)size;
        }
      break;

    case 'n':
      arguments->no_mmap = true;
      break;

    case 's':
      arguments->sync = true;
      break;
    case 'v':
      arguments->verbose = true;
      break;
    case ARGP_KEY_ARG:
................................................................................
int
main (int argc, char **argv)
{
  struct arguments arguments;
  int mode = ZDB_CREATOR;
  error_t argp_result;

  arguments.mmap_size = 1024;
  arguments.cache_size = 128;
  arguments.no_mmap = false;
  arguments.no_lock = false;

  arguments.verbose = false;

  arguments.sync = false;


  argp_result = argp_parse (&argp, argc, argv, 0, 0, &arguments);
  if (argp_result)
    error (EXIT_FAILURE, argp_result, "Failed to parse arguments");

  if (signal (SIGINT, termination_handler) == SIG_IGN)
    signal (SIGINT, SIG_IGN);
................................................................................
  if (signal (SIGTERM, termination_handler) == SIG_IGN)
    signal (SIGTERM, SIG_IGN);

  if (arguments.sync)
    mode ^= ZDB_SYNC;
  if (arguments.no_lock)
    mode ^= ZDB_NOLOCK;

  if (arguments.no_mmap)
    mode ^= ZDB_NOMMAP;


  /* open the database */

  database = zdb_open (arguments.args[0], mode, arguments.mmap_size,
                         arguments.cache_size,  arguments.verbose);
  if (!database)
    error (EXIT_FAILURE, errno,
           "Failed to create database %s", arguments.args[0]);
  else if (zdb_close (database, arguments.verbose) != 0)
    error (EXIT_FAILURE, errno,
           "Failed to close database %s", arguments.args[0]);
  exit (EXIT_SUCCESS);
}

Changes to src/zdbf.c.

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
...
106
107
108
109
110
111
112
113










114
115
116
117
118
119
120
121
122
123
124
...
196
197
198
199
200
201
202
203

204
205
206
207
208
209
210
211
212
213
214
215
...
221
222
223
224
225
226
227
228
229
230
231
232
233
234

235
236
237
238
239
240
241
#include <config.h>
#include <string.h>
#include <stdio.h>
#include "error.h"
#include "argp.h"
#include <signal.h>
#include <stdbool.h>

#include "zdb.h"

const char *program_name = "zdbf";
const char *argp_program_version = PACKAGE_STRING;
const char *argp_program_bug_address = PACKAGE_BUGREPORT;
static void *database;
static char doc[] =
  "zdbf -- a tool for fetching records from a DBM database\v"
  "If an input file is provided, lines are read from it as individual queries, "
  "otherwise queries are taken from stdin.";
static char args_doc[] = "DATABASE";

static struct argp_option options[] = {










  {"input", (int)'i', "FILE", 0,
   "Read queries from a file", 0},
  {"all", (int)'a', (const char *)NULL, 0,
   "Fetch all the records in the database", 0},
  {"delim", (int)'d', "CHAR", 0,
   "Character to print between keys and values.  Keys will not be printed if "
   "left unspecified.", 0},
  {"sync", (int)'s', (const char *)NULL, 0,
   "Automatically synchronize all database operations to the disk", 0},
  {"no-lock", (int)'l', (const char *)NULL, 0,
   "Do not perform any locking of the database file", 0},
#ifdef HAVE_GDBM
  {"no-mmap", (int)'n', (const char *)NULL, 0,
   "Do not memory-map the database file", 0},
#endif
  {"verbose", (int)'v', (const char *)NULL, 0,
   "Print extra information.", 0},
  {(const char *)NULL, 0, (const char *)NULL, 0, (const char *)NULL, 0}
};

struct arguments
{
  char *args[1];
  char *input_file;





  bool all;
  char *delim;
  bool sync;
  bool no_lock;
#ifdef HAVE_GDBM
  bool no_mmap;
#endif
  bool verbose;
};

static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
  struct arguments *arguments = state->input;

  switch (key)
    {
    case 'a':
      arguments->all = true;











      break;
    case 'd':
      if (strlen (arg) > 1)
        argp_usage (state);
      arguments->delim = arg;
      break;
    case 'i':
................................................................................
          strncpy (arguments->input_file, arg, strlen (arg));
          arguments->input_file[strlen (arg)] = '\0';
        }
      break;
    case 'l':
      arguments->no_lock = true;
      break;
#ifdef HAVE_GDBM










    case 'n':
      arguments->no_mmap = true;
      break;
#endif
    case 's':
      arguments->sync = true;
      break;
    case 'v':
      arguments->verbose = true;
      break;
    case ARGP_KEY_ARG:
................................................................................
main (int argc, char **argv)
{
  FILE *input;
  struct arguments arguments;
  int mode = ZDB_READER;
  error_t argp_result;

  arguments.verbose = false;

  arguments.sync = false;
  arguments.no_lock = false;
#ifdef HAVE_GDBM
  arguments.no_mmap = false;
#endif
  arguments.input_file = NULL;
  arguments.all = false;
  arguments.delim = NULL;

  argp_result = argp_parse (&argp, argc, argv, 0, 0, &arguments);
  if (argp_result)
    error (EXIT_FAILURE, argp_result, "Failed to parse arguments");
................................................................................
  if (signal (SIGTERM, termination_handler) == SIG_IGN)
    signal (SIGTERM, SIG_IGN);

  if (arguments.sync)
    mode ^= ZDB_SYNC;
  if (arguments.no_lock)
    mode ^= ZDB_NOLOCK;
#ifdef HAVE_GDBM
  if (arguments.no_mmap)
    mode ^= ZDB_NOMMAP;
#endif

  /* open the database */
  database = zdb_open (arguments.args[0], mode, arguments.verbose);

  if (!database)
    error (EXIT_FAILURE, errno,
           "Failed to open database %s", arguments.args[0]);

  /* if the "all" argument was passed, output all the records */
  /* and exit */
  if (arguments.all != 0)







>













>
>
>
>
>
>
>
>
>
>









<
<
<
<
<
<
<
<







>
>
>
>
>



<
<
<
<
<






|




>
>
>
>
>
>
>
>
>
>
>







 







|
>
>
>
>
>
>
>
>
>
>



<







 







|
>
|

<
|
|







 







<


<


|
>







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
...
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
...
219
220
221
222
223
224
225
226
227
228
229

230
231
232
233
234
235
236
237
238
...
244
245
246
247
248
249
250

251
252

253
254
255
256
257
258
259
260
261
262
263
#include <config.h>
#include <string.h>
#include <stdio.h>
#include "error.h"
#include "argp.h"
#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
#include "zdb.h"

const char *program_name = "zdbf";
const char *argp_program_version = PACKAGE_STRING;
const char *argp_program_bug_address = PACKAGE_BUGREPORT;
static void *database;
static char doc[] =
  "zdbf -- a tool for fetching records from a DBM database\v"
  "If an input file is provided, lines are read from it as individual queries, "
  "otherwise queries are taken from stdin.";
static char args_doc[] = "DATABASE";

static struct argp_option options[] = {
  {"mmap-size",   (int)'m', "NUM", 0,
   "The size (in bytes) of the memory-mapped region to use (default=1024)", 0},
  {"cache-size", (int)'c', "NUM", 0,
   "The bucket cache size to use (default=128)", 0},
  {"no-mmap", (int)'n', (const char *)NULL, 0,
   "Do not memory-map the database file", 0},
  {"no-lock", (int)'l', (const char *)NULL, 0,
   "Do not perform any locking of the database file", 0},
  {"verbose", (int)'v', (const char *)NULL, 0,
   "Print extra information.", 0},
  {"input", (int)'i', "FILE", 0,
   "Read queries from a file", 0},
  {"all", (int)'a', (const char *)NULL, 0,
   "Fetch all the records in the database", 0},
  {"delim", (int)'d', "CHAR", 0,
   "Character to print between keys and values.  Keys will not be printed if "
   "left unspecified.", 0},
  {"sync", (int)'s', (const char *)NULL, 0,
   "Automatically synchronize all database operations to the disk", 0},








  {(const char *)NULL, 0, (const char *)NULL, 0, (const char *)NULL, 0}
};

struct arguments
{
  char *args[1];
  char *input_file;
  size_t mmap_size;
  size_t cache_size;
  bool no_mmap;
  bool no_lock;
  bool verbose;
  bool all;
  char *delim;
  bool sync;





};

static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
  struct arguments *arguments = state->input;
  unsigned long int size;
  switch (key)
    {
    case 'a':
      arguments->all = true;
      break;
    case 'c':
      size = arg ? strtoul (arg, NULL, 10) : 128UL;
      if (size > SIZE_MAX)
        {
          arguments->cache_size = SIZE_MAX;
        }
      else
        {
          arguments->cache_size = (size_t)size;
        }
      break;
    case 'd':
      if (strlen (arg) > 1)
        argp_usage (state);
      arguments->delim = arg;
      break;
    case 'i':
................................................................................
          strncpy (arguments->input_file, arg, strlen (arg));
          arguments->input_file[strlen (arg)] = '\0';
        }
      break;
    case 'l':
      arguments->no_lock = true;
      break;
    case 'm':
      size = arg ? strtoul (arg, NULL, 10) : 131072UL;
      if (size > SIZE_MAX)
        {
          arguments->mmap_size = SIZE_MAX;
        }
      else
        {
          arguments->mmap_size = (size_t)size;
        }
      break;
    case 'n':
      arguments->no_mmap = true;
      break;

    case 's':
      arguments->sync = true;
      break;
    case 'v':
      arguments->verbose = true;
      break;
    case ARGP_KEY_ARG:
................................................................................
main (int argc, char **argv)
{
  FILE *input;
  struct arguments arguments;
  int mode = ZDB_READER;
  error_t argp_result;

  arguments.mmap_size = 1024;
  arguments.cache_size = 128;
  arguments.no_mmap = false;
  arguments.no_lock = false;

  arguments.verbose = false;
  arguments.sync = false;
  arguments.input_file = NULL;
  arguments.all = false;
  arguments.delim = NULL;

  argp_result = argp_parse (&argp, argc, argv, 0, 0, &arguments);
  if (argp_result)
    error (EXIT_FAILURE, argp_result, "Failed to parse arguments");
................................................................................
  if (signal (SIGTERM, termination_handler) == SIG_IGN)
    signal (SIGTERM, SIG_IGN);

  if (arguments.sync)
    mode ^= ZDB_SYNC;
  if (arguments.no_lock)
    mode ^= ZDB_NOLOCK;

  if (arguments.no_mmap)
    mode ^= ZDB_NOMMAP;


  /* open the database */
  database = zdb_open (arguments.args[0], mode, arguments.mmap_size,
                       arguments.cache_size, arguments.verbose);
  if (!database)
    error (EXIT_FAILURE, errno,
           "Failed to open database %s", arguments.args[0]);

  /* if the "all" argument was passed, output all the records */
  /* and exit */
  if (arguments.all != 0)

Changes to src/zdbi.c.

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
...
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
...
129
130
131
132
133
134
135
136
137
138
139
140
141
142

143
144
145
146
147
148
149
150
151
const char *argp_program_bug_address = PACKAGE_BUGREPORT;
static void *database;
static char doc[] =
  "zdbi -- inspect a DBM database";
static char args_doc[] = "DATABASE";

static struct argp_option options[] = {






  {"no-lock", (int)'l', (const char *)NULL, 0,
   "Do not perform any locking of the database file", 0},
  {"verbose", (int)'v', (const char *)NULL, 0,
   "Print extra information.", 0},
#ifdef HAVE_GDBM
  {"no-mmap", (int)'n', (const char *)NULL, 0,
   "Do not memory-map the database file", 0},
#endif
  {(const char *)NULL, 0, (const char *)NULL, 0, (const char *)NULL, 0}
};

struct arguments
{
  char *args[1];
  bool no_lock;
#ifdef HAVE_GDBM
  bool no_mmap;
#endif
  bool verbose;
};

static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
  struct arguments *arguments = state->input;
  unsigned long int size;
  switch (key)
    {











    case 'l':
      arguments->no_lock = true;
      break;
#ifdef HAVE_GDBM










    case 'n':
      arguments->no_mmap = true;
      break;
#endif
    case 'v':
      arguments->verbose = true;
      break;
    case ARGP_KEY_ARG:
      if (state->arg_num >= 1)
        argp_usage (state);
      arguments->args[state->arg_num] = arg;
................................................................................
int
main (int argc, char **argv)
{
  struct arguments arguments;
  int mode = ZDB_READER;
  error_t argp_result;

  arguments.no_lock = false;
#ifdef HAVE_GDBM
  arguments.no_mmap = false;
#endif
  arguments.verbose = false;

  argp_result = argp_parse (&argp, argc, argv, 0, 0, &arguments);
  if (argp_result)
    error (EXIT_FAILURE, argp_result, "Failed to parse arguments");

  if (signal (SIGINT, termination_handler) == SIG_IGN)
................................................................................
  if (signal (SIGHUP, termination_handler) == SIG_IGN)
    signal (SIGHUP, SIG_IGN);
  if (signal (SIGTERM, termination_handler) == SIG_IGN)
    signal (SIGTERM, SIG_IGN);

  if (arguments.no_lock)
    mode ^= ZDB_NOLOCK;
#ifdef HAVE_GDBM
  if (arguments.no_mmap)
    mode ^= ZDB_NOMMAP;
#endif

  /* open the database */
  database = zdb_open (arguments.args[0], mode, arguments.verbose);

  if (!database)
    error (EXIT_FAILURE, errno,
           "Failed to open database %s", arguments.args[0]);
  zdb_print_info (database);
  if (zdb_close (database, arguments.verbose) != 0)
    error (EXIT_FAILURE, errno,
           "Failed to close database %s", arguments.args[0]);
  exit (EXIT_SUCCESS);
}







>
>
>
>
>
>




<
<
<
<






|
|

|










>
>
>
>
>
>
>
>
>
>
>



|
>
>
>
>
>
>
>
>
>
>



<







 







|
|

|







 







<


<


|
>









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
...
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
...
151
152
153
154
155
156
157

158
159

160
161
162
163
164
165
166
167
168
169
170
171
172
const char *argp_program_bug_address = PACKAGE_BUGREPORT;
static void *database;
static char doc[] =
  "zdbi -- inspect a DBM database";
static char args_doc[] = "DATABASE";

static struct argp_option options[] = {
  {"mmap-size",   (int)'m', "NUM", 0,
   "The size (in bytes) of the memory-mapped region to use (default=1024)", 0},
  {"cache-size", (int)'c', "NUM", 0,
   "The bucket cache size to use (default=128)", 0},
  {"no-mmap", (int)'n', (const char *)NULL, 0,
   "Do not memory-map the database file", 0},
  {"no-lock", (int)'l', (const char *)NULL, 0,
   "Do not perform any locking of the database file", 0},
  {"verbose", (int)'v', (const char *)NULL, 0,
   "Print extra information.", 0},




  {(const char *)NULL, 0, (const char *)NULL, 0, (const char *)NULL, 0}
};

struct arguments
{
  char *args[1];
  size_t mmap_size;
  size_t cache_size;
  bool no_mmap;
  bool no_lock;
  bool verbose;
};

static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
  struct arguments *arguments = state->input;
  unsigned long int size;
  switch (key)
    {
    case 'c':
      size = arg ? strtoul (arg, NULL, 10) : 128UL;
      if (size > SIZE_MAX)
        {
          arguments->cache_size = SIZE_MAX;
        }
      else
        {
          arguments->cache_size = (size_t)size;
        }
      break;
    case 'l':
      arguments->no_lock = true;
      break;
    case 'm':
      size = arg ? strtoul (arg, NULL, 10) : 131072UL;
      if (size > SIZE_MAX)
        {
          arguments->mmap_size = SIZE_MAX;
        }
      else
        {
          arguments->mmap_size = (size_t)size;
        }
      break;
    case 'n':
      arguments->no_mmap = true;
      break;

    case 'v':
      arguments->verbose = true;
      break;
    case ARGP_KEY_ARG:
      if (state->arg_num >= 1)
        argp_usage (state);
      arguments->args[state->arg_num] = arg;
................................................................................
int
main (int argc, char **argv)
{
  struct arguments arguments;
  int mode = ZDB_READER;
  error_t argp_result;

  arguments.mmap_size = 1024;
  arguments.cache_size = 128;
  arguments.no_mmap = false;
  arguments.no_lock = false;
  arguments.verbose = false;

  argp_result = argp_parse (&argp, argc, argv, 0, 0, &arguments);
  if (argp_result)
    error (EXIT_FAILURE, argp_result, "Failed to parse arguments");

  if (signal (SIGINT, termination_handler) == SIG_IGN)
................................................................................
  if (signal (SIGHUP, termination_handler) == SIG_IGN)
    signal (SIGHUP, SIG_IGN);
  if (signal (SIGTERM, termination_handler) == SIG_IGN)
    signal (SIGTERM, SIG_IGN);

  if (arguments.no_lock)
    mode ^= ZDB_NOLOCK;

  if (arguments.no_mmap)
    mode ^= ZDB_NOMMAP;


  /* open the database */
  database = zdb_open (arguments.args[0], mode, arguments.mmap_size,
                       arguments.cache_size, arguments.verbose);
  if (!database)
    error (EXIT_FAILURE, errno,
           "Failed to open database %s", arguments.args[0]);
  zdb_print_info (database);
  if (zdb_close (database, arguments.verbose) != 0)
    error (EXIT_FAILURE, errno,
           "Failed to close database %s", arguments.args[0]);
  exit (EXIT_SUCCESS);
}

Changes to src/zdbr.c.

23
24
25
26
27
28
29

30
31
32
33
34
35
36
..
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
..
98
99
100
101
102
103
104
105










106
107
108
109
110
111
112
113
114
115
116
...
191
192
193
194
195
196
197
198

199
200
201
202
203
204
205
206
207
208
209
210
...
215
216
217
218
219
220
221
222
223
224
225
226
227
228

229
230
231
232
233
234
235
#include <config.h>
#include <string.h>
#include <stdio.h>
#include "error.h"
#include "argp.h"
#include <signal.h>
#include <stdbool.h>

#include "zdb.h"

const char *program_name = "zdbr";
const char *argp_program_version = PACKAGE_STRING;
const char *argp_program_bug_address = PACKAGE_BUGREPORT;
void *database;
static char doc[] =
................................................................................
  "many records from the database, some fragmentation can occur, in which case "
  "it is recommended to reorganize the database. If the 'reorganize' argument "
  "is passed, the database will be reorganized after any records have been "
  "removed";
static char args_doc[] = "DATABASE";

static struct argp_option options[] = {










  {"input", (int)'i', "FILE", 0,
   "Read queries from a file", 0},
  {"reorganize", (int)'r', (const char *)NULL, 0,
   "Reorganize the database", 0},
  {"sync", (int)'s', (const char *)NULL, 0,
   "Automatically synchronize all database operations to the disk", 0},
  {"no-lock", (int)'l', (const char *)NULL, 0,
   "Do not perform any locking of the database file", 0},
#ifdef HAVE_GDBM
  {"no-mmap", (int)'n', (const char *)NULL, 0,
   "Do not memory-map the database file", 0},
#endif
  {"verbose", (int)'v', (const char *)NULL, 0,
   "Print extra information.", 0},
  {(const char *)NULL, 0, (const char *)NULL, 0, (const char *)NULL, 0}
};

struct arguments
{
  char *args[1];





  char *input_file;
  bool reorg;
  bool sync;
  bool no_lock;
#ifdef HAVE_GDBM
  bool no_mmap;
#endif
  bool verbose;
};

static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
  struct arguments *arguments = state->input;


  switch (key)
    {











    case 'i':
      if (arguments->input_file)
        free (arguments->input_file);
      if (!arg)
        {
          arguments->input_file = NULL;
        }
................................................................................
          strncpy (arguments->input_file, arg, strlen (arg));
          arguments->input_file[strlen (arg)] = '\0';
        }
      break;
    case 'l':
      arguments->no_lock = true;
      break;
#ifdef HAVE_GDBM










    case 'n':
      arguments->no_mmap = true;
      break;
#endif
    case 'r':
      arguments->reorg = true;
      break;
    case 's':
      arguments->sync = true;
      break;
    case 'v':
................................................................................
main (int argc, char **argv)
{
  FILE *input;
  struct arguments arguments;
  int mode = ZDB_WRITER;
  error_t argp_result;

  arguments.verbose = false;

  arguments.sync = false;
  arguments.no_lock = false;
#ifdef HAVE_GDBM
  arguments.no_mmap = false;
#endif
  arguments.input_file = NULL;
  arguments.reorg = false;

  argp_result = argp_parse (&argp, argc, argv, 0, 0, &arguments);
  if (argp_result)
    error (EXIT_FAILURE, argp_result, "Failed to parse arguments");

................................................................................
  if (signal (SIGTERM, termination_handler) == SIG_IGN)
    signal (SIGTERM, SIG_IGN);

  if (arguments.sync)
    mode ^= ZDB_SYNC;
  if (arguments.no_lock)
    mode ^= ZDB_NOLOCK;
#ifdef HAVE_GDBM
  if (arguments.no_mmap)
    mode ^= ZDB_NOMMAP;
#endif

  /* open the database */
  database = zdb_open (arguments.args[0], mode, arguments.verbose);

  if (!database)
    error (EXIT_FAILURE, errno,
           "Failed to open database %s", arguments.args[0]);

  /* if an input file was provided, use it, otherwise use stdin */
  if (!arguments.input_file || strcmp (arguments.input_file, "-") == 0)
    input = stdin;







>







 







>
>
>
>
>
>
>
>
>
>






<
<
<
<
<
<
<
<






>
>
>
>
>



<
<
<
<
<






<
>


>
>
>
>
>
>
>
>
>
>
>







 







|
>
>
>
>
>
>
>
>
>
>



<







 







|
>
|

<
|
|







 







<


<


|
>







23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
..
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
...
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
...
214
215
216
217
218
219
220
221
222
223
224

225
226
227
228
229
230
231
232
233
...
238
239
240
241
242
243
244

245
246

247
248
249
250
251
252
253
254
255
256
257
#include <config.h>
#include <string.h>
#include <stdio.h>
#include "error.h"
#include "argp.h"
#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
#include "zdb.h"

const char *program_name = "zdbr";
const char *argp_program_version = PACKAGE_STRING;
const char *argp_program_bug_address = PACKAGE_BUGREPORT;
void *database;
static char doc[] =
................................................................................
  "many records from the database, some fragmentation can occur, in which case "
  "it is recommended to reorganize the database. If the 'reorganize' argument "
  "is passed, the database will be reorganized after any records have been "
  "removed";
static char args_doc[] = "DATABASE";

static struct argp_option options[] = {
  {"mmap-size",   (int)'m', "NUM", 0,
   "The size (in bytes) of the memory-mapped region to use (default=1024)", 0},
  {"cache-size", (int)'c', "NUM", 0,
   "The bucket cache size to use (default=128)", 0},
  {"no-mmap", (int)'n', (const char *)NULL, 0,
   "Do not memory-map the database file", 0},
  {"no-lock", (int)'l', (const char *)NULL, 0,
   "Do not perform any locking of the database file", 0},
  {"verbose", (int)'v', (const char *)NULL, 0,
   "Print extra information.", 0},
  {"input", (int)'i', "FILE", 0,
   "Read queries from a file", 0},
  {"reorganize", (int)'r', (const char *)NULL, 0,
   "Reorganize the database", 0},
  {"sync", (int)'s', (const char *)NULL, 0,
   "Automatically synchronize all database operations to the disk", 0},








  {(const char *)NULL, 0, (const char *)NULL, 0, (const char *)NULL, 0}
};

struct arguments
{
  char *args[1];
  size_t mmap_size;
  size_t cache_size;
  bool no_mmap;
  bool no_lock;
  bool verbose;
  char *input_file;
  bool reorg;
  bool sync;





};

static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
  struct arguments *arguments = state->input;

  unsigned long int size;
  switch (key)
    {
    case 'c':
      size = arg ? strtoul (arg, NULL, 10) : 128UL;
      if (size > SIZE_MAX)
        {
          arguments->cache_size = SIZE_MAX;
        }
      else
        {
          arguments->cache_size = (size_t)size;
        }
      break;
    case 'i':
      if (arguments->input_file)
        free (arguments->input_file);
      if (!arg)
        {
          arguments->input_file = NULL;
        }
................................................................................
          strncpy (arguments->input_file, arg, strlen (arg));
          arguments->input_file[strlen (arg)] = '\0';
        }
      break;
    case 'l':
      arguments->no_lock = true;
      break;
    case 'm':
      size = arg ? strtoul (arg, NULL, 10) : 131072UL;
      if (size > SIZE_MAX)
        {
          arguments->mmap_size = SIZE_MAX;
        }
      else
        {
          arguments->mmap_size = (size_t)size;
        }
      break;
    case 'n':
      arguments->no_mmap = true;
      break;

    case 'r':
      arguments->reorg = true;
      break;
    case 's':
      arguments->sync = true;
      break;
    case 'v':
................................................................................
main (int argc, char **argv)
{
  FILE *input;
  struct arguments arguments;
  int mode = ZDB_WRITER;
  error_t argp_result;

  arguments.mmap_size = 1024;
  arguments.cache_size = 128;
  arguments.no_mmap = false;
  arguments.no_lock = false;

  arguments.verbose = false;
  arguments.sync = false;
  arguments.input_file = NULL;
  arguments.reorg = false;

  argp_result = argp_parse (&argp, argc, argv, 0, 0, &arguments);
  if (argp_result)
    error (EXIT_FAILURE, argp_result, "Failed to parse arguments");

................................................................................
  if (signal (SIGTERM, termination_handler) == SIG_IGN)
    signal (SIGTERM, SIG_IGN);

  if (arguments.sync)
    mode ^= ZDB_SYNC;
  if (arguments.no_lock)
    mode ^= ZDB_NOLOCK;

  if (arguments.no_mmap)
    mode ^= ZDB_NOMMAP;


  /* open the database */
  database = zdb_open (arguments.args[0], mode, arguments.mmap_size,
                       arguments.cache_size, arguments.verbose);
  if (!database)
    error (EXIT_FAILURE, errno,
           "Failed to open database %s", arguments.args[0]);

  /* if an input file was provided, use it, otherwise use stdin */
  if (!arguments.input_file || strcmp (arguments.input_file, "-") == 0)
    input = stdin;

Changes to src/zdbs.c.

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
...
100
101
102
103
104
105
106
107










108
109
110
111
112
113
114
115
116
117
118
...
219
220
221
222
223
224
225
226

227
228
229
230
231
232
233
234
235
236
237
238
239
...
243
244
245
246
247
248
249
250
251
252
253
254
255
256

257
258
259
260
261
262
263
#include <config.h>
#include <string.h>
#include <stdio.h>
#include "error.h"
#include "argp.h"
#include <signal.h>
#include <stdbool.h>

#include "zdb.h"

const char *program_name = "zdbs";
const char *argp_program_version = PACKAGE_STRING;
const char *argp_program_bug_address = PACKAGE_BUGREPORT;
void *database;
static char doc[] =
................................................................................
  "zdbs -- a tool for storing records in a DBM database\v"
  "If an input file is provided, lines are read from it as new records, "
  "otherwise records are added from stdin. Records are expected in the "
  "format KEY|VALUE.";
static char args_doc[] = "DATABASE";

static struct argp_option options[] = {










  {"input", (int)'i', "FILE", 0,
   "Read new records from a file", 0},
  {"delim", (int)'d', "CHAR", 0,
   "Character used to separate keys from values (default '|')", 0},
  {"sync", 's', (const char *)NULL, 0,
   "Automatically synchronize all database operations to the disk", 0},
  {"no-lock", (int)'l', (const char *)NULL, 0,
   "Do not perform any locking of the database file", 0},
#ifdef HAVE_GDBM
  {"no-mmap", (int)'n', (const char *)NULL, 0,
   "Do not memory-map the database file", 0},
#endif
  {"verbose", (int)'v', (const char *)NULL, 0,
   "Print extra information.", 0},
  {(const char *)NULL, 0, (const char *)NULL, 0, (const char *)NULL, 0}
};

struct arguments
{
  char *args[1];





  char *input_file;
  const char *delim;
  bool sync;
  bool no_lock;
#ifdef HAVE_GDBM
  bool no_mmap;
#endif
  bool verbose;
};

static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
  struct arguments *arguments = state->input;


  switch (key)
    {











    case 'd':
      if (strlen (arg) > 1)
        argp_usage (state);
      arguments->delim = arg;
      break;
    case 'i':
      if (arguments->input_file)
................................................................................
          strncpy (arguments->input_file, arg, strlen (arg));
          arguments->input_file[strlen (arg)] = '\0';
        }
      break;
    case 'l':
      arguments->no_lock = true;
      break;
#ifdef HAVE_GDBM










    case 'n':
      arguments->no_mmap = true;
      break;
#endif
    case 's':
      arguments->sync = true;
      break;
    case 'v':
      arguments->verbose = true;
      break;
    case ARGP_KEY_ARG:
................................................................................
{
  FILE *input;
  struct arguments arguments;
  int mode = ZDB_WRITER;
  error_t argp_result;
  int result;

  arguments.verbose = false;

  arguments.sync = false;
  arguments.no_lock = false;
#ifdef HAVE_GDBM
  arguments.no_mmap = false;
#endif
  arguments.input_file = "-";
  arguments.delim = "|";

  argp_result = argp_parse (&argp, argc, argv, 0, 0, &arguments);
  if (argp_result)
    error (EXIT_FAILURE, argp_result, "Failed to parse arguments");

  if (signal (SIGINT, termination_handler) == SIG_IGN)
................................................................................
  if (signal (SIGTERM, termination_handler) == SIG_IGN)
    signal (SIGTERM, SIG_IGN);

  if (arguments.sync)
    mode ^= ZDB_SYNC;
  if (arguments.no_lock)
    mode ^= ZDB_NOLOCK;
#ifdef HAVE_GDBM
  if (arguments.no_mmap)
    mode ^= ZDB_NOMMAP;
#endif

  /* open the database */
  database = zdb_open (arguments.args[0], mode, arguments.verbose);

  if (!database)
    error (EXIT_FAILURE, errno,
           "Failed to open database %s", arguments.args[0]);

  /* if an input file was provided, use it, otherwise use stdin */
  if (!arguments.input_file || strcmp (arguments.input_file, "-") == 0)
    input = stdin;







>







 







>
>
>
>
>
>
>
>
>
>






<
<
<
<
<
<
<
<






>
>
>
>
>



<
<
<
<
<






<
>


>
>
>
>
>
>
>
>
>
>
>







 







|
>
>
>
>
>
>
>
>
>
>



<







 







|
>
|

<
|
|
|







 







<


<


|
>







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
...
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
...
242
243
244
245
246
247
248
249
250
251
252

253
254
255
256
257
258
259
260
261
262
...
266
267
268
269
270
271
272

273
274

275
276
277
278
279
280
281
282
283
284
285
#include <config.h>
#include <string.h>
#include <stdio.h>
#include "error.h"
#include "argp.h"
#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
#include "zdb.h"

const char *program_name = "zdbs";
const char *argp_program_version = PACKAGE_STRING;
const char *argp_program_bug_address = PACKAGE_BUGREPORT;
void *database;
static char doc[] =
................................................................................
  "zdbs -- a tool for storing records in a DBM database\v"
  "If an input file is provided, lines are read from it as new records, "
  "otherwise records are added from stdin. Records are expected in the "
  "format KEY|VALUE.";
static char args_doc[] = "DATABASE";

static struct argp_option options[] = {
  {"mmap-size",   (int)'m', "NUM", 0,
   "The size (in bytes) of the memory-mapped region to use (default=1024)", 0},
  {"cache-size", (int)'c', "NUM", 0,
   "The bucket cache size to use (default=128)", 0},
  {"no-mmap", (int)'n', (const char *)NULL, 0,
   "Do not memory-map the database file", 0},
  {"no-lock", (int)'l', (const char *)NULL, 0,
   "Do not perform any locking of the database file", 0},
  {"verbose", (int)'v', (const char *)NULL, 0,
   "Print extra information.", 0},
  {"input", (int)'i', "FILE", 0,
   "Read new records from a file", 0},
  {"delim", (int)'d', "CHAR", 0,
   "Character used to separate keys from values (default '|')", 0},
  {"sync", 's', (const char *)NULL, 0,
   "Automatically synchronize all database operations to the disk", 0},








  {(const char *)NULL, 0, (const char *)NULL, 0, (const char *)NULL, 0}
};

struct arguments
{
  char *args[1];
  size_t mmap_size;
  size_t cache_size;
  bool no_mmap;
  bool no_lock;
  bool verbose;
  char *input_file;
  const char *delim;
  bool sync;





};

static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
  struct arguments *arguments = state->input;

  unsigned long int size;
  switch (key)
    {
    case 'c':
      size = arg ? strtoul (arg, NULL, 10) : 128UL;
      if (size > SIZE_MAX)
        {
          arguments->cache_size = SIZE_MAX;
        }
      else
        {
          arguments->cache_size = (size_t)size;
        }
      break;
    case 'd':
      if (strlen (arg) > 1)
        argp_usage (state);
      arguments->delim = arg;
      break;
    case 'i':
      if (arguments->input_file)
................................................................................
          strncpy (arguments->input_file, arg, strlen (arg));
          arguments->input_file[strlen (arg)] = '\0';
        }
      break;
    case 'l':
      arguments->no_lock = true;
      break;
    case 'm':
      size = arg ? strtoul (arg, NULL, 10) : 131072UL;
      if (size > SIZE_MAX)
        {
          arguments->mmap_size = SIZE_MAX;
        }
      else
        {
          arguments->mmap_size = (size_t)size;
        }
      break;
    case 'n':
      arguments->no_mmap = true;
      break;

    case 's':
      arguments->sync = true;
      break;
    case 'v':
      arguments->verbose = true;
      break;
    case ARGP_KEY_ARG:
................................................................................
{
  FILE *input;
  struct arguments arguments;
  int mode = ZDB_WRITER;
  error_t argp_result;
  int result;

  arguments.mmap_size = 1024;
  arguments.cache_size = 128;
  arguments.no_mmap = false;
  arguments.no_lock = false;

  arguments.verbose = false;
  arguments.sync = false;
  arguments.input_file = NULL;
  arguments.delim = "|";

  argp_result = argp_parse (&argp, argc, argv, 0, 0, &arguments);
  if (argp_result)
    error (EXIT_FAILURE, argp_result, "Failed to parse arguments");

  if (signal (SIGINT, termination_handler) == SIG_IGN)
................................................................................
  if (signal (SIGTERM, termination_handler) == SIG_IGN)
    signal (SIGTERM, SIG_IGN);

  if (arguments.sync)
    mode ^= ZDB_SYNC;
  if (arguments.no_lock)
    mode ^= ZDB_NOLOCK;

  if (arguments.no_mmap)
    mode ^= ZDB_NOMMAP;


  /* open the database */
  database = zdb_open (arguments.args[0], mode, arguments.mmap_size,
                       arguments.cache_size, arguments.verbose);
  if (!database)
    error (EXIT_FAILURE, errno,
           "Failed to open database %s", arguments.args[0]);

  /* if an input file was provided, use it, otherwise use stdin */
  if (!arguments.input_file || strcmp (arguments.input_file, "-") == 0)
    input = stdin;

Changes to tests/system_tests.test.in.

1
2
3
4
5
6
7
8
9
10
11
..
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/bin/sh
# system_tests.test --- Run system tests on the zeptodb executables

# Copyright (C) 2013 Brandon Invergo <brandon@invergo.net>

# Author: Brandon Invergo <brandon@invergo.net>

# This program 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 3
# of the License, or (at your option) any later version.
................................................................................
        pass $1 "database created"
    else
        fail $1 "database creation process succeeded but file does not exist"
    fi
    rm ${TESTDB}
}

test_create_large_db () {
    "${ZDBC}" --num-buckets=1000000000000000000 \
        --mmap-size=1000000000000000000 ${TESTDB} || \
        fail $1 "large database creation failed"
    if [ -f ${TESTDB} ]; then
        pass $1 "large database creation succeeded"
    else
        fail $1 "large database creation succeeded but file does not exist"
    fi
    rm ${TESTDB}
    return $?
}

create_test_db () {
    "${ZDBC}" ${TESTDB}



|







 







|
|



|

|







1
2
3
4
5
6
7
8
9
10
11
..
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/bin/sh
# system_tests.test --- Run system tests on the zeptodb executables

# Copyright (C) 2013, 2016 Brandon Invergo <brandon@invergo.net>

# Author: Brandon Invergo <brandon@invergo.net>

# This program 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 3
# of the License, or (at your option) any later version.
................................................................................
        pass $1 "database created"
    else
        fail $1 "database creation process succeeded but file does not exist"
    fi
    rm ${TESTDB}
}

test_large_args () {
    "${ZDBC}" --cache-size=1000000000000000000 \
        --mmap-size=1000000000000000000 ${TESTDB} || \
        fail $1 "large database creation failed"
    if [ -f ${TESTDB} ]; then
        pass $1 "large argument usage succeeded"
    else
        fail $1 "large argument usage failed"
    fi
    rm ${TESTDB}
    return $?
}

create_test_db () {
    "${ZDBC}" ${TESTDB}