tdb:tdbtool: add transaction_start/_commit/_cancel commands.
[amitay/samba.git] / lib / tdb / tools / tdbtool.c
index 2a11cdaef3d0e22ab9a8739ac2d3c59637a32882..193140b801dae1e8973955b32090f28419198be9 100644 (file)
@@ -40,6 +40,9 @@ static int disable_mmap;
 enum commands {
        CMD_CREATE_TDB,
        CMD_OPEN_TDB,
+       CMD_TRANSACTION_START,
+       CMD_TRANSACTION_COMMIT,
+       CMD_TRANSACTION_CANCEL,
        CMD_ERASE,
        CMD_DUMP,
        CMD_INSERT,
@@ -70,6 +73,9 @@ typedef struct {
 COMMAND_TABLE cmd_table[] = {
        {"create",      CMD_CREATE_TDB},
        {"open",        CMD_OPEN_TDB},
+       {"transaction_start",   CMD_TRANSACTION_START},
+       {"transaction_commit",  CMD_TRANSACTION_COMMIT},
+       {"transaction_cancel",  CMD_TRANSACTION_CANCEL},
        {"erase",       CMD_ERASE},
        {"dump",        CMD_DUMP},
        {"insert",      CMD_INSERT},
@@ -169,6 +175,9 @@ static void help(void)
 "tdbtool: \n"
 "  create    dbname     : create a database\n"
 "  open      dbname     : open an existing database\n"
+"  transaction_start    : start a transaction\n"
+"  transaction_commit   : commit a transaction\n"
+"  transaction_cancel   : cancel a transaction\n"
 "  erase                : erase the database\n"
 "  dump                 : dump the database as strings\n"
 "  keys                 : dump the database keys as strings\n"
@@ -182,6 +191,7 @@ static void help(void)
 "  list                 : print the database hash table and freelist\n"
 "  free                 : print the database freelist\n"
 "  check                : check the integrity of an opened database\n"
+"  speed                : perform speed tests on the database\n"
 "  ! command            : execute system command\n"
 "  1 | first            : print the first record\n"
 "  n | next             : print the next record\n"
@@ -392,15 +402,68 @@ static void speed_tdb(const char *tlimit)
 {
        unsigned timelimit = tlimit?atoi(tlimit):0;
        double t;
-       int ops=0;
-       if (timelimit == 0) timelimit = 10;
+       int ops;
+       if (timelimit == 0) timelimit = 5;
+
+       ops = 0;
+       printf("Testing store speed for %u seconds\n", timelimit);
+       _start_timer();
+       do {
+               long int r = random();
+               TDB_DATA key, dbuf;
+               key.dptr = (unsigned char *)"store test";
+               key.dsize = strlen((char *)key.dptr);
+               dbuf.dptr = (unsigned char *)&r;
+               dbuf.dsize = sizeof(r);
+               tdb_store(tdb, key, dbuf, TDB_REPLACE);
+               t = _end_timer();
+               ops++;
+       } while (t < timelimit);
+       printf("%10.3f ops/sec\n", ops/t);
+
+       ops = 0;
+       printf("Testing fetch speed for %u seconds\n", timelimit);
+       _start_timer();
+       do {
+               long int r = random();
+               TDB_DATA key, dbuf;
+               key.dptr = (unsigned char *)"store test";
+               key.dsize = strlen((char *)key.dptr);
+               dbuf.dptr = (unsigned char *)&r;
+               dbuf.dsize = sizeof(r);
+               tdb_fetch(tdb, key);
+               t = _end_timer();
+               ops++;
+       } while (t < timelimit);
+       printf("%10.3f ops/sec\n", ops/t);
+
+       ops = 0;
+       printf("Testing transaction speed for %u seconds\n", timelimit);
+       _start_timer();
+       do {
+               long int r = random();
+               TDB_DATA key, dbuf;
+               key.dptr = (unsigned char *)"transaction test";
+               key.dsize = strlen((char *)key.dptr);
+               dbuf.dptr = (unsigned char *)&r;
+               dbuf.dsize = sizeof(r);
+               tdb_transaction_start(tdb);
+               tdb_store(tdb, key, dbuf, TDB_REPLACE);
+               tdb_transaction_commit(tdb);
+               t = _end_timer();
+               ops++;
+       } while (t < timelimit);
+       printf("%10.3f ops/sec\n", ops/t);
+
+       ops = 0;
        printf("Testing traverse speed for %u seconds\n", timelimit);
        _start_timer();
-       while ((t=_end_timer()) < timelimit) {
+       do {
                tdb_traverse(tdb, traverse_fn, NULL);
-               printf("%10.3f ops/sec\r", (++ops)/t);
-       }
-       printf("\n");
+               t = _end_timer();
+               ops++;
+       } while (t < timelimit);
+       printf("%10.3f ops/sec\n", ops/t);
 }
 
 static void toggle_mmap(void)
@@ -506,7 +569,9 @@ static int do_command(void)
             return 0;
        case CMD_SYSTEM:
            /* Shell command */
-           system(arg1);
+           if (system(arg1) == -1) {
+               terror("system() call failed\n");
+           }
            return 0;
        case CMD_QUIT:
            return 1;
@@ -519,6 +584,18 @@ static int do_command(void)
                return 0;
            }
            switch (mycmd) {
+           case CMD_TRANSACTION_START:
+               bIterate = 0;
+               tdb_transaction_start(tdb);
+               return 0;
+           case CMD_TRANSACTION_COMMIT:
+               bIterate = 0;
+               tdb_transaction_commit(tdb);
+               return 0;
+           case CMD_TRANSACTION_CANCEL:
+               bIterate = 0;
+               tdb_transaction_cancel(tdb);
+               return 0;
            case CMD_ERASE:
                bIterate = 0;
                tdb_traverse(tdb, do_delete_fn, NULL);