ctdlload: skeleton code for table ingestion
authorArt Cancro <ajc@citadel.org>
Thu, 13 Jul 2023 15:34:07 +0000 (06:34 -0900)
committerArt Cancro <ajc@citadel.org>
Thu, 13 Jul 2023 15:34:07 +0000 (06:34 -0900)
citadel/utils/ctdlload.c

index 158635005f1699c6082c1c8cb92da3faa79d1ce0..430486152a15b3e67984094efa45c2476ded39de 100644 (file)
@@ -116,27 +116,76 @@ void close_dbenv(DB_ENV *dbenv) {
 
 
 // Ingest one line of dump data.  NOT REENTRANT
-void ingest_one(char *line) {
-       static char last_cdb[16] = "";
-       char this_cdb[16];
+void ingest_one(char *line, DB_ENV *dst_dbenv) {
+
+       static int previous_cdb = -1 ;
+       static int current_cdb = -1 ;
+       static DB *dst_dbp;
+       char record_type[32];
+       int ret;
+       char dbfilename[32];
 
        // We are assuming that the lines of the dump file will generally be sorted by table.
        // By remembering the last table we worked with, we can do close/open if the table changes.
 
-       extract_token(this_cdb, line, 0, '|', sizeof this_cdb);
-       if (strcmp(this_cdb, last_cdb)) {
+       extract_token(record_type, line, 0, '|', sizeof record_type);
+
+       if (!strcasecmp(record_type, "msgtext"))                current_cdb = CDB_MSGMAIN;
+       else if (!strcasecmp(record_type, "msgmeta"))           current_cdb = CDB_MSGMAIN;
+       else if (!strcasecmp(record_type, "user"))              current_cdb = CDB_USERS;
+       else if (!strcasecmp(record_type, "room"))              current_cdb = CDB_ROOMS;
+       else if (!strcasecmp(record_type, "floor"))             current_cdb = CDB_FLOORTAB;
+       else if (!strcasecmp(record_type, "msglist"))           current_cdb = CDB_MSGLISTS;
+       else if (!strcasecmp(record_type, "visit"))             current_cdb = CDB_VISIT;
+       else if (!strcasecmp(record_type, "dir"))               current_cdb = CDB_DIRECTORY;
+       else if (!strcasecmp(record_type, "use"))               current_cdb = CDB_USETABLE;
+       else if (!strcasecmp(record_type, "bigmsg"))            current_cdb = CDB_BIGMSGS;
+       else if (!strcasecmp(record_type, "euidindex"))         current_cdb = CDB_EUIDINDEX;
+       else if (!strcasecmp(record_type, "usersbynumber"))     current_cdb = CDB_USERSBYNUMBER;
+       else if (!strcasecmp(record_type, "config"))            current_cdb = CDB_CONFIG;
+       else                                                    current_cdb = -1 ;
+
+
+       if (current_cdb != previous_cdb) {
+
+               if (previous_cdb >= 0) {
+                       fprintf(stderr, "Close %d\n", previous_cdb);
+                       ret = dst_dbp->close(dst_dbp, 0);
+                       if (ret) {
+                               fprintf(stderr, "db: db_close: %s\n", db_strerror(ret));
+                       }
+               }
+
+               if (current_cdb >= 0) {
+                       fprintf(stderr, " Open %d\n", current_cdb);
+                       snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", current_cdb);
+
+                       // create a database handle for the destination table
+                       ret = db_create(&dst_dbp, dst_dbenv, 0);
+                       if (ret) {
+                               fprintf(stderr, "db: db_create: %s\n", db_strerror(ret));
+                               fprintf(stderr, "db: exit code %d\n", ret);
+                               exit(CTDLEXIT_DB);
+                       }
+               
+                       // open the file containing the destination table
+                       ret = dst_dbp->open(dst_dbp, NULL, dbfilename, NULL, DB_BTREE, (DB_CREATE | DB_TRUNCATE), 0600);
+                       if (ret) {
+                               fprintf(stderr, "db: db_open: %s\n", db_strerror(ret));
+                               fprintf(stderr, "db: exit code %d\n", ret);
+                               exit(CTDLEXIT_DB);
+                       }
+               }
 
-               printf("Close %s\n", last_cdb);
-               printf(" Open %s\n", this_cdb);
 
-               strcpy(last_cdb, this_cdb);
+               previous_cdb = current_cdb;
        }
 
 }
 
 
 // This is the loop that loads the dump data.  NOT REENTRANT
-void ingest(void) {
+void ingest(DB_ENV *dst_dbenv) {
        static size_t line_alloc = 1;
        static char *line;
        static size_t line_len = 0;
@@ -161,7 +210,7 @@ void ingest(void) {
                }
 
                if (line_len > 0) {
-                       ingest_one(line);
+                       ingest_one(line, dst_dbenv);
                }
 
        } while (ch >= 0);
@@ -203,7 +252,7 @@ int main(int argc, char **argv) {
        system(cmd);
 
        dst_dbenv = open_dbenv(dst_dir);
-       ingest();
+       ingest(dst_dbenv);
        close_dbenv(dst_dbenv);
 
        exit(0);