CURRINT_REPOSITORY_TEMPL_ Guard<Obj,wait_m>& RepositoryBase<CURRINT_REPOSITORY_T_> // ::replace_object (const ObjId& id, const Par& param/*, bool freeMemory*/) { RLOCK(objectsM); Obj* obj = 0; try { obj = objects->at (id).get(); } catch (const std::out_of_range&) { THROW_EXCEPTION(NoSuchId, id); } if (!obj) THROW_PROGRAM_ERROR; //obj->freeze(); (*objects)[id].charge( dynamic_cast<Obj*>(param.transform_object (obj)) ); SCHECK ((*objects)[id]); if ((*objects)[id].get() != obj) if (true /*freeMemory*/) delete obj; return (*objects)[id]; }
Out RepositoryBase<CURRINT_REPOSITORY_T_> // ::get_object_ids_by_pred (Out res, Pred p) const { RLOCK(objectsM); for (ObjId i = 0; i < objects->size (); i++) if ((*objects)[i] && p (*(*objects)[i])) *res++ = i; return res; }
CURRINT_REPOSITORY_TEMPL_ Guard<Obj,wait_m>& RepositoryBase <CURRINT_REPOSITORY_T_> // ::get_object_by_id (const ObjId& id) const { try { RLOCK(objectsM); return objects->at (id); } catch (const std::out_of_range&) { THROW_EXCEPTION_PLACE(get_object_by_id, NoSuchId, id); } }
CURRINT_REPOSITORY_TEMPL_ void RepositoryBase<CURRINT_REPOSITORY_T_> // ::for_each(std::function<void(const Guard<Obj,wait_m>&)> f) const { RLOCK(objectsM); for (const auto& v : *objects) { const Guard<Obj,wait_m>& g = ConstValue(v); if (g) f(g); //f (const_cast<const Guard<Obj,wait_m>&>(*ptr)); } }
CURRINT_REPOSITORY_TEMPL_ void RepositoryBase<CURRINT_REPOSITORY_T_> // ::for_each(std::function<void(Guard<Obj,wait_m>&)> f) { RLOCK(objectsM); for (auto& v : *objects) { Value vv(v); Guard<Obj,wait_m>& g = vv; if (g) f(g); } }
CURRINT_REPOSITORY_TEMPL_ void RepositoryBase<CURRINT_REPOSITORY_T_> // ::delete_object_by_id (const ObjId& id/*, bool freeMemory*/) { Obj* ptr = 0; { RLOCK(objectsM); try { ptr = objects->at (id).get(); delete_object_id(id); if (ptr == 0) THROW_EXCEPTION(NoSuchId, id); } catch (const std::out_of_range&) { THROW_EXCEPTION(NoSuchId, id); } } //ptr->freeze(); if (true /*freeMemory*/) delete ptr; }
CURRINT_REPOSITORY_TEMPL_ Guard<Obj,wait_m>& RepositoryBase<CURRINT_REPOSITORY_T_> // ::create_object (const Par& param) { Guard<Obj,wait_m>* res = nullptr; Obj* obj = 0; // <NB> cinfo.objectId is empty at the first call to // param ObjectCreationInfo cinfo; cinfo.repository = this; { RLOCK (objectsM); const ObjId objId = allocate_new_object_id_internal (cinfo, param); toString (objId, cinfo.objectId); // dynamic cast for use with inherited parameters // <NB> this must be called inside the lock to allow // query repo data structures from create_derivation obj = dynamic_cast<Obj*> (param.create_derivation (cinfo)); SCHECK (obj); res = &insert_object (objId, obj); } LOG_TRACE(log, "Object " << *obj << " is created."); if (cinfo.objectCreated) cinfo.objectCreated->set(); // <NB> after inserting into repo // and unlocking assert(res); return *res; }
CURRINT_SPARK_REPOSITORY_TEMPL_ List<Guard<Obj,wait_m>*> SparkRepository<CURRINT_SPARK_REPOSITORY_T_> // ::create_several_objects(Par& param) { List<Guard<Obj,wait_m>*> out; Obj* obj = 0; ObjectCreationInfo cinfo; cinfo.repository = this; { RLOCK (this->objectsM); const size_t n = param.n_objects(cinfo); // count objects to be created (it also can create // them) for (size_t k = 0; k < n; k++) { cinfo.objectId.clear(); const ObjId objId = this->allocate_new_object_id_internal(cinfo, param); toString(objId, cinfo.objectId); // dynamic cast for use with inherited parameters obj = dynamic_cast<Obj*> (param.create_next_derivation (cinfo)); SCHECK (obj); out.push_back(&this->insert_object(objId, obj)); LOG_TRACE(log, "Object " << *obj << " is created."); if (cinfo.objectCreated) cinfo.objectCreated->set(); } } return out; }
int main(int argc, char **argv) { char *shmname = NULL; void *shmptr = NULL; int i, scan_to; gint shmsize; wg_int rlock = 0; wg_int wlock = 0; /* look for commands in argv[1] or argv[2] */ if(argc < 3) scan_to = argc; else scan_to = 3; shmsize = 0; /* 0 size causes default size to be used */ /* 1st loop through, shmname is NULL for default. If * the first argument is not a recognizable command, it * is assumed to be the shmname and the next argument * is checked against known commands. */ for(i=1; i<scan_to; i++) { if (!strcmp(argv[i],"help") || !strcmp(argv[i],"-h")) { usage(argv[0]); exit(0); } if (!strcmp(argv[i],"version") || !strcmp(argv[i],"-v")) { wg_print_code_version(); exit(0); } if (!strcmp(argv[i],"free")) { /* free shared memory */ wg_delete_database(shmname); exit(0); } if(argc>(i+1) && !strcmp(argv[i],"import")){ wg_int err, minsize, maxsize; int flags = 0; if(argv[i+1][0] == '-') { flags = parse_flag(argv[++i]); if(argc<=(i+1)) { /* Filename argument missing */ usage(argv[0]); exit(1); } } err = wg_check_dump(NULL, argv[i+1], &minsize, &maxsize); if(err) { fprintf(stderr, "Import failed.\n"); break; } shmptr=wg_attach_memsegment(shmname, minsize, maxsize, 1, (flags & FLAGS_LOGGING)); if(!shmptr) { fprintf(stderr, "Failed to attach to database.\n"); exit(1); } /* Locking is handled internally by the dbdump.c functions */ err = wg_import_dump(shmptr,argv[i+1]); if(!err) printf("Database imported.\n"); else if(err<-1) fprintf(stderr, "Fatal error in wg_import_dump, db may have"\ " become corrupt\n"); else fprintf(stderr, "Import failed.\n"); break; } else if(argc>(i+1) && !strcmp(argv[i],"export")){ wg_int err; int flags = 0; if(argv[i+1][0] == '-') { flags = parse_flag(argv[++i]); if(argc<=(i+1)) { /* Filename argument missing */ usage(argv[0]); exit(1); } } shmptr=wg_attach_database(shmname, shmsize); if(!shmptr) { fprintf(stderr, "Failed to attach to database.\n"); exit(1); } /* Locking is handled internally by the dbdump.c functions */ if(flags & FLAGS_FORCE) err = wg_dump_internal(shmptr,argv[i+1], 0); else err = wg_dump(shmptr,argv[i+1]); if(err<-1) fprintf(stderr, "Fatal error in wg_dump, db may have"\ " become corrupt\n"); else if(err) fprintf(stderr, "Export failed.\n"); break; } #ifdef USE_DBLOG else if(argc>(i+1) && !strcmp(argv[i],"replay")){ wg_int err; shmptr=wg_attach_database(shmname, shmsize); if(!shmptr) { fprintf(stderr, "Failed to attach to database.\n"); exit(1); } WLOCK(shmptr, wlock); err = wg_replay_log(shmptr,argv[i+1]); WULOCK(shmptr, wlock); if(!err) printf("Log suggessfully imported from file.\n"); else if(err<-1) fprintf(stderr, "Fatal error when importing, database may have "\ "become corrupt\n"); else fprintf(stderr, "Failed to import log (database unmodified).\n"); break; } #endif else if(argc>(i+1) && !strcmp(argv[i],"exportcsv")){ shmptr=wg_attach_database(shmname, shmsize); if(!shmptr) { fprintf(stderr, "Failed to attach to database.\n"); exit(1); } RLOCK(shmptr, wlock); wg_export_db_csv(shmptr,argv[i+1]); RULOCK(shmptr, wlock); break; } else if(argc>(i+1) && !strcmp(argv[i],"importcsv")){ wg_int err; shmptr=wg_attach_database(shmname, shmsize); if(!shmptr) { fprintf(stderr, "Failed to attach to database.\n"); exit(1); } WLOCK(shmptr, wlock); err = wg_import_db_csv(shmptr,argv[i+1]); WULOCK(shmptr, wlock); if(!err) printf("Data imported from file.\n"); else if(err<-1) fprintf(stderr, "Fatal error when importing, data may be partially"\ " imported\n"); else fprintf(stderr, "Import failed.\n"); break; } #ifdef USE_REASONER else if(argc>(i+1) && !strcmp(argv[i],"importprolog")){ wg_int err; shmptr=wg_attach_database(shmname, shmsize); if(!shmptr) { fprintf(stderr, "Failed to attach to database.\n"); exit(1); } err = wg_import_prolog_file(shmptr,argv[i+1]); if(!err) printf("Data imported from prolog file.\n"); else if(err<-1) fprintf(stderr, "Fatal error when importing, data may be partially"\ " imported\n"); else fprintf(stderr, "Import failed.\n"); break; } else if(argc>(i+1) && !strcmp(argv[i],"importotter")){ wg_int err; shmptr=wg_attach_database(shmname, shmsize); if(!shmptr) { fprintf(stderr, "Failed to attach to database.\n"); exit(1); } err = wg_import_otter_file(shmptr,argv[i+1]); if(!err) printf("Data imported from otter file.\n"); else if(err<-1) fprintf(stderr, "Fatal error when importing otter file, data may be partially"\ " imported\n"); else fprintf(stderr, "Import failed.\n"); break; } else if(argc>i && !strcmp(argv[i],"runreasoner")){ wg_int err; shmptr=wg_attach_database(shmname, shmsize); if(!shmptr) { fprintf(stderr, "Failed to attach to database.\n"); exit(1); } //printf("about to call wg_run_reasoner\n"); err = wg_run_reasoner(shmptr,argc,argv); //if(!err); //printf("wg_run_reasoner finished ok.\n"); //else //fprintf(stderr, "wg_run_reasoner finished with an error %d.\n",err); //break; break; } else if(argc>i && !strcmp(argv[i],"testreasoner")){ wg_int err; //printf("about to call wg_test_reasoner\n"); err = wg_test_reasoner(argc,argv); //if(!err); //printf("wg_test_reasoner finished ok.\n"); //else //fprintf(stderr, "wg_test_reasoner finished with an error %d.\n",err); //break; break; } #endif #ifdef HAVE_RAPTOR else if(argc>(i+2) && !strcmp(argv[i],"exportrdf")){ wg_int err; int pref_fields = atol(argv[i+1]); shmptr=wg_attach_database(shmname, shmsize); if(!shmptr) { fprintf(stderr, "Failed to attach to database.\n"); exit(1); } printf("Exporting with %d prefix fields.\n", pref_fields); RLOCK(shmptr, wlock); err = wg_export_raptor_rdfxml_file(shmptr, pref_fields, argv[i+2]); RULOCK(shmptr, wlock); if(err) fprintf(stderr, "Export failed.\n"); break; } else if(argc>(i+3) && !strcmp(argv[i],"importrdf")){ wg_int err; int pref_fields = atol(argv[i+1]); int suff_fields = atol(argv[i+2]); shmptr=wg_attach_database(shmname, shmsize); if(!shmptr) { fprintf(stderr, "Failed to attach to database.\n"); exit(1); } printf("Importing with %d prefix fields, %d suffix fields.\n,", pref_fields, suff_fields); WLOCK(shmptr, wlock); err = wg_import_raptor_file(shmptr, pref_fields, suff_fields, wg_rdfparse_default_callback, argv[i+3]); WULOCK(shmptr, wlock); if(!err) printf("Data imported from file.\n"); else if(err<-1) fprintf(stderr, "Fatal error when importing, data may be partially"\ " imported\n"); else fprintf(stderr, "Import failed.\n"); break; } #endif else if(!strcmp(argv[i],"test")) { /* This test function does it's own memory allocation. */ wg_run_tests(WG_TEST_QUICK, 2); break; } else if(!strcmp(argv[i],"fulltest")) { wg_run_tests(WG_TEST_FULL, 2); break; } else if(!strcmp(argv[i], "header")) { shmptr=wg_attach_database(shmname, shmsize); if(!shmptr) { fprintf(stderr, "Failed to attach to database.\n"); exit(1); } RLOCK(shmptr, wlock); wg_show_db_memsegment_header(shmptr); RULOCK(shmptr, wlock); break; } #ifdef _WIN32 else if(!strcmp(argv[i],"server")) { int flags = 0; if(argc>(i+1) && argv[i+1][0] == '-') { flags = parse_flag(argv[++i]); } if(argc>(i+1)) { shmsize = parse_shmsize(argv[i+1]); if(!shmsize) fprintf(stderr, "Failed to parse memory size, using default.\n"); } shmptr=wg_attach_memsegment(shmname, shmsize, shmsize, 1, (flags & FLAGS_LOGGING)); if(!shmptr) { fprintf(stderr, "Failed to attach to database.\n"); exit(1); } printf("Press Ctrl-C to end and release the memory.\n"); while(_getch() != 3); break; } #else else if(!strcmp(argv[i],"create")) { int flags = 0; if(argc>(i+1) && argv[i+1][0] == '-') { flags = parse_flag(argv[++i]); } if(argc>(i+1)) { shmsize = parse_shmsize(argv[i+1]); if(!shmsize) fprintf(stderr, "Failed to parse memory size, using default.\n"); } shmptr=wg_attach_memsegment(shmname, shmsize, shmsize, 1, (flags & FLAGS_LOGGING)); if(!shmptr) { fprintf(stderr, "Failed to attach to database.\n"); exit(1); } break; } #endif else if(argc>(i+1) && !strcmp(argv[i], "fill")) { int rows = atol(argv[i+1]); if(!rows) { fprintf(stderr, "Invalid number of rows.\n"); exit(1); } shmptr=wg_attach_database(shmname, shmsize); if(!shmptr) { fprintf(stderr, "Failed to attach to database.\n"); exit(1); } WLOCK(shmptr, wlock); if(argc > (i+2) && !strcmp(argv[i+2], "mix")) wg_genintdata_mix(shmptr, rows, TESTREC_SIZE); else if(argc > (i+2) && !strcmp(argv[i+2], "desc")) wg_genintdata_desc(shmptr, rows, TESTREC_SIZE); else wg_genintdata_asc(shmptr, rows, TESTREC_SIZE); WULOCK(shmptr, wlock); printf("Data inserted\n"); break; } else if(argc>(i+1) && !strcmp(argv[i],"select")) { int rows = atol(argv[i+1]); int from = 0; if(!rows) { fprintf(stderr, "Invalid number of rows.\n"); exit(1); } if(argc > (i+2)) from = atol(argv[i+2]); shmptr=wg_attach_database(shmname, shmsize); if(!shmptr) { fprintf(stderr, "Failed to attach to database.\n"); exit(1); } RLOCK(shmptr, wlock); selectdata(shmptr, rows, from); RULOCK(shmptr, wlock); break; } else if(argc>(i+1) && !strcmp(argv[i],"add")) { int err; shmptr=wg_attach_database(shmname, shmsize); if(!shmptr) { fprintf(stderr, "Failed to attach to database.\n"); exit(1); } WLOCK(shmptr, wlock); err = add_row(shmptr, argv+i+1, argc-i-1); WULOCK(shmptr, wlock); if(!err) printf("Row added.\n"); break; } else if(argc>(i+2) && !strcmp(argv[i],"del")) { shmptr=wg_attach_database(shmname, shmsize); if(!shmptr) { fprintf(stderr, "Failed to attach to database.\n"); exit(1); } /* Delete works like query(), except deletes the matching rows */ del(shmptr, argv+i+1, argc-i-1); break; break; } else if(argc>(i+3) && !strcmp(argv[i],"query")) { shmptr=wg_attach_database(shmname, shmsize); if(!shmptr) { fprintf(stderr, "Failed to attach to database.\n"); exit(1); } /* Query handles it's own locking */ query(shmptr, argv+i+1, argc-i-1); break; } else if(argc>i && !strcmp(argv[i],"addjson")){ wg_int err; shmptr=wg_attach_database(shmname, shmsize); if(!shmptr) { fprintf(stderr, "Failed to attach to database.\n"); exit(1); } WLOCK(shmptr, wlock); /* the filename parameter is optional */ err = wg_parse_json_file(shmptr, (argc>(i+1) ? argv[i+1] : NULL)); WULOCK(shmptr, wlock); if(!err) printf("JSON document imported.\n"); else if(err<-1) fprintf(stderr, "Fatal error when importing, data may be partially"\ " imported\n"); else fprintf(stderr, "Import failed.\n"); break; } else if(argc>(i+1) && !strcmp(argv[i],"findjson")) { shmptr=wg_attach_database(shmname, shmsize); if(!shmptr) { fprintf(stderr, "Failed to attach to database.\n"); exit(1); } WLOCK(shmptr, wlock); findjson(shmptr, argv[i+1]); WULOCK(shmptr, wlock); break; } shmname = argv[1]; /* no match, assume shmname was given */ } if(i==scan_to) { /* loop completed normally ==> no commands found */ usage(argv[0]); } if(shmptr) { RULOCK(shmptr, rlock); WULOCK(shmptr, wlock); wg_detach_database(shmptr); } exit(0); }