static struct ddb_cons * ddb_cons_alloc(void) { struct ddb_cons *cons = ddb_cons_new(); if (!cons) PyErr_NoMemory(); return cons; }
int main(int argc, char **argv) { if (argc < 2) { fprintf(stderr, "Usage:\n"); fprintf(stderr, "create_discodb discodb.out input.txt\n"); fprintf(stderr, "where input.txt contain a key-value pair on each line, devided by space.\n"); exit(1); } FILE *in; FILE *out; uint64_t size; char *data; struct ddb_cons *db = ddb_cons_new(); uint64_t flags = 0; flags |= getenv("DONT_COMPRESS") ? DDB_OPT_DISABLE_COMPRESSION: 0; flags |= getenv("UNIQUE_ITEMS") ? DDB_OPT_UNIQUE_ITEMS: 0; if (!db) { fprintf(stderr, "DB init failed\n"); exit(1); } if (!(in = fopen(argv[2], "r"))) { fprintf(stderr, "Couldn't open %s\n", argv[2]); exit(1); } if (getenv("KEYS_ONLY")) read_keys(in, db); else read_pairs(in, db); fprintf(stderr, "Packing the index..\n"); if (!(data = ddb_finalize(db, &size, flags))) { fprintf(stderr, "Packing the index failed\n"); exit(1); } ddb_cons_free(db); if (!(out = fopen(argv[1], "w"))) { fprintf(stderr, "Opening file %s failed\n", argv[1]); exit(1); } if (!fwrite(data, size, 1, out)) { fprintf(stderr, "Writing file %s failed\n", argv[1]); exit(1); } fclose(out); free(data); fprintf(stderr, "Ok! Index written to %s\n", argv[1]); return 0; }
int main(int argc, char **argv) { if (argc < 2){ fprintf(stderr, "Usage:\n"); fprintf(stderr, "create_discodb [discodb.out] [file_1] ... [file_N]\n"); fprintf(stderr, "where input files contain key on the first line and values on the next lines.\n"); exit(1); } struct ddb_cons *db = ddb_cons_new(); if (!db){ fprintf(stderr, "DB init failed\n"); exit(1); } int i = 0; for (i = 2; i < argc; i++){ uint32_t n; struct ddb_entry key = {NULL, 0}; struct ddb_entry *values = read_file(argv[i], &key, &n); if (ddb_add(db, &key, values, n)){ fprintf(stderr, "Adding entries from %s failed: out of memory\n", argv[i]); exit(1); } while(n--) free((char*)values[n].data); free(values); free((char*)key.data); } fprintf(stderr, "%u files read. Packing the index..\n", argc - 2); uint64_t size; char *data; if (!(data = ddb_finalize(db, &size))){ fprintf(stderr, "Packing the index failed: duplicate keys or out of memory\n"); exit(1); } FILE *out; if (!(out = fopen(argv[1], "w"))){ fprintf(stderr, "Opening file %s failed\n", argv[1]); exit(1); } fwrite(data, size, 1, out); fclose(out); free(data); fprintf(stderr, "Ok! Index written to %s\n", argv[1]); return 0; }
struct ddb_cons *ddb_cons_ddb(struct ddb *db) { struct ddb_cons *cons; struct ddb_cursor *keys = NULL, *vals = NULL; const struct ddb_entry *k, *v; int errno = 0; if (!(cons = ddb_cons_new())) goto error; if (!(keys = ddb_keys(db))) goto error; while ((k = ddb_next(keys, &errno))) { if (!(vals = ddb_getitem(db, k))) goto error; while ((v = ddb_next(vals, &errno))) if (ddb_cons_add(cons, k, v)) goto error; if (errno) goto error; ddb_free_cursor(vals); } if (errno) goto error; ddb_free_cursor(keys); return cons; error: if (cons) ddb_cons_free(cons); if (keys) ddb_free_cursor(keys); if (vals) ddb_free_cursor(vals); return NULL; }