IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 10110 for trunk/ippdb/src


Ignore:
Timestamp:
Nov 20, 2006, 1:58:03 PM (20 years ago)
Author:
jhoblitt
Message:

VERSION 0.0.57

Location:
trunk/ippdb/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/ippdb/src/ippdb.c

    r10060 r10110  
    3131#include "ippdb.h"
    3232
     33#define EXPTAGCOUNTER_TABLE_NAME "expTagCounter"
    3334#define SUMMITEXP_TABLE_NAME "summitExp"
    3435#define SUMMITIMFILE_TABLE_NAME "summitImfile"
     
    235236}
    236237
     238static void expTagCounterRowFree(expTagCounterRow *object);
     239
     240expTagCounterRow *expTagCounterRowAlloc(psU64 counter)
     241{
     242    expTagCounterRow *_object;
     243
     244    _object = psAlloc(sizeof(expTagCounterRow));
     245    psMemSetDeallocator(_object, (psFreeFunc)expTagCounterRowFree);
     246
     247    _object->counter = counter;
     248
     249    return _object;
     250}
     251
     252static void expTagCounterRowFree(expTagCounterRow *object)
     253{
     254}
     255
     256bool expTagCounterCreateTable(psDB *dbh)
     257{
     258    psMetadata *md = psMetadataAlloc();
     259    if (!psMetadataAdd(md, PS_LIST_TAIL, "counter", PS_DATA_U64, NULL, 0)) {
     260        psError(PS_ERR_UNKNOWN, false, "failed to add item counter");
     261        psFree(md);
     262        return false;
     263    }
     264
     265    bool status = psDBCreateTable(dbh, EXPTAGCOUNTER_TABLE_NAME, md);
     266
     267    psFree(md);
     268
     269    return status;
     270}
     271
     272bool expTagCounterDropTable(psDB *dbh)
     273{
     274    return psDBDropTable(dbh, EXPTAGCOUNTER_TABLE_NAME);
     275}
     276
     277bool expTagCounterInsert(psDB * dbh, psU64 counter)
     278{
     279    psMetadata *md = psMetadataAlloc();
     280    if (!psMetadataAdd(md, PS_LIST_TAIL, "counter", PS_DATA_U64, NULL, counter)) {
     281        psError(PS_ERR_UNKNOWN, false, "failed to add item counter");
     282        psFree(md);
     283        return false;
     284    }
     285
     286    bool status = psDBInsertOneRow(dbh, EXPTAGCOUNTER_TABLE_NAME, md);
     287    psFree(md);
     288
     289    return status;
     290}
     291
     292long long expTagCounterDelete(psDB *dbh, const psMetadata *where, unsigned long long limit)
     293{
     294    long long       deleted = 0;
     295
     296    long long count = psDBDeleteRows(dbh, EXPTAGCOUNTER_TABLE_NAME, where, limit);
     297    if (count < 0) {
     298        psError(PS_ERR_UNKNOWN, true, "failed to delete row from expTagCounter");
     299        return count;
     300
     301        deleted += count;
     302    }
     303
     304    return deleted;
     305}
     306bool expTagCounterInsertObject(psDB *dbh, expTagCounterRow *object)
     307{
     308    return expTagCounterInsert(dbh, object->counter);
     309}
     310
     311bool expTagCounterInsertObjects(psDB *dbh, psArray *objects)
     312{
     313    for (long i = 0; i < psArrayLength(objects); i++) {
     314        if (!expTagCounterInsertObject(dbh, objects->data[i])) {
     315            return false;
     316        }
     317    }
     318
     319    return true;
     320}
     321
     322bool expTagCounterInsertFits(psDB *dbh, const psFits *fits)
     323{
     324    psArray         *rowSet;
     325
     326    // move to (the first?) extension named  EXPTAGCOUNTER_TABLE_NAME
     327    if (!psFitsMoveExtName(fits, EXPTAGCOUNTER_TABLE_NAME)) {
     328        psError(PS_ERR_UNKNOWN, true, "failed to find FITS extension %s", EXPTAGCOUNTER_TABLE_NAME);
     329        return false;
     330    }
     331
     332    // check HDU type
     333    if (psFitsGetExtType(fits) != PS_FITS_TYPE_BINARY_TABLE)  {
     334        psError(PS_ERR_UNKNOWN, true, "FITS HDU type is not PS_FITS_TYPE_BINARY_TABLE");
     335        return false;
     336    }
     337
     338    // read fits table
     339    rowSet = psFitsReadTable(fits);
     340    if (!rowSet) {
     341        psError(PS_ERR_UNKNOWN, true, "FITS read error or FITS table is empty");
     342        psFree(rowSet);
     343        return false;
     344    }
     345
     346    if (!psDBInsertRows(dbh, EXPTAGCOUNTER_TABLE_NAME, rowSet)) {
     347        psError(PS_ERR_UNKNOWN, false, "databse insert failed");
     348        psFree(rowSet);
     349        return false;
     350    }
     351
     352    psFree(rowSet);
     353
     354    return true;
     355}
     356
     357bool expTagCounterSelectRowsFits(psDB *dbh, psFits *fits, const psMetadata *where, unsigned long long limit)
     358{
     359    psArray         *rowSet;
     360
     361    rowSet = psDBSelectRows(dbh, EXPTAGCOUNTER_TABLE_NAME, where, limit);
     362    if (!rowSet) {
     363        return false;
     364    }
     365
     366    // output to fits
     367    if (!psFitsWriteTable(fits, NULL, rowSet, EXPTAGCOUNTER_TABLE_NAME)) {
     368        psError(PS_ERR_UNKNOWN, false, "FITS table write failed");
     369        psFree(rowSet);
     370        return false;
     371    }
     372
     373    psFree(rowSet);
     374
     375    return true;
     376}
     377
     378psMetadata *expTagCounterMetadataFromObject(const expTagCounterRow *object)
     379{
     380    psMetadata *md = psMetadataAlloc();
     381    if (!psMetadataAdd(md, PS_LIST_TAIL, "counter", PS_DATA_U64, NULL, object->counter)) {
     382        psError(PS_ERR_UNKNOWN, false, "failed to add item counter");
     383        psFree(md);
     384        return false;
     385    }
     386
     387
     388    return md;
     389}
     390
     391expTagCounterRow *expTagCounterObjectFromMetadata(psMetadata *md)
     392{
     393
     394bool status = false;
     395    psU64 counter = psMetadataLookupU64(&status, md, "counter");
     396    if (!status) {
     397        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item counter");
     398        return false;
     399    }
     400
     401    return expTagCounterRowAlloc(counter);
     402}
     403psArray *expTagCounterSelectRowObjects(psDB *dbh, const psMetadata *where, unsigned long long limit)
     404{
     405    psArray         *rowSet;
     406    psArray         *returnSet;
     407    psU64           i;
     408
     409    rowSet = psDBSelectRows(dbh, EXPTAGCOUNTER_TABLE_NAME, where, limit);
     410    if (!rowSet) {
     411        return NULL;
     412    }
     413
     414    // convert psMetadata rows to row objects
     415
     416    returnSet = psArrayAllocEmpty(rowSet->n);
     417
     418    for (i = 0; i < rowSet->n; i++) {
     419        expTagCounterRow *object = expTagCounterObjectFromMetadata(rowSet->data[i]);
     420        psArrayAdd(returnSet, 0, object);
     421        psFree(object);
     422    }
     423
     424    psFree(rowSet);
     425
     426    return returnSet;
     427}
     428bool expTagCounterDeleteObject(psDB *dbh, const expTagCounterRow *object)
     429{
     430    psMetadata *where = expTagCounterMetadataFromObject(object);
     431    long long count = psDBDeleteRows(dbh, EXPTAGCOUNTER_TABLE_NAME, where, 0);
     432    psFree(where)
     433    if (count < 0) {
     434        psError(PS_ERR_UNKNOWN, true, "failed to delete row from expTagCounter");
     435        return false;
     436    }
     437    if (count > 1) {
     438        // XXX should this be a psAbort() instead?  It is possible that
     439        // having an object match multiple rows was by design.
     440        psError(PS_ERR_UNKNOWN, true, "expTagCounterRow object matched more then one row.  Check your database schema");
     441        return false;
     442    }
     443
     444    return true;
     445}
     446long long expTagCounterDeleteRowObjects(psDB *dbh, const psArray *objects, unsigned long long limit)
     447{
     448    long long       deleted = 0;
     449
     450    for (long long i = 0; i < objects->n; i++) {
     451        expTagCounterRow *object = objects->data[i];
     452        psMetadata *where = expTagCounterMetadataFromObject(object);
     453        long long count = psDBDeleteRows(dbh, EXPTAGCOUNTER_TABLE_NAME, where, limit);
     454        psFree(where)
     455        if (count < 0) {
     456            psError(PS_ERR_UNKNOWN, true, "failed to delete row from expTagCounter");
     457            return count;
     458        }
     459
     460        deleted += count;
     461    }
     462
     463    return deleted;
     464}
     465bool expTagCounterPrintObjects(FILE *stream, psArray *objects, bool mdcf)
     466{
     467    PS_ASSERT_PTR_NON_NULL(objects, false);
     468
     469    psMetadata *output = psMetadataAlloc();
     470    for (long i = 0; i < psArrayLength(objects); i++) {
     471        psMetadata *md = expTagCounterMetadataFromObject(objects->data[i]);
     472        if (!psMetadataAddMetadata(
     473            output,
     474            PS_LIST_TAIL,
     475            EXPTAGCOUNTER_TABLE_NAME,
     476            PS_META_DUPLICATE_OK,
     477            NULL,
     478            md
     479        )) {
     480            psError(PS_ERR_UNKNOWN, false, "failed to add metadata");
     481            psFree(md);
     482            psFree(output);
     483            return false;
     484        }
     485        psFree(md);
     486    }
     487
     488    if (!ippdbPrintMetadataRaw(stream, output, mdcf)) {
     489        psError(PS_ERR_UNKNOWN, false, "failed to print metadata");
     490        psFree(output);
     491    }
     492    psFree(output);
     493
     494    return true;
     495}
    237496static void summitExpRowFree(summitExpRow *object);
    238497
     
    24082667static void newExpRowFree(newExpRow *object);
    24092668
    2410 newExpRow *newExpRowAlloc(psU64 exp_tag, const char *exp_id, const char *camera, const char *telescope, psTime* dateobs, const char *exp_type, psS32 imfiles)
     2669newExpRow *newExpRowAlloc(const char *exp_tag, const char *exp_id, const char *camera, const char *telescope, psTime* dateobs, const char *exp_type, psS32 imfiles)
    24112670{
    24122671    newExpRow       *_object;
     
    24152674    psMemSetDeallocator(_object, (psFreeFunc)newExpRowFree);
    24162675
    2417     _object->exp_tag = exp_tag;
     2676    _object->exp_tag = psStringCopy(exp_tag);
    24182677    _object->exp_id = psStringCopy(exp_id);
    24192678    _object->camera = psStringCopy(camera);
     
    24282687static void newExpRowFree(newExpRow *object)
    24292688{
     2689    psFree(object->exp_tag);
    24302690    psFree(object->exp_id);
    24312691    psFree(object->camera);
     
    24382698{
    24392699    psMetadata *md = psMetadataAlloc();
    2440     if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_tag", PS_DATA_U64, "Primary Key AUTO_INCREMENT", 0)) {
     2700    if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_tag", PS_DATA_STRING, "Primary Key", "64")) {
    24412701        psError(PS_ERR_UNKNOWN, false, "failed to add item exp_tag");
    24422702        psFree(md);
     
    24892749}
    24902750
    2491 bool newExpInsert(psDB * dbh, psU64 exp_tag, const char *exp_id, const char *camera, const char *telescope, psTime* dateobs, const char *exp_type, psS32 imfiles)
     2751bool newExpInsert(psDB * dbh, const char *exp_tag, const char *exp_id, const char *camera, const char *telescope, psTime* dateobs, const char *exp_type, psS32 imfiles)
    24922752{
    24932753    psMetadata *md = psMetadataAlloc();
    2494     if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_tag", PS_DATA_U64, NULL, exp_tag)) {
     2754    if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_tag", PS_DATA_STRING, NULL, exp_tag)) {
    24952755        psError(PS_ERR_UNKNOWN, false, "failed to add item exp_tag");
    24962756        psFree(md);
     
    26232883{
    26242884    psMetadata *md = psMetadataAlloc();
    2625     if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_tag", PS_DATA_U64, NULL, object->exp_tag)) {
     2885    if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_tag", PS_DATA_STRING, NULL, object->exp_tag)) {
    26262886        psError(PS_ERR_UNKNOWN, false, "failed to add item exp_tag");
    26272887        psFree(md);
     
    26672927
    26682928bool status = false;
    2669     psU64 exp_tag = psMetadataLookupU64(&status, md, "exp_tag");
     2929    char* exp_tag = psMetadataLookupPtr(&status, md, "exp_tag");
    26702930    if (!status) {
    26712931        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item exp_tag");
  • trunk/ippdb/src/ippdb.h

    r10060 r10110  
    125125);
    126126
     127/** expTagCounterRow data structure
     128 *
     129 * Structure for representing a single row of expTagCounter table data.
     130 */
     131
     132typedef struct {
     133    psU64           counter;
     134} expTagCounterRow;
     135
     136/** Creates a new expTagCounterRow object
     137 *
     138 *  @return A new expTagCounterRow object or NULL on failure.
     139 */
     140
     141expTagCounterRow *expTagCounterRowAlloc(
     142    psU64           counter
     143);
     144
     145/** Creates a new expTagCounter table
     146 *
     147 * @return true on success
     148 */
     149
     150bool expTagCounterCreateTable(
     151    psDB            *dbh                ///< Database handle
     152);
     153
     154/** Deletes a expTagCounter table
     155 *
     156 * @return true on success
     157 */
     158
     159bool expTagCounterDropTable(
     160    psDB            *dbh                ///< Database handle
     161);
     162
     163/** Insert a single row into a table
     164 *
     165 * This function constructs and inserts a single row based on it's parameters.
     166 *
     167 * @return true on success
     168 */
     169
     170bool expTagCounterInsert(
     171    psDB            *dbh,               ///< Database handle
     172    psU64           counter
     173);
     174
     175/** Deletes up to limit rows from the database and returns the number of rows actually deleted.
     176 *
     177 * @return A The number of rows removed or a negative value on error
     178 */
     179
     180long long expTagCounterDelete(
     181    psDB            *dbh,               ///< Database handle
     182    const psMetadata *where,            ///< Row match criteria
     183    unsigned long long limit            ///< Maximum number of elements to delete
     184);
     185
     186/** Insert a single expTagCounterRow object into a table
     187 *
     188 * This function constructs and inserts a single row based on it's parameters.
     189 *
     190 * @return true on success
     191 */
     192
     193bool expTagCounterInsertObject(
     194    psDB            *dbh,               ///< Database handle
     195    expTagCounterRow *object             ///< expTagCounterRow object
     196);
     197
     198/** Insert an array of expTagCounterRow object into a table
     199 *
     200 * This function constructs and inserts multiple rows based on it's parameters.
     201 *
     202 * @return true on success
     203 */
     204
     205bool expTagCounterInsertObjects(
     206    psDB            *dbh,               ///< Database handle
     207    psArray         *objects            ///< array of expTagCounterRow objects
     208);
     209
     210/** Insert data from a binary FITS table expTagCounterRow into the database
     211 *
     212 * This function expects a psFits object with a FITS table as the first
     213 * extension.  The table must have at least one row of data in it, that is of
     214 * the appropriate format (number of columns and their type).  All other
     215 * extensions are ignored.
     216 *
     217 * @return true on success
     218 */
     219
     220bool expTagCounterInsertFits(
     221    psDB            *dbh,               ///< Database handle
     222    const psFits    *fits               ///< psFits object
     223);
     224
     225/** Selects up to limit from the database and returns them in a binary FITS table
     226 *
     227 * This function assumes an empty psFits object and will create a FITS table
     228 * as the first extension.
     229 *
     230 *  See psDBSelectRows() for documentation on the format of where.
     231 *
     232 * @return true on success
     233 */
     234
     235bool expTagCounterSelectRowsFits(
     236    psDB            *dbh,               ///< Database handle
     237    psFits          *fits,              ///< psFits object
     238    const psMetadata *where,            ///< Row match criteria
     239    unsigned long long limit            ///< Maximum number of elements to return
     240);
     241
     242/** Convert a expTagCounterRow into an equivalent psMetadata
     243 *
     244 * @return A psMetadata pointer or NULL on error
     245 */
     246
     247psMetadata *expTagCounterMetadataFromObject(
     248    const expTagCounterRow *object             ///< fooRow to convert into a psMetadata
     249);
     250
     251/** Convert a psMetadata into an equivalent fooRow
     252 *
     253 * @return A expTagCounterRow pointer or NULL on error
     254 */
     255
     256expTagCounterRow *expTagCounterObjectFromMetadata(
     257    psMetadata      *md                 ///< psMetadata to convert into a fooRow
     258);
     259/** Selects up to limit rows from the database and returns as expTagCounterRow objects in a psArray
     260 *
     261 *  See psDBSelectRows() for documentation on the format of where.
     262 *
     263 * @return A psArray pointer or NULL on error
     264 */
     265
     266psArray *expTagCounterSelectRowObjects(
     267    psDB            *dbh,               ///< Database handle
     268    const psMetadata *where,            ///< Row match criteria
     269    unsigned long long limit            ///< Maximum number of elements to return
     270);
     271/** Deletes a row from the database coresponding to an expTagCounter
     272 *
     273 *  Note that a 'where' search psMetadata is constructed from each object and
     274 *  used to find rows to delete.
     275 *
     276 * @return A The number of rows removed or a negative value on error
     277 */
     278
     279bool expTagCounterDeleteObject(
     280    psDB            *dbh,               ///< Database handle
     281    const expTagCounterRow *object    ///< Object to delete
     282);
     283/** Deletes up to limit rows from the database and returns the number of rows actually deleted.
     284 *
     285 *  Note that a 'where' search psMetadata is constructed from each object and
     286 *  used to find rows to delete.
     287 *
     288 * @return A The number of rows removed or a negative value on error
     289 */
     290
     291long long expTagCounterDeleteRowObjects(
     292    psDB            *dbh,               ///< Database handle
     293    const psArray   *objects,           ///< Array of objects to delete
     294    unsigned long long limit            ///< Maximum number of elements to delete
     295);
     296/** Formats and prints an array of expTagCounterRow objects
     297 *
     298 * When mdcf is set the formated output is in psMetadataConfig
     299 * format, otherwise it is in a simple tabular format.
     300 *
     301 * @return true on success
     302 */
     303
     304bool expTagCounterPrintObjects(
     305    FILE            *stream,            ///< a stream
     306    psArray         *objects,           ///< An array of expTagCounterRow objects
     307    bool            mdcf                ///< format as mdconfig or simple
     308);
    127309/** summitExpRow data structure
    128310 *
     
    13071489
    13081490typedef struct {
    1309     psU64           exp_tag;
     1491    char            *exp_tag;
    13101492    char            *exp_id;
    13111493    char            *camera;
     
    13221504
    13231505newExpRow *newExpRowAlloc(
    1324     psU64           exp_tag,
     1506    const char      *exp_tag,
    13251507    const char      *exp_id,
    13261508    const char      *camera,
     
    13581540bool newExpInsert(
    13591541    psDB            *dbh,               ///< Database handle
    1360     psU64           exp_tag,
     1542    const char      *exp_tag,
    13611543    const char      *exp_id,
    13621544    const char      *camera,
Note: See TracChangeset for help on using the changeset viewer.