xref: /titanic_52/usr/src/lib/libnisdb/db_c.x (revision a506a34ceb0e9dcc6c61bf0560202f8538928650)
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 %#pragma ident	"%Z%%M%	%I%	%E% SMI"
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #if RPC_HDR
327c478bd9Sstevel@tonic-gate %#ifndef _DB_DB_H
337c478bd9Sstevel@tonic-gate %#define _DB_DB_H
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #ifdef USINGC
367c478bd9Sstevel@tonic-gate %#include "db_mindex_c.h"
377c478bd9Sstevel@tonic-gate %#include "db_vers_c.h"
387c478bd9Sstevel@tonic-gate %#include "db_entry_c.h"
397c478bd9Sstevel@tonic-gate #else
407c478bd9Sstevel@tonic-gate %#include "nisdb_rw.h"
417c478bd9Sstevel@tonic-gate %#include "db_log.h"
427c478bd9Sstevel@tonic-gate %#include "db_mindex.h"
437c478bd9Sstevel@tonic-gate %#include "db_vers.h"
447c478bd9Sstevel@tonic-gate %#include "db_entry.h"
457c478bd9Sstevel@tonic-gate %#include "db_scheme.h"
46*a506a34cSth160488 #endif /* USINGC */
47*a506a34cSth160488 #endif /* RPC_HDR */
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #if RPC_HDR || RPC_XDR
507c478bd9Sstevel@tonic-gate #ifdef USINGC
517c478bd9Sstevel@tonic-gate struct db {
527c478bd9Sstevel@tonic-gate   char* logfilename;
537c478bd9Sstevel@tonic-gate   char* dbfilename;
547c478bd9Sstevel@tonic-gate   char* tmpfilename;
557c478bd9Sstevel@tonic-gate   db_log *logfile;
567c478bd9Sstevel@tonic-gate   db_mindex internal_db;
577c478bd9Sstevel@tonic-gate   bool logfile_opened;
587c478bd9Sstevel@tonic-gate   bool changed;
597c478bd9Sstevel@tonic-gate   __nisdb_rwlock_t db_rwlock;
607c478bd9Sstevel@tonic-gate };
61*a506a34cSth160488 #endif /* USINGC */
62*a506a34cSth160488 #endif /* RPC_HDR */
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #ifndef USINGC
657c478bd9Sstevel@tonic-gate #ifdef RPC_HDR
667c478bd9Sstevel@tonic-gate %class db {
677c478bd9Sstevel@tonic-gate %  char* logfilename;
687c478bd9Sstevel@tonic-gate %  char* dbfilename;
697c478bd9Sstevel@tonic-gate %  char* tmpfilename;
707c478bd9Sstevel@tonic-gate %  db_log *logfile;
717c478bd9Sstevel@tonic-gate %  db_mindex internal_db;
727c478bd9Sstevel@tonic-gate %  bool_t logfile_opened;
737c478bd9Sstevel@tonic-gate %  bool_t changed;
747c478bd9Sstevel@tonic-gate %  STRUCTRWLOCK(db);
757c478bd9Sstevel@tonic-gate %
767c478bd9Sstevel@tonic-gate %/* Delete old log file and descriptor */
777c478bd9Sstevel@tonic-gate %  int reset_log();
787c478bd9Sstevel@tonic-gate %
797c478bd9Sstevel@tonic-gate %/* Open log file (and creates descriptor) if it has not been opened */
807c478bd9Sstevel@tonic-gate %  int open_log(bool_t copylog);
817c478bd9Sstevel@tonic-gate %
827c478bd9Sstevel@tonic-gate %/* Incorporate updates in log to database already loaded.
837c478bd9Sstevel@tonic-gate %   Does not affect "logfile" */
847c478bd9Sstevel@tonic-gate %  int incorporate_log( char * );
857c478bd9Sstevel@tonic-gate %
867c478bd9Sstevel@tonic-gate %/* Execute the specified action using the rest of the arguments as input.
877c478bd9Sstevel@tonic-gate %   Return  a structure db_result containing the result. */
887c478bd9Sstevel@tonic-gate %  db_result * exec_action( db_action, db_query *, entry_object *,
897c478bd9Sstevel@tonic-gate %			  db_next_desc *previous );
907c478bd9Sstevel@tonic-gate % public:
917c478bd9Sstevel@tonic-gate %/*  Log the given action and execute it.
927c478bd9Sstevel@tonic-gate %    The minor version of the database is updated after the action has
937c478bd9Sstevel@tonic-gate %    been executed and the database is flagged as being changed.
947c478bd9Sstevel@tonic-gate %    Return the structure db_result, or NULL if the loggin failed or the
957c478bd9Sstevel@tonic-gate %    action is unknown. */
967c478bd9Sstevel@tonic-gate %  db_result * log_action( db_action, db_query *, entry_object * );
977c478bd9Sstevel@tonic-gate %
987c478bd9Sstevel@tonic-gate %  /* closes log file if opened */
997c478bd9Sstevel@tonic-gate %  /* removes self from list of dbs have opened files */
1007c478bd9Sstevel@tonic-gate %  int close_log(int bypass_standby = 0);
1017c478bd9Sstevel@tonic-gate %
1027c478bd9Sstevel@tonic-gate %/* Constructor:  Create a database using the given name, 'dbname.'
1037c478bd9Sstevel@tonic-gate %   The database is stored in a file named 'dbname'.
1047c478bd9Sstevel@tonic-gate %   The log file is stored in a file named 'dbname'.log.
1057c478bd9Sstevel@tonic-gate %   A temporary file 'dbname'.tmp is also used.   */
1067c478bd9Sstevel@tonic-gate %  db( char * );
1077c478bd9Sstevel@tonic-gate %
1087c478bd9Sstevel@tonic-gate %/* Destructor:  deletes filenames and logfile descriptor.
1097c478bd9Sstevel@tonic-gate %   Note that associated files should be removed separately.  */
1107c478bd9Sstevel@tonic-gate %  ~db();
1117c478bd9Sstevel@tonic-gate %
1127c478bd9Sstevel@tonic-gate %/* Write out in-memory copy of database to file.
1137c478bd9Sstevel@tonic-gate %   1.  Update major version.
1147c478bd9Sstevel@tonic-gate %   2.  Dump contents to temporary file.
1157c478bd9Sstevel@tonic-gate %   3.  Rename temporary file to real database file.
1167c478bd9Sstevel@tonic-gate %   4.  Remove log file.
1177c478bd9Sstevel@tonic-gate %   A checkpoint is done only if it has changed since the previous checkpoint.
1187c478bd9Sstevel@tonic-gate %   Returns TRUE if checkpoint was successful; FALSE otherwise. */
1197c478bd9Sstevel@tonic-gate %  bool_t checkpoint();
1207c478bd9Sstevel@tonic-gate %
1217c478bd9Sstevel@tonic-gate %/* Load database and incorporate any logged updates into the loaded copy.
1227c478bd9Sstevel@tonic-gate %   Return TRUE if load succeeds; FALSE otherwise. */
1237c478bd9Sstevel@tonic-gate %  bool_t load();
1247c478bd9Sstevel@tonic-gate %
1257c478bd9Sstevel@tonic-gate %/* Dump this database to a file.
1267c478bd9Sstevel@tonic-gate %   Return TRUE if dump succeeds; FALSE otherwise. */
1277c478bd9Sstevel@tonic-gate %  bool_t dump(char *outfile) {return (internal_db.dump(outfile));};
1287c478bd9Sstevel@tonic-gate %
1297c478bd9Sstevel@tonic-gate %/* Initialize the database using table scheme 's'.
1307c478bd9Sstevel@tonic-gate %   Because the 'scheme' must be 'remembered' between restarts,
1317c478bd9Sstevel@tonic-gate %   after the initialization, the empty database is checkpointed to record
1327c478bd9Sstevel@tonic-gate %   the scheme. Returns TRUE if initialization succeeds; FALSE otherwise. */
1337c478bd9Sstevel@tonic-gate %  bool_t init( db_scheme *s );
1347c478bd9Sstevel@tonic-gate %
1357c478bd9Sstevel@tonic-gate %/* Print out the database's statistics. */
1367c478bd9Sstevel@tonic-gate %  void print()    { internal_db.print_stats();}
1377c478bd9Sstevel@tonic-gate %
1387c478bd9Sstevel@tonic-gate %/* Return whether the database has changed since its previous checkpoint. */
1397c478bd9Sstevel@tonic-gate %  bool_t changedp() { return changed;}
1407c478bd9Sstevel@tonic-gate %
1417c478bd9Sstevel@tonic-gate %/* Return the version of the database. */
1427c478bd9Sstevel@tonic-gate %  vers* get_version() { return internal_db.get_version(); }
1437c478bd9Sstevel@tonic-gate %
1447c478bd9Sstevel@tonic-gate %
1457c478bd9Sstevel@tonic-gate %/* Execute 'action' using the rest of the arguments as input.
1467c478bd9Sstevel@tonic-gate %   Return the result of the operation in a db_result structure;
1477c478bd9Sstevel@tonic-gate %   Return NULL if the request is unknown.
1487c478bd9Sstevel@tonic-gate %   If the action involves updates (ADD and REMOVE), it is logged first. */
1497c478bd9Sstevel@tonic-gate %  db_result* execute( db_action, db_query *, entry_object *,
1507c478bd9Sstevel@tonic-gate %			  db_next_desc* previous );
1517c478bd9Sstevel@tonic-gate %
1527c478bd9Sstevel@tonic-gate %/* Execute log entry 'j' on this db.  'j' is executed if its version is
1537c478bd9Sstevel@tonic-gate %   later than that of the database; if executed, the database's version
1547c478bd9Sstevel@tonic-gate %   will be changed to that of 'j', regardless of the status of the operation.
1557c478bd9Sstevel@tonic-gate %   Returns TRUE if 'j' was executed;   FALSE if it was not.
1567c478bd9Sstevel@tonic-gate %   Log entry is added to this database's log if log_entry is applied. */
1577c478bd9Sstevel@tonic-gate %  bool_t execute_log_entry( db_log_entry * );
1587c478bd9Sstevel@tonic-gate %
1597c478bd9Sstevel@tonic-gate %/* Return structure db_log_list containing entries that are later
1607c478bd9Sstevel@tonic-gate %   than the version 'v' given.  */
1617c478bd9Sstevel@tonic-gate %  db_log_list * get_log_entries_since( vers * );
1627c478bd9Sstevel@tonic-gate %
1637c478bd9Sstevel@tonic-gate %/* Sync the table log file */
1647c478bd9Sstevel@tonic-gate %  db_status	sync_log();
1657c478bd9Sstevel@tonic-gate %
1667c478bd9Sstevel@tonic-gate %/* Delete all files associated with database. */
1677c478bd9Sstevel@tonic-gate %  int remove_files();
1687c478bd9Sstevel@tonic-gate %
1697c478bd9Sstevel@tonic-gate %  /* for debugging */
1707c478bd9Sstevel@tonic-gate %/* Print information on all indices of the database to stdout. */
1717c478bd9Sstevel@tonic-gate %  void print_all_indices() {internal_db.print_all_indices();}
1727c478bd9Sstevel@tonic-gate %
1737c478bd9Sstevel@tonic-gate %/* Print information on specified index of the the database to stdout. */
1747c478bd9Sstevel@tonic-gate %  void print_index( int n ) {internal_db.print_index( n ); }
1757c478bd9Sstevel@tonic-gate %
1767c478bd9Sstevel@tonic-gate %/* Mark this instance deferred */
1777c478bd9Sstevel@tonic-gate %  void markDeferred(void) {
1787c478bd9Sstevel@tonic-gate %	internal_db.markDeferred();
1797c478bd9Sstevel@tonic-gate %  }
1807c478bd9Sstevel@tonic-gate %
1817c478bd9Sstevel@tonic-gate %/* Remove defer marking */
1827c478bd9Sstevel@tonic-gate %  void unmarkDeferred(void) {
1837c478bd9Sstevel@tonic-gate %	internal_db.unmarkDeferred();
1847c478bd9Sstevel@tonic-gate %  }
1857c478bd9Sstevel@tonic-gate %
1867c478bd9Sstevel@tonic-gate %/* Exclusive access to this instance */
1877c478bd9Sstevel@tonic-gate %  int acqexcl(void) {
1887c478bd9Sstevel@tonic-gate %	return (WLOCK(db));
1897c478bd9Sstevel@tonic-gate %  }
1907c478bd9Sstevel@tonic-gate %
1917c478bd9Sstevel@tonic-gate %/* Non-blocking exclusive access */
1927c478bd9Sstevel@tonic-gate %  int tryacqexcl(void) {
1937c478bd9Sstevel@tonic-gate %	return (TRYWLOCK(db));
1947c478bd9Sstevel@tonic-gate %  }
1957c478bd9Sstevel@tonic-gate %
1967c478bd9Sstevel@tonic-gate %/* Release exclusive access */
1977c478bd9Sstevel@tonic-gate %  int relexcl(void) {
1987c478bd9Sstevel@tonic-gate %	return (WULOCK(db));
1997c478bd9Sstevel@tonic-gate %  }
2007c478bd9Sstevel@tonic-gate %
2017c478bd9Sstevel@tonic-gate %/* Non-exclusive (readonly) access to this instance */
2027c478bd9Sstevel@tonic-gate %  int acqnonexcl(void) {
2037c478bd9Sstevel@tonic-gate %	return (RLOCK(db));
2047c478bd9Sstevel@tonic-gate %  }
2057c478bd9Sstevel@tonic-gate %
2067c478bd9Sstevel@tonic-gate %/* Release non-exlusive access */
2077c478bd9Sstevel@tonic-gate %  int relnonexcl(void) {
2087c478bd9Sstevel@tonic-gate %	return (RULOCK(db));
2097c478bd9Sstevel@tonic-gate %  }
2107c478bd9Sstevel@tonic-gate %
2117c478bd9Sstevel@tonic-gate %/* Destroy instance lock */
2127c478bd9Sstevel@tonic-gate %  int destroylock(void) {
2137c478bd9Sstevel@tonic-gate %	return (DESTROYRW(db));
2147c478bd9Sstevel@tonic-gate %  }
2157c478bd9Sstevel@tonic-gate %
2167c478bd9Sstevel@tonic-gate %/* Pass configuration information to the db_mindex */
2177c478bd9Sstevel@tonic-gate %  bool_t configure(char *objName);
2187c478bd9Sstevel@tonic-gate %
2197c478bd9Sstevel@tonic-gate %/* Export a pointer to the db_mindex structure */
2207c478bd9Sstevel@tonic-gate %  db_mindex *mindex(void);
2217c478bd9Sstevel@tonic-gate %};
2227c478bd9Sstevel@tonic-gate %
2237c478bd9Sstevel@tonic-gate %typedef class db * dbp;
224*a506a34cSth160488 #endif /* USINGC */
225*a506a34cSth160488 #endif /* RPC_HDR */
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate #if RPC_HDR
228*a506a34cSth160488 %#endif /* _DB_DB_H */
229*a506a34cSth160488 #endif /* RPC_HDR */
230