Browse Source

* merge simdb-{query,write} to simdb-tool

master
Alex 'AdUser' Z 8 years ago
parent
commit
cc2c2b0270
  1. 14
      src/CMakeLists.txt
  2. 61
      src/simdb-tool.c
  3. 103
      src/simdb-write.c

14
src/CMakeLists.txt

@ -15,15 +15,7 @@ if (WITH_TOOLS)
add_executable("simdb-1to2" "simdb-1to2.c") add_executable("simdb-1to2" "simdb-1to2.c")
install(TARGETS "simdb-1to2" RUNTIME DESTINATION "bin") install(TARGETS "simdb-1to2" RUNTIME DESTINATION "bin")
add_executable("simdb-query" "simdb-query.c") add_executable("simdb-tool" "simdb-tool.c")
target_link_libraries("simdb-query" "simdb") target_link_libraries("simdb-tool" "simdb")
install(TARGETS "simdb-query" RUNTIME DESTINATION "bin") install(TARGETS "simdb-tool" RUNTIME DESTINATION "bin")
if (${SAMPLER} STREQUAL "dummy")
message(STATUS "Sampler set to 'dummy', not building 'simdb-write' tool")
else ()
add_executable("simdb-write" "simdb-write.c")
target_link_libraries("simdb-write" "simdb")
install(TARGETS "simdb-write" RUNTIME DESTINATION "bin")
endif ()
endif () endif ()

61
src/simdb-query.c → src/simdb-tool.c

@ -30,9 +30,12 @@ void usage(int exitcode) {
" -t <int> Maximum difference pct (0 - 50, default: 10%%)\n" " -t <int> Maximum difference pct (0 - 50, default: 10%%)\n"
); );
fprintf(stderr, fprintf(stderr,
" -A <num>,<path> Add sample from 'path' as record 'num'\n"
" -B <num> Show bitmap for this sample\n" " -B <num> Show bitmap for this sample\n"
" -C <a>,<b> Show difference percent for this samples\n" " -C <a>,<b> Show difference percent for this samples\n"
" -D <num> Delete record <num>\n"
" -F <a>,<b> Show difference bitmap for this samples\n" " -F <a>,<b> Show difference bitmap for this samples\n"
" -I Create database (init)\n"
" -S <num> Search for images similar ot this sample\n" " -S <num> Search for images similar ot this sample\n"
" -U <num> Show db usage map, <num> entries per column\n" " -U <num> Show db usage map, <num> entries per column\n"
" Special case - 0, output will be single line\n" " Special case - 0, output will be single line\n"
@ -45,8 +48,9 @@ void usage(int exitcode) {
/** /**
* @brief Print bitmap to stdout as ascii-square * @brief Print bitmap to stdout as ascii-square
* @param map Source bitmap * @param map Source bitmap
* @note Height of "square" is equals to BITMAP_SIDE, * @parma side Side of the bitmap side
* but width is BITMAP_SIDE x 2, for ease of reading * @note Height of "square" is equals to @a side,
* but width is side x 2, for ease of reading
*/ */
void void
bitmap_print(const char *map, size_t side) { bitmap_print(const char *map, size_t side) {
@ -93,8 +97,7 @@ int search_similar(simdb_t *db, int num, float maxdiff) {
return 0; return 0;
} }
int db_usage_map(simdb_t *db, unsigned short int cols) int db_usage_map(simdb_t *db, unsigned short int cols) {
{
char *map = NULL; char *map = NULL;
char *m = NULL; char *m = NULL;
char row[256]; char row[256];
@ -133,8 +136,7 @@ int db_usage_map(simdb_t *db, unsigned short int cols)
return 0; return 0;
} }
int db_usage_slice(simdb_t *db, int offset, uint16_t limit) int db_usage_slice(simdb_t *db, int offset, uint16_t limit) {
{
char *map = NULL; char *map = NULL;
limit = simdb_usage_slice(db, &map, offset, limit); limit = simdb_usage_slice(db, &map, offset, limit);
@ -219,12 +221,12 @@ int rec_diff(simdb_t *db, int a, int b, unsigned short int showmap) {
return ret; return ret;
} }
int main(int argc, char **argv) int main(int argc, char **argv) {
{ enum { undef, add, del, init, search, bitmap, usage_map, usage_slice, diff } mode = undef;
enum { undef, search, bitmap, usage_map, usage_slice, diff } mode = undef;
const char *db_path = NULL; const char *db_path = NULL;
float maxdiff = 0.10; float maxdiff = 0.10;
unsigned short int cols = 64, map = 0, ret = 0; unsigned short int cols = 64, map = 0, ret = 0;
char *sample = NULL;
simdb_t *db = NULL; simdb_t *db = NULL;
int a = 0, b = 0; int a = 0, b = 0;
char *c = NULL; char *c = NULL;
@ -234,7 +236,7 @@ int main(int argc, char **argv)
if (argc < 3) if (argc < 3)
usage(EXIT_FAILURE); usage(EXIT_FAILURE);
while ((opt = getopt(argc, argv, "b:t:B:C:F:S:U:W:")) != -1) { while ((opt = getopt(argc, argv, "b:t:A:B:C:D:F:IS:U:W:")) != -1) {
switch (opt) { switch (opt) {
case 'b' : case 'b' :
db_path = optarg; db_path = optarg;
@ -247,10 +249,24 @@ int main(int argc, char **argv)
} }
maxdiff /= 100; maxdiff /= 100;
break; break;
case 'A' :
mode = add;
if ((c = strchr(optarg, ',')) == NULL)
usage(EXIT_FAILURE);
a = atoll(optarg);
sample = c + 1;
break;
case 'B' : case 'B' :
mode = bitmap; mode = bitmap;
a = atoll(optarg); a = atoll(optarg);
break; break;
case 'D' :
mode = del;
a = atoll(optarg);
break;
case 'I' :
mode = init;
break;
case 'F' : case 'F' :
map = 1; map = 1;
case 'C' : case 'C' :
@ -288,12 +304,37 @@ int main(int argc, char **argv)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (mode == init) {
if (!simdb_create(db_path)) {
fprintf(stderr, "database init: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
}
if ((db = simdb_open(db_path, 0, &err)) == NULL) { if ((db = simdb_open(db_path, 0, &err)) == NULL) {
fprintf(stderr, "database open: %d\n", err); fprintf(stderr, "database open: %d\n", err);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
switch (mode) { switch (mode) {
case add :
if (a == 0 || sample == NULL)
usage(EXIT_FAILURE);
if ((err = simdb_record_add(db, a, sample, 0)) < 0) {
fprintf(stderr, "%s\n", simdb_error(err));
exit(EXIT_FAILURE);
} else {
fprintf(stderr, "added as record #%d", err);
}
break;
case del :
if ((err = simdb_record_del(db, a)) < 0) {
fprintf(stderr, "%s\n", simdb_error(err));
exit(EXIT_FAILURE);
}
break;
case init :
/* this case already handled above */
break;
case search : case search :
if (a <= 0) { if (a <= 0) {
fprintf(stderr, "can't parse number\n"); fprintf(stderr, "can't parse number\n");

103
src/simdb-write.c

@ -1,103 +0,0 @@
#include "common.h"
#include "simdb.h"
#include <getopt.h>
void usage(int exitcode) {
fprintf(stderr,
"Usage: simdb-write <opts>\n"
" -b <path> Path to database\n"
);
fprintf(stderr,
" -A <num>,<path> Add sample from 'path' as record 'num'\n"
" -D <num> Delete record <num>\n"
" -I Create database (init)\n"
);
exit(exitcode);
}
int main(int argc, char **argv)
{
enum { undef, add, del, init } mode = undef;
char opt;
const char *db_path = NULL;
const char *sample = NULL;
const char *c = NULL;
simdb_t *db = NULL;
int num = 0, err = 0;
if (argc < 3)
usage(EXIT_FAILURE);
while ((opt = getopt(argc, argv, "b:A:D:I")) != -1) {
switch (opt) {
case 'b' :
db_path = optarg;
break;
case 'h' :
usage(EXIT_SUCCESS);
break;
case 'A' :
mode = add;
if ((c = strchr(optarg, ',')) == NULL)
usage(EXIT_FAILURE);
num = atoll(optarg);
sample = c + 1;
break;
case 'D' :
mode = del;
num = atoll(optarg);
break;
case 'I' :
mode = init;
break;
default :
usage(EXIT_FAILURE);
break;
}
}
if (db_path == NULL) {
fprintf(stderr, "error: db path not set\n");
usage(EXIT_FAILURE);
}
if (mode == init) {
if (!simdb_create(db_path)) {
fprintf(stderr, "database init: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
}
if ((db = simdb_open(db_path, 1, &err)) == NULL) {
fprintf(stderr, "can't open database: %d\n", err);
exit(EXIT_FAILURE);
}
switch (mode) {
case add :
if (num == 0 || sample == NULL)
usage(EXIT_FAILURE);
if ((err = simdb_record_add(db, num, sample, 0)) < 0) {
fprintf(stderr, "%s\n", simdb_error(err));
exit(EXIT_FAILURE);
} else {
fprintf(stderr, "added as record #%d", err);
}
break;
case del :
if ((err = simdb_record_del(db, num)) < 0) {
fprintf(stderr, "%s\n", simdb_error(err));
exit(EXIT_FAILURE);
}
break;
case init :
/* this case already handled above */
break;
default:
usage(EXIT_FAILURE);
break;
}
simdb_close(db);
exit(EXIT_SUCCESS);
}
Loading…
Cancel
Save