Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright 2001-2004 Brandon Long
      3  * All Rights Reserved.
      4  *
      5  * ClearSilver Templating System
      6  *
      7  * This code is made available under the terms of the ClearSilver License.
      8  * http://www.clearsilver.net/license.hdf
      9  *
     10  */
     11 
     12 #ifndef __WDB_H_
     13 #define __WDB_H_ 1
     14 
     15 #include "util/skiplist.h"
     16 #include "util/dict.h"
     17 #include "util/ulist.h"
     18 #include <db.h>
     19 
     20 typedef struct _column
     21 {
     22   char *name;
     23   int ondisk_index;       /* index# on disk, constant for life of db,
     24 			     must be 1 or higher */
     25   int inmem_index;        /* load time specific, needs to be flushed on
     26 				 alter table */
     27   char type;
     28 } WDBColumn;
     29 
     30 typedef struct _row
     31 {
     32   int table_version;     /* random number which maps to the same number
     33 			    of the table defn when loaded to verify they
     34 			    match */
     35   char *key_value;
     36   int data_count;
     37   void *data[1];
     38 } WDBRow;
     39 
     40 typedef struct _cursor
     41 {
     42   int table_version;     /* random number which maps to the same number
     43 			    of the table defn when loaded to verify they
     44 			    match */
     45   DBC *db_cursor;
     46 } WDBCursor;
     47 
     48 typedef struct _wdb
     49 {
     50   char *name;
     51   char *key;
     52   char *path;
     53   dictCtx attrs;
     54   dictCtx cols;
     55   skipList ondisk;
     56   ULIST *cols_l;
     57   DB *db;
     58   int last_ondisk;
     59   int defn_dirty;        /* must save defn on destroy */
     60   int table_version;     /* random number which maps to the same number
     61 			    of the table defn when loaded/changed to
     62 			    verify they match */
     63 } WDB;
     64 
     65 
     66 #define WDB_TYPE_STR 's'
     67 #define WDB_TYPE_INT 'i'
     68 
     69 #define WDBC_FIRST (1<<0)
     70 #define WDBC_NEXT  (1<<1)
     71 #define WDBC_FIND  (1<<2)
     72 
     73 #define WDBR_INSERT (1<<0)
     74 
     75 NEOERR * wdb_open (WDB **wdb, const char *name, int flags);
     76 NEOERR * wdb_save (WDB *wdb);
     77 NEOERR * wdb_update (WDB *wdb, const char *name, const char *key);
     78 NEOERR * wdb_create (WDB **wdb, const char *path, const char *name,
     79                      const char *key, ULIST *col_def, int flags);
     80 void wdb_destroy (WDB **wdb);
     81 NEOERR * wdb_column_insert (WDB *wdb, int loc, const char *key, char type);
     82 NEOERR * wdb_column_delete (WDB *wdb, const char *name);
     83 NEOERR * wdb_column_update (WDB *wdb, const char *oldkey, const char *newkey);
     84 NEOERR * wdb_column_exchange (WDB *wdb, const char *key1, const char *key2);
     85 
     86 /*
     87  * function: wdb_keys
     88  * description: this function returns the key and column names for the
     89  *              current database
     90  * input: wdb - open database
     91  * output: primary_key - pointer to the primary key
     92  *         data - pointer to a ULIST of the columns.
     93  *         both of these are allocated structures, you can clear data
     94  *         with uListDestroy (data, ULIST_FREE)
     95  * return: STATUS_OK on no error or egerr.h error
     96  */
     97 NEOERR * wdb_keys (WDB *wdb, char **primary_key, ULIST **data);
     98 
     99 NEOERR * wdb_attr_get (WDB *wdb, const char *key, char **value);
    100 NEOERR * wdb_attr_set (WDB *wdb, const char *key, const char *value);
    101 NEOERR * wdb_attr_next (WDB *wdb, char **key, char **value);
    102 NEOERR * wdbr_lookup (WDB *wdb, const char *key, WDBRow **row);
    103 NEOERR * wdbr_create (WDB *wdb, const char *key, WDBRow **row);
    104 NEOERR * wdbr_save (WDB *wdb, WDBRow *row, int flags);
    105 NEOERR * wdbr_delete (WDB *wdb, const char *key);
    106 NEOERR * wdbr_destroy (WDB *wdb, WDBRow **row);
    107 NEOERR * wdbr_get (WDB *wdb, WDBRow *row, const char *key, void **value);
    108 NEOERR * wdbr_set (WDB *wdb, WDBRow *row, const char *key, void *value);
    109 NEOERR * wdbr_dump (WDB *wdb, WDBRow *row);
    110 NEOERR * wdbr_next (WDB *wdb, WDBCursor *cursor, WDBRow **row, int flags);
    111 NEOERR * wdbr_find (WDB *wdb, WDBCursor *cursor, const char *key, WDBRow **row);
    112 NEOERR * wdbc_create (WDB *wdb, WDBCursor **cursor);
    113 NEOERR * wdbc_destroy (WDB *wdb, WDBCursor **cursor);
    114 
    115 #endif /* __WDB_H_ */
    116