cbaseTM
The C Database Library
Citadel
Brookville, IndianaCopyright (c) 1989, 1991 Citadel
All rights reserved
Citadel Software, Inc.
241 East Eleventh Street
Brookville, IN 47012
317-647-4720
BBS 317-647-2403
Version 1.0.2
This manual is protected by United States copyright law. No part of it
may be reproduced without the express written permission of Citadel.
Technical Support
The Citadel BBS is available 24 hours a day. Voice support is available
between 10 a.m. and 4 p.m. EST. When calling for technical support,
please have ready the following information:
- product name and version number
- operating system and version number
- C compiler and version number
- computer brand and model
UNIX is a trademark of AT&T. Turbo C is a trademark of Borland
International, Inc.
Contents
Introduction 1
Chapter 1. A Tutorial Introduction 3
1.1 Defining a Database
1.2 Using the cbase Library
Chapter 2. cbase Architecture 9
Chapter 3. The Data Definition Language 11
Chapter 4. cbase Library Functions 13
4.1 Access Control Functions
4.2 Lock Functions
4.3 Record Cursor Position Functions
4.4 Key Cursor Position Functions
4.5 Input/Output Functions
4.6 Import/Export Functions
Chapter 5. Custom Indexing 23
Chapter 6. An Example Program 27
6.1 Data Definition
6.2 Opening a cbase
6.3 Locking a cbase
6.4 Accessing a cbase
6.5 Closing a cbase
6.6 Storing Variable Length Text
Appendix A. Installation Instructions 37
A1 manx
A2 The blkio Library
A3 The lseq Library
A4 The btree Library
A5 The cbase Library
A6 Combining Libraries
A7 cbddlp
A8 rolodeck
A9 Troubleshooting
Appendix B. Defining New Data Types 45
B1 The Type Name
B2 The Comparison Function
B3 The Export and Import Functions
B4 The Type Count
Appendix C. Porting to a New Operating System 49
C1 The OPSYS and CCOM Macros
C2 The File Descriptor Type
C3 System Calls for File Access
C4 System Calls for File Locking
C5 Debugging
References 53
Introduction
cbase is a complete multiuser C database file management library,
providing indexed and sequential access on multiple keys. Custom
indexing beyond that performed automatically by cbase can also be
performed. cbase features a layered architecture (see Figure 2.1), and
actually includes four individual libraries. Below is a summary of the
library's main features.
cbase Features
Portable
- Written in strict adherence to ANSI C standard.
- K&R C compatibility maintained.
- All operating system dependent code is isolated, making it easy to
port to new systems easy.
- UNIX and DOS currently supported.
- Complete C source code included.
Buffered
- Both records and indexes are buffered using LRU (least recently
used) buffering.
Fast and efficient random access
- B+-trees are used for inverted file key storage.
- Multiple keys are supported.
- Both unique and duplicate keys are supported.
Fast and efficient sequential access
- B+-trees also allow keyed sequential access.
- Records are stored in doubly linked lists for non-keyed sequential
access.
- Both types of sequential access are bidirectional.
Multiuser
- Read-only locking.
Other Features
- Text file data import and export.
- Custom data types can be defined.
- Marker used to detect corrupt files.
- Reference documentation is in standard UNIX manual entry format,
including errno values.
Utilities
- cbddlp, a data definition language processor, is provided to
automatically generate the C code defining a database.
Chapter 1: A Tutorial Introduction
We begin with a brief example of a cbase application to provide the
reader with a general understanding of the basic elements involved.
Details on everything presented here will come in later chapters. The
running example in this tutorial will be a minimal inventory database
consisting of a single type of record having fields for a unique part
code, a part description, bin location, and quantity in stock.
1.1 Defining a Database
The first step in any database application is to design the logical
structure of the database, i.e., the records to be stored and the fields
in those records. This logical design must then be encoded somehow into
the application, which involves the construction of data structures that
can be quite lengthy and tedious to input. To facilitate this process,
cbase allows databases to be defined using a relatively concise data
definition language (DDL).
The most important DDL element is the record statement. record is
similar to the C struct statement, but database data types are used
rather than C types, and fields can be made keys simply by prefixing the
keyword key. Using the keyword unique in addition will cause the key to
be constrained to be unique. What being a key means is that an index is
automatically maintained for that field, allowing quick searches to be
performed on that field as well as rapid sequential processing of the
records in the sort order of that field. The DDL statements data file
and index file are used to specify the filename for each data file and
index file. C preprocessor statements can also be included in a DDL
file.
Figure 1.1 shows the complete DDL description part.ddl for our
inventory database. This information must now be translated into a form
accessible from a C program. This is done with the cbase DDL processor
cbddlp.
cbddlp part.ddl
From the information in part.ddl, cbddlp generates all the necessary
macros and data structures necessary to completely define the database
in C. Two C source files are generated: a .h file to be included in
every module, and a .i file to be included in only one, normally the one
containing main. The contents of the .i file will be used only
internally by cbase, but the contents of the .h file are required by the
application program.
Figure 1.2 lists the header file part.h generated from part.ddl.
First notice that the C preprocessor statements have been passed through
unaltered. There is then a macro identifying the cbase that is the
record name converted to upper case. For each record statement in the
DDL file, there is a corresponding C struct statement using the same
record name as its identifier. For each field in a record there is an
upper case macro identifying it, and a macro for the number of fields in
the record. Finally, there is a declaration for the field list data
structure in the .i file that is passed to the cbase library functions
to create and open a cbase. The prefix for the field count and field
list identifiers are taken from the characters of the first field name
preceding the first underscore.
/* constants */
#define PTCODE_MAX (11) /* part code length max */
#define PTDESC_MAX (30) /* part description length max */
#define PTBIN_MAX (4) /* part bin length max */
/* file assignments */
data file "part.dat" contains part;
index file "ptcode.ndx" contains pt_code;
评论0