예제 #1
0
파일: dbaccess.c 프로젝트: skm42/opendias
extern char *getTagId(char *tagname) {

  struct simpleLinkedList *vars, *rSet;
  char *sql2, *ret = NULL;
  char *sql = o_printf("SELECT tagid FROM tags WHERE tagname = '%s'", tagname);

  rSet = runquery_db(sql);
  if( rSet != NULL ) {
    ret = o_strdup(readData_db(rSet, "tagid"));
  }
  else {
    o_log(DEBUGM, "no tag was found. Adding a new one.");
    sql2 = o_strdup("INSERT INTO tags (tagname) VALUES (?)");

    vars = sll_init();
    sll_append(vars, DB_TEXT );
    sll_append(vars, tagname );

    runUpdate_db(sql2, vars);
    free(sql2);

    ret = itoa(last_insert(), 10);
  }
  free_recordset( rSet );
  free(sql);

  o_log(DEBUGM, "Using tagid of %s", ret);
  return ret;
}
예제 #2
0
파일: dbaccess.c 프로젝트: skm42/opendias
extern int countDocsWithTag( char *tagid ) {

  char *sql = o_printf("SELECT COUNT(tagid) ct FROM doc_tags WHERE tagid = '%s'", tagid);
  int ret = 0;

  struct simpleLinkedList *rSet = runquery_db(sql);
  if( rSet ) {
    ret = atoi(readData_db(rSet, "ct"));
  }
  free_recordset( rSet );
  free(sql);
  o_log(DEBUGM, "Tag is being used on %i docs", ret);

  return ret;
}
예제 #3
0
파일: dbaccess.c 프로젝트: skm42/opendias
extern char *getScanParam(char *scanid, int param_option) {

  char *sql, *vvalue = NULL;
  struct simpleLinkedList *rSet;

  sql = o_printf("SELECT param_value \
                  FROM scan_params \
                  WHERE client_id = '%s' \
                  AND param_option = %i", scanid, param_option);

  rSet = runquery_db(sql);
  if( rSet ) {
    vvalue = o_strdup(readData_db(rSet, "param_value"));
  }
  free_recordset( rSet );
  free(sql);

  return vvalue;
}
예제 #4
0
void *backpopulate_phash_inner( void *u) {

  pthread_t thread[MAX_THREADS];
  pthread_attr_t attr;
  int thread_pointer = 0;
  int avail_processors = 1;

  // We may have 16 processing cors, but only use what we need
  avail_processors = get_nprocs() - 1;
  if( avail_processors > MAX_THREADS ) {
    avail_processors = MAX_THREADS;
  }

  // initialise threading
  pthread_attr_init(&attr);
  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

  for( thread_pointer = 0; thread_pointer < MAX_THREADS; thread_pointer++ ) {
    pthread_create( &thread[ thread_pointer], &attr, stub, (void *)NULL );
  }
  thread_pointer = 0;

  // What tasks do we need to do
  struct simpleLinkedList *rSet;
  char *sql = o_strdup("SELECT filetype, docid FROM docs WHERE image_phash = 0");
  rSet = runquery_db(sql, NULL);
  if( rSet != NULL ) {
    do {

      // Queue up the next task
      int docid = atoi( readData_db(rSet, "docid") );
      char *docfilename;
      if( ( 0 == strcmp("2", readData_db(rSet, "filetype") ) ) 
      || ( 0 == strcmp("4", readData_db(rSet, "filetype") ) ) ) {
        docfilename = o_printf("%s/scans/%d_1.jpg", BASE_DIR, docid);
      }
      else {
        docfilename = o_printf("%s/scans/%d_thumb.jpg", BASE_DIR, docid);
      }

      // Wait for an available worker
      while( thread_active[thread_pointer] == 1 ) {
        thread_pointer++;
        if( thread_pointer > avail_processors ) {
          thread_pointer = 0;
        }
        usleep(200);
      }

      // Wait for it to join back first
      pthread_join( thread[ thread_pointer ], NULL);

      // Give instructions to the next worker
      struct process_phash *data = malloc( sizeof( struct process_phash ) );
      data->filename = docfilename;
      data->docid = docid;
      data->thread_id = thread_pointer;
      thread_active[thread_pointer] = 1;
      pthread_create( &thread[ thread_pointer], &attr, process_doc, (void *)data );

    } while ( nextRow( rSet ) );
    free_recordset( rSet );
  }
  free(sql);

  // Wait for everything t ofinish
  for( thread_pointer = 0; thread_pointer < MAX_THREADS; thread_pointer++ ) {
    pthread_join( thread[ thread_pointer ], NULL);
  }

  // Set the config flag, so we don't try this again.
  o_log(INFORMATION, "Marking that the backpopulation of pHash is: complete" );
  sql = o_strdup("UPDATE config SET config_value = 'complete' WHERE config_option = 'backpopulate_phash'");
  runUpdate_db(sql, NULL);
  free(sql);

  return NULL;
}
예제 #5
0
int setup (char *configFile) {

  struct simpleLinkedList *rSet;
  char *location, *conf, *sql;

  printf("entering setup\n");

  // Defaults
  VERBOSITY = DEBUGM;
  LOG_DIR = o_printf("%s/log/opendias", VAR_DIR);

  // Get 'DB' location
  if (configFile != NULL) {
    conf = o_strdup(configFile);
  }
  else {
    conf = o_printf("%s/opendias/opendias.conf", ETC_DIR);
    if( 0 != access(conf, F_OK) ) {
      o_log(INFORMATION, "Config not in GNU location: %s. Attempting system config dir /etc/opendias/opendias.conf", conf);
      free(conf);
      conf = o_strdup("/etc/opendias/opendias.conf");
    }
  }

  o_log(INFORMATION, "|Using config file: %s", conf);
  if( 0 == load_file_to_memory(conf, &location) ) {
    o_log(ERROR, "|Cannot find main config file: %s", conf);
    free(location);
    free(conf);
    return 1;
  }
  free(conf);

  chop(location);
  BASE_DIR = o_strdup(location);
  o_log(INFORMATION, "|Which says the database is at: %s", BASE_DIR);

  // Open (& maybe update) the database.
  if(connect_db (1)) { // 1 = create if required
    free(BASE_DIR);
    free(location);
    return 1;
  }
  free(location);

  o_log(INFORMATION, "|Current config is: ");
  sql = o_strdup("SELECT config_option, config_value FROM config");

  rSet = runquery_db(sql, NULL);
  if( rSet != NULL ) {
    do {
      char *config_option, *config_value;
      config_option = o_strdup(readData_db(rSet, "config_option"));
      config_value = o_strdup(readData_db(rSet, "config_value"));

      if ( config_option == NULL || config_value == NULL ) {
        printf("either option or value is NULL\n");
      } 
      else {
        //o_log(INFORMATION, "    %s = %s", config_option, config_value);
        //remark: the pipe in the message causes o_log i_o_log to crash
        //	caused by debug.c i_o_log by double use of vprintf
        o_log(INFORMATION, "|    %s = %s", config_option, config_value);
      }

      if( 0 == strcmp(config_option, "log_verbosity") ) {
        VERBOSITY = atoi(config_value);
      }

      free(config_option);
      free(config_value);
    } while ( nextRow( rSet ) );
  }
  free_recordset( rSet );
  free(sql);

  return 0;
}