| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #include <stdio.h> |
| #include <stdlib.h> |
| #include <stdint.h> |
| #include <string.h> |
| #include "sqlite3.h" |
|
|
| static uint64_t rng; |
| static uint64_t next_rand(void) { |
| rng = rng * 6364136223846793005ULL + 1442695040888963407ULL; |
| return rng >> 17; |
| } |
|
|
| static void must(sqlite3 *db, int rc, const char *what) { |
| if (rc != SQLITE_OK && rc != SQLITE_DONE && rc != SQLITE_ROW) { |
| fprintf(stderr, "%s failed: %d %s\n", what, rc, sqlite3_errmsg(db)); |
| exit(1); |
| } |
| } |
|
|
| static void run(sqlite3 *db, const char *sql) { |
| char *err = NULL; |
| int rc = sqlite3_exec(db, sql, NULL, NULL, &err); |
| if (rc != SQLITE_OK) { |
| fprintf(stderr, "exec '%s' failed: %s\n", sql, err ? err : "?"); |
| exit(1); |
| } |
| } |
|
|
| int main(int argc, char **argv) { |
| if (argc != 3) { |
| fprintf(stderr, "usage: %s <db-path> <nrows>\n", argv[0]); |
| return 2; |
| } |
| const char *path = argv[1]; |
| long nrows = atol(argv[2]); |
|
|
| remove(path); |
| sqlite3 *db = NULL; |
| must(db, sqlite3_open_v2(path, &db, |
| SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL), |
| "open"); |
|
|
| run(db, "PRAGMA page_size=4096;"); |
| run(db, "PRAGMA journal_mode=OFF;"); |
| run(db, "PRAGMA synchronous=OFF;"); |
| run(db, "PRAGMA cache_size=-524288;"); |
| run(db, "CREATE TABLE records(" |
| " id INTEGER PRIMARY KEY," |
| " k INTEGER NOT NULL," |
| " v0 INTEGER NOT NULL," |
| " v1 INTEGER NOT NULL," |
| " payload TEXT NOT NULL);"); |
|
|
| sqlite3_stmt *ins = NULL; |
| must(db, sqlite3_prepare_v2(db, |
| "INSERT INTO records(id,k,v0,v1,payload) VALUES(?,?,?,?,?)", -1, |
| &ins, NULL), "prepare insert"); |
|
|
| char payload[65]; |
| memset(payload, 'a', sizeof(payload) - 1); |
| payload[sizeof(payload) - 1] = '\0'; |
|
|
| rng = 0x5deece66dULL; |
| run(db, "BEGIN;"); |
| for (long i = 1; i <= nrows; i++) { |
| uint64_t r0 = next_rand(); |
| uint64_t r1 = next_rand(); |
| |
| |
| long k = (long)((r0 * 2654435761ULL) % (uint64_t)(2 * nrows)); |
| for (int c = 0; c < 16; c++) |
| payload[c] = (char)('a' + (int)((r1 >> (c * 2)) & 0xf)); |
| sqlite3_bind_int64(ins, 1, i); |
| sqlite3_bind_int64(ins, 2, k); |
| sqlite3_bind_int64(ins, 3, (sqlite3_int64)(r0 & 0xffffff)); |
| sqlite3_bind_int64(ins, 4, (sqlite3_int64)(r1 & 0xffffff)); |
| sqlite3_bind_text(ins, 5, payload, (int)sizeof(payload) - 1, |
| SQLITE_STATIC); |
| int rc = sqlite3_step(ins); |
| must(db, rc, "step insert"); |
| sqlite3_reset(ins); |
| if ((i % 500000) == 0) { |
| run(db, "COMMIT;"); |
| run(db, "BEGIN;"); |
| fprintf(stderr, "inserted %ld rows\n", i); |
| } |
| } |
| run(db, "COMMIT;"); |
| sqlite3_finalize(ins); |
|
|
| run(db, "CREATE INDEX idx_k ON records(k);"); |
| run(db, "ANALYZE;"); |
| sqlite3_close(db); |
| fprintf(stderr, "setup done: %ld rows\n", nrows); |
| return 0; |
| } |
|
|