[srvx-commits] CVS: services/src saxdb.c,1.8,1.9
Brought to you by:
entrope
|
From: Entrope <en...@us...> - 2002-08-27 02:09:02
|
Update of /cvsroot/srvx/services/src
In directory usw-pr-cvs1:/tmp/cvs-serv7404/src
Modified Files:
saxdb.c
Log Message:
fix "mondo" database (it wouldn't read and write in the proper order)
Index: saxdb.c
===================================================================
RCS file: /cvsroot/srvx/services/src/saxdb.c,v
retrieving revision 1.8
retrieving revision 1.9
diff -C2 -r1.8 -r1.9
*** saxdb.c 21 Aug 2002 05:41:03 -0000 1.8
--- saxdb.c 27 Aug 2002 02:08:59 -0000 1.9
***************
*** 35,38 ****
--- 35,39 ----
unsigned int write_interval;
time_t last_write;
+ struct saxdb *prev;
};
***************
*** 46,53 ****
--- 47,56 ----
#define COMPLEX(CTX) ((CTX)->complex.list[(CTX)->complex.used-1])
+ static struct saxdb *last_db;
static struct saxdb *mondo_db;
static struct dict *saxdbs; /* -> struct saxdb */
static struct dict *mondo_sections; /* -> struct saxdb */
static struct module *saxdb_module;
+ static int use_mondo;
static void
***************
*** 55,58 ****
--- 58,62 ----
struct dict *data;
+ assert(db);
assert(db->filename);
data = parse_database(db->filename);
***************
*** 77,81 ****
dict_insert(saxdbs, db->name, db);
sprintf(mondo_path, "dbs/%s/mondo_section", name);
! if (!conf_get_data(mondo_path, RECDB_QSTRING)) saxdb_read_db(db);
return db;
}
--- 81,87 ----
dict_insert(saxdbs, db->name, db);
sprintf(mondo_path, "dbs/%s/mondo_section", name);
! if (reader && !conf_get_data(mondo_path, RECDB_QSTRING)) saxdb_read_db(db);
! db->prev = last_db;
! last_db = db;
return db;
}
***************
*** 88,92 ****
assert(db->filename);
! sprintf(tmp_fname, "%s.new", db->name);
memset(&ctx, 0, sizeof(ctx));
ctx.output = fopen(tmp_fname, "w+");
--- 94,98 ----
assert(db->filename);
! sprintf(tmp_fname, "%s.new", db->filename);
memset(&ctx, 0, sizeof(ctx));
ctx.output = fopen(tmp_fname, "w+");
***************
*** 114,117 ****
--- 120,124 ----
}
db->last_write = now;
+ log(MAIN_LOG, LOG_INFO, "Wrote %s database to disk.\n", db->name);
return 0;
}
***************
*** 120,126 ****
saxdb_timed_write(void *data) {
struct saxdb *db = data;
! if (!saxdb_write_db(db)) {
! log(MAIN_LOG, LOG_ERROR, "Successfully made timed write of database %s\n", db->filename);
! }
timeq_add(now + db->write_interval, saxdb_timed_write, db);
}
--- 127,131 ----
saxdb_timed_write(void *data) {
struct saxdb *db = data;
! saxdb_write_db(db);
timeq_add(now + db->write_interval, saxdb_timed_write, db);
}
***************
*** 264,302 ****
}
! static SAXDB_READER(saxdb_mondo_reader) {
! struct saxdb *saxdb;
! dict_iterator_t it;
! struct record_data *rd;
int res;
! for (it = dict_first(db); it; it = iter_next(it)) {
! saxdb = dict_find(mondo_sections, iter_key(it), NULL);
! if (!saxdb) continue;
! rd = iter_data(it);
! if (rd->type == RECDB_OBJECT) {
! res = saxdb->reader(rd->d.object);
! if (res) return res;
! }
}
return 0;
}
! static SAXDB_WRITER(saxdb_mondo_writer) {
! struct saxdb *saxdb;
! dict_iterator_t it;
! int res;
! for (it = dict_first(mondo_sections); it; it = iter_next(it)) {
! saxdb = iter_data(it);
! saxdb_start_record(ctx, iter_key(it), 1);
! res = saxdb->writer(ctx);
! if (res) return res;
! /* cheat a little here ;) */
! saxdb_put_char(ctx, '\n');
saxdb_end_record(ctx);
}
return 0;
}
static void
saxdb_conf_read(void) {
--- 269,312 ----
}
! static int
! saxdb_mondo_read(struct dict *db, struct saxdb *saxdb) {
int res;
+ struct dict *subdb;
! if (saxdb->prev && (res = saxdb_mondo_read(db, saxdb->prev))) return res;
! if (saxdb->mondo_section
! && (subdb = database_get_data(db, saxdb->mondo_section, RECDB_OBJECT))
! && (res = saxdb->reader(subdb))) {
! log(MAIN_LOG, LOG_OTHER, " mondo section read for %s failed: %d\n", saxdb->mondo_section, res);
! return res;
}
return 0;
}
! static SAXDB_READER(saxdb_mondo_reader) {
! return saxdb_mondo_read(db, last_db);
! }
! static int
! saxdb_mondo_write(struct saxdb_context *ctx, struct saxdb *saxdb) {
! int res;
! if (saxdb->prev && (res = saxdb_mondo_write(ctx, saxdb->prev))) return res;
! if (saxdb->mondo_section) {
! saxdb_start_record(ctx, saxdb->mondo_section, 1);
! if ((res = saxdb->writer(ctx))) {
! log(MAIN_LOG, LOG_OTHER, " mondo section write for %s failed: %d\n", saxdb->mondo_section, res);
! return res;
! }
saxdb_end_record(ctx);
+ /* cheat a little here to put a newline between mondo sections */
+ saxdb_put_char(ctx, '\n');
}
return 0;
}
+ static SAXDB_WRITER(saxdb_mondo_writer) {
+ return saxdb_mondo_write(ctx, last_db);
+ }
+
static void
saxdb_conf_read(void) {
***************
*** 311,314 ****
--- 321,325 ----
if (!conf) return;
timeq_del(0, saxdb_timed_write, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
+ use_mondo = 0;
for (it = dict_first(conf); it; it = iter_next(it)) {
rd = iter_data(it);
***************
*** 323,326 ****
--- 334,338 ----
db->mondo_section = strdup(str);
dict_insert(mondo_sections, db->mondo_section, db);
+ use_mondo = 1;
}
if ((str = database_get_data(node, "filename", RECDB_QSTRING))) {
***************
*** 336,344 ****
}
}
- if ((node = database_get_data(conf, "mondo", RECDB_OBJECT))) {
- mondo_db = dict_find(saxdbs, "mondo", NULL);
- } else {
- mondo_db = NULL;
- }
}
--- 348,351 ----
***************
*** 399,403 ****
dict_set_free_data(saxdbs, saxdb_free);
mondo_sections = dict_new();
! saxdb_register("mondo", saxdb_mondo_reader, saxdb_mondo_writer);
saxdb_module = module_register("saxdb", MAIN_LOG, "saxdb.help", NULL);
modcmd_register(saxdb_module, "write", cmd_write, 2, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
--- 406,411 ----
dict_set_free_data(saxdbs, saxdb_free);
mondo_sections = dict_new();
! mondo_db = saxdb_register("mondo", NULL, saxdb_mondo_writer);
! mondo_db->reader = saxdb_mondo_reader;
saxdb_module = module_register("saxdb", MAIN_LOG, "saxdb.help", NULL);
modcmd_register(saxdb_module, "write", cmd_write, 2, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
***************
*** 407,409 ****
--- 415,418 ----
void saxdb_finalize(void) {
conf_register_reload(saxdb_conf_read);
+ if (use_mondo) saxdb_read_db(mondo_db);
}
|