home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / ixemul-39.47-env-bin.lha / man / cat3 / db.0 < prev    next >
Text File  |  1993-12-07  |  17KB  |  331 lines

  1. btree_open, hash_open, recno_open - database access methods
  2. ##iinncclluuddee <<ssyyss//ttyyppeess..hh>>
  3. ##iinncclluuddee <<ddbb..hh>>
  4.  
  5. DDBB **
  6. bbttrreeee__ooppeenn((ccoonnsstt cchhaarr **ffiillee,, iinntt ffllaaggss,, iinntt mmooddee,,
  7.      ccoonnsstt BBTTRREEEEIINNFFOO ** ooppeenniinnffoo));;
  8.  
  9. DDBB **
  10. hhaasshh__ooppeenn((ccoonnsstt cchhaarr **ffiillee,, iinntt ffllaaggss,, iinntt mmooddee,,
  11.      ccoonnsstt HHAASSHHIINNFFOO ** ooppeenniinnffoo));;
  12.  
  13. DDBB **
  14. rreeccnnoo__ooppeenn((ccoonnsstt cchhaarr **ffiillee,, iinntt ffllaaggss,, iinntt mmooddee,,
  15.      ccoonnsstt RREECCNNOOIINNFFOO ** ooppeenniinnffoo));;
  16. and  are  access  method  interfaces  to database files in btree,
  17. hashed, and flat­file formats, respectively.  The btree format is
  18. a  representation  of  a  sorted,  balanced  tree structure.  The
  19. hashed format is an  extensible,  dynamic  hashing  scheme.   The
  20. flat­file  format  is  a  UNIX file with fixed or variable length
  21. lines.  These formats are described in more detail below.  Access
  22. to all file types is based on key/data pairs.  Each routine opens
  23. for reading and/or writing.  Databases never intended to be  pre­
  24. served  on  disk  may be created by setting the file parameter to
  25. NULL.  The and are as specified to the routine, however, only the
  26. O_CREAT, O_EXCL, O_RDONLY, O_RDWR, O_TRUNC and O_WRONLY flags are
  27. meaningful.  The argument is a pointer to an access  method  spe­
  28. cific  structure  described  below.   The  open routines return a
  29. pointer to a DB structure on success and NULL on error.   The  DB
  30. structure contains at least the following fields:
  31.  
  32. typedef struct {
  33. int (*close)(const DB *db);
  34. int (*sync)(const DB *db);
  35. int (*del)(const DB *db, const DBT *key, u_int flags);
  36. int (*get)(const DB *db, DBT *key, DBT *data, u_int flags);
  37. int (*put)(const DB *db, const DBT *key, const DBT *data,
  38.      u_int flags);
  39. int (*seq)(const DB *db, DBT *key, DBT *data, u_int flags);
  40. int type;
  41. void *openinfo;
  42. } DB;
  43. The  elements of this structure consist of a pointer to an access
  44. method specific structure and a set  of  routines  which  perform
  45. various  functions.   All  of  these routines take a pointer to a
  46. structure as returned by one of the open routines,  one  or  more
  47. pointers  to  key/data structures, and, optionally, a flag value.
  48. openinfo A pointer to an internal structure specific to  the  ac­
  49. cess  method.  type The type of the underlying access method; ei­
  50. ther DB_BTREE, DB_HASH or DB_RECNO.  close A pointer to a routine
  51. to  flush  any cached information to disk, free any allocated re­
  52. sources, and close the database file.  Since key/data  pairs  may
  53. be cached in memory, failing to close the file with a routine may
  54. result in inconsistent or lost information.  routines  return  ­1
  55. on  error  (setting and 0 on success.  del A pointer to a routine
  56. to remove key/data pairs from the database.  routines  return  ­1
  57. on error (setting 0 on success, and 1 if the specified was not in
  58. the file.  get A pointer to a routine which is the interface  for
  59. keyed retrieval from the database.  The address and length of the
  60. data associated with the specified are returned in the  structure
  61. referenced  by routines return ­1 on error (setting 0 on success,
  62. and 1 if the was not in the file.  put A pointer to a routine  to
  63. store  key/data pairs in the database.  The parameter must be set
  64. to one of the following values: R_IAFTER Append the data  immedi­
  65. ately  after the data referenced by creating a new key/data pair.
  66. (This implies that the access method is able to create new  keys,
  67. i.e.  the  keys  are ordered and independent, for example, record
  68. numbers.  Applicable only to the access method.)   R_IBEFORE  In­
  69. sert  the data immediately before the data referenced by creating
  70. a new key/data pair.  (This implies that  the  access  method  is
  71. able  to  create new keys, i.e. the keys are ordered and indepen­
  72. dent, for example, record numbers.  Applicable only to the access
  73. method.)   R_NOOVERWRITE  Enter the new key/data pair only if the
  74. key does not previously exist.  R_PUT Enter the new key/data pair
  75. and  replace  any previously existing key.  routines return ­1 on
  76. error (setting 0 on success, and 1 if the R_NOOVERWRITE  was  set
  77. and  the key already exists in the file.  seq A pointer to a rou­
  78. tine which is the interface for  sequential  retrieval  from  the
  79. database.   The address and length of the key are returned in the
  80. structure referenced by and the address and length  of  the  data
  81. are  returned  in the structure referenced by Sequential key/data
  82. pair retrieval may begin at any time, and  the  position  of  the
  83. ``cursor''  is not affected by calls to the or routines.  Modifi­
  84. cations to the database during a sequential scan will be reflect­
  85. ed  in the scan, i.e. records inserted behind the cursor will not
  86. be returned while records inserted in front of the cursor will be
  87. returned.   The  flag  value  must be set to one of the following
  88. values: R_CURSOR The data associated with the  specified  key  is
  89. returned.   This  differs  from  the routines in that it sets the
  90. ``cursor'' to the location of the key  as  well.   (This  implies
  91. that  the  access  method  has  a  implicit  order which does not
  92. change.  Applicable only to the and access methods.)  R_FIRST The
  93. first key/data pair of the database is returned.  R_LAST The last
  94. key/data pair of the database is returned.   (This  implies  that
  95. the  access  method  has  a implicit order which does not change.
  96. Applicable only to the and access methods.)  R_NEXT Retrieve  the
  97. key/data  pair  immediately after the key/data pair most recently
  98. retrieved using the routine.  The cursor is moved to the returned
  99. key/data pair.  If is set to R_NEXT the first time the routine is
  100. called, the first key/data pair  of  the  database  is  returned.
  101. R_PREV Retrieve the key/data pair immediately before the key/data
  102. pair most recently retrieved using the routine.   The  cursor  is
  103. moved  to  the  returned  key/data pair.  If is set to R_PREV the
  104. first time the routine is called, the last key/data pair  of  the
  105. database is returned.  (This implies that the access method has a
  106. implicit order which does not change.  Applicable only to the and
  107. access  methods.)  routines return ­1 on error (setting 0 on suc­
  108. cess, 1 if there are no more key/data pairs  available.   If  the
  109. access  method is being used, and if the database file is a char­
  110. acter special file and no complete key/data pairs  are  currently
  111. available, the routines return 2.  sync A pointer to a routine to
  112. flush any cached information to disk.  If the database is in mem­
  113. ory  only,  the  routine  has  no effect and will always succeed.
  114. routines return ­1 on error (setting and 0 on success.  Access to
  115. all  file  types  is based on key/data pairs.  Both keys and data
  116. are represented by the following data structure: typedef struct {
  117. void *data;
  118. size_t size; } DBT; The elements of the DBT structure are defined
  119. as follows: data A pointer to a byte string.  size The length  of
  120. the  byte string.  Key/data strings must fit into available memo­
  121. ry.  One of the access methods is a  btree:  a  sorted,  balanced
  122. tree structure with associated key/data pairs.  The access method
  123. specific data structure provided to is as follows: typedef struct
  124. { u_long flags;
  125. u_int psize;
  126. u_int cachesize;
  127. int (*compare)(const void *, const void *);
  128. int  lorder;  } BTREEINFO; The elements of this structure are de­
  129. fined as follows: flags The flag value is specified by any of the
  130. following  values:  R_DUP On insertion, if the key to be inserted
  131. already exists, permit insertion anyway.  This flag  permits  du­
  132. plicate keys in the tree.  By default, duplicates are not permit­
  133. ted, and attempts to insert them will fail.  Note, the  order  of
  134. retrieval  of  key/data  pairs  with duplicate keys is undefined.
  135. cachesize A suggested maximum  size,  in  bytes,  of  the  memory
  136. cache.   Setting this value to zero specifies that an appropriate
  137. amount of memory should be used.  Since every search examines the
  138. root  page of the tree, caching the most recently used pages sub­
  139. stantially improves access time.  In  addition,  physical  writes
  140. are  delayed  as long as possible, so a moderate cache can reduce
  141. the number of I/O operations significantly.  Obviously,  using  a
  142. cache  increases the likelihood of corruption or lost data if the
  143. system crashes while a tree is being modified.  However,  caching
  144. 10  pages  decreases the creation time of a large tree by between
  145. two and three orders of magnitude.  compare Compare is a user de­
  146. fined  comparison function.  It must return an integer less than,
  147. equal to, or greater than zero if the first argument  is  consid­
  148. ered  to be respectively less than, equal to, or greater than the
  149. second.  The same comparison function must be  used  on  a  given
  150. tree every time it is opened.  If no comparison function is spec­
  151. ified, is used.  lorder The byte order for 4­byte integers in the
  152. stored  database metadata.  The number should represent the order
  153. as an integer; for example, big endian order would be the  number
  154. 4,321.  If is 0 (no order is specified) the current host order is
  155. used.  If the  file already exists, the specified  value  is  ig­
  156. nored  and the value specified when the tree was created is used.
  157. (Obviously, portability of the data forming the key/data pairs is
  158. the  concern of the application program.)  psize Page size is the
  159. size in bytes of the pages used for nodes in the  tree.   If  the
  160. file already exists, the specified value is ignored and the value
  161. specified when the tree was created is used.  If is zero, an  ap­
  162. propriate  page size is chosen (based on the system memory and/or
  163. file system constraints), but will never be less than 512  bytes.
  164. If  the  pointer  to the data structure is NULL, the routine will
  165. use appropriate values.  If the database file already exists, and
  166. the  O_TRUNC flag is not specified to the parameter ignored.  Key
  167. structures may reference byte strings of slightly less than  one­
  168. half the tree's page size only (see Data structures may reference
  169. byte strings of essentially unlimited length.   Searches,  inser­
  170. tions,  and  deletions  in  a  btree will all complete in O lg N.
  171. Forward sequential scans of a tree are from the least key to  the
  172. greatest.  Space freed up by deleting key/data pairs from a btree
  173. is never reclaimed, although it is normally  made  available  for
  174. reuse.  The exception to this is that space occupied by large da­
  175. ta items (those greater than one quarter the size of a  page)  is
  176. neither  reclaimed nor reused.  This means that the btree storage
  177. structure is grow­only.  The only solutions are to  avoid  exces­
  178. sive  deletions,  or  to  create a fresh tree periodically from a
  179. scan of an existing one.  One of the access methods is hashed ac­
  180. cess and storage.  The access method specific data structure pro­
  181. vided to is as follows:
  182.  
  183. typedef struct { u_long (*hash)(const void *, const size_t);
  184. u_int cachesize;
  185. int bsize;
  186. int ffactor;
  187. int lorder;
  188. int nelem; } HASHINFO; The elements of this structure are defined
  189. as  follows: bsize defines the hash table bucket size, and is, by
  190. default, 256 bytes.  It may be preferable to  increase  the  page
  191. size  for  disk­resident tables and tables with large data items.
  192. cachesize A suggested maximum  size,  in  bytes,  of  the  memory
  193. cache.   Setting this value to zero specifies that an appropriate
  194. amount of memory should be used.   ffactor  indicates  a  desired
  195. density  within  the  hash  table.  It is an approximation of the
  196. number of keys allowed to accumulate in any one bucket, determin­
  197. ing  when  the hash table grows or shrinks.  The default value is
  198. 8.  hash is a user defined hash function.  Since no hash function
  199. performs  equally  well  on  all possible data, the user may find
  200. that the built­in hash function does poorly on a particular  data
  201. set.   User  specified  hash functions must take two arguments (a
  202. pointer to a byte string and a length) and return an u_long to be
  203. used  as  the hash value.  lorder The byte order for 4­byte inte­
  204. gers in the stored database metadata.  The number  should  repre­
  205. sent the order as an integer; for example, big endian order would
  206. be the number 4,321.  If is 0 (no order is specified) the current
  207. host  order  is used.  If the  file already exists, the specified
  208. value is ignored and the value specified when the tree was creat­
  209. ed  is  used.   (Obviously,  portability  of the data forming the
  210. key/data pairs is the concern of the application program.)  nelem
  211. is  an estimate of the final size of the hash table.  If not set,
  212. the default value is 1.  If not set or set too low,  hash  tables
  213. will  expand  gracefully  as  keys are entered, although a slight
  214. performance degradation may be noticed.  If the  pointer  to  the
  215. data  structure is NULL, the routine will use appropriate values.
  216. If the hash table already exists, and the  O_TRUNC  flag  is  not
  217. specified  to the parameters and are ignored.  If a hash function
  218. is specified, will attempt to  determine  if  the  hash  function
  219. specified is the same as the one with which the database was cre­
  220. ated, and will fail if it is not.  Both key and  data  structures
  221. may  reference  byte  strings  of  essentially  unlimited length.
  222. Backward compatible interfaces to the routines described  in  and
  223. are  provided,  however, these interfaces are not compatible with
  224. previous file formats.  One of the access methods is either vari­
  225. able  or fixed­length records, the former delimited by a specific
  226. byte value.  The access method specific data  structure  provided
  227. to is as follows:
  228.  
  229. typedef struct { u_long flags;
  230. u_int cachesize;
  231. size_t reclen;
  232. u_char  bval; } RECNOINFO; The elements of this structure are de­
  233. fined as follows: flags The flag value is specified by any of the
  234. following  values:  R_FIXEDLEN  The records are fixed­length, not
  235. byte delimited.  The structure element specifies  the  length  of
  236. the  record, and the structure element is used as the pad charac­
  237. ter.  R_SNAPSHOT This flag requires that a snapshot of  the  file
  238. be  taken  when  is  called, instead of permitting any unmodified
  239. records to be read from the original file.  cachesize A suggested
  240. maximum  size, in bytes, of the memory cache.  Setting this value
  241. to zero specifies that an appropriate amount of memory should  be
  242. used.   reclen The length of a fixed­length record.  bval The de­
  243. limiting byte to be used to mark the end of a  record  for  vari­
  244. able­length  records,  and  the  pad  character  for fixed­length
  245. records.  Variable­length and  fixed­length  data  files  require
  246. structures to reference the following structure:
  247.  
  248. typedef struct { u_long length;
  249. u_long number;
  250. u_long offset;
  251. u_char  valid; } RECNOKEY; The elements of this structure are de­
  252. fined as follows: length The length of the  record.   number  The
  253. record number.  offset The offset in the file at which the record
  254. is located.  valid A flag value which indicates the  validity  of
  255. the  other  fields in the structure.  The flag value is specified
  256. by one or more of  the  following  values:  R_LENGTH  The  record
  257. length  is valid.  R_NUMBER The record number is valid.  R_OFFSET
  258. The byte offset is valid.  If the record retrieval is successful,
  259. the  record  number, byte offset and record length are set in the
  260. RECNOKEY structure referenced by the  caller's  structure.   Data
  261. structures  may  reference  byte strings of essentially unlimited
  262. length.  The routines may fail and set  for  any  of  the  errors
  263. specified for the library routines and or the following: [EFTYPE]
  264. A file used by one of  the  routines  is  incorrectly  formatted.
  265. [EINVAL]  A parameter has been specified (hash function, pad byte
  266. etc.) that is incompatible with the current file specification or
  267. there  is  a  mismatch between the version number of file and the
  268. software.  The routines may fail and set for any  of  the  errors
  269. specified  for  the library routines or The and routines may fail
  270. and set for any of the errors specified for the library  routines
  271. or  The routines may fail and set for any of the errors specified
  272. for the library routine Per­Ake  Larson,  Communications  of  the
  273. ACM, April 1988.
  274. Margo  Seltzer, USENIX Proceedings, Winter 1991.  The typedef DBT
  275. is a mnemonic for ``data base thang'', and was used because noone
  276. could  think of a reasonable name that wasn't already used.  None
  277. of the access methods provide  any  form  of  concurrent  access,
  278. locking,  or transactions.  Only big and little endian byte order
  279. is supported.
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.