annotate 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
rev   line source
mas01cr@395 1 #include "audioDB.h"
mas01cr@395 2 extern "C" {
mas01cr@395 3 #include "audioDB_API.h"
mas01cr@395 4 }
mas01cr@395 5
mas01cr@395 6 int audiodb_status(adb_t *adb, adb_status_t *status) {
mas01cr@395 7 /* FIXME: it would be nice to be able to test for "is this database
mas01cr@395 8 pointer valid", but at the moment we punt that to memory
mas01cr@395 9 discipline. */
mas01cr@395 10
mas01cr@395 11 unsigned dudCount = 0;
mas01cr@395 12 unsigned nullCount = 0;
mas01cr@395 13
mas01cr@395 14 off_t trackTableLength = ALIGN_PAGE_UP(adb->header->numFiles * O2_TRACKTABLE_ENTRY_SIZE);
mas01cr@395 15 unsigned *trackTable = 0;
mas01cr@395 16 void *tmp = 0;
mas01cr@395 17 if (adb->header->length > 0) {
mas01cr@395 18 tmp = mmap(0, trackTableLength, PROT_READ, MAP_SHARED, adb->fd, adb->header->trackTableOffset);
mas01cr@395 19 if (tmp == (void *) -1) {
mas01cr@395 20 return 1;
mas01cr@395 21 }
mas01cr@395 22 trackTable = (unsigned *) tmp;
mas01cr@395 23 }
mas01cr@395 24
mas01cr@395 25 for(unsigned k = 0; k < adb->header->numFiles; k++) {
mas01cr@395 26 /* FIXME: this bare "16" here reveals a problem (or maybe two).
mas01cr@395 27 * 16 here means the default value of the sequenceLength parameter
mas01cr@395 28 * initializer (both in C++ and corresponding to the "-l" or
mas01cr@395 29 * "--sequencelength" command-line argument).
mas01cr@395 30 *
mas01cr@395 31 * The problem is that the API as currently designed provides no
mas01cr@395 32 * way to pass that information in to this routine; there's no
mas01cr@395 33 * input parameter; nor is there in the SOAP version of this
mas01cr@395 34 * query. However, there /is/ a way to pass that information on
mas01cr@395 35 * the command-line -- though that codepath is completely
mas01cr@395 36 * untested. I can see that it might be useful to provide this
mas01cr@395 37 * information, but at present it's probably completely unused, so
mas01cr@395 38 * the compromise for now is to hardwire the 16.
mas01cr@395 39 */
mas01cr@395 40 if(trackTable[k] < 16) {
mas01cr@395 41 dudCount++;
mas01cr@395 42 if(!trackTable[k]) {
mas01cr@395 43 nullCount++;
mas01cr@395 44 }
mas01cr@395 45 }
mas01cr@395 46 }
mas01cr@395 47
mas01cr@395 48 if(adb->header->length > 0) {
mas01cr@395 49 if(munmap(trackTable, trackTableLength)) {
mas01cr@395 50 return 1;
mas01cr@395 51 }
mas01cr@395 52 }
mas01cr@395 53
mas01cr@395 54 status->numFiles = adb->header->numFiles;
mas01cr@395 55 status->dim = adb->header->dim;
mas01cr@395 56 status->length = adb->header->length;
mas01cr@395 57 status->dudCount = dudCount;
mas01cr@395 58 status->nullCount = nullCount;
mas01cr@395 59 status->flags = adb->header->flags;
mas01cr@395 60 status->data_region_size = adb->header->timesTableOffset - adb->header->dataOffset;
mas01cr@395 61
mas01cr@395 62 return 0;
mas01cr@395 63 }
mas01cr@395 64