Database Library
Dbopen() – opens all three types of files.
DB structure – Collection of function pointers.
#include<db.h>
Typedef struct_db{
DBTYPE type;
int (*close)(struct_db *);
int (*del)(const struct_db *, const DBT *,u_int);
int (*get)(const struct_db *, const DBT *,u_int);
int (*put)(const struct_db *, const DBT *,u_int);
int (*seq)(const struct_db *, const DBT *,u_int);
int (*sync)(const struct_db *, u_int);
int (*fd)(const struct_db *);
Void *internal;
}DB;
TYPES OFDATABASES
Hash tables For large amount
of
Btrees databases
(efficient)
Simple record number oriented access
All database types treated as key/value pairs.
DBT RECORD
#include<db.h>
typedef struct
{
Void *data;
Size_t size;
}DBT;
BASIC OPERATIONS
1. Opening a db file
DB *dbopen(char *filename, int flags, int
mode, DBTYPE type, const void *openinfo);
Flags and mode – same as file open.
Type – DB_HASH, DB_BTREE or DB_RECNO
Last parameter set to null and DB use default.
If dbopen success returns pointer
Otherwise returns NULL.
CLOSING A DATABASE
int close(const DB *db);
OBTAINING FILE DESCRIPTOR
File descriptor – for locking purpose
Int fd(DB *db);
File descriptor returned for the database file
always refers the same file. ----locking
SYNCING THE DATABASE
Int sync(const DB *db, unsigned int flags);
Db caches data in RAM
Kernal caches disk writes
To ensure On disk database is consistent
with buffered structures -- synchronize
Db flushes internal buffers and calls fsync().
Flags:
DB_HASH , DB_BTREE -0
DB_RECNO---- R_RECNOSYNC
READING RECORDS
2 ways
Looking up a record by its key
Reading sequential key/value pairs
Int seq(const DB *db, DBT *key, DBT *value,
unsigned int flags);
Flags ---
R_CURSOR
R_FIRST
R_NEXT
R_LAST
R_PREV
Reading a particular record
• Int get (const DB *db, const DBT *key, DBT *value,
unsigned int flags);
Modifying the database – adding & deleting
records
• int put(const DB *db, const DBT *key, DBT *value,
unsigned int flags);
• int del(const DB *db, const DBT *key, unsigned int
flags);