xref: /titanic_51/usr/src/lib/libnisdb/db_c.x (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  *	db_c.x
24*7c478bd9Sstevel@tonic-gate  *
25*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
26*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
27*7c478bd9Sstevel@tonic-gate  */
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate %#pragma ident	"%Z%%M%	%I%	%E% SMI"
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #if RPC_HDR
32*7c478bd9Sstevel@tonic-gate %#ifndef _DB_DB_H
33*7c478bd9Sstevel@tonic-gate %#define _DB_DB_H
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #ifdef USINGC
36*7c478bd9Sstevel@tonic-gate %#include "db_mindex_c.h"
37*7c478bd9Sstevel@tonic-gate %#include "db_vers_c.h"
38*7c478bd9Sstevel@tonic-gate %#include "db_entry_c.h"
39*7c478bd9Sstevel@tonic-gate #else
40*7c478bd9Sstevel@tonic-gate %#include "nisdb_rw.h"
41*7c478bd9Sstevel@tonic-gate %#include "db_log.h"
42*7c478bd9Sstevel@tonic-gate %#include "db_mindex.h"
43*7c478bd9Sstevel@tonic-gate %#include "db_vers.h"
44*7c478bd9Sstevel@tonic-gate %#include "db_entry.h"
45*7c478bd9Sstevel@tonic-gate %#include "db_scheme.h"
46*7c478bd9Sstevel@tonic-gate #endif USINGC
47*7c478bd9Sstevel@tonic-gate #endif RPC_HDR
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate #if RPC_HDR || RPC_XDR
50*7c478bd9Sstevel@tonic-gate #ifdef USINGC
51*7c478bd9Sstevel@tonic-gate struct db {
52*7c478bd9Sstevel@tonic-gate   char* logfilename;
53*7c478bd9Sstevel@tonic-gate   char* dbfilename;
54*7c478bd9Sstevel@tonic-gate   char* tmpfilename;
55*7c478bd9Sstevel@tonic-gate   db_log *logfile;
56*7c478bd9Sstevel@tonic-gate   db_mindex internal_db;
57*7c478bd9Sstevel@tonic-gate   bool logfile_opened;
58*7c478bd9Sstevel@tonic-gate   bool changed;
59*7c478bd9Sstevel@tonic-gate   __nisdb_rwlock_t db_rwlock;
60*7c478bd9Sstevel@tonic-gate };
61*7c478bd9Sstevel@tonic-gate #endif USINGC
62*7c478bd9Sstevel@tonic-gate #endif RPC_HDR
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate #ifndef USINGC
65*7c478bd9Sstevel@tonic-gate #ifdef RPC_HDR
66*7c478bd9Sstevel@tonic-gate %class db {
67*7c478bd9Sstevel@tonic-gate %  char* logfilename;
68*7c478bd9Sstevel@tonic-gate %  char* dbfilename;
69*7c478bd9Sstevel@tonic-gate %  char* tmpfilename;
70*7c478bd9Sstevel@tonic-gate %  db_log *logfile;
71*7c478bd9Sstevel@tonic-gate %  db_mindex internal_db;
72*7c478bd9Sstevel@tonic-gate %  bool_t logfile_opened;
73*7c478bd9Sstevel@tonic-gate %  bool_t changed;
74*7c478bd9Sstevel@tonic-gate %  STRUCTRWLOCK(db);
75*7c478bd9Sstevel@tonic-gate %
76*7c478bd9Sstevel@tonic-gate %/* Delete old log file and descriptor */
77*7c478bd9Sstevel@tonic-gate %  int reset_log();
78*7c478bd9Sstevel@tonic-gate %
79*7c478bd9Sstevel@tonic-gate %/* Open log file (and creates descriptor) if it has not been opened */
80*7c478bd9Sstevel@tonic-gate %  int open_log(bool_t copylog);
81*7c478bd9Sstevel@tonic-gate %
82*7c478bd9Sstevel@tonic-gate %/* Incorporate updates in log to database already loaded.
83*7c478bd9Sstevel@tonic-gate %   Does not affect "logfile" */
84*7c478bd9Sstevel@tonic-gate %  int incorporate_log( char * );
85*7c478bd9Sstevel@tonic-gate %
86*7c478bd9Sstevel@tonic-gate %/* Execute the specified action using the rest of the arguments as input.
87*7c478bd9Sstevel@tonic-gate %   Return  a structure db_result containing the result. */
88*7c478bd9Sstevel@tonic-gate %  db_result * exec_action( db_action, db_query *, entry_object *,
89*7c478bd9Sstevel@tonic-gate %			  db_next_desc *previous );
90*7c478bd9Sstevel@tonic-gate % public:
91*7c478bd9Sstevel@tonic-gate %/*  Log the given action and execute it.
92*7c478bd9Sstevel@tonic-gate %    The minor version of the database is updated after the action has
93*7c478bd9Sstevel@tonic-gate %    been executed and the database is flagged as being changed.
94*7c478bd9Sstevel@tonic-gate %    Return the structure db_result, or NULL if the loggin failed or the
95*7c478bd9Sstevel@tonic-gate %    action is unknown. */
96*7c478bd9Sstevel@tonic-gate %  db_result * log_action( db_action, db_query *, entry_object * );
97*7c478bd9Sstevel@tonic-gate %
98*7c478bd9Sstevel@tonic-gate %  /* closes log file if opened */
99*7c478bd9Sstevel@tonic-gate %  /* removes self from list of dbs have opened files */
100*7c478bd9Sstevel@tonic-gate %  int close_log(int bypass_standby = 0);
101*7c478bd9Sstevel@tonic-gate %
102*7c478bd9Sstevel@tonic-gate %/* Constructor:  Create a database using the given name, 'dbname.'
103*7c478bd9Sstevel@tonic-gate %   The database is stored in a file named 'dbname'.
104*7c478bd9Sstevel@tonic-gate %   The log file is stored in a file named 'dbname'.log.
105*7c478bd9Sstevel@tonic-gate %   A temporary file 'dbname'.tmp is also used.   */
106*7c478bd9Sstevel@tonic-gate %  db( char * );
107*7c478bd9Sstevel@tonic-gate %
108*7c478bd9Sstevel@tonic-gate %/* Destructor:  deletes filenames and logfile descriptor.
109*7c478bd9Sstevel@tonic-gate %   Note that associated files should be removed separately.  */
110*7c478bd9Sstevel@tonic-gate %  ~db();
111*7c478bd9Sstevel@tonic-gate %
112*7c478bd9Sstevel@tonic-gate %/* Write out in-memory copy of database to file.
113*7c478bd9Sstevel@tonic-gate %   1.  Update major version.
114*7c478bd9Sstevel@tonic-gate %   2.  Dump contents to temporary file.
115*7c478bd9Sstevel@tonic-gate %   3.  Rename temporary file to real database file.
116*7c478bd9Sstevel@tonic-gate %   4.  Remove log file.
117*7c478bd9Sstevel@tonic-gate %   A checkpoint is done only if it has changed since the previous checkpoint.
118*7c478bd9Sstevel@tonic-gate %   Returns TRUE if checkpoint was successful; FALSE otherwise. */
119*7c478bd9Sstevel@tonic-gate %  bool_t checkpoint();
120*7c478bd9Sstevel@tonic-gate %
121*7c478bd9Sstevel@tonic-gate %/* Load database and incorporate any logged updates into the loaded copy.
122*7c478bd9Sstevel@tonic-gate %   Return TRUE if load succeeds; FALSE otherwise. */
123*7c478bd9Sstevel@tonic-gate %  bool_t load();
124*7c478bd9Sstevel@tonic-gate %
125*7c478bd9Sstevel@tonic-gate %/* Dump this database to a file.
126*7c478bd9Sstevel@tonic-gate %   Return TRUE if dump succeeds; FALSE otherwise. */
127*7c478bd9Sstevel@tonic-gate %  bool_t dump(char *outfile) {return (internal_db.dump(outfile));};
128*7c478bd9Sstevel@tonic-gate %
129*7c478bd9Sstevel@tonic-gate %/* Initialize the database using table scheme 's'.
130*7c478bd9Sstevel@tonic-gate %   Because the 'scheme' must be 'remembered' between restarts,
131*7c478bd9Sstevel@tonic-gate %   after the initialization, the empty database is checkpointed to record
132*7c478bd9Sstevel@tonic-gate %   the scheme. Returns TRUE if initialization succeeds; FALSE otherwise. */
133*7c478bd9Sstevel@tonic-gate %  bool_t init( db_scheme *s );
134*7c478bd9Sstevel@tonic-gate %
135*7c478bd9Sstevel@tonic-gate %/* Print out the database's statistics. */
136*7c478bd9Sstevel@tonic-gate %  void print()    { internal_db.print_stats();}
137*7c478bd9Sstevel@tonic-gate %
138*7c478bd9Sstevel@tonic-gate %/* Return whether the database has changed since its previous checkpoint. */
139*7c478bd9Sstevel@tonic-gate %  bool_t changedp() { return changed;}
140*7c478bd9Sstevel@tonic-gate %
141*7c478bd9Sstevel@tonic-gate %/* Return the version of the database. */
142*7c478bd9Sstevel@tonic-gate %  vers* get_version() { return internal_db.get_version(); }
143*7c478bd9Sstevel@tonic-gate %
144*7c478bd9Sstevel@tonic-gate %
145*7c478bd9Sstevel@tonic-gate %/* Execute 'action' using the rest of the arguments as input.
146*7c478bd9Sstevel@tonic-gate %   Return the result of the operation in a db_result structure;
147*7c478bd9Sstevel@tonic-gate %   Return NULL if the request is unknown.
148*7c478bd9Sstevel@tonic-gate %   If the action involves updates (ADD and REMOVE), it is logged first. */
149*7c478bd9Sstevel@tonic-gate %  db_result* execute( db_action, db_query *, entry_object *,
150*7c478bd9Sstevel@tonic-gate %			  db_next_desc* previous );
151*7c478bd9Sstevel@tonic-gate %
152*7c478bd9Sstevel@tonic-gate %/* Execute log entry 'j' on this db.  'j' is executed if its version is
153*7c478bd9Sstevel@tonic-gate %   later than that of the database; if executed, the database's version
154*7c478bd9Sstevel@tonic-gate %   will be changed to that of 'j', regardless of the status of the operation.
155*7c478bd9Sstevel@tonic-gate %   Returns TRUE if 'j' was executed;   FALSE if it was not.
156*7c478bd9Sstevel@tonic-gate %   Log entry is added to this database's log if log_entry is applied. */
157*7c478bd9Sstevel@tonic-gate %  bool_t execute_log_entry( db_log_entry * );
158*7c478bd9Sstevel@tonic-gate %
159*7c478bd9Sstevel@tonic-gate %/* Return structure db_log_list containing entries that are later
160*7c478bd9Sstevel@tonic-gate %   than the version 'v' given.  */
161*7c478bd9Sstevel@tonic-gate %  db_log_list * get_log_entries_since( vers * );
162*7c478bd9Sstevel@tonic-gate %
163*7c478bd9Sstevel@tonic-gate %/* Sync the table log file */
164*7c478bd9Sstevel@tonic-gate %  db_status	sync_log();
165*7c478bd9Sstevel@tonic-gate %
166*7c478bd9Sstevel@tonic-gate %/* Delete all files associated with database. */
167*7c478bd9Sstevel@tonic-gate %  int remove_files();
168*7c478bd9Sstevel@tonic-gate %
169*7c478bd9Sstevel@tonic-gate %  /* for debugging */
170*7c478bd9Sstevel@tonic-gate %/* Print information on all indices of the database to stdout. */
171*7c478bd9Sstevel@tonic-gate %  void print_all_indices() {internal_db.print_all_indices();}
172*7c478bd9Sstevel@tonic-gate %
173*7c478bd9Sstevel@tonic-gate %/* Print information on specified index of the the database to stdout. */
174*7c478bd9Sstevel@tonic-gate %  void print_index( int n ) {internal_db.print_index( n ); }
175*7c478bd9Sstevel@tonic-gate %
176*7c478bd9Sstevel@tonic-gate %/* Mark this instance deferred */
177*7c478bd9Sstevel@tonic-gate %  void markDeferred(void) {
178*7c478bd9Sstevel@tonic-gate %	internal_db.markDeferred();
179*7c478bd9Sstevel@tonic-gate %  }
180*7c478bd9Sstevel@tonic-gate %
181*7c478bd9Sstevel@tonic-gate %/* Remove defer marking */
182*7c478bd9Sstevel@tonic-gate %  void unmarkDeferred(void) {
183*7c478bd9Sstevel@tonic-gate %	internal_db.unmarkDeferred();
184*7c478bd9Sstevel@tonic-gate %  }
185*7c478bd9Sstevel@tonic-gate %
186*7c478bd9Sstevel@tonic-gate %/* Exclusive access to this instance */
187*7c478bd9Sstevel@tonic-gate %  int acqexcl(void) {
188*7c478bd9Sstevel@tonic-gate %	return (WLOCK(db));
189*7c478bd9Sstevel@tonic-gate %  }
190*7c478bd9Sstevel@tonic-gate %
191*7c478bd9Sstevel@tonic-gate %/* Non-blocking exclusive access */
192*7c478bd9Sstevel@tonic-gate %  int tryacqexcl(void) {
193*7c478bd9Sstevel@tonic-gate %	return (TRYWLOCK(db));
194*7c478bd9Sstevel@tonic-gate %  }
195*7c478bd9Sstevel@tonic-gate %
196*7c478bd9Sstevel@tonic-gate %/* Release exclusive access */
197*7c478bd9Sstevel@tonic-gate %  int relexcl(void) {
198*7c478bd9Sstevel@tonic-gate %	return (WULOCK(db));
199*7c478bd9Sstevel@tonic-gate %  }
200*7c478bd9Sstevel@tonic-gate %
201*7c478bd9Sstevel@tonic-gate %/* Non-exclusive (readonly) access to this instance */
202*7c478bd9Sstevel@tonic-gate %  int acqnonexcl(void) {
203*7c478bd9Sstevel@tonic-gate %	return (RLOCK(db));
204*7c478bd9Sstevel@tonic-gate %  }
205*7c478bd9Sstevel@tonic-gate %
206*7c478bd9Sstevel@tonic-gate %/* Release non-exlusive access */
207*7c478bd9Sstevel@tonic-gate %  int relnonexcl(void) {
208*7c478bd9Sstevel@tonic-gate %	return (RULOCK(db));
209*7c478bd9Sstevel@tonic-gate %  }
210*7c478bd9Sstevel@tonic-gate %
211*7c478bd9Sstevel@tonic-gate %/* Destroy instance lock */
212*7c478bd9Sstevel@tonic-gate %  int destroylock(void) {
213*7c478bd9Sstevel@tonic-gate %	return (DESTROYRW(db));
214*7c478bd9Sstevel@tonic-gate %  }
215*7c478bd9Sstevel@tonic-gate %
216*7c478bd9Sstevel@tonic-gate %/* Pass configuration information to the db_mindex */
217*7c478bd9Sstevel@tonic-gate %  bool_t configure(char *objName);
218*7c478bd9Sstevel@tonic-gate %
219*7c478bd9Sstevel@tonic-gate %/* Export a pointer to the db_mindex structure */
220*7c478bd9Sstevel@tonic-gate %  db_mindex *mindex(void);
221*7c478bd9Sstevel@tonic-gate %};
222*7c478bd9Sstevel@tonic-gate %
223*7c478bd9Sstevel@tonic-gate %typedef class db * dbp;
224*7c478bd9Sstevel@tonic-gate #endif USINGC
225*7c478bd9Sstevel@tonic-gate #endif RPC_HDR
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate #if RPC_HDR
228*7c478bd9Sstevel@tonic-gate %#endif _DB_DB_H
229*7c478bd9Sstevel@tonic-gate #endif RPC_HDR
230