From fcbf63e62c627deae76c1b8cb8c0876c536ed811 Mon Sep 17 00:00:00 2001 From: Jari Vetoniemi Date: Mon, 16 Mar 2020 18:49:26 +0900 Subject: Fresh start --- jni/ruby/ext/sdbm/Makefile | 288 ++++++++++++ jni/ruby/ext/sdbm/_sdbm.c | 952 +++++++++++++++++++++++++++++++++++++ jni/ruby/ext/sdbm/depend | 19 + jni/ruby/ext/sdbm/extconf.h | 4 + jni/ruby/ext/sdbm/extconf.rb | 4 + jni/ruby/ext/sdbm/init.c | 1065 ++++++++++++++++++++++++++++++++++++++++++ jni/ruby/ext/sdbm/sdbm.h | 86 ++++ 7 files changed, 2418 insertions(+) create mode 100644 jni/ruby/ext/sdbm/Makefile create mode 100644 jni/ruby/ext/sdbm/_sdbm.c create mode 100644 jni/ruby/ext/sdbm/depend create mode 100644 jni/ruby/ext/sdbm/extconf.h create mode 100644 jni/ruby/ext/sdbm/extconf.rb create mode 100644 jni/ruby/ext/sdbm/init.c create mode 100644 jni/ruby/ext/sdbm/sdbm.h (limited to 'jni/ruby/ext/sdbm') diff --git a/jni/ruby/ext/sdbm/Makefile b/jni/ruby/ext/sdbm/Makefile new file mode 100644 index 0000000..16cc9d2 --- /dev/null +++ b/jni/ruby/ext/sdbm/Makefile @@ -0,0 +1,288 @@ + +SHELL = /bin/sh + +# V=0 quiet, V=1 verbose. other values don't work. +V = 0 +Q1 = $(V:1=) +Q = $(Q1:0=@) +ECHO1 = $(V:1=@:) +ECHO = $(ECHO1:0=@echo) +NULLCMD = : + +#### Start of system configuration section. #### +top_srcdir = $(topdir)/. +srcdir = $(top_srcdir)/ext/sdbm +topdir = ../.. +hdrdir = $(top_srcdir)/include +arch_hdrdir = $(extout)/include/$(arch) +PATH_SEPARATOR = : +VPATH = $(srcdir):$(arch_hdrdir)/ruby:$(hdrdir)/ruby +RUBYLIB = +RUBYOPT = - +prefix = $(DESTDIR)/usr/local +rubysitearchprefix = $(rubylibprefix)/$(sitearch) +rubyarchprefix = $(rubylibprefix)/$(arch) +rubylibprefix = $(libdir)/$(RUBY_BASE_NAME) +exec_prefix = $(prefix) +vendorarchhdrdir = $(vendorhdrdir)/$(sitearch) +sitearchhdrdir = $(sitehdrdir)/$(sitearch) +rubyarchhdrdir = $(rubyhdrdir)/$(arch) +vendorhdrdir = $(rubyhdrdir)/vendor_ruby +sitehdrdir = $(rubyhdrdir)/site_ruby +rubyhdrdir = $(includedir)/$(RUBY_VERSION_NAME) +vendorarchdir = $(vendorlibdir)/$(sitearch) +vendorlibdir = $(vendordir)/$(ruby_version) +vendordir = $(rubylibprefix)/vendor_ruby +sitearchdir = $(sitelibdir)/$(sitearch) +sitelibdir = $(sitedir)/$(ruby_version) +sitedir = $(rubylibprefix)/site_ruby +rubyarchdir = $(rubylibdir)/$(arch) +rubylibdir = $(rubylibprefix)/$(ruby_version) +sitearchincludedir = $(includedir)/$(sitearch) +archincludedir = $(includedir)/$(arch) +sitearchlibdir = $(libdir)/$(sitearch) +archlibdir = $(libdir)/$(arch) +ridir = $(datarootdir)/$(RI_BASE_NAME) +mandir = $(datarootdir)/man +localedir = $(datarootdir)/locale +libdir = $(exec_prefix)/lib +psdir = $(docdir) +pdfdir = $(docdir) +dvidir = $(docdir) +htmldir = $(docdir) +infodir = $(datarootdir)/info +docdir = $(datarootdir)/doc/$(PACKAGE) +oldincludedir = $(DESTDIR)/usr/include +includedir = $(prefix)/include +localstatedir = $(prefix)/var +sharedstatedir = $(prefix)/com +sysconfdir = $(prefix)/etc +datadir = $(datarootdir) +datarootdir = $(prefix)/share +libexecdir = $(exec_prefix)/libexec +sbindir = $(exec_prefix)/sbin +bindir = $(exec_prefix)/bin +archdir = $(rubyarchdir) + + +CC = gcc +CXX = g++ +LIBRUBY = $(LIBRUBY_SO) +LIBRUBY_A = lib$(RUBY_SO_NAME)-static.a +LIBRUBYARG_SHARED = -Wl,-R$(libdir) -L$(libdir) -l$(RUBY_SO_NAME) +LIBRUBYARG_STATIC = -Wl,-R$(libdir) -L$(libdir) -l$(RUBY_SO_NAME)-static +empty = +OUTFLAG = -o $(empty) +COUTFLAG = -o $(empty) + +RUBY_EXTCONF_H = extconf.h +cflags = $(optflags) $(debugflags) $(warnflags) +optflags = -O3 -fno-fast-math +debugflags = -ggdb3 +warnflags = -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wunused-variable -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wimplicit-function-declaration -Wdeprecated-declarations -Wno-packed-bitfield-compat +CCDLFLAGS = -fPIC +CFLAGS = $(CCDLFLAGS) $(cflags) -fPIC $(ARCH_FLAG) +INCFLAGS = -I. -I$(arch_hdrdir) -I$(hdrdir) -I$(srcdir) +DEFS = +CPPFLAGS = -DRUBY_EXTCONF_H=\"$(RUBY_EXTCONF_H)\" $(DEFS) $(cppflags) +CXXFLAGS = $(CCDLFLAGS) $(cxxflags) $(ARCH_FLAG) +ldflags = -L. -fstack-protector -rdynamic -Wl,-export-dynamic +dldflags = +ARCH_FLAG = +DLDFLAGS = $(ldflags) $(dldflags) $(ARCH_FLAG) +LDSHARED = $(CC) -shared +LDSHAREDXX = $(CXX) -shared +AR = ar +EXEEXT = + +RUBY_INSTALL_NAME = $(RUBY_BASE_NAME) +RUBY_SO_NAME = ruby +RUBYW_INSTALL_NAME = +RUBY_VERSION_NAME = $(RUBY_BASE_NAME)-$(ruby_version) +RUBYW_BASE_NAME = rubyw +RUBY_BASE_NAME = ruby + +arch = x86_64-linux +sitearch = $(arch) +ruby_version = 2.2.0 +ruby = $(topdir)/miniruby -I'$(topdir)' -I'$(top_srcdir)/lib' -I'$(extout)/$(arch)' -I'$(extout)/common' +RUBY = $(ruby) +ruby_headers = $(hdrdir)/ruby.h $(hdrdir)/ruby/ruby.h $(hdrdir)/ruby/defines.h $(hdrdir)/ruby/missing.h $(hdrdir)/ruby/intern.h $(hdrdir)/ruby/st.h $(hdrdir)/ruby/subst.h $(arch_hdrdir)/ruby/config.h $(RUBY_EXTCONF_H) + +RM = rm -f +RM_RF = $(RUBY) -run -e rm -- -rf +RMDIRS = rmdir --ignore-fail-on-non-empty -p +MAKEDIRS = /bin/mkdir -p +INSTALL = /usr/bin/install -c +INSTALL_PROG = $(INSTALL) -m 0755 +INSTALL_DATA = $(INSTALL) -m 644 +COPY = cp +TOUCH = exit > + +#### End of system configuration section. #### + +preload = + +libpath = . $(topdir) +LIBPATH = -L. -L$(topdir) +DEFFILE = + +CLEANFILES = mkmf.log +DISTCLEANFILES = +DISTCLEANDIRS = + +extout = $(topdir)/.ext +extout_prefix = $(extout)$(target_prefix)/ +target_prefix = +LOCAL_LIBS = +LIBS = $(LIBRUBYARG_SHARED) -lpthread -lgmp -ldl -lcrypt -lm -lc +ORIG_SRCS = _sdbm.c init.c +SRCS = $(ORIG_SRCS) +OBJS = _sdbm.o init.o +HDRS = $(srcdir)/sdbm.h $(srcdir)/extconf.h +TARGET = sdbm +TARGET_NAME = sdbm +TARGET_ENTRY = Init_$(TARGET_NAME) +DLLIB = $(TARGET).so +EXTSTATIC = +STATIC_LIB = $(TARGET).a + +TIMESTAMP_DIR = $(extout)/.timestamp +BINDIR = $(extout)/bin +RUBYCOMMONDIR = $(extout)/common +RUBYLIBDIR = $(RUBYCOMMONDIR)$(target_prefix) +RUBYARCHDIR = $(extout)/$(arch)$(target_prefix) +HDRDIR = $(extout)/include/ruby$(target_prefix) +ARCHHDRDIR = $(extout)/include/$(arch)/ruby$(target_prefix) + +TARGET_SO = $(RUBYARCHDIR)/$(DLLIB) +CLEANLIBS = $(RUBYARCHDIR)/$(TARGET).so +CLEANOBJS = *.o *.bak + +all: install +static: all +.PHONY: all install static install-so install-rb +.PHONY: clean clean-so clean-static clean-rb + +clean-static:: +clean-rb-default:: +clean-rb:: +clean-so:: +clean: clean-so clean-static clean-rb-default clean-rb + -$(Q)$(RM) $(CLEANLIBS) $(CLEANOBJS) $(CLEANFILES) .*.time + +distclean-rb-default:: +distclean-rb:: +distclean-so:: +distclean-static:: +distclean: clean distclean-so distclean-static distclean-rb-default distclean-rb + -$(Q)$(RM) Makefile $(RUBY_EXTCONF_H) conftest.* mkmf.log + -$(Q)$(RM) core ruby$(EXEEXT) *~ $(DISTCLEANFILES) + -$(Q)$(RMDIRS) $(DISTCLEANDIRS) 2> /dev/null || true + +realclean: distclean +install: install-so install-rb + +install-so: $(RUBYARCHDIR)/$(DLLIB) +clean-so:: + -$(Q)$(RM) $(RUBYARCHDIR)/$(DLLIB) + -$(Q)$(RMDIRS) $(RUBYARCHDIR) 2> /dev/null || true +clean-static:: + -$(Q)$(RM) $(STATIC_LIB) +install-rb: pre-install-rb install-rb-default +install-rb-default: pre-install-rb-default +pre-install-rb: Makefile +pre-install-rb-default: Makefile +pre-install-rb-default: + @$(NULLCMD) +$(TIMESTAMP_DIR)/.RUBYARCHDIR.time: + $(Q) $(MAKEDIRS) $(@D) $(RUBYARCHDIR) + $(Q) $(TOUCH) $@ + +site-install: site-install-so site-install-rb +site-install-so: install-so +site-install-rb: install-rb + +.SUFFIXES: .c .m .cc .mm .cxx .cpp .o .S + +.cc.o: + $(ECHO) compiling $(<) + $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $< + +.cc.S: + $(ECHO) translating $(<) + $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $< + +.mm.o: + $(ECHO) compiling $(<) + $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $< + +.mm.S: + $(ECHO) translating $(<) + $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $< + +.cxx.o: + $(ECHO) compiling $(<) + $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $< + +.cxx.S: + $(ECHO) translating $(<) + $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $< + +.cpp.o: + $(ECHO) compiling $(<) + $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $< + +.cpp.S: + $(ECHO) translating $(<) + $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $< + +.c.o: + $(ECHO) compiling $(<) + $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $< + +.c.S: + $(ECHO) translating $(<) + $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -S $< + +.m.o: + $(ECHO) compiling $(<) + $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $< + +.m.S: + $(ECHO) translating $(<) + $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -S $< + +$(RUBYARCHDIR)/$(DLLIB): $(OBJS) Makefile $(TIMESTAMP_DIR)/.RUBYARCHDIR.time + $(ECHO) linking shared-object $(DLLIB) + -$(Q)$(RM) $(@) + $(Q) $(LDSHARED) -o $@ $(OBJS) $(LIBPATH) $(DLDFLAGS) $(LOCAL_LIBS) $(LIBS) + +$(STATIC_LIB): $(OBJS) + -$(Q)$(RM) $(@) + $(ECHO) linking static-library $(@) + $(Q) $(AR) cru $@ $(OBJS) + -$(Q)ranlib $(@) 2> /dev/null || true + +### +$(OBJS): $(RUBY_EXTCONF_H) + +# AUTOGENERATED DEPENDENCIES START +_sdbm.o: $(RUBY_EXTCONF_H) +_sdbm.o: $(arch_hdrdir)/ruby/config.h +_sdbm.o: $(hdrdir)/ruby/defines.h +_sdbm.o: $(hdrdir)/ruby/missing.h +_sdbm.o: _sdbm.c +_sdbm.o: sdbm.h +init.o: $(RUBY_EXTCONF_H) +init.o: $(arch_hdrdir)/ruby/config.h +init.o: $(hdrdir)/ruby/defines.h +init.o: $(hdrdir)/ruby/intern.h +init.o: $(hdrdir)/ruby/missing.h +init.o: $(hdrdir)/ruby/ruby.h +init.o: $(hdrdir)/ruby/st.h +init.o: $(hdrdir)/ruby/subst.h +init.o: $(top_srcdir)/include/ruby.h +init.o: init.c +init.o: sdbm.h +# AUTOGENERATED DEPENDENCIES END diff --git a/jni/ruby/ext/sdbm/_sdbm.c b/jni/ruby/ext/sdbm/_sdbm.c new file mode 100644 index 0000000..d810bef --- /dev/null +++ b/jni/ruby/ext/sdbm/_sdbm.c @@ -0,0 +1,952 @@ +/* + * sdbm - ndbm work-alike hashed database library + * based on Per-Aake Larson's Dynamic Hashing algorithms. BIT 18 (1978). + * author: oz@nexus.yorku.ca + * status: public domain. + * + * core routines + */ + +#include "ruby/ruby.h" + +#ifdef HAVE_UNISTD_H +#include +#endif + +#include "sdbm.h" + +/* + * sdbm - ndbm work-alike hashed database library + * tuning and portability constructs [not nearly enough] + * author: oz@nexus.yorku.ca + */ + +#define BYTESIZ 8 + +#ifdef BSD42 +#define SEEK_SET L_SET +#define memset(s,c,n) bzero((s), (n)) /* only when c is zero */ +#define memcpy(s1,s2,n) bcopy((s2), (s1), (n)) +#define memcmp(s1,s2,n) bcmp((s1),(s2),(n)) +#endif + +/* + * important tuning parms (hah) + */ + +#ifndef SEEDUPS +#define SEEDUPS 1 /* always detect duplicates */ +#endif +#ifndef BADMESS +#define BADMESS 1 /* generate a message for worst case: + cannot make room after SPLTMAX splits */ +#endif + +/* + * misc + */ +#ifdef DEBUG +#define debug(x) printf x +#else +#define debug(x) +#endif + +#ifdef BIG_E +#define GET_SHORT(p, i) (((unsigned)((unsigned char *)(p))[(i)*2] << 8) + (((unsigned char *)(p))[(i)*2 + 1])) +#define PUT_SHORT(p, i, s) (((unsigned char *)(p))[(i)*2] = (unsigned char)((s) >> 8), ((unsigned char *)(p))[(i)*2 + 1] = (unsigned char)(s)) +#else +#define GET_SHORT(p, i) ((p)[(i)]) +#define PUT_SHORT(p, i, s) ((p)[(i)] = (s)) +#endif + +/*#include "pair.h"*/ +static int fitpair proto((char *, int)); +static void putpair proto((char *, datum, datum)); +static datum getpair proto((char *, datum)); +static int delpair proto((char *, datum)); +static int chkpage proto((char *)); +static datum getnkey proto((char *, int)); +static void splpage proto((char *, char *, long)); +#if SEEDUPS +static int duppair proto((char *, datum)); +#endif + +#include +#include +#ifdef DOSISH +#include +#endif +#include +#include +#ifdef BSD42 +#include +#else +#include +/*#include */ +#endif +#ifndef O_BINARY +#define O_BINARY 0 +#endif + +#include +#ifndef EPERM +#define EPERM EACCES +#endif +#include + +#ifdef __STDC__ +#include +#endif + +#ifndef NULL +#define NULL 0 +#endif + +/* + * externals + */ +#if !defined(__sun) && !defined(_WIN32) && !defined(__CYGWIN__) && !defined(errno) +extern int errno; +#endif + +/* + * forward + */ +static int getdbit proto((DBM *, long)); +static int setdbit proto((DBM *, long)); +static int getpage proto((DBM *, long)); +static datum getnext proto((DBM *)); +static int makroom proto((DBM *, long, int)); + +/* + * useful macros + */ +#define bad(x) ((x).dptr == NULL || (x).dsize < 0) +#define exhash(item) sdbm_hash((item).dptr, (item).dsize) +#define ioerr(db) ((db)->flags |= DBM_IOERR) + +#define OFF_PAG(off) (long) (off) * PBLKSIZ +#define OFF_DIR(off) (long) (off) * DBLKSIZ + +static long masks[] = { + 000000000000L, 000000000001L, 000000000003L, + 000000000007L, 000000000017L, 000000000037L, + 000000000077L, 000000000177L, 000000000377L, + 000000000777L, 000000001777L, 000000003777L, + 000000007777L, 000000017777L, 000000037777L, + 000000077777L, 000000177777L, 000000377777L, + 000000777777L, 000001777777L, 000003777777L, + 000007777777L, 000017777777L, 000037777777L, + 000077777777L, 000177777777L, 000377777777L, + 000777777777L, 001777777777L, 003777777777L, + 007777777777L, 017777777777L +}; + +datum nullitem = {NULL, 0}; + +DBM * +sdbm_open(register char *file, register int flags, register int mode) +{ + register DBM *db; + register char *dirname; + register char *pagname; + register size_t n; + + if (file == NULL || !*file) + return errno = EINVAL, (DBM *) NULL; +/* + * need space for two separate filenames + */ + n = strlen(file) * 2 + strlen(DIRFEXT) + strlen(PAGFEXT) + 2; + + if ((dirname = malloc(n)) == NULL) + return errno = ENOMEM, (DBM *) NULL; +/* + * build the file names + */ + dirname = strcat(strcpy(dirname, file), DIRFEXT); + pagname = strcpy(dirname + strlen(dirname) + 1, file); + pagname = strcat(pagname, PAGFEXT); + + db = sdbm_prep(dirname, pagname, flags, mode); + free((char *) dirname); + return db; +} + +static int +fd_set_cloexec(int fd) +{ + /* MinGW don't have F_GETFD and FD_CLOEXEC. [ruby-core:40281] */ +#ifdef F_GETFD + int flags, ret; + flags = fcntl(fd, F_GETFD); /* should not fail except EBADF. */ + if (flags == -1) { + return -1; + } + if (2 < fd) { + if (!(flags & FD_CLOEXEC)) { + flags |= FD_CLOEXEC; + ret = fcntl(fd, F_SETFD, flags); + if (ret == -1) { + return -1; + } + } + } +#endif + return 0; +} + +DBM * +sdbm_prep(char *dirname, char *pagname, int flags, int mode) +{ + register DBM *db; + struct stat dstat; + + if ((db = (DBM *) malloc(sizeof(DBM))) == NULL) + return errno = ENOMEM, (DBM *) NULL; + + db->pagf = -1; + db->dirf = -1; + db->flags = 0; + db->hmask = 0; + db->blkptr = 0; + db->keyptr = 0; +/* + * adjust user flags so that WRONLY becomes RDWR, + * as required by this package. Also set our internal + * flag for RDONLY. + */ + if (flags & O_WRONLY) + flags = (flags & ~O_WRONLY) | O_RDWR; + if (flags & O_RDONLY) + db->flags = DBM_RDONLY; +/* + * open the files in sequence, and stat the dirfile. + * If we fail anywhere, undo everything, return NULL. + */ + flags |= O_BINARY; +#ifdef O_CLOEXEC + flags |= O_CLOEXEC; +#endif + + if ((db->pagf = open(pagname, flags, mode)) == -1) goto err; + if (fd_set_cloexec(db->pagf) == -1) goto err; + if ((db->dirf = open(dirname, flags, mode)) == -1) goto err; + if (fd_set_cloexec(db->dirf) == -1) goto err; +/* + * need the dirfile size to establish max bit number. + */ + if (fstat(db->dirf, &dstat) == -1) goto err; +/* + * zero size: either a fresh database, or one with a single, + * unsplit data page: dirpage is all zeros. + */ + db->dirbno = (!dstat.st_size) ? 0 : -1; + db->pagbno = -1; + db->maxbno = dstat.st_size * (long) BYTESIZ; + + (void) memset(db->pagbuf, 0, PBLKSIZ); + (void) memset(db->dirbuf, 0, DBLKSIZ); +/* + * success + */ + return db; + + err: + if (db->pagf != -1) + (void) close(db->pagf); + if (db->dirf != -1) + (void) close(db->dirf); + free((char *) db); + return (DBM *) NULL; +} + +void +sdbm_close(register DBM *db) +{ + if (db == NULL) + errno = EINVAL; + else { + (void) close(db->dirf); + (void) close(db->pagf); + free((char *) db); + } +} + +datum +sdbm_fetch(register DBM *db, datum key) +{ + if (db == NULL || bad(key)) + return errno = EINVAL, nullitem; + + if (getpage(db, exhash(key))) + return getpair(db->pagbuf, key); + + return ioerr(db), nullitem; +} + +int +sdbm_delete(register DBM *db, datum key) +{ + if (db == NULL || bad(key)) + return errno = EINVAL, -1; + if (sdbm_rdonly(db)) + return errno = EPERM, -1; + + if (getpage(db, exhash(key))) { + if (!delpair(db->pagbuf, key)) + return -1; +/* + * update the page file + */ + if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0 + || write(db->pagf, db->pagbuf, PBLKSIZ) < 0) + return ioerr(db), -1; + + return 0; + } + + return ioerr(db), -1; +} + +int +sdbm_store(register DBM *db, datum key, datum val, int flags) +{ + int need; + register long hash; + + if (db == NULL || bad(key)) + return errno = EINVAL, -1; + if (sdbm_rdonly(db)) + return errno = EPERM, -1; + + need = key.dsize + val.dsize; +/* + * is the pair too big (or too small) for this database ?? + */ + if (need < 0 || need > PAIRMAX) + return errno = EINVAL, -1; + + if (getpage(db, (hash = exhash(key)))) { +/* + * if we need to replace, delete the key/data pair + * first. If it is not there, ignore. + */ + if (flags == DBM_REPLACE) + (void) delpair(db->pagbuf, key); +#if SEEDUPS + else if (duppair(db->pagbuf, key)) + return 1; +#endif +/* + * if we do not have enough room, we have to split. + */ + if (!fitpair(db->pagbuf, need)) + if (!makroom(db, hash, need)) + return ioerr(db), -1; +/* + * we have enough room or split is successful. insert the key, + * and update the page file. + */ + (void) putpair(db->pagbuf, key, val); + + if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0 + || write(db->pagf, db->pagbuf, PBLKSIZ) < 0) + return ioerr(db), -1; + /* + * success + */ + return 0; + } + + return ioerr(db), -1; +} + +/* + * makroom - make room by splitting the overfull page + * this routine will attempt to make room for SPLTMAX times before + * giving up. + */ +static int +makroom(register DBM *db, long int hash, int need) +{ + long newp; + char twin[PBLKSIZ]; +#if defined _WIN32 && !defined __CYGWIN__ + char zer[PBLKSIZ]; + long oldtail; +#endif + char *pag = db->pagbuf; + char *new = twin; + register int smax = SPLTMAX; + + do { +/* + * split the current page + */ + (void) splpage(pag, new, db->hmask + 1); +/* + * address of the new page + */ + newp = (hash & db->hmask) | (db->hmask + 1); + debug(("newp: %ld\n", newp)); +/* + * write delay, read avoidance/cache shuffle: + * select the page for incoming pair: if key is to go to the new page, + * write out the previous one, and copy the new one over, thus making + * it the current page. If not, simply write the new page, and we are + * still looking at the page of interest. current page is not updated + * here, as sdbm_store will do so, after it inserts the incoming pair. + */ + +#if defined _WIN32 && !defined __CYGWIN__ + /* + * Fill hole with 0 if made it. + * (hole is NOT read as 0) + */ + oldtail = lseek(db->pagf, 0L, SEEK_END); + memset(zer, 0, PBLKSIZ); + while (OFF_PAG(newp) > oldtail) { + if (lseek(db->pagf, 0L, SEEK_END) < 0 || + write(db->pagf, zer, PBLKSIZ) < 0) { + + return 0; + } + oldtail += PBLKSIZ; + } +#endif + + if (hash & (db->hmask + 1)) { + if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0 + || write(db->pagf, db->pagbuf, PBLKSIZ) < 0) + return 0; + db->pagbno = newp; + (void) memcpy(pag, new, PBLKSIZ); + } + else if (lseek(db->pagf, OFF_PAG(newp), SEEK_SET) < 0 + || write(db->pagf, new, PBLKSIZ) < 0) + return 0; + + if (!setdbit(db, db->curbit)) + return 0; +/* + * see if we have enough room now + */ + if (fitpair(pag, need)) + return 1; +/* + * try again... update curbit and hmask as getpage would have + * done. because of our update of the current page, we do not + * need to read in anything. BUT we have to write the current + * [deferred] page out, as the window of failure is too great. + */ + db->curbit = 2 * db->curbit + + ((hash & (db->hmask + 1)) ? 2 : 1); + db->hmask |= (db->hmask + 1); + + if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0 + || write(db->pagf, db->pagbuf, PBLKSIZ) < 0) + return 0; + + } while (--smax); +/* + * if we are here, this is real bad news. After SPLTMAX splits, + * we still cannot fit the key. say goodnight. + */ +#if BADMESS + (void) (write(2, "sdbm: cannot insert after SPLTMAX attempts.\n", 44) < 0); +#endif + return 0; + +} + +/* + * the following two routines will break if + * deletions aren't taken into account. (ndbm bug) + */ +datum +sdbm_firstkey(register DBM *db) +{ + if (db == NULL) + return errno = EINVAL, nullitem; +/* + * start at page 0 + */ + (void) memset(db->pagbuf, 0, PBLKSIZ); + if (lseek(db->pagf, OFF_PAG(0), SEEK_SET) < 0 + || read(db->pagf, db->pagbuf, PBLKSIZ) < 0) + return ioerr(db), nullitem; + db->pagbno = 0; + db->blkptr = 0; + db->keyptr = 0; + + return getnext(db); +} + +datum +sdbm_nextkey(register DBM *db) +{ + if (db == NULL) + return errno = EINVAL, nullitem; + return getnext(db); +} + +/* + * all important binary trie traversal + */ +static int +getpage(register DBM *db, register long int hash) +{ + register int hbit; + register long dbit; + register long pagb; + + dbit = 0; + hbit = 0; + while (dbit < db->maxbno && getdbit(db, dbit)) + dbit = 2 * dbit + ((hash & ((long) 1 << hbit++)) ? 2 : 1); + + debug(("dbit: %d...", dbit)); + + db->curbit = dbit; + db->hmask = masks[hbit]; + + pagb = hash & db->hmask; +/* + * see if the block we need is already in memory. + * note: this lookaside cache has about 10% hit rate. + */ + if (pagb != db->pagbno) { +/* + * note: here, we assume a "hole" is read as 0s. + * if not, must zero pagbuf first. + */ + (void) memset(db->pagbuf, 0, PBLKSIZ); + + if (lseek(db->pagf, OFF_PAG(pagb), SEEK_SET) < 0 + || read(db->pagf, db->pagbuf, PBLKSIZ) < 0) + return 0; + if (!chkpage(db->pagbuf)) { + return 0; + } + db->pagbno = pagb; + + debug(("pag read: %d\n", pagb)); + } + return 1; +} + +static int +getdbit(register DBM *db, register long int dbit) +{ + register long c; + register long dirb; + + c = dbit / BYTESIZ; + dirb = c / DBLKSIZ; + + if (dirb != db->dirbno) { + if (lseek(db->dirf, OFF_DIR(dirb), SEEK_SET) < 0 + || read(db->dirf, db->dirbuf, DBLKSIZ) < 0) + return 0; + db->dirbno = dirb; + + debug(("dir read: %d\n", dirb)); + } + + return db->dirbuf[c % DBLKSIZ] & (1 << (dbit % BYTESIZ)); +} + +static int +setdbit(register DBM *db, register long int dbit) +{ + register long c; + register long dirb; + + c = dbit / BYTESIZ; + dirb = c / DBLKSIZ; + + if (dirb != db->dirbno) { + if (lseek(db->dirf, OFF_DIR(dirb), SEEK_SET) < 0 + || read(db->dirf, db->dirbuf, DBLKSIZ) < 0) + return 0; + db->dirbno = dirb; + + debug(("dir read: %d\n", dirb)); + } + + db->dirbuf[c % DBLKSIZ] |= (1 << (dbit % BYTESIZ)); + + if (dbit >= db->maxbno) + db->maxbno += (long) DBLKSIZ * BYTESIZ; + + if (lseek(db->dirf, OFF_DIR(dirb), SEEK_SET) < 0 + || write(db->dirf, db->dirbuf, DBLKSIZ) < 0) + return 0; + + return 1; +} + +/* + * getnext - get the next key in the page, and if done with + * the page, try the next page in sequence + */ +static datum +getnext(register DBM *db) +{ + datum key; + + for (;;) { + db->keyptr++; + key = getnkey(db->pagbuf, db->keyptr); + if (key.dptr != NULL) + return key; +/* + * we either run out, or there is nothing on this page.. + * try the next one... If we lost our position on the + * file, we will have to seek. + */ + db->keyptr = 0; + if (db->pagbno != db->blkptr++) + if (lseek(db->pagf, OFF_PAG(db->blkptr), SEEK_SET) < 0) + break; + db->pagbno = db->blkptr; + if (read(db->pagf, db->pagbuf, PBLKSIZ) <= 0) + break; + if (!chkpage(db->pagbuf)) { + break; + } + } + + return ioerr(db), nullitem; +} + +/* pair.c */ +/* + * sdbm - ndbm work-alike hashed database library + * based on Per-Aake Larson's Dynamic Hashing algorithms. BIT 18 (1978). + * author: oz@nexus.yorku.ca + * status: public domain. + * + * page-level routines + */ + +#ifndef BSD42 +/*#include */ +#endif + +#define exhash(item) sdbm_hash((item).dptr, (item).dsize) + +/* + * forward + */ +static int seepair proto((char *, int, char *, int)); + +/* + * page format: + * +------------------------------+ + * ino | n | keyoff | datoff | keyoff | + * +------------+--------+--------+ + * | datoff | - - - ----> | + * +--------+---------------------+ + * | F R E E A R E A | + * +--------------+---------------+ + * | <---- - - - | data | + * +--------+-----+----+----------+ + * | key | data | key | + * +--------+----------+----------+ + * + * calculating the offsets for free area: if the number + * of entries (ino[0]) is zero, the offset to the END of + * the free area is the block size. Otherwise, it is the + * nth (ino[ino[0]]) entry's offset. + */ + +static int +fitpair(char *pag, int need) +{ + register int n; + register int off; + register int free; + register short *ino = (short *) pag; + + off = ((n = GET_SHORT(ino,0)) > 0) ? GET_SHORT(ino,n) : PBLKSIZ; + free = off - (n + 1) * (int)sizeof(short); + need += 2 * (int)sizeof(short); + + debug(("free %d need %d\n", free, need)); + + return need <= free; +} + +static void +putpair(char *pag, datum key, datum val) +{ + register int n; + register int off; + register short *ino = (short *) pag; + + off = ((n = GET_SHORT(ino,0)) > 0) ? GET_SHORT(ino,n) : PBLKSIZ; +/* + * enter the key first + */ + off -= key.dsize; + if (key.dsize) + (void) memcpy(pag + off, key.dptr, key.dsize); + PUT_SHORT(ino,n + 1,off); +/* + * now the data + */ + off -= val.dsize; + if (val.dsize) + (void) memcpy(pag + off, val.dptr, val.dsize); + PUT_SHORT(ino,n + 2,off); +/* + * adjust item count + */ + PUT_SHORT(ino,0,GET_SHORT(ino,0) + 2); +} + +static datum +getpair(char *pag, datum key) +{ + register int i; + register int n; + datum val; + register short *ino = (short *) pag; + + if ((n = GET_SHORT(ino,0)) == 0) + return nullitem; + + if ((i = seepair(pag, n, key.dptr, key.dsize)) == 0) + return nullitem; + + val.dptr = pag + GET_SHORT(ino,i + 1); + val.dsize = GET_SHORT(ino,i) - GET_SHORT(ino,i + 1); + return val; +} + +#if SEEDUPS +static int +duppair(char *pag, datum key) +{ + register short *ino = (short *) pag; + return GET_SHORT(ino,0) > 0 && + seepair(pag, GET_SHORT(ino,0), key.dptr, key.dsize) > 0; +} +#endif + +static datum +getnkey(char *pag, int num) +{ + datum key; + register int off; + register short *ino = (short *) pag; + + num = num * 2 - 1; + if (GET_SHORT(ino,0) == 0 || num > GET_SHORT(ino,0)) + return nullitem; + + off = (num > 1) ? GET_SHORT(ino,num - 1) : PBLKSIZ; + + key.dptr = pag + GET_SHORT(ino,num); + key.dsize = off - GET_SHORT(ino,num); + + return key; +} + +static int +delpair(char *pag, datum key) +{ + register int n; + register int i; + register short *ino = (short *) pag; + + if ((n = GET_SHORT(ino,0)) == 0) + return 0; + + if ((i = seepair(pag, n, key.dptr, key.dsize)) == 0) + return 0; +/* + * found the key. if it is the last entry + * [i.e. i == n - 1] we just adjust the entry count. + * hard case: move all data down onto the deleted pair, + * shift offsets onto deleted offsets, and adjust them. + * [note: 0 < i < n] + */ + if (i < n - 1) { + register int m; + register char *dst = pag + (i == 1 ? PBLKSIZ : GET_SHORT(ino,i - 1)); + register char *src = pag + GET_SHORT(ino,i + 1); + register ptrdiff_t zoo = dst - src; + + debug(("free-up %"PRIdPTRDIFF" ", zoo)); +/* + * shift data/keys down + */ + m = GET_SHORT(ino,i + 1) - GET_SHORT(ino,n); +#ifdef DUFF +#define MOVB *--dst = *--src + + if (m > 0) { + register int loop = (m + 8 - 1) >> 3; + + switch (m & (8 - 1)) { + case 0: do { + MOVB; case 7: MOVB; + case 6: MOVB; case 5: MOVB; + case 4: MOVB; case 3: MOVB; + case 2: MOVB; case 1: MOVB; + } while (--loop); + } + } +#else +#ifdef MEMMOVE + memmove(dst-m, src-m, m); +#else + while (m--) + *--dst = *--src; +#endif +#endif +/* + * adjust offset index up + */ + while (i < n - 1) { + PUT_SHORT(ino,i, GET_SHORT(ino,i + 2) + zoo); + i++; + } + } + PUT_SHORT(ino, 0, GET_SHORT(ino, 0) - 2); + return 1; +} + +/* + * search for the key in the page. + * return offset index in the range 0 < i < n. + * return 0 if not found. + */ +static int +seepair(char *pag, register int n, register char *key, register int siz) +{ + register int i; + register int off = PBLKSIZ; + register short *ino = (short *) pag; + + for (i = 1; i < n; i += 2) { + if (siz == off - GET_SHORT(ino,i) && + memcmp(key, pag + GET_SHORT(ino,i), siz) == 0) + return i; + off = GET_SHORT(ino,i + 1); + } + return 0; +} + +static void +splpage(char *pag, char *new, long int sbit) +{ + datum key; + datum val; + + register int n; + register int off = PBLKSIZ; + char cur[PBLKSIZ]; + register short *ino = (short *) cur; + + (void) memcpy(cur, pag, PBLKSIZ); + (void) memset(pag, 0, PBLKSIZ); + (void) memset(new, 0, PBLKSIZ); + + n = GET_SHORT(ino,0); + for (ino++; n > 0; ino += 2) { + key.dptr = cur + GET_SHORT(ino,0); + key.dsize = off - GET_SHORT(ino,0); + val.dptr = cur + GET_SHORT(ino,1); + val.dsize = GET_SHORT(ino,0) - GET_SHORT(ino,1); +/* + * select the page pointer (by looking at sbit) and insert + */ + (void) putpair((exhash(key) & sbit) ? new : pag, key, val); + + off = GET_SHORT(ino,1); + n -= 2; + } + + debug(("%d split %d/%d\n", ((short *) cur)[0] / 2, + ((short *) new)[0] / 2, + ((short *) pag)[0] / 2)); +} + +/* + * check page sanity: + * number of entries should be something + * reasonable, and all offsets in the index should be in order. + * this could be made more rigorous. + */ +static int +chkpage(char *pag) +{ + register int n; + register int off; + register short *ino = (short *) pag; + + if ((n = GET_SHORT(ino,0)) < 0 || n > PBLKSIZ / (int)sizeof(short)) + return 0; + + if (n > 0) { + off = PBLKSIZ; + for (ino++; n > 0; ino += 2) { + if (GET_SHORT(ino,0) > off || GET_SHORT(ino,1) > off || + GET_SHORT(ino,1) > GET_SHORT(ino,0)) + return 0; + off = GET_SHORT(ino,1); + n -= 2; + } + } + return 1; +} + +/* hash.c */ +/* + * sdbm - ndbm work-alike hashed database library + * based on Per-Aake Larson's Dynamic Hashing algorithms. BIT 18 (1978). + * author: oz@nexus.yorku.ca + * status: public domain. keep it that way. + * + * hashing routine + */ + +/* + * polynomial conversion ignoring overflows + * [this seems to work remarkably well, in fact better + * then the ndbm hash function. Replace at your own risk] + * use: 65599 nice. + * 65587 even better. + */ +long +sdbm_hash(register char *str, register int len) +{ + register unsigned long n = 0; + +#ifdef DUFF + +#define HASHC n = *str++ + 65599 * n + + if (len > 0) { + register int loop = (len + 8 - 1) >> 3; + + switch(len & (8 - 1)) { + case 0: do { + HASHC; case 7: HASHC; + case 6: HASHC; case 5: HASHC; + case 4: HASHC; case 3: HASHC; + case 2: HASHC; case 1: HASHC; + } while (--loop); + } + + } +#else + while (len--) + n = ((*str++) & 255) + 65587L * n; +#endif + return n; +} diff --git a/jni/ruby/ext/sdbm/depend b/jni/ruby/ext/sdbm/depend new file mode 100644 index 0000000..b8f5513 --- /dev/null +++ b/jni/ruby/ext/sdbm/depend @@ -0,0 +1,19 @@ +# AUTOGENERATED DEPENDENCIES START +_sdbm.o: $(RUBY_EXTCONF_H) +_sdbm.o: $(arch_hdrdir)/ruby/config.h +_sdbm.o: $(hdrdir)/ruby/defines.h +_sdbm.o: $(hdrdir)/ruby/missing.h +_sdbm.o: _sdbm.c +_sdbm.o: sdbm.h +init.o: $(RUBY_EXTCONF_H) +init.o: $(arch_hdrdir)/ruby/config.h +init.o: $(hdrdir)/ruby/defines.h +init.o: $(hdrdir)/ruby/intern.h +init.o: $(hdrdir)/ruby/missing.h +init.o: $(hdrdir)/ruby/ruby.h +init.o: $(hdrdir)/ruby/st.h +init.o: $(hdrdir)/ruby/subst.h +init.o: $(top_srcdir)/include/ruby.h +init.o: init.c +init.o: sdbm.h +# AUTOGENERATED DEPENDENCIES END diff --git a/jni/ruby/ext/sdbm/extconf.h b/jni/ruby/ext/sdbm/extconf.h new file mode 100644 index 0000000..6df6e45 --- /dev/null +++ b/jni/ruby/ext/sdbm/extconf.h @@ -0,0 +1,4 @@ +#ifndef EXTCONF_H +#define EXTCONF_H +#define BADMESS 0 +#endif diff --git a/jni/ruby/ext/sdbm/extconf.rb b/jni/ruby/ext/sdbm/extconf.rb new file mode 100644 index 0000000..67796fb --- /dev/null +++ b/jni/ruby/ext/sdbm/extconf.rb @@ -0,0 +1,4 @@ +require 'mkmf' + +$defs << "-D""BADMESS=0" +create_makefile("sdbm") diff --git a/jni/ruby/ext/sdbm/init.c b/jni/ruby/ext/sdbm/init.c new file mode 100644 index 0000000..e9ae766 --- /dev/null +++ b/jni/ruby/ext/sdbm/init.c @@ -0,0 +1,1065 @@ +/************************************************ + + sdbminit.c - + + $Author: nobu $ + created at: Fri May 7 08:34:24 JST 1999 + + Copyright (C) 1995-2001 Yukihiro Matsumoto + +************************************************/ + +#include "ruby.h" + +#include "sdbm.h" +#include +#include + +/* + * Document-class: SDBM + * + * SDBM provides a simple file-based key-value store, which can only store + * String keys and values. + * + * Note that Ruby comes with the source code for SDBM, while the DBM and GDBM + * standard libraries rely on external libraries and headers. + * + * === Examples + * + * Insert values: + * + * require 'sdbm' + * + * SDBM.open 'my_database' do |db| + * db['apple'] = 'fruit' + * db['pear'] = 'fruit' + * db['carrot'] = 'vegetable' + * db['tomato'] = 'vegetable' + * end + * + * Bulk update: + * + * require 'sdbm' + * + * SDBM.open 'my_database' do |db| + * db.update('peach' => 'fruit', 'tomato' => 'fruit') + * end + * + * Retrieve values: + * + * require 'sdbm' + * + * SDBM.open 'my_database' do |db| + * db.each do |key, value| + * puts "Key: #{key}, Value: #{value}" + * end + * end + * + * Outputs: + * + * Key: apple, Value: fruit + * Key: pear, Value: fruit + * Key: carrot, Value: vegetable + * Key: peach, Value: fruit + * Key: tomato, Value: fruit + */ + +static VALUE rb_cDBM, rb_eDBMError; + +struct dbmdata { + int di_size; + DBM *di_dbm; +}; + +static void +closed_sdbm(void) +{ + rb_raise(rb_eDBMError, "closed SDBM file"); +} + +#define GetDBM(obj, dbmp) do {\ + TypedData_Get_Struct((obj), struct dbmdata, &sdbm_type, (dbmp));\ + if ((dbmp) == 0) closed_sdbm();\ + if ((dbmp)->di_dbm == 0) closed_sdbm();\ +} while (0) + +#define GetDBM2(obj, dbmp, dbm) do {\ + GetDBM((obj), (dbmp));\ + (dbm) = (dbmp)->di_dbm;\ +} while (0) + +static void +free_sdbm(void *ptr) +{ + struct dbmdata *dbmp = ptr; + + if (dbmp->di_dbm) sdbm_close(dbmp->di_dbm); + ruby_xfree(dbmp); +} + +static size_t +memsize_dbm(const void *ptr) +{ + size_t size = 0; + const struct dbmdata *dbmp = ptr; + if (dbmp) { + size += sizeof(*dbmp); + if (dbmp->di_dbm) size += sizeof(DBM); + } + return size; +} + +static const rb_data_type_t sdbm_type = { + "sdbm", + {0, free_sdbm, memsize_dbm,}, + 0, 0, + RUBY_TYPED_FREE_IMMEDIATELY, +}; + +/* + * call-seq: + * sdbm.close -> nil + * + * Closes the database file. + * + * Raises SDBMError if the database is already closed. + */ +static VALUE +fsdbm_close(VALUE obj) +{ + struct dbmdata *dbmp; + + GetDBM(obj, dbmp); + sdbm_close(dbmp->di_dbm); + dbmp->di_dbm = 0; + + return Qnil; +} + +/* + * call-seq: + * sdbm.closed? -> true or false + * + * Returns +true+ if the database is closed. + */ +static VALUE +fsdbm_closed(VALUE obj) +{ + struct dbmdata *dbmp; + + TypedData_Get_Struct(obj, struct dbmdata, &sdbm_type, dbmp); + if (dbmp == 0) + return Qtrue; + if (dbmp->di_dbm == 0) + return Qtrue; + + return Qfalse; +} + +static VALUE +fsdbm_alloc(VALUE klass) +{ + return TypedData_Wrap_Struct(klass, &sdbm_type, 0); +} +/* + * call-seq: + * SDBM.new(filename, mode = 0666) + * + * Creates a new database handle by opening the given +filename+. SDBM actually + * uses two physical files, with extensions '.dir' and '.pag'. These extensions + * will automatically be appended to the +filename+. + * + * If the file does not exist, a new file will be created using the given + * +mode+, unless +mode+ is explicitly set to nil. In the latter case, no + * database will be created. + * + * If the file exists, it will be opened in read/write mode. If this fails, it + * will be opened in read-only mode. + */ +static VALUE +fsdbm_initialize(int argc, VALUE *argv, VALUE obj) +{ + volatile VALUE file; + VALUE vmode; + DBM *dbm; + struct dbmdata *dbmp; + int mode; + + if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) { + mode = 0666; /* default value */ + } + else if (NIL_P(vmode)) { + mode = -1; /* return nil if DB not exist */ + } + else { + mode = NUM2INT(vmode); + } + FilePathValue(file); + + dbm = 0; + if (mode >= 0) + dbm = sdbm_open(RSTRING_PTR(file), O_RDWR|O_CREAT, mode); + if (!dbm) + dbm = sdbm_open(RSTRING_PTR(file), O_RDWR, 0); + if (!dbm) + dbm = sdbm_open(RSTRING_PTR(file), O_RDONLY, 0); + + if (!dbm) { + if (mode == -1) return Qnil; + rb_sys_fail_str(file); + } + + dbmp = ALLOC(struct dbmdata); + DATA_PTR(obj) = dbmp; + dbmp->di_dbm = dbm; + dbmp->di_size = -1; + + return obj; +} + +/* + * call-seq: + * SDBM.open(filename, mode = 0666) + * SDBM.open(filename, mode = 0666) { |sdbm| ... } + * + * If called without a block, this is the same as SDBM.new. + * + * If a block is given, the new database will be passed to the block and + * will be safely closed after the block has executed. + * + * Example: + * + * require 'sdbm' + * + * SDBM.open('my_database') do |db| + * db['hello'] = 'world' + * end + */ +static VALUE +fsdbm_s_open(int argc, VALUE *argv, VALUE klass) +{ + VALUE obj = fsdbm_alloc(klass); + + if (NIL_P(fsdbm_initialize(argc, argv, obj))) { + return Qnil; + } + + if (rb_block_given_p()) { + return rb_ensure(rb_yield, obj, fsdbm_close, obj); + } + + return obj; +} + +static VALUE +fsdbm_fetch(VALUE obj, VALUE keystr, VALUE ifnone) +{ + datum key, value; + struct dbmdata *dbmp; + DBM *dbm; + + ExportStringValue(keystr); + key.dptr = RSTRING_PTR(keystr); + key.dsize = RSTRING_LENINT(keystr); + + GetDBM2(obj, dbmp, dbm); + value = sdbm_fetch(dbm, key); + if (value.dptr == 0) { + if (ifnone == Qnil && rb_block_given_p()) + return rb_yield(rb_external_str_new(key.dptr, key.dsize)); + return ifnone; + } + return rb_external_str_new(value.dptr, value.dsize); +} + +/* + * call-seq: + * sdbm[key] -> value or nil + * + * Returns the +value+ in the database associated with the given +key+ string. + * + * If no value is found, returns +nil+. + */ +static VALUE +fsdbm_aref(VALUE obj, VALUE keystr) +{ + return fsdbm_fetch(obj, keystr, Qnil); +} + +/* + * call-seq: + * sdbm.fetch(key) -> value or nil + * sdbm.fetch(key) { |key| ... } + * + * Returns the +value+ in the database associated with the given +key+ string. + * + * If a block is provided, the block will be called when there is no + * +value+ associated with the given +key+. The +key+ will be passed in as an + * argument to the block. + * + * If no block is provided and no value is associated with the given +key+, + * then an IndexError will be raised. + */ +static VALUE +fsdbm_fetch_m(int argc, VALUE *argv, VALUE obj) +{ + VALUE keystr, valstr, ifnone; + + rb_scan_args(argc, argv, "11", &keystr, &ifnone); + valstr = fsdbm_fetch(obj, keystr, ifnone); + if (argc == 1 && !rb_block_given_p() && NIL_P(valstr)) + rb_raise(rb_eIndexError, "key not found"); + + return valstr; +} + +/* + * call-seq: + * sdbm.key(value) -> key + * + * Returns the +key+ associated with the given +value+. If more than one + * +key+ corresponds to the given +value+, then the first key to be found + * will be returned. If no keys are found, +nil+ will be returned. + */ +static VALUE +fsdbm_key(VALUE obj, VALUE valstr) +{ + datum key, val; + struct dbmdata *dbmp; + DBM *dbm; + + ExportStringValue(valstr); + val.dptr = RSTRING_PTR(valstr); + val.dsize = RSTRING_LENINT(valstr); + + GetDBM2(obj, dbmp, dbm); + for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) { + val = sdbm_fetch(dbm, key); + if (val.dsize == RSTRING_LEN(valstr) && + memcmp(val.dptr, RSTRING_PTR(valstr), val.dsize) == 0) + return rb_external_str_new(key.dptr, key.dsize); + } + return Qnil; +} + +/* + * :nodoc: + */ +static VALUE +fsdbm_index(VALUE hash, VALUE value) +{ + rb_warn("SDBM#index is deprecated; use SDBM#key"); + return fsdbm_key(hash, value); +} + +/* call-seq: + * sdbm.select { |key, value| ... } -> Array + * + * Returns a new Array of key-value pairs for which the block returns +true+. + * + * Example: + * + * require 'sdbm' + * + * SDBM.open 'my_database' do |db| + * db['apple'] = 'fruit' + * db['pear'] = 'fruit' + * db['spinach'] = 'vegetable' + * + * veggies = db.select do |key, value| + * value == 'vegetable' + * end #=> [["apple", "fruit"], ["pear", "fruit"]] + * end + */ +static VALUE +fsdbm_select(VALUE obj) +{ + VALUE new = rb_ary_new(); + datum key, val; + DBM *dbm; + struct dbmdata *dbmp; + + GetDBM2(obj, dbmp, dbm); + for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) { + VALUE assoc, v; + val = sdbm_fetch(dbm, key); + assoc = rb_assoc_new(rb_external_str_new(key.dptr, key.dsize), + rb_external_str_new(val.dptr, val.dsize)); + v = rb_yield(assoc); + if (RTEST(v)) { + rb_ary_push(new, assoc); + } + GetDBM2(obj, dbmp, dbm); + } + + return new; +} + +/* call-seq: + * sdbm.values_at(key, ...) -> Array + * + * Returns an Array of values corresponding to the given keys. + */ +static VALUE +fsdbm_values_at(int argc, VALUE *argv, VALUE obj) +{ + VALUE new = rb_ary_new2(argc); + int i; + + for (i=0; i value or nil + * sdbm.delete(key) { |key, value| ... } + * + * Deletes the key-value pair corresponding to the given +key+. If the + * +key+ exists, the deleted value will be returned, otherwise +nil+. + * + * If a block is provided, the deleted +key+ and +value+ will be passed to + * the block as arguments. If the +key+ does not exist in the database, the + * value will be +nil+. + */ +static VALUE +fsdbm_delete(VALUE obj, VALUE keystr) +{ + datum key, value; + struct dbmdata *dbmp; + DBM *dbm; + VALUE valstr; + + fdbm_modify(obj); + ExportStringValue(keystr); + key.dptr = RSTRING_PTR(keystr); + key.dsize = RSTRING_LENINT(keystr); + + GetDBM2(obj, dbmp, dbm); + dbmp->di_size = -1; + + value = sdbm_fetch(dbm, key); + if (value.dptr == 0) { + if (rb_block_given_p()) return rb_yield(keystr); + return Qnil; + } + + /* need to save value before sdbm_delete() */ + valstr = rb_external_str_new(value.dptr, value.dsize); + + if (sdbm_delete(dbm, key)) { + dbmp->di_size = -1; + rb_raise(rb_eDBMError, "dbm_delete failed"); + } + else if (dbmp->di_size >= 0) { + dbmp->di_size--; + } + return valstr; +} + +/* + * call-seq: + * sdbm.shift -> Array or nil + * + * Removes a key-value pair from the database and returns them as an + * Array. If the database is empty, returns +nil+. + */ +static VALUE +fsdbm_shift(VALUE obj) +{ + datum key, val; + struct dbmdata *dbmp; + DBM *dbm; + VALUE keystr, valstr; + + fdbm_modify(obj); + GetDBM2(obj, dbmp, dbm); + key = sdbm_firstkey(dbm); + if (!key.dptr) return Qnil; + val = sdbm_fetch(dbm, key); + keystr = rb_external_str_new(key.dptr, key.dsize); + valstr = rb_external_str_new(val.dptr, val.dsize); + sdbm_delete(dbm, key); + if (dbmp->di_size >= 0) { + dbmp->di_size--; + } + + return rb_assoc_new(keystr, valstr); +} + +/* + * call-seq: + * sdbm.delete_if { |key, value| ... } -> self + * sdbm.reject! { |key, value| ... } -> self + * + * Iterates over the key-value pairs in the database, deleting those for + * which the block returns +true+. + */ +static VALUE +fsdbm_delete_if(VALUE obj) +{ + datum key, val; + struct dbmdata *dbmp; + DBM *dbm; + VALUE keystr, valstr; + VALUE ret, ary = rb_ary_new(); + int i, status = 0, n; + + fdbm_modify(obj); + GetDBM2(obj, dbmp, dbm); + n = dbmp->di_size; + dbmp->di_size = -1; + for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) { + val = sdbm_fetch(dbm, key); + keystr = rb_external_str_new(key.dptr, key.dsize); + valstr = rb_external_str_new(val.dptr, val.dsize); + ret = rb_protect(rb_yield, rb_assoc_new(rb_str_dup(keystr), valstr), &status); + if (status != 0) break; + if (RTEST(ret)) rb_ary_push(ary, keystr); + GetDBM2(obj, dbmp, dbm); + } + + for (i = 0; i < RARRAY_LEN(ary); i++) { + keystr = RARRAY_PTR(ary)[i]; + ExportStringValue(keystr); + key.dptr = RSTRING_PTR(keystr); + key.dsize = RSTRING_LENINT(keystr); + if (sdbm_delete(dbm, key)) { + rb_raise(rb_eDBMError, "sdbm_delete failed"); + } + } + if (status) rb_jump_tag(status); + if (n > 0) dbmp->di_size = n - RARRAY_LENINT(ary); + + return obj; +} + +/* + * call-seq: + * sdbm.clear -> self + * + * Deletes all data from the database. + */ +static VALUE +fsdbm_clear(VALUE obj) +{ + datum key; + struct dbmdata *dbmp; + DBM *dbm; + + fdbm_modify(obj); + GetDBM2(obj, dbmp, dbm); + dbmp->di_size = -1; + while (key = sdbm_firstkey(dbm), key.dptr) { + if (sdbm_delete(dbm, key)) { + rb_raise(rb_eDBMError, "sdbm_delete failed"); + } + } + dbmp->di_size = 0; + + return obj; +} + +/* + * call-seq: + * sdbm.invert -> Hash + * + * Returns a Hash in which the key-value pairs have been inverted. + * + * Example: + * + * require 'sdbm' + * + * SDBM.open 'my_database' do |db| + * db.update('apple' => 'fruit', 'spinach' => 'vegetable') + * + * db.invert #=> {"fruit" => "apple", "vegetable" => "spinach"} + * end + */ +static VALUE +fsdbm_invert(VALUE obj) +{ + datum key, val; + struct dbmdata *dbmp; + DBM *dbm; + VALUE keystr, valstr; + VALUE hash = rb_hash_new(); + + GetDBM2(obj, dbmp, dbm); + for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) { + val = sdbm_fetch(dbm, key); + keystr = rb_external_str_new(key.dptr, key.dsize); + valstr = rb_external_str_new(val.dptr, val.dsize); + rb_hash_aset(hash, valstr, keystr); + } + return hash; +} + +/* + * call-seq: + * sdbm[key] = value -> value + * sdbm.store(key, value) -> value + * + * Stores a new +value+ in the database with the given +key+ as an index. + * + * If the +key+ already exists, this will update the +value+ associated with + * the +key+. + * + * Returns the given +value+. + */ +static VALUE +fsdbm_store(VALUE obj, VALUE keystr, VALUE valstr) +{ + datum key, val; + struct dbmdata *dbmp; + DBM *dbm; + + if (valstr == Qnil) { + fsdbm_delete(obj, keystr); + return Qnil; + } + + fdbm_modify(obj); + ExportStringValue(keystr); + ExportStringValue(valstr); + + key.dptr = RSTRING_PTR(keystr); + key.dsize = RSTRING_LENINT(keystr); + + val.dptr = RSTRING_PTR(valstr); + val.dsize = RSTRING_LENINT(valstr); + + GetDBM2(obj, dbmp, dbm); + dbmp->di_size = -1; + if (sdbm_store(dbm, key, val, DBM_REPLACE)) { +#ifdef HAVE_DBM_CLAERERR + sdbm_clearerr(dbm); +#endif + if (errno == EPERM) rb_sys_fail(0); + rb_raise(rb_eDBMError, "sdbm_store failed"); + } + + return valstr; +} + +static VALUE +update_i(RB_BLOCK_CALL_FUNC_ARGLIST(pair, dbm)) +{ + Check_Type(pair, T_ARRAY); + if (RARRAY_LEN(pair) < 2) { + rb_raise(rb_eArgError, "pair must be [key, value]"); + } + fsdbm_store(dbm, RARRAY_PTR(pair)[0], RARRAY_PTR(pair)[1]); + return Qnil; +} + +/* + * call-seq: + * sdbm.update(pairs) -> self + * + * Insert or update key-value pairs. + * + * This method will work with any object which implements an each_pair + * method, such as a Hash. + */ +static VALUE +fsdbm_update(VALUE obj, VALUE other) +{ + rb_block_call(other, rb_intern("each_pair"), 0, 0, update_i, obj); + return obj; +} + +/* + * call-seq: + * sdbm.replace(pairs) -> self + * + * Empties the database, then inserts the given key-value pairs. + * + * This method will work with any object which implements an each_pair + * method, such as a Hash. + */ +static VALUE +fsdbm_replace(VALUE obj, VALUE other) +{ + fsdbm_clear(obj); + rb_block_call(other, rb_intern("each_pair"), 0, 0, update_i, obj); + return obj; +} + +/* + * call-seq: + * sdbm.length -> integer + * sdbm.size -> integer + * + * Returns the number of keys in the database. + */ +static VALUE +fsdbm_length(VALUE obj) +{ + datum key; + struct dbmdata *dbmp; + DBM *dbm; + int i = 0; + + GetDBM2(obj, dbmp, dbm); + if (dbmp->di_size > 0) return INT2FIX(dbmp->di_size); + + for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) { + i++; + } + dbmp->di_size = i; + + return INT2FIX(i); +} + +/* + * call-seq: + * sdbm.empty? -> true or false + * + * Returns +true+ if the database is empty. + */ +static VALUE +fsdbm_empty_p(VALUE obj) +{ + datum key; + struct dbmdata *dbmp; + DBM *dbm; + + GetDBM(obj, dbmp); + if (dbmp->di_size < 0) { + dbm = dbmp->di_dbm; + + for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) { + return Qfalse; + } + } + else { + if (dbmp->di_size) + return Qfalse; + } + return Qtrue; +} + +/* + * call-seq: + * sdbm.each_value + * sdbm.each_value { |value| ... } + * + * Iterates over each +value+ in the database. + * + * If no block is given, returns an Enumerator. + */ +static VALUE +fsdbm_each_value(VALUE obj) +{ + datum key, val; + struct dbmdata *dbmp; + DBM *dbm; + + RETURN_ENUMERATOR(obj, 0, 0); + + GetDBM2(obj, dbmp, dbm); + for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) { + val = sdbm_fetch(dbm, key); + rb_yield(rb_external_str_new(val.dptr, val.dsize)); + GetDBM2(obj, dbmp, dbm); + } + return obj; +} + +/* + * call-seq: + * sdbm.each_key + * sdbm.each_key { |key| ... } + * + * Iterates over each +key+ in the database. + * + * If no block is given, returns an Enumerator. + */ +static VALUE +fsdbm_each_key(VALUE obj) +{ + datum key; + struct dbmdata *dbmp; + DBM *dbm; + + RETURN_ENUMERATOR(obj, 0, 0); + + GetDBM2(obj, dbmp, dbm); + for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) { + rb_yield(rb_external_str_new(key.dptr, key.dsize)); + GetDBM2(obj, dbmp, dbm); + } + return obj; +} + +/* + * call-seq: + * sdbm.each + * sdbm.each { |key, value| ... } + * sdbm.each_pair + * sdbm.each_pair { |key, value| ... } + * + * Iterates over each key-value pair in the database. + * + * If no block is given, returns an Enumerator. + */ +static VALUE +fsdbm_each_pair(VALUE obj) +{ + datum key, val; + DBM *dbm; + struct dbmdata *dbmp; + VALUE keystr, valstr; + + RETURN_ENUMERATOR(obj, 0, 0); + + GetDBM2(obj, dbmp, dbm); + for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) { + val = sdbm_fetch(dbm, key); + keystr = rb_external_str_new(key.dptr, key.dsize); + valstr = rb_external_str_new(val.dptr, val.dsize); + rb_yield(rb_assoc_new(keystr, valstr)); + GetDBM2(obj, dbmp, dbm); + } + + return obj; +} + +/* + * call-seq: + * sdbm.keys -> Array + * + * Returns a new Array containing the keys in the database. + */ +static VALUE +fsdbm_keys(VALUE obj) +{ + datum key; + struct dbmdata *dbmp; + DBM *dbm; + VALUE ary; + + GetDBM2(obj, dbmp, dbm); + ary = rb_ary_new(); + for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) { + rb_ary_push(ary, rb_external_str_new(key.dptr, key.dsize)); + } + + return ary; +} + +/* + * call-seq: + * sdbm.values -> Array + * + * Returns a new Array containing the values in the database. + */ +static VALUE +fsdbm_values(VALUE obj) +{ + datum key, val; + struct dbmdata *dbmp; + DBM *dbm; + VALUE ary; + + GetDBM2(obj, dbmp, dbm); + ary = rb_ary_new(); + for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) { + val = sdbm_fetch(dbm, key); + rb_ary_push(ary, rb_external_str_new(val.dptr, val.dsize)); + } + + return ary; +} + +/* + * call-seq: + * sdbm.include?(key) -> true or false + * sdbm.key?(key) -> true or false + * sdbm.member?(key) -> true or false + * sdbm.has_key?(key) -> true or false + * + * Returns +true+ if the database contains the given +key+. + */ +static VALUE +fsdbm_has_key(VALUE obj, VALUE keystr) +{ + datum key, val; + struct dbmdata *dbmp; + DBM *dbm; + + ExportStringValue(keystr); + key.dptr = RSTRING_PTR(keystr); + key.dsize = RSTRING_LENINT(keystr); + + GetDBM2(obj, dbmp, dbm); + val = sdbm_fetch(dbm, key); + if (val.dptr) return Qtrue; + return Qfalse; +} + +/* + * call-seq: + * sdbm.value?(key) -> true or false + * sdbm.has_value?(key) -> true or false + * + * Returns +true+ if the database contains the given +value+. + */ +static VALUE +fsdbm_has_value(VALUE obj, VALUE valstr) +{ + datum key, val; + struct dbmdata *dbmp; + DBM *dbm; + + ExportStringValue(valstr); + val.dptr = RSTRING_PTR(valstr); + val.dsize = RSTRING_LENINT(valstr); + + GetDBM2(obj, dbmp, dbm); + for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) { + val = sdbm_fetch(dbm, key); + if (val.dsize == RSTRING_LENINT(valstr) && + memcmp(val.dptr, RSTRING_PTR(valstr), val.dsize) == 0) + return Qtrue; + } + return Qfalse; +} + +/* + * call-seq: + * sdbm.to_a -> Array + * + * Returns a new Array containing each key-value pair in the database. + * + * Example: + * + * require 'sdbm' + * + * SDBM.open 'my_database' do |db| + * db.update('apple' => 'fruit', 'spinach' => 'vegetable') + * + * db.to_a #=> [["apple", "fruit"], ["spinach", "vegetable"]] + * end + */ +static VALUE +fsdbm_to_a(VALUE obj) +{ + datum key, val; + struct dbmdata *dbmp; + DBM *dbm; + VALUE ary; + + GetDBM2(obj, dbmp, dbm); + ary = rb_ary_new(); + for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) { + val = sdbm_fetch(dbm, key); + rb_ary_push(ary, rb_assoc_new(rb_external_str_new(key.dptr, key.dsize), + rb_external_str_new(val.dptr, val.dsize))); + } + + return ary; +} + +/* + * call-seq: + * sdbm.to_hash -> Hash + * + * Returns a new Hash containing each key-value pair in the database. + */ +static VALUE +fsdbm_to_hash(VALUE obj) +{ + datum key, val; + struct dbmdata *dbmp; + DBM *dbm; + VALUE hash; + + GetDBM2(obj, dbmp, dbm); + hash = rb_hash_new(); + for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) { + val = sdbm_fetch(dbm, key); + rb_hash_aset(hash, rb_external_str_new(key.dptr, key.dsize), + rb_external_str_new(val.dptr, val.dsize)); + } + + return hash; +} + +/* + * call-seq: + * sdbm.reject { |key, value| ... } -> Hash + * + * Creates a new Hash using the key-value pairs from the database, then + * calls Hash#reject with the given block, which returns a Hash with + * only the key-value pairs for which the block returns +false+. + */ +static VALUE +fsdbm_reject(VALUE obj) +{ + return rb_hash_delete_if(fsdbm_to_hash(obj)); +} + +void +Init_sdbm(void) +{ + rb_cDBM = rb_define_class("SDBM", rb_cObject); + rb_eDBMError = rb_define_class("SDBMError", rb_eStandardError); + /* Document-class: SDBMError + * Exception class used to return errors from the sdbm library. + */ + rb_include_module(rb_cDBM, rb_mEnumerable); + + rb_define_alloc_func(rb_cDBM, fsdbm_alloc); + rb_define_singleton_method(rb_cDBM, "open", fsdbm_s_open, -1); + + rb_define_method(rb_cDBM, "initialize", fsdbm_initialize, -1); + rb_define_method(rb_cDBM, "close", fsdbm_close, 0); + rb_define_method(rb_cDBM, "closed?", fsdbm_closed, 0); + rb_define_method(rb_cDBM, "[]", fsdbm_aref, 1); + rb_define_method(rb_cDBM, "fetch", fsdbm_fetch_m, -1); + rb_define_method(rb_cDBM, "[]=", fsdbm_store, 2); + rb_define_method(rb_cDBM, "store", fsdbm_store, 2); + rb_define_method(rb_cDBM, "index", fsdbm_index, 1); + rb_define_method(rb_cDBM, "key", fsdbm_key, 1); + rb_define_method(rb_cDBM, "select", fsdbm_select, 0); + rb_define_method(rb_cDBM, "values_at", fsdbm_values_at, -1); + rb_define_method(rb_cDBM, "length", fsdbm_length, 0); + rb_define_method(rb_cDBM, "size", fsdbm_length, 0); + rb_define_method(rb_cDBM, "empty?", fsdbm_empty_p, 0); + rb_define_method(rb_cDBM, "each", fsdbm_each_pair, 0); + rb_define_method(rb_cDBM, "each_value", fsdbm_each_value, 0); + rb_define_method(rb_cDBM, "each_key", fsdbm_each_key, 0); + rb_define_method(rb_cDBM, "each_pair", fsdbm_each_pair, 0); + rb_define_method(rb_cDBM, "keys", fsdbm_keys, 0); + rb_define_method(rb_cDBM, "values", fsdbm_values, 0); + rb_define_method(rb_cDBM, "shift", fsdbm_shift, 0); + rb_define_method(rb_cDBM, "delete", fsdbm_delete, 1); + rb_define_method(rb_cDBM, "delete_if", fsdbm_delete_if, 0); + rb_define_method(rb_cDBM, "reject!", fsdbm_delete_if, 0); + rb_define_method(rb_cDBM, "reject", fsdbm_reject, 0); + rb_define_method(rb_cDBM, "clear", fsdbm_clear, 0); + rb_define_method(rb_cDBM,"invert", fsdbm_invert, 0); + rb_define_method(rb_cDBM,"update", fsdbm_update, 1); + rb_define_method(rb_cDBM,"replace", fsdbm_replace, 1); + + rb_define_method(rb_cDBM, "has_key?", fsdbm_has_key, 1); + rb_define_method(rb_cDBM, "include?", fsdbm_has_key, 1); + rb_define_method(rb_cDBM, "key?", fsdbm_has_key, 1); + rb_define_method(rb_cDBM, "member?", fsdbm_has_key, 1); + rb_define_method(rb_cDBM, "has_value?", fsdbm_has_value, 1); + rb_define_method(rb_cDBM, "value?", fsdbm_has_value, 1); + + rb_define_method(rb_cDBM, "to_a", fsdbm_to_a, 0); + rb_define_method(rb_cDBM, "to_hash", fsdbm_to_hash, 0); +} diff --git a/jni/ruby/ext/sdbm/sdbm.h b/jni/ruby/ext/sdbm/sdbm.h new file mode 100644 index 0000000..c3b53c4 --- /dev/null +++ b/jni/ruby/ext/sdbm/sdbm.h @@ -0,0 +1,86 @@ +/* + * sdbm - ndbm work-alike hashed database library + * based on Per-Ake Larson's Dynamic Hashing algorithms. BIT 18 (1978). + * author: oz@nexus.yorku.ca + * status: public domain. + */ +#ifndef _SDBM_H_ +#define _SDBM_H_ + +#include + +#define DBLKSIZ 4096 +#define PBLKSIZ 1024 +#define PAIRMAX 1008 /* arbitrary on PBLKSIZ-N */ +#define SPLTMAX 10 /* maximum allowed splits */ + /* for a single insertion */ +#define DIRFEXT ".dir" +#define PAGFEXT ".pag" + +typedef struct { + int dirf; /* directory file descriptor */ + int pagf; /* page file descriptor */ + int flags; /* status/error flags, see below */ + int keyptr; /* current key for nextkey */ + off_t maxbno; /* size of dirfile in bits */ + long curbit; /* current bit number */ + long hmask; /* current hash mask */ + long blkptr; /* current block for nextkey */ + long blkno; /* current page to read/write */ + long pagbno; /* current page in pagbuf */ + char pagbuf[PBLKSIZ]; /* page file block buffer */ + long dirbno; /* current block in dirbuf */ + char dirbuf[DBLKSIZ]; /* directory file block buffer */ +} DBM; + +#define DBM_RDONLY 0x1 /* data base open read-only */ +#define DBM_IOERR 0x2 /* data base I/O error */ + +/* + * utility macros + */ +#define sdbm_rdonly(db) ((db)->flags & DBM_RDONLY) +#define sdbm_error(db) ((db)->flags & DBM_IOERR) + +#define sdbm_clearerr(db) ((db)->flags &= ~DBM_IOERR) /* ouch */ + +#define sdbm_dirfno(db) ((db)->dirf) +#define sdbm_pagfno(db) ((db)->pagf) + +typedef struct { + char *dptr; + int dsize; +} datum; + +extern datum nullitem; + +#if defined(__STDC__) +#define proto(p) p +#else +#define proto(p) () +#endif + +/* + * flags to sdbm_store + */ +#define DBM_INSERT 0 +#define DBM_REPLACE 1 + +/* + * ndbm interface + */ +extern DBM *sdbm_open proto((char *, int, int)); +extern void sdbm_close proto((DBM *)); +extern datum sdbm_fetch proto((DBM *, datum)); +extern int sdbm_delete proto((DBM *, datum)); +extern int sdbm_store proto((DBM *, datum, datum, int)); +extern datum sdbm_firstkey proto((DBM *)); +extern datum sdbm_nextkey proto((DBM *)); + +/* + * other + */ +extern DBM *sdbm_prep proto((char *, char *, int, int)); +extern long sdbm_hash proto((char *, int)); + +#endif /* _SDBM_H_ */ -- cgit v1.2.3