view open.cpp @ 404:1fb8bee777e5 api-inversion

Begin working towards inverting audioDB::insert() / audiodb_insert(). New data type audiodb_datum_t, roughly corresponding to a "track" in current audioDB parlance; it contains exactly the feature information and metadata to record. New function audiodb_insert_datum() to insert one of these audiodb_datum_t objects into the database; the intention is that not only can insertion of feature files be implemented in terms of this function, but that it will be a useful function in its own right, callable perhaps from PD, Max/MSP, and/or a VAMP plugin. This function is complicated enough that it actually gets a comment. Implement audioDB::insert() in terms of audiodb_insert_datum(), via a wrapper which handles the slightly wacky error/non-error case of attempting to insert features with a key that already exists in the database. Delete whole rafts of code. We can't quite delete everything because there's batchinsert / batchinsert_large_adb to sort out; the good news is that the batchinsert operation can simply be implemented as a loop around audiodb_insert_datum() without loss of efficiency. (There's also a stray extra audiodb_insert() in libtests/0027/, found through an earlier iteration of this patch.)
author mas01cr
date Fri, 05 Dec 2008 22:32:43 +0000
parents 58b88ab69424
children ad2206c24986
line wrap: on
line source
#include "audioDB.h"
extern "C" {
#include "audioDB_API.h"
#include "audioDB-internals.h"
}

static bool audiodb_check_header(adb_header_t *header) {
  /* FIXME: use syslog() or write to stderr or something to give the
     poor user some diagnostics. */
  if(header->magic == O2_OLD_MAGIC) {
    return false;
  }
  if(header->magic != O2_MAGIC) {
    return false;
  }
  if(header->version != O2_FORMAT_VERSION) {
    return false;
  }
  if(header->headerSize != O2_HEADERSIZE) {
    return false;
  }
  return true;
}

static int audiodb_collect_keys(adb_t *adb) {
  char *key_table = 0;
  size_t key_table_length = 0;

  if(adb->header->length > 0) {
    unsigned nfiles = adb->header->numFiles;
    key_table_length = ALIGN_PAGE_UP(nfiles * O2_FILETABLE_ENTRY_SIZE);
    mmap_or_goto_error(char *, key_table, adb->header->fileTableOffset, key_table_length);
    for (unsigned i = 0; i < nfiles; i++) {
      adb->keys->insert(key_table + i*O2_FILETABLE_ENTRY_SIZE);
    }
    munmap(key_table, key_table_length);
  }

  return 0;

 error:
  maybe_munmap(key_table, key_table_length);
  return 1;
}

adb_t *audiodb_open(const char *path, int flags) {
  adb_t *adb = 0;
  int fd = -1;

  flags &= (O_RDONLY|O_RDWR);
  fd = open(path, flags);
  if(fd == -1) {
    goto error;
  }
  if(acquire_lock(fd, flags == O_RDWR)) {
    goto error;
  }

  adb = (adb_t *) malloc(sizeof(adb_t));
  if(!adb) {
    goto error;
  }
  adb->fd = fd;
  adb->flags = flags;
  adb->path = (char *) malloc(1+strlen(path));
  if(!(adb->path)) {
    goto error;
  }
  strcpy(adb->path, path);

  adb->header = (adb_header_t *) malloc(sizeof(adb_header_t));
  if(!(adb->header)) {
    goto error;
  }
  if(read(fd, (char *) adb->header, O2_HEADERSIZE) != O2_HEADERSIZE) {
    goto error;
  }
  if(!audiodb_check_header(adb->header)) {
    goto error;
  }

  adb->keys = new std::set<std::string>;
  if(!adb->keys) {
    goto error;
  }
  if(audiodb_collect_keys(adb)) {
    goto error;
  }
  return adb;

 error:
  if(adb) {
    if(adb->header) {
      free(adb->header);
    }
    if(adb->path) {
      free(adb->path);
    }
    if(adb->keys) {
      delete adb->keys;
    }
    free(adb);
  }
  if(fd != -1) {
    close(fd);
  }
  return NULL;
}