Mercurial > hg > audiodb
changeset 406:c279adeb47f4 api-inversion
Slight rearrangement of insert code.
Move most of audiodb_insert() into a helper function to create an
adb_datum_t from an adb_insert_t. (Most of the rest of it goes into
another helper function for cleaning up).
Now audiodb_insert() is small enough that it's plausible to move the
O2_FLAG_LARGE_ADB into it from audioDB::insert. The plan is to add
support for the LARGE_ADB case in audiodb_insert(), at which point
audiodb_batchinsert() will Just Work, and we'll be able to delete
audioDB::batchinsert_large_adb (which is good, because it's an
almost-but-not-quite-identical copy of audioDB::batchinsert).
author | mas01cr |
---|---|
date | Fri, 05 Dec 2008 22:56:12 +0000 |
parents | ef4792df8f93 |
children | a82a2d9b2451 |
files | insert.cpp |
diffstat | 1 files changed, 67 insertions(+), 57 deletions(-) [+] |
line wrap: on
line diff
--- a/insert.cpp Fri Dec 05 22:32:49 2008 +0000 +++ b/insert.cpp Fri Dec 05 22:56:12 2008 +0000 @@ -168,31 +168,42 @@ return(dbH->numFiles < maxfiles); } -int audiodb_insert(adb_t *adb, adb_insert_t *insert) { - adb_datum_t datum; +static int audiodb_free_datum(adb_datum_t *datum) { + if(datum->data) { + free(datum->data); + } + if(datum->power) { + free(datum->power); + } + if(datum->times) { + free(datum->times); + } + return 0; +} + +static int audiodb_insert_create_datum(adb_insert_t *insert, adb_datum_t *datum) { int fd = 0; FILE *file = NULL; struct stat st; off_t size; - int err; - datum.data = NULL; - datum.power = NULL; - datum.times = NULL; + datum->data = NULL; + datum->power = NULL; + datum->times = NULL; if((fd = open(insert->features, O_RDONLY)) == -1) { goto error; } if(fstat(fd, &st)) { goto error; } - read(fd, &(datum.dim), sizeof(uint32_t)); + read(fd, &(datum->dim), sizeof(uint32_t)); size = st.st_size - sizeof(uint32_t); - datum.nvectors = size / (sizeof(double) * datum.dim); - datum.data = (double *) malloc(size); - if(!datum.data) { + datum->nvectors = size / (sizeof(double) * datum->dim); + datum->data = (double *) malloc(size); + if(!datum->data) { goto error; } - read(fd, datum.data, size); + read(fd, datum->data, size); close(fd); fd = 0; if(insert->power) { @@ -203,18 +214,18 @@ if(fstat(fd, &st)) { goto error; } - if((st.st_size - sizeof(uint32_t)) != (size / datum.dim)) { + if((st.st_size - sizeof(uint32_t)) != (size / datum->dim)) { goto error; } read(fd, &dim, sizeof(uint32_t)); if(dim != 1) { goto error; } - datum.power = (double *) malloc(size / datum.dim); - if(!datum.power) { + datum->power = (double *) malloc(size / datum->dim); + if(!datum->power) { goto error; } - read(fd, datum.power, size / datum.dim); + read(fd, datum->power, size / datum->dim); close(fd); } if(insert->times) { @@ -222,16 +233,16 @@ if(!(file = fopen(insert->times, "r"))) { goto error; } - datum.times = (double *) malloc(2 * size / datum.dim); - if(!datum.times) { + datum->times = (double *) malloc(2 * size / datum->dim); + if(!datum->times) { goto error; } if(fscanf(file, " %lf", &t) != 1) { goto error; } - tp = datum.times; + tp = datum->times; *tp++ = t; - for(unsigned int n = 0; n < datum.nvectors - 1; n++) { + for(unsigned int n = 0; n < datum->nvectors - 1; n++) { if(fscanf(file, " %lf", &t) != 1) { goto error; } @@ -244,21 +255,8 @@ *tp = t; fclose(file); } - datum.key = insert->key ? insert->key : insert->features; - err = audiodb_insert_datum(adb, &datum); - free(datum.data); - if(datum.power) { - free(datum.power); - } - if(datum.times) { - free(datum.times); - } - if(err == 2) { - return 0; - } - else { - return err; - } + datum->key = insert->key ? insert->key : insert->features; + return 0; error: if(fd > 0) { @@ -267,16 +265,30 @@ if(file) { fclose(file); } - if(datum.data) { - free(datum.data); + audiodb_free_datum(datum); + return 1; +} + +int audiodb_insert(adb_t *adb, adb_insert_t *insert) { + if(adb->header->flags & O2_FLAG_LARGE_ADB) { + return 1; + } else { + adb_datum_t datum; + int err; + + if(audiodb_insert_create_datum(insert, &datum)) { + return 1; + } + err = audiodb_insert_datum(adb, &datum); + audiodb_free_datum(&datum); + + if(err == 2) { + return 0; + } + else { + return err; + } } - if(datum.power) { - free(datum.power); - } - if(datum.times) { - free(datum.times); - } - return 1; } int audiodb_batchinsert(adb_t *adb, adb_insert_t *insert, unsigned int size) { @@ -295,20 +307,18 @@ error("failed to open database", dbName); } } - if(adb->header->flags & O2_FLAG_LARGE_ADB) { - - } else { - /* at this point, we have powerfd (an fd), timesFile (a - * std::ifstream *) and inFile (a char *). Wacky, huh? Ignore - * the wackiness and just use the names. */ - adb_insert_t insert; - insert.features = inFile; - insert.times = timesFileName; - insert.power = powerFileName; - insert.key = key; - if(audiodb_insert(adb, &insert)) { - error("insertion failure", inFile); - } + + /* at this point, we have powerfd (an fd), timesFile (a + * std::ifstream *) and inFile (a char *). Wacky, huh? Ignore + * the wackiness and just use the names. */ + adb_insert_t insert; + insert.features = inFile; + insert.times = timesFileName; + insert.power = powerFileName; + insert.key = key; + + if(audiodb_insert(adb, &insert)) { + error("insertion failure", inFile); } status(dbName); }