Browse Source

* drop simdb_{record,block}_read()

master
Alex 'AdUser' Z 8 years ago
parent
commit
4a37f289c0
  1. 34
      src/database.c
  2. 20
      src/simdb.h
  3. 3
      tests/CMakeLists.txt
  4. 62
      tests/database.c

34
src/database.c

@ -237,24 +237,6 @@ simdb_write(simdb_t *db, int start, int records, simdb_urec_t *data) {
return records;
}
int simdb_record_read(simdb_t *db, simdb_rec_t *rec) {
ssize_t bytes = 0;
assert(db != NULL);
assert(rec != NULL);
assert(rec->num > 0);
DB_READ(db, rec->data, SIMDB_REC_LEN, rec->num * SIMDB_REC_LEN);
if (bytes != SIMDB_REC_LEN)
return errno ? SIMDB_ERR_SYSTEM : SIMDB_ERR_NXRECORD;
if (rec->data[0] != 0xFF)
return 0;
return 1;
}
int simdb_record_write(simdb_t *db, simdb_rec_t *rec) {
ssize_t bytes = 0;
@ -273,22 +255,6 @@ int simdb_record_write(simdb_t *db, simdb_rec_t *rec) {
return 1;
}
int simdb_block_read(simdb_t *db, simdb_block_t *blk) {
ssize_t bytes = 0;
assert(db != NULL);
assert(blk != NULL);
assert(blk->start > 0);
assert(blk->records > 0);
FREE(blk->data);
CALLOC(blk->data, blk->records, SIMDB_REC_LEN);
DB_READ(db, blk->data, blk->records * SIMDB_REC_LEN, blk->start * SIMDB_REC_LEN);
blk->records = bytes / SIMDB_REC_LEN;
return blk->records;
}
int
simdb_records_count(simdb_t * const db) {
struct stat st;

20
src/simdb.h

@ -121,16 +121,6 @@ int simdb_search(simdb_t * const db, int num,
simdb_search_t * const search,
simdb_match_t ** matches);
/**
* @brief Read single record from database
* @param db Database handle
* @param rec Record struct
* @note @a num member of @a rec struct should be set
* @retval 1 on success
* @retval 0 if record exists but not used,
* @retval <0 on error (system error or record missing)
*/
int simdb_record_read(simdb_t *db, simdb_rec_t *rec);
/**
* @brief Write single record to database
* @param db Database handle
@ -148,16 +138,6 @@ int simdb_record_write(simdb_t *db, simdb_rec_t *rec);
*/
bool simdb_record_create(simdb_rec_t * const rec, const char *path);
/**
* @brief Read continious records block from database
* @param db Database handle
* @param blk Block struct
* @note @a start and @a records members of @a blk struct should be set
* @retval >=0 As number of records actually read
* @retval <0 on error
*/
int simdb_block_read(simdb_t *db, simdb_block_t *blk);
/**
* @brief Get database capacity
*/

3
tests/CMakeLists.txt

@ -6,6 +6,3 @@ add_test("test/record" "test-record")
add_executable("test-io" "io.c" "../src/database.c" "../src/bitmap.c")
add_test("test/io" "test-io")
add_executable("test-database" "database.c" "../src/database.c" "../src/bitmap.c")
add_test("test/database" "test-database")

62
tests/database.c

@ -1,62 +0,0 @@
#include "../src/common.h"
#include "../src/simdb.h"
int main() {
simdb_t *db;
simdb_rec_t rec[2];
simdb_block_t blk;
char *path = "test.db";
int mode = 0, err = 0, num;
bool ret;
unlink(path);
db = simdb_open(path, mode, &err);
assert(db == NULL);
assert(err == -1); /* no such file */
ret = simdb_create(path);
assert(ret == true);
db = simdb_open(path, mode, &err);
assert(db != NULL);
rec[0].num = 1;
err = simdb_record_read(db, rec);
assert(err == SIMDB_ERR_NXRECORD);
memset(rec[0].data, 0xAA, SIMDB_REC_LEN);
memset(rec[0].data, 0xFF, 1); /* record is used */
num = simdb_record_write(db, rec);
assert(num == SIMDB_ERR_READONLY); /* database open in read-only mode */
simdb_close(db);
mode |= SIMDB_FLAG_WRITE;
db = simdb_open(path, mode, &err);
assert(db != NULL);
num = simdb_record_write(db, rec);
assert(num == 1); /* success */
num = simdb_record_read(db, rec);
assert(num == 1);
blk.start = 1;
blk.records = 2;
blk.data = NULL;
num = simdb_block_read(db, &blk);
assert(num == 1);
assert(blk.records == 1);
assert(blk.data != NULL);
rec[0].num = 1;
rec[1].num = 3;
simdb_close(db);
unlink(path);
return 0;
}
Loading…
Cancel
Save