home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Frostbyte's 1980s DOS Shareware Collection
/
floppyshareware.zip
/
floppyshareware
/
DOOG
/
CBASE09.ZIP
/
BTREE.ZIP
/
BTREE.H
< prev
next >
Wrap
Text File
|
1989-08-31
|
5KB
|
141 lines
/* Copyright (c) 1989 Citadel */
/* All Rights Reserved */
/* #ident "btree.h 1.1 - 89/07/03" */
/*man---------------------------------------------------------------------------
NAME
btree - B-tree file management library
SYNOPSIS
#include <btree.h>
DESCRIPTION
The btree library consists of a set of routines for the creation and
manipulation of file-based B-tree structures. The B+-tree variety
of B-tree is used to provide efficient sequential as well as random
access.
btree uses the blkio library for file access and buffering. Therefore
bexit should be used in place of exit when using btree.
SEE ALSO
btclose, btcreate, btcursor, btdelcur, btdelete, btfirst, btgetcur,
btgetk, btgetlck, btinsert, btkeycnt, btkeysize, btlast, btlock,
btnext, btopen, btprev, btsearch, btsetbuf, btsetcur, btsetvbuf,
btsync.
------------------------------------------------------------------------------*/
#ifndef BTREE_H /* prevent multiple includes */
#define BTREE_H
#include <blkio.h>
/* #include <stddef.h> */
/* btree constants */
#define BTOPEN_MAX BOPEN_MAX /* max # btrees open at once */
#define BTBUFCNT ((size_t)4) /* default # of nodes to buffer */
/* macro to calculate buffer size needed by a btree */
#define BTBUFSIZ(M, KEYSIZE, BUFCNT) (sizeof(bthdr_t) + ((BUFCNT) * \
(offsetof(btnode_t, key_p) + \
(((M) - 1) * (KEYSIZE)) + ((M) * sizeof(bpos_t)))))
/* btree type definitions */
typedef struct { /* btree position */
bpos_t node; /* block number of node */
size_t key; /* key number in node */
} btpos_t;
typedef struct { /* btree node */
size_t n; /* number of keys in node (n <= (m - 1)) */
bpos_t lsib; /* block number of left sibling */
bpos_t rsib; /* block number of right sibling */
void *key_p; /* pointer to m keys */
bpos_t *child_p; /* pointer to (m + 1) child block numbers */
} btnode_t;
typedef struct { /* (key, child) tuple */
void *key_p; /* pointer to one key */
bpos_t child; /* child block number */
} bttuple_t;
typedef struct { /* btree file header */
bpos_t flh; /* block number of free list head */
size_t m; /* m-way search tree */
size_t keysize; /* size of keys */
int flags; /* flags */
bpos_t root; /* block number of root node */
bpos_t first; /* block number of first key */
bpos_t last; /* block number of last key */
size_t keycnt; /* number of keys currently stored in btree */
size_t height; /* current height of tree */
} bthdr_t;
typedef struct { /* btree control structure */
bthdr_t bthdr; /* header record */
BLKFILE *bp; /* block file */
int (*cmp_p)(); /* key compare function */
int flags; /* status flags */
btpos_t cbtpos; /* current btree position */
btnode_t *cbtnp; /* current node */
btpos_t *sp; /* search path to current position */
} btree_t;
extern btree_t btb[BTOPEN_MAX]; /* btree control structure table declaration */
/* bthdr flag bits */
#define BTHMOD (01) /* btree file being modified */
/* btb flag bits */
#define BTOPEN (03) /* open status bits */
#define BTREAD (01) /* btree is open for reading */
#define BTWRITE (02) /* btree is open for writing */
#define BTLOCKS (030) /* lock status bits */
#define BTRDLCK (010) /* btree is read locked */
#define BTWRLCK (020) /* btree is write locked */
#define BTERR (0100) /* error has occurred on this btree */
/* function declarations */
int btclose(/* btree_t *btp */);
int btcreate(/* char *filename, size_t m, size_t keysize */);
#define btcursor(BTP) ((void *)(((BTP)->cbtpos.node == 0) ? NULL : ~NULL))
int btdelcur(/* btree_t *btp, void *buf */);
int btdelete(/* btree_t *btp, void *buf */);
int btfirst(/* btree_t *btp */);
int btgetcur(/* btree_t *btp, btpos_t *btpos_p);
int btgetk(/* btree_t *btp, void *buf */);
int btgetlck(/* btree_t *btp */);
int btinsert(/* btree_t *btp, void *buf */);
#define btkeycnt(BTP) ((BTP)->bthdr.keycnt)
#define btkeysize(BTP) ((BTP)->bthdr.keysize)
int btlast(/* btree_t *btp */);
int btlock(/* btree_t *btp, int l_type */);
int btnext(/* btree_t *btp */);
btree_t * btopen(/* char *filename, char *type, int (*cmp_p)() */);
int btprev(/* btree_t *btp */);
int btsearch(/* btree_t *btp, void *buf */);
int btsetbuf(/* btree_t *btp, void *buf */);
int btsetcur(/* btree_t *btp, btpos_t *btpos_p */);
int btsetvbuf(/* btree_t *btp, void *buf, size_t bufcnt */);
int btsync(/* btree_t *btp */);
/* btree lock types */
#define BT_UNLCK (0) /* unlock */
#define BT_RDLCK (1) /* read lock */
#define BT_WRLCK (2) /* write lock */
#define BT_RDLKW (3) /* read lock, wait */
#define BT_WRLKW (4) /* write lock, wait */
/* btree error codes */
#define BTEOS (-100) /* start of btree error code domain */
#define BTEMFILE (BTEOS - 1) /* too many open btrees */
#define BTECORRUPT (BTEOS - 2) /* btree is corrupt */
#define BTENOPEN (BTEOS - 3) /* btree is not open */
#define BTENBUF (BTEOS - 4) /* buffering is off */
#define BTELOCK (BTEOS - 5) /* incorrect lock type */
#define BTENKEY (BTEOS - 6) /* no key */
#define BTEDUPKEY (BTEOS - 7) /* duplicate key */
#define BTEPANIC (BTEOS - 10) /* internal btree error */
#endif /* #ifndef BTREE_H */