4 // Databases (hash tables)
5 const int DB_BUCKETS = 8192;
6 void db_save(int db, string filename)
8 int fh = fopen(filename, FILE_WRITE);
11 LOG_WARNINGF("^1Can't write DB to %s\n", filename);
14 fputs(fh, strcat(ftos(DB_BUCKETS), "\n"));
15 for (int i = 0, n = buf_getsize(db); i < n; ++i)
16 fputs(fh, strcat(bufstr_get(db, i), "\n"));
25 void db_put(int db, string key, string value);
27 int db_load(string filename)
29 int db = buf_create();
30 if (db < 0) return -1;
31 int fh = fopen(filename, FILE_READ);
32 if (fh < 0) return db;
34 if (stoi(l) == DB_BUCKETS)
36 for (int i = 0; (l = fgets(fh)); ++i)
38 if (l != "") bufstr_set(db, i, l);
43 // different count of buckets, or a dump?
44 // need to reorganize the database then (SLOW)
46 // note: we also parse the first line (l) in case the DB file is
47 // missing the bucket count
50 int n = tokenizebyseparator(l, "\\");
51 for (int j = 2; j < n; j += 2)
52 db_put(db, argv(j - 1), uri_unescape(argv(j)));
54 while ((l = fgets(fh)));
60 void db_dump(int db, string filename)
62 int fh = fopen(filename, FILE_WRITE);
63 if (fh < 0) LOG_FATALF("Can't dump DB to %s\n");
65 for (int i = 0, n = buf_getsize(db); i < n; ++i)
67 int m = tokenizebyseparator(bufstr_get(db, i), "\\");
68 for (int j = 2; j < m; j += 2)
69 fputs(fh, strcat("\\", argv(j - 1), "\\", argv(j), "\n"));
79 string db_get(int db, string key)
81 int h = crc16(false, key) % DB_BUCKETS;
82 return uri_unescape(infoget(bufstr_get(db, h), key));
85 #define db_remove(db, key) db_put(db, key, "")
87 void db_put(int db, string key, string value)
89 int h = crc16(false, key) % DB_BUCKETS;
90 bufstr_set(db, h, infoadd(bufstr_get(db, h), key, uri_escape(value)));
95 LOG_INFO("LOAD...\n");
96 int db = db_load("foo.db");
97 LOG_INFO("LOADED. FILL...\n");
98 for (int i = 0; i < DB_BUCKETS; ++i)
99 db_put(db, ftos(random()), "X");
100 LOG_INFO("FILLED. SAVE...\n");
101 db_save(db, "foo.db");
102 LOG_INFO("SAVED. CLOSE...\n");
104 LOG_INFO("CLOSED.\n");