xref: /illumos-gate/usr/src/lib/libnisdb/db_c.x (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  *	db_c.x
247c478bd9Sstevel@tonic-gate  *
25*a506a34cSth160488  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
267c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #if RPC_HDR
307c478bd9Sstevel@tonic-gate %#ifndef _DB_DB_H
317c478bd9Sstevel@tonic-gate %#define _DB_DB_H
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #ifdef USINGC
347c478bd9Sstevel@tonic-gate %#include "db_mindex_c.h"
357c478bd9Sstevel@tonic-gate %#include "db_vers_c.h"
367c478bd9Sstevel@tonic-gate %#include "db_entry_c.h"
377c478bd9Sstevel@tonic-gate #else
387c478bd9Sstevel@tonic-gate %#include "nisdb_rw.h"
397c478bd9Sstevel@tonic-gate %#include "db_log.h"
407c478bd9Sstevel@tonic-gate %#include "db_mindex.h"
417c478bd9Sstevel@tonic-gate %#include "db_vers.h"
427c478bd9Sstevel@tonic-gate %#include "db_entry.h"
437c478bd9Sstevel@tonic-gate %#include "db_scheme.h"
44*a506a34cSth160488 #endif /* USINGC */
45*a506a34cSth160488 #endif /* RPC_HDR */
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #if RPC_HDR || RPC_XDR
487c478bd9Sstevel@tonic-gate #ifdef USINGC
497c478bd9Sstevel@tonic-gate struct db {
507c478bd9Sstevel@tonic-gate   char* logfilename;
517c478bd9Sstevel@tonic-gate   char* dbfilename;
527c478bd9Sstevel@tonic-gate   char* tmpfilename;
537c478bd9Sstevel@tonic-gate   db_log *logfile;
547c478bd9Sstevel@tonic-gate   db_mindex internal_db;
557c478bd9Sstevel@tonic-gate   bool logfile_opened;
567c478bd9Sstevel@tonic-gate   bool changed;
577c478bd9Sstevel@tonic-gate   __nisdb_rwlock_t db_rwlock;
587c478bd9Sstevel@tonic-gate };
59*a506a34cSth160488 #endif /* USINGC */
60*a506a34cSth160488 #endif /* RPC_HDR */
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #ifndef USINGC
637c478bd9Sstevel@tonic-gate #ifdef RPC_HDR
647c478bd9Sstevel@tonic-gate %class db {
657c478bd9Sstevel@tonic-gate %  char* logfilename;
667c478bd9Sstevel@tonic-gate %  char* dbfilename;
677c478bd9Sstevel@tonic-gate %  char* tmpfilename;
687c478bd9Sstevel@tonic-gate %  db_log *logfile;
697c478bd9Sstevel@tonic-gate %  db_mindex internal_db;
707c478bd9Sstevel@tonic-gate %  bool_t logfile_opened;
717c478bd9Sstevel@tonic-gate %  bool_t changed;
727c478bd9Sstevel@tonic-gate %  STRUCTRWLOCK(db);
737c478bd9Sstevel@tonic-gate %
747c478bd9Sstevel@tonic-gate %/* Delete old log file and descriptor */
757c478bd9Sstevel@tonic-gate %  int reset_log();
767c478bd9Sstevel@tonic-gate %
777c478bd9Sstevel@tonic-gate %/* Open log file (and creates descriptor) if it has not been opened */
787c478bd9Sstevel@tonic-gate %  int open_log(bool_t copylog);
797c478bd9Sstevel@tonic-gate %
807c478bd9Sstevel@tonic-gate %/* Incorporate updates in log to database already loaded.
817c478bd9Sstevel@tonic-gate %   Does not affect "logfile" */
827c478bd9Sstevel@tonic-gate %  int incorporate_log( char * );
837c478bd9Sstevel@tonic-gate %
847c478bd9Sstevel@tonic-gate %/* Execute the specified action using the rest of the arguments as input.
857c478bd9Sstevel@tonic-gate %   Return  a structure db_result containing the result. */
867c478bd9Sstevel@tonic-gate %  db_result * exec_action( db_action, db_query *, entry_object *,
877c478bd9Sstevel@tonic-gate %			  db_next_desc *previous );
887c478bd9Sstevel@tonic-gate % public:
897c478bd9Sstevel@tonic-gate %/*  Log the given action and execute it.
907c478bd9Sstevel@tonic-gate %    The minor version of the database is updated after the action has
917c478bd9Sstevel@tonic-gate %    been executed and the database is flagged as being changed.
927c478bd9Sstevel@tonic-gate %    Return the structure db_result, or NULL if the loggin failed or the
937c478bd9Sstevel@tonic-gate %    action is unknown. */
947c478bd9Sstevel@tonic-gate %  db_result * log_action( db_action, db_query *, entry_object * );
957c478bd9Sstevel@tonic-gate %
967c478bd9Sstevel@tonic-gate %  /* closes log file if opened */
977c478bd9Sstevel@tonic-gate %  /* removes self from list of dbs have opened files */
987c478bd9Sstevel@tonic-gate %  int close_log(int bypass_standby = 0);
997c478bd9Sstevel@tonic-gate %
1007c478bd9Sstevel@tonic-gate %/* Constructor:  Create a database using the given name, 'dbname.'
1017c478bd9Sstevel@tonic-gate %   The database is stored in a file named 'dbname'.
1027c478bd9Sstevel@tonic-gate %   The log file is stored in a file named 'dbname'.log.
1037c478bd9Sstevel@tonic-gate %   A temporary file 'dbname'.tmp is also used.   */
1047c478bd9Sstevel@tonic-gate %  db( char * );
1057c478bd9Sstevel@tonic-gate %
1067c478bd9Sstevel@tonic-gate %/* Destructor:  deletes filenames and logfile descriptor.
1077c478bd9Sstevel@tonic-gate %   Note that associated files should be removed separately.  */
1087c478bd9Sstevel@tonic-gate %  ~db();
1097c478bd9Sstevel@tonic-gate %
1107c478bd9Sstevel@tonic-gate %/* Write out in-memory copy of database to file.
1117c478bd9Sstevel@tonic-gate %   1.  Update major version.
1127c478bd9Sstevel@tonic-gate %   2.  Dump contents to temporary file.
1137c478bd9Sstevel@tonic-gate %   3.  Rename temporary file to real database file.
1147c478bd9Sstevel@tonic-gate %   4.  Remove log file.
1157c478bd9Sstevel@tonic-gate %   A checkpoint is done only if it has changed since the previous checkpoint.
1167c478bd9Sstevel@tonic-gate %   Returns TRUE if checkpoint was successful; FALSE otherwise. */
1177c478bd9Sstevel@tonic-gate %  bool_t checkpoint();
1187c478bd9Sstevel@tonic-gate %
1197c478bd9Sstevel@tonic-gate %/* Load database and incorporate any logged updates into the loaded copy.
1207c478bd9Sstevel@tonic-gate %   Return TRUE if load succeeds; FALSE otherwise. */
1217c478bd9Sstevel@tonic-gate %  bool_t load();
1227c478bd9Sstevel@tonic-gate %
1237c478bd9Sstevel@tonic-gate %/* Dump this database to a file.
1247c478bd9Sstevel@tonic-gate %   Return TRUE if dump succeeds; FALSE otherwise. */
1257c478bd9Sstevel@tonic-gate %  bool_t dump(char *outfile) {return (internal_db.dump(outfile));};
1267c478bd9Sstevel@tonic-gate %
1277c478bd9Sstevel@tonic-gate %/* Initialize the database using table scheme 's'.
1287c478bd9Sstevel@tonic-gate %   Because the 'scheme' must be 'remembered' between restarts,
1297c478bd9Sstevel@tonic-gate %   after the initialization, the empty database is checkpointed to record
1307c478bd9Sstevel@tonic-gate %   the scheme. Returns TRUE if initialization succeeds; FALSE otherwise. */
1317c478bd9Sstevel@tonic-gate %  bool_t init( db_scheme *s );
1327c478bd9Sstevel@tonic-gate %
1337c478bd9Sstevel@tonic-gate %/* Print out the database's statistics. */
1347c478bd9Sstevel@tonic-gate %  void print()    { internal_db.print_stats();}
1357c478bd9Sstevel@tonic-gate %
1367c478bd9Sstevel@tonic-gate %/* Return whether the database has changed since its previous checkpoint. */
1377c478bd9Sstevel@tonic-gate %  bool_t changedp() { return changed;}
1387c478bd9Sstevel@tonic-gate %
1397c478bd9Sstevel@tonic-gate %/* Return the version of the database. */
1407c478bd9Sstevel@tonic-gate %  vers* get_version() { return internal_db.get_version(); }
1417c478bd9Sstevel@tonic-gate %
1427c478bd9Sstevel@tonic-gate %
1437c478bd9Sstevel@tonic-gate %/* Execute 'action' using the rest of the arguments as input.
1447c478bd9Sstevel@tonic-gate %   Return the result of the operation in a db_result structure;
1457c478bd9Sstevel@tonic-gate %   Return NULL if the request is unknown.
1467c478bd9Sstevel@tonic-gate %   If the action involves updates (ADD and REMOVE), it is logged first. */
1477c478bd9Sstevel@tonic-gate %  db_result* execute( db_action, db_query *, entry_object *,
1487c478bd9Sstevel@tonic-gate %			  db_next_desc* previous );
1497c478bd9Sstevel@tonic-gate %
1507c478bd9Sstevel@tonic-gate %/* Execute log entry 'j' on this db.  'j' is executed if its version is
1517c478bd9Sstevel@tonic-gate %   later than that of the database; if executed, the database's version
1527c478bd9Sstevel@tonic-gate %   will be changed to that of 'j', regardless of the status of the operation.
1537c478bd9Sstevel@tonic-gate %   Returns TRUE if 'j' was executed;   FALSE if it was not.
1547c478bd9Sstevel@tonic-gate %   Log entry is added to this database's log if log_entry is applied. */
1557c478bd9Sstevel@tonic-gate %  bool_t execute_log_entry( db_log_entry * );
1567c478bd9Sstevel@tonic-gate %
1577c478bd9Sstevel@tonic-gate %/* Return structure db_log_list containing entries that are later
1587c478bd9Sstevel@tonic-gate %   than the version 'v' given.  */
1597c478bd9Sstevel@tonic-gate %  db_log_list * get_log_entries_since( vers * );
1607c478bd9Sstevel@tonic-gate %
1617c478bd9Sstevel@tonic-gate %/* Sync the table log file */
1627c478bd9Sstevel@tonic-gate %  db_status	sync_log();
1637c478bd9Sstevel@tonic-gate %
1647c478bd9Sstevel@tonic-gate %/* Delete all files associated with database. */
1657c478bd9Sstevel@tonic-gate %  int remove_files();
1667c478bd9Sstevel@tonic-gate %
1677c478bd9Sstevel@tonic-gate %  /* for debugging */
1687c478bd9Sstevel@tonic-gate %/* Print information on all indices of the database to stdout. */
1697c478bd9Sstevel@tonic-gate %  void print_all_indices() {internal_db.print_all_indices();}
1707c478bd9Sstevel@tonic-gate %
1717c478bd9Sstevel@tonic-gate %/* Print information on specified index of the the database to stdout. */
1727c478bd9Sstevel@tonic-gate %  void print_index( int n ) {internal_db.print_index( n ); }
1737c478bd9Sstevel@tonic-gate %
1747c478bd9Sstevel@tonic-gate %/* Mark this instance deferred */
1757c478bd9Sstevel@tonic-gate %  void markDeferred(void) {
1767c478bd9Sstevel@tonic-gate %	internal_db.markDeferred();
1777c478bd9Sstevel@tonic-gate %  }
1787c478bd9Sstevel@tonic-gate %
1797c478bd9Sstevel@tonic-gate %/* Remove defer marking */
1807c478bd9Sstevel@tonic-gate %  void unmarkDeferred(void) {
1817c478bd9Sstevel@tonic-gate %	internal_db.unmarkDeferred();
1827c478bd9Sstevel@tonic-gate %  }
1837c478bd9Sstevel@tonic-gate %
1847c478bd9Sstevel@tonic-gate %/* Exclusive access to this instance */
1857c478bd9Sstevel@tonic-gate %  int acqexcl(void) {
1867c478bd9Sstevel@tonic-gate %	return (WLOCK(db));
1877c478bd9Sstevel@tonic-gate %  }
1887c478bd9Sstevel@tonic-gate %
1897c478bd9Sstevel@tonic-gate %/* Non-blocking exclusive access */
1907c478bd9Sstevel@tonic-gate %  int tryacqexcl(void) {
1917c478bd9Sstevel@tonic-gate %	return (TRYWLOCK(db));
1927c478bd9Sstevel@tonic-gate %  }
1937c478bd9Sstevel@tonic-gate %
1947c478bd9Sstevel@tonic-gate %/* Release exclusive access */
1957c478bd9Sstevel@tonic-gate %  int relexcl(void) {
1967c478bd9Sstevel@tonic-gate %	return (WULOCK(db));
1977c478bd9Sstevel@tonic-gate %  }
1987c478bd9Sstevel@tonic-gate %
1997c478bd9Sstevel@tonic-gate %/* Non-exclusive (readonly) access to this instance */
2007c478bd9Sstevel@tonic-gate %  int acqnonexcl(void) {
2017c478bd9Sstevel@tonic-gate %	return (RLOCK(db));
2027c478bd9Sstevel@tonic-gate %  }
2037c478bd9Sstevel@tonic-gate %
2047c478bd9Sstevel@tonic-gate %/* Release non-exlusive access */
2057c478bd9Sstevel@tonic-gate %  int relnonexcl(void) {
2067c478bd9Sstevel@tonic-gate %	return (RULOCK(db));
2077c478bd9Sstevel@tonic-gate %  }
2087c478bd9Sstevel@tonic-gate %
2097c478bd9Sstevel@tonic-gate %/* Destroy instance lock */
2107c478bd9Sstevel@tonic-gate %  int destroylock(void) {
2117c478bd9Sstevel@tonic-gate %	return (DESTROYRW(db));
2127c478bd9Sstevel@tonic-gate %  }
2137c478bd9Sstevel@tonic-gate %
2147c478bd9Sstevel@tonic-gate %/* Pass configuration information to the db_mindex */
2157c478bd9Sstevel@tonic-gate %  bool_t configure(char *objName);
2167c478bd9Sstevel@tonic-gate %
2177c478bd9Sstevel@tonic-gate %/* Export a pointer to the db_mindex structure */
2187c478bd9Sstevel@tonic-gate %  db_mindex *mindex(void);
2197c478bd9Sstevel@tonic-gate %};
2207c478bd9Sstevel@tonic-gate %
2217c478bd9Sstevel@tonic-gate %typedef class db * dbp;
222*a506a34cSth160488 #endif /* USINGC */
223*a506a34cSth160488 #endif /* RPC_HDR */
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate #if RPC_HDR
226*a506a34cSth160488 %#endif /* _DB_DB_H */
227*a506a34cSth160488 #endif /* RPC_HDR */
228