Skip to content

Commit b058ede

Browse files
committed
Check in v0.1 sources
0 parents  commit b058ede

19 files changed

+1593
-0
lines changed

LICENSE

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Licensed under the MIT license (same as Lua), reproduced below:
2+
---
3+
4+
Copyright (c) 2008 A.S. Bradbury
5+
6+
Permission is hereby granted, free of charge, to any person
7+
obtaining a copy of this software and associated documentation
8+
files (the "Software"), to deal in the Software without
9+
restriction, including without limitation the rights to use,
10+
copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the
12+
Software is furnished to do so, subject to the following
13+
conditions:
14+
15+
The above copyright notice and this permission notice shall be
16+
included in all copies or substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25+
OTHER DEALINGS IN THE SOFTWARE.

Makefile

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
VERSION= 0.1
2+
3+
# change these to reflect your Lua installation
4+
LUA= /usr
5+
LUAINC= $(LUA)/include
6+
LUALIB= $(LUA)/lib
7+
LUABIN= $(LUA)/bin
8+
9+
# probably no need to change anything below here
10+
CC= gcc
11+
CFLAGS= $(INCS) $(WARN) -O2
12+
WARN= -Wall
13+
INCS= -I$(LUAINC)
14+
15+
CDB_OBJS = cdb_init.o cdb_find.o cdb_findnext.o cdb_seq.o cdb_seek.o \
16+
cdb_unpack.o \
17+
cdb_make_add.o cdb_make_put.o cdb_make.o cdb_hash.o
18+
19+
OBJS= $(CDB_OBJS) lcdb.o
20+
SOS= cdb.so
21+
22+
all: $(SOS)
23+
24+
$(SOS): $(OBJS)
25+
$(CC) -o $@ -shared $(OBJS) $(LIBS)
26+
27+
.PHONY: clean test distr
28+
clean:
29+
rm -f $(OBJS) $(SOS) core core.* a.out
30+
31+
test: all
32+
./lunit test.lua
33+
34+
tar: clean
35+
git archive --format=tar --prefix=lua-tinycdb-$(VERSION)/ v$(VERSION) | gzip > lua-tinycdb-$(VERSION).tar.gz
36+

README

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Package: lua-tinycdb
2+
Version: 0.1
3+
Author: A.S. Bradbury
4+
Website: http://asbradbury.org/projects/lua-tinycdb/
5+
License: MIT/X11
6+
Description:
7+
A binding to the tinycdb[1] library by Michael Tokarev,
8+
which is a public domain implementation of Daniel J. Bernstein's Constant
9+
Database (cdb)[2]. A cdb is a key-value store (much like BDB/gdbm), but it
10+
cannot be updated at runtime (only rebuilt). Reading is very fast, and
11+
rebuilding is done atomically by building the new database and then
12+
renaming. Includes the tinycdb source so there are no external dependencies.
13+
14+
I include below the list of advantages of the cdb database structure, stolen
15+
(and slightly modified) from D.J. Bernstein's cdb page:
16+
* Fast lookups: A successful lookup in a large database normally takes
17+
just two disk accesses. An unsuccessful lookup takes only one.
18+
* Low overhead: A database uses 2048 bytes, plus 24 bytes per record, plus
19+
the space for keys and data.
20+
* Fast atomic database replacement
21+
22+
[1] = http://www.corpit.ru/mjt/tinycdb.html
23+
[2] = http://cr.yp.to/cdb.html
24+
25+
See also:
26+
Taj Khattra's luacdb (http://www3.telus.net/taj_khattra/luacdb.html)
27+
A lua interface using D.J. Bernstein's original cdb library. lua-tinycdb
28+
was developed independently.

cdb.h

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/* $Id: cdb.h,v 1.9 2006/06/28 15:19:19 mjt Exp $
2+
* public cdb include file
3+
*
4+
* This file is a part of tinycdb package by Michael Tokarev, [email protected].
5+
* Public domain.
6+
*/
7+
8+
#ifndef TINYCDB_VERSION
9+
#define TINYCDB_VERSION 0.76
10+
11+
#ifdef __cplusplus
12+
extern "C" {
13+
#endif
14+
15+
typedef unsigned int cdbi_t; /* compatibility */
16+
17+
/* common routines */
18+
unsigned cdb_hash(const void *buf, unsigned len);
19+
unsigned cdb_unpack(const unsigned char buf[4]);
20+
void cdb_pack(unsigned num, unsigned char buf[4]);
21+
22+
struct cdb {
23+
int cdb_fd; /* file descriptor */
24+
/* private members */
25+
unsigned cdb_fsize; /* datafile size */
26+
unsigned cdb_dend; /* end of data ptr */
27+
const unsigned char *cdb_mem; /* mmap'ed file memory */
28+
unsigned cdb_vpos, cdb_vlen; /* found data */
29+
unsigned cdb_kpos, cdb_klen; /* found key */
30+
};
31+
32+
#define CDB_STATIC_INIT {0,0,0,0,0,0,0,0}
33+
34+
#define cdb_datapos(c) ((c)->cdb_vpos)
35+
#define cdb_datalen(c) ((c)->cdb_vlen)
36+
#define cdb_keypos(c) ((c)->cdb_kpos)
37+
#define cdb_keylen(c) ((c)->cdb_klen)
38+
#define cdb_fileno(c) ((c)->cdb_fd)
39+
40+
int cdb_init(struct cdb *cdbp, int fd);
41+
void cdb_free(struct cdb *cdbp);
42+
43+
int cdb_read(const struct cdb *cdbp,
44+
void *buf, unsigned len, unsigned pos);
45+
#define cdb_readdata(cdbp, buf) \
46+
cdb_read((cdbp), (buf), cdb_datalen(cdbp), cdb_datapos(cdbp))
47+
#define cdb_readkey(cdbp, buf) \
48+
cdb_read((cdbp), (buf), cdb_keylen(cdbp), cdb_keypos(cdbp))
49+
50+
const void *cdb_get(const struct cdb *cdbp, unsigned len, unsigned pos);
51+
#define cdb_getdata(cdbp) \
52+
cdb_get((cdbp), cdb_datalen(cdbp), cdb_datapos(cdbp))
53+
#define cdb_getkey(cdbp) \
54+
cdb_get((cdbp), cdb_keylen(cdbp), cdb_keypos(cdbp))
55+
56+
int cdb_find(struct cdb *cdbp, const void *key, unsigned klen);
57+
58+
struct cdb_find {
59+
struct cdb *cdb_cdbp;
60+
unsigned cdb_hval;
61+
const unsigned char *cdb_htp, *cdb_htab, *cdb_htend;
62+
unsigned cdb_httodo;
63+
const void *cdb_key;
64+
unsigned cdb_klen;
65+
};
66+
67+
int cdb_findinit(struct cdb_find *cdbfp, struct cdb *cdbp,
68+
const void *key, unsigned klen);
69+
int cdb_findnext(struct cdb_find *cdbfp);
70+
71+
#define cdb_seqinit(cptr, cdbp) ((*(cptr))=2048)
72+
int cdb_seqnext(unsigned *cptr, struct cdb *cdbp);
73+
74+
/* old simple interface */
75+
/* open file using standard routine, then: */
76+
int cdb_seek(int fd, const void *key, unsigned klen, unsigned *dlenp);
77+
int cdb_bread(int fd, void *buf, int len);
78+
79+
/* cdb_make */
80+
81+
struct cdb_make {
82+
int cdb_fd; /* file descriptor */
83+
/* private */
84+
unsigned cdb_dpos; /* data position so far */
85+
unsigned cdb_rcnt; /* record count so far */
86+
unsigned char cdb_buf[4096]; /* write buffer */
87+
unsigned char *cdb_bpos; /* current buf position */
88+
struct cdb_rl *cdb_rec[256]; /* list of arrays of record infos */
89+
};
90+
91+
enum cdb_put_mode {
92+
CDB_PUT_ADD = 0, /* add unconditionnaly, like cdb_make_add() */
93+
#define CDB_PUT_ADD CDB_PUT_ADD
94+
CDB_FIND = CDB_PUT_ADD,
95+
CDB_PUT_REPLACE, /* replace: do not place to index OLD record */
96+
#define CDB_PUT_REPLACE CDB_PUT_REPLACE
97+
CDB_FIND_REMOVE = CDB_PUT_REPLACE,
98+
CDB_PUT_INSERT, /* add only if not already exists */
99+
#define CDB_PUT_INSERT CDB_PUT_INSERT
100+
CDB_PUT_WARN, /* add unconditionally but ret. 1 if exists */
101+
#define CDB_PUT_WARN CDB_PUT_WARN
102+
CDB_PUT_REPLACE0, /* if a record exists, fill old one with zeros */
103+
#define CDB_PUT_REPLACE0 CDB_PUT_REPLACE0
104+
CDB_FIND_FILL0 = CDB_PUT_REPLACE0
105+
};
106+
107+
int cdb_make_start(struct cdb_make *cdbmp, int fd);
108+
int cdb_make_add(struct cdb_make *cdbmp,
109+
const void *key, unsigned klen,
110+
const void *val, unsigned vlen);
111+
int cdb_make_exists(struct cdb_make *cdbmp,
112+
const void *key, unsigned klen);
113+
int cdb_make_find(struct cdb_make *cdbmp,
114+
const void *key, unsigned klen,
115+
enum cdb_put_mode mode);
116+
int cdb_make_put(struct cdb_make *cdbmp,
117+
const void *key, unsigned klen,
118+
const void *val, unsigned vlen,
119+
enum cdb_put_mode mode);
120+
int cdb_make_finish(struct cdb_make *cdbmp);
121+
122+
/* Exposed for lua-tinycdb */
123+
void cdb_make_free(struct cdb_make *cdbmp);
124+
125+
#ifdef __cplusplus
126+
} /* extern "C" */
127+
#endif
128+
129+
#endif /* include guard */

cdb.txt

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
A structure for constant databases
2+
19960914
3+
Copyright 1996
4+
D. J. Bernstein, [email protected]
5+
6+
A cdb is an associative array: it maps strings (``keys'') to strings
7+
(``data'').
8+
9+
A cdb contains 256 pointers to linearly probed open hash tables. The
10+
hash tables contain pointers to (key,data) pairs. A cdb is stored in
11+
a single file on disk:
12+
13+
+----------------+---------+-------+-------+-----+---------+
14+
| p0 p1 ... p255 | records | hash0 | hash1 | ... | hash255 |
15+
+----------------+---------+-------+-------+-----+---------+
16+
17+
Each of the 256 initial pointers states a position and a length. The
18+
position is the starting byte position of the hash table. The length
19+
is the number of slots in the hash table.
20+
21+
Records are stored sequentially, without special alignment. A record
22+
states a key length, a data length, the key, and the data.
23+
24+
Each hash table slot states a hash value and a byte position. If the
25+
byte position is 0, the slot is empty. Otherwise, the slot points to
26+
a record whose key has that hash value.
27+
28+
Positions, lengths, and hash values are 32-bit quantities, stored in
29+
little-endian form in 4 bytes. Thus a cdb must fit into 4 gigabytes.
30+
31+
A record is located as follows. Compute the hash value of the key in
32+
the record. The hash value modulo 256 is the number of a hash table.
33+
The hash value divided by 256, modulo the length of that table, is a
34+
slot number. Probe that slot, the next higher slot, and so on, until
35+
you find the record or run into an empty slot.
36+
37+
The cdb hash function is ``h = ((h << 5) + h) ^ c'', with a starting
38+
hash of 5381.

cdb_find.c

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/* $Id: cdb_find.c,v 1.8 2003/11/03 16:42:41 mjt Exp $
2+
* cdb_find routine
3+
*
4+
* This file is a part of tinycdb package by Michael Tokarev, [email protected].
5+
* Public domain.
6+
*/
7+
8+
#include "cdb_int.h"
9+
10+
int
11+
cdb_find(struct cdb *cdbp, const void *key, unsigned klen)
12+
{
13+
const unsigned char *htp; /* hash table pointer */
14+
const unsigned char *htab; /* hash table */
15+
const unsigned char *htend; /* end of hash table */
16+
unsigned httodo; /* ht bytes left to look */
17+
unsigned pos, n;
18+
19+
unsigned hval;
20+
21+
if (klen >= cdbp->cdb_dend) /* if key size is too large */
22+
return 0;
23+
24+
hval = cdb_hash(key, klen);
25+
26+
/* find (pos,n) hash table to use */
27+
/* first 2048 bytes (toc) are always available */
28+
/* (hval % 256) * 8 */
29+
htp = cdbp->cdb_mem + ((hval << 3) & 2047); /* index in toc (256x8) */
30+
n = cdb_unpack(htp + 4); /* table size */
31+
if (!n) /* empty table */
32+
return 0; /* not found */
33+
httodo = n << 3; /* bytes of htab to lookup */
34+
pos = cdb_unpack(htp); /* htab position */
35+
if (n > (cdbp->cdb_fsize >> 3) /* overflow of httodo ? */
36+
|| pos < cdbp->cdb_dend /* is htab inside data section ? */
37+
|| pos > cdbp->cdb_fsize /* htab start within file ? */
38+
|| httodo > cdbp->cdb_fsize - pos) /* entrie htab within file ? */
39+
return errno = EPROTO, -1;
40+
41+
htab = cdbp->cdb_mem + pos; /* htab pointer */
42+
htend = htab + httodo; /* after end of htab */
43+
/* htab starting position: rest of hval modulo htsize, 8bytes per elt */
44+
htp = htab + (((hval >> 8) % n) << 3);
45+
46+
for(;;) {
47+
pos = cdb_unpack(htp + 4); /* record position */
48+
if (!pos)
49+
return 0;
50+
if (cdb_unpack(htp) == hval) {
51+
if (pos > cdbp->cdb_dend - 8) /* key+val lengths */
52+
return errno = EPROTO, -1;
53+
if (cdb_unpack(cdbp->cdb_mem + pos) == klen) {
54+
if (cdbp->cdb_dend - klen < pos + 8)
55+
return errno = EPROTO, -1;
56+
if (memcmp(key, cdbp->cdb_mem + pos + 8, klen) == 0) {
57+
n = cdb_unpack(cdbp->cdb_mem + pos + 4);
58+
pos += 8;
59+
if (cdbp->cdb_dend < n || cdbp->cdb_dend - n < pos + klen)
60+
return errno = EPROTO, -1;
61+
cdbp->cdb_kpos = pos;
62+
cdbp->cdb_klen = klen;
63+
cdbp->cdb_vpos = pos + klen;
64+
cdbp->cdb_vlen = n;
65+
return 1;
66+
}
67+
}
68+
}
69+
httodo -= 8;
70+
if (!httodo)
71+
return 0;
72+
if ((htp += 8) >= htend)
73+
htp = htab;
74+
}
75+
76+
}

0 commit comments

Comments
 (0)