2010年4月26日

GDBM /NDBM 資料庫

GDBM - The GNU database manager,一套簡單的資料庫管理函數。

dbm 是 UNIX 上一套古老的常見資料庫處理介面,而 ndbm 則是改良 dbm 缺失而設計的,兩者的介面差異頗大。gdbm 則是根據ndbm 的介面設計的,兩者間非常相似, ndbm 的函數名稱皆以 dbm_ 開頭,而 gdbm 為了加以區別,則以 gdbm_ 開頭,並且利用hash 表存放鍵值,大幅節省搜尋時間,除此之外,皆可對應。其資料庫主要利用每一筆資料都有一對"鍵值"及"內容"。利用鍵值做三個動作{storing 儲存,retrieval 擷取,deletion 刪除}資料。

編譯設定

在 ubuntu 中,必須安裝「libgdbm-dev」套件。
使用 gdbm 的相關函式作法如下:

#include
編譯時加上 -l gdbm 以連結函式庫
使用 ndbm 的相關函式作法如下:
#include <gdbm-ndbm.h>
編譯時加上 -l gdbm_compat 以連結函式庫

SYNOPSIS
#include <gdbm.h>
extern gdbm_error
gdbm_errno
/*
0 GDBM_NO_ERROR No error


1 GDBM_MALLOC_ERROR Malloc error

2 GDBM_BLOCK_SIZE_ERROR Block size error

3 GDBM_FILE_OPEN_ERROR File open error

4 GDBM_FILE_WRITE_ERROR File write error

5 GDBM_FILE_SEEK_ERROR File seek error

6 GDBM_FILE_READ_ERROR File read error

7 GDBM_BAD_MAGIC_NUMBER Bad magic number

8 GDBM_EMPTY_DATABASE Empty database

9 GDBM_CANT_BE_READER Can't be reader

10 GDBM_CANT_BE_WRITER Can't be writer

11 GDBM_READER_CANT_DELETE Reader can't delete

12 GDBM_READER_CANT_STORE Reader can't store

13 GDBM_READER_CANT_REORGANIZE Reader can't reorganize

14 GDBM_UNKNOWN_UPDATE Unknown update

15 GDBM_ITEM_NOT_FOUND Item not found

16 GDBM_REORGANIZE_FAILED Reorganize failed

17 GDBM_CANNOT_REPLACE Cannot replace

18 GDBM_ILLEGAL_DATA Illegal data

19 GDBM_OPT_ALREADY_SET Option already set

20 GDBM_OPT_ILLEGAL Illegal option
*/

extern char
*gdbm_version

GDBM_FILE
gdbm_open (name, block_size, read_write, mode, fatal_func)
char * name;
int block_size, read_write, mode;
void (*fatal_func) ();

void
gdbm_close (dbf)
GDBM_FILE dbf;

int
gdbm_store (dbf, key, content, flag)
GDBM_FILE dbf;
datum key, content;
int flag;

datum
gdbm_fetch (dbf, key)
GDBM_FILE dbf;
datum key;

int
gdbm_delete (dbf, key)
GDBM_FILE dbf;
datum key;

datum
gdbm_firstkey (dbf)
GDBM_FILE dbf;

datum
gdbm_nextkey (dbf, key)
GDBM_FILE dbf;
datum key;

int
gdbm_reorganize (dbf)
GDBM_FILE dbf;

void
gdbm_sync (dbf)
GDBM_FILE dbf;

int
gdbm_exists (dbf, key)
GDBM_FILE dbf;
datum key;

char *
gdbm_strerror (errno)
gdbm_error errno;

int
gdbm_setopt (dbf, option, value, size)
GDBM_FILE dbf;
int option;
int *value;
int size;

int
gdbm_fdesc (dbf)
GDBM_FILE dbf;

DBM Compatability routines:

#include

int
dbminit (name)
char *name;

int
store (key, content)
datum key, content;

datum
fetch (key)
datum key;

int
delete (key)
datum key;

datum
firstkey ()

datum
nextkey (key)
datum key;

int
dbmclose ()

NDBM Compatability routines:

#include

DBM
*dbm_open (name, flags, mode)
char *name;
int flags, mode;

void
dbm_close (file)
DBM *file;

datum
dbm_fetch (file, key)
DBM *file;
datum key;

int
dbm_store (file, key, content, flags)
DBM *file;
datum key, content;
int flags;

int
dbm_delete (file, key)
DBM *file;
datum key;

datum
dbm_firstkey (file)
DBM *file;

datum
dbm_nextkey (file)
DBM *file;

int
dbm_error (file)
DBM *file;

int
dbm_clearerr (file)
DBM *file;

int
dbm_pagfno (file)
DBM *file;

int
dbm_dirfno (file)
DBM *file;

int
dbm_rdonly (file)
DBM *file;
Reference:
http://manpages.ubuntu.com/manpages/karmic/man3/gdbm.3gdbm.html
http://www.vivtek.com/gdbm/api.html

沒有留言: