static rc_t DbInit(rc_t rc, const CmdLine* args, Db* db) { const char name[] = "READ_FILTER"; assert(args && db); memset(db, 0, sizeof *db); if (rc != 0) { return rc; } db->table = args->table; if (rc == 0) { rc = VDBManagerMakeUpdate(&db->mgr, NULL); DISP_RC(rc, "while calling VDBManagerMakeUpdate"); } if (rc == 0) { rc = VDBManagerWritable(db->mgr, args->table); if (rc != 0) { if (GetRCState(rc) == rcLocked) { rc = VDBManagerUnlock(db->mgr, args->table); if (rc != 0) { PLOGERR(klogErr, (klogErr, rc, "while calling VDBManagerUnlock('$(table)')", "table=%s", args->table)); } db->locked = true; } else { PLOGERR(klogErr, (klogErr, rc, "while calling VDBManagerWritable('$(table)')", "table=%s", args->table)); if (rc == RC(rcDB, rcPath, rcAccessing, rcPath, rcReadonly)) { PLOGERR(klogErr, (klogErr, rc, "N.B. It is possible '$(table)' was not locked properly" , "table=%s", args->table)); } } } } if (rc == 0) { db->locked = true; /* has to be locked in production mode */ rc = VDBManagerOpenTableUpdate (db->mgr, &db->tbl, NULL, args->table); if (rc != 0) { VDatabase *vdb; rc_t rc2 = VDBManagerOpenDBUpdate ( db->mgr, &vdb, NULL , args->table ); if( rc2 == 0) { rc2 = VDatabaseOpenTableUpdate ( vdb, &db->tbl, "SEQUENCE" ); if (rc2 == 0 ) rc = 0; VDatabaseRelease ( vdb ); } } if(rc != 0){ PLOGERR(klogErr, (klogErr, rc, "while opening VTable '$(table)'", "table=%s", args->table)); } } if( rc == 0) { rc = VTableCreateCursorRead(db->tbl, &db->rCursor); DISP_RC(rc, "while creating read cursor"); if (rc == 0) { rc = VCursorAddColumn(db->rCursor, &db->rFilterIdx, "%s", name); if (rc != 0) { PLOGERR(klogErr, (klogErr, rc, "while adding $(name) to read cursor", "name=%s", name)); } } if (rc == 0) { rc = VCursorOpen(db->rCursor); DISP_RC(rc, "while opening read cursor"); } } if (rc == 0) { rc = VTableCreateCursorWrite(db->tbl, &db->wCursor, kcmInsert); DISP_RC(rc, "while creating write cursor"); if (rc == 0) { rc = VCursorAddColumn(db->wCursor, &db->wIdx, "%s", name); if (rc != 0) { PLOGERR(klogErr, (klogErr, rc, "while adding $(name) to write cursor", "name=%s", name)); } } if (rc == 0) { rc = VCursorOpen(db->wCursor); DISP_RC(rc, "while opening write cursor"); } } return rc; }
rc_t CC KMain(int argc, char* argv[]) { const char table[] = "/home/klymenka/REDACT-IN"; const char name[] = "READ_FILTER"; rc_t rc = 0; bool locked = false; VDBManager* mgr; VTable *tbl; const VCursor *rCursor = NULL; int i; LogLevelSet("info"); for (i = 1; i < argc; ++i) { if (!strcmp(argv[i], "-+")) { if (++i <= argc) { #if _DEBUGGING KDbgSetString(argv[i]); #endif } } } /*KDbgSetString("VDB");*/ if (rc == 0) { /* +01: ManagerMake */ LOGMSG(klogInfo, "VDBManagerMakeUpdate"); rc = VDBManagerMakeUpdate(&mgr, NULL); DISP_RC_INT(rc, "while calling VDBManagerMakeUpdate"); } if (rc == 0) { rc = VDBManagerWritable(mgr, table); if (GetRCState(rc) == rcLocked) { LOGMSG(klogInfo, "VDBManagerUnlock"); rc = VDBManagerUnlock(mgr, table); DISP_RC_INT(rc, "while calling VDBManagerUnlock"); locked = true; } } if (rc == 0) { /* +02: OpenTable */ PLOGMSG(klogInfo, (klogInfo, "VDBManagerOpenTableUpdate(\"$(t)\")", "t=%s", table)); rc = VDBManagerOpenTableUpdate (mgr, &tbl, NULL, table); if (rc != 0) { PLOGERR(klogErr, (klogErr, rc, "while opening VTable '$(path)'", "path=%s", table)); } } if (rc == 0) { /* +03: CreateCursorRead */ LOGMSG(klogInfo, "VDBManagerUnlock"); rc = VTableCreateCursorRead(tbl, &rCursor); DISP_RC_INT(rc, "while creating read cursor"); #if 1 if (rc == 0) { uint32_t idx; PLOGMSG(klogInfo, (klogInfo, "VCursorAddColumn(read cursor, \"$(n)\")", "n=%s", name)); rc = VCursorAddColumn(rCursor, &idx, name); if (rc != 0) { PLOGERR(klogErr, (klogErr, rc, "while adding $(name) to read cursor", "name=%s", name)); } } #endif if (rc == 0) { LOGMSG(klogInfo, "VCursorOpen(read cursor)"); rc = VCursorOpen(rCursor); DISP_RC_INT(rc, "while opening read cursor"); } } if (rc == 0) { VCursor *cursor; uint32_t idx; /* +04: CreateCursorWrite */ LOGMSG(klogInfo, "VTableCreateCursorWrite"); rc = VTableCreateCursorWrite(tbl, &cursor, kcmInsert); DISP_RC_INT(rc, "while creating write cursor"); if (rc == 0) { PLOGMSG(klogInfo, (klogInfo, "VCursorAddColumn(write cursor, \"$(n)\")", "n=%s", name)); rc = VCursorAddColumn(cursor, &idx, name); if (rc != 0) { PLOGERR(klogErr, (klogErr, rc, "while adding $(name) to write cursor", "name=%s", name)); } } if (rc == 0) { LOGMSG(klogInfo, "VCursorOpen(write cursor)"); rc = VCursorOpen(cursor); DISP_RC_INT(rc, "while opening write cursor"); } #if 1 for (i = 0; i < 3 && rc == 0; ++i) { if (rc == 0) { PLOGMSG(klogInfo, (klogInfo, "VCursorOpenRow(write cursor) $(i)", "i=%d", i)); rc = VCursorOpenRow(cursor); DISP_RC_INT(rc, "while opening row to write"); } if (rc == 0) { char buffer[1]; char b; switch (i) { case 0: buffer[0] = SRA_READ_FILTER_CRITERIA; buffer[0] = SRA_READ_FILTER_REJECT; break; case 1: buffer[0] = SRA_READ_FILTER_REJECT; buffer[0] = SRA_READ_FILTER_CRITERIA; break; case 2: buffer[0] = SRA_READ_FILTER_REDACTED; break; } buffer[0] = SRA_READ_FILTER_PASS; b = buffer[0]; PLOGMSG(klogInfo, (klogInfo, "VCursorWrite('$(v)') $(i)", "v=%s,i=%d", b == SRA_READ_FILTER_REDACTED ? "SRA_READ_FILTER_REDACTED" : "?", i)); rc = VCursorWrite(cursor, idx, 8, buffer, 0, 1); DISP_RC_INT(rc, "while writing"); } if (rc == 0) { PLOGMSG(klogInfo, (klogInfo, "VCursorCommitRow(write cursor) $(i)", "i=%d", i)); rc = VCursorCommitRow(cursor); DISP_RC_INT(rc, "while committing row"); } PLOGMSG(klogInfo, (klogInfo, "VCursorCloseRow(write cursor) $(i)", "i=%d", i)); { rc_t rc2 = VCursorCloseRow(cursor); DISP_RC_INT(rc2, "while closing row"); if (rc == 0) { rc = rc2; } } } #endif LOGMSG(klogInfo, "VCursorRelease(read cursor)"); /* -03: CreateCursorRead */ VCursorRelease(rCursor); if (rc == 0) { LOGMSG(klogInfo, "VCursorCommit(write cursor)"); rc = VCursorCommit(cursor); DISP_RC_INT(rc, "while committing cursor"); } LOGMSG(klogInfo, "VCursorRelease(write cursor)"); /* -04: CreateCursorWrite */ VCursorRelease(cursor); } LOGMSG(klogInfo, "VTableRelease"); /* -02: OpenTable */ VTableRelease(tbl); LOGMSG(klogInfo, "VDBManagerLock"); if (locked) { rc_t rc2 = VDBManagerLock(mgr, table); DISP_RC_INT(rc2, "while VDBManagerLock"); } /* -01: ManagerMake */ LOGMSG(klogInfo, "VDBManagerRelease"); VDBManagerRelease(mgr); if (rc == 0) { LOGMSG(klogInfo, "SUCCESS"); } else { LOGMSG(klogInfo, "FAILURE"); } return rc; }
rc_t matcher_execute( matcher* self, const p_matcher_input in ) { VSchema * dflt_schema; const VTable * src_table; rc_t rc; if ( self == NULL ) return RC( rcExe, rcNoTarg, rcResolving, rcSelf, rcNull ); if ( in->manager == NULL || in->add_schemas == NULL || in->cfg == NULL || in->columns == NULL || in->src_path == NULL || in->dst_path == NULL || in->dst_tabname == NULL ) return RC( rcExe, rcNoTarg, rcResolving, rcParam, rcNull ); rc = matcher_build_column_vector( self, in->columns ); if ( rc != 0 ) return rc; rc = matcher_exclude_columns( self, in->excluded_columns ); if ( rc != 0 ) return rc; rc = helper_parse_schema( in->manager, &dflt_schema, in->add_schemas ); if ( rc != 0 ) return rc; rc = VDBManagerOpenTableRead( in->manager, &src_table, dflt_schema, "%s", in->src_path ); if ( rc == 0 ) { const VSchema * src_schema; rc = VTableOpenSchema ( src_table, &src_schema ); if ( rc == 0 ) { rc = matcher_read_src_types( self, src_table, src_schema ); if ( rc == 0 ) { if ( in->legacy_schema != NULL ) rc = VSchemaParseFile ( dflt_schema, "%s", in->legacy_schema ); if ( rc == 0 ) { VTable * dst_table; KCreateMode cmode = kcmParents; const VSchema * dst_schema = src_schema; if ( in->legacy_schema != NULL ) dst_schema = dflt_schema; if ( in->force_unlock ) VDBManagerUnlock ( in->manager, "%s", in->dst_path ); if ( in->force_kcmInit ) cmode |= kcmInit; else cmode |= kcmCreate; rc = VDBManagerCreateTable( in->manager, &dst_table, dst_schema, in->dst_tabname, cmode, "%s", in->dst_path ); if ( rc == 0 ) { rc = matcher_read_dst_types( self, dst_table, dst_schema ); if ( rc == 0 ) { rc = matcher_make_type_matrix( self ); if ( rc == 0 ) rc = matcher_match_matrix( self, src_schema, in->cfg ); } VTableRelease( dst_table ); if ( !(in->force_kcmInit) ) KDirectoryRemove ( in->dir, true, "%s", in->dst_path ); } } } VSchemaRelease( src_schema ); } VTableRelease( src_table ); } VSchemaRelease( dflt_schema ); return rc; }