dbtest master
authorhgn <hgodden00@gmail.com>
Fri, 2 May 2025 17:18:27 +0000 (18:18 +0100)
committerhgn <hgodden00@gmail.com>
Fri, 2 May 2025 17:18:27 +0000 (18:18 +0100)
src/dbtest.c

index db34a7f550b96bb7a69184f8d9ded4b334cfe8de..d0ad269977c7523257d1f11cf4dc2e6dd81b3993 100644 (file)
@@ -10,6 +10,7 @@
 #include "vg/vg_log.h"
 #include "vg/vg_m.h"
 #include "vg/vg_db.h"
+#include "vg/vg_opt.h"
 
 const char *stringybob = "Hello World!";
 
@@ -37,13 +38,32 @@ static i32 entry_comp_steamid( vg_skipper_context *ctx, void *comparand, u16 ite
    else return compid < steamid? -1: 1;
 }
 
-int main( int argc, char *argv[] )
+int main( int argc, const char *argv[] )
 {
    vg_log_init();
+   _vg_opt_init( argc, argv );
+
+   bool die_mid_transaction = 0,
+        die_in_kinda_safe_place = 0;
+
+   if( vg_long_opt( "die-mid-transaction", "" ) )
+      die_mid_transaction = 1;
+
+   if( vg_long_opt( "die-safe", "" ) )
+      die_in_kinda_safe_place = 1;
+
+   int num_to_add = 2;
+   const char *arg;
+   if( (arg = vg_long_opt_arg( "add", "" )) )
+      num_to_add = atoi( arg );
+
+   if( !_vg_opt_check() )
+      exit(0);
+
    char yes[512];
    strcpy( yes, stringybob );
 
-   vg_db_open( &db, "main.db" );
+   vg_db_open( &db, "main.db", "main.db.wal" );
 
    u64 table_addr = db.userdata_address+offsetof(struct playground,table),
        steamid_skip_addr = db.userdata_address+offsetof(struct playground,steamid_skipper);
@@ -52,6 +72,8 @@ int main( int argc, char *argv[] )
    vg_db_dumb_table_init( &db, table_addr, sizeof(struct entry), max );
    vg_db_skipper_init( &db, steamid_skip_addr, max );
    //vg_db_skipper_init( &db, db.userdata_address+offsetof(struct playground,time_skipper), max );
+
+   vg_db_checkpoint( &db );
    
    vg_skipper_context ctx = {
       .address = steamid_skip_addr,
@@ -59,7 +81,7 @@ int main( int argc, char *argv[] )
       .fn_compare = entry_comp_steamid
    };
    
-   for( u32 i=0; i<8; i ++ )
+   for( u32 i=0; i<num_to_add; i ++ )
    {
       u64 entry_addr = vg_db_dumb_table_append( &db, table_addr );
       struct entry entry = 
@@ -68,7 +90,15 @@ int main( int argc, char *argv[] )
          .something_else = vg_randu32(&db.rand) & 0xff
       };
       vg_db_write( &db, entry_addr, &entry, sizeof(entry) );
+
+      if( die_mid_transaction )
+      {
+         vg_error( "Dieing mid transaction\n" );
+         exit(1);
+      }
+
       vg_db_skipper_placement( &db, &ctx, vg_db_dumb_table_count( &db, table_addr )-1, &entry.steamid );
+      vg_db_checkpoint( &db );
    }
 
 #if 0
@@ -95,6 +125,12 @@ int main( int argc, char *argv[] )
       vg_info( "Steamid: %lx\n", thingy.steamid );
    }
 
+   if( die_in_kinda_safe_place )
+   {
+      vg_error( "Dieing in kinda safe place\n" );
+      exit(1);
+   }
+
 #if 0
    u64 users_tree_address = db.userdata_address + offsetof(struct skaterift_database, users_tree);
    vg_db_tree_init( &db, users_tree_address );