]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
test_libcephsqlite: test random inserts
authorPatrick Donnelly <pdonnell@redhat.com>
Tue, 23 Jul 2019 21:46:03 +0000 (14:46 -0700)
committerPatrick Donnelly <pdonnell@redhat.com>
Fri, 19 Mar 2021 15:52:54 +0000 (08:52 -0700)
Signed-off-by: Patrick Donnelly <pdonnell@redhat.com>
src/test/test_libcephsqlite.cc

index 989d1b0c24c0bc4488c4f9bcc57d8a63b2fdce13..248a9682f14945ea2dc913249e774bd76bace1a4 100644 (file)
 
 #include <sqlite3.h>
 
+#define sqlcatchcode(S, code) \
+do {\
+    rc = S;\
+    if (rc != code) {\
+        if (rc == SQLITE_BUSY) {\
+            rc = EAGAIN;\
+        } else {\
+            std::cerr << "[" << __FILE__ << ":" << __LINE__ << "]"\
+                      << " sqlite3 error: " << rc << " `" << sqlite3_errstr(rc)\
+                      << "': " << sqlite3_errmsg(db) << std::endl;\
+            if (rc == SQLITE_CONSTRAINT) {\
+                rc = EINVAL;\
+            } else {\
+                rc = EIO;\
+            }\
+        }\
+        sqlite3_finalize(stmt);\
+        stmt = NULL;\
+        goto out;\
+    }\
+} while (0)
+
+#define sqlcatch(S) sqlcatchcode(S, 0)
+
 void create_db(sqlite3 *db);
+void insert_rand(sqlite3 *db, uint64_t count, uint64_t size);
 void insert_db(sqlite3 *db, const char *file_name);
 long int count_db(sqlite3 *db);
 void list_db(sqlite3 *db);
@@ -64,6 +89,8 @@ int main(int argc, char **argv)
 
     if (cmd == "create") {
         create_db(db);
+    } else if (cmd == "insert_rand") {
+        insert_rand(db, strtoul(argv[2], NULL, 10), strtoul(argv[3], NULL, 10)); /* argv[2] contains file name of data file */
     } else if (cmd == "insert") {
         insert_db(db, argv[2]); /* argv[2] contains file name of data file */
     } else if (cmd == "count") {
@@ -132,6 +159,34 @@ out:
     sqlite3_finalize(stmt);
 }
 
+void insert_rand(sqlite3 *db, uint64_t count, uint64_t size)
+{
+    static const char SQL[] =
+      "CREATE TABLE IF NOT EXISTS rand(text BLOB NOT NULL);"
+      "INSERT INTO rand VALUES (randomblob(?));"
+      ;
+
+    sqlite3_stmt *stmt = NULL;
+    const char *current = SQL;
+    int rc;
+
+    sqlcatch(sqlite3_prepare_v2(db, current, -1, &stmt, &current));
+    sqlcatchcode(sqlite3_step(stmt), SQLITE_DONE);
+    sqlcatch(sqlite3_finalize(stmt));
+
+    sqlcatch(sqlite3_prepare_v2(db, current, -1, &stmt, &current));
+    sqlcatch(sqlite3_bind_int64(stmt, 1, (sqlite3_int64)size));
+    for (uint64_t i = 0; i < count; i++) {
+      sqlcatchcode(sqlite3_step(stmt), SQLITE_DONE);
+      std::cout << "last row inserted: " << sqlite3_last_insert_rowid(db) << std::endl;
+    }
+    sqlcatch(sqlite3_finalize(stmt));
+
+out:
+    (void)0;
+}
+
+
 void insert_db(sqlite3 *db, const char *file_name)
 {
     int row = 0;