libdoltlite-sys 0.38.21

Native bindings to the DoltLite SQLite-compatible library
Documentation
#
# 30-August-2023
#
#-------------------------------------------------------------------------
#
# Tests for the bcv_stat table in daemon mode.
#

source [file join [file dirname [info script]] test_common.tcl]
set testprefix dmn_vtab4.test

bcv_create_container cont1
bcv_upload_new_db cont1 test.db {
  CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
  BEGIN;
  WITH s(i) AS ( VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<10000 )
  INSERT INTO t1(y) SELECT hex(randomblob(500)) FROM s;
  COMMIT;
}

bcv_upload_new_db cont1 test.db2 {
  CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
  BEGIN;
  WITH s(i) AS ( VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<8000 )
  INSERT INTO t1(y) SELECT hex(randomblob(500)) FROM s;
  COMMIT;
}

start_new_daemon 0 
set cmd [bcvfs_create testdir.0 "bcvfs"]
bcvfs_attach $cmd cont1

sqlite3 db /cont1/test.db -vfs bcvfs
sqlite3_bcvfs_register_vtab db

do_execsql_test 1.0 {
  SELECT container, database, nblock, ncache FROM bcv_database;
} {
  cont1 test.db 79 1
  cont1 test.db2 63 0
}

do_execsql_test 1.1 {
  SELECT * FROM bcv_stat WHERE name NOT like 'memory%'
} {
  nlock 1 ncache 1 cachesize 8192
}

do_execsql_test 1.2 { 
  SELECT count(y) FROM t1 WHERE x<=5000;
} {
  5000
}

do_execsql_test 1.3 {
  SELECT container, database, nblock, ncache FROM bcv_database;
} {
  cont1 test.db 79 40
  cont1 test.db2 63 0
}

do_execsql_test 1.4 {
  SELECT * FROM bcv_stat WHERE name NOT like 'memory%'
} {
  nlock 40 ncache 40 cachesize 8192
}

sqlite3 db2 /cont1/test.db2 -vfs bcvfs
sqlite3_bcvfs_register_vtab db2

do_execsql2_test 1.5 {
  SELECT count(y) FROM t1
} {
  8000
}

do_execsql_test 1.6 {
  SELECT * FROM bcv_stat WHERE name NOT like 'memory%'
} {
  nlock 40 ncache 103 cachesize 8192
}

do_execsql2_test 1.7 {
  SELECT * FROM bcv_stat WHERE name NOT like 'memory%'
} {
  nlock 63 ncache 103 cachesize 8192
}

do_execsql2_test 1.8 {
  BEGIN;
    SELECT * FROM bcv_stat WHERE name NOT like 'memory%'
} {
  nlock 63 ncache 103 cachesize 8192
}

do_execsql_test 1.9 {
  SELECT * FROM bcv_stat WHERE name NOT like 'memory%'
} {
  nlock 103 ncache 103 cachesize 8192
}

do_execsql2_test 1.10 {
  COMMIT
}

do_execsql_test 1.11 {
  SELECT * FROM bcv_stat WHERE name NOT like 'memory%'
} {
  nlock 40 ncache 103 cachesize 8192
}


db close
db2 close
$cmd destroy

wait_daemon 0

#-------------------------------------------------------------------------

bcv_create_container cont1
bcv_upload_new_db cont1 test.db {
  CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
  INSERT INTO t1 VALUES(1, '1 1');
}
bcv_upload_new_db cont1 test.db2 {
  CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
  INSERT INTO t1 VALUES(2, '2 2');
}
bcv_upload_new_db cont1 test.db3 {
  CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
  INSERT INTO t1 VALUES(3, '3 3');
}
bcv_upload_new_db cont1 test.db4 {
  CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
  INSERT INTO t1 VALUES(4, '4 4');
}

start_new_daemon 0 
set cmd [bcvfs_create testdir.0 "bcvfs"]
bcvfs_attach $cmd cont1

sqlite3 db / -vfs bcvfs
sqlite3_bcvfs_register_vtab db

do_execsql_test 2.0 {
  SELECT container, database, ntrans FROM bcv_database
} {
  cont1 test.db  0
  cont1 test.db2 0
  cont1 test.db3 0
  cont1 test.db4 0
}

unset -nocomplain ::handles
set handle_counter 0
proc open_reader {hdl} {
  if {![info exists ::handles($hdl)]} {
    if {0==[regexp {.*\((.*)\)} $hdl -> dbname]} {
      error "failed to extract db name from $hdl
    }
    set ::handles($hdl) "hdl[incr ::handle_counter]"
    sqlite3 $::handles($hdl) /cont1/$dbname -vfs bcvfs
  }
  $::handles($hdl) eval {
    BEGIN; SELECT * FROM t1;
  }
}
proc close_reader_trans {hdl} {
  $::handles($hdl) eval { COMMIT }
}
proc close_reader {hdl} {
  $::handles($hdl) close
  unset ::handles($hdl)
}

foreach {tn script qres} {
  1 {
    open_reader r1(test.db)
    open_reader r2(test.db)
  } {
    test.db  2 2
    test.db2 0 0
    test.db3 0 0
    test.db4 0 0
  }

  2 {
    open_reader r3(test.db)
    open_reader r4(test.db)
    open_reader r5(test.db3)
  } {
    test.db  4 4
    test.db2 0 0
    test.db3 1 1
    test.db4 0 0
  }

  3 {
    close_reader_trans r3(test.db)
    close_reader r5(test.db3)
  } {
    test.db  4 3
    test.db2 0 0
    test.db3 0 0
    test.db4 0 0
  }

  4 {
    open_reader r5(test.db4)
    open_reader r6(test.db4)
    open_reader r7(test.db4)
    open_reader r8(test.db4)
    close_reader_trans r6(test.db4)
    close_reader_trans r7(test.db4)
  } {
    test.db  4 3
    test.db2 0 0
    test.db3 0 0
    test.db4 4 2
  }

  5 {
    close_reader r5(test.db4)
    after 100
  } {
    test.db  4 3
    test.db2 0 0
    test.db3 0 0
    test.db4 3 1
  }

} {
  do_test 2.1.$tn.1 {
    eval $::script
    set {} {}
  } {}

  do_execsql_test 2.1.$tn.2 {
    SELECT database, nclient, ntrans FROM bcv_database 
  } $qres
}

foreach h [array names ::handles] {
  $::handles($h) close
}

db close
$cmd destroy
wait_daemon 0

finish_test