/* Release all resources reserved by the table. * If there is a serialization in progress, it is canceled. */ int sdb_reset( sdb_table_t *tbl) { int i; if( tbl->state == SDB_ST_SERIALIZING) sdb_serialize_cancel( tbl); if( tbl->state != SDB_ST_READING) return SDB_EBADSTATE; // TODO must be able to reset an unconfigured table switch( tbl->storage_kind) { case SDB_SK_RAM: { struct sdb_chunk_t *p, *q; struct sdb_ram_storage_t *ram = & tbl->storage.ram; p = ram->first_chunk; while( p) { q=p->next; BS_MEM_FREE( p); p=q; } p = BS_MEM_ALLOC( sizeof( struct sdb_chunk_t) + SDB_MIN_CHUNK_SIZE - SDB_CHUNK_SIZE); if( ! p) { tbl->state = SDB_ST_BROKEN; return SDB_EMEM; } p->next = NULL; ram->first_chunk = p; ram->last_chunk = p; ram->last_chunk_ptr = & ram->last_chunk; ram->last_chunk_size = SDB_MIN_CHUNK_SIZE; break; } #ifdef SDB_FILE_SUPPORT case SDB_SK_FILE: // identifier/filename is stored as the 1st conf string tbl->storage.file = freopen( tbl->conf_strings, "w+", tbl->storage.file); // erases content if( ! tbl->storage.file) return SDB_EBADFILE; break; #endif } tbl->nwrittenbytes = 0; tbl->nwrittenobjects = 0; // reset data analysis for( i=0; i<tbl->ncolumns; i++) { sdb_column_t *c = tbl->columns + i; if( SDB_SM_SMALLEST == SDB_SM_CONTAINER(c->serialization_method)) { c->data_analysis.delta_sum = 0; c->data_analysis.all_integer = 1; c->data_analysis.all_numeric = 1; } } if( tbl->bss_ctx) bss_reset( tbl->bss_ctx); return SDB_EOK; }
/* Release resources reserved by the table. * Warning: it is an error, with unspecified result, to close a table * which is the consolidation destination of another table. If a consolidation * attempt is made on the source table, a memory corruption is likely to occur. */ void sdb_close( sdb_table_t *tbl) { if( tbl->state == SDB_ST_SERIALIZING) sdb_serialize_cancel( tbl); tbl->state = SDB_ST_BROKEN; if( tbl->columns) { BS_MEM_FREE( tbl->columns); tbl->columns = NULL; } if( tbl->consolidation) { // columns are allocated in the same block as the cons struct. // BS_MEM_FREE( tbl->consolidation->dst_columns); BS_MEM_FREE( tbl->consolidation); tbl->consolidation = NULL; } switch( tbl->storage_kind) { case SDB_SK_RAM: { struct sdb_chunk_t *p = tbl->storage.ram.first_chunk, *q; while( p) { q=p->next; BS_MEM_FREE( p); p=q; } tbl->storage.ram.first_chunk = tbl->storage.ram.last_chunk = NULL; break; } #ifdef SDB_FILE_SUPPORT case SDB_SK_FILE: { if(tbl->storage.file) { fclose( tbl->storage.file); tbl->storage.file = NULL; } break; } #endif } if( tbl->bss_ctx) { BS_MEM_FREE( tbl->bss_ctx); tbl->bss_ctx=NULL; } BS_MEM_FREE( tbl->conf_strings); tbl->conf_strings = NULL; }
// mysdb :serialize_cancel() static int api_serialize_cancel( lua_State *L) { int r = sdb_serialize_cancel( lua_sdb_checktable( L, 1)); lua_settop( L, 1); // in case of extra args lua_pushinteger( L, r); // no lua_error, just a status return 2; // push table back }