Ejemplo n.º 1
0
// mysdb :trim()
static int api_trim( lua_State *L) {
    int r = sdb_trim( lua_sdb_checktable( L, 1));
    if( r) return push_sdb_error( L, r);
    return 1; // push table back
}
Ejemplo n.º 2
0
/*
 * Initialize a table structure, return SDB_EOK or SDB_EMEM.
 * The result table must still have its columns configured with
 * calls to sdb_column() before it can accept data.
 *
 * This form is primarily useful when the column number,
 * names and serialization methods are not known at compile-time.
 * In simple cases, the sdb_init() interface is lighter and more readable.
 *
 * Initialize the table passed as 1st parameter as an unconfigured table.
 * next steps: configure its columns with sdb_column(), write data in it,
 * periodically serialize or consolidate it. */
static int sdb_initwithoutcolumns( sdb_table_t *tbl, const char *id, const char *path, sdb_ncolumn_t ncolumns,
		enum sdb_storage_kind_t storage_kind) {
    int r;
#ifdef SDB_VERBOSE_PRINT
    printf( "Initialized %d bytes of sdb struct\n", sizeof( * tbl));
#endif

    if( 0 == ncolumns || SDB_NCOLUMN_INVALID <= ncolumns)
        return SDB_EINVALID;

    memset( tbl, 0, sizeof( *tbl));
    tbl->conf_string_idx   = 0;
    tbl->conf_strings      = NULL;

    /* The first string in `conf_strings` is the table id, the 2nd is the table path */
    r = new_conf_string( tbl, id);
    if( r<0) goto fail_id;
    r = new_conf_string( tbl, path);
    if( r<0) goto fail_path;

    /* Allocate dynamic arrays. */
    tbl->columns = BS_MEM_ALLOC( ncolumns * sizeof( tbl->columns[0]));
    if( ! tbl->columns) goto fail_columns;

    /* Other initializations. */
    tbl->state             = SDB_ST_UNCONFIGURED;
    tbl->ncolumns          = ncolumns;
    tbl->consolidation     = NULL;
    tbl->serialization_ctx = NULL;
    tbl->nwrittenbytes     = 0;
    tbl->nwrittenobjects   = 0;
    tbl->conf_col          = 0;
    tbl->maxwrittenobjects = 0;

    r = sdb_untrim( tbl);
    if( r<0) goto fail_untrim;

    switch(storage_kind) {
    case SDB_SK_RAM: {
        struct sdb_ram_storage_t *ram = & tbl->storage.ram;
        sdb_chunk_t *c = BS_MEM_ALLOC( sizeof( * c)+SDB_MIN_CHUNK_SIZE-SDB_CHUNK_SIZE);
        if( ! c) goto fail_chunk;
        c->next = NULL;
        tbl->storage_kind = SDB_SK_RAM;
        ram->first_chunk = c;
        ram->last_chunk = c;
        ram->last_chunk_ptr = & ram->last_chunk;
        ram->last_chunk_size = SDB_MIN_CHUNK_SIZE;
        break;
    }
#ifdef SDB_FLASH_SUPPORT
    case SDB_SK_FLASH: {
        tbl->storage_kind = SDB_SK_FLASH;
//#        error "flash storage not implemented"
        return -1;

        break;
    }
#endif
#ifdef SDB_FILE_SUPPORT
    case SDB_SK_FILE: {
        FILE *fd = fopen( id, "a+");
        if( ! fd) return SDB_EBADFILE;
        tbl->storage_kind = SDB_SK_FILE;
        tbl->storage.file = fd;

        // TODO: get in a state where config is retrieved and
        // column configuration through sdb_setcolumn() calls
        // can be skipped?
        break;
    }
#endif
    }

    /* Handle allocation failures. */
    if( 0) {
        fail_chunk:
        sdb_trim( tbl);
        fail_untrim:
        BS_MEM_FREE( tbl->columns);
        fail_columns:
        fail_path:
        BS_MEM_FREE( tbl->conf_strings);
        fail_id:
        memset( tbl, 0, sizeof( *tbl));
        tbl->state = SDB_ST_BROKEN;
        return SDB_EMEM;
    }

    return SDB_EOK;
}