Exemplo n.º 1
0
int main(int argc, char **argv) {

  char* shmname = NULL;
  void* shmptr;
  int rcnt = -1, wcnt = -1;
#ifndef _WIN32
  struct timeval tv;
#endif
  unsigned long long start_ms, end_ms;

  if(argc==4) {
    shmname = argv[1];
    rcnt = atol(argv[2]);
    wcnt = atol(argv[3]);
  }

  if(rcnt<0 || wcnt<0) {
    fprintf(stderr, "usage: %s <shmname> <readers> <writers>\n", argv[0]);
    exit(1);
  }

  shmptr=wg_attach_database(shmname,DBSIZE);
  if (shmptr==NULL)
    exit(2);

  if(prepare_data(shmptr)) {
    wg_delete_database(shmname);
    exit(3);
  }

#ifdef _WIN32
  start_ms = (unsigned long long) GetTickCount();
#else
  gettimeofday(&tv, NULL);
  start_ms = tv.tv_sec * 1000 + tv.tv_usec / 1000;
#endif

  run_workers(shmptr, rcnt, wcnt);

#ifdef _WIN32
  end_ms = (unsigned long long) GetTickCount();
#else
  gettimeofday(&tv, NULL);
  end_ms = tv.tv_sec * 1000 + tv.tv_usec / 1000;
#endif

  check_data(shmptr, wcnt);

  fprintf(stdout, "elapsed: %d ms\n", (int) (end_ms - start_ms));

  wg_delete_database(shmname);

  exit(0);
}
Exemplo n.º 2
0
int main(int argc, char **argv) {
 
  void* shmptr;
  
  /* Create a database with custom key and 2M size */
  shmptr=wg_attach_database("9273", 2000000);

  /* Using default key and size:
  shmptr=wg_attach_database(NULL, 0);
  */

  if(!shmptr) {
    fprintf(stderr, "Failed to attach to database.\n");
    exit(1);
  }

  /* We have successfully attached, run the demo code */
  run_demo(shmptr);

  /* Clean up. The shared memory area is released. This is
   * useful for the purposes of this demo, but might best be
   * avoided for more persistent databases. */
  wg_delete_database("9273");
  /* Database with default key:
  wg_delete_database(NULL);
  */
  exit(0);
}
Exemplo n.º 3
0
int main(int argc, char **argv) {
  void *db;
  char *name="1";
  int i;
  for(i=0;i<1000;i++) { // 100000
    db = wg_attach_database(name,1000000000);  // 10000000
    if (!db) { printf("failed at try %d\n", i);  exit(0); }
    wg_detach_database(db);
    wg_delete_database(name);
  }  
  printf("i %d\n", i);
  return 0;
}
Exemplo n.º 4
0
int main(int argc, char **argv) {
  void *db, *rec;
  char *name="2";
  int i;
  db = wg_attach_database(name, 1000000000);
  if (!db) { printf("db creation failed \n"); exit(0); }
  for(i=0;i<10000000;i++) {   
    rec = wg_create_record(db, 9);
    if (!rec) { printf("record creation failed \n"); exit(0); }   
  }  
  printf("i %d\n", i);
  wg_detach_database(db);
  wg_delete_database(name);
  return 0;
}
Exemplo n.º 5
0
int main(int argc, char **argv) {
  void *db, *rec;
  char *name="51";
  int i,j,count=0;
  db = wg_attach_database(name, 1000000000);
  if (!db) { printf("db creation failed \n"); exit(0); }
  for(i=0;i<10000000;i++) {
    rec = wg_create_raw_record(db, 5);
    if (!rec) { printf("record creation failed \n"); exit(0); }      
    for (j=0;j<5;j++) {
      wg_set_new_field(db,rec,j,wg_encode_int(db,i+j));
      //count+=wg_decode_int(db,wg_get_field(db, rec, j));
    }
  }  
  printf("i %d count %d\n", i,count);
  wg_detach_database(db);
  wg_delete_database(name);
  return 0;
}
Exemplo n.º 6
0
int main(int argc, char **argv){
	const char* test_str = "TEST STRING HEAP";
	// create new db pointer used for allocate db
  void* db;

  // in order to use db, we have to attach the db with shared memroy allocated by OS.
  db = wg_attach_database("1000", 2000000);

  char* tp = NULL;
  int i = 0;

  // test: allocate 200000 times and try to copy something into allocaed space
  // inorder to test basic feature and memroy leak
  for( i = 0; i < 2000000; ++i) { 
    // allocate 
    // this statement shows basic way to use dbmalloc: it is almost the same as system one
    // except for the first paramater: db pointer 
    //                     (we have to let malloc know which heap it is using now.)
    // second paramater is size of space, in byte.
  	tp = (char*)dbmalloc(db,1024*sizeof(char));

    strcpy(tp,test_str);
  	//free
    //the free function is the same as system, execept for first paramater pointing to db that is being used
    dbfree(db,tp);
  
  }


  //delete data base that we used in this program
	wg_delete_database("1000");




  return 0;
}
Exemplo n.º 7
0
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);
}
Exemplo n.º 8
0
static char* drop(thread_data_p tdata, char* inparams[], char* invalues[], int incount) {
  char* database=tdata->database;
  char *token=NULL;
  int i,tmp;
  void* db=NULL; // actual database pointer
  char *res;
  int lock_id=0;
  int found=0;
  char errbuf[ERRBUF_LEN];  
  
  // find and check parameters
  for(i=0;i<incount;i++) {
    if (strncmp(inparams[i],"db",MAXQUERYLEN)==0) {
      database=invalues[i];
      if(database==NULL || strlen(database)<1 || atoi(database)<=0) {
        return errhalt(DB_NAME_ERR,tdata);
      }
    } else {  
      // handle generic parameters for all queries: at end of param check
      res=handle_generic_param(tdata,inparams[i],invalues[i],&token,errbuf);      
      if (res!=NULL) return res;  // return error string
    }    
  }  
  // authorization
  if (!authorize(ADMIN_LEVEL,tdata,database,token)) {
    return errhalt(NOT_AUTHORIZED_ERR,tdata);
  }  
  // all parameters and values were understood
  // create output string buffer (may be reallocated later)
  tdata->buf=str_new(INITIAL_MALLOC);
  if (tdata->buf==NULL) return errhalt(MALLOC_ERR,tdata);
  tdata->bufsize=INITIAL_MALLOC;
  tdata->bufptr=tdata->buf;
  op_print_data_start(tdata,0);
  // check if access allowed in the conf file
  if (database!=NULL && (tdata->global)->conf->dbases.used>0) {
    for(i=0;i<(tdata->global)->conf->dbases.used;i++) {
      if (!strcmp(database,(tdata->global)->conf->dbases.vals[i])) {
        found=1;
        break;
      }
    }
    if (!found) return errhalt(DB_AUTHORIZE_ERR,tdata);
  }
  // first try to attach to an existing database 
  db=op_attach_database(tdata,database,ADMIN_LEVEL);
  if (db==NULL) {
    return errhalt(DB_NOT_EXISTS_ERR,tdata);
  } else {  
    // database exists, take lock
    if (tdata->realthread && tdata->common->shutdown) return NULL; // for multithreading only
    tdata->db=db;
    lock_id = wg_start_write(db); // get write lock
    tdata->lock_id=lock_id;
    tdata->lock_type=WRITE_LOCK_TYPE;
    if (!lock_id) return err_clear_detach_halt(LOCK_ERR,tdata); 
    tmp=wg_detach_database(db); // detaches a database: returns 0 if OK
    if (tmp) return err_clear_detach_halt(DB_DROP_ERR,tdata);
    tmp=wg_delete_database(database);    
    if (tmp) return errhalt(DB_DROP_ERR,tdata);
  }  
  // deleted successfully 
  tdata->db=NULL;
  tdata->lock_id=0;
  if(!str_guarantee_space(tdata,MIN_STRLEN)) 
      return err_clear_detach_halt(MALLOC_ERR,tdata);
  strcpy(tdata->bufptr,"1");
  tdata->bufptr+=strlen("1");
  // end activity
  if(!op_print_data_end(tdata,0))
    return errhalt(MALLOC_ERR,tdata);    
  return tdata->buf;
}