[mw-devel] MW3 r1060 - trunk/src
arthur at sucs.org
arthur at sucs.org
Sat Nov 8 18:27:18 GMT 2008
Author: arthur
Date: 2008-11-08 18:27:18 +0000 (Sat, 08 Nov 2008)
New Revision: 1060
Removed:
trunk/src/db.c
trunk/src/db.h
trunk/src/db_new.c
trunk/src/dbglue.c
trunk/src/dbglue.h
trunk/src/dbutil.h
trunk/src/gnudb.c
trunk/src/gnudb.h
trunk/src/mwdb.c
trunk/src/mwdb.h
trunk/src/test.c
Modified:
trunk/src/Makefile
Log:
remove obsolete and unused db code
Modified: trunk/src/Makefile
===================================================================
--- trunk/src/Makefile 2008-10-24 11:58:53 UTC (rev 1059)
+++ trunk/src/Makefile 2008-11-08 18:27:18 UTC (rev 1060)
@@ -68,7 +68,7 @@
-include $(CODE:.c=.d)
-.PHONY: all install clean wipe test
+.PHONY: all install clean wipe
messages.po: $(CODE) $(HDRS)
xgettext --copyright-holder="Justin Mitchell <arthur at sucs.org>" -E --no-wrap --keyword=_ $^ -j -o messages.po
@@ -115,9 +115,6 @@
clean:
-rm -f *.o *.d red mw del_user
-test: test.o db.o dbglue.o mwdb.o gnudb.o
- $(CC) $(LDFLAGS) -lgdbm -o $@ $^
-
export: export.o perms.o strings.o special.o
$(CC) $(LDFLAGS) -o $@ $^
Deleted: trunk/src/db.c
===================================================================
--- trunk/src/db.c 2008-10-24 11:58:53 UTC (rev 1059)
+++ trunk/src/db.c 2008-11-08 18:27:18 UTC (rev 1060)
@@ -1,144 +0,0 @@
-/************************************************************************
-** Section: Milliways Database abstraction layer **
-** Author: Justin **
-** Summary: abstract the details of files and formats away from the **
-** main code to allow greater flexibility in the data struct- **
-** ures, and migration to client-server **
-************************************************************************/
-
-/**
- * Changes
- *
- * 27/12/01 (finnw) Moved the (small) mw-specific part of this module
- * to mw_newdb.c. Added 'db_setimpl' function to choose
- * the implementation and table types at startup.
- **/
-
-/***
-Function summaries:
-
-BDImpl db_set_impl ( DBImpl newImpl )
- Sets the current database implementation and table list, which is used
- by db_open() to determine the correct file location and/or format.
- Returns the old implementation, normally NULL if this function is
- called at startup.
-
-DB * db_open( char * name )
-
- Connects to the requested database and returns an identifier
- which is used by all subsequence actions.
- name is symbolic, the real path is to be hidden
-
-void * db_get(DB * db, void * oid, char *field )
-
- Fetches the contents of the named field of the given object,
- return NULL for error, otherwise return a malloc'ed string
-
-int db_set(DB * db, void * oid, char * field, int mode, char * value )
-
- Sets the value of field for the object to the value,
- mode is one of the following modes of operation
- DB_SET_ADD treat as a set and turn on these flags
- DB_SET_DEL treat as a set and turn off these flags
- DB_SET replace/create fields contents
- DB_CREATE create field, fail if already exists
- DB_REMOVE ignore content and delete field
- DB_APPEND append new content, creating if necessary
- returns the new/current DBoid on success, or 0 on failure.
-
-int db_match (DB *db, char *oid, char *field, int mode, char *value )
-
- Performs comparisons on the contents of a named field,
- returning true or false values.
- mode is a collection of the following modes and modifiers
-
- DB_SET_ALL treate as a set, are all these flags set
- DB_SET_ANY treate as a set, are any of these flags set
- DB_SET_NONE treat as a set, are none of these flags set
- DB_SET_EXACT treat as a set, are only these flags set
- DB_SUBST treat as string, does it contain this substring
- DB_CMP treat as string, does it match
- DB_LT, DB_GT, DB_EQ, DB_LE, DB_GE, DB_NE
- treat as number, < > == <= >= !=
- modifier: DB_ICASE ignore case on set or string match
-
-int db_list(DB db, char *field, int mode, char *value, DBoid result[], int nresult);
-
- Inserts at most 'num' oid's int result for objects in dbase that match
- the field and value criteria. returns the number of matches
- found. unused results will be set to 0
- uses same comparison flags as db_match
-
-Additional Notes:
-
-DB type is a pointer to the structure which holds details about
-the database file that has opened.
-
-****/
-
-
-#include <stdio.h>
-#include <fcntl.h>
-#include <errno.h>
-#include <string.h>
-#include <stdlib.h>
-#include <sys/file.h>
-
-#include "db.h"
-#include "dbglue.h"
-
-static DBImpl dbtypes = NULL;
-
-DBImpl db_set_impl( DBImpl newImpl )
-{
- DBImpl oldImpl = dbtypes;
- dbtypes = newImpl;
- return oldImpl;
-}
-
-DB db_open( char *name )
-{
- struct db_file_table *func;
- DB new;
-
- func=dbtypes;
- while (func!=NULL && func->name!=NULL
- && strcasecmp(name, func->name)!=0 ) {
- func++;
- }
-
- if (func==NULL || func->name==NULL) {
-#ifdef DEBUG
- fprintf(stderr, "Cant find database type for file %s\n", name);
-#endif
- return(NULL);
- }
-
- new = malloc(sizeof(struct db_file));
- new->type = func;
- new->file = func->open( func->filename );
-
- if (new->file == NULL) {
- free(new);
- return(NULL);
- }
- return(new);
-}
-
-int db_close( DB file )
-{
- if (file==NULL) return(-1);
- return ( file->type->close( file->file ) );
-}
-
-void * db_get(DB file, void *oid, void *field)
-{
- if (file==NULL) return(NULL);
- return ( file->type->get( file, oid, field ) );
-}
-
-int db_set(DB file, void *oid, void *field, int mode, char *value)
-{
- if (file==NULL) return(-1);
- return ( file->type->set( file, oid, field, mode, value ) );
-}
Deleted: trunk/src/db.h
===================================================================
--- trunk/src/db.h 2008-10-24 11:58:53 UTC (rev 1059)
+++ trunk/src/db.h 2008-11-08 18:27:18 UTC (rev 1060)
@@ -1,52 +0,0 @@
-/***
- *** db type definitions
- ***/
-
-typedef struct db_file_table {
- char *name;
- char *filename;
-
- void * (* open)();
- void * (* get)();
- int (* set)();
- int (* close)();
-} *DBImpl;
-
-typedef struct db_file {
- struct db_file_table * type;
- void * file;
-} * DB;
-
-
-/***
- *** Flags and Modes
- ***/
-/* modes for db_set only. */
-#define DB_SET_ADD 1
-#define DB_SET_DEL 2
-#define DB_SET 3
-#define DB_CREATE 4
-#define DB_REMOVE 5
-#define DB_APPEND 6
-
-/* modes for db_match and db_list */
-#define DB_SET_ALL 0x0001
-#define DB_SET_ANY 0x0002
-#define DB_SET_NONE 0x0003
-#define DB_SET_EXACT 0x0004
-#define DB_SUBST 0x0005
-#define DB_CMP 0x0006
-#define DB_NUM_LT 0x0010
-#define DB_NUM_GT 0x0020
-#define DB_NUM_LE 0x0030
-#define DB_NUM_GE 0x0040
-#define DB_NUM_EQ 0x0050
-#define DB_NUM_NE 0x0060
-#define DB_ICASE 0x1000
-
-/* db.c */
-DBImpl db_setimpl(DBImpl newImpl);
-DB db_open(char *name);
-int db_close(DB file);
-void *db_get(DB file, void *oid, void *field);
-int db_set(DB file, void *oid, void *field, int mode, char *value);
Deleted: trunk/src/db_new.c
===================================================================
--- trunk/src/db_new.c 2008-10-24 11:58:53 UTC (rev 1059)
+++ trunk/src/db_new.c 2008-11-08 18:27:18 UTC (rev 1060)
@@ -1,13 +0,0 @@
-/************************************************************************
-** Section: Milliways Database abstraction layer **
-** Author: Finn **
-** Summary: database function bindings to use the new database **
-** functions (gnudb/mwdb). **
-************************************************************************/
-
-struct db_file_table db_types3[]= {
-{ "user", "bbuser", db_text_open, db_text_get, db_text_set, db_text__close },
-{ "who", "bbsession", db_text_open, db_oid_get, db_oid_set, db_text_close },
-{ NULL, NULL, NULL, NULL, NULL }
-};
-
Deleted: trunk/src/dbglue.c
===================================================================
--- trunk/src/dbglue.c 2008-10-24 11:58:53 UTC (rev 1059)
+++ trunk/src/dbglue.c 2008-11-08 18:27:18 UTC (rev 1060)
@@ -1,111 +0,0 @@
-/*** MW Database Glue
- ***
- *** Sticks the Neutral API to the Backends
- ***
- *** NB: Uses a lot of typecasting things to (void *)
- *** to hide differences between numbers and strings
- *** from the outside world.
- ***/
-
-#include <stdio.h>
-#include "bb.h"
-#include "db.h"
-#include "gnudb.h"
-#include "mwdb.h"
-
-/* we only need the one open function right now,
- * as the mwdb file is common to both
- */
-void *db_text_open(char *filename)
-{
- int file;
- char name[1024];
-
- snprintf(name, 1023, "%s/db/%s.db", HOMEPATH, filename);
- if ((file=mwdb_open(name)) <0)
- return(NULL);
-
- return((void *)file);
-}
-
-int db_text_close(void *file)
-{
- return(close((int)file));
-}
-
-/* First set, functions to deal with double-hashed files,
- * ie. files requiring both keys to be converted from
- * text to a number before lookup
- */
-
-void *db_text_get(DB file, void *oid, void *field)
-{
- char filename[1024];
- int key, fld;
- char *answer;
-
- snprintf(filename, 1023, "%s/db/%s.key", HOMEPATH, file->type->filename);
- if ((key=gnudb_get(filename, oid))<0) {
- printf("Cant find key %s in %s\n", (char *)oid, (char *)filename);
- return(NULL);
- }
- snprintf(filename, 1023, "%s/db/%s.field", HOMEPATH, file->type->filename);
- if ((fld=gnudb_get(filename, field))<0) {
- printf("Cant find fieldname %s in %s\n", (char *)field, filename);
- return(NULL);
- }
- if ((answer=mwdb_get((int)file->file, key, fld))==NULL) {
- printf("No answer\n");
- return(NULL);
- }
- return(answer);
-}
-
-int db_text_set(DB file, void *oid, void *field, int mode, char *value)
-{
- char filename[1024];
- int key, fld;
-
- snprintf(filename, 1023, "%s/db/%s.key", HOMEPATH, file->type->filename);
- key=gnudb_newkey(filename, oid);
- snprintf(filename, 1023, "%s/db/%s.field", HOMEPATH, file->type->filename);
- fld=gnudb_newkey(filename, field);
-
- return(mwdb_set((int)file->file, key, fld, value));
-}
-
-/* Second set, functions to deal with single-hashed files
- * when the first key is an int, and the second text
- */
-
-void *db_oid_get(DB file, void *oid, void* field)
-{
- char filename[1024];
- int key, fld;
- char *answer;
-
- key=(uint)oid;
-
- snprintf(filename, 1023, "%s/db/%s.field", HOMEPATH, file->type->filename);
- if ((fld=gnudb_get(filename, field))<0) {
- printf("Cant find fieldname %s in %s\n", (char *)field, filename);
- return(NULL);
- }
- if ((answer=mwdb_get((int)file->file, key, fld))==NULL) {
- printf("No answer\n");
- return(NULL);
- }
- return(answer);
-}
-
-int db_oid_set(DB file, void *oid, void *field, int mode, char *value)
-{
- char filename[1024];
- int key, fld;
-
- key=(uint)oid;
- snprintf(filename, 1023, "%s/db/%s.field", HOMEPATH, file->type->filename);
- fld=gnudb_newkey(filename, field);
-
- return(mwdb_set((int)file->file, key, fld, value));
-}
Deleted: trunk/src/dbglue.h
===================================================================
--- trunk/src/dbglue.h 2008-10-24 11:58:53 UTC (rev 1059)
+++ trunk/src/dbglue.h 2008-11-08 18:27:18 UTC (rev 1060)
@@ -1,7 +0,0 @@
-/* dbglue.c */
-void *db_text_open(char *filename);
-int db_text_close(void *file);
-void *db_text_get(DB file, void *oid, void *field);
-int db_text_set(DB file, void *oid, void *field, int mode, char *value);
-void *db_oid_get(DB file, void *oid, void *field);
-int db_oid_set(DB file, void *oid, void *field, int mode, char *value);
Deleted: trunk/src/dbutil.h
===================================================================
--- trunk/src/dbutil.h 2008-10-24 11:58:53 UTC (rev 1059)
+++ trunk/src/dbutil.h 2008-11-08 18:27:18 UTC (rev 1060)
@@ -1,33 +0,0 @@
-#ifndef DBUTIL_H
-#define DBUTIL_H
-
-/* Database field types */
-#define DBF_STRING 0 /* Field is a string */
-#define DBF_NUM 1 /* Field is numeric */
-#define DBF_SET 2 /* Field is a set */
-#define DBF_INT DBF_NUM /* Only one numeric type, for now */
-#define DBF_CHRSET (DBF_STRING|DBF_SET)
-#define DBF_OID DBF_INT /* Pointer into same/other table */
-
-/* Error codes for database operations */
-extern int db_errno;
-
-/* Errors similar to some in libc (thus hopefully easy to remember) */
-#define DB_EPERM 1 /* User permissions don't allow operation */
-#define DB_ENOENT 2 /* Primary key not found */
-#define DB_EBADF 9 /* DB->file is invalid */
-#define DB_EAGAIN 11 /* Non-blocking lock failed: really EAGAIN */
-#define DB_EACCES 13 /* User permissions don't allow operation */
-/* Duplicate primary key, or field exists (with DB_CREATE) */
-#define DB_EEXIST 17
-/* Write would exceed the limit of a file format, e.g. trying to create
- * a 65th folder */
-#define DB_EFBIG 27
-#define DB_ENOSYS 38 /* Operation not implemented */
-
-/* Errors unrelated to any libc errors */
-#define DB_ESYS 1000 /* I/O operation failed; refer to errno */
-/* Type mismatch, e.g. DB_SET_DEL on a field marked as numeric */
-#define DB_ETYPE 1001
-
-#endif /* DBUTIL_H */
Deleted: trunk/src/gnudb.c
===================================================================
--- trunk/src/gnudb.c 2008-10-24 11:58:53 UTC (rev 1059)
+++ trunk/src/gnudb.c 2008-11-08 18:27:18 UTC (rev 1060)
@@ -1,138 +0,0 @@
-#include <fcntl.h>
-#include <stdio.h>
-#include <errno.h>
-#include <stdlib.h>
-#include <string.h>
-#include <gdbm.h>
-
-#include "gnudb.h"
-
-/***
- *** Milliways Database backend for gdb
- ***
- *** These are the low level access functions for the hashing indexes
- *** used to convert the text labeled object-id and fieldnames into the
- *** numerical indexes required by the central core.
- ***
- *** As this is a simple name -> mapping we can utilise existing system
- *** libraries, in this case gdbm, which are already optimised and have
- *** good hashing and indexing functions.
- ***
- *** Locking is inherant in the access routines, meaning that leaving
- *** the file open in r/w mode would block other accesses, so for now
- *** we open it as required.
- ***/
-
-#ifdef DEBUG
-main()
-{
- unsigned int i;
-
- i=gnudb_newkey("useri", "username");
- printf("Added key 'username' at index %d\n", i);
- i=gnudb_newkey("useri", "status");
- printf("Added key 'status' at index %d\n", i);
-}
-#endif
-
-/* Create a new key, and return its number */
-unsigned int gnudb_newkey(char *filename, char *key)
-{
- GDBM_FILE ff;
- datum newkey, index;
- int *serial, i;
-
- if (key==NULL || *key==0) {
- fprintf(stderr, "gnudb_set: Bad key\n");
- return(-1);
- }
-
- if ((ff=gdbm_open(filename, 1024, GDBM_WRCREAT, 0600, NULL))==NULL) {
- fprintf(stderr, "gdbm_open failed for %s: %s\n",
- filename, gdbm_strerror(gdbm_errno));
- return(-1);
- }
-
- newkey.dptr=key;
- newkey.dsize=strlen(key)+1;
- index=gdbm_fetch(ff, newkey);
- if (index.dptr!=NULL) { /* already there */
- memcpy(&i, index.dptr, index.dsize);
- free(index.dptr);
- gdbm_close(ff);
-#ifdef DEBUG
- printf("Duplicate of %d\n", i);
-#endif
- return(i); /* pretend we added it */
- }
-
- /* first find the index element for assigning data numbers */
- newkey.dptr="##index";
- newkey.dsize=strlen(newkey.dptr)+1;
- index=gdbm_fetch(ff, newkey);
- if (index.dptr==NULL) { /* make a new index */
- serial=(int *)malloc(sizeof(*serial));
- *serial=0;
- index.dptr=(char *)serial;
- index.dsize=sizeof(*serial);
-#ifdef DEBUG
- printf("Making new index\n", *serial);
-#endif
- }
-
- /* add one to the index and write it back */
- serial=(int *)index.dptr;
-#ifdef DEBUG
- printf("Index was %d ", *serial);
-#endif
- (*serial)++;
-#ifdef DEBUG
- printf("now %d\n", *serial);
-#endif
- if (gdbm_store(ff, newkey, index, GDBM_REPLACE)) {
- fprintf(stderr, "Index update failed: %s\n",
- gdbm_strerror(gdbm_errno));
- }
-
- newkey.dptr=key;
- newkey.dsize=strlen(key)+1;
- if (gdbm_store(ff, newkey, index, GDBM_INSERT)) {
- fprintf(stderr, "New key insert failed, argh... %s\n",
- gdbm_strerror(gdbm_errno));
- }
-
- i=*serial;
- free(index.dptr);
- gdbm_close(ff);
- return(i);
-}
-
-unsigned int gnudb_get(char *filename, char *key)
-{
- GDBM_FILE ff;
- datum newkey, index;
- int i;
-
- if (key==NULL || *key==0) {
- fprintf(stderr, "gnudb_get: Bad key\n");
- return(-1);
- }
-
- if ((ff=gdbm_open(filename, 1024, GDBM_READER, 0600, NULL))==NULL) {
- fprintf(stderr, "gdbm_open failed for %s: %s\n",
- filename, gdbm_strerror(gdbm_errno));
- return(-1);
- }
-
- newkey.dptr=key;
- newkey.dsize=strlen(newkey.dptr)+1;
- index=gdbm_fetch(ff, newkey);
- if (index.dptr!=NULL) {
- memcpy(&i, index.dptr, index.dsize);
- free(index.dptr);
- return(i); /* found index number */
- }else
- return(0); /* not found */
- gdbm_close(ff);
-}
-
Deleted: trunk/src/gnudb.h
===================================================================
--- trunk/src/gnudb.h 2008-10-24 11:58:53 UTC (rev 1059)
+++ trunk/src/gnudb.h 2008-11-08 18:27:18 UTC (rev 1060)
@@ -1,3 +0,0 @@
-/* gnudb.c */
-unsigned int gnudb_newkey(char *filename, char *key);
-unsigned int gnudb_get(char *filename, char *key);
Deleted: trunk/src/mwdb.c
===================================================================
--- trunk/src/mwdb.c 2008-10-24 11:58:53 UTC (rev 1059)
+++ trunk/src/mwdb.c 2008-11-08 18:27:18 UTC (rev 1060)
@@ -1,308 +0,0 @@
-#include <stdio.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <fcntl.h>
-#include <errno.h>
-#include <string.h>
-#include <sys/file.h>
-
-#include "mwdb.h"
-
-/***
- *** Milliways Database backend, main data store
- ***
- *** These are the low level access functions for the central core
- *** Milliways database system, which contains two numerical keys
- *** and an arbitrary length data field.
- ***
- *** Data is stored in multiples of a fixed blocksize (32 bytes) which allows
- *** for small changes in the size of a piece of data without having to
- *** constantly move it around.
- ***
- *** The two keys are unsigned integers, with the 0 (zero) value reserved
- *** to indicate unused/deleted sections, the combination of the two keys
- *** must be unique, and are generally taken to be an object-id (eg. username)
- *** and a field reference number.
- ***
- *** Record structure
- ***
- *** unsigned integer record size (multiples of blocksize)
- *** unsigned integer oid object id number (zero for empty)
- *** unsigned integer fieldno field name identifier
- *** unsigned integer datasize size of the actual data
- *** char [] data datasize bytes of data.
- *** zeros padding zero padding, (size-16-datasize bytes)
- ***
- *** When an record is added, or its contents have grown to more than
- *** the existing record size, that record is marked as empty, and the
- *** contents placed in the first available empty record of the correct
- *** new size, or is appended to the end of the file.
- ***
- *** Data is stored in an unordered fashion, and so all searches are linear,
- *** but fast, due to only requiring the first three numbers (12 bytes)
- *** to be read, the rest can be skipped with a seek as we know the record
- *** size.
- ***
- *** Locking: exclusive locks requested when modifying the file,
- *** and shared locks requested when just reading, so there can
- *** only be one writer, or, lots of readers at a time.
- ***/
-
-#ifdef DEBUG
-int main(int argc, char **argv)
-{
- int ff;
- unsigned int oid, key;
- char *value;
-
- if ((ff=mwdb_open("user.db"))<0) exit(0);
-
- mwdb_set(ff, 1, 1, "hello");
- value=mwdb_get(ff, 1, 1);
- if (value) {
- printf("Test returns okay '%s'\n", value);
- free(value);
- }else
- printf("Test fails\n");
- mwdb_delete(ff, 1, 1);
- mwdb_list(ff);
- mwdb_close(ff);
-
- return(0);
-}
-#endif
-
-int mwdb_open(char *file)
-{
- int ff;
- if ((ff=open(file, O_RDWR|O_CREAT, 0600))<0) {
- fprintf(stderr, "%s error: %s\n", file, strerror(errno));
- return(-1);
- }
- return(ff);
-}
-
-int mwdb_close(int ff)
-{
- return close(ff);
-}
-
-int mwdb_set(int ff, unsigned int oid, unsigned int key, char *value)
-{
- char *val;
- int size, vs, vals;
- int ss, so, sk;
- off_t emptyslot=0;
- int done=0;
-
- if (value==NULL) return(-1);
- vs=strlen(value);
-
- if (!oid) {printf("Error: Invalid oid value\n"); return(-1); }
- if (!key) {printf("Error: Invalid key value\n"); return(-1); }
- if (!vs) printf("Warning: Empty value\n");
-
- size= (sizeof(int) * 4) + vs;
- size+= MWDB_BLOCKSIZE-(size%MWDB_BLOCKSIZE);
- vals= size - (sizeof(int) * 4);
- val=(char *)malloc(size+1);
- strcpy(val, value);
-
-#ifdef DEBUG
- printf("Scanning for existing record\n");
-#endif
-
- flock(ff, LOCK_EX);
- lseek(ff, 0, SEEK_SET);
- while (read(ff, &ss, sizeof(int))==sizeof(int)) {
- read(ff, &so, sizeof(int));
- read(ff, &sk, sizeof(int));
- if (so==0) {
-#ifdef DEBUG
- printf("Free block size %d ", ss);
-#endif
- if (ss==size && emptyslot==0) {
-#ifdef DEBUG
- printf("Recording.\n");
-#endif
- emptyslot=lseek(ff, 0, SEEK_CUR) - 2*sizeof(int);
- }else{
-#ifdef DEBUG
- printf("Ignoring.\n");
-#endif
- }
- }else
- if (so==oid && sk==key) {
-#ifdef DEBUG
- printf("Found existing. ");
-#endif
- if (ss<size) {
-#ifdef DEBUG
- printf("Too small, moving.\n");
-#endif
- /* delete current block */
- lseek(ff, -2*sizeof(int), SEEK_CUR);
- so=0;
- write(ff, &so, sizeof(so));
- write(ff, &so, sizeof(so));
-
- if (emptyslot) {
- /* we spotted a slot on the way in */
- lseek(ff, emptyslot, SEEK_SET);
- write(ff, &oid, sizeof(oid));
- write(ff, &key, sizeof(key));
- write(ff, &vs, sizeof(vs));
- write(ff, val, vals);
- done=1;
- break;
- } /* else wait for a better slot */
- }else { /* write the new version */
-#ifdef DEBUG
- printf("Overwriting.\n");
-#endif
- write(ff, &vs, sizeof(vs));
- write(ff, val, vals);
- done=1;
- break;
- }
- }
- lseek(ff, ss-(3*sizeof(int)), SEEK_CUR);
- }
-
- if (!done) {
-
- if (emptyslot) {
-#ifdef DEBUG
- printf("Reusing empty slot.\n");
-#endif
- lseek(ff, emptyslot, SEEK_SET);
- }else {
-#ifdef DEBUG
- printf("Appending entry at %d\n", lseek(ff, 0, SEEK_CUR));
-#endif
- write(ff, &size, sizeof(size));
- }
- write(ff, &oid, sizeof(oid));
- write(ff, &key, sizeof(key));
- write(ff, &vs, sizeof(vs));
- write(ff, val, vals);
- }
- flock(ff, LOCK_UN);
- free(val);
-
-#ifdef DEBUG
- printf("Written block=%d, oid=%X key=%X value='%s' (%d)\n",
- size, oid, key, value, vs);
-#endif
- return(0);
-}
-
-int mwdb_delete(int ff, unsigned int oid, unsigned int key)
-{
- unsigned int ss, so, sk;
- int rec=0, done=0;
-
- if (!oid) {printf("Error: Invalid oid value\n"); return(-1); }
- if (!key) {printf("Error: Invalid key value\n"); return(-1); }
-
- flock(ff, LOCK_EX);
- lseek(ff, 0, SEEK_SET);
- while (read(ff, &ss, sizeof(int))==sizeof(int)) {
- read(ff, &so, sizeof(int));
- read(ff, &sk, sizeof(int));
-
- rec++;
- if (so==oid && sk==key) {
- done++;
-#ifdef DEBUG
- printf("Deleting record %d key %d/%d size %d\n",
- rec, so, sk, ss);
-#endif
- lseek(ff, -2*sizeof(so), SEEK_CUR);
- so=0;
- write(ff, &so, sizeof(so));
- write(ff, &so, sizeof(so));
-
- }
- lseek(ff, ss - (3* sizeof(ss)), SEEK_CUR);
- }
- flock(ff, LOCK_UN);
-#ifdef DEBUG
- printf("Checked %d records, deleted %d.\n", rec, done);
-#endif
- return(done);
-}
-
-char *mwdb_get(int ff, unsigned int oid, unsigned int key)
-{
- unsigned int ss, so, sk, sv;
- char *value;
- int rec=0;
- int i;
-
- if (!oid) {printf("Error: Invalid oid value\n"); return(NULL); }
- if (!key) {printf("Error: Invalid key value\n"); return(NULL); }
-
- flock(ff, LOCK_SH);
- lseek(ff, 0, SEEK_SET);
- while ((i=read(ff, &ss, sizeof(int)))==sizeof(int)) {
- read(ff, &so, sizeof(so));
- read(ff, &sk, sizeof(sk));
- rec++;
- if (so==oid && sk==key) {
- read(ff, &sv, sizeof(sv));
- value=(char *)malloc(sv+1);
- read(ff, value, sv);
- value[sv]=0;
- flock(ff, LOCK_UN);
- return(value);
- }
- lseek(ff, ss - (3* sizeof(ss)), SEEK_CUR);
- }
- flock(ff, LOCK_UN);
- printf("read failed with %d - %s\n", i, strerror(errno));
- printf("mwdb_get: oid=%d key=%d not found in %d records.\n",
- oid, key, rec);
- return(NULL);
-}
-
-/** debugging function that lists entire database to stdout
- **/
-void mwdb_list(int ff)
-{
- unsigned int oid, ks, vs;
- unsigned int key;
- char *value;
- unsigned int size;
- int rec=0;
-
- flock(ff, LOCK_SH);
- lseek(ff, 0, SEEK_SET);
- printf (" 0 ");
- while ( read(ff, &size, sizeof(size)) == sizeof(size) ) {
- read(ff, &oid, sizeof(oid));
-
- if (oid==0) {
- printf("Empty block %d bytes\n", size);
- lseek(ff, size-(2*sizeof(int)), SEEK_CUR);
- }else {
- read(ff, &key, sizeof(key));
- read(ff, &vs, sizeof(vs));
-
- ks=size-(sizeof(int)*4);
- value=(char *)malloc(ks+1);
- read(ff, value, ks);
- value[vs]=0;
-
- printf("oid=%d key=%d value='%s' (%d) block=%d\n",
- oid, key, value, vs, size);
- free(value);
- }
- printf("%4d ", (uint)lseek(ff,0,SEEK_CUR));
-
- rec++;
- }
- flock(ff, LOCK_UN);
- printf("End.\n");
- printf("%d records read.\n", rec);
-}
Deleted: trunk/src/mwdb.h
===================================================================
--- trunk/src/mwdb.h 2008-10-24 11:58:53 UTC (rev 1059)
+++ trunk/src/mwdb.h 2008-11-08 18:27:18 UTC (rev 1060)
@@ -1,9 +0,0 @@
-
-#define MWDB_BLOCKSIZE 32
-
-/* mwdb.c prototypes */
-int mwdb_open(char *file);
-int mwdb_close(int ff);
-int mwdb_set(int ff, unsigned int oid, unsigned int key, char *value);
-int mwdb_delete(int ff, unsigned int oid, unsigned int key);
-char *mwdb_get(int ff, unsigned int oid, unsigned int key);
Deleted: trunk/src/test.c
===================================================================
--- trunk/src/test.c 2008-10-24 11:58:53 UTC (rev 1059)
+++ trunk/src/test.c 2008-11-08 18:27:18 UTC (rev 1060)
@@ -1,22 +0,0 @@
-#include <stdio.h>
-#include "db.h"
-
-int main(int argc, char *argv[])
-{
- DB ff;
- char *text;
-
- if ((ff=db_open("user"))==NULL) {
- printf("Failed open\n");
- exit(0);
- }
-
- db_set(ff, "arthur", "name", 0, "Justin Mitchell");
- text=db_get(ff, "arthur", "name");
- if (text==NULL)
- printf("Boo hiss, couldnt get the record.\n");
- else
- printf("It says the word is '%s'\n", text);
- free(text);
- db_close(ff);
-}
More information about the mw-devel
mailing list