view status.cpp @ 395:bc7a821004bb api-inversion

Invert audioDB::status / audiodb_status(). To do that without breaking abstractions, we actually need a new field in the status structure, storing the size of the data region. Previously, this was computed in the audioDB::status request from the database header, but I'm assuming that "user" code doesn't have access to such internals. While we're at it, name some intermediate values in audioDB::status() so that I don't get confused. Here's the thing, though: we need to make sure that the adb_t * that we have from audiodb_open() or audiodb_create() is propagated all the way through into the C++ routines that implement library functions -- in particular those which actually write to the database; otherwise we won't have a consistent view in memory of the header on-disk (as the adb header that will have been written to disk won't be the same as the one in memory). We can do that, by altering the "API" audioDB constructors to take the adb_t * argument, and setting the adb field in the audioDB object that we've already introduced to that. But now we need to be careful a couple of times: if we have one, then audioDB::initTables() mustn't stomp on it; also, if we're only constructing an audioDB instance to fulfil an API request, we mustn't audiodb_close() the one we have when we destroy the audioDB object, because the adb_t * is the one we have passed in and are going to reuse in later calls to the API. The good news is that we can be careful in just these ways with minimal code. The really good news is that once the inversion is complete, all of this horribleness will automatically go away (as there will be no code which constructs audioDB objects to fulfil API functions). Hooray! It's almost like it was all planned this way.
author mas01cr
date Tue, 25 Nov 2008 16:41:01 +0000
parents
children a65b31660804
line wrap: on
line source
#include "audioDB.h"
extern "C" {
#include "audioDB_API.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;

  off_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;
}