view status.cpp @ 408:f0a69693eaef api-inversion

The lesser of two evils, part 1. Most of the body of audiodb_insert_datum() will apply to "LARGE_ADB"-type insertions: checking for the right flags, checking for enough space free, synchronizing the header. Wouldn't it be nice if we could reuse all that code (or at least the bits that apply) without one horrible almost-identical cut-and-paste job (see batchinsert_large_adb(), or if that's not compelling enough, the four almost-identical query loops from before the Great Refactoring). Well, yes, it would. Sadly C makes it mildly difficult, because its functions are explicitly typed (so we can't pass arbitrary arguments of other types, even if they're ABI-compatible), while its macros are textual (which makes writing and maintaining them horrible). The thought of a union argument was briefly entertained and then discarded as being just Too Weird. So, instead, (ab)use the oldest trick in the book: void *. Define an adb_datum_internal_t which has void * instead of double *; the intention is that this internal data type can be constructed both from an adb_datum_t and some notional adb_reference_t (which looks very much like an adb_insert_t at the time of writing, with char * structure entries representing filenames). This adb_datum_internal_t structure is very much an internals-only thing, so put its definition in the internals header. Call what was previously audiodb_insert_datum() a new function audiodb_insert_datum_internal(), made static so that really no-one is tempted to call it other than ourselves. audiodb_insert_datum() is then trivial in terms of this new function, if stupidly tedious. (If we were playing dangerously, we could just perform a cast, but relying on the fact that sizeof(double *) = sizeof(void *) would almost certainly end up biting when we least expect. Incidental inclusion in this patch, since I noticed it at the time: actually check for the O2_FLAG_L2NORM before scribbling all over the l2norm table. Somewhat unsurprisingly, there are as yet no tests to defend against this (harmless, as it turns out) erroneous behaviour.
author mas01cr
date Tue, 09 Dec 2008 20:53:39 +0000
parents 58b88ab69424
children 62a0515f59be
line wrap: on
line source
#include "audioDB.h"
extern "C" {
#include "audioDB_API.h"
#include "audioDB-internals.h"
}

int audiodb_status(adb_t *adb, adb_status_t *status) {
  /* FIXME: it would be nice to be able to test for "is this database
     pointer valid", but at the moment we punt that to memory
     discipline.  */

  unsigned dudCount = 0;
  unsigned nullCount = 0;

  size_t trackTableLength = ALIGN_PAGE_UP(adb->header->numFiles * O2_TRACKTABLE_ENTRY_SIZE);
  unsigned *trackTable = 0;
  void *tmp = 0;
  if (adb->header->length > 0)  {
    tmp = mmap(0, trackTableLength, PROT_READ, MAP_SHARED, adb->fd, adb->header->trackTableOffset);
    if (tmp == (void *) -1) {
      return 1;
    }
    trackTable = (unsigned *) tmp;
  }

  for(unsigned k = 0; k < adb->header->numFiles; k++) {
    /* FIXME: this bare "16" here reveals a problem (or maybe two).
     * 16 here means the default value of the sequenceLength parameter
     * initializer (both in C++ and corresponding to the "-l" or
     * "--sequencelength" command-line argument).
     *
     * The problem is that the API as currently designed provides no
     * way to pass that information in to this routine; there's no
     * input parameter; nor is there in the SOAP version of this
     * query.  However, there /is/ a way to pass that information on
     * the command-line -- though that codepath is completely
     * untested.  I can see that it might be useful to provide this
     * information, but at present it's probably completely unused, so
     * the compromise for now is to hardwire the 16.
     */ 
    if(trackTable[k] < 16) {
      dudCount++; 
      if(!trackTable[k]) {
	nullCount++; 
      } 
    }
  }

  if(adb->header->length > 0) {
    if(munmap(trackTable, trackTableLength)) {
      return 1;
    }
  }

  status->numFiles = adb->header->numFiles;
  status->dim = adb->header->dim;
  status->length = adb->header->length;
  status->dudCount = dudCount;
  status->nullCount = nullCount;
  status->flags = adb->header->flags;
  status->data_region_size = adb->header->timesTableOffset - adb->header->dataOffset;

  return 0;
}