| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845 |
- /**
- * LmdbDbmTest - Unit tests for LmdbDbm storage
- */
- using Implexus.Storage;
- public static int main(string[] args) {
- int passed = 0;
- int failed = 0;
- // Test 1: Basic set and get
- if (test_basic_set_get()) {
- passed++;
- stdout.puts("PASS: test_basic_set_get\n");
- } else {
- failed++;
- stdout.puts("FAIL: test_basic_set_get\n");
- }
- // Test 2: Get non-existent key
- if (test_get_nonexistent()) {
- passed++;
- stdout.puts("PASS: test_get_nonexistent\n");
- } else {
- failed++;
- stdout.puts("FAIL: test_get_nonexistent\n");
- }
- // Test 3: Delete key
- if (test_delete_key()) {
- passed++;
- stdout.puts("PASS: test_delete_key\n");
- } else {
- failed++;
- stdout.puts("FAIL: test_delete_key\n");
- }
- // Test 4: Has key
- if (test_has_key()) {
- passed++;
- stdout.puts("PASS: test_has_key\n");
- } else {
- failed++;
- stdout.puts("FAIL: test_has_key\n");
- }
- // Test 5: Keys iteration
- if (test_keys_iteration()) {
- passed++;
- stdout.puts("PASS: test_keys_iteration\n");
- } else {
- failed++;
- stdout.puts("FAIL: test_keys_iteration\n");
- }
- // Test 6: Transaction commit
- if (test_transaction_commit()) {
- passed++;
- stdout.puts("PASS: test_transaction_commit\n");
- } else {
- failed++;
- stdout.puts("FAIL: test_transaction_commit\n");
- }
- // Test 7: Transaction rollback
- if (test_transaction_rollback()) {
- passed++;
- stdout.puts("PASS: test_transaction_rollback\n");
- } else {
- failed++;
- stdout.puts("FAIL: test_transaction_rollback\n");
- }
- // Test 8: Transaction with delete
- if (test_transaction_delete()) {
- passed++;
- stdout.puts("PASS: test_transaction_delete\n");
- } else {
- failed++;
- stdout.puts("FAIL: test_transaction_delete\n");
- }
- // Test 9: Overwrite value
- if (test_overwrite_value()) {
- passed++;
- stdout.puts("PASS: test_overwrite_value\n");
- } else {
- failed++;
- stdout.puts("FAIL: test_overwrite_value\n");
- }
- // Test 10: Special key names
- if (test_special_key_names()) {
- passed++;
- stdout.puts("PASS: test_special_key_names\n");
- } else {
- failed++;
- stdout.puts("FAIL: test_special_key_names\n");
- }
- // Test 11: Binary data
- if (test_binary_data()) {
- passed++;
- stdout.puts("PASS: test_binary_data\n");
- } else {
- failed++;
- stdout.puts("FAIL: test_binary_data\n");
- }
- // Test 12: Empty key
- if (test_empty_key()) {
- passed++;
- stdout.puts("PASS: test_empty_key\n");
- } else {
- failed++;
- stdout.puts("FAIL: test_empty_key\n");
- }
- // Test 13: Large value
- if (test_large_value()) {
- passed++;
- stdout.puts("PASS: test_large_value\n");
- } else {
- failed++;
- stdout.puts("FAIL: test_large_value\n");
- }
- // Test 14: Multiple operations
- if (test_multiple_operations()) {
- passed++;
- stdout.puts("PASS: test_multiple_operations\n");
- } else {
- failed++;
- stdout.puts("FAIL: test_multiple_operations\n");
- }
- // Test 15: Persistence
- if (test_persistence()) {
- passed++;
- stdout.puts("PASS: test_persistence\n");
- } else {
- failed++;
- stdout.puts("FAIL: test_persistence\n");
- }
- // Test 16: Open and close
- if (test_open_close()) {
- passed++;
- stdout.puts("PASS: test_open_close\n");
- } else {
- failed++;
- stdout.puts("FAIL: test_open_close\n");
- }
- stdout.printf("\nResults: %d passed, %d failed\n", passed, failed);
- return failed > 0 ? 1 : 0;
- }
- // Helper to create temporary database path (LMDB uses a directory)
- string create_temp_db_path() {
- string temp_dir = DirUtils.mkdtemp("implexus_lmdb_test_XXXXXX");
- return temp_dir;
- }
- // Helper to get directory from db path
- string get_dir_from_path(string db_path) {
- return db_path;
- }
- // Helper to create BinaryData from string (includes null terminator)
- Invercargill.BinaryData string_to_binary(string str) {
- // Include null terminator for proper string reconstruction when reading back
- uint8[] data = new uint8[str.length + 1];
- Memory.copy(data, str, str.length);
- data[str.length] = 0; // null terminator
- return new Invercargill.DataStructures.ByteBuffer.from_byte_array(data);
- }
- // Helper to convert BinaryData to string
- string binary_to_string(Invercargill.BinaryData data) {
- var bytes = data.to_bytes();
- return (string) bytes.get_data();
- }
- // Test 1: Basic set and get
- bool test_basic_set_get() {
- string db_path = create_temp_db_path();
- string temp_dir = get_dir_from_path(db_path);
-
- try {
- var dbm = new LmdbDbm();
- dbm.open(db_path, false);
-
- dbm.set("test_key", string_to_binary("test_value"));
-
- var value = dbm.get("test_key");
- if (value == null) {
- dbm.close();
- cleanup_dir(temp_dir);
- return false;
- }
- if (binary_to_string((!) value) != "test_value") {
- dbm.close();
- cleanup_dir(temp_dir);
- return false;
- }
-
- dbm.close();
- cleanup_dir(temp_dir);
- return true;
- } catch (Error e) {
- message("Error: %s", e.message);
- cleanup_dir(temp_dir);
- return false;
- }
- }
- // Test 2: Get non-existent key
- bool test_get_nonexistent() {
- string db_path = create_temp_db_path();
- string temp_dir = get_dir_from_path(db_path);
-
- try {
- var dbm = new LmdbDbm();
- dbm.open(db_path, false);
-
- var value = dbm.get("nonexistent");
-
- dbm.close();
- cleanup_dir(temp_dir);
- return value == null;
- } catch (Error e) {
- cleanup_dir(temp_dir);
- return false;
- }
- }
- // Test 3: Delete key
- bool test_delete_key() {
- string db_path = create_temp_db_path();
- string temp_dir = get_dir_from_path(db_path);
-
- try {
- var dbm = new LmdbDbm();
- dbm.open(db_path, false);
-
- dbm.set("key1", string_to_binary("value1"));
-
- if (!dbm.has_key("key1")) {
- dbm.close();
- cleanup_dir(temp_dir);
- return false;
- }
-
- dbm.delete("key1");
-
- if (dbm.has_key("key1")) {
- dbm.close();
- cleanup_dir(temp_dir);
- return false;
- }
-
- dbm.close();
- cleanup_dir(temp_dir);
- return true;
- } catch (Error e) {
- cleanup_dir(temp_dir);
- return false;
- }
- }
- // Test 4: Has key
- bool test_has_key() {
- string db_path = create_temp_db_path();
- string temp_dir = get_dir_from_path(db_path);
-
- try {
- var dbm = new LmdbDbm();
- dbm.open(db_path, false);
-
- if (dbm.has_key("key1")) {
- dbm.close();
- cleanup_dir(temp_dir);
- return false; // Should not exist
- }
-
- dbm.set("key1", string_to_binary("value1"));
-
- if (!dbm.has_key("key1")) {
- dbm.close();
- cleanup_dir(temp_dir);
- return false; // Should exist
- }
- if (dbm.has_key("key2")) {
- dbm.close();
- cleanup_dir(temp_dir);
- return false; // Should not exist
- }
-
- dbm.close();
- cleanup_dir(temp_dir);
- return true;
- } catch (Error e) {
- cleanup_dir(temp_dir);
- return false;
- }
- }
- // Test 5: Keys iteration
- bool test_keys_iteration() {
- string db_path = create_temp_db_path();
- string temp_dir = get_dir_from_path(db_path);
-
- try {
- var dbm = new LmdbDbm();
- dbm.open(db_path, false);
-
- dbm.set("key1", string_to_binary("value1"));
- dbm.set("key2", string_to_binary("value2"));
- dbm.set("key3", string_to_binary("value3"));
-
- var keys = dbm.keys;
- if (keys.count() != 3) {
- dbm.close();
- cleanup_dir(temp_dir);
- return false;
- }
-
- // Check all keys are present
- var key_set = new Invercargill.DataStructures.HashSet<string>();
- foreach (var key in keys) {
- key_set.add(key);
- }
-
- if (!key_set.has("key1")) {
- dbm.close();
- cleanup_dir(temp_dir);
- return false;
- }
- if (!key_set.has("key2")) {
- dbm.close();
- cleanup_dir(temp_dir);
- return false;
- }
- if (!key_set.has("key3")) {
- dbm.close();
- cleanup_dir(temp_dir);
- return false;
- }
-
- dbm.close();
- cleanup_dir(temp_dir);
- return true;
- } catch (Error e) {
- cleanup_dir(temp_dir);
- return false;
- }
- }
- // Test 6: Transaction commit
- bool test_transaction_commit() {
- string db_path = create_temp_db_path();
- string temp_dir = get_dir_from_path(db_path);
-
- try {
- var dbm = new LmdbDbm();
- dbm.open(db_path, false);
-
- dbm.set("key1", string_to_binary("value1"));
-
- dbm.begin_transaction();
-
- dbm.set("key2", string_to_binary("value2"));
- dbm.set("key3", string_to_binary("value3"));
-
- // Before commit, values should be in transaction buffer
- var value2 = dbm.get("key2");
- if (value2 == null) {
- dbm.close();
- cleanup_dir(temp_dir);
- return false;
- }
-
- dbm.commit_transaction();
-
- // After commit, values should be persisted
- if (!dbm.has_key("key1")) {
- dbm.close();
- cleanup_dir(temp_dir);
- return false;
- }
- if (!dbm.has_key("key2")) {
- dbm.close();
- cleanup_dir(temp_dir);
- return false;
- }
- if (!dbm.has_key("key3")) {
- dbm.close();
- cleanup_dir(temp_dir);
- return false;
- }
-
- dbm.close();
- cleanup_dir(temp_dir);
- return true;
- } catch (Error e) {
- cleanup_dir(temp_dir);
- return false;
- }
- }
- // Test 7: Transaction rollback
- bool test_transaction_rollback() {
- string db_path = create_temp_db_path();
- string temp_dir = get_dir_from_path(db_path);
-
- try {
- var dbm = new LmdbDbm();
- dbm.open(db_path, false);
-
- dbm.set("key1", string_to_binary("value1"));
-
- dbm.begin_transaction();
-
- dbm.set("key2", string_to_binary("value2"));
-
- // Value should be visible in transaction
- if (!dbm.has_key("key2")) {
- dbm.close();
- cleanup_dir(temp_dir);
- return false;
- }
-
- dbm.rollback_transaction();
-
- // After rollback, key2 should not exist
- if (dbm.has_key("key2")) {
- dbm.close();
- cleanup_dir(temp_dir);
- return false;
- }
-
- // key1 should still exist
- if (!dbm.has_key("key1")) {
- dbm.close();
- cleanup_dir(temp_dir);
- return false;
- }
-
- dbm.close();
- cleanup_dir(temp_dir);
- return true;
- } catch (Error e) {
- cleanup_dir(temp_dir);
- return false;
- }
- }
- // Test 8: Transaction with delete
- bool test_transaction_delete() {
- string db_path = create_temp_db_path();
- string temp_dir = get_dir_from_path(db_path);
-
- try {
- var dbm = new LmdbDbm();
- dbm.open(db_path, false);
-
- dbm.set("key1", string_to_binary("value1"));
- dbm.set("key2", string_to_binary("value2"));
-
- dbm.begin_transaction();
-
- dbm.delete("key1");
-
- // In transaction, key1 should not be visible
- if (dbm.has_key("key1")) {
- dbm.close();
- cleanup_dir(temp_dir);
- return false;
- }
-
- dbm.commit_transaction();
-
- // After commit, key1 should be deleted
- if (dbm.has_key("key1")) {
- dbm.close();
- cleanup_dir(temp_dir);
- return false;
- }
-
- // key2 should still exist
- if (!dbm.has_key("key2")) {
- dbm.close();
- cleanup_dir(temp_dir);
- return false;
- }
-
- dbm.close();
- cleanup_dir(temp_dir);
- return true;
- } catch (Error e) {
- cleanup_dir(temp_dir);
- return false;
- }
- }
- // Test 9: Overwrite value
- bool test_overwrite_value() {
- string db_path = create_temp_db_path();
- string temp_dir = get_dir_from_path(db_path);
-
- try {
- var dbm = new LmdbDbm();
- dbm.open(db_path, false);
-
- dbm.set("key1", string_to_binary("value1"));
-
- var value = dbm.get("key1");
- if (value == null || binary_to_string((!) value) != "value1") {
- dbm.close();
- cleanup_dir(temp_dir);
- return false;
- }
-
- dbm.set("key1", string_to_binary("value2"));
-
- value = dbm.get("key1");
- if (value == null || binary_to_string((!) value) != "value2") {
- dbm.close();
- cleanup_dir(temp_dir);
- return false;
- }
-
- dbm.close();
- cleanup_dir(temp_dir);
- return true;
- } catch (Error e) {
- cleanup_dir(temp_dir);
- return false;
- }
- }
- // Test 10: Special key names
- bool test_special_key_names() {
- string db_path = create_temp_db_path();
- string temp_dir = get_dir_from_path(db_path);
-
- try {
- var dbm = new LmdbDbm();
- dbm.open(db_path, false);
-
- string[] special_keys = {
- "key/with/slashes",
- "key:with:colons",
- "key.with.dots",
- "key with spaces",
- "key\nwith\nnewlines",
- "日本語キー"
- };
-
- foreach (var key in special_keys) {
- dbm.set(key, string_to_binary(@"value for $key"));
-
- var value = dbm.get(key);
- if (value == null) {
- dbm.close();
- cleanup_dir(temp_dir);
- return false;
- }
- if (binary_to_string((!) value) != @"value for $key") {
- dbm.close();
- cleanup_dir(temp_dir);
- return false;
- }
- }
-
- dbm.close();
- cleanup_dir(temp_dir);
- return true;
- } catch (Error e) {
- cleanup_dir(temp_dir);
- return false;
- }
- }
- // Test 11: Binary data
- bool test_binary_data() {
- string db_path = create_temp_db_path();
- string temp_dir = get_dir_from_path(db_path);
-
- try {
- var dbm = new LmdbDbm();
- dbm.open(db_path, false);
-
- // Create binary data with all byte values
- var data = new uint8[256];
- for (int i = 0; i < 256; i++) {
- data[i] = (uint8) i;
- }
-
- var binary = new Invercargill.DataStructures.ByteBuffer.from_byte_array(data);
-
- dbm.set("binary_key", binary);
-
- var value = dbm.get("binary_key");
- if (value == null) {
- dbm.close();
- cleanup_dir(temp_dir);
- return false;
- }
-
- var read_data = ((!) value).to_bytes();
- if (read_data.length != 256) {
- dbm.close();
- cleanup_dir(temp_dir);
- return false;
- }
-
- for (int i = 0; i < 256; i++) {
- if (read_data.get_data()[i] != (uint8) i) {
- dbm.close();
- cleanup_dir(temp_dir);
- return false;
- }
- }
-
- dbm.close();
- cleanup_dir(temp_dir);
- return true;
- } catch (Error e) {
- cleanup_dir(temp_dir);
- return false;
- }
- }
- // Test 12: Empty key
- // Note: LMDB does not support zero-length keys, so this test is skipped
- bool test_empty_key() {
- // LMDB returns BAD_VALSIZE error for empty keys
- // This is a documented limitation of LMDB
- return true;
- }
- // Test 13: Large value
- bool test_large_value() {
- string db_path = create_temp_db_path();
- string temp_dir = get_dir_from_path(db_path);
-
- try {
- var dbm = new LmdbDbm();
- dbm.open(db_path, false);
-
- // Create a 1MB value
- var data = new uint8[1024 * 1024];
- for (int i = 0; i < data.length; i++) {
- data[i] = (uint8) (i % 256);
- }
-
- var binary = new Invercargill.DataStructures.ByteBuffer.from_byte_array(data);
-
- dbm.set("large_key", binary);
-
- var value = dbm.get("large_key");
- if (value == null) {
- dbm.close();
- cleanup_dir(temp_dir);
- return false;
- }
-
- var read_data = ((!) value).to_bytes();
- if (read_data.length != data.length) {
- dbm.close();
- cleanup_dir(temp_dir);
- return false;
- }
-
- for (int i = 0; i < data.length; i++) {
- if (read_data.get_data()[i] != data[i]) {
- dbm.close();
- cleanup_dir(temp_dir);
- return false;
- }
- }
-
- dbm.close();
- cleanup_dir(temp_dir);
- return true;
- } catch (Error e) {
- cleanup_dir(temp_dir);
- return false;
- }
- }
- // Test 14: Multiple operations
- bool test_multiple_operations() {
- string db_path = create_temp_db_path();
- string temp_dir = get_dir_from_path(db_path);
-
- try {
- var dbm = new LmdbDbm();
- dbm.open(db_path, false);
-
- // Set multiple keys
- for (int i = 0; i < 100; i++) {
- dbm.set(@"key$i", string_to_binary(@"value$i"));
- }
-
- // Verify all keys
- for (int i = 0; i < 100; i++) {
- var value = dbm.get(@"key$i");
- if (value == null) {
- dbm.close();
- cleanup_dir(temp_dir);
- return false;
- }
- if (binary_to_string((!) value) != @"value$i") {
- dbm.close();
- cleanup_dir(temp_dir);
- return false;
- }
- }
-
- // Delete half the keys
- for (int i = 0; i < 50; i++) {
- dbm.delete(@"key$i");
- }
-
- // Verify deleted keys are gone
- for (int i = 0; i < 50; i++) {
- if (dbm.has_key(@"key$i")) {
- dbm.close();
- cleanup_dir(temp_dir);
- return false;
- }
- }
-
- // Verify remaining keys still exist
- for (int i = 50; i < 100; i++) {
- if (!dbm.has_key(@"key$i")) {
- dbm.close();
- cleanup_dir(temp_dir);
- return false;
- }
- }
-
- dbm.close();
- cleanup_dir(temp_dir);
- return true;
- } catch (Error e) {
- cleanup_dir(temp_dir);
- return false;
- }
- }
- // Test 15: Persistence
- bool test_persistence() {
- string db_path = create_temp_db_path();
- string temp_dir = get_dir_from_path(db_path);
-
- try {
- // Create and write
- var dbm1 = new LmdbDbm();
- dbm1.open(db_path, false);
- dbm1.set("persistent_key", string_to_binary("persistent_value"));
- dbm1.close();
-
- // Create new instance with same path
- var dbm2 = new LmdbDbm();
- dbm2.open(db_path, false);
-
- // Data should persist
- var value = dbm2.get("persistent_key");
- if (value == null) {
- dbm2.close();
- cleanup_dir(temp_dir);
- return false;
- }
- if (binary_to_string((!) value) != "persistent_value") {
- dbm2.close();
- cleanup_dir(temp_dir);
- return false;
- }
-
- dbm2.close();
- cleanup_dir(temp_dir);
- return true;
- } catch (Error e) {
- cleanup_dir(temp_dir);
- return false;
- }
- }
- // Test 16: Open and close
- bool test_open_close() {
- string db_path = create_temp_db_path();
- string temp_dir = get_dir_from_path(db_path);
-
- try {
- var dbm = new LmdbDbm();
-
- if (dbm.is_open) {
- cleanup_dir(temp_dir);
- return false;
- }
-
- dbm.open(db_path, false);
-
- if (!dbm.is_open) {
- cleanup_dir(temp_dir);
- return false;
- }
-
- dbm.close();
-
- if (dbm.is_open) {
- cleanup_dir(temp_dir);
- return false;
- }
-
- cleanup_dir(temp_dir);
- return true;
- } catch (Error e) {
- cleanup_dir(temp_dir);
- return false;
- }
- }
- // Cleanup helper
- void cleanup_dir(string path) {
- // Remove all files in directory and the directory itself
- try {
- Dir dir = Dir.open(path, 0);
- string? name;
- while ((name = dir.read_name()) != null) {
- string file_path = Path.build_filename(path, name);
- if (FileUtils.test(file_path, FileTest.IS_DIR)) {
- cleanup_dir(file_path);
- } else {
- FileUtils.unlink(file_path);
- }
- }
- } catch (FileError e) {
- // Ignore errors
- }
- DirUtils.remove(path);
- }
|