4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
25 * Copyright (c) 1988-2000 by Sun Microsystems, Inc.
26 * All Rights Reserved.
29 #pragma ident "%Z%%M% %I% %E% SMI"
42 #include "db_headers.h"
43 #include "db_dictlog.h"
49 * Constructor: Create a log entry using the given parameters. Note that
50 * pointers to db_query and entry_object are simply assigned, not copied.
52 db_dictlog_entry::db_dictlog_entry(int a
, vers
* v
, char *tname
,
63 db_dictlog_entry::~db_dictlog_entry()
65 /* we might not have allocated these ourselves, so we cannot delete them */
68 /* prints a line from the journal */
70 db_dictlog_entry::print()
80 printf ("action(%d): ", action
);
84 aversion
.print(stdout
);
86 if (table_name
!= NULL
)
87 printf ("table %s\n", table_name
);
89 printf("no table!\n");
90 bversion
.print(stdout
);
95 free_table_entry(table_obj
* obj
)
100 if (obj
->ta_type
!= NULL
)
103 table_col
* tcs
= obj
->ta_cols
.ta_cols_val
;
105 for (i
= 0; i
< obj
->ta_cols
.ta_cols_len
; i
++) {
106 if (tcs
[i
].tc_name
!= NULL
)
107 delete tcs
[i
].tc_name
;
111 if (obj
->ta_path
!= NULL
)
117 delete_log_entry(db_dictlog_entry
*lentry
)
122 if ((tname
= lentry
->get_table_name())) {
125 if ((obj
= lentry
->get_table_object())) {
126 free_table_entry(obj
);
133 * Execute given function 'func' on log.
134 * function takes as arguments: pointer to log entry, character pointer to
135 * another argument, and pointer to an integer, which is used as a counter.
136 * 'func' should increment this value for each successful application.
137 * The log is traversed until either 'func' returns FALSE, or when the log
138 * is exhausted. The second argument to 'execute_on_log' is passed as the
139 * second argument to 'func'. The third argument, 'clean' determines whether
140 * the log entry is deleted after the function has been applied.
141 * Returns the number of times that 'func' incremented its third argument.
144 db_dictlog::execute_on_log(bool_t (*func
) (db_dictlog_entry
*,
146 char* dict
, bool_t clean
)
152 WRITELOCK(this, 0, "w db_dictlog::execute_on_log");
153 if (open() == FALSE
) { // open log
154 WRITEUNLOCK(this, 0, "wu db_dictlog::execute_on_log");
161 if ((*func
)(j
, dict
, &count
) == FALSE
) done
= TRUE
;
162 if (clean
) delete_log_entry(j
);
166 WRITEUNLOCK(this, count
, "wu db_dictlog::execute_on_log");
171 print_log_entry(db_dictlog_entry
*j
, char*, int *count
)
178 /* Print contents of log file to stdout */
182 return (execute_on_log(&(print_log_entry
), NULL
));
186 * Return the next element in current log; return NULL if end of log or error.
187 * Log must have been opened for READ.
194 READLOCK(this, NULL
, "r db_dictlog::get");
195 if (mode
!= PICKLE_READ
) {
196 READUNLOCK(this, NULL
, "ru db_dictlog::get");
200 j
= new db_dictlog_entry
;
203 READUNLOCK(this, NULL
, "ru db_dictlog::get");
206 if (xdr_db_dictlog_entry(&(xdr
), j
) == FALSE
) {
207 delete_log_entry (j
);
208 /* WARNING("Could not sucessfully finish reading log"); */
209 READUNLOCK(this, NULL
, "ru db_dictlog::get");
213 WARNING("truncated log entry found");
217 READUNLOCK(this, j
, "ru db_dictlog::get");
221 /* Append given log entry to log. */
223 db_dictlog::append(db_dictlog_entry
*j
)
227 WRITELOCK(this, -1, "w db_dictlog::append");
228 if (mode
!= PICKLE_APPEND
) {
229 WRITEUNLOCK(this, -1, "wu db_dictlog::append");
233 /* xdr returns TRUE if successful, FALSE otherwise */
234 status
= ((xdr_db_dictlog_entry(&(xdr
), j
)) ? 0 : -1);
236 WARNING("db_dictlog: could not write log entry");
237 WRITEUNLOCK(this, status
, "wu db_dictlog::append");
241 status
= fflush(file
);
243 WARNING("db_dictlog: could not flush log entry to disk");
244 WRITEUNLOCK(this, status
, "wu db_dictlog::append");
248 status
= fsync(fileno(file
));
250 WARNING("db_dictlog: could not sync log entry to disk");
253 WRITEUNLOCK(this, status
, "wu db_dictlog::append");