From bfcb1761fdb0caf87c23117d369dea2cac6a57a8 Mon Sep 17 00:00:00 2001 From: Greg Burd Date: Mon, 3 Aug 2026 11:35:26 -0400 Subject: [PATCH] chore: remove remaining C# remnants MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Follow-up to the C# binding removal — clean up what was missed: - test/csharp/ (54 NUnit C# test files) removed. The one non-C# item there, the old-format upgrade fixture bdb4.7.db, was collateral; relocated to test/db/fixtures/ and run_upgrade.sh repointed (it's a 4.7 format DB, nothing to do with C#). - docs_src: drop the C# pages (build_win_csharp.md, programmer_reference/ csharp.md), their _meta.toml nav entries, and the dangling links to them in the install + programmer-reference indexes (STL renumbered to chapter 6). Historical C# mentions in the changelog pages are left (accurate history). - lang/java/libdb_java/db.i: drop the dead #elif SWIGCSHARP branch (db_csharp.i no longer exists; the guard was never defined post-removal). Dormant Oracle-era dist packaging (winmsi/win_projects/buildpkg) still names C# but is out of the make/ninja/nix flow; left untouched. docs build clean, no dangling C# links. --- docs_src/guides/installation/_meta.toml | 1 - docs_src/guides/installation/build_win.md | 2 - .../guides/installation/build_win_csharp.md | 28 - docs_src/guides/installation/index.md | 2 - .../guides/programmer_reference/_meta.toml | 1 - .../guides/programmer_reference/csharp.md | 34 - docs_src/guides/programmer_reference/index.md | 6 +- lang/java/libdb_java/db.i | 2 - test/csharp/AllTestData.xml | 851 ----- test/csharp/BTreeCursorTest.cs | 1258 -------- test/csharp/BTreeDatabaseConfigTest.cs | 87 - test/csharp/BTreeDatabaseTest.cs | 2811 ----------------- test/csharp/CSharpTestFixture.cs | 47 - test/csharp/Configuration.cs | 1119 ------- test/csharp/CursorConfigTest.cs | 79 - test/csharp/CursorTest.cs | 1812 ----------- test/csharp/DatabaseConfigTest.cs | 115 - test/csharp/DatabaseEnvironmentConfigTest.cs | 297 -- test/csharp/DatabaseEnvironmentTest.cs | 2248 ------------- test/csharp/DatabaseExceptionTest.cs | 265 -- test/csharp/DatabaseTest.cs | 109 - test/csharp/DotNetTest.csproj | 120 - test/csharp/ForeignKeyTest.cs | 263 -- test/csharp/HashCursorTest.cs | 239 -- test/csharp/HashDatabaseConfigTest.cs | 80 - test/csharp/HashDatabaseTest.cs | 509 --- test/csharp/HeapDatabaseConfigTest.cs | 57 - test/csharp/HeapDatabaseTest.cs | 461 --- test/csharp/JoinCursorTest.cs | 199 -- test/csharp/LockTest.cs | 252 -- test/csharp/LockingConfigTest.cs | 154 - test/csharp/LogConfigTest.cs | 292 -- test/csharp/LogCursorTest.cs | 315 -- test/csharp/MPoolConfigTest.cs | 83 - test/csharp/MutexConfigTest.cs | 78 - test/csharp/MutexTest.cs | 119 - test/csharp/QueueDatabaseConfigTest.cs | 84 - test/csharp/QueueDatabaseTest.cs | 771 ----- test/csharp/RecnoCursorTest.cs | 241 -- test/csharp/RecnoDatabaseConfigTest.cs | 124 - test/csharp/RecnoDatabaseTest.cs | 466 --- test/csharp/ReplicationConfigTest.cs | 188 -- test/csharp/ReplicationTest.cs | 996 ------ .../SecondaryBTreeDatabaseConfigTest.cs | 108 - test/csharp/SecondaryBTreeDatabaseTest.cs | 295 -- test/csharp/SecondaryCursorTest.cs | 1534 --------- test/csharp/SecondaryDatabaseConfigTest.cs | 74 - test/csharp/SecondaryDatabaseTest.cs | 792 ----- .../csharp/SecondaryHashDatabaseConfigTest.cs | 102 - test/csharp/SecondaryHashDatabaseTest.cs | 382 --- .../SecondaryQueueDatabaseConfigTest.cs | 102 - test/csharp/SecondaryQueueDatabaseTest.cs | 215 -- .../SecondaryRecnoDatabaseConfigTest.cs | 112 - test/csharp/SecondaryRecnoDatabaseTest.cs | 255 -- test/csharp/SequenceConfigTest.cs | 123 - test/csharp/SequenceTest.cs | 378 --- test/csharp/TestException.cs | 49 - test/csharp/TransactionCommitTokenTest.cs | 261 -- test/csharp/TransactionConfigTest.cs | 73 - test/csharp/TransactionTest.cs | 429 --- test/csharp/XMLReader.cs | 48 - test/{csharp => db/fixtures}/bdb4.7.db | Bin test/db/run_upgrade.sh | 4 +- 63 files changed, 3 insertions(+), 22598 deletions(-) delete mode 100644 docs_src/guides/installation/build_win_csharp.md delete mode 100644 docs_src/guides/programmer_reference/csharp.md delete mode 100644 test/csharp/AllTestData.xml delete mode 100644 test/csharp/BTreeCursorTest.cs delete mode 100644 test/csharp/BTreeDatabaseConfigTest.cs delete mode 100644 test/csharp/BTreeDatabaseTest.cs delete mode 100644 test/csharp/CSharpTestFixture.cs delete mode 100644 test/csharp/Configuration.cs delete mode 100644 test/csharp/CursorConfigTest.cs delete mode 100644 test/csharp/CursorTest.cs delete mode 100644 test/csharp/DatabaseConfigTest.cs delete mode 100644 test/csharp/DatabaseEnvironmentConfigTest.cs delete mode 100644 test/csharp/DatabaseEnvironmentTest.cs delete mode 100644 test/csharp/DatabaseExceptionTest.cs delete mode 100644 test/csharp/DatabaseTest.cs delete mode 100644 test/csharp/DotNetTest.csproj delete mode 100644 test/csharp/ForeignKeyTest.cs delete mode 100644 test/csharp/HashCursorTest.cs delete mode 100644 test/csharp/HashDatabaseConfigTest.cs delete mode 100644 test/csharp/HashDatabaseTest.cs delete mode 100644 test/csharp/HeapDatabaseConfigTest.cs delete mode 100644 test/csharp/HeapDatabaseTest.cs delete mode 100644 test/csharp/JoinCursorTest.cs delete mode 100644 test/csharp/LockTest.cs delete mode 100644 test/csharp/LockingConfigTest.cs delete mode 100644 test/csharp/LogConfigTest.cs delete mode 100644 test/csharp/LogCursorTest.cs delete mode 100644 test/csharp/MPoolConfigTest.cs delete mode 100644 test/csharp/MutexConfigTest.cs delete mode 100644 test/csharp/MutexTest.cs delete mode 100644 test/csharp/QueueDatabaseConfigTest.cs delete mode 100644 test/csharp/QueueDatabaseTest.cs delete mode 100644 test/csharp/RecnoCursorTest.cs delete mode 100644 test/csharp/RecnoDatabaseConfigTest.cs delete mode 100644 test/csharp/RecnoDatabaseTest.cs delete mode 100644 test/csharp/ReplicationConfigTest.cs delete mode 100644 test/csharp/ReplicationTest.cs delete mode 100644 test/csharp/SecondaryBTreeDatabaseConfigTest.cs delete mode 100644 test/csharp/SecondaryBTreeDatabaseTest.cs delete mode 100644 test/csharp/SecondaryCursorTest.cs delete mode 100644 test/csharp/SecondaryDatabaseConfigTest.cs delete mode 100644 test/csharp/SecondaryDatabaseTest.cs delete mode 100644 test/csharp/SecondaryHashDatabaseConfigTest.cs delete mode 100644 test/csharp/SecondaryHashDatabaseTest.cs delete mode 100644 test/csharp/SecondaryQueueDatabaseConfigTest.cs delete mode 100644 test/csharp/SecondaryQueueDatabaseTest.cs delete mode 100644 test/csharp/SecondaryRecnoDatabaseConfigTest.cs delete mode 100644 test/csharp/SecondaryRecnoDatabaseTest.cs delete mode 100644 test/csharp/SequenceConfigTest.cs delete mode 100644 test/csharp/SequenceTest.cs delete mode 100644 test/csharp/TestException.cs delete mode 100644 test/csharp/TransactionCommitTokenTest.cs delete mode 100644 test/csharp/TransactionConfigTest.cs delete mode 100644 test/csharp/TransactionTest.cs delete mode 100644 test/csharp/XMLReader.cs rename test/{csharp => db/fixtures}/bdb4.7.db (100%) diff --git a/docs_src/guides/installation/_meta.toml b/docs_src/guides/installation/_meta.toml index 5be6e7008..be74d309a 100644 --- a/docs_src/guides/installation/_meta.toml +++ b/docs_src/guides/installation/_meta.toml @@ -24,7 +24,6 @@ order = [ "win_build_cxx", "win_build_stl", "build_win_java", - "build_win_csharp", "build_win_sql", "build_win_tcl", "win_build_dist_dll", diff --git a/docs_src/guides/installation/build_win.md b/docs_src/guides/installation/build_win.md index 0d4a68540..3ad4bb8c1 100644 --- a/docs_src/guides/installation/build_win.md +++ b/docs_src/guides/installation/build_win.md @@ -29,8 +29,6 @@ source: docs/installation/build_win.html [Building the Java API](build_win_java.md) - [Building the C# API](build_win_csharp.md) - [Building the SQL API](build_win_sql.md) [Binary Compatibility With SQLite](build_win_sql.md#idp290248) diff --git a/docs_src/guides/installation/build_win_csharp.md b/docs_src/guides/installation/build_win_csharp.md deleted file mode 100644 index 9b8c8eb98..000000000 --- a/docs_src/guides/installation/build_win_csharp.md +++ /dev/null @@ -1,28 +0,0 @@ ---- -title: "Building the C# API" -api-name: "Building the C# API" -source: docs/installation/build_win_csharp.html ---- -## Building the C# API - -The C# support is built by a separate Visual Studio solution and requires version 2.0 (or higher) of the .NET platform. If the Berkeley DB utilities are required, build Berkeley DB first following the instructions under Building Berkeley DB for 32 bit Windows or Building Berkeley DB for 64-bit Windows. - -To build the C# API in Visual Studio 2005/Visual Studio 2008, the solution is `build_windows\BDB_dotnet.sln`; in Visual Studio 2010, the solution is `build_windows\BDB_dotnet_vs2010.sln`. - -By default, the solution will build the native libraries, the managed assembly and all example programs. The NUnit tests need to be built explicitly because of their dependence upon the NUnit assembly. The native libraries will be placed in one of the following subdirectories, depending upon the chosen configuration: - -| | -|-------------------------------| -| `build_windows\Win32\Debug` | -| `build_windows\Win32\Release` | -| `build_windows\x64\Debug` | -| `build_windows\x64\Release` | - -The managed assembly and all C# example programs will be placed in one of the following subdirectories, depending upon the chosen configuration: - -| | -|--------------------------------| -| `build_windows\AnyCPU\Debug` | -| `build_windows\AnyCPU\Release` | - -The native libraries need to be locatable by the .NET platform, meaning they must be copied into an application's directory, the Windows or System directory, or their location must be added to the PATH environment variable. The example programs demonstrate how to programmatically edit the PATH variable. diff --git a/docs_src/guides/installation/index.md b/docs_src/guides/installation/index.md index 901bf0792..f029b4ba3 100644 --- a/docs_src/guides/installation/index.md +++ b/docs_src/guides/installation/index.md @@ -95,8 +95,6 @@ source: docs/installation/index.html [Building the Java API](build_win_java.md) - [Building the C# API](build_win_csharp.md) - [Building the SQL API](build_win_sql.md) [Binary Compatibility With SQLite](build_win_sql.md#idp290248) diff --git a/docs_src/guides/programmer_reference/_meta.toml b/docs_src/guides/programmer_reference/_meta.toml index 622f16342..6999be722 100644 --- a/docs_src/guides/programmer_reference/_meta.toml +++ b/docs_src/guides/programmer_reference/_meta.toml @@ -55,7 +55,6 @@ order = [ "java_compat", "java_program", "java_faq", - "csharp", "stl", "stl_usecase", "stl_examples", diff --git a/docs_src/guides/programmer_reference/csharp.md b/docs_src/guides/programmer_reference/csharp.md deleted file mode 100644 index 495d81d9f..000000000 --- a/docs_src/guides/programmer_reference/csharp.md +++ /dev/null @@ -1,34 +0,0 @@ ---- -title: "Chapter 6. C# API" -api-name: "Chapter 6. C# API" -source: docs/programmer_reference/csharp.html ---- -## Chapter 6. C# API - -**Table of Contents** - - [Compatibility](csharp.md#csharp_compat) - -You can use Berkeley DB in your application through the C# API. To understand the application concepts relating to Berkeley DB, see the first few chapters of this manual. For a general discussion on how to build Berkeley DB applications, see the Berkeley DB Getting Started Guides of C or C++. You can also review the example code of C and C++ from the examples_c and examples_cxx directories. For a description of all the classes, functions, and enumerations of Berkeley DB C# API, see the Berkeley DB C# API Reference Guide. - -A separate Visual Studio solution is provided to build the Berkeley DB C# classes, the examples, and the native support library. See Building the C# API in the Berkeley DB Installation and Build Guide for more information. - -The C# API requires .NET framework version 2.0 or above, and expects that it has already been installed on your system. For the sake of discussion, we assume that the Berkeley DB source is in a directory called db-*VERSION*; for example, you downloaded a Berkeley DB archive, and you did not change the top-level directory name. The files related to C# are in four subdirectories of db-*VERSION*: csharp (the C# source files), libdb_csharp (the C++ files that provide the "glue" between C# and Berkeley DB,) examples_csharp (containing all example code) and test\scr037 (containing NUnit tests for the API). - -Building the C# API produces a managed assembly `libdb_dotnet`*`VERSION`*`.dll`, containing the API, and two native libraries: `libdb_csharp`*`VERSION`*`.dll` and `libdb`*`VERSION`*`.dll`. (For all three files, *VERSION* is \[MAJOR\]\[MINOR\], i.e. for version 4.8 the managed assembly is `libdb_dotnet48.dll`.) Following the existing convention, native libraries are placed in either `db-`*`VERSION`*`\build_windows\Win32`or `db-`*`VERSION`*`\build_windows\x64`, depending upon the platform being targeted. In all cases, the managed assembly will be placed in `db-`*`VERSION`*`\build_windows\AnyCPU`. - -Because the C# API uses P/Invoke, for your application to use Berkeley DB successfully, the .NET framework needs to be able to locate the native libaries. This means the native libraries need to either be copied to your application's directory, the Windows or System directory, or the location of the libraries needs to be added to the `PATH` environment variable. See the MSDN documentation of the DllImport attribute and Dynamic-Link Library Search Order for further information. - -If you get the following exception when you run, the .NET platform probably is unable to locate the native libraries: - -``` c -System.TypeInitializationException -``` - -To ensure that everything is running correctly, you may want to try a simple test from the example programs in the `db-`*`VERSION`*`\examples_csharp` directory. - -For example, the ex_access sample program will prompt for text input lines, which are then stored in a Btree database named `access.db`. It is designed to be run from either the `db-`*`VERSION`*`\build_windows\Debug` or `db-`*`VERSION`*`\build_windows\Release` directory. Try giving it a few lines of input text and then a blank line. Before it exits, you should see a list of the lines you entered display with data items. This is a simple check to make sure the fundamental configuration is working correctly. - -## Compatibility - -The Berkeley DB C# API has been tested with the Microsoft .NET Framework versions 2.0, 3.0, 3.5, and 4.0. diff --git a/docs_src/guides/programmer_reference/index.md b/docs_src/guides/programmer_reference/index.md index dbed63c48..cb10f679e 100644 --- a/docs_src/guides/programmer_reference/index.md +++ b/docs_src/guides/programmer_reference/index.md @@ -255,11 +255,7 @@ To obtain a copy of this document's original source code, please submit a reques [Java FAQ](java_faq.md) - [6. C# API](csharp.md) - - [Compatibility](csharp.md#csharp_compat) - - [7. Standard Template Library API](stl.md) + [6. Standard Template Library API](stl.md) [Dbstl introduction](stl.md#stl_intro) diff --git a/lang/java/libdb_java/db.i b/lang/java/libdb_java/db.i index 0199a167c..5c571efae 100644 --- a/lang/java/libdb_java/db.i +++ b/lang/java/libdb_java/db.i @@ -12,8 +12,6 @@ #if defined(SWIGJAVA) %include "db_java.i" -#elif defined(SWIGCSHARP) -%include "db_csharp.i" #endif typedef unsigned char u_int8_t; diff --git a/test/csharp/AllTestData.xml b/test/csharp/AllTestData.xml deleted file mode 100644 index f372a8f67..000000000 --- a/test/csharp/AllTestData.xml +++ /dev/null @@ -1,851 +0,0 @@ - - - - - True - 1234 - - 1048576 - 0 - 1 - - True - TestDatabaseConfig - True - True - True - 8196 - HIGH - True - True - - 123 - AES - - True - True - True - - - - True - .//test_DB - .//test_DB - TestDatabaseConfigWithEnv - - - - - - False - 1234 - - 1048576 - 0 - 1 - - ALWAYS - False - UNSORTED - - 123 - AES - - TestDatabaseConfig - True - 64 - False - False - True - 8192 - HIGH - False - False - False - True - False - False - - - - - False - 1234 - - 1048576 - 0 - 1 - - ALWAYS - False - UNSORTED - TestDatabaseConfig - True - True - 64 - False - False - True - 8192 - HIGH - False - False - - 123 - AES - - False - True - False - False - - - - - False - 1234 - - 1048576 - 0 - 1 - - ALWAYS - False - UNSORTED - TestConfigWithoutEnv - 7 - False - True - True - 100 - 8192 - HIGH - False - False - - 123 - AES - - False - True - False - - - - - False - 1234 - - 1048576 - 0 - 1 - - ALWAYS - False - UNSORTED - TestDatabaseConfig - 2 - True - False - False - False - 10 - 8192 - HIGH - False - False - - 123 - AES - - False - False - - - - - False - 1234 - - 1048576 - 0 - 1 - - ALWAYS - False - UNSORTED - TestConfigWithoutEnv - False - True - True - 8192 - HIGH - False - False - - 123 - AES - - False - True - False - - - - - False - 1234 - - 1048576 - 0 - 1 - - ALWAYS - False - UNSORTED - TestDatabaseConfig - True - False - False - False - 8192 - HIGH - False - False - - 123 - AES - - False - False - - - - - False - ../backing.log - 1234 - - 1048576 - 0 - 1 - - True - IF_NEEDED - 10 - True - UNSORTED - TestDatabaseConfig - 2 - True - 100 - 10 - False - True - 8192 - HIGH - True - True - True - - 123 - AES - - True - True - True - True - - - - - False - 1234 - - 1048576 - 0 - 1 - - True - ALWAYS - False - UNSORTED - TestDatabaseConfig - 2 - True - 20 - 10 - False - False - 40960 - HIGH - False - False - - 123 - AES - - False - True - False - - - - - False - 1234 - - 1048576 - 0 - 1 - - True - ALWAYS - False - UNSORTED - TestDatabaseConfig - 2 - True - False - 100 - 10 - False - False - 8192 - HIGH - False - False - - 123 - AES - - False - True - False - - - - - False - 1234 - - 1048576 - 0 - 1 - - ALWAYS - 100 - False - UNSORTED - TestDatabaseConfig - True - True - 100 - False - False - True - 100 - 8192 - HIGH - False - False - False - - 123 - AES - - False - False - True - False - False - - - - - - - 1 - 1 - 1 - - - 1 - 1 - 1 - - - 1 - 1 - 1 - - - MAX_LOCKS - 10 - 10 - 10 - 3 - - - - - True - 4096 - ./ - 755 - True - True - 1048576 - True - 30720 - True - - - - - 256 - 10 - 50 - 5 - - - - - 2 - True - True - WRITE_NOSYNC - - - - - - 1048576 - 0 - 1 - - - 10485760 - 0 - 1 - - 10 - - 10 - 1000 - - 1048576 - - - - - True - True - - - - - NEVER - SORTED - True - True - 100 - True - True - - - - - False - True - ALWAYS - SORTED - True - False - 64 - - - - - True - True - NEVER - SORTED - 5 - 100 - - - - - True - True - ALWAYS - SORTED - 5 - 100 - - - - - True - True - NEVER - 100 - 10 - 4 - - - - - True - True - ALWAYS - 100 - 10 - 4 - - - - - True - True - .//backing - NEVER - 100 - 100 - 100 - False - False - - - - - True - True - ALWAYS - 100 - 120 - 50 - False - False - - - - - 1048576 - ALWAYS - True - True - True - 10 - 1000 - 17 - True - - - - - 1048576 - ALWAYS - True - True - False - 10 - True - - - - - True - True - True - ./ - - ./ - ./TestDbEnv - - TestConfig - True - True - 10 - True - rwx------ - True - 1000 - True - True - True - True - True - True - True - True - True - True - .//test_DB - True - True - True - True - 2008-12-09 - True - True - True - True - True - True - True - True - - True - True - True - True - True - True - True - True - True - True - True - True - True - True - - True - - - - - 10 - 20 - - - 30 - 40 - - - MAX_LOCKS - 50 - 60 - 70 - 3 - - - True - 10240 - ./ - 755 - True - True - 1048576 - True - 20480 - True - - - 512 - 128 - 15 - 10 - - - 2000 - True - 100 - 100 - True - 15 - 1050 - 5000 - 100 - 50 - 1025 - False - True - 1 - ALL - - 127.0.0.0 - 11111 - - True - True - - - - - True - False - True - ./ - - ./ - ./TestDbEnv - - TestConfigAll - False - True - 10 - True - rwx------ - False - 1000 - False - False - False - False - True - False - False - False - False - False - .//test_DB - True - False - False - False - 2008-12-09 - False - False - True - True - True - True - True - True - - True - True - True - False - False - True - True - True - True - True - True - True - True - False - - True - - - - - 2 - 1 - - - 1 - 2 - - - MAX_LOCKS - 50 - 10 - 60 - 10 - - - - False - 10240 - ./ - 755 - True - False - 1048576 - False - 204800 - True - - - - 512 - 128 - 10000 - 10 - - - - - 10485760 - 0 - 2 - - - 1048576 - 0 - 0 - - 5 - - 1 - 1000 - - 1048576 - - - - 2000 - True - 100 - 100 - True - 15 - 1050 - 5000 - 100 - 50 - 1025 - False - True - 1 - ALL - - 127.0.0.0 - 11111 - - True - True - - - - - - 2 - VERY_HIGH - True - True - - - - - 1000 - True - 101 - 102 - True - 15 - 1050 - 5000 - 100 - 50 - 1025 - False - True - 1 - ALL - - 127.0.0.0 - 11111 - - True - True - - - \ No newline at end of file diff --git a/test/csharp/BTreeCursorTest.cs b/test/csharp/BTreeCursorTest.cs deleted file mode 100644 index ab60ea829..000000000 --- a/test/csharp/BTreeCursorTest.cs +++ /dev/null @@ -1,1258 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Threading; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class BTreeCursorTest : CSharpTestFixture - { - - private DatabaseEnvironment paramEnv; - private BTreeDatabase paramDB; - private EventWaitHandle signal; - - private delegate void BTCursorMoveFuncDelegate( - BTreeCursor cursor, LockingInfo lockingInfo); - private BTCursorMoveFuncDelegate btCursorFunc; - - [TestFixtureSetUp] - public void SetUpTestFixture() - { - testFixtureName = "BTreeCursorTest"; - base.SetUpTestfixture(); - } - - [Test] - public void TestAddKeyFirst() - { - BTreeDatabase db; - BTreeCursor cursor; - KeyValuePair pair; - - testName = "TestAddKeyFirst"; - SetUpTest(true); - - // Add record("key", "data") into database. - CursorTest.GetCursorInBtreeDBWithoutEnv( - testHome, testName, out db, out cursor); - CursorTest.AddOneByCursor(db, cursor); - - // Add record("key","data1") as the first of the data item of "key". - pair = new KeyValuePair( - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key")), - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data1"))); - cursor.Add(pair, Cursor.InsertLocation.FIRST); - - // Confirm the record is added as the first of the data item of "key". - cursor.Move(new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key")), true); - Assert.AreEqual(ASCIIEncoding.ASCII.GetBytes("data1"), - cursor.Current.Value.Data); - - cursor.Close(); - db.Close(); - } - - [Test] - public void TestAddKeyLast() - { - BTreeDatabase db; - BTreeCursor cursor; - KeyValuePair pair; - - testName = "TestAddKeyLast"; - SetUpTest(true); - - // Add record("key", "data") into database. - CursorTest.GetCursorInBtreeDBWithoutEnv(testHome, testName, - out db, out cursor); - CursorTest.AddOneByCursor(db, cursor); - - // Add new record("key","data1") as the last of the data item of "key". - pair = new KeyValuePair( - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key")), - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data1"))); - cursor.Add(pair, Cursor.InsertLocation.LAST); - - // Confirm the record is added as the first of the data item of "key". - cursor.Move(new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key")), true); - Assert.AreNotEqual(ASCIIEncoding.ASCII.GetBytes("data1"), - cursor.Current.Value.Data); - - cursor.Close(); - db.Close(); - } - - [Test] - public void TestAddUnique() - { - BTreeDatabase db; - BTreeCursor cursor; - KeyValuePair pair; - - testName = "TestAddUnique"; - SetUpTest(true); - - // Open a database and cursor. - BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - - // To put no duplicate data, the database should be set to be sorted. - dbConfig.Duplicates = DuplicatesPolicy.SORTED; - db = BTreeDatabase.Open(testHome + "/" + testName + ".db", dbConfig); - cursor = db.Cursor(); - - // Add record("key", "data") into database. - CursorTest.AddOneByCursor(db, cursor); - - // Fail to add duplicate record("key","data"). - pair = new KeyValuePair( - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key")), - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data"))); - try - { - cursor.AddUnique(pair); - } - catch (KeyExistException) - { - } - finally - { - cursor.Close(); - db.Close(); - } - } - - [Test] - public void TestDuplicateWithSamePos() - { - BTreeDatabase db; - BTreeDatabaseConfig dbConfig; - BTreeCursor cursor, dupCursor; - DatabaseEnvironment env; - DatabaseEnvironmentConfig envConfig; - DatabaseEntry key, data; - KeyValuePair pair; - Transaction txn; - - testName = "TestDuplicateWithSamePos"; - SetUpTest(true); - - envConfig = new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseMPool = true; - envConfig.UseTxns = true; - envConfig.NoMMap = false; - env = DatabaseEnvironment.Open(testHome, envConfig); - - txn = env.BeginTransaction(); - dbConfig = new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Env = env; - db = BTreeDatabase.Open(testName + ".db", dbConfig, txn); - key = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key")); - data = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data")); - pair = new KeyValuePair(key, data); - db.Put(key, data, txn); - txn.Commit(); - - txn = env.BeginTransaction(); - cursor = db.Cursor(txn); - cursor.Move(key, true); - - //Duplicate a new cursor to the same position. - dupCursor = cursor.Duplicate(true); - - // Overwrite the record. - dupCursor.Overwrite(new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("newdata"))); - - // Confirm that the original data doesn't exist. - Assert.IsFalse(dupCursor.Move(pair, true)); - - dupCursor.Close(); - cursor.Close(); - txn.Commit(); - db.Close(); - env.Close(); - } - - [Test, ExpectedException(typeof(ExpectedTestException))] - public void TestDuplicateToDifferentPos() - { - BTreeDatabase db; - BTreeDatabaseConfig dbConfig; - BTreeCursor cursor, dupCursor; - DatabaseEnvironment env; - DatabaseEnvironmentConfig envConfig; - DatabaseEntry key, data; - KeyValuePair pair; - Transaction txn; - - testName = "TestDuplicateToDifferentPos"; - SetUpTest(true); - - envConfig = new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseMPool = true; - envConfig.UseTxns = true; - envConfig.NoMMap = false; - env = DatabaseEnvironment.Open(testHome, envConfig); - - txn = env.BeginTransaction(); - dbConfig = new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Env = env; - db = BTreeDatabase.Open(testName + ".db", dbConfig, txn); - key = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key")); - data = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data")); - pair = new KeyValuePair(key, data); - db.Put(key, data, txn); - txn.Commit(); - - txn = env.BeginTransaction(); - cursor = db.Cursor(txn); - cursor.Move(key, true); - - //Duplicate a new cursor to the same position. - dupCursor = cursor.Duplicate(false); - - /* - * The duplicate cursor points to nothing so overwriting the - * record is not allowed. - */ - try - { - dupCursor.Overwrite(new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("newdata"))); - } - catch (DatabaseException) - { - throw new ExpectedTestException(); - } - finally - { - dupCursor.Close(); - cursor.Close(); - txn.Commit(); - db.Close(); - env.Close(); - } - } - - [Test] - public void TestInsertAfter() - { - BTreeDatabase db; - BTreeCursor cursor; - DatabaseEntry data; - KeyValuePair pair; - - testName = "TestInsertAfter"; - SetUpTest(true); - - // Add record("key", "data") into database. - CursorTest.GetCursorInBtreeDBWithoutEnv(testHome, testName, - out db, out cursor); - CursorTest.AddOneByCursor(db, cursor); - - // Insert the new record("key","data1") after the record("key", "data"). - data = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data1")); - cursor.Insert(data, Cursor.InsertLocation.AFTER); - - /* - * Move the cursor to the record("key", "data") and confirm that - * the next record is the one just inserted. - */ - pair = new KeyValuePair( - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key")), - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data"))); - Assert.IsTrue(cursor.Move(pair, true)); - Assert.IsTrue(cursor.MoveNext()); - Assert.AreEqual(ASCIIEncoding.ASCII.GetBytes("key"), cursor.Current.Key.Data); - Assert.AreEqual(ASCIIEncoding.ASCII.GetBytes("data1"), cursor.Current.Value.Data); - - cursor.Close(); - db.Close(); - } - - [Test] - public void TestMoveFirstMultipleAndMultipleKey() - { - testName = "TestMoveFirstMultipleAndMultipleKey"; - SetUpTest(true); - string btreeDBFileName = testHome + "/" + - testName + ".db"; - BTreeDatabase db; - BTreeCursor cursor; - KeyValuePair pair; - MultipleKeyDatabaseEntry multiPair; - int cnt; - int[] size = new int[2]; - size[0] = 0; - size[1] = 1024; - - BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.ALWAYS; - dbConfig.Duplicates = DuplicatesPolicy.UNSORTED; - dbConfig.PageSize = 1024; - GetMultipleDB(btreeDBFileName, dbConfig, null, out db, out cursor); - - for (int i = 0; i < 2; i++) { - cnt = 0; - if (size[i] == 0) - cursor.MoveFirstMultiple(); - else - cursor.MoveFirstMultiple(size[i]); - pair = cursor.CurrentMultiple; - foreach (DatabaseEntry dbt in pair.Value) - cnt++; - Assert.AreEqual(1, cnt); - } - - for (int i = 0; i < 2; i++) { - cnt = 0; - if (size[i] == 0) - cursor.MoveFirstMultipleKey(); - else - cursor.MoveFirstMultipleKey(size[i]); - multiPair = cursor.CurrentMultipleKey; - foreach (KeyValuePair - dbt in multiPair) - cnt++; - Assert.Less(1, cnt); - } - - cursor.Close(); - db.Close(); - } - - [Test] - public void TestMoveMultiple() - { - testName = "TestMoveMultiple"; - SetUpTest(true); - string btreeDBFileName = testHome + "/" + - testName + ".db"; - BTreeDatabase db; - BTreeCursor cursor; - DatabaseEntry key; - KeyValuePair pair; - int cnt; - - BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.ALWAYS; - dbConfig.Duplicates = DuplicatesPolicy.UNSORTED; - dbConfig.PageSize = 1024; - GetMultipleDB(btreeDBFileName, dbConfig, null, out db, out cursor); - - // Move cursor to pairs with exact 99 as its key. - cnt = 0; - key = new DatabaseEntry(BitConverter.GetBytes((int)99)); - cursor.MoveMultiple(key, true); - pair = cursor.CurrentMultiple; - Assert.AreEqual(99, BitConverter.ToInt32(pair.Key.Data, 0)); - foreach (DatabaseEntry dbt in pair.Value) - cnt++; - Assert.AreEqual(2, cnt); - - // Move cursor to pairs with the smallest key larger than 100. - cnt = 0; - key = new DatabaseEntry(BitConverter.GetBytes((int)100)); - cursor.MoveMultiple(key, false); - pair = cursor.CurrentMultiple; - Assert.AreEqual(101, BitConverter.ToInt32(pair.Key.Data, 0)); - foreach (DatabaseEntry dbt in pair.Value) - cnt++; - Assert.AreEqual(1, cnt); - - cursor.Close(); - db.Close(); - } - - [Test] - public void TestMoveMultipleKey() - { - testName = "TestMoveMultipleKey"; - SetUpTest(true); - string btreeDBFileName = testHome + "/" + - testName + ".db"; - BTreeDatabase db; - BTreeCursor cursor; - DatabaseEntry key; - MultipleKeyDatabaseEntry mulPair; ; - int cnt; - - BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.ALWAYS; - dbConfig.Duplicates = DuplicatesPolicy.UNSORTED; - dbConfig.PageSize = 1024; - GetMultipleDB(btreeDBFileName, dbConfig, null, out db, out cursor); - - /* - * Bulk retrieve key/value pair from the pair whose key - * is exact 99. - */ - cnt = 0; - key = new DatabaseEntry(BitConverter.GetBytes((int)99)); - cursor.MoveMultipleKey(key, true); - mulPair = cursor.CurrentMultipleKey; - foreach (KeyValuePair - pair in mulPair) { - Assert.GreaterOrEqual(3, - BitConverter.ToInt32(pair.Key.Data, 0) - 98); - cnt++; - } - Assert.AreEqual(3, cnt); - - /* - * Bulk retrieve key/value pair from the pair whose key - * is the smallest one larger than 100. - */ - cnt = 0; - key = new DatabaseEntry(BitConverter.GetBytes((int)100)); - cursor.MoveMultipleKey(key, false); - mulPair = cursor.CurrentMultipleKey; - foreach (KeyValuePair - pair in mulPair) { - Assert.AreEqual(101, - BitConverter.ToInt32(pair.Key.Data, 0)); - cnt++; - } - Assert.LessOrEqual(1, cnt); - - cnt = 0; - key = new DatabaseEntry(BitConverter.GetBytes((int)100)); - cursor.MoveMultipleKey(key, false, 1024); - mulPair = cursor.CurrentMultipleKey; - foreach (KeyValuePair - pair in mulPair) { - Assert.AreEqual(101, - BitConverter.ToInt32(pair.Key.Data, 0)); - Assert.AreEqual(101, - BitConverter.ToInt32(pair.Value.Data, 0)); - cnt++; - } - Assert.LessOrEqual(1, cnt); - - cursor.Close(); - db.Close(); - } - - [Test] - public void TestMoveMultipleKeyWithRecno() - { - testName = "TestMoveMultipleKeyWithRecno"; - SetUpTest(true); - string btreeDBFileName = testHome + "/" + - testName + ".db"; - BTreeDatabase db; - BTreeCursor cursor; - MultipleKeyDatabaseEntry multiDBT; - int cnt; - - BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); - dbConfig.UseRecordNumbers = true; - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.PageSize = 1024; - GetMultipleDB(btreeDBFileName, dbConfig, null, out db, out cursor); - - cnt = 0; - cursor.MoveMultipleKey(98); - multiDBT = cursor.CurrentMultipleKey; - foreach (KeyValuePair - pair in multiDBT) - cnt++; - Assert.AreEqual(3, cnt); - - cnt = 0; - cursor.MoveMultipleKey(98, 1024); - multiDBT = cursor.CurrentMultipleKey; - foreach (KeyValuePair - pair in multiDBT) - cnt++; - Assert.AreEqual(3, cnt); - - cursor.Close(); - db.Close(); - } - - [Test] - public void TestMoveMultiplePairs() - { - testName = "TestMoveMultiplePairs"; - SetUpTest(true); - string btreeDBFileName = testHome + "/" + - testName + ".db"; - BTreeDatabase db; - BTreeCursor cursor; - DatabaseEntry key, data; - KeyValuePair pair; - MultipleKeyDatabaseEntry multiKeyDBTs1, multiKeyDBTs2; - int cnt; - - BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.ALWAYS; - dbConfig.Duplicates = DuplicatesPolicy.SORTED; - dbConfig.PageSize = 1024; - GetMultipleDB(btreeDBFileName, dbConfig, null, out db, out cursor); - - /* - * Bulk retrieve pairs from the pair whose key/data - * is exact 99/99. - */ - cnt = 0; - key = new DatabaseEntry(BitConverter.GetBytes((int)99)); - data = new DatabaseEntry(BitConverter.GetBytes((int)99)); - pair = new KeyValuePair(key, data); - cursor.MoveMultipleKey(pair, true); - multiKeyDBTs1 = cursor.CurrentMultipleKey; - foreach (KeyValuePair - p in multiKeyDBTs1) - cnt++; - Assert.AreEqual(3, cnt); - - // Bulk retrieve pairs from the pair whose key is exact 99. - cnt = 0; - key = new DatabaseEntry(BitConverter.GetBytes((int)99)); - data = new DatabaseEntry(BitConverter.GetBytes((int)98)); - cursor.MoveMultipleKey(pair, true); - multiKeyDBTs2 = cursor.CurrentMultipleKey; - foreach (KeyValuePair - dbts in multiKeyDBTs2) - cnt++; - Assert.AreEqual(3, cnt); - - /* - * Bulk retrieve pairs from the pair whose key is - * exact 99 in buffer size of 1024k. - */ - cnt = 0; - key = new DatabaseEntry(BitConverter.GetBytes((int)99)); - data = new DatabaseEntry(BitConverter.GetBytes((int)102)); - cursor.MoveMultipleKey(pair, true, 1024); - multiKeyDBTs2 = cursor.CurrentMultipleKey; - foreach (KeyValuePair - dbts in multiKeyDBTs2) - cnt++; - Assert.AreEqual(3, cnt); - - cursor.Close(); - db.Close(); - } - - [Test] - public void TestMoveMultiplePairWithKey() - { - testName = "TestMoveMultiplePairWithKey"; - SetUpTest(true); - string btreeDBFileName = testHome + "/" + - testName + ".db"; - BTreeDatabase db; - BTreeCursor cursor; - DatabaseEntry key, data; - KeyValuePair pair; - KeyValuePair mulPair; - int cnt; - - BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.ALWAYS; - dbConfig.Duplicates = DuplicatesPolicy.UNSORTED; - dbConfig.PageSize = 1024; - GetMultipleDB(btreeDBFileName, dbConfig, null, out db, out cursor); - - /* - * Move the cursor to pairs with exact 99 as its key - * and 99 as its data. - */ - cnt = 0; - key = new DatabaseEntry(BitConverter.GetBytes((int)99)); - data = new DatabaseEntry(BitConverter.GetBytes((int)99)); - pair = new KeyValuePair(key, data); - cursor.MoveMultiple(pair, true); - mulPair = cursor.CurrentMultiple; - Assert.AreEqual(99, BitConverter.ToInt32(mulPair.Key.Data, 0)); - foreach (DatabaseEntry dbt in mulPair.Value) { - Assert.AreEqual(99, BitConverter.ToInt32(dbt.Data, 0)); - cnt++; - } - Assert.AreEqual(1, cnt); - - // Move cursor to pairs with the smallest key larger than 100. - cnt = 0; - key = new DatabaseEntry(BitConverter.GetBytes((int)100)); - data = new DatabaseEntry(BitConverter.GetBytes((int)100)); - cursor.MoveMultiple(pair, false); - mulPair = cursor.CurrentMultiple; - Assert.AreEqual(99, BitConverter.ToInt32(mulPair.Key.Data, 0)); - foreach (DatabaseEntry dbt in mulPair.Value) { - Assert.GreaterOrEqual(1, - BitConverter.ToInt32(dbt.Data, 0) - 99); - cnt++; - } - Assert.AreEqual(1, cnt); - - cursor.Close(); - db.Close(); - } - - [Test] - public void TestMoveNextMultipleKey() - { - testName = "TestMoveNextMultipleKey"; - SetUpTest(true); - - BTreeDatabase db; - BTreeCursor cursor; - DatabaseEnvironment env; - Transaction txn; - int cnt = 0; - - BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.ALWAYS; - dbConfig.Duplicates = DuplicatesPolicy.UNSORTED; - - GetMultipleDB(testHome, testName + ".db", dbConfig, - out env, out txn, out db, out cursor); - - /* - * Position the cursor to the first record, and bulk get - * all other records. - */ - Assert.True(cursor.MoveFirst()); - Assert.True(cursor.MoveNextMultipleKey()); - MultipleKeyDatabaseEntry pairs = - cursor.CurrentMultipleKey; - foreach (KeyValuePair p - in pairs) - cnt++; - Assert.AreEqual(cnt, 100); - - // Bulk read uncommitted records. - cnt = 0; - LockingInfo lockingInfo = new LockingInfo(); - Assert.True(cursor.MoveFirst(lockingInfo)); - lockingInfo.IsolationDegree = Isolation.DEGREE_ONE; - Assert.True(cursor.MoveNextMultipleKey(lockingInfo)); - pairs = cursor.CurrentMultipleKey; - foreach (KeyValuePair p - in pairs) - cnt++; - Assert.AreEqual(cnt, 100); - - cursor.Dispose(); - txn.Commit(); - - /* - * Insert duplicate records of the key in the last - * record. - */ - txn = env.BeginTransaction(); - cursor = db.Cursor(txn); - Assert.IsTrue(cursor.MoveLast()); - DatabaseEntry key = cursor.Current.Key; - for (int i = 200; i < 800; i++) - cursor.Insert(new DatabaseEntry( - BitConverter.GetBytes(i)), - Cursor.InsertLocation.AFTER); - - /* - * Move to the first duplicate records, and bulk read - * all duplicate ones. - */ - cnt = 0; - Assert.True(cursor.Move(key, true)); - while (cursor.MoveNextDuplicateMultipleKey( - (int)db.Pagesize)) { - pairs = cursor.CurrentMultipleKey; - foreach (KeyValuePair< - DatabaseEntry, DatabaseEntry> p in pairs) { - Assert.AreEqual(key.Data, p.Key.Data); - cnt++; - } - } - Assert.AreEqual(cnt, 600); - - cursor.Close(); - db.Close(); - txn.Commit(); - env.Close(); - } - - [Test] - public void TestMoveMultipleWithRecno() - { - testName = "TestMoveMultipleWithRecno"; - SetUpTest(true); - string btreeDBFileName = testHome + "/" + - testName + ".db"; - BTreeDatabase db; - BTreeCursor cursor; - KeyValuePair pair; - int cnt; - - BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.ALWAYS; - dbConfig.PageSize = 1024; - dbConfig.UseRecordNumbers = true; - GetMultipleDB(btreeDBFileName, dbConfig, null, out db, out cursor); - - // Move cursor to the No.100 record. - cnt = 0; - cursor.MoveMultiple(100); - pair = cursor.CurrentMultiple; - Assert.AreEqual(100, BitConverter.ToInt32(pair.Key.Data, 0)); - foreach (DatabaseEntry dbt in pair.Value) - cnt++; - Assert.AreEqual(1, cnt); - - // Move cursor to the No.100 record with buffer size of 1024k. - cnt = 0; - cursor.MoveMultiple(100, 1024); - pair = cursor.CurrentMultiple; - Assert.AreEqual(100, BitConverter.ToInt32(pair.Key.Data, 0)); - foreach (DatabaseEntry dbt in pair.Value) - cnt++; - Assert.AreEqual(1, cnt); - - cursor.Close(); - db.Close(); - } - - [Test] - public void TestMoveNextDuplicateMultipleAndMultipleKey() - { - testName = "TestMoveNextDuplicateMultipleAndMultipleKey"; - SetUpTest(true); - string btreeDBFileName = testHome + "/" + - testName + ".db"; - BTreeDatabase db; - BTreeCursor cursor; - DatabaseEntry key, data; - KeyValuePair pair; - KeyValuePair pairs; - MultipleKeyDatabaseEntry multiPair; - int cnt; - int[] size = new int[2]; - size[0] = 0; - size[1] = 1024; - - BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.ALWAYS; - dbConfig.Duplicates = DuplicatesPolicy.SORTED; - dbConfig.PageSize = 1024; - GetMultipleDB(btreeDBFileName, dbConfig, null, out db, out cursor); - - key = new DatabaseEntry(BitConverter.GetBytes(99)); - data = new DatabaseEntry(BitConverter.GetBytes(99)); - pair = new KeyValuePair(key, data); - - for (int j = 0; j < 2; j++) { - for (int i = 0; i < 2; i++) { - cnt = 0; - cursor.Move(pair, true); - if (j == 0) { - if (size[i] == 0) - Assert.IsTrue(cursor.MoveNextDuplicateMultiple()); - else - Assert.IsTrue(cursor.MoveNextDuplicateMultiple(size[i])); - pairs = cursor.CurrentMultiple; - foreach (DatabaseEntry dbt in pairs.Value) { - Assert.AreEqual(100, BitConverter.ToInt32(dbt.Data, 0)); - cnt++; - } - Assert.AreEqual(1, cnt); - } else { - if (size[i] == 0) - Assert.IsTrue(cursor.MoveNextDuplicateMultipleKey()); - else - Assert.IsTrue(cursor.MoveNextDuplicateMultipleKey(size[i])); - multiPair = cursor.CurrentMultipleKey; - foreach (KeyValuePair p in multiPair) { - Assert.AreEqual(100, BitConverter.ToInt32(p.Value.Data, 0)); - cnt++; - } - Assert.AreEqual(1, cnt); - } - } - } - - cursor.Close(); - db.Close(); - } - - [Test] - public void TestMoveNextUniqueMultipleAndMultipleKey() - { - testName = "TestMoveNextUniqueMultipleAndMultipleKey"; - SetUpTest(true); - string btreeDBFileName = testHome + "/" + - testName + ".db"; - BTreeDatabase db; - BTreeCursor cursor; - DatabaseEntry key, data; - KeyValuePair pair; - KeyValuePair pairs; - MultipleKeyDatabaseEntry multiPair; - int cnt; - int[] size = new int[2]; - size[0] = 0; - size[1] = 1024; - - BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.ALWAYS; - dbConfig.Duplicates = DuplicatesPolicy.UNSORTED; - dbConfig.PageSize = 1024; - GetMultipleDB(btreeDBFileName, dbConfig, null, out db, out cursor); - - key = new DatabaseEntry(BitConverter.GetBytes(99)); - data = new DatabaseEntry(BitConverter.GetBytes(99)); - pair = new KeyValuePair(key, data); - - for (int j = 0; j < 2; j++) { - for (int i = 0; i < 2; i++) { - cnt = 0; - cursor.Move(pair, true); - if (j == 0) { - if (size[i] == 0) - Assert.IsTrue(cursor.MoveNextUniqueMultiple()); - else - Assert.IsTrue(cursor.MoveNextUniqueMultiple(size[i])); - pairs = cursor.CurrentMultiple; - foreach (DatabaseEntry dbt in pairs.Value) { - Assert.AreEqual(101, BitConverter.ToInt32(dbt.Data, 0)); - cnt++; - } - Assert.AreEqual(1, cnt); - } else { - if (size[i] == 0) - Assert.IsTrue(cursor.MoveNextUniqueMultipleKey()); - else - Assert.IsTrue(cursor.MoveNextUniqueMultipleKey(size[i])); - multiPair = cursor.CurrentMultipleKey; - foreach (KeyValuePair p in multiPair) { - Assert.AreEqual(101, BitConverter.ToInt32(p.Value.Data, 0)); - cnt++; - } - Assert.AreEqual(1, cnt); - } - } - } - - cursor.Close(); - db.Close(); - } - - [Test] - public void TestRefreshMultipleAndMultipleKey() - { - testName = "TestRefreshMultipleAndMultipleKey"; - SetUpTest(true); - string btreeDBFileName = testHome + "/" + - testName + ".db"; - BTreeDatabase db; - BTreeCursor cursor; - DatabaseEntry key, data; - KeyValuePair pair; - KeyValuePair pairs; - MultipleKeyDatabaseEntry multiPair; - int cnt; - int[] size = new int[2]; - size[0] = 0; - size[1] = 1024; - - BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.ALWAYS; - dbConfig.Duplicates = DuplicatesPolicy.SORTED; - dbConfig.PageSize = 1024; - GetMultipleDB(btreeDBFileName, dbConfig, null, out db, out cursor); - - key = new DatabaseEntry(BitConverter.GetBytes(99)); - data = new DatabaseEntry(BitConverter.GetBytes(99)); - pair = new KeyValuePair(key, data); - - for (int j = 0; j < 2; j++) { - for (int i = 0; i < 2; i++) { - cnt = 0; - cursor.Move(pair, true); - if (j == 0) { - if (size[i] == 0) - Assert.IsTrue(cursor.RefreshMultiple()); - else - Assert.IsTrue(cursor.RefreshMultiple(size[i])); - pairs = cursor.CurrentMultiple; - foreach (DatabaseEntry dbt in pairs.Value) - cnt++; - Assert.AreEqual(2, cnt); - } else { - if (size[i] == 0) - Assert.IsTrue(cursor.RefreshMultipleKey()); - else - Assert.IsTrue(cursor.RefreshMultipleKey(size[i])); - multiPair = cursor.CurrentMultipleKey; - foreach (KeyValuePair p in multiPair) - cnt++; - Assert.AreEqual(3, cnt); - } - } - } - - cursor.Close(); - db.Close(); - } - - private void GetMultipleDB(string dbFileName, - BTreeDatabaseConfig dbConfig, Transaction txn, - out BTreeDatabase db, out BTreeCursor cursor) - { - if (txn == null) { - db = BTreeDatabase.Open(dbFileName, dbConfig); - cursor = db.Cursor(); - } else { - db = BTreeDatabase.Open( - dbFileName, dbConfig, txn); - CursorConfig cursorConfig = new CursorConfig(); - cursor = db.Cursor(cursorConfig, txn); - } - - KeyValuePair pair; - DatabaseEntry key, data; - for (int i = 1; i < 100; i++) { - key = new DatabaseEntry(BitConverter.GetBytes(i)); - data = new DatabaseEntry(BitConverter.GetBytes(i)); - pair = new KeyValuePair(key, data); - cursor.Add(pair); - } - - if (dbConfig.UseRecordNumbers == true) { - byte[] bytes = new byte[512]; - for (int i = 0; i < 512; i++) - bytes[i] = (byte)i; - key = new DatabaseEntry(BitConverter.GetBytes(100)); - data = new DatabaseEntry(bytes); - pair = new KeyValuePair(key, data); - cursor.Add(pair); - } else { - if (dbConfig.Duplicates == DuplicatesPolicy.UNSORTED || - dbConfig.Duplicates == DuplicatesPolicy.SORTED) { - key = new DatabaseEntry(BitConverter.GetBytes(99)); - data = new DatabaseEntry(BitConverter.GetBytes(100)); - pair = new KeyValuePair(key, data); - cursor.Add(pair); - } - - key = new DatabaseEntry(BitConverter.GetBytes(101)); - data = new DatabaseEntry(BitConverter.GetBytes(101)); - pair = new KeyValuePair(key, data); - cursor.Add(pair); - } - } - - private void GetMultipleDB(string home, string dbFileName, - BTreeDatabaseConfig dbConfig, out DatabaseEnvironment env, - out Transaction txn, out BTreeDatabase db, - out BTreeCursor cursor) - { - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseTxns = true; - envConfig.UseLocking = true; - envConfig.UseLogging = true; - envConfig.UseMPool = true; - env = DatabaseEnvironment.Open(home, envConfig); - txn = env.BeginTransaction(); - if (dbConfig == null) - dbConfig = new BTreeDatabaseConfig(); - dbConfig.Env = env; - GetMultipleDB(dbFileName, dbConfig, txn, out db, - out cursor); - } - - [Test] - public void TestInsertBefore() - { - BTreeDatabase db; - BTreeCursor cursor; - DatabaseEntry data; - KeyValuePair pair; - - testName = "TestInsertBefore"; - SetUpTest(true); - - // Add record("key", "data") into database. - CursorTest.GetCursorInBtreeDBWithoutEnv( - testHome, testName, out db, out cursor); - CursorTest.AddOneByCursor(db, cursor); - - // Insert the new record("key","data1") before the record("key", "data"). - data = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data1")); - cursor.Insert(data, Cursor.InsertLocation.BEFORE); - - /* - * Move the cursor to the record("key", "data") and confirm - * that the previous record is the one just inserted. - */ - pair = new KeyValuePair( - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key")), - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data"))); - Assert.IsTrue(cursor.Move(pair, true)); - Assert.IsTrue(cursor.MovePrev()); - Assert.AreEqual(ASCIIEncoding.ASCII.GetBytes("key"),cursor.Current.Key.Data); - Assert.AreEqual(ASCIIEncoding.ASCII.GetBytes("data1"), cursor.Current.Value.Data); - - cursor.Close(); - db.Close(); - } - - [Test] - public void TestMoveToRecno() - { - BTreeDatabase db; - BTreeCursor cursor; - - testName = "TestMoveToRecno"; - SetUpTest(true); - - GetCursorInBtreeDBUsingRecno(testHome, testName, - out db, out cursor); - for (int i = 0; i < 10; i++) - db.Put( - new DatabaseEntry(BitConverter.GetBytes(i)), - new DatabaseEntry(BitConverter.GetBytes(i))); - - MoveCursorToRecno(cursor, null); - cursor.Close(); - db.Close(); - } - - [Test] - public void TestMoveToRecnoWithRMW() - { - testName = "TestMoveToRecnoWithRMW"; - SetUpTest(true); - - btCursorFunc = new BTCursorMoveFuncDelegate( - MoveCursorToRecno); - - // Move to a specified key and a key/data pair. - MoveWithRMW(testHome, testName); - } - - [Test] - public void TestRecno() - { - BTreeDatabase db; - BTreeCursor cursor; - - testName = "TestRecno"; - SetUpTest(true); - - GetCursorInBtreeDBUsingRecno(testHome, testName, - out db, out cursor); - for (int i = 0; i < 10; i++) - db.Put( - new DatabaseEntry(BitConverter.GetBytes(i)), - new DatabaseEntry(BitConverter.GetBytes(i))); - - ReturnRecno(cursor, null); - cursor.Close(); - db.Close(); - } - - [Test] - public void TestRecnoWithRMW() - { - testName = "TestRecnoWithRMW"; - SetUpTest(true); - - // Use MoveCursorToRecno() as its move function. - btCursorFunc = new BTCursorMoveFuncDelegate( - ReturnRecno); - - // Move to a specified key and a key/data pair. - MoveWithRMW(testHome, testName); - } - - /* - * Move the cursor according to recno. The recno - * starts from 1 and is increased by 1. - */ - public void MoveCursorToRecno(BTreeCursor cursor, - LockingInfo lck) - { - for (uint i = 1; i <= 5; i++) - if (lck == null) - Assert.IsTrue(cursor.Move(i)); - else - Assert.IsTrue(cursor.Move(i, lck)); - } - - /*l - * Move the cursor according to a given recno and - * return the current record's recno. The given recno - * and the one got from the cursor should be the same. - */ - public void ReturnRecno(BTreeCursor cursor, - LockingInfo lck) - { - for (uint i = 1; i <= 5; i++) - if (lck == null) - { - if (cursor.Move(i) == true) - Assert.AreEqual(i, cursor.Recno()); - } - else - { - if (cursor.Move(i, lck) == true) - Assert.AreEqual(i, cursor.Recno(lck)); - } - } - - public void GetCursorInBtreeDBUsingRecno(string home, - string name, out BTreeDatabase db, - out BTreeCursor cursor) - { - string dbFileName = home + "/" + name + ".db"; - BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); - dbConfig.UseRecordNumbers = true; - dbConfig.Creation = CreatePolicy.IF_NEEDED; - db = BTreeDatabase.Open(dbFileName, dbConfig); - cursor = db.Cursor(); - } - - public void RdMfWt() - { - Transaction txn = paramEnv.BeginTransaction(); - BTreeCursor dbc = paramDB.Cursor(txn); - - try - { - LockingInfo lck = new LockingInfo(); - lck.ReadModifyWrite = true; - - // Read record. - btCursorFunc(dbc, lck); - - // Block the current thread until event is set. - signal.WaitOne(); - - // Write new records into database. - DatabaseEntry key = new DatabaseEntry(BitConverter.GetBytes(55)); - DatabaseEntry data = new DatabaseEntry(BitConverter.GetBytes(55)); - dbc.Add(new KeyValuePair(key, data)); - - dbc.Close(); - txn.Commit(); - } - catch (DeadlockException) - { - dbc.Close(); - txn.Abort(); - } - } - - public void MoveWithRMW(string home, string name) - { - paramEnv = null; - paramDB = null; - - // Open the environment. - DatabaseEnvironmentConfig envCfg = - new DatabaseEnvironmentConfig(); - envCfg.Create = true; - envCfg.FreeThreaded = true; - envCfg.UseLocking = true; - envCfg.UseLogging = true; - envCfg.UseMPool = true; - envCfg.UseTxns = true; - paramEnv = DatabaseEnvironment.Open(home, envCfg); - - // Open database in transaction. - Transaction openTxn = paramEnv.BeginTransaction(); - BTreeDatabaseConfig cfg = new BTreeDatabaseConfig(); - cfg.Creation = CreatePolicy.ALWAYS; - cfg.Env = paramEnv; - cfg.FreeThreaded = true; - cfg.PageSize = 4096; - // Use record number. - cfg.UseRecordNumbers = true; - paramDB = BTreeDatabase.Open(name + ".db", cfg, openTxn); - openTxn.Commit(); - - /* - * Put 10 different, 2 duplicate and another different - * records into database. - */ - Transaction txn = paramEnv.BeginTransaction(); - for (int i = 0; i < 13; i++) - { - DatabaseEntry key, data; - if (i == 10 || i == 11) - { - key = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key")); - data = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data")); - } - else - { - key = new DatabaseEntry(BitConverter.GetBytes(i)); - data = new DatabaseEntry(BitConverter.GetBytes(i)); - } - paramDB.Put(key, data, txn); - } - - txn.Commit(); - - // Get a event wait handle. - signal = new EventWaitHandle(false, - EventResetMode.ManualReset); - - /* - * Start RdMfWt() in two threads. RdMfWt() reads - * and writes data into database. - */ - Thread t1 = new Thread(new ThreadStart(RdMfWt)); - Thread t2 = new Thread(new ThreadStart(RdMfWt)); - t1.Start(); - t2.Start(); - - // Give both threads time to read before signalling them to write. - Thread.Sleep(1000); - - // Invoke the write operation in both threads. - signal.Set(); - - // Return the number of deadlocks. - while (t1.IsAlive || t2.IsAlive) - { - /* - * Give both threads time to write before - * counting the number of deadlocks. - */ - Thread.Sleep(1000); - uint deadlocks = paramEnv.DetectDeadlocks(DeadlockPolicy.DEFAULT); - - // Confirm that there won't be any deadlock. - Assert.AreEqual(0, deadlocks); - } - - t1.Join(); - t2.Join(); - paramDB.Close(); - paramEnv.Close(); - } - - } -} - diff --git a/test/csharp/BTreeDatabaseConfigTest.cs b/test/csharp/BTreeDatabaseConfigTest.cs deleted file mode 100644 index 4e4fc30d7..000000000 --- a/test/csharp/BTreeDatabaseConfigTest.cs +++ /dev/null @@ -1,87 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class BTreeDatabaseConfigTest : DatabaseConfigTest - { - - [TestFixtureSetUp] - public override void SetUpTestFixture() - { - testFixtureName = "BTreeDatabaseConfigTest"; - base.SetUpTestfixture(); - } - - [Test] - override public void TestConfigWithoutEnv() - { - testName = "TestConfigWithoutEnv"; - SetUpTest(false); - XmlElement xmlElem = Configuration.TestSetUp( - testFixtureName, testName); - BTreeDatabaseConfig btreeConfig = - new BTreeDatabaseConfig(); - Config(xmlElem, ref btreeConfig, true); - Confirm(xmlElem, btreeConfig, true); - } - - public static void Confirm(XmlElement - xmlElement, BTreeDatabaseConfig btreeDBConfig, - bool compulsory) - { - DatabaseConfig dbConfig = btreeDBConfig; - Confirm(xmlElement, dbConfig, compulsory); - - // Confirm Btree database specific configuration - Configuration.ConfirmDuplicatesPolicy(xmlElement, - "Duplicates", btreeDBConfig.Duplicates, compulsory); - Configuration.ConfirmBool(xmlElement, - "NoReverseSplitting", - btreeDBConfig.NoReverseSplitting, compulsory); - Configuration.ConfirmBool(xmlElement, - "UseRecordNumbers", btreeDBConfig.UseRecordNumbers, - compulsory); - Configuration.ConfirmCreatePolicy(xmlElement, - "Creation", btreeDBConfig.Creation, compulsory); - Configuration.ConfirmUint(xmlElement, "MinKeysPerPage", - btreeDBConfig.MinKeysPerPage, compulsory); - } - - public static void Config(XmlElement xmlElement, - ref BTreeDatabaseConfig btreeDBConfig, bool compulsory) - { - uint minKeysPerPage = new uint(); - DatabaseConfig dbConfig = btreeDBConfig; - Config(xmlElement, ref dbConfig, compulsory); - - // Configure specific fields/properties of Btree db - Configuration.ConfigDuplicatesPolicy(xmlElement, - "Duplicates", ref btreeDBConfig.Duplicates, - compulsory); - Configuration.ConfigBool(xmlElement, - "NoReverseSplitting", - ref btreeDBConfig.NoReverseSplitting, compulsory); - Configuration.ConfigBool(xmlElement, - "UseRecordNumbers", - ref btreeDBConfig.UseRecordNumbers, compulsory); - Configuration.ConfigCreatePolicy(xmlElement, - "Creation", ref btreeDBConfig.Creation, compulsory); - if (Configuration.ConfigUint(xmlElement, - "MinKeysPerPage", ref minKeysPerPage, compulsory)) - btreeDBConfig.MinKeysPerPage = minKeysPerPage; - } - } -} diff --git a/test/csharp/BTreeDatabaseTest.cs b/test/csharp/BTreeDatabaseTest.cs deleted file mode 100644 index 57d195e2a..000000000 --- a/test/csharp/BTreeDatabaseTest.cs +++ /dev/null @@ -1,2811 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class BTreeDatabaseTest : DatabaseTest - { - - [TestFixtureSetUp] - public void SetUpTestFixture() - { - testFixtureName = "BTreeDatabaseTest"; - base.SetUpTestfixture(); - } - - [Test] - public void TestCompactWithoutTxn() - { - int i, nRecs; - nRecs = 1000; - testName = "TestCompactWithoutTxn"; - SetUpTest(true); - string btreeDBFileName = testHome + "/" + - testName + ".db"; - - BTreeDatabaseConfig btreeDBConfig = - new BTreeDatabaseConfig(); - btreeDBConfig.Creation = CreatePolicy.ALWAYS; - // The minimum page size - btreeDBConfig.PageSize = 512; - btreeDBConfig.BTreeCompare = - new EntryComparisonDelegate(dbIntCompare); - using (BTreeDatabase btreeDB = BTreeDatabase.Open( - btreeDBFileName, btreeDBConfig)) - { - DatabaseEntry key; - DatabaseEntry data; - - // Fill the database with entries from 0 to 999 - for (i = 0; i < nRecs; i++) - { - key = new DatabaseEntry( - BitConverter.GetBytes(i)); - data = new DatabaseEntry( -ASCIIEncoding.ASCII.GetBytes(Configuration.RandomString(100))); - btreeDB.Put(key, data); - } - - /* - * Delete entries below 50, between 300 and - * 500 and above 700 - */ - for (i = 0; i < nRecs; i++) - if (i < (int)(nRecs * 0.05) || - i > (int)(nRecs * 0.7) || - (i < (int)(nRecs * 0.5) && - i > (int)(nRecs * 0.3))) - { - key = new DatabaseEntry( - BitConverter.GetBytes(i)); - btreeDB.Delete(key); - } - - btreeDB.Sync(); - long fileSize = new FileInfo( - btreeDBFileName).Length; - - // Compact database - CompactConfig cCfg = new CompactConfig(); - cCfg.FillPercentage = 80; - cCfg.Pages = 4; - cCfg.Timeout = 1000; - cCfg.TruncatePages = true; - cCfg.start = new DatabaseEntry( - BitConverter.GetBytes(1)); - cCfg.stop = new DatabaseEntry( - BitConverter.GetBytes(7000)); - CompactData compactData = btreeDB.Compact(cCfg); - // Verify output statistics fields. - Assert.AreEqual(0, compactData.Deadlocks); - Assert.LessOrEqual(0, compactData.EmptyBuckets); - Assert.LessOrEqual(0, compactData.Levels); - Assert.Less(0, compactData.PagesExamined); - Assert.Less(0, compactData.PagesFreed); - Assert.Less(compactData.PagesFreed, - compactData.PagesTruncated); - - btreeDB.Sync(); - long compactedFileSize = - new FileInfo(btreeDBFileName).Length; - Assert.Less(compactedFileSize, fileSize); - } - } - - [Test] - public void TestCompression() { - testName = "TestCompression"; - SetUpTest(true); - string btreeDBName = testHome + "/" + testName + ".db"; - - BTreeDatabaseConfig cfg = new BTreeDatabaseConfig(); - cfg.Creation = CreatePolicy.ALWAYS; - cfg.SetCompression(compress, decompress); - BTreeDatabase db = BTreeDatabase.Open(btreeDBName, cfg); - DatabaseEntry key, data; - char[] keyData = { 'A', 'A', 'A', 'A' }; - data = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("abcdefghij")); - int i; - for (i = 0; i < 2000; i++) { - // Write random data - key = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes(keyData)); - db.Put(key, data); - - // Bump the key. Rollover from Z to A if necessary - int j = keyData.Length; - do { - j--; - if (keyData[j]++ == 'Z') - keyData[j] = 'A'; - } while (keyData[j] == 'A'); - } - db.Close(); - } - - bool compress(DatabaseEntry prevKey, DatabaseEntry prevData, - DatabaseEntry key, DatabaseEntry data, ref byte[] dest, out int size) { - /* - * Just a dummy function that doesn't do any compression. It just - * writes the 5 byte key and 10 byte data to the buffer. - */ - size = key.Data.Length + data.Data.Length; - if (size > dest.Length) - return false; - key.Data.CopyTo(dest, 0); - data.Data.CopyTo(dest, key.Data.Length); - return true; - } - - KeyValuePair decompress( - DatabaseEntry prevKey, DatabaseEntry prevData, byte[] compressed, out uint bytesRead) { - byte[] keyData = new byte[4]; - byte[] dataData = new byte[10]; - Array.ConstrainedCopy(compressed, 0, keyData, 0, 4); - Array.ConstrainedCopy(compressed, 4, dataData, 0, 10); - DatabaseEntry key = new DatabaseEntry(keyData); - DatabaseEntry data = new DatabaseEntry(dataData); - bytesRead = (uint)(key.Data.Length + data.Data.Length); - return new KeyValuePair(key, data); - } - - /* - * Test the default compression - which is prefix compression - * of keys. This should work well with ordered keys and short - * data items. - */ - [Test] - public void TestCompressionDefault() { - testName = "TestCompressionDefault"; - SetUpTest(true); - string btreeDBName = testHome + "/" + testName + ".db"; - - BTreeDatabaseConfig cfg = new BTreeDatabaseConfig(); - cfg.Creation = CreatePolicy.ALWAYS; - BTreeDatabase db = BTreeDatabase.Open(btreeDBName, cfg); - DatabaseEntry key, data; - char[] keyData = { 'A', 'A', 'A', 'A' }; - data = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("A")); - int i; - for (i = 0; i < 5000; i++) { - // Write random data - key = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes(keyData)); - db.Put(key, data); - - // Bump the key. - int j = keyData.Length; - do { - j--; - if (keyData[j]++ == 'Z') - keyData[j] = 'A'; - } while (keyData[j] == 'A'); - } - db.Close(); - - FileInfo dbInfo = new FileInfo(btreeDBName); - long uncompressedSize = dbInfo.Length; - Configuration.ClearDir(testHome); - - cfg = new BTreeDatabaseConfig(); - cfg.Creation = CreatePolicy.ALWAYS; - cfg.SetCompression(); - db = BTreeDatabase.Open(btreeDBName, cfg); - keyData = new char[]{ 'A', 'A', 'A', 'A' }; - data = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("A")); - for (i = 0; i < 5000; i++) { - // Write random data - key = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes(keyData)); - db.Put(key, data); - - // Bump the key. - int j = keyData.Length; - do { - j--; - if (keyData[j]++ == 'Z') - keyData[j] = 'A'; - } while (keyData[j] == 'A'); - } - Cursor dbc = db.Cursor(); - foreach (KeyValuePair kvp - in dbc) - i--; - dbc.Close(); - Assert.AreEqual(i, 0); - db.Close(); - - dbInfo = new FileInfo(btreeDBName); - Assert.Less(dbInfo.Length, uncompressedSize); - Console.WriteLine( - "Uncompressed: {0}", uncompressedSize); - Console.WriteLine("Compressed: {0}", dbInfo.Length); - - Configuration.ClearDir(testHome); - - cfg = new BTreeDatabaseConfig(); - cfg.Creation = CreatePolicy.ALWAYS; - cfg.SetCompression(); - db = BTreeDatabase.Open(btreeDBName, cfg); - for (i = 1023; i < 1124; i++){ - key = new DatabaseEntry(BitConverter.GetBytes(i)); - data = new DatabaseEntry(BitConverter.GetBytes(i + 3)); - db.Put(key, data); - } - dbc = db.Cursor(); - foreach (KeyValuePair kvp in dbc){ - int keyInt = BitConverter.ToInt32(kvp.Key.Data, 0); - int dataInt = BitConverter.ToInt32(kvp.Value.Data, 0); - Assert.AreEqual(3, dataInt - keyInt); - } - dbc.Close(); - - db.Close(); - } - - [Test, ExpectedException(typeof(AccessViolationException))] - public void TestClose() - { - testName = "TestClose"; - SetUpTest(true); - string btreeDBFileName = testHome + "/" + - testName + ".db"; - - BTreeDatabaseConfig btreeDBConfig = - new BTreeDatabaseConfig(); - btreeDBConfig.Creation = CreatePolicy.ALWAYS; - BTreeDatabase btreeDB = BTreeDatabase.Open( - btreeDBFileName, btreeDBConfig); - btreeDB.Close(); - DatabaseEntry key = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("hi")); - DatabaseEntry data = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("hi")); - btreeDB.Put(key, data); - } - - [Test] - public void TestCloseWithoutSync() - { - testName = "TestCloseWithoutSync"; - SetUpTest(true); - string btreeDBName = testName + ".db"; - - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.ForceFlush = true; - envConfig.UseTxns = true; - envConfig.UseMPool = true; - envConfig.UseLogging = true; - envConfig.LogSystemCfg = new LogConfig(); - envConfig.LogSystemCfg.ForceSync = false; - envConfig.LogSystemCfg.AutoRemove = true; - DatabaseEnvironment env = DatabaseEnvironment.Open( - testHome, envConfig); - - TransactionConfig txnConfig = new TransactionConfig(); - txnConfig.SyncAction = - TransactionConfig.LogFlush.WRITE_NOSYNC; - Transaction txn = env.BeginTransaction(txnConfig); - - BTreeDatabaseConfig btreeConfig = - new BTreeDatabaseConfig(); - btreeConfig.Creation = CreatePolicy.ALWAYS; - btreeConfig.Env = env; - - BTreeDatabase btreeDB = BTreeDatabase.Open( - btreeDBName, btreeConfig, txn); - - DatabaseEntry key = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("key")); - DatabaseEntry data = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("data")); - Assert.IsFalse(btreeDB.Exists(key, txn)); - btreeDB.Put(key, data, txn); - btreeDB.Close(false); - txn.Commit(); - env.Close(); - - BTreeDatabaseConfig dbConfig = - new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.NEVER; - using (BTreeDatabase db = BTreeDatabase.Open( - testHome + "/" + btreeDBName, dbConfig)) - { - Assert.IsFalse(db.Exists(key)); - } - } - - [Test] - public void TestCursorWithoutEnv() - { - BTreeCursor cursor; - BTreeDatabase db; - string dbFileName; - - testName = "TestCursorWithoutEnv"; - SetUpTest(true); - dbFileName = testHome + "/" + testName + ".db"; - - // Open btree database. - OpenBtreeDB(null, null, dbFileName, out db); - - // Get a cursor. - cursor = db.Cursor(); - - /* - * Add a record to the database with cursor and - * confirm that the record exists in the database. - */ - CursorTest.AddOneByCursor(db, cursor); - - // Close cursor and database. - cursor.Close(); - db.Close(); - } - - [Test] - public void TestCursorWithConfigInTxn() - { - BTreeCursor cursor; - BTreeDatabase db; - DatabaseEnvironment env; - Transaction txn; - string dbFileName; - - testName = "TestCursorWithConfigInTxn"; - SetUpTest(true); - dbFileName = testName + ".db"; - - // Open environment and begin a transaction. - - SetUpEnvAndTxn(testHome, out env, out txn); - OpenBtreeDB(env, txn, dbFileName, out db); - - // Config and get a cursor. - cursor = db.Cursor(new CursorConfig(), txn); - - /* - * Add a record to the database with cursor and - * confirm that the record exists in the database. - */ - CursorTest.AddOneByCursor(db, cursor); - - /* - * Close cursor, database, commit the transaction - * and close the environment. - */ - cursor.Close(); - db.Close(); - txn.Commit(); - env.Close(); - } - - [Test] - public void TestCursorWithoutConfigInTxn() - { - BTreeCursor cursor; - BTreeDatabase db; - DatabaseEnvironment env; - Transaction txn; - string dbFileName; - - testName = "TestCursorWithoutConfigInTxn"; - SetUpTest(true); - dbFileName = testName + ".db"; - - // Open environment and begin a transaction. - SetUpEnvAndTxn(testHome, out env, out txn); - OpenBtreeDB(env, txn, dbFileName, out db); - - // Get a cursor in the transaction. - cursor = db.Cursor(txn); - - /* - * Add a record to the database with cursor and - * confirm that the record exists in the database. - */ - CursorTest.AddOneByCursor(db, cursor); - - /* - * Close cursor, database, commit the transaction - * and close the environment. - */ - cursor.Close(); - db.Close(); - txn.Commit(); - env.Close(); - } - - [Test, ExpectedException(typeof(ExpectedTestException))] - public void TestDelete() - { - testName = "TestDelete"; - SetUpTest(true); - string btreeDBFileName = testHome + "/" + - testName + ".db"; - - BTreeDatabaseConfig btreeDBConfig = - new BTreeDatabaseConfig(); - btreeDBConfig.Creation = CreatePolicy.ALWAYS; - BTreeDatabase btreeDB = BTreeDatabase.Open( - btreeDBFileName, btreeDBConfig); - DatabaseEntry key = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("key")); - DatabaseEntry data = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("data")); - btreeDB.Put(key, data); - btreeDB.Delete(key); - try - { - btreeDB.Get(key); - } - catch (NotFoundException) - { - throw new ExpectedTestException(); - } - finally - { - btreeDB.Close(); - } - } - - [Test] - public void TestDupCompare() - { - testName = "TestDupCompare"; - SetUpTest(true); - - BTreeDatabaseConfig cfg = new BTreeDatabaseConfig(); - cfg.Creation = CreatePolicy.IF_NEEDED; - cfg.Duplicates = DuplicatesPolicy.UNSORTED; - - // Do not set the duplicate comparison delegate. - using (BTreeDatabase db = BTreeDatabase.Open( - testHome + "/" + testName + ".db", cfg)) { - try { - int ret = db.DupCompare( - new DatabaseEntry( - BitConverter.GetBytes(255)), - new DatabaseEntry( - BitConverter.GetBytes(257))); - throw new TestException(); - } catch(NullReferenceException) { - } - } - - /* - * Set the duplicate comparison delegate, and verify - * the comparison delegate. - */ - cfg.DuplicateCompare = - new EntryComparisonDelegate(dbIntCompare); - using (BTreeDatabase db = BTreeDatabase.Open( - testHome + "/" + testName + "1.db", cfg)) { - int ret = db.DupCompare( - new DatabaseEntry(BitConverter.GetBytes(255)), - new DatabaseEntry(BitConverter.GetBytes(257))); - Assert.Greater(0, ret); - } - } - - [Test] - public void TestEncryption() { - testName = "TestEncryption"; - SetUpTest(true); - - BTreeDatabase db1; - - BTreeDatabaseConfig dbCfg = - new BTreeDatabaseConfig(); - dbCfg.Creation = CreatePolicy.IF_NEEDED; - - // Open an encrypted database. - dbCfg.SetEncryption("bdb", EncryptionAlgorithm.AES); - using (db1 = BTreeDatabase.Open( - testHome + "/" + testName + ".db", dbCfg)) { - Assert.IsTrue(db1.Encrypted); - for (int i = 0; i < 10; i++) - db1.Put(new DatabaseEntry - (BitConverter.GetBytes(i)), - new DatabaseEntry( - BitConverter.GetBytes(i))); - } - - // Verify the database is encrypted. - BTreeDatabaseConfig verifyDbCfg = - new BTreeDatabaseConfig(); - verifyDbCfg.Creation = CreatePolicy.IF_NEEDED; - verifyDbCfg.SetEncryption( - dbCfg.EncryptionPassword, - dbCfg.EncryptAlgorithm); - verifyDbCfg.Encrypted = true; - using (db1 = BTreeDatabase.Open( - testHome + "/" + testName + ".db", - verifyDbCfg)) { - for (int i = 0; i < 10; i++) - db1.Get(new DatabaseEntry( - BitConverter.GetBytes(i))); - }; - } - - [Test] - public void TestExist() - { - testName = "TestExist"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - - BTreeDatabaseConfig btreeDBConfig = - new BTreeDatabaseConfig(); - btreeDBConfig.Creation = CreatePolicy.IF_NEEDED; - BTreeDatabase btreeDB; - using (btreeDB = BTreeDatabase.Open( - dbFileName, btreeDBConfig)) - { - DatabaseEntry key = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("key")); - DatabaseEntry data = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("data")); - - btreeDB.Put(key, data); - Assert.IsTrue(btreeDB.Exists( - new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("key")))); - Assert.IsFalse(btreeDB.Exists( - new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("data")))); - } - } - - [Test] - public void TestExistWithTxn() - { - BTreeDatabase btreeDB; - Transaction txn; - DatabaseEnvironmentConfig envConfig; - DatabaseEnvironment env; - DatabaseEntry key, data; - - testName = "TestExistWithTxn"; - SetUpTest(true); - - // Open an environment. - envConfig = new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseMPool = true; - envConfig.UseTxns = true; - env = DatabaseEnvironment.Open(testHome, envConfig); - - // Begin a transaction. - txn = env.BeginTransaction(); - - // Open a database. - BTreeDatabaseConfig btreeDBConfig = - new BTreeDatabaseConfig(); - btreeDBConfig.Creation = CreatePolicy.IF_NEEDED; - btreeDBConfig.Env = env; - btreeDB = BTreeDatabase.Open(testName + ".db", - btreeDBConfig, txn); - - // Put key data pair into database. - key = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("key")); - data = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("data")); - btreeDB.Put(key, data, txn); - - // Confirm that the pair exists in the database. - Assert.IsTrue(btreeDB.Exists(new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("key")), txn)); - Assert.IsFalse(btreeDB.Exists(new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("data")), txn)); - - // Dispose all. - btreeDB.Close(); - txn.Commit(); - env.Close(); - } - - [Test] - public void TestExistWithLockingInfo() - { - BTreeDatabase btreeDB; - DatabaseEnvironment env; - DatabaseEntry key, data; - - testName = "TestExistWithLockingInfo"; - SetUpTest(true); - - // Open the environment. - DatabaseEnvironmentConfig envCfg = - new DatabaseEnvironmentConfig(); - envCfg.Create = true; - envCfg.FreeThreaded = true; - envCfg.UseLocking = true; - envCfg.UseLogging = true; - envCfg.UseMPool = true; - envCfg.UseTxns = true; - env = DatabaseEnvironment.Open( - testHome, envCfg); - - // Open database in transaction. - Transaction openTxn = env.BeginTransaction(); - BTreeDatabaseConfig cfg = - new BTreeDatabaseConfig(); - cfg.Creation = CreatePolicy.ALWAYS; - cfg.Env = env; - cfg.FreeThreaded = true; - cfg.PageSize = 4096; - cfg.Duplicates = DuplicatesPolicy.UNSORTED; - btreeDB = BTreeDatabase.Open(testName + ".db", - cfg, openTxn); - openTxn.Commit(); - - // Put key data pair into database. - Transaction txn = env.BeginTransaction(); - key = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("key")); - data = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("data")); - btreeDB.Put(key, data, txn); - - // Confirm that the pair exists in the database with LockingInfo. - LockingInfo lockingInfo = new LockingInfo(); - lockingInfo.ReadModifyWrite = true; - - // Confirm that the pair exists in the database. - Assert.IsTrue(btreeDB.Exists(new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("key")), txn, lockingInfo)); - Assert.IsFalse(btreeDB.Exists(new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("data")), txn, lockingInfo)); - txn.Commit(); - - btreeDB.Close(); - env.Close(); - } - - [Test] - public void TestGetByKey() - { - testName = "TestGetByKey"; - SetUpTest(true); - string btreeDBFileName = testHome + "/" + - testName + ".db"; - string btreeDBName = - Path.GetFileNameWithoutExtension(btreeDBFileName); - - BTreeDatabaseConfig btreeDBConfig = - new BTreeDatabaseConfig(); - btreeDBConfig.Creation = CreatePolicy.ALWAYS; - BTreeDatabase btreeDB = BTreeDatabase.Open( - btreeDBFileName, btreeDBName, btreeDBConfig); - - DatabaseEntry key = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("key")); - DatabaseEntry data = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("data")); - btreeDB.Put(key, data); - - KeyValuePair pair = - new KeyValuePair(); - pair = btreeDB.Get(key); - Assert.AreEqual(pair.Key.Data, key.Data); - Assert.AreEqual(pair.Value.Data, data.Data); - btreeDB.Close(); - } - - [Test] - public void TestGetByRecno() - { - testName = "TestGetByRecno"; - SetUpTest(true); - string btreeDBFileName = testHome + "/" + - testName + ".db"; - string btreeDBName = - Path.GetFileNameWithoutExtension(btreeDBFileName); - - BTreeDatabaseConfig btreeDBConfig = - new BTreeDatabaseConfig(); - btreeDBConfig.Creation = CreatePolicy.ALWAYS; - btreeDBConfig.UseRecordNumbers = true; - BTreeDatabase btreeDB = BTreeDatabase.Open( - btreeDBFileName, btreeDBName, btreeDBConfig); - Assert.IsTrue(btreeDB.RecordNumbers); - - DatabaseEntry key = new DatabaseEntry(); - DatabaseEntry data = new DatabaseEntry(); - uint recno, count, value; - for (recno = 1; recno <= 100; recno++) - { - value = 200 - recno; - Configuration.dbtFromString(key, - Convert.ToString(value)); - Configuration.dbtFromString(data, - Convert.ToString(value)); - btreeDB.Put(key, data); - } - - KeyValuePair pair = - new KeyValuePair(); - - for (count = 1; ; count++) - { - try - { - pair = btreeDB.Get(count); - } - catch (NotFoundException) - { - Assert.AreEqual(101, count); - break; - } - value = 299 - 200 + count; - Assert.AreEqual(value.ToString(), - Configuration.strFromDBT(pair.Key)); - } - - btreeDB.Close(); - } - - [Test, ExpectedException(typeof(NotFoundException))] - public void TestGetBoth() - { - testName = "TestGetBoth"; - SetUpTest(true); - string btreeDBFileName = testHome + "/" + - testName + ".db"; - string btreeDBName = - Path.GetFileNameWithoutExtension(btreeDBFileName); - - BTreeDatabaseConfig btreeDBConfig = - new BTreeDatabaseConfig(); - btreeDBConfig.Creation = CreatePolicy.ALWAYS; - using (BTreeDatabase btreeDB = BTreeDatabase.Open( - btreeDBFileName, btreeDBName, btreeDBConfig)) - { - DatabaseEntry key = new DatabaseEntry(); - DatabaseEntry data = new DatabaseEntry(); - - Configuration.dbtFromString(key, "key"); - Configuration.dbtFromString(data, "data"); - btreeDB.Put(key, data); - KeyValuePair pair = - new KeyValuePair(); - pair = btreeDB.GetBoth(key, data); - Assert.AreEqual(key.Data, pair.Key.Data); - Assert.AreEqual(data.Data, pair.Value.Data); - - Configuration.dbtFromString(key, "key"); - Configuration.dbtFromString(data, "key"); - btreeDB.GetBoth(key, data); - } - } - - [Test] - public void TestGetBothMultiple() - { - testName = "TestGetBothMultiple"; - SetUpTest(true); - string btreeDBFileName = testHome + "/" + - testName + ".db"; - string btreeDBName = testName; - DatabaseEntry key, data; - KeyValuePair kvp; - int cnt; - - BTreeDatabaseConfig btreeDBConfig = - new BTreeDatabaseConfig(); - btreeDBConfig.Creation = CreatePolicy.ALWAYS; - btreeDBConfig.Duplicates = DuplicatesPolicy.UNSORTED; - btreeDBConfig.PageSize = 1024; - using (BTreeDatabase btreeDB = GetMultipleDB( - btreeDBFileName, btreeDBName, btreeDBConfig)) { - key = new DatabaseEntry(BitConverter.GetBytes(100)); - data = new DatabaseEntry(BitConverter.GetBytes(100)); - - kvp = btreeDB.GetBothMultiple(key, data); - cnt = 0; - foreach (DatabaseEntry dbt in kvp.Value) - cnt++; - Assert.AreEqual(cnt, 10); - - kvp = btreeDB.GetBothMultiple(key, data, 1024); - cnt = 0; - foreach (DatabaseEntry dbt in kvp.Value) - cnt++; - Assert.AreEqual(cnt, 10); - } - } - - [Test] - public void TestGetMultiple() - { - testName = "TestGetMultiple"; - SetUpTest(true); - string btreeDBFileName = testHome + "/" + - testName + ".db"; - string btreeDBName = testName; - - BTreeDatabaseConfig btreeDBConfig = - new BTreeDatabaseConfig(); - btreeDBConfig.Creation = CreatePolicy.ALWAYS; - btreeDBConfig.Duplicates = DuplicatesPolicy.UNSORTED; - btreeDBConfig.PageSize = 512; - - using (BTreeDatabase btreeDB = GetMultipleDB( - btreeDBFileName, btreeDBName, btreeDBConfig)) { - DatabaseEntry key = new DatabaseEntry( - BitConverter.GetBytes(10)); - KeyValuePair kvp = - btreeDB.GetMultiple(key, 1024); - int cnt = 0; - foreach (DatabaseEntry dbt in kvp.Value) - cnt++; - Assert.AreEqual(cnt, 10); - - key = new DatabaseEntry( - BitConverter.GetBytes(102)); - kvp = btreeDB.GetMultiple(key, 1024); - cnt = 0; - foreach (DatabaseEntry dbt in kvp.Value) - cnt++; - Assert.AreEqual(cnt, 1); - } - } - - [Test] - public void TestGetMultipleByRecno() - { - testName = "TestGetMultipleByRecno"; - SetUpTest(true); - string btreeDBFileName = testHome + "/" + - testName + ".db"; - string btreeDBName = - Path.GetFileNameWithoutExtension(btreeDBFileName); - - BTreeDatabaseConfig btreeDBConfig = - new BTreeDatabaseConfig(); - btreeDBConfig.Creation = CreatePolicy.ALWAYS; - btreeDBConfig.Duplicates = DuplicatesPolicy.NONE; - btreeDBConfig.UseRecordNumbers = true; - using (BTreeDatabase btreeDB = GetMultipleDB( - btreeDBFileName, btreeDBName, btreeDBConfig)) { - int recno = 44; - KeyValuePair kvp = - btreeDB.GetMultiple((uint)recno); - int cnt = 0; - int kdata = BitConverter.ToInt32(kvp.Key.Data, 0); - Assert.AreEqual(kdata, recno); - foreach (DatabaseEntry dbt in kvp.Value) { - cnt++; - int ddata = BitConverter.ToInt32(dbt.Data, 0); - Assert.AreEqual(ddata, recno); - } - Assert.AreEqual(cnt, 1); - } - } - - [Test] - public void TestGetMultipleByRecnoInSize() - { - testName = "TestGetMultipleByRecnoInSize"; - SetUpTest(true); - string btreeDBFileName = testHome + "/" + - testName + ".db"; - string btreeDBName = - Path.GetFileNameWithoutExtension(btreeDBFileName); - - BTreeDatabaseConfig btreeDBConfig = - new BTreeDatabaseConfig(); - btreeDBConfig.Creation = CreatePolicy.ALWAYS; - btreeDBConfig.Duplicates = DuplicatesPolicy.NONE; - btreeDBConfig.UseRecordNumbers = true; - btreeDBConfig.PageSize = 512; - using (BTreeDatabase btreeDB = GetMultipleDB( - btreeDBFileName, btreeDBName, btreeDBConfig)) { - int recno = 100; - int bufferSize = 1024; - KeyValuePair kvp = - btreeDB.GetMultiple((uint)recno, bufferSize); - int cnt = 0; - int kdata = BitConverter.ToInt32(kvp.Key.Data, 0); - Assert.AreEqual(kdata, recno); - foreach (DatabaseEntry dbt in kvp.Value) { - cnt++; - Assert.AreEqual(dbt.Data.Length, 111); - } - Assert.AreEqual(1, cnt); - } - } - - [Test] - public void TestGetMultipleInSize() - { - testName = "TestGetMultipleInSize"; - SetUpTest(true); - string btreeDBFileName = testHome + "/" + - testName + ".db"; - string btreeDBName = - Path.GetFileNameWithoutExtension(btreeDBFileName); - - BTreeDatabaseConfig btreeDBConfig = - new BTreeDatabaseConfig(); - btreeDBConfig.Creation = CreatePolicy.ALWAYS; - btreeDBConfig.Duplicates = DuplicatesPolicy.UNSORTED; - btreeDBConfig.PageSize = 1024; - using (BTreeDatabase btreeDB = GetMultipleDB( - btreeDBFileName, btreeDBName, btreeDBConfig)) { - - int num = 101; - DatabaseEntry key = new DatabaseEntry( - BitConverter.GetBytes(num)); - int bufferSize = 10240; - KeyValuePair kvp = - btreeDB.GetMultiple(key, bufferSize); - int cnt = 0; - foreach (DatabaseEntry dbt in kvp.Value) { - cnt++; - Assert.AreEqual(BitConverter.ToInt32( - dbt.Data, 0), num); - num++; - } - Assert.AreEqual(cnt, 923); - } - } - - [Test] - public void TestGetWithTxn() - { - testName = "TestGetWithTxn"; - SetUpTest(true); - - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseLogging = true; - envConfig.UseMPool = true; - envConfig.UseTxns = true; - DatabaseEnvironment env = DatabaseEnvironment.Open( - testHome, envConfig); - - try - { - Transaction openTxn = env.BeginTransaction(); - BTreeDatabaseConfig dbConfig = - new BTreeDatabaseConfig(); - dbConfig.Env = env; - dbConfig.Creation = CreatePolicy.IF_NEEDED; - BTreeDatabase db = BTreeDatabase.Open( - testName + ".db", dbConfig, openTxn); - openTxn.Commit(); - - Transaction putTxn = env.BeginTransaction(); - try - { - for (int i = 0; i < 20; i++) - db.Put(new DatabaseEntry( - BitConverter.GetBytes(i)), - new DatabaseEntry( - BitConverter.GetBytes(i)), putTxn); - putTxn.Commit(); - } - catch (DatabaseException e) - { - putTxn.Abort(); - db.Close(); - throw e; - } - - Transaction getTxn = env.BeginTransaction(); - KeyValuePair pair; - try - { - for (int i = 0; i < 20; i++) - { - pair = db.Get(new DatabaseEntry( - BitConverter.GetBytes(i)), getTxn); - Assert.AreEqual(BitConverter.GetBytes(i), - pair.Key.Data); - } - - getTxn.Commit(); - db.Close(); - } - catch (DatabaseException) - { - getTxn.Abort(); - db.Close(); - throw new TestException(); - } - } - catch (DatabaseException) - { - } - finally - { - env.Close(); - } - } - - [Test] - public void TestKeyRange() - { - testName = "TestKeyRange"; - SetUpTest(true); - string btreeDBFileName = testHome + "/" + - testName + ".db"; - string btreeDBName = Path.GetFileNameWithoutExtension( - btreeDBFileName); - - BTreeDatabaseConfig btreeDBConfig = - new BTreeDatabaseConfig(); - btreeDBConfig.Creation = CreatePolicy.ALWAYS; - BTreeDatabase btreeDB = BTreeDatabase.Open( - btreeDBFileName, btreeDBName, btreeDBConfig); - - DatabaseEntry key = new DatabaseEntry(); - DatabaseEntry data = new DatabaseEntry(); - uint recno; - for (recno = 1; recno <= 10; recno++) - { - Configuration.dbtFromString(key, - Convert.ToString(recno)); - Configuration.dbtFromString(data, - Convert.ToString(recno)); - btreeDB.Put(key, data); - } - - Configuration.dbtFromString(key, Convert.ToString(5)); - KeyRange keyRange = btreeDB.KeyRange(key); - Assert.AreEqual(0.5, keyRange.Less); - Assert.AreEqual(0.1, keyRange.Equal); - Assert.AreEqual(0.4, keyRange.Greater); - - btreeDB.Close(); - } - - [Test] - public void TestNoWaitDbExclusiveLock() - { - testName = "TestNoWaitDbExclusiveLock"; - SetUpTest(true); - - // Open an environment. - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.AutoCommit = true; - envConfig.Create = true; - envConfig.UseMPool = true; - envConfig.UseLocking = true; - envConfig.UseLogging = true; - envConfig.UseTxns = true; - DatabaseEnvironment env = DatabaseEnvironment.Open( - testHome, envConfig); - - // Open a database. - BTreeDatabaseConfig btreeDBConfig = - new BTreeDatabaseConfig(); - btreeDBConfig.Env = env; - string btreeDBFileName = testName + ".db"; - BTreeDatabase btreeDB; - - btreeDBConfig.Creation = CreatePolicy.IF_NEEDED; - btreeDB = BTreeDatabase.Open( - btreeDBFileName, btreeDBConfig); - Assert.IsNull(btreeDB.NoWaitDbExclusiveLock); - btreeDB.Close(); - - btreeDBConfig.NoWaitDbExclusiveLock = false; - btreeDB = BTreeDatabase.Open( - btreeDBFileName, btreeDBConfig); - Assert.AreEqual(btreeDB.NoWaitDbExclusiveLock, false); - btreeDB.Close(); - - btreeDBConfig.NoWaitDbExclusiveLock = true; - btreeDB = BTreeDatabase.Open( - btreeDBFileName, btreeDBConfig); - Assert.AreEqual(btreeDB.NoWaitDbExclusiveLock, true); - btreeDB.Close(); - env.Close(); - } - - [Test] - public void TestOpenExistingBtreeDB() - { - testName = "TestOpenExistingBtreeDB"; - SetUpTest(true); - string btreeDBFileName = testHome + "/" + - testName + ".db"; - - BTreeDatabaseConfig btreeConfig = - new BTreeDatabaseConfig(); - btreeConfig.Creation = CreatePolicy.ALWAYS; - BTreeDatabase btreeDB = BTreeDatabase.Open( - btreeDBFileName, btreeConfig); - btreeDB.Close(); - - DatabaseConfig dbConfig = new DatabaseConfig(); - Database db = Database.Open(btreeDBFileName, - dbConfig); - Assert.AreEqual(db.Type, DatabaseType.BTREE); - Assert.AreEqual(db.Creation, CreatePolicy.NEVER); - db.Close(); - } - - [Test] - public void TestOpenNewBtreeDB() - { - testName = "TestOpenNewBtreeDB"; - SetUpTest(true); - string btreeDBFileName = testHome + "/" + - testName + ".db"; - - XmlElement xmlElem = Configuration.TestSetUp( - testFixtureName, testName); - BTreeDatabaseConfig btreeConfig = - new BTreeDatabaseConfig(); - BTreeDatabaseConfigTest.Config(xmlElem, - ref btreeConfig, true); - BTreeDatabase btreeDB = BTreeDatabase.Open( - btreeDBFileName, btreeConfig); - Confirm(xmlElem, btreeDB, true); - btreeDB.Close(); - } - - [Test] - public void TestOpenMulDBInSingleFile() - { - testName = "TestOpenMulDBInSingleFile"; - SetUpTest(true); - string btreeDBFileName = testHome + "/" + - testName + ".db"; - string[] btreeDBArr = new string[4]; - - for (int i = 0; i < 4; i++) - btreeDBArr[i] = Path.GetFileNameWithoutExtension( - btreeDBFileName) + i; - - Configuration.ClearDir(testHome); - - BTreeDatabaseConfig btreeDBConfig = - new BTreeDatabaseConfig(); - btreeDBConfig.Creation = CreatePolicy.IF_NEEDED; - - BTreeDatabase btreeDB; - for (int i = 0; i < 4; i++) - { - btreeDB = BTreeDatabase.Open(btreeDBFileName, - btreeDBArr[i], btreeDBConfig); - Assert.AreEqual(CreatePolicy.IF_NEEDED, btreeDB.Creation); - btreeDB.Close(); - } - - DatabaseConfig dbConfig = new DatabaseConfig(); - Database db; - for (int i = 0; i < 4; i++) - { - using (db = Database.Open(btreeDBFileName, - btreeDBArr[i], dbConfig)) - { - Assert.AreEqual(btreeDBArr[i], - db.DatabaseName); - Assert.AreEqual(DatabaseType.BTREE, - db.Type); - } - } - } - - [Test] - public void TestOpenWithTxn() - { - testName = "TestOpenWithTxn"; - SetUpTest(true); - string btreeDBName = testName + ".db"; - - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseTxns = true; - envConfig.UseMPool = true; - - DatabaseEnvironment env = DatabaseEnvironment.Open( - testHome, envConfig); - Transaction txn = env.BeginTransaction( - new TransactionConfig()); - - BTreeDatabaseConfig btreeConfig = - new BTreeDatabaseConfig(); - btreeConfig.Creation = CreatePolicy.ALWAYS; - btreeConfig.Env = env; - - /* - * If environmnet home is set, the file name in Open() - * is the relative path. - */ - BTreeDatabase btreeDB = BTreeDatabase.Open( - btreeDBName, btreeConfig, txn); - Assert.IsTrue(btreeDB.Transactional); - btreeDB.Close(); - txn.Commit(); - env.Close(); - } - - [Test] - public void TestPartialGet() - { - testName = "TestPartialGet"; - SetUpTest(true); - - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseTxns = true; - envConfig.UseMPool = true; - DatabaseEnvironment env = DatabaseEnvironment.Open( - testHome, envConfig); - - BTreeDatabase db; - PopulateDb(env, out db); - - // Partially get the first byte and verify it is 'h'. - DatabaseEntry key, data; - KeyValuePair pair; - Transaction txn = env.BeginTransaction(); - for (int i = 0; i < 100; i++) { - key = new DatabaseEntry(BitConverter.GetBytes(i)); - data = new DatabaseEntry(0, 1); - pair = db.Get(key, data, txn); - Assert.AreEqual(1, pair.Value.Data.Length); - Assert.AreEqual((byte)'h', pair.Value.Data[0]); - } - txn.Commit(); - db.Close(); - env.Close(); - - /* - * Open the existing database and partially get - * non-existing data. - */ - using (Database edb = Database.Open( - testHome + "/" + testName + ".db", - new DatabaseConfig())) { - key = new DatabaseEntry(BitConverter.GetBytes(0)); - data = new DatabaseEntry(10, 1); - pair = edb.Get(key, data); - Assert.AreEqual(null, pair.Value.Data); - } - } - - [Test] - public void TestPartialPut() - { - testName = "TestPartialPut"; - SetUpTest(true); - - BTreeDatabase db; - PopulateDb(null, out db); - - // Partially update the records. - DatabaseEntry key, data; - KeyValuePair pair; - byte[] partialBytes = ASCIIEncoding.ASCII.GetBytes("aa"); - byte[] valueBytes; - for (int i = 0; i < 100; i++) { - key = new DatabaseEntry(BitConverter.GetBytes(i)); - if (i < 50) - data = new DatabaseEntry( - partialBytes, 0, 2); - else { - data = new DatabaseEntry(partialBytes); - data.Partial = true; - data.PartialLen = 2; - data.PartialOffset = 0; - } - Assert.AreEqual(0, data.PartialOffset); - Assert.AreEqual(2, data.PartialLen); - Assert.AreEqual(true, data.Partial); - db.Put(key, data); - } - // Verify that the records have been partially changed. - valueBytes = ASCIIEncoding.ASCII.GetBytes("hello"); - valueBytes[0] = (byte)'a'; - valueBytes[1] = (byte)'a'; - for (int i = 0; i < 100; i++) { - key = new DatabaseEntry(BitConverter.GetBytes(i)); - pair = db.Get(key); - Assert.AreEqual( - BitConverter.GetBytes(i), pair.Key.Data); - Assert.AreEqual(valueBytes, pair.Value.Data); - } - - // Partial put from non-existing offset. - key = new DatabaseEntry(BitConverter.GetBytes(0)); - data = new DatabaseEntry(partialBytes, 100, 2); - db.Put(key, data); - - // Verify that the records have been partially changed. - pair = db.Get(key); - Assert.AreEqual(102, pair.Value.Data.Length); - Assert.AreEqual(valueBytes[0], - pair.Value.Data[100]); - Assert.AreEqual(valueBytes[0], - pair.Value.Data[101]); - - /* - * Reuse the DatabaseEntry for non-partial. The partial - * length and offset should be ignored. - */ - key = new DatabaseEntry(BitConverter.GetBytes(0)); - data.Partial = false; - db.Put(key, data); - pair = db.Get(key); - Assert.AreEqual(2, pair.Value.Data.Length); - - db.Close(); - } - - private void PopulateDb(DatabaseEnvironment env, - out BTreeDatabase db) - { - DatabaseEntry key, data; - Transaction txn = null; - string dbName; - - if (env != null) { - txn = env.BeginTransaction(); - dbName = testName + ".db"; - } else - dbName = testHome + "/" + testName + ".db"; - - OpenBtreeDB(env, txn, dbName, out db); - - for (int i = 0; i < 100; i++) { - key = new DatabaseEntry( - BitConverter.GetBytes(i)); - data = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("hello")); - db.Put(key, data, txn); - } - - if (txn != null) - txn.Commit(); - } - - [Test] - public void TestPrefixCompare() - { - testName = "TestPrefixCompare"; - SetUpTest(true); - string btreeDBFileName = testHome + "/" + - testName + ".db"; - - BTreeDatabaseConfig dbConfig = - new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Duplicates = DuplicatesPolicy.SORTED; - dbConfig.BTreeCompare = - new EntryComparisonDelegate(dbIntCompare); - dbConfig.BTreePrefixCompare = - new EntryPrefixComparisonDelegate(dbPrefixCompare); - BTreeDatabase db = BTreeDatabase.Open( - btreeDBFileName, dbConfig); - - Assert.AreEqual(3, db.PrefixCompare(new DatabaseEntry( - BitConverter.GetBytes((Int16)255)), new DatabaseEntry( - BitConverter.GetBytes((Int32)65791)))); - - Assert.AreEqual(1, db.PrefixCompare(new DatabaseEntry( - BitConverter.GetBytes((Int64)255)), new DatabaseEntry( - BitConverter.GetBytes((Int64)4294967552)))); - - db.Close(); - } - - [Test] - public void TestPutMultiple() - { - testName = "TestPutMultiple"; - SetUpTest(true); - - PutMultiple(null); - - Configuration.ClearDir(testHome); - DatabaseEnvironmentConfig cfg = - new DatabaseEnvironmentConfig(); - cfg.Create = true; - cfg.UseLogging = true; - cfg.UseMPool = true; - cfg.UseTxns = true; - DatabaseEnvironment env = DatabaseEnvironment.Open( - testHome, cfg); - PutMultiple(env); - env.Close(); - } - - private void PutMultiple(DatabaseEnvironment env) - { - List kList = new List(); - List vList = new List(); - BTreeDatabase db; - DatabaseEntry key, value; - Transaction txn; - string dbFileName = (env == null) ? testHome + - "/" + testName + ".db" : testName + ".db"; - int i; - - BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - if (env != null) { - dbConfig.Env = env; - txn = env.BeginTransaction(); - db = BTreeDatabase.Open( - dbFileName, dbConfig, txn); - txn.Commit(); - } else - db = BTreeDatabase.Open(dbFileName, dbConfig); - - for (i = 0; i < 100; i++) { - key = new DatabaseEntry( - BitConverter.GetBytes(i)); - value = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("data" + i + - Configuration.RandomString(512))); - kList.Add(key); - vList.Add(value); - } - - // Create bulk buffer for non-recno based keys. - MultipleDatabaseEntry keyBuff = - new MultipleDatabaseEntry(kList, false); - Assert.IsFalse(keyBuff.Recno); - - // Create bulk buffer for values. - MultipleDatabaseEntry valBuff = - new MultipleDatabaseEntry(vList, false); - i = 0; - foreach (DatabaseEntry dbt in valBuff) { - Assert.AreEqual(vList[i].Data, dbt.Data); - i++; - } - Assert.AreEqual(100, i); - - // Create bulk buffer from another key buffer. - MultipleDatabaseEntry keyBuff1 = - new MultipleDatabaseEntry( - keyBuff.Data, keyBuff.Recno); - i = 0; - foreach (DatabaseEntry dbt in keyBuff1) { - Assert.AreEqual(kList[i].Data, dbt.Data); - i++; - } - Assert.AreEqual(100, i); - - if (env != null) { - txn = env.BeginTransaction(); - db.Put(keyBuff, valBuff, txn); - Assert.AreEqual(100, db.Truncate(txn)); - txn.Commit(); - } else { - /* - * Bulk insert to database with key and value - * buffers. - */ - db.Put(keyBuff, valBuff); - - // Verify all records exist as expected. - Cursor cursor = db.Cursor(); - i = 99; - Assert.IsTrue(cursor.MoveLast()); - Assert.AreEqual(kList[i].Data, - cursor.Current.Key.Data); - Assert.AreEqual(vList[i].Data, - cursor.Current.Value.Data); - while (cursor.MovePrev()) { - i--; - Assert.AreEqual(kList[i].Data, - cursor.Current.Key.Data); - Assert.AreEqual(vList[i].Data, - cursor.Current.Value.Data); - } - Assert.AreEqual(0, i); - cursor.Close(); - /* - * Dumped all records. The number of records - * should be 100. - */ - Assert.AreEqual(100, db.Truncate()); - - /* - * Bulk insert to database with a copied key - * buffer and a value buffer. - */ - db.Put(keyBuff1, valBuff); - cursor = db.Cursor(); - Assert.IsTrue(cursor.MoveLast()); - i = 99; - Assert.AreEqual(kList[i].Data, - cursor.Current.Key.Data); - Assert.AreEqual(vList[i].Data, - cursor.Current.Value.Data); - while (cursor.MovePrev()) { - i--; - Assert.AreEqual(kList[i].Data, - cursor.Current.Key.Data); - Assert.AreEqual(vList[i].Data, - cursor.Current.Value.Data); - } - cursor.Close(); - Assert.AreEqual(0, i); - /* - * Dumped all records. The number of records - * should be 100. - */ - Assert.AreEqual(100, db.Truncate()); - } - - db.Close(); - } - - [Test] - public void TestPutMultipleKey() - { - testName = "TestPutMultipleKey"; - SetUpTest(true); - - PutMultipleKey(null); - - Configuration.ClearDir(testHome); - DatabaseEnvironmentConfig cfg = - new DatabaseEnvironmentConfig(); - cfg.Create = true; - cfg.UseLogging = true; - cfg.UseMPool = true; - cfg.UseTxns = true; - DatabaseEnvironment env = DatabaseEnvironment.Open( - testHome, cfg); - PutMultipleKey(env); - env.Close(); - } - - private void PutMultipleKey(DatabaseEnvironment env) - { - List> pList = - new List>(); - BTreeDatabase db; - DatabaseEntry key, value; - Transaction txn; - string dbFileName = (env == null) ? testHome + - "/" + testName + ".db" : testName + ".db"; - int i; - - BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - if (env != null) { - dbConfig.Env = env; - txn = env.BeginTransaction(); - db = BTreeDatabase.Open( - dbFileName, dbConfig, txn); - txn.Commit(); - } else - db = BTreeDatabase.Open(dbFileName, dbConfig); - - for (i = 0; i < 100; i++) { - key = new DatabaseEntry( - BitConverter.GetBytes(i)); - value = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("data" + - i + Configuration.RandomString(512))); - pList.Add(new KeyValuePair< - DatabaseEntry, DatabaseEntry>(key, value)); - } - - // Create btree bulk buffer for key/value pairs. - MultipleKeyDatabaseEntry pairBuff = - new MultipleKeyDatabaseEntry(pList, false); - i = 0; - foreach (KeyValuePair - pair in pairBuff) { - Assert.AreEqual(pList[i].Key.Data, - pair.Key.Data); - Assert.AreEqual(pList[i].Value.Data, - pair.Value.Data); - i++; - } - Assert.AreEqual(100, i); - - /* - * Create bulk buffer from another key/value pairs - * bulk buffer. - */ - MultipleKeyDatabaseEntry pairBuff1 = - new MultipleKeyDatabaseEntry( - pairBuff.Data, false); - Assert.AreEqual(false, pairBuff1.Recno); - i = 0; - foreach (KeyValuePair - pair in pairBuff1) { - Assert.AreEqual(pList[i].Key.Data, - pair.Key.Data); - Assert.AreEqual(pList[i].Value.Data, - pair.Value.Data); - i++; - } - Assert.AreEqual(100, i); - - if (env == null) { - // Bulk insert with key/value pair bulk buffer. - db.Put(pairBuff); - Cursor cursor = db.Cursor(); - Assert.IsTrue(cursor.MoveFirst()); - i = 0; - Assert.AreEqual(pList[i].Key.Data, - cursor.Current.Key.Data); - Assert.AreEqual(pList[i].Value.Data, - cursor.Current.Value.Data); - while (cursor.MoveNext()) { - i++; - Assert.AreEqual(pList[i].Key.Data, - cursor.Current.Key.Data); - Assert.AreEqual(pList[i].Value.Data, - cursor.Current.Value.Data); - } - Assert.AreEqual(99, i); - cursor.Close(); - /* - * Dump all records from the database. The - * number of records should be 100. - */ - Assert.AreEqual(100, db.Truncate()); - - // Bulk insert with copied key/value pair buffer. - db.Put(pairBuff1); - cursor = db.Cursor(); - Assert.IsTrue(cursor.MoveFirst()); - i = 0; - Assert.AreEqual(pList[i].Key.Data, - cursor.Current.Key.Data); - Assert.AreEqual(pList[i].Value.Data, - cursor.Current.Value.Data); - while (cursor.MoveNext()) { - i++; - Assert.AreEqual(pList[i].Key.Data, - cursor.Current.Key.Data); - Assert.AreEqual(pList[i].Value.Data, - cursor.Current.Value.Data); - } - Assert.AreEqual(99, i); - cursor.Close(); - Assert.AreEqual(100, db.Truncate()); - } else { - txn = env.BeginTransaction(); - db.Put(pairBuff, txn); - Assert.AreEqual(100, db.Truncate(txn)); - txn.Commit(); - } - - db.Close(); - } - - [Test] - public void TestPutWithoutTxn() - { - testName = "TestPutWithoutTxn"; - SetUpTest(true); - string btreeDBFileName = testHome + "/" + - testName + ".db"; - - BTreeDatabaseConfig btreeDBConfig = - new BTreeDatabaseConfig(); - btreeDBConfig.Creation = CreatePolicy.ALWAYS; - DatabaseEntry key, data; - KeyValuePair pair; - using (BTreeDatabase btreeDB = BTreeDatabase.Open( - btreeDBFileName, btreeDBConfig)) - { - // Put integer into database - key = new DatabaseEntry( - BitConverter.GetBytes((int)0)); - data = new DatabaseEntry( - BitConverter.GetBytes((int)0)); - btreeDB.Put(key, data); - pair = btreeDB.Get(key); - Assert.AreEqual(key.Data, pair.Key.Data); - Assert.AreEqual(data.Data, pair.Value.Data); - - // Partial put to update the existing pair. - data.Partial = true; - data.PartialLen = 4; - data.PartialOffset = 4; - btreeDB.Put(key, data); - pair = btreeDB.Get(key); - Assert.AreEqual(key.Data, pair.Key.Data); - Assert.AreEqual(8, pair.Value.Data.Length); - } - } - - [Test, ExpectedException(typeof(KeyExistException))] - public void TestPutNoOverWrite() - { - testName = "TestPutNoOverWrite"; - SetUpTest(true); - string btreeDBFileName = testHome + "/" + - testName + ".db"; - - BTreeDatabaseConfig btreeDBConfig = - new BTreeDatabaseConfig(); - btreeDBConfig.Creation = CreatePolicy.ALWAYS; - DatabaseEntry key, data, newData; - using (BTreeDatabase btreeDB = BTreeDatabase.Open( - btreeDBFileName, btreeDBConfig)) - { - key = new DatabaseEntry( - BitConverter.GetBytes((int)0)); - data = new DatabaseEntry( - BitConverter.GetBytes((int)0)); - newData = new DatabaseEntry( - BitConverter.GetBytes((int)1)); - - btreeDB.Put(key, data); - btreeDB.PutNoOverwrite(key, newData); - } - } - - [Test, ExpectedException(typeof(ExpectedTestException))] - public void TestPutNoDuplicateWithTxn() - { - testName = "TestPutNoDuplicateWithTxn"; - SetUpTest(true); - - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseLogging = true; - envConfig.UseMPool = true; - envConfig.UseTxns = true; - DatabaseEnvironment env = DatabaseEnvironment.Open( - testHome, envConfig); - - Transaction txn = env.BeginTransaction(); - BTreeDatabaseConfig btreeDBConfig = - new BTreeDatabaseConfig(); - btreeDBConfig.Creation = CreatePolicy.ALWAYS; - btreeDBConfig.Env = env; - btreeDBConfig.Duplicates = DuplicatesPolicy.SORTED; - - DatabaseEntry key, data; - try - { - using (BTreeDatabase btreeDB = - BTreeDatabase.Open( - testName + ".db", btreeDBConfig, txn)) - { - key = new DatabaseEntry( - BitConverter.GetBytes((int)0)); - data = new DatabaseEntry( - BitConverter.GetBytes((int)0)); - - btreeDB.Put(key, data, txn); - btreeDB.PutNoDuplicate(key, data, txn); - } - txn.Commit(); - } - catch (KeyExistException) - { - txn.Abort(); - throw new ExpectedTestException(); - } - finally - { - env.Close(); - } - } - - [Test, ExpectedException(typeof(ExpectedTestException))] - public void TestRemoveDBFile() - { - testName = "TestRemoveDBFile"; - SetUpTest(true); - - - RemoveDBWithoutEnv(testHome, testName, false); - } - - [Test, ExpectedException(typeof(ExpectedTestException))] - public void TestRemoveOneDBFromDBFile() - { - testName = "TestRemoveOneDBFromDBFile"; - SetUpTest(true); - - RemoveDBWithoutEnv(testHome, testName, true); - } - - public void RemoveDBWithoutEnv(string home, string dbName, bool ifDBName) - { - string dbFileName = home + "/" + dbName + ".db"; - - Configuration.ClearDir(home); - - BTreeDatabaseConfig btreeDBConfig = - new BTreeDatabaseConfig(); - btreeDBConfig.Creation = CreatePolicy.IF_NEEDED; - - BTreeDatabase btreeDB; - if (ifDBName == false) - { - btreeDB = BTreeDatabase.Open(dbFileName, btreeDBConfig); - btreeDB.Close(); - BTreeDatabase.Remove(dbFileName); - throw new ExpectedTestException(); - } - else - { - btreeDB = BTreeDatabase.Open(dbFileName, dbName, btreeDBConfig); - btreeDB.Close(); - BTreeDatabase.Remove(dbFileName, dbName); - Assert.IsTrue(File.Exists(dbFileName)); - try - { - btreeDB = BTreeDatabase.Open(dbFileName, dbName, new BTreeDatabaseConfig()); - btreeDB.Close(); - } - catch (DatabaseException) - { - throw new ExpectedTestException(); - } - } - } - - [Test] - public void TestRemoveDBFromFileInEnv() - { - testName = "TestRemoveDBFromFileInEnv"; - SetUpTest(true); - - RemoveDatabase(testHome, testName, true, true); - } - - [Test] - public void TestRemoveDBFromEnv() - { - testName = "TestRemoveDBFromEnv"; - SetUpTest(true); - - RemoveDatabase(testHome, testName, true, false); - } - - public void RemoveDatabase(string home, string dbName, - bool ifEnv, bool ifDBName) - { - string dbFileName = dbName + ".db"; - - Configuration.ClearDir(home); - - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseCDB = true; - envConfig.UseMPool = true; - - DatabaseEnvironment env; - env = DatabaseEnvironment.Open(home, envConfig); - - try - { - BTreeDatabaseConfig btreeDBConfig = - new BTreeDatabaseConfig(); - btreeDBConfig.Creation = CreatePolicy.ALWAYS; - btreeDBConfig.Env = env; - BTreeDatabase btreeDB = BTreeDatabase.Open( - dbFileName, dbName, btreeDBConfig); - btreeDB.Close(); - - if (ifEnv == true && ifDBName == true) - BTreeDatabase.Remove(dbFileName, dbName, env); - else if (ifEnv == true) - BTreeDatabase.Remove(dbFileName, env); - } - catch (DatabaseException) - { - throw new TestException(); - } - finally - { - try - { - BTreeDatabaseConfig dbConfig = - new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.NEVER; - dbConfig.Env = env; - BTreeDatabase db = BTreeDatabase.Open( - dbFileName, dbName, dbConfig); - Assert.AreEqual(db.Creation, CreatePolicy.NEVER); - db.Close(); - throw new TestException(); - } - catch (DatabaseException) - { - } - finally - { - env.Close(); - } - } - } - - [Test] - public void TestRenameDB() - { - testName = "TestRenameDB"; - SetUpTest(true); - string btreeDBFileName = testHome + "/" + testName + ".db"; - string btreeDBName = testName; - string newBtreeDBName = btreeDBName + "1" + ".db"; - - try - { - BTreeDatabaseConfig btreeDBConfig = - new BTreeDatabaseConfig(); - btreeDBConfig.Creation = CreatePolicy.ALWAYS; - BTreeDatabase btreeDB = BTreeDatabase.Open( - btreeDBFileName, btreeDBName, btreeDBConfig); - btreeDB.Close(); - BTreeDatabase.Rename(btreeDBFileName, - btreeDBName, newBtreeDBName); - - BTreeDatabaseConfig dbConfig = - new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.NEVER; - BTreeDatabase newDB = BTreeDatabase.Open( - btreeDBFileName, newBtreeDBName, dbConfig); - newDB.Close(); - } - catch (DatabaseException e) - { - throw new TestException(e.Message); - } - finally - { - try - { - BTreeDatabaseConfig dbConfig = - new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.NEVER; - BTreeDatabase db = BTreeDatabase.Open( - btreeDBFileName, btreeDBName, - dbConfig); - throw new TestException(testName); - } - catch (DatabaseException) - { - } - } - } - - [Test] - public void TestRenameDBFile() - { - testName = "TestRenameDB"; - SetUpTest(true); - string btreeDBFileName = testHome + "/" + - testName + ".db"; - string newBtreeDBFileName = testHome + "/" + - testName + "1.db"; - - BTreeDatabaseConfig btreeDBConfig = - new BTreeDatabaseConfig(); - btreeDBConfig.Creation = CreatePolicy.ALWAYS; - BTreeDatabase btreeDB = BTreeDatabase.Open( - btreeDBFileName, btreeDBConfig); - btreeDB.Close(); - - BTreeDatabase.Rename(btreeDBFileName, - newBtreeDBFileName); - Assert.IsFalse(File.Exists(btreeDBFileName)); - Assert.IsTrue(File.Exists(newBtreeDBFileName)); - } - - [Test] - public void TestSync() - { - testName = "TestSync"; - SetUpTest(true); - string btreeDBName = testName + ".db"; - - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.ForceFlush = true; - envConfig.UseTxns = true; - envConfig.UseMPool = true; - envConfig.UseLogging = true; - envConfig.LogSystemCfg = new LogConfig(); - envConfig.LogSystemCfg.ForceSync = false; - envConfig.LogSystemCfg.AutoRemove = true; - DatabaseEnvironment env = DatabaseEnvironment.Open( - testHome, envConfig); - - TransactionConfig txnConfig = new TransactionConfig(); - txnConfig.SyncAction = - TransactionConfig.LogFlush.WRITE_NOSYNC; - Transaction txn = env.BeginTransaction(txnConfig); - - BTreeDatabaseConfig btreeConfig = - new BTreeDatabaseConfig(); - btreeConfig.Creation = CreatePolicy.ALWAYS; - btreeConfig.Env = env; - - BTreeDatabase btreeDB = BTreeDatabase.Open( - btreeDBName, btreeConfig, txn); - - DatabaseEntry key = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("key")); - DatabaseEntry data = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("data")); - Assert.IsFalse(btreeDB.Exists(key, txn)); - btreeDB.Put(key, data, txn); - btreeDB.Sync(); - btreeDB.Close(false); - txn.Commit(); - env.Close(); - - BTreeDatabaseConfig dbConfig = - new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.NEVER; - using (BTreeDatabase db = BTreeDatabase.Open( - testHome + "/" + btreeDBName, dbConfig)) - { - Assert.IsTrue(db.Exists(key)); - } - } - - [Test] - public void TestTruncate() - { - testName = "TestTruncate"; - SetUpTest(true); - string btreeDBFileName = testHome + "/" + - testName + ".db"; - - BTreeDatabaseConfig btreeDBConfig = - new BTreeDatabaseConfig(); - btreeDBConfig.Creation = CreatePolicy.ALWAYS; - btreeDBConfig.CacheSize = - new CacheInfo(0, 30 * 1024, 1); - BTreeDatabase btreeDB = BTreeDatabase.Open( - btreeDBFileName, btreeDBConfig); - DatabaseEntry key; - DatabaseEntry data; - for (int i = 0; i < 100; i++) - { - key = new DatabaseEntry( - BitConverter.GetBytes(i)); - data = new DatabaseEntry( - BitConverter.GetBytes(i)); - btreeDB.Put(key, data); - } - uint count = btreeDB.Truncate(); - Assert.AreEqual(100, count); - Assert.IsFalse(btreeDB.Exists(new DatabaseEntry( - BitConverter.GetBytes((int)50)))); - btreeDB.Close(); - } - - [Test] - public void TestTruncateInTxn() - { - testName = "TestTruncateInTxn"; - SetUpTest(true); - - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseMPool = true; - envConfig.UseTxns = true; - DatabaseEnvironment env = DatabaseEnvironment.Open( - testHome, envConfig); - - try - { - Transaction openTxn = env.BeginTransaction(); - BTreeDatabaseConfig dbConfig = - new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Env = env; - BTreeDatabase db = BTreeDatabase.Open( - testName + ".db", dbConfig, openTxn); - openTxn.Commit(); - - Transaction putTxn = env.BeginTransaction(); - try - { - DatabaseEntry key, data; - for (int i = 0; i < 10; i++) - { - key = new DatabaseEntry( - BitConverter.GetBytes(i)); - data = new DatabaseEntry( - BitConverter.GetBytes(i)); - db.Put(key, data, putTxn); - } - - putTxn.Commit(); - } - catch (DatabaseException e) - { - putTxn.Abort(); - db.Close(); - throw e; - } - - Transaction trunTxn = env.BeginTransaction(); - try - { - uint count = db.Truncate(trunTxn); - Assert.AreEqual(10, count); - Assert.IsFalse(db.Exists( - new DatabaseEntry( - BitConverter.GetBytes((int)5)), trunTxn)); - trunTxn.Commit(); - db.Close(); - } - catch (DatabaseException) - { - trunTxn.Abort(); - db.Close(); - throw new TestException(); - } - } - catch (DatabaseException) - { - } - finally - { - env.Close(); - } - } - - [Test] - public void TestTruncateUnusedPages() - { - testName = "TestTruncateUnusedPages"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - - BTreeDatabaseConfig dbConfig = - new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.ALWAYS; - dbConfig.PageSize = 512; - BTreeDatabase db = BTreeDatabase.Open( - dbFileName, dbConfig); - DatabaseEntry key, data; - for (int i = 0; i < 100; i++) - { - key = new DatabaseEntry( - BitConverter.GetBytes(i)); - data = new DatabaseEntry( - BitConverter.GetBytes(i)); - db.Put(key, data); - } - - for (int i = 0; i < 80; i++) - db.Delete(new DatabaseEntry( - BitConverter.GetBytes(i))); - - uint count = db.TruncateUnusedPages(); - Assert.LessOrEqual(0, count); - - db.Close(); - } - - [Test] - public void TestTruncateUnusedPagesWithTxn() - { - testName = "TestTruncateUnusedPagesWithTxn"; - SetUpTest(true); - - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseMPool = true; - envConfig.UseTxns = true; - DatabaseEnvironment env = DatabaseEnvironment.Open( - testHome, envConfig); - - BTreeDatabase db; - try - { - Transaction openTxn = env.BeginTransaction(); - try - { - BTreeDatabaseConfig dbConfig = - new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Env = env; - dbConfig.PageSize = 512; - db = BTreeDatabase.Open( - testName + ".db", dbConfig, openTxn); - openTxn.Commit(); - Assert.AreEqual(512, db.Pagesize); - } - catch (DatabaseException e) - { - openTxn.Abort(); - throw e; - } - - Transaction putTxn = env.BeginTransaction(); - try - { - DatabaseEntry key, data; - for (int i = 0; i < 100; i++) - { - key = new DatabaseEntry( - BitConverter.GetBytes(i)); - data = new DatabaseEntry( - BitConverter.GetBytes(i)); - db.Put(key, data, putTxn); - } - - putTxn.Commit(); - } - catch (DatabaseException e) - { - putTxn.Abort(); - db.Close(); - throw e; - } - - Transaction delTxn = env.BeginTransaction(); - try - { - for (int i = 20; i <= 80; i++) - db.Delete(new DatabaseEntry( - BitConverter.GetBytes(i)), delTxn); - delTxn.Commit(); - } - catch (DatabaseException e) - { - delTxn.Abort(); - db.Close(); - throw e; - } - - Transaction trunTxn = env.BeginTransaction(); - try - { - uint trunPages = db.TruncateUnusedPages( - trunTxn); - Assert.LessOrEqual(0, trunPages); - trunTxn.Commit(); - db.Close(); - } - catch (DatabaseException) - { - trunTxn.Abort(); - db.Close(); - throw new Exception(); - } - } - catch (DatabaseException) - { - } - finally - { - env.Close(); - } - } - - [Test] - public void TestSalvage() - { - testName = "TestSalvage"; - SetUpTest(true); - string btreeDBFileName = testHome + "/" + - testName + ".db"; - string printableOutPut = testHome + "/" + - "printableOutPut"; - string inprintableOutPut = testHome + "/" + - "inprintableOutPut"; - - BTreeDatabaseConfig btreeDBConfig = - new BTreeDatabaseConfig(); - btreeDBConfig.Creation = CreatePolicy.ALWAYS; - BTreeDatabase btreeDB = BTreeDatabase.Open( - btreeDBFileName, btreeDBConfig); - - DatabaseEntry key; - DatabaseEntry data; - - for (uint i = 0; i < 10; i++) - { - key = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes(i.ToString())); - data = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes(i.ToString())); - btreeDB.Put(key, data); - } - - btreeDB.Close(); - - StreamWriter sw1 = new StreamWriter(printableOutPut); - StreamWriter sw2 = new StreamWriter(inprintableOutPut); - BTreeDatabase.Salvage(btreeDBFileName, btreeDBConfig, - true, true, sw1); - BTreeDatabase.Salvage(btreeDBFileName, btreeDBConfig, - false, true, sw2); - sw1.Close(); - sw2.Close(); - - FileStream file1 = new FileStream(printableOutPut, - FileMode.Open); - FileStream file2 = new FileStream(inprintableOutPut, - FileMode.Open); - if (file1.Length == file2.Length) - { - int filebyte1 = 0; - int filebyte2 = 0; - do - { - filebyte1 = file1.ReadByte(); - filebyte2 = file2.ReadByte(); - } while ((filebyte1 == filebyte2) && - (filebyte1 != -1)); - Assert.AreNotEqual(filebyte1, filebyte2); - } - - file1.Close(); - file2.Close(); - } - - [Test] - public void TestUpgrade() - { - testName = "TestUpgrade"; - SetUpTest(true); - - string srcDBFileName = "../../bdb4.7.db"; - string testDBFileName = testHome + "/bdb4.7.db"; - - FileInfo srcDBFileInfo = new FileInfo(srcDBFileName); - - //Copy the file. - srcDBFileInfo.CopyTo(testDBFileName); - Assert.IsTrue(File.Exists(testDBFileName)); - - BTreeDatabase.Upgrade(testDBFileName, - new DatabaseConfig(), true); - - // Open the upgraded database file. - BTreeDatabase db = BTreeDatabase.Open( - testDBFileName, new BTreeDatabaseConfig()); - db.Close(); - } - - [Test, ExpectedException(typeof(TestException))] - public void TestVerify() - { - testName = "TestVerify"; - SetUpTest(true); - string btreeDBFileName = testHome + "/" + - testName + ".db"; - string btreeDBName = - Path.GetFileNameWithoutExtension(btreeDBFileName); - - BTreeDatabaseConfig btreeDBConfig = - new BTreeDatabaseConfig(); - btreeDBConfig.Creation = CreatePolicy.ALWAYS; - BTreeDatabase btreeDB = BTreeDatabase.Open( - btreeDBFileName, btreeDBName, btreeDBConfig); - btreeDB.Close(); - btreeDBConfig.Duplicates = DuplicatesPolicy.SORTED; - BTreeDatabase.Verify(btreeDBFileName, btreeDBConfig, - Database.VerifyOperation.NO_ORDER_CHECK); - try - { - BTreeDatabase.Verify(btreeDBFileName, - btreeDBConfig, - Database.VerifyOperation.ORDER_CHECK_ONLY); - } - catch (DatabaseException) - { - throw new TestException(testName); - } - finally - { - BTreeDatabase.Verify(btreeDBFileName, - btreeDBName, btreeDBConfig, - Database.VerifyOperation.ORDER_CHECK_ONLY); - } - - } - - [Test] - public void TestStats() - { - testName = "TestStats"; - SetUpTest(true); - string dbFileName = testHome + "/" + - testName + ".db"; - - BTreeDatabaseConfig dbConfig = - new BTreeDatabaseConfig(); - ConfigCase1(dbConfig); - BTreeDatabase db = BTreeDatabase.Open(dbFileName, - dbConfig); - - BTreeStats stats = db.Stats(); - ConfirmStatsPart1Case1(stats); - - // Put 500 records into the database. - PutRecordCase1(db, null); - - stats = db.Stats(); - ConfirmStatsPart2Case1(stats); - - // Delete some data to get some free pages. - byte[] bigArray = new byte[10240]; - db.Delete(new DatabaseEntry(bigArray)); - - db.PrintStats(); - db.PrintFastStats(); - - db.Close(); - } - - [Test] - public void TestStatsInTxn() - { - testName = "TestStatsInTxn"; - SetUpTest(true); - - StatsInTxn(testHome, testName, false); - } - - [Test] - public void TestStatsWithIsolation() - { - testName = "TestStatsWithIsolation"; - SetUpTest(true); - - StatsInTxn(testHome, testName, true); - } - - [Test] - public void TestMultipleDBSingleFile() - { - testName = "TestMultipleDBSingleFile"; - SetUpTest(true); - string btreeDBName = testHome + "/" + testName + ".db"; - - string dbName = "test"; - - /* Create and initialize database object, open the database. */ - BTreeDatabaseConfig btreeDBconfig = new BTreeDatabaseConfig(); - btreeDBconfig.Creation = CreatePolicy.IF_NEEDED; - btreeDBconfig.ErrorPrefix = testName; - btreeDBconfig.UseRecordNumbers = true; - - BTreeDatabase btreeDB = BTreeDatabase.Open(btreeDBName, dbName, - btreeDBconfig); - btreeDB.Close(); - btreeDB = BTreeDatabase.Open(btreeDBName, dbName + "2", - btreeDBconfig); - btreeDB.Close(); - - BTreeDatabaseConfig dbcfg = new BTreeDatabaseConfig(); - dbcfg.ReadOnly = true; - BTreeDatabase newDb = BTreeDatabase.Open(btreeDBName, dbcfg); - Boolean val = newDb.HasMultiple; - Assert.IsTrue(val); - newDb.Close(); - } - - public void StatsInTxn(string home, string name, bool ifIsolation) - { - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - EnvConfigCase1(envConfig); - DatabaseEnvironment env = DatabaseEnvironment.Open( - home, envConfig); - - Transaction openTxn = env.BeginTransaction(); - BTreeDatabaseConfig dbConfig = - new BTreeDatabaseConfig(); - ConfigCase1(dbConfig); - dbConfig.Env = env; - BTreeDatabase db = BTreeDatabase.Open(name + ".db", - dbConfig, openTxn); - openTxn.Commit(); - - Transaction statsTxn = env.BeginTransaction(); - BTreeStats stats; - BTreeStats fastStats; - if (ifIsolation == false) - { - stats = db.Stats(statsTxn); - fastStats = db.FastStats(statsTxn); - } - else - { - stats = db.Stats(statsTxn, Isolation.DEGREE_ONE); - fastStats = db.FastStats(statsTxn, - Isolation.DEGREE_ONE); - } - ConfirmStatsPart1Case1(stats); - - // Put 500 records into the database. - PutRecordCase1(db, statsTxn); - - if (ifIsolation == false) - stats = db.Stats(statsTxn); - else - stats = db.Stats(statsTxn, Isolation.DEGREE_TWO); - ConfirmStatsPart2Case1(stats); - - // Delete some data to get some free pages. - byte[] bigArray = new byte[10240]; - db.Delete(new DatabaseEntry(bigArray), statsTxn); - if (ifIsolation == false) - stats = db.Stats(statsTxn); - else - stats = db.Stats(statsTxn, Isolation.DEGREE_THREE); - ConfirmStatsPart3Case1(stats); - - db.PrintStats(true); - Assert.AreEqual(0, stats.EmptyPages); - - statsTxn.Commit(); - db.Close(); - env.Close(); - } - - public void EnvConfigCase1(DatabaseEnvironmentConfig cfg) - { - cfg.Create = true; - cfg.UseTxns = true; - cfg.UseMPool = true; - cfg.UseLogging = true; - } - - public void ConfigCase1(BTreeDatabaseConfig dbConfig) - { - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Duplicates = DuplicatesPolicy.UNSORTED; - dbConfig.PageSize = 4096; - dbConfig.MinKeysPerPage = 10; - } - - public void PutRecordCase1(BTreeDatabase db, Transaction txn) - { - byte[] bigArray = new byte[10240]; - for (int i = 0; i < 100; i++) - { - if (txn == null) - db.Put(new DatabaseEntry(BitConverter.GetBytes(i)), - new DatabaseEntry(BitConverter.GetBytes(i))); - else - db.Put(new DatabaseEntry(BitConverter.GetBytes(i)), - new DatabaseEntry(BitConverter.GetBytes(i)), txn); - } - for (int i = 100; i < 500; i++) - { - if (txn == null) - db.Put(new DatabaseEntry(bigArray), - new DatabaseEntry(bigArray)); - else - db.Put(new DatabaseEntry(bigArray), - new DatabaseEntry(bigArray), txn); - } - } - - public void ConfirmStatsPart1Case1(BTreeStats stats) - { - Assert.AreEqual(1, stats.EmptyPages); - Assert.AreNotEqual(0, stats.LeafPagesFreeBytes); - Assert.AreEqual(1, stats.Levels); - Assert.AreNotEqual(0, stats.MagicNumber); - Assert.AreEqual(1, stats.MetadataFlags); - Assert.AreEqual(10, stats.MinKey); - Assert.AreEqual(2, stats.nPages); - Assert.AreEqual(4096, stats.PageSize); - Assert.AreEqual(9, stats.Version); - } - - public void ConfirmStatsPart2Case1(BTreeStats stats) - { - Assert.AreNotEqual(0, stats.DuplicatePages); - Assert.AreNotEqual(0, stats.DuplicatePagesFreeBytes); - Assert.AreNotEqual(0, stats.InternalPages); - Assert.AreNotEqual(0, stats.InternalPagesFreeBytes); - Assert.AreNotEqual(0, stats.LeafPages); - Assert.AreEqual(500, stats.nData); - Assert.AreEqual(101, stats.nKeys); - Assert.AreNotEqual(0, stats.OverflowPages); - Assert.AreNotEqual(0, stats.OverflowPagesFreeBytes); - } - - public void ConfirmStatsPart3Case1(BTreeStats stats) - { - Assert.AreNotEqual(0, stats.FreePages); - } - - private int dbIntCompare(DatabaseEntry dbt1, - DatabaseEntry dbt2) - { - int a, b; - a = BitConverter.ToInt32(dbt1.Data, 0); - b = BitConverter.ToInt32(dbt2.Data, 0); - return a - b; - } - - private uint dbPrefixCompare(DatabaseEntry dbt1, - DatabaseEntry dbt2) - { - uint cnt, len; - - len = Math.Min((uint)dbt1.Data.Length, (uint)dbt2.Data.Length); - for (cnt = 0; cnt < len; cnt++) - { - if (dbt1.Data[cnt] != dbt2.Data[cnt]) - return cnt + 1; - } - if (dbt1.Data.Length > dbt2.Data.Length) - return (uint)dbt2.Data.Length + 1; - else if (dbt1.Data.Length < dbt2.Data.Length) - return (uint)dbt1.Data.Length + 1; - else - return (uint)dbt1.Data.Length; - } - - public void SetUpEnvAndTxn(string home, - out DatabaseEnvironment env, out Transaction txn) - { - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseTxns = true; - envConfig.UseMPool = true; - env = DatabaseEnvironment.Open(home, envConfig); - txn = env.BeginTransaction(); - } - - public void OpenBtreeDB(DatabaseEnvironment env, - Transaction txn, string dbFileName, - out BTreeDatabase db) - { - BTreeDatabaseConfig dbConfig = - new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - if (env != null) - { - dbConfig.Env = env; - dbConfig.NoMMap = false; - db = BTreeDatabase.Open(dbFileName, dbConfig, txn); - } - else - { - db = BTreeDatabase.Open(dbFileName, dbConfig); - } - } - - private BTreeDatabase GetMultipleDB( - string filename, string dbname, BTreeDatabaseConfig cfg) { - BTreeDatabase ret; - DatabaseEntry data, key; - - ret = BTreeDatabase.Open(filename, dbname, cfg); - key = null; - if (cfg.UseRecordNumbers) { - /* - * Dups aren't allowed with record numbers, so - * we have to put different data. Also, record - * numbers start at 1, so we do too, which makes - * checking results easier. - */ - for (int i = 1; i < 100; i++) { - key = new DatabaseEntry( - BitConverter.GetBytes(i)); - data = new DatabaseEntry( - BitConverter.GetBytes(i)); - ret.Put(key, data); - } - - key = new DatabaseEntry( - BitConverter.GetBytes(100)); - data = new DatabaseEntry(); - data.Data = new byte[111]; - for (int i = 0; i < 111; i++) - data.Data[i] = (byte)i; - ret.Put(key, data); - } else { - for (int i = 0; i < 100; i++) { - if (i % 10 == 0) - key = new DatabaseEntry( - BitConverter.GetBytes(i)); - data = new DatabaseEntry( - BitConverter.GetBytes(i)); - /* Don't put nulls into the db. */ - Assert.IsFalse(key == null); - Assert.IsFalse(data == null); - ret.Put(key, data); - } - - if (cfg.Duplicates == DuplicatesPolicy.UNSORTED) { - /* Add in duplicates to check GetBothMultiple */ - key = new DatabaseEntry( - BitConverter.GetBytes(100)); - data = new DatabaseEntry( - BitConverter.GetBytes(100)); - for (int i = 0; i < 10; i++) - ret.Put(key, data); - - /* - * Add duplicates to check GetMultiple - * with given buffer size. - */ - for (int i = 101; i < 1024; i++) { - key = new DatabaseEntry( - BitConverter.GetBytes(101)); - data = new DatabaseEntry( - BitConverter.GetBytes(i)); - ret.Put(key, data); - } - - key = new DatabaseEntry( - BitConverter.GetBytes(102)); - data = new DatabaseEntry(); - data.Data = new byte[112]; - for (int i = 0; i < 112; i++) - data.Data[i] = (byte)i; - ret.Put(key, data); - } - } - return ret; - } - - public static void Confirm(XmlElement xmlElem, - BTreeDatabase btreeDB, bool compulsory) - { - DatabaseTest.Confirm(xmlElem, btreeDB, compulsory); - Configuration.ConfirmDuplicatesPolicy(xmlElem, - "Duplicates", btreeDB.Duplicates, compulsory); - Configuration.ConfirmUint(xmlElem, "MinKeysPerPage", - btreeDB.MinKeysPerPage, compulsory); - /* - * BTreeDatabase.RecordNumbers is the value of - * BTreeDatabaseConfig.UseRecordNumbers. - */ - Configuration.ConfirmBool(xmlElem, "UseRecordNumbers", - btreeDB.RecordNumbers, compulsory); - /* - * BTreeDatabase.ReverseSplit is the value of - * BTreeDatabaseConfig.NoReverseSplitting. - */ - Configuration.ConfirmBool(xmlElem, "NoReverseSplitting", - btreeDB.ReverseSplit, compulsory); - Assert.AreEqual(DatabaseType.BTREE, btreeDB.Type); - string type = btreeDB.ToString(); - Assert.IsNotNull(type); - } - } -} - diff --git a/test/csharp/CSharpTestFixture.cs b/test/csharp/CSharpTestFixture.cs deleted file mode 100644 index f9e53d3d7..000000000 --- a/test/csharp/CSharpTestFixture.cs +++ /dev/null @@ -1,47 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2010, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - public class CSharpTestFixture - { - protected internal string testFixtureName; - protected internal string testFixtureHome; - protected internal string testName; - protected internal string testHome; - - public virtual void SetUpTestfixture() - { - Console.WriteLine("---- TestClass Started: " + testFixtureName + ".cs ----"); - testFixtureHome = "./TestOut/" + testFixtureName; - Configuration.ClearDir(testFixtureHome); - } - - [TestFixtureTearDown] - public void TearDownTestfixture() - { - Console.WriteLine("---- TestClass Finished: " + testFixtureName + ".cs ----"); - } - - public void SetUpTest(bool clean) - { - Console.WriteLine(" ==== TestCase Started: " + testName + " ===="); - testHome = testFixtureHome + "/" + testName; - if (clean == true) - Configuration.ClearDir(testHome); - } - - [TearDown] - public void TearDownTest() - { - Console.WriteLine(" ==== TestCase Finished: " + testName + " ===="); - } - } -} diff --git a/test/csharp/Configuration.cs b/test/csharp/Configuration.cs deleted file mode 100644 index 7283e1046..000000000 --- a/test/csharp/Configuration.cs +++ /dev/null @@ -1,1119 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Xml; -using BerkeleyDB; -using NUnit.Framework; - -namespace CsharpAPITest -{ - public class Configuration - { - public static Random random = new Random(); - - /* - * Configure the value with data in xml and return true or - * false to indicate if the value is configured. If there - * is no testing data and it is optional, return false. If - * there is no testing data in xml and it is compulsory, - * ConfigNotFoundException will be thrown. If any testing - * data is provided, the value will be set by the testing - * data and true will be returned. - */ - #region Config - public static void ConfigAckPolicy(XmlElement xmlElem, - string name, ref AckPolicy ackPolicy, bool compulsory) - { - XmlNode xmlNode = XMLReader.GetNode(xmlElem, - name); - if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - else if (xmlNode != null) - { - string policy = xmlNode.InnerText; - if (policy == "ALL") - ackPolicy = AckPolicy.ALL; - else if (policy == "ALL_PEERS") - ackPolicy = AckPolicy.ALL_PEERS; - else if (policy == "NONE") - ackPolicy = AckPolicy.NONE; - else if (policy == "ONE") - ackPolicy = AckPolicy.ONE; - else if (policy == "ONE_PEER") - ackPolicy = AckPolicy.ONE_PEER; - else if (policy == "QUORUM") - ackPolicy = AckPolicy.QUORUM; - else - throw new InvalidConfigException(name); - } - } - - public static bool ConfigBool(XmlElement xmlElem, - string name, ref bool value, bool compulsory) - { - XmlNode xmlNode; - - xmlNode = XMLReader.GetNode(xmlElem, name); - if (xmlNode == null && compulsory == false) - return false; - else if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - - value = bool.Parse(xmlNode.InnerText); - return true; - } - - public static bool ConfigByteOrder(XmlElement xmlElem, - string name, ref ByteOrder byteOrder, bool compulsory) - { - XmlNode xmlNode; - - xmlNode = XMLReader.GetNode(xmlElem, name); - if (xmlNode == null && compulsory == false) - return false; - else if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - - byteOrder = ByteOrder.FromConst( - int.Parse(xmlNode.InnerText)); - return true; - } - - public static bool ConfigByteMatrix(XmlElement xmlElem, - string name, ref byte[,] byteMatrix, bool compulsory) - { - int i, j, matrixLen; - - XmlNode xmlNode = XMLReader.GetNode(xmlElem, name); - if (xmlNode == null && compulsory == false) - return false; - else if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - - matrixLen = xmlNode.ChildNodes.Count; - byte[,] matrix = new byte[matrixLen, matrixLen]; - for (i = 0; i < matrixLen; i++) - { - if (xmlNode.ChildNodes[i].ChildNodes.Count != matrixLen) - throw new ConfigNotFoundException(name); - for (j = 0; j < matrixLen; j++) - { - matrix[i, j] = byte.Parse( - xmlNode.ChildNodes[i].ChildNodes[j].InnerText); - } - } - - byteMatrix = matrix; - return true; - } - - public static bool ConfigCacheInfo(XmlElement xmlElem, - string name, ref CacheInfo cacheSize, bool compulsory) - { - XmlNode xmlNode; - XmlNode xmlChildNode; - uint bytes; - uint gigabytes; - int nCaches; - - xmlNode = XMLReader.GetNode(xmlElem, name); - if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - else if (xmlNode != null) - { - if ((xmlChildNode = XMLReader.GetNode( - (XmlElement)xmlNode, "Bytes")) != null) - { - bytes = uint.Parse(xmlChildNode.InnerText); - if ((xmlChildNode = XMLReader.GetNode( - (XmlElement)xmlNode, "Gigabytes")) != null) - { - gigabytes = uint.Parse(xmlChildNode.InnerText); - if ((xmlChildNode = XMLReader.GetNode( - (XmlElement)xmlNode, "NCaches")) != null) - { - nCaches = int.Parse(xmlChildNode.InnerText); - cacheSize = new CacheInfo(gigabytes,bytes,nCaches); - return true; - } - } - } - } - - return false; - } - - public static bool ConfigCachePriority(XmlElement xmlElem, - string name, ref CachePriority cachePriority, bool compulsory) - { - XmlNode xmlNode; - string priority; - - xmlNode = XMLReader.GetNode(xmlElem, name); - if (xmlNode == null && compulsory == false) - return false; - else if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - - priority = xmlNode.InnerText; - if (priority == "DEFAULT") - cachePriority = CachePriority.DEFAULT; - else if (priority == "HIGH") - cachePriority = CachePriority.HIGH; - else if (priority == "LOW") - cachePriority = CachePriority.LOW; - else if (priority == "VERY_HIGH") - cachePriority = CachePriority.VERY_HIGH; - else if (priority == "VERY_LOW") - cachePriority = CachePriority.VERY_LOW; - else - throw new InvalidConfigException(name); - - return true; - } - - public static bool ConfigCreatePolicy(XmlElement xmlElem, - string name, ref CreatePolicy createPolicy, bool compulsory) - { - XmlNode xmlNode; - - xmlNode = XMLReader.GetNode(xmlElem, name); - if (xmlNode == null && compulsory == false) - return false; - else if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - - if (xmlNode.InnerText == "ALWAYS") - createPolicy = CreatePolicy.ALWAYS; - else if (xmlNode.InnerText == "IF_NEEDED") - createPolicy = CreatePolicy.IF_NEEDED; - else if (xmlNode.InnerText == "NEVER") - createPolicy = CreatePolicy.NEVER; - else - throw new InvalidConfigException(name); - - return true; - } - - public static bool ConfigDuplicatesPolicy(XmlElement xmlElem, - string name, ref DuplicatesPolicy duplicatePolicy,bool compulsory) - { - XmlNode xmlNode; - - xmlNode = XMLReader.GetNode(xmlElem, name); - if (xmlNode == null && compulsory == false) - return false; - else if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - - if (xmlNode.InnerText == "NONE") - duplicatePolicy = DuplicatesPolicy.NONE; - else if (xmlNode.InnerText == "SORTED") - duplicatePolicy = DuplicatesPolicy.SORTED; - else if (xmlNode.InnerText == "UNSORTED") - duplicatePolicy = DuplicatesPolicy.UNSORTED; - else - throw new InvalidConfigException(name); - - return true; - } - - public static bool ConfigDateTime(XmlElement xmlElem, - string name, ref DateTime time, bool compulsory) - { - XmlNode xmlNode; - - xmlNode = XMLReader.GetNode(xmlElem, name); - if (xmlNode == null && compulsory == false) - return false; - else if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - - time = DateTime.Parse(xmlNode.InnerText); - return true; - } - - public static bool ConfigDeadlockPolicy(XmlElement xmlElem, - string name, ref DeadlockPolicy deadlockPolicy, bool compulsory) - { - XmlNode xmlNode = XMLReader.GetNode(xmlElem, name); - if (xmlNode == null && compulsory == false) - return false; - else if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - - string policy = xmlNode.InnerText; - if (policy == "DEFAULT") - deadlockPolicy = DeadlockPolicy.DEFAULT; - else if (policy == "EXPIRE") - deadlockPolicy = DeadlockPolicy.EXPIRE; - else if (policy == "MAX_LOCKS") - deadlockPolicy = DeadlockPolicy.MAX_LOCKS; - else if (policy == "MAX_WRITE") - deadlockPolicy = DeadlockPolicy.MAX_WRITE; - else if (policy == "MIN_LOCKS") - deadlockPolicy = DeadlockPolicy.MIN_LOCKS; - else if (policy == "MIN_WRITE") - deadlockPolicy = DeadlockPolicy.MIN_WRITE; - else if (policy == "OLDEST") - deadlockPolicy = DeadlockPolicy.OLDEST; - else if (policy == "RANDOM") - deadlockPolicy = DeadlockPolicy.RANDOM; - else if (policy == "YOUNGEST") - deadlockPolicy = DeadlockPolicy.YOUNGEST; - else - throw new InvalidConfigException(name); - return true; - } - - public static bool ConfigEncryption(XmlElement xmlElem, - string name, DatabaseConfig dbConfig, bool compulsory) - { - EncryptionAlgorithm alg; - XmlNode xmlNode; - string tmp, password; - - xmlNode = XMLReader.GetNode(xmlElem, name); - if (xmlNode == null && compulsory == false) - return false; - else if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - - password = XMLReader.GetNode((XmlElement)xmlNode, - "password").InnerText; - tmp = XMLReader.GetNode((XmlElement)xmlNode, "algorithm").InnerText; - if (tmp == "AES") - alg = EncryptionAlgorithm.AES; - else - alg = EncryptionAlgorithm.DEFAULT; - dbConfig.SetEncryption(password, alg); - return true; - } - - public static bool ConfigInt(XmlElement xmlElem, - string name, ref int value, bool compulsory) - { - XmlNode xmlNode; - - xmlNode = XMLReader.GetNode(xmlElem, name); - if (xmlNode == null && compulsory == false) - return false; - else if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - - value = int.Parse(xmlNode.InnerText); - return true; - } - - public static bool ConfigIsolation(XmlElement xmlElem, - string name, ref Isolation value, bool compulsory) - { - XmlNode xmlNode; - int isolationDegree; - - xmlNode = XMLReader.GetNode(xmlElem, name); - if (xmlNode == null && compulsory == false) - return false; - else if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - - isolationDegree = int.Parse(xmlNode.InnerText); - if (isolationDegree == 1) - value = Isolation.DEGREE_ONE; - else if (isolationDegree == 2) - value = Isolation.DEGREE_TWO; - else if (isolationDegree == 3) - value = Isolation.DEGREE_THREE; - else - throw new InvalidConfigException(name); - - return true; - } - - public static bool ConfigLogFlush(XmlElement xmlElem, - string name, ref TransactionConfig.LogFlush value, - bool compulsory) - { - XmlNode xmlNode; - string logFlush; - - xmlNode = XMLReader.GetNode(xmlElem, name); - if (xmlNode == null && compulsory == false) - return false; - else if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - - logFlush = xmlNode.InnerText; - if (logFlush == "DEFAULT") - value = TransactionConfig.LogFlush.DEFAULT; - else if (logFlush == "NOSYNC") - value = TransactionConfig.LogFlush.NOSYNC; - else if (logFlush == "WRITE_NOSYNC") - value = TransactionConfig.LogFlush.WRITE_NOSYNC; - else if (logFlush == "SYNC") - value = TransactionConfig.LogFlush.SYNC; - else - throw new InvalidConfigException(name); - - return true; - } - - public static bool ConfigLong(XmlElement xmlElem, - string name, ref long value, bool compulsory) - { - XmlNode xmlNode; - - xmlNode = XMLReader.GetNode(xmlElem, name); - if (xmlNode == null && compulsory == false) - return false; - else if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - - value = long.Parse(xmlNode.InnerText); - return true; - } - - public static bool ConfigMaxSequentialWrites( - XmlElement xmlElem, string name, - MPoolConfig mpoolConfig, bool compulsory) - { - XmlNode xmlNode; - uint pause; - int writes; - - xmlNode = XMLReader.GetNode(xmlElem, name); - if (xmlNode == null && compulsory == false) - return false; - else if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - - pause = uint.Parse(XMLReader.GetNode( - (XmlElement)xmlNode, "pause").InnerText); - writes = int.Parse(XMLReader.GetNode( - (XmlElement)xmlNode,"maxWrites").InnerText); - mpoolConfig.SetMaxSequentialWrites(writes, pause); - return true; - } - - public static bool ConfigReplicationHostAddress( - XmlElement xmlElem, string name, - ref DbSiteConfig siteConfig, bool compulsory) - { - XmlNode xmlNode = XMLReader.GetNode( - xmlElem, name); - if (xmlNode == null && compulsory == false) - return false; - else if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - - siteConfig.Host = XMLReader.GetNode( - (XmlElement)xmlNode, "Host").InnerText; - siteConfig.Port = uint.Parse(XMLReader.GetNode( - (XmlElement)xmlNode, "Port").InnerText); - return true; - } - - public static bool ConfigString(XmlElement xmlElem, - string name, ref string valChar, bool compulsory) - { - XmlNode xmlNode; - - xmlNode = XMLReader.GetNode(xmlElem, name); - if (xmlNode == null && compulsory == false) - return false; - else if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - - valChar = xmlNode.InnerText; - return true; - } - - public static bool ConfigStringList(XmlElement xmlElem, - string name, ref List strings, bool compulsory) - { - XmlNode xmlNode; - - xmlNode = XMLReader.GetNode(xmlElem, name); - if (xmlNode == null && compulsory == false) - return false; - else if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - - XmlNodeList list = xmlNode.ChildNodes; - for (int i = 0; i < list.Count; i++) - strings.Add(list[i].InnerText); - - return true; - } - - public static bool ConfigUint(XmlElement xmlElem, - string name, ref uint value, bool compulsory) - { - XmlNode xmlNode; - - xmlNode = XMLReader.GetNode(xmlElem, name); - if (xmlNode == null && compulsory == false) - return false; - else if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - - value = uint.Parse(xmlNode.InnerText); - return true; - } - - public static bool ConfigVerboseMessages( - XmlElement xmlElem, string name, - ref VerboseMessages verbose, bool compulsory) - { - XmlNode xmlNode = XMLReader.GetNode(xmlElem, - name); - if (xmlNode == null && compulsory == false) - return false; - else if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - - ConfigBool((XmlElement)xmlNode, "AllFileOps", - ref verbose.AllFileOps, compulsory); - ConfigBool((XmlElement)xmlNode, "Deadlock", - ref verbose.Deadlock, compulsory); - ConfigBool((XmlElement)xmlNode, "FileOps", - ref verbose.FileOps, compulsory); - ConfigBool((XmlElement)xmlNode, "Recovery", - ref verbose.Recovery, compulsory); - ConfigBool((XmlElement)xmlNode, "Register", - ref verbose.Register, compulsory); - ConfigBool((XmlElement)xmlNode, "Replication", - ref verbose.Replication, compulsory); - ConfigBool((XmlElement)xmlNode, "ReplicationElection", - ref verbose.ReplicationElection, compulsory); - ConfigBool((XmlElement)xmlNode, "ReplicationLease", - ref verbose.ReplicationLease, compulsory); - ConfigBool((XmlElement)xmlNode, "ReplicationMessages", - ref verbose.ReplicationMessages, compulsory); - ConfigBool((XmlElement)xmlNode, "ReplicationMisc", - ref verbose.ReplicationMisc, compulsory); - ConfigBool((XmlElement)xmlNode, "ReplicationSync", - ref verbose.ReplicationSync, compulsory); - ConfigBool((XmlElement)xmlNode, "RepMgrConnectionFailure", - ref verbose.RepMgrConnectionFailure, compulsory); - ConfigBool((XmlElement)xmlNode, "RepMgrMisc", - ref verbose.RepMgrMisc, compulsory); - ConfigBool((XmlElement)xmlNode, "WaitsForTable", - ref verbose.WaitsForTable, compulsory); - return true; - } - - #endregion Config - - /* - * Confirm that the given value is the same with that in - * xml. If there is no testing data in xml and it is - * compulsory, the ConfigNotFoundException will be thrown. - * If there is no testing data and it is optional, nothing - * will be done. If any testing data is provided, the value - * will be checked. - */ - #region Confirm - public static void ConfirmAckPolicy(XmlElement xmlElem, - string name, AckPolicy ackPolicy, bool compulsory) - { - XmlNode xmlNode = XMLReader.GetNode(xmlElem, - name); - if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - else if (xmlNode != null) - { - string policy = xmlNode.InnerText; - if (policy == "ALL") - Assert.AreEqual(AckPolicy.ALL, - ackPolicy); - else if (policy == "ALL_PEERS") - Assert.AreEqual(AckPolicy.ALL_PEERS, - ackPolicy); - else if (policy == "NONE") - Assert.AreEqual(AckPolicy.NONE, - ackPolicy); - else if (policy == "ONE") - Assert.AreEqual(AckPolicy.ONE, - ackPolicy); - else if (policy == "ONE_PEER") - Assert.AreEqual(AckPolicy.ONE_PEER, - ackPolicy); - else if (policy == "QUORUM") - Assert.AreEqual(AckPolicy.QUORUM, - ackPolicy); - else - throw new InvalidConfigException(name); - } - } - - public static void ConfirmBool(XmlElement xmlElem, - string name, bool value, bool compulsory) - { - XmlNode xmlNode; - bool expected; - - xmlNode = XMLReader.GetNode(xmlElem, - name); - if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - else if (xmlNode != null) - { - if (xmlNode.ChildNodes.Count > 1) - { - expected = bool.Parse( - xmlNode.FirstChild.NextSibling.InnerText); - Assert.AreEqual(expected, value); - } - } - } - - /* - * If configure MACHINE, the ByteOrder in database will - * switch to LITTLE_ENDIAN or BIG_ENDIAN according to the - * current machine. - */ - public static void ConfirmByteOrder(XmlElement xmlElem, - string name, ByteOrder byteOrder, bool compulsory) - { - XmlNode xmlNode; - ByteOrder specOrder; - - xmlNode = XMLReader.GetNode(xmlElem, name); - if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - else if (xmlNode != null) - { - specOrder = ByteOrder.FromConst(int.Parse( - xmlNode.InnerText)); - if (specOrder == ByteOrder.MACHINE) - Assert.AreNotEqual(specOrder, byteOrder); - else - Assert.AreEqual(specOrder, byteOrder); - } - } - - public static void ConfirmByteMatrix(XmlElement xmlElem, - string name, byte[,] byteMatrix, bool compulsory) - { - int i, j, matrixLen; - - XmlNode xmlNode = XMLReader.GetNode(xmlElem, name); - if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - else if (xmlNode != null) - { - /* - * If the length of the 2 matrixes are not - * the same, the matrixes are definately - * not equal. - */ - matrixLen = xmlNode.ChildNodes.Count; - Assert.AreEqual(matrixLen * matrixLen,byteMatrix.Length); - - /* - * Go over every element in the matrix to - * see if the same with the given xml data. - */ - for (i = 0; i < matrixLen; i++) - { - if (xmlNode.ChildNodes[i].ChildNodes.Count != matrixLen) - throw new ConfigNotFoundException(name); - for (j = 0; j < matrixLen; j++) - Assert.AreEqual( - byte.Parse(xmlNode.ChildNodes[i].ChildNodes[j].InnerText), - byteMatrix[i, j]); - } - } - } - - public static void ConfirmCachePriority(XmlElement xmlElem, - string name, CachePriority priority, bool compulsory) - { - XmlNode xmlNode; - - xmlNode = XMLReader.GetNode(xmlElem, name); - if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - else if (xmlNode != null) - { - if (xmlNode.InnerText == "DEFAULT") - Assert.AreEqual(CachePriority.DEFAULT, priority); - else if (xmlNode.InnerText == "HIGH") - Assert.AreEqual(CachePriority.HIGH, priority); - else if (xmlNode.InnerText == "LOW") - Assert.AreEqual(CachePriority.LOW, priority); - else if (xmlNode.InnerText == "VERY_HIGH") - Assert.AreEqual(CachePriority.VERY_HIGH, priority); - else if (xmlNode.InnerText == "VERY_LOW") - Assert.AreEqual(CachePriority.VERY_LOW, priority); - } - } - - public static void ConfirmCacheSize(XmlElement xmlElem, - string name, CacheInfo cache, bool compulsory) - { - uint bytes; - uint gigabytes; - int nCaches; - XmlNode xmlNode; - XmlNode xmlChildNode; - - xmlNode = XMLReader.GetNode(xmlElem, name); - if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - else if (xmlNode != null) - { - if ((xmlChildNode = XMLReader.GetNode( - (XmlElement)xmlNode, "Bytes")) != null) - { - bytes = uint.Parse(xmlChildNode.InnerText); - if ((xmlChildNode = XMLReader.GetNode( - (XmlElement)xmlNode, "Gigabytes")) != null) - { - gigabytes = uint.Parse(xmlChildNode.InnerText); - if ((xmlChildNode = XMLReader.GetNode( - (XmlElement)xmlNode, - "NCaches")) != null) - { - nCaches = int.Parse(xmlChildNode.InnerText); - Assert.LessOrEqual(bytes, cache.Bytes); - Assert.AreEqual(gigabytes, cache.Gigabytes); - Assert.AreEqual(nCaches, cache.NCaches); - } - } - } - } - } - - /* - * If bytes in CacheSize is assigned, the bytes in cachesize - * couldn't be the default one. - */ - public static void ConfirmCacheSize(XmlElement xmlElem, - string name, CacheInfo cache, uint defaultCache, - bool compulsory) - { - uint bytes; - uint gigabytes; - int nCaches; - XmlNode xmlNode; - XmlNode xmlChildNode; - - xmlNode = XMLReader.GetNode(xmlElem, name); - if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - else if (xmlNode != null) - { - if ((xmlChildNode = XMLReader.GetNode( - (XmlElement)xmlNode, "Bytes")) != null) - { - bytes = defaultCache; - if ((xmlChildNode = XMLReader.GetNode( - (XmlElement)xmlNode, "Gigabytes")) != null) - { - gigabytes = uint.Parse(xmlChildNode.InnerText); - if ((xmlChildNode = XMLReader.GetNode( - (XmlElement)xmlNode, "NCaches")) != null) - { - nCaches = int.Parse(xmlChildNode.InnerText); - Assert.AreNotEqual(bytes, cache.Bytes); - Assert.AreEqual(gigabytes, cache.Gigabytes); - Assert.AreEqual(nCaches, cache.NCaches); - } - } - } - } - } - - public static void ConfirmCreatePolicy(XmlElement xmlElem, - string name, CreatePolicy createPolicy, bool compulsory) - { - XmlNode xmlNode; - - xmlNode = XMLReader.GetNode(xmlElem, name); - if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - else if (xmlNode != null) - { - if (xmlNode.InnerText == "ALWAYS") - Assert.IsTrue(createPolicy.Equals(CreatePolicy.ALWAYS)); - else if (xmlNode.InnerText == "IF_NEEDED") - Assert.IsTrue(createPolicy.Equals(CreatePolicy.IF_NEEDED)); - else if (xmlNode.InnerText == "NEVER") - Assert.IsTrue(createPolicy.Equals(CreatePolicy.NEVER)); - } - } - - public static void ConfirmDataBaseType(XmlElement xmlElem, - string name, DatabaseType dbType, bool compulsory) - { - XmlNode xmlNode; - xmlNode = XMLReader.GetNode(xmlElem, name); - if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - else if (xmlNode != null) - { - if (xmlNode.InnerText == "BTREE") - Assert.AreEqual(dbType, DatabaseType.BTREE); - else if (xmlNode.InnerText == "HASH") - Assert.AreEqual(dbType, DatabaseType.HASH); - else if (xmlNode.InnerText == "QUEUE") - Assert.AreEqual(dbType, DatabaseType.QUEUE); - else if (xmlNode.InnerText == "RECNO") - Assert.AreEqual(dbType, DatabaseType.RECNO); - else if (xmlNode.InnerText == "UNKNOWN") - Assert.AreEqual(dbType, DatabaseType.UNKNOWN); - } - } - - public static void ConfirmDateTime(XmlElement xmlElem, - string name, DateTime time, bool compulsory) - { - XmlNode xmlNode; - - xmlNode = XMLReader.GetNode(xmlElem, name); - if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - else if (xmlNode != null) - Assert.AreEqual(DateTime.Parse( - xmlNode.InnerText), time); - } - - public static void ConfirmDeadlockPolicy(XmlElement xmlElem, - string name, DeadlockPolicy deadlockPolicy, bool compulsory) - { - XmlNode xmlNode = XMLReader.GetNode(xmlElem, name); - if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - else if (xmlNode != null) - { - string policy = xmlNode.InnerText; - if (policy == "DEFAULT") - Assert.AreEqual(DeadlockPolicy.DEFAULT, deadlockPolicy); - else if (policy == "EXPIRE") - Assert.AreEqual(DeadlockPolicy.EXPIRE, deadlockPolicy); - else if (policy == "MAX_LOCKS") - Assert.AreEqual(DeadlockPolicy.MAX_LOCKS, deadlockPolicy); - else if (policy == "MAX_WRITE") - Assert.AreEqual(DeadlockPolicy.MAX_WRITE, deadlockPolicy); - else if (policy == "MIN_LOCKS") - Assert.AreEqual(DeadlockPolicy.MIN_LOCKS, deadlockPolicy); - else if (policy == "MIN_WRITE") - Assert.AreEqual(DeadlockPolicy.MIN_WRITE, deadlockPolicy); - else if (policy == "OLDEST") - Assert.AreEqual(DeadlockPolicy.OLDEST, deadlockPolicy); - else if (policy == "RANDOM") - Assert.AreEqual(DeadlockPolicy.RANDOM, deadlockPolicy); - else if (policy == "YOUNGEST") - Assert.AreEqual(DeadlockPolicy.YOUNGEST, deadlockPolicy); - else - throw new InvalidConfigException(name); - } - } - - public static void ConfirmDuplicatesPolicy( - XmlElement xmlElem, string name, - DuplicatesPolicy duplicatedPolicy, bool compulsory) - { - XmlNode xmlNode; - xmlNode = XMLReader.GetNode(xmlElem, name); - if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - else if (xmlNode != null) - { - if (xmlNode.InnerText == "NONE") - Assert.AreEqual(duplicatedPolicy, DuplicatesPolicy.NONE); - else if (xmlNode.InnerText == "SORTED") - Assert.AreEqual(duplicatedPolicy, DuplicatesPolicy.SORTED); - else if (xmlNode.InnerText == "UNSORTED") - Assert.AreEqual(duplicatedPolicy, DuplicatesPolicy.UNSORTED); - } - } - - public static void ConfirmEncryption(XmlElement xmlElem, - string name, string dPwd, EncryptionAlgorithm dAlg, bool compulsory) - { - EncryptionAlgorithm alg; - XmlNode xmlNode = XMLReader.GetNode(xmlElem, - name); - if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - else if (xmlNode != null) - { - string password = XMLReader.GetNode( - (XmlElement)xmlNode, "password").InnerText; - string tmp = XMLReader.GetNode( - (XmlElement)xmlNode, "algorithm").InnerText; - if (tmp == "AES") - alg = EncryptionAlgorithm.AES; - else - alg = EncryptionAlgorithm.DEFAULT; - Assert.AreEqual(dAlg, alg); - Assert.AreEqual(dPwd, dPwd); - } - } - - public static void ConfirmInt(XmlElement xmlElem, - string name, int value, bool compulsory) - { - XmlNode xmlNode; - xmlNode = XMLReader.GetNode(xmlElem, name); - if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - else if (xmlNode != null) - Assert.AreEqual(int.Parse(xmlNode.InnerText), value); - } - - public static void ConfirmIsolation(XmlElement xmlElem, - string name, Isolation value, bool compulsory) - { - XmlNode xmlNode; - int isolationDegree; - - xmlNode = XMLReader.GetNode(xmlElem, name); - if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - else if (xmlNode != null) - { - isolationDegree = int.Parse(xmlNode.InnerText); - if (isolationDegree == 1) - Assert.AreEqual(Isolation.DEGREE_ONE, value); - else if (isolationDegree == 2) - Assert.AreEqual(Isolation.DEGREE_TWO, value); - else if (isolationDegree == 3) - Assert.AreEqual(Isolation.DEGREE_THREE, value); - else - throw new InvalidConfigException(name); - } - } - - public static void ConfirmLogFlush(XmlElement xmlElem, - string name, TransactionConfig.LogFlush value, - bool compulsory) - { - XmlNode xmlNode; - string logFlush; - - xmlNode = XMLReader.GetNode(xmlElem, name); - if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - else if (xmlNode != null) - { - logFlush = xmlNode.InnerText; - if (logFlush == "DEFAULT") - Assert.AreEqual(TransactionConfig.LogFlush.DEFAULT, value); - else if (logFlush == "NOSYNC") - Assert.AreEqual(TransactionConfig.LogFlush.NOSYNC, value); - else if (logFlush == "WRITE_NOSYNC") - Assert.AreEqual(TransactionConfig.LogFlush.WRITE_NOSYNC, value); - else if (logFlush == "SYNC") - Assert.AreEqual(TransactionConfig.LogFlush.SYNC, value); - else - throw new InvalidConfigException(name); - } - } - - public static void ConfirmLong(XmlElement xmlElem, - string name, long value, bool compulsory) - { - XmlNode xmlNode; - xmlNode = XMLReader.GetNode(xmlElem, name); - if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - else if (xmlNode != null) - Assert.AreEqual(long.Parse(xmlNode.InnerText), value); - } - - public static void ConfirmMaxSequentialWrites( - XmlElement xmlElem, string name, - uint mPause, int mWrites, bool compulsory) - { - XmlNode xmlNode; - uint pause; - int writes; - - xmlNode = XMLReader.GetNode(xmlElem, name); - if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - else if (xmlNode != null) - { - writes = int.Parse(XMLReader.GetNode( - (XmlElement)xmlNode, "maxWrites").InnerText); - pause = uint.Parse(XMLReader.GetNode( - (XmlElement)xmlNode, "pause").InnerText); - - Assert.AreEqual(mPause, pause); - Assert.AreEqual(mWrites, writes); - } - } - - public static void ConfirmReplicationHostAddress( - XmlElement xmlElem, string name, - DbSiteConfig siteConfig, bool compulsory) - { - XmlNode xmlNode = XMLReader.GetNode(xmlElem, name); - if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - else if (xmlNode != null) - { - string host = XMLReader.GetNode( - (XmlElement)xmlNode, "Host").InnerText; - uint port = uint.Parse(XMLReader.GetNode( - (XmlElement)xmlNode, "Port").InnerText); - - Assert.AreEqual(host, siteConfig.Host); - Assert.AreEqual(port, siteConfig.Port); - } - } - - public static void ConfirmString(XmlElement xmlElem, - string name, string str, bool compulsory) - { - XmlNode xmlNode; - - if (str != null) - { - xmlNode = XMLReader.GetNode(xmlElem, name); - if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - else if (xmlNode != null) - { - if (xmlNode.HasChildNodes) - Assert.AreEqual( - xmlNode.FirstChild.InnerText, str); - } - } - } - - public static void ConfirmStringList(XmlElement xmlElem, - string name, List strings, bool compulsory) - { - XmlNode xmlNode; - - if (strings != null) - { - xmlNode = XMLReader.GetNode(xmlElem, name); - if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - else if (xmlNode != null) - { - if (xmlNode.HasChildNodes) - { - XmlNodeList list = xmlNode.ChildNodes; - for (int i = 0; i < xmlNode.ChildNodes.Count;i++) - Assert.IsTrue( - strings.Contains(list[i].InnerText)); - } - } - } - } - - public static void ConfirmUint(XmlElement xmlElem, - string name, uint value, bool compulsory) - { - XmlNode xmlNode; - - xmlNode = XMLReader.GetNode(xmlElem, name); - if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - else if (xmlNode != null) - Assert.AreEqual(uint.Parse(xmlNode.InnerText), value); - } - - public static void ConfirmVerboseMessages( - XmlElement xmlElem, string name, - VerboseMessages verbose, bool compulsory) - { - XmlNode xmlNode = XMLReader.GetNode(xmlElem, name); - if (xmlNode == null && compulsory == true) - throw new ConfigNotFoundException(name); - else if (xmlNode != null) - { - ConfirmBool((XmlElement)xmlNode, "AllFileOps", - verbose.AllFileOps, compulsory); - ConfirmBool((XmlElement)xmlNode, "Deadlock", - verbose.Deadlock, compulsory); - ConfirmBool((XmlElement)xmlNode, "FileOps", - verbose.FileOps, compulsory); - ConfirmBool((XmlElement)xmlNode, "Recovery", - verbose.Recovery, compulsory); - ConfirmBool((XmlElement)xmlNode, "Register", - verbose.Register, compulsory); - ConfirmBool((XmlElement)xmlNode, "Replication", - verbose.Replication, compulsory); - ConfirmBool((XmlElement)xmlNode, "ReplicationElection", - verbose.ReplicationElection, compulsory); - ConfirmBool((XmlElement)xmlNode, "ReplicationLease", - verbose.ReplicationLease, compulsory); - ConfirmBool((XmlElement)xmlNode, "ReplicationMessages", - verbose.ReplicationMessages, compulsory); - ConfirmBool((XmlElement)xmlNode, "ReplicationMisc", - verbose.ReplicationMisc, compulsory); - ConfirmBool((XmlElement)xmlNode, "ReplicationSync", - verbose.ReplicationSync, compulsory); - ConfirmBool((XmlElement)xmlNode, "RepMgrConnectionFailure", - verbose.RepMgrConnectionFailure, compulsory); - ConfirmBool((XmlElement)xmlNode, "RepMgrMisc", - verbose.RepMgrMisc, compulsory); - ConfirmBool((XmlElement)xmlNode,"WaitsForTable", - verbose.WaitsForTable, compulsory); - } - } - #endregion Confirm - - public static void dbtFromString(DatabaseEntry dbt, string s) - { - dbt.Data = System.Text.Encoding.ASCII.GetBytes(s); - } - - public static string strFromDBT(DatabaseEntry dbt) - { - - System.Text.ASCIIEncoding decode = new ASCIIEncoding(); - return decode.GetString(dbt.Data); - } - - /* - * Reading params successfully returns true. Unless returns - * false. The retrieved Xml fragment is returning in xmlElem. - */ - public static XmlElement TestSetUp(string testFixtureName, string testName) - { - XMLReader xmlReader = new XMLReader("../../AllTestData.xml"); - XmlElement xmlElem = xmlReader.GetXmlElement(testFixtureName, testName); - if (xmlElem == null) - throw new ConfigNotFoundException(testFixtureName + ":" + testName); - else - return xmlElem; - } - - /* - * Delete existing test output directory and its files, - * then create a new one. - */ - public static void ClearDir(string testDir) - { - if (Directory.Exists(testDir)) - Directory.Delete(testDir, true); - Directory.CreateDirectory(testDir); - } - - public static string RandomString(int max) { - string text = ""; - int len = random.Next(max); - - for (int i = 0; i < len; i++) - text += "a"; - - return text; - } - } -} diff --git a/test/csharp/CursorConfigTest.cs b/test/csharp/CursorConfigTest.cs deleted file mode 100644 index b4397faa4..000000000 --- a/test/csharp/CursorConfigTest.cs +++ /dev/null @@ -1,79 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Threading; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class CursorConfigTest : CSharpTestFixture - { - - [TestFixtureSetUp] - public void SetUpTestFixture() - { - testFixtureName = "CursorConfigTest"; - base.SetUpTestfixture(); - } - - [Test] - public void TestConfig() - { - testName = "TestConfig"; - SetUpTest(false); - /* - * Configure the fields/properties and see if - * they are updated successfully. - */ - CursorConfig cursorConfig = new CursorConfig(); - XmlElement xmlElem = Configuration.TestSetUp( - testFixtureName, testName); - Config(xmlElem, ref cursorConfig, true); - Confirm(xmlElem, cursorConfig, true); - } - - public static void Confirm(XmlElement xmlElement, - CursorConfig cursorConfig, bool compulsory) - { - Configuration.ConfirmIsolation(xmlElement, - "IsolationDegree", cursorConfig.IsolationDegree, - compulsory); - Configuration.ConfirmCachePriority(xmlElement, - "Priority", cursorConfig.Priority, - compulsory); - Configuration.ConfirmBool(xmlElement, - "SnapshotIsolation", cursorConfig.SnapshotIsolation, - compulsory); - Configuration.ConfirmBool(xmlElement, - "WriteCursor", cursorConfig.WriteCursor, - compulsory); - } - - public static void Config(XmlElement xmlElement, - ref CursorConfig cursorConfig, bool compulsory) - { - Configuration.ConfigIsolation(xmlElement, - "IsolationDegree", ref cursorConfig.IsolationDegree, - compulsory); - Configuration.ConfigCachePriority(xmlElement, - "Priority", ref cursorConfig.Priority, compulsory); - Configuration.ConfigBool(xmlElement, - "SnapshotIsolation", ref cursorConfig.SnapshotIsolation, - compulsory); - Configuration.ConfigBool(xmlElement, - "WriteCursor", ref cursorConfig.WriteCursor, - compulsory); - } - } -} diff --git a/test/csharp/CursorTest.cs b/test/csharp/CursorTest.cs deleted file mode 100644 index 493792430..000000000 --- a/test/csharp/CursorTest.cs +++ /dev/null @@ -1,1812 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Threading; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class CursorTest : CSharpTestFixture - { - private DatabaseEnvironment paramEnv; - private BTreeDatabase paramDB; - private Transaction readTxn; - private Transaction updateTxn; - - private EventWaitHandle signal; - - private delegate void CursorMoveFuncDelegate( - Cursor cursor, uint kOffset, uint kLen, - uint dOffset, uint dLen, LockingInfo lockingInfo); - private CursorMoveFuncDelegate cursorFunc; - - [TestFixtureSetUp] - public void SetUpTestFixture() - { - testFixtureName = "CursorTest"; - base.SetUpTestfixture(); - } - - [Test] - public void TestAdd() - { - BTreeDatabase db; - BTreeCursor cursor; - - testName = "TestAdd"; - SetUpTest(true); - - // Open a database and a cursor. - GetCursorInBtreeDBWithoutEnv(testHome, testName, - out db, out cursor); - - // Add a record and confirm that it exists. - AddOneByCursor(db, cursor); - - // Intend to insert pair to a wrong position. - KeyValuePair pair = - new KeyValuePair( - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key")), - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data"))); - try { - try { - cursor.Add(pair, Cursor.InsertLocation.AFTER); - throw new TestException(); - } catch (ArgumentException) - {} - - try { - cursor.Add(pair, Cursor.InsertLocation.BEFORE); - throw new TestException(); - } catch (ArgumentException) - {} - } finally { - cursor.Close(); - db.Close(); - } - } - - [Test] - public void TestCompare() { - BTreeDatabase db; - BTreeCursor dbc1, dbc2; - DatabaseEntry data, key; - - testName = "TestCompare"; - SetUpTest(true); - - // Open a database and a cursor. Then close it. - GetCursorInBtreeDBWithoutEnv(testHome, testName, - out db, out dbc1); - dbc2 = db.Cursor(); - - for (int i = 0; i < 10; i++) { - key = new DatabaseEntry(BitConverter.GetBytes(i)); - data = new DatabaseEntry(BitConverter.GetBytes(i)); - db.Put(key, data); - } - key = new DatabaseEntry(BitConverter.GetBytes(5)); - Assert.IsTrue(dbc1.Move(key, true)); - Assert.IsTrue(dbc2.Move(key, true)); - Assert.IsTrue(dbc1.Compare(dbc2)); - Assert.IsTrue(dbc1.MoveNext()); - Assert.IsFalse(dbc1.Compare(dbc2)); - dbc1.Close(); - dbc2.Close(); - db.Close(); - } - - [Test] - public void TestClose() - { - BTreeDatabase db; - BTreeCursor cursor; - - testName = "TestClose"; - SetUpTest(true); - - // Open a database and a cursor. Then close it. - GetCursorInBtreeDBWithoutEnv(testHome, testName, - out db, out cursor); - cursor.Close(); - db.Close(); - } - - [Test] - public void TestCloseResmgr() - { - testName = "TestCloseResmgr"; - SetUpTest(true); - - TestCloseResmgr_int(true); - TestCloseResmgr_int(false); - } - void TestCloseResmgr_int(bool havetxn) - { - BTreeDatabase db; - BTreeCursor cursor, csr; - CursorConfig cursorConfig; - DatabaseEnvironment env; - Transaction txn; - - DatabaseEntry key, data; - KeyValuePair pair; - - txn = null; - - cursorConfig = new CursorConfig(); - // Open an environment, a database and a cursor. - if (havetxn) - GetCursorInBtreeDBInTDS(testHome, testName, - cursorConfig, out env, out db, - out cursor, out txn); - else - GetCursorInBtreeDB(testHome, testName, - cursorConfig, out env, out db, - out cursor); - - // Add a record to db via cursor. - key = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("key")); - data = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("data")); - pair = new KeyValuePair - (key, data); - byte []kbytes; - byte []dbytes; - - for (int i = 0; i < 100; i++) { - kbytes = ASCIIEncoding.ASCII. - GetBytes("key" + i); - dbytes = ASCIIEncoding.ASCII. - GetBytes("data" + i); - key.Data = kbytes; - - data.Data = dbytes; - - cursor.Add(pair); - } - // Do not close cursor. - - csr = db.Cursor(cursorConfig, txn); - - while (csr.MoveNext()) { - // Do nothing for now. - } - // Do not close csr. - if (havetxn && txn != null) - txn.Commit(); - env.CloseForceSync(); - - } - - [Test] - public void TestCount() - { - BTreeDatabase db; - BTreeCursor cursor; - - testName = "TestCount"; - SetUpTest(true); - - // Write one record into database with cursor. - GetCursorInBtreeDBWithoutEnv(testHome, testName, - out db, out cursor); - AddOneByCursor(db, cursor); - - /* - * Confirm that that the count operation returns 1 as - * the number of records in the database. - */ - Assert.AreEqual(1, cursor.Count()); - cursor.Close(); - db.Close(); - } - - [Test] - public void TestCurrent() - { - BTreeDatabase db; - BTreeCursor cursor; - - testName = "TestCurrent"; - SetUpTest(true); - - // Write a record into database with cursor. - GetCursorInBtreeDBWithoutEnv(testHome, - testName, out db, out cursor); - AddOneByCursor(db, cursor); - - /* - * Confirm the current record that the cursor - * points to is the one that just added by the - * cursor. - */ - Assert.IsTrue(cursor.MoveFirst()); - Assert.AreEqual( - ASCIIEncoding.ASCII.GetBytes("key"), - cursor.Current.Key.Data); - Assert.AreEqual( - ASCIIEncoding.ASCII.GetBytes("data"), - cursor.Current.Value.Data); - cursor.Close(); - db.Close(); - } - - [Test] - public void TestDelete() - { - BTreeDatabase db; - BTreeCursor cursor; - - testName = "TestDelete"; - SetUpTest(true); - - // Write a record into database with cursor. - GetCursorInBtreeDBWithoutEnv(testHome, - testName, out db, out cursor); - AddOneByCursor(db, cursor); - - // Delete the current record. - cursor.Delete(); - - // Confirm that the record no longer exists in the db. - Assert.AreEqual(0, cursor.Count()); - - cursor.Close(); - db.Close(); - } - - [Test] - public void TestDispose() - { - BTreeDatabase db; - BTreeCursor cursor; - - testName = "TestDispose"; - SetUpTest(true); - - // Write a record into database with cursor. - GetCursorInBtreeDBWithoutEnv(testHome, - testName, out db, out cursor); - - // Dispose the cursor. - cursor.Dispose(); - - db.Close(); - } - - [Test] - public void TestDuplicate() - { - DatabaseEnvironment env; - BTreeDatabase db; - BTreeCursor cursor; - Transaction txn; - testName = "TestDuplicate"; - SetUpTest(true); - - GetCursorInBtreeDBInTDS(testHome, testName, - new CursorConfig(), out env, out db, out cursor, - out txn); - for (int i = 0; i < 10; i++) - cursor.Add(new KeyValuePair< - DatabaseEntry, DatabaseEntry>( - new DatabaseEntry(BitConverter.GetBytes(i)), - new DatabaseEntry(BitConverter.GetBytes(i)))); - // Duplicate the cursor and keep its position. - BTreeCursor newCursor = cursor.Duplicate(true); - Assert.AreEqual(newCursor.Current.Key, - cursor.Current.Key); - Assert.AreEqual(newCursor.Current.Key, - cursor.Current.Key); - cursor.Close(); - newCursor.Close(); - db.Close(); - txn.Commit(); - env.Close(); - } - - [Test] - public void TestIsolationDegree() - { - BTreeDatabase db; - BTreeCursor cursor; - CursorConfig cursorConfig; - DatabaseEnvironment env; - Transaction txn; - - testName = "TestIsolationDegree"; - SetUpTest(true); - - Isolation[] isolationDegrees = new Isolation[3]; - isolationDegrees[0] = Isolation.DEGREE_ONE; - isolationDegrees[1] = Isolation.DEGREE_TWO; - isolationDegrees[2] = Isolation.DEGREE_THREE; - - IsolationDelegate[] delegates = { - new IsolationDelegate(CursorReadUncommited), - new IsolationDelegate(CursorReadCommited), - new IsolationDelegate(CursorRead)}; - - cursorConfig = new CursorConfig(); - for (int i = 0; i < 3; i++) { - cursorConfig.IsolationDegree = isolationDegrees[i]; - GetCursorInBtreeDBInTDS(testHome + "/" + i.ToString(), - testName, cursorConfig, out env, out db, - out cursor, out txn); - cursor.Close(); - db.Close(); - txn.Commit(); - env.Close(); - } - } - - public delegate void IsolationDelegate( - DatabaseEnvironment env, BTreeDatabase db, - Cursor cursor, Transaction txn); - - /* - * Configure a transactional cursor to have degree 2 - * isolation, which permits data read by this cursor - * to be deleted prior to the commit of the transaction - * for this cursor. - */ - public void CursorReadCommited( - DatabaseEnvironment env, BTreeDatabase db, - Cursor cursor, Transaction txn) - { - Console.WriteLine("CursorReadCommited"); - } - - /* - * Configure a transactional cursor to have degree 1 - * isolation. The cursor's read operations could return - * modified but not yet commited data. - */ - public void CursorReadUncommited( - DatabaseEnvironment env, BTreeDatabase db, - Cursor cursor, Transaction txn) - { - Console.WriteLine("CursorReadUncommited"); - } - - /* - * Only return committed data. - */ - public void CursorRead( - DatabaseEnvironment env, BTreeDatabase db, - Cursor cursor, Transaction txn) - { - Console.WriteLine("CursorRead"); - } - - [Test] - public void TestMoveToExactKey() - { - BTreeDatabase db; - BTreeCursor cursor; - - testName = "TestMoveToExactKey"; - SetUpTest(true); - - GetCursorInBtreeDBWithoutEnv(testHome, - testName, out db, out cursor); - - // Add one record into database. - DatabaseEntry key = new DatabaseEntry( - BitConverter.GetBytes((int)0)); - DatabaseEntry data = new DatabaseEntry( - BitConverter.GetBytes((int)0)); - KeyValuePair pair = - new KeyValuePair( - key, data); - cursor.Add(pair); - - // Move the cursor exactly to the specified key. - MoveCursor(cursor, 1, 1, 2, 2, null); - cursor.Close(); - db.Close(); - } - - [Test] - public void TestMoveToExactPair() - { - BTreeDatabase db; - BTreeCursor cursor; - DatabaseEntry key, data; - - testName = "TestMoveToExactPair"; - SetUpTest(true); - - GetCursorInBtreeDBWithoutEnv(testHome, - testName, out db, out cursor); - - // Add one record into database. - key = new DatabaseEntry( - BitConverter.GetBytes((int)0)); - data = new DatabaseEntry( - BitConverter.GetBytes((int)0)); - KeyValuePair pair = - new KeyValuePair(key, data); - cursor.Add(pair); - - // Move the cursor exactly to the specified key/data pair. - MoveCursor(cursor, 1, 1, 2, 2, null); - cursor.Close(); - db.Close(); - - } - - [Test] - public void TestMoveWithRMW() - { - testName = "TestMoveWithRMW"; - SetUpTest(true); - - // Use MoveCursor() as its move function. - cursorFunc = new CursorMoveFuncDelegate(MoveCursor); - - // Move to a specified key and a key/data pair. - MoveWithRMW(testHome, testName); - } - - [Test] - public void TestMoveFirst() - { - BTreeDatabase db; - BTreeCursor cursor; - - testName = "TestMoveFirst"; - SetUpTest(true); - - GetCursorInBtreeDBWithoutEnv(testHome, testName, - out db, out cursor); - AddOneByCursor(db, cursor); - - // Move to the first record. - MoveCursorToFirst(cursor, 1, 1, 2, 2, null); - - cursor.Close(); - db.Close(); - } - - [Test] - public void TestMoveFirstWithRMW() - { - testName = "TestMoveFirstWithRMW"; - SetUpTest(true); - - // Use MoveCursorToFirst() as its move function. - cursorFunc = new CursorMoveFuncDelegate(MoveCursorToFirst); - - // Read the first record with write lock. - MoveWithRMW(testHome, testName); - } - - [Test] - public void TestMoveLast() - { - BTreeDatabase db; - BTreeCursor cursor; - - testName = "TestMoveLast"; - SetUpTest(true); - - GetCursorInBtreeDBWithoutEnv(testHome, testName, - out db, out cursor); - AddOneByCursor(db, cursor); - - // Move the cursor to the last record. - MoveCursorToLast(cursor, 1, 1, 2, 2, null); - - cursor.Close(); - db.Close(); - } - - [Test] - public void TestMoveLastWithRMW() - { - testName = "TestMoveLastWithRMW"; - SetUpTest(true); - - // Use MoveCursorToLast() as its move function. - cursorFunc = new CursorMoveFuncDelegate(MoveCursorToLast); - - // Read the last recod with write lock. - MoveWithRMW(testHome, testName); - } - - [Test] - public void TestMoveNext() - { - BTreeDatabase db; - BTreeCursor cursor; - - testName = "TestMoveNext"; - SetUpTest(true); - - GetCursorInBtreeDBWithoutEnv(testHome, testName, - out db, out cursor); - - // Put ten records to the database. - for (int i = 0; i < 10; i++) - db.Put(new DatabaseEntry(BitConverter.GetBytes(i)), - new DatabaseEntry(BitConverter.GetBytes(i))); - - // Move the cursor from the first record to the fifth. - MoveCursorToNext(cursor, 1, 1, 2, 2, null); - - cursor.Close(); - db.Close(); - } - - [Test] - public void TestMoveNextWithRMW() - { - testName = "TestMoveLastWithRMW"; - SetUpTest(true); - - // Use MoveCursorToNext() as its move function. - cursorFunc = new CursorMoveFuncDelegate( - MoveCursorToNext); - - /* - * Read the first record to the fifth record with - * write lock. - */ - MoveWithRMW(testHome, testName); - } - - [Test] - public void TestMoveNextDuplicate() - { - BTreeDatabase db; - BTreeCursor cursor; - - testName = "TestMoveNextDuplicate"; - SetUpTest(true); - - GetCursorInBtreeDBWithoutEnv(testHome, testName, - out db, out cursor); - - // Add ten duplicate records to the database. - for (int i = 0; i < 10; i++) - db.Put(new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("key")), - new DatabaseEntry( - BitConverter.GetBytes(i))); - - /* - * Move the cursor from one duplicate record to - * another duplicate one. - */ - MoveCursorToNextDuplicate(cursor, 1, 1, 2, 2, null); - - cursor.Close(); - db.Close(); - } - - [Test] - public void TestMoveNextDuplicateWithRMW() - { - testName = "TestMoveNextDuplicateWithRMW"; - SetUpTest(true); - - /* - * Use MoveCursorToNextDuplicate() as its - * move function. - */ - cursorFunc = new CursorMoveFuncDelegate( - MoveCursorToNextDuplicate); - - /* - * Read the first record to the fifth record with - * write lock. - */ - MoveWithRMW(testHome, testName); - } - - [Test] - public void TestMoveNextUnique() - { - BTreeDatabase db; - BTreeCursor cursor; - - testName = "TestMoveNextUnique"; - SetUpTest(true); - - GetCursorInBtreeDBWithoutEnv(testHome, testName, - out db, out cursor); - - // Add ten different records to the database. - for (int i = 0; i < 10; i++) - db.Put(new DatabaseEntry( - BitConverter.GetBytes(i)), - new DatabaseEntry( - BitConverter.GetBytes(i))); - - // Move to five unique records. - MoveCursorToNextUnique(cursor, 1, 1, 2, 2, null); - - cursor.Close(); - db.Close(); - } - - [Test] - public void TestMoveNextUniqueWithRMW() - { - testName = "TestMoveNextUniqueWithRMW"; - SetUpTest(true); - - /* - * Use MoveCursorToNextUnique() as its - * move function. - */ - cursorFunc = new CursorMoveFuncDelegate( - MoveCursorToNextUnique); - - /* - * Move to five unique records. - */ - MoveWithRMW(testHome, testName); - } - - [Test] - public void TestMovePrev() - { - BTreeDatabase db; - BTreeCursor cursor; - - testName = "TestMovePrev"; - SetUpTest(true); - - GetCursorInBtreeDBWithoutEnv(testHome, testName, - out db, out cursor); - - // Add ten records to the database. - for (int i = 0; i < 10; i++) - AddOneByCursor(db, cursor); - - // Move the cursor to previous five records - MoveCursorToPrev(cursor, 1, 1, 2, 2, null); - - cursor.Close(); - db.Close(); - } - - [Test] - public void TestMovePrevWithRMW() - { - testName = "TestMovePrevWithRMW"; - SetUpTest(true); - - /* - * Use MoveCursorToNextDuplicate() as its - * move function. - */ - cursorFunc = new CursorMoveFuncDelegate( - MoveCursorToPrev); - - // Read previous record in write lock. - MoveWithRMW(testHome, testName); - } - - [Test] - public void TestMovePrevDuplicate() - { - BTreeDatabase db; - BTreeCursor cursor; - - testName = "TestMovePrevDuplicate"; - SetUpTest(true); - - GetCursorInBtreeDBWithoutEnv(testHome, testName, - out db, out cursor); - - // Add ten records to the database. - for (int i = 0; i < 10; i++) - AddOneByCursor(db, cursor); - - // Move the cursor to previous duplicate records. - MoveCursorToPrevDuplicate(cursor, 1, 1, 2, 2, null); - - cursor.Close(); - db.Close(); - } - - [Test] - public void TestMovePrevDuplicateWithRMW() - { - testName = "TestMovePrevDuplicateWithRMW"; - SetUpTest(true); - - /* - * Use MoveCursorToNextDuplicate() as its - * move function. - */ - cursorFunc = new CursorMoveFuncDelegate( - MoveCursorToPrevDuplicate); - - // Read the previous duplicate record in write lock. - MoveWithRMW(testHome, testName); - } - - [Test] - public void TestMovePrevUnique() - { - BTreeDatabase db; - BTreeCursor cursor; - - testName = "TestMovePrevUnique"; - SetUpTest(true); - - GetCursorInBtreeDBWithoutEnv(testHome, testName, - out db, out cursor); - - // Add ten records to the database. - for (int i = 0; i < 10; i++) - db.Put(new DatabaseEntry(BitConverter.GetBytes(i)), - new DatabaseEntry(BitConverter.GetBytes(i))); - - // Move the cursor to previous unique records. - MoveCursorToPrevUnique(cursor, 1, 1, 2, 2, null); - - cursor.Close(); - db.Close(); - } - - [Test] - public void TestMovePrevUniqueWithRMW() - { - testName = "TestMovePrevDuplicateWithRMW"; - SetUpTest(true); - - /* - * Use MoveCursorToPrevUnique() as its - * move function. - */ - cursorFunc = new CursorMoveFuncDelegate( - MoveCursorToPrevUnique); - - // Read the previous unique record in write lock. - MoveWithRMW(testHome, testName); - } - - [Test] - public void TestPartialPut() - { - testName = "TestPartialPut"; - SetUpTest(true); - - BTreeDatabaseConfig cfg = new BTreeDatabaseConfig(); - cfg.Creation = CreatePolicy.IF_NEEDED; - BTreeDatabase db = BTreeDatabase.Open( - testHome + "/" + testName, cfg); - PopulateDb(ref db); - - /* - * Partially replace the first byte in the current - * data with 'a', and verify that the first byte in - * the retrieved current data is 'a'. - */ - BTreeCursor cursor = db.Cursor(); - byte[] bytes = new byte[1]; - bytes[0] = (byte)'a'; - DatabaseEntry data = new DatabaseEntry(bytes, 0, 1); - KeyValuePair pair; - while(cursor.MoveNext()) { - cursor.Overwrite(data); - Assert.IsTrue(cursor.Refresh()); - pair = cursor.Current; - Assert.AreEqual((byte)'a', pair.Value.Data[0]); - } - - cursor.Close(); - db.Close(); - } - - private void PopulateDb(ref BTreeDatabase db) - { - if (db == null) { - BTreeDatabaseConfig cfg = - new BTreeDatabaseConfig(); - cfg.Creation = CreatePolicy.IF_NEEDED; - db = BTreeDatabase.Open( - testHome + "/" + testName, cfg); - } - for (int i = 0; i < 100; i++) - db.Put( - new DatabaseEntry(BitConverter.GetBytes(i)), - new DatabaseEntry(BitConverter.GetBytes(i))); - } - - [Test] - public void TestPriority() - { - CachePriority[] priorities; - CursorConfig cursorConfig; - - testName = "TestPriority"; - SetUpTest(true); - cursorConfig = new CursorConfig(); - priorities = new CachePriority[6]; - priorities[0] = CachePriority.DEFAULT; - priorities[1] = CachePriority.HIGH; - priorities[2] = CachePriority.LOW; - priorities[3] = CachePriority.VERY_HIGH; - priorities[4] = CachePriority.VERY_LOW; - priorities[5] = null; - - for (int i = 0; i < 6; i++) { - if (priorities[i] != null) { - cursorConfig.Priority = priorities[i]; - Assert.AreEqual(priorities[i], cursorConfig.Priority); - } - GetCursorWithConfig(testHome + "/" + testName + ".db", - DatabaseType.BTREE.ToString(), cursorConfig, DatabaseType.BTREE); - GetCursorWithConfig(testHome + "/" + testName + ".db", - DatabaseType.HASH.ToString(), cursorConfig, DatabaseType.HASH); - GetCursorWithConfig(testHome + "/" + testName + ".db", - DatabaseType.QUEUE.ToString(), cursorConfig, DatabaseType.QUEUE); - GetCursorWithConfig(testHome + "/" + testName + ".db", - DatabaseType.RECNO.ToString(), cursorConfig, DatabaseType.RECNO); - } - } - - private void GetCursorWithConfig(string dbFile, string dbName, - CursorConfig cfg, DatabaseType type) - { - Database db; - Cursor cursor; - Configuration.ClearDir(testHome); - if (type == DatabaseType.BTREE) { - BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - db = BTreeDatabase.Open(dbFile, dbName, dbConfig); - cursor = ((BTreeDatabase)db).Cursor(cfg); - } else if (type == DatabaseType.HASH) { - HashDatabaseConfig dbConfig = new HashDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - db = (HashDatabase)HashDatabase.Open(dbFile, dbName, dbConfig); - cursor = ((HashDatabase)db).Cursor(cfg); - } else if (type == DatabaseType.QUEUE) { - QueueDatabaseConfig dbConfig = new QueueDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Length = 100; - db = QueueDatabase.Open(dbFile, dbConfig); - cursor = ((QueueDatabase)db).Cursor(cfg); - } else if (type == DatabaseType.RECNO) { - RecnoDatabaseConfig dbConfig = new RecnoDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - db = RecnoDatabase.Open(dbFile, dbName, dbConfig); - cursor = ((RecnoDatabase)db).Cursor(cfg); - } else - throw new TestException(); - - if (cfg.Priority != null) - Assert.AreEqual(cursor.Priority, cfg.Priority); - else - Assert.AreEqual(CachePriority.DEFAULT, cursor.Priority); - - Cursor dupCursor = cursor.Duplicate(false); - Assert.AreEqual(cursor.Priority, dupCursor.Priority); - cursor.Close(); - db.Close(); - } - - [Test] - public void TestRefresh() - { - BTreeDatabase db; - BTreeCursor cursor; - DatabaseEnvironment env; - Transaction txn; - - testName = "TestRefresh"; - SetUpTest(true); - - GetCursorInBtreeDBWithoutEnv(testHome, testName, - out db, out cursor); - // Write a record with cursor and Refresh the cursor. - MoveCursorToCurrentRec(cursor, 1, 1, 2, 2, null); - cursor.Dispose(); - db.Dispose(); - - Configuration.ClearDir(testHome); - GetCursorInBtreeDBInTDS(testHome, testName, null, out env, - out db, out cursor, out txn); - LockingInfo lockingInfo = new LockingInfo(); - lockingInfo.IsolationDegree = Isolation.DEGREE_ONE; - MoveCursorToCurrentRec(cursor, 1, 1, 2, 2, lockingInfo); - cursor.Close(); - txn.Commit(); - db.Close(); - env.Close(); - } - - [Test] - public void TestRefreshWithRMW() - { - testName = "TestRefreshWithRMW"; - SetUpTest(true); - - cursorFunc = new CursorMoveFuncDelegate( - MoveCursorToCurrentRec); - - // Read the previous unique record in write lock. - MoveWithRMW(testHome, testName); - } - - [Test] - public void TestSnapshotIsolation() - { - BTreeDatabaseConfig dbConfig; - DatabaseEntry key, data; - DatabaseEnvironmentConfig envConfig; - Thread readThread, updateThread; - Transaction txn; - - updateTxn = null; - readTxn = null; - paramDB = null; - paramEnv = null; - testName = "TestSnapshotIsolation"; - SetUpTest(true); - - /* - * Open environment with DB_MULTIVERSION - * which is required by DB_TXN_SNAPSHOT. - */ - envConfig = new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseMVCC = true; - envConfig.UseTxns = true; - envConfig.UseMPool = true; - envConfig.UseLocking = true; - envConfig.TxnTimeout = 1000; - paramEnv = DatabaseEnvironment.Open( - testHome, envConfig); - paramEnv.DetectDeadlocks(DeadlockPolicy.YOUNGEST); - - /* - * Open a transactional database and put 1000 records - * into it within transaction. - */ - txn = paramEnv.BeginTransaction(); - dbConfig = new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.UseMVCC = true; - dbConfig.Env = paramEnv; - paramDB = BTreeDatabase.Open( - testName + ".db", dbConfig, txn); - for (int i = 0; i < 256; i++) - { - key = new DatabaseEntry( - BitConverter.GetBytes(i)); - data = new DatabaseEntry( - BitConverter.GetBytes(i)); - paramDB.Put(key, data, txn); - } - txn.Commit(); - - /* - * Begin two threads, read and update thread. - * The update thread runs a update transaction - * using full read/write locking. The read thread - * set DB_TXN_SNAPSHOT on read-only cursor. - */ - readThread = new Thread(new ThreadStart(ReadTxn)); - updateThread = new Thread(new ThreadStart(UpdateTxn)); - updateThread.Start(); - Thread.Sleep(1000); - readThread.Start(); - readThread.Join(); - updateThread.Join(); - - // Commit transacion in both two threads. - if (updateTxn != null) - updateTxn.Commit(); - if (readTxn != null) - readTxn.Commit(); - - /* - * Confirm that the overwrite operation works. - */ - ConfirmOverwrite(); - - paramDB.Close(); - paramEnv.Close(); - } - - public void ReadTxn() - { - // Get a new transaction for reading the db. - TransactionConfig txnConfig = - new TransactionConfig(); - txnConfig.Snapshot = true; - readTxn = paramEnv.BeginTransaction( - txnConfig); - - // Get a new cursor for putting record into db. - CursorConfig cursorConfig = new CursorConfig(); - cursorConfig.WriteCursor = false; - BTreeCursor cursor = paramDB.Cursor( - cursorConfig, readTxn); - - // Continually reading record from db. - try - { - Assert.IsTrue(cursor.MoveFirst()); - int i = 0; - do - { - Assert.AreEqual( - BitConverter.ToInt32( - cursor.Current.Key.Data, 0), - BitConverter.ToInt32( - cursor.Current.Value.Data, 0)); - } while (i <= 1000 && cursor.MoveNext()); - } - catch (DeadlockException) - { - } - finally - { - cursor.Close(); - } - } - - public void UpdateTxn() - { - int int32Value; - DatabaseEntry data; - - // Get a new transaction for updating the db. - TransactionConfig txnConfig = - new TransactionConfig(); - txnConfig.IsolationDegree = - Isolation.DEGREE_THREE; - - updateTxn = - paramEnv.BeginTransaction(txnConfig); - - // Continually putting record to db. - - BTreeCursor cursor = - paramDB.Cursor(updateTxn); - - // Move the cursor to the first record. - Assert.IsTrue(cursor.MoveFirst()); - int i = 0; - try - { - do - { - - int32Value = BitConverter.ToInt32( - cursor.Current.Value.Data, 0); - data = new DatabaseEntry( - BitConverter.GetBytes(int32Value - 1)); - cursor.Overwrite(data); - } while (i <= 1000 && cursor.MoveNext()); - } - catch (DeadlockException) - { - } - finally - { - cursor.Close(); - } - } - - [Test] - public void TestWriteCursor() - { - BTreeCursor cursor; - BTreeDatabase db; - CursorConfig cursorConfig; - DatabaseEnvironment env; - - testName = "TestWriteCursor"; - SetUpTest(true); - - cursorConfig = new CursorConfig(); - cursorConfig.WriteCursor = true; - - GetCursorInBtreeDBInCDS(testHome, testName, - cursorConfig, out env, out db, out cursor); - - /* - * Add a record by cursor to the database. If the - * WriteCursor doesn't work, exception will be - * throwed in the environment which is configured - * with DB_INIT_CDB. - */ - try - { - AddOneByCursor(db, cursor); - } - catch (DatabaseException) - { - throw new TestException(); - } - finally - { - cursor.Close(); - db.Close(); - env.Close(); - } - } - - public void ConfirmOverwrite() - { - Transaction confirmTxn = paramEnv.BeginTransaction(); - BTreeCursor cursor = paramDB.Cursor(confirmTxn); - - int i = 0; - Assert.IsTrue(cursor.MoveFirst()); - do - { - Assert.AreNotEqual( - cursor.Current.Key.Data, - cursor.Current.Value.Data); - } while (i <= 1000 && cursor.MoveNext()); - - cursor.Close(); - confirmTxn.Commit(); - } - - public static void AddOneByCursor(Database db, Cursor cursor) - { - DatabaseEntry key, data; - KeyValuePair pair; - - // Add a record to db via cursor. - key = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key")); - data = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data")); - pair = new KeyValuePair(key, data); - cursor.Add(pair); - - // Confirm that the record has been put to the database. - Assert.IsTrue(db.Exists(key)); - } - - public static void GetCursorInBtreeDBWithoutEnv( - string home, string name, out BTreeDatabase db, - out BTreeCursor cursor) - { - string dbFileName = home + "/" + name + ".db"; - - BTreeDatabaseConfig dbConfig = - new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Duplicates = DuplicatesPolicy.UNSORTED; - db = BTreeDatabase.Open(dbFileName, dbConfig); - cursor = db.Cursor(); - } - - public static void GetCursorInBtreeDBInTDS( - string home, string name, - CursorConfig cursorConfig, - out DatabaseEnvironment env, out BTreeDatabase db, - out BTreeCursor cursor, out Transaction txn) - { - string dbFileName = name + ".db"; - - Configuration.ClearDir(home); - - // Open an environment. - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseMPool = true; - envConfig.UseTxns = true; - envConfig.NoMMap = false; - envConfig.UseLocking = true; - env = DatabaseEnvironment.Open(home, envConfig); - - // Begin a transaction. - txn = env.BeginTransaction(); - - /* - * Open an btree database. The underlying database - * should be opened with ReadUncommitted if the - * cursor's isolation degree will be set to be 1. - */ - BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Env = env; - if (cursorConfig != null && - cursorConfig.IsolationDegree == Isolation.DEGREE_ONE) - dbConfig.ReadUncommitted = true; - - db = BTreeDatabase.Open(dbFileName, dbConfig, txn); - - // Get a cursor in the transaction. - if (cursorConfig != null) - cursor = db.Cursor(cursorConfig, txn); - else - cursor = db.Cursor(txn); - } - - public static void GetCursorInBtreeDB( - string home, string name, - CursorConfig cursorConfig, - out DatabaseEnvironment env, out BTreeDatabase db, - out BTreeCursor cursor) - { - string dbFileName = name + ".db"; - - Configuration.ClearDir(home); - - // Open an environment. - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseMPool = true; - envConfig.UseTxns = true; - envConfig.NoMMap = false; - envConfig.UseLocking = true; - env = DatabaseEnvironment.Open(home, envConfig); - - /* - * Open an btree database. The underlying database - * should be opened with ReadUncommitted if the - * cursor's isolation degree will be set to be 1. - */ - BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Env = env; - if (cursorConfig.IsolationDegree == Isolation.DEGREE_ONE) - dbConfig.ReadUncommitted = true; - - db = BTreeDatabase.Open(dbFileName, dbConfig, null); - - // Get a cursor in the transaction. - cursor = db.Cursor(cursorConfig, null); - } - // Get a cursor in CDS. - public static void GetCursorInBtreeDBInCDS( - string home, string name, - CursorConfig cursorConfig, - out DatabaseEnvironment env, out BTreeDatabase db, - out BTreeCursor cursor) - { - string dbFileName = name + ".db"; - Configuration.ClearDir(home); - - // Open an environment. - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseCDB = true; - envConfig.UseMPool = true; - env = DatabaseEnvironment.Open(home, envConfig); - - /* - * Open an btree database. The underlying database - * should be opened with ReadUncommitted if the - * cursor's isolation degree will be set to be 1. - */ - BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Env = env; - - if (cursorConfig.IsolationDegree == Isolation.DEGREE_ONE) - dbConfig.ReadUncommitted = true; - - db = BTreeDatabase.Open(dbFileName, dbConfig); - - // Get a cursor in the transaction. - cursor = db.Cursor(cursorConfig); - } - - public void RdMfWt() - { - Transaction txn = paramEnv.BeginTransaction(); - Cursor dbc = paramDB.Cursor(txn); - - try - { - LockingInfo lck = new LockingInfo(); - lck.ReadModifyWrite = true; - - // Read record. - cursorFunc(dbc, 1, 1, 2, 2, lck); - - // Block the current thread until event is set. - signal.WaitOne(); - - // Write new records into database. - DatabaseEntry key = new DatabaseEntry( - BitConverter.GetBytes(55)); - DatabaseEntry data = new DatabaseEntry( - BitConverter.GetBytes(55)); - dbc.Add(new KeyValuePair(key, data)); - - dbc.Close(); - txn.Commit(); - } - catch (DeadlockException) - { - dbc.Close(); - txn.Abort(); - } - } - - - public void MoveWithRMW(string home, string name) - { - paramEnv = null; - paramDB = null; - - // Open the environment. - DatabaseEnvironmentConfig envCfg = - new DatabaseEnvironmentConfig(); - envCfg.Create = true; - envCfg.FreeThreaded = true; - envCfg.UseLocking = true; - envCfg.UseLogging = true; - envCfg.UseMPool = true; - envCfg.UseTxns = true; - paramEnv = DatabaseEnvironment.Open(home, envCfg); - - // Open database in transaction. - Transaction openTxn = paramEnv.BeginTransaction(); - BTreeDatabaseConfig cfg = new BTreeDatabaseConfig(); - cfg.Creation = CreatePolicy.ALWAYS; - cfg.Env = paramEnv; - cfg.FreeThreaded = true; - cfg.PageSize = 4096; - cfg.Duplicates = DuplicatesPolicy.UNSORTED; - paramDB = BTreeDatabase.Open(name + ".db", cfg, openTxn); - openTxn.Commit(); - - /* - * Put 10 different, 2 duplicate and another different - * records into database. - */ - Transaction txn = paramEnv.BeginTransaction(); - for (int i = 0; i < 14; i++) - { - DatabaseEntry key, data; - if (i == 10 || i == 11 || i == 12) - { - key = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("key")); - data = new DatabaseEntry( - BitConverter.GetBytes(i)); - } - else - { - key = new DatabaseEntry( - BitConverter.GetBytes(i)); - data = new DatabaseEntry( - BitConverter.GetBytes(i)); - } - paramDB.Put(key, data, txn); - } - - txn.Commit(); - - // Get a event wait handle. - signal = new EventWaitHandle(false, EventResetMode.ManualReset); - - /* - * Start RdMfWt() in two threads. RdMfWt() reads - * and writes data into database. - */ - Thread t1 = new Thread(new ThreadStart(RdMfWt)); - Thread t2 = new Thread(new ThreadStart(RdMfWt)); - t1.Start(); - t2.Start(); - - /* - * Give both threads time to read before signalling - * them to write. - */ - Thread.Sleep(1000); - - // Invoke the write operation in both threads. - signal.Set(); - - // Return the number of deadlocks. - while (t1.IsAlive || t2.IsAlive) - { - /* - * Give both threads time to write before - * counting the number of deadlocks. - */ - Thread.Sleep(1000); - uint deadlocks = paramEnv.DetectDeadlocks( - DeadlockPolicy.DEFAULT); - - // Confirm that there won't be any deadlock. - Assert.AreEqual(0, deadlocks); - } - - t1.Join(); - t2.Join(); - paramDB.Close(); - paramEnv.Close(); - } - - /* - * Move the cursor to an exisiting key and key/data - * pair with LockingInfo. - */ - public void MoveCursor(Cursor dbc, - uint kOffset, uint kLen, - uint dOffset, uint dLen, - LockingInfo lck) - { - DatabaseEntry key, data; - KeyValuePair pair; - - key = new DatabaseEntry( - BitConverter.GetBytes((int)0)); - data = new DatabaseEntry( - BitConverter.GetBytes((int)0)); - pair = new KeyValuePair(key, data); - - if (lck == null) { - // Move to an existing key. - Assert.IsTrue(dbc.Move(key, true)); - - // Move to an existing key/data pair. - Assert.IsTrue(dbc.Move(pair, true)); - - /* Move to an existing key, and return - * partial data. - */ - data = new DatabaseEntry(dOffset, dLen); - Assert.IsTrue(dbc.Move(key, data, true)); - CheckPartial(null, 0, 0, - dbc.Current.Value, dOffset, dLen); - - // Partially key match. - byte[] partbytes = new byte[kLen]; - for (int i = 0; i < kLen; i++) - partbytes[i] = BitConverter.GetBytes( - (int)1)[kOffset + i]; - key = new DatabaseEntry(partbytes, kOffset, kLen); - Assert.IsTrue(dbc.Move(key, false)); - Assert.AreEqual(kLen, dbc.Current.Key.Data.Length); - CheckPartial(dbc.Current.Key, kOffset, kLen, - null, 0, 0); - } else { - // Move to an existing key. - Assert.IsTrue(dbc.Move(key, true, lck)); - - // Move to an existing key/data pair. - Assert.IsTrue(dbc.Move(pair, true, lck)); - - /* - * Move to an existing key, and return - * partial data. - */ - data = new DatabaseEntry(dOffset, dLen); - Assert.IsTrue(dbc.Move(key, data, true, lck)); - CheckPartial(null, 0, 0, - dbc.Current.Value, dOffset, dLen); - - // Partially key match. - byte[] partbytes = new byte[kLen]; - for (int i = 0; i < kLen; i++) - partbytes[i] = BitConverter.GetBytes( - (int)1)[kOffset + i]; - key = new DatabaseEntry(partbytes, kOffset, kLen); - Assert.IsTrue(dbc.Move(key, false, lck)); - Assert.AreEqual(kLen, dbc.Current.Key.Data.Length); - CheckPartial(dbc.Current.Key, kOffset, kLen, - null, 0, 0); - } - } - - /* - * Move the cursor to the first record in a nonempty - * database. The returning value should be true. - */ - public void MoveCursorToFirst(Cursor dbc, - uint kOffset, uint kLen, - uint dOffset, uint dLen, LockingInfo lck) - { - DatabaseEntry key = new DatabaseEntry(kOffset, kLen); - DatabaseEntry data = new DatabaseEntry(dOffset, dLen); - if (lck == null) { - Assert.IsTrue(dbc.MoveFirst()); - Assert.IsTrue(dbc.MoveFirst(key, data)); - } else { - Assert.IsTrue(dbc.MoveFirst(lck)); - Assert.IsTrue(dbc.MoveFirst(key, data, lck)); - } - - CheckPartial(dbc.Current.Key, kOffset, kLen, - dbc.Current.Value, dOffset, dLen); - } - - /* - * Move the cursor to last record in a nonempty - * database. The returning value should be true. - */ - public void MoveCursorToLast(Cursor dbc, - uint kOffset, uint kLen, - uint dOffset, uint dLen, LockingInfo lck) - { - DatabaseEntry key = new DatabaseEntry(kOffset, kLen); - DatabaseEntry data = new DatabaseEntry(dOffset, dLen); - if (lck == null) { - Assert.IsTrue(dbc.MoveLast()); - Assert.IsTrue(dbc.MoveLast(key, data)); - } else { - Assert.IsTrue(dbc.MoveLast(lck)); - Assert.IsTrue(dbc.MoveLast(key, data, lck)); - } - CheckPartial(dbc.Current.Key, kOffset, kLen, - dbc.Current.Value, dOffset, dLen); - } - - /* - * Move the cursor to the next record in the database - * with more than five records. The returning values of - * every move operation should be true. - */ - public void MoveCursorToNext(Cursor dbc, - uint kOffset, uint kLen, - uint dOffset, uint dLen, LockingInfo lck) - { - DatabaseEntry key = new DatabaseEntry(kOffset, kLen); - DatabaseEntry data = new DatabaseEntry(dOffset, dLen); - for (int i = 0; i < 5; i++) { - if (lck == null) { - Assert.IsTrue(dbc.MoveNext()); - Assert.IsTrue(dbc.MoveNext(key, data)); - } else { - Assert.IsTrue(dbc.MoveNext(lck)); - Assert.IsTrue( - dbc.MoveNext(key, data, lck)); - } - CheckPartial(dbc.Current.Key, kOffset, kLen, - dbc.Current.Value, dOffset, dLen); - } - } - - /* - * Move the cursor to the next duplicate record in - * the database which has more than 2 duplicate - * records. The returning value should be true. - */ - public void MoveCursorToNextDuplicate(Cursor dbc, - uint kOffset, uint kLen, - uint dOffset, uint dLen, LockingInfo lck) - { - DatabaseEntry key = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("key")); - - /* - * The cursor should point to any record in the - * database before it move to the next duplicate - * record. - */ - if (lck == null) { - dbc.Move(key, true); - Assert.IsTrue(dbc.MoveNextDuplicate()); - Assert.IsTrue(dbc.MoveNextDuplicate( - new DatabaseEntry(kOffset, kLen), - new DatabaseEntry(dOffset, dLen))); - } else { - /* - * Both the move and move next duplicate - * operation should use LockingInfo. If any - * one doesn't use LockingInfo, deadlock still - * occurs. - */ - dbc.Move(key, true, lck); - Assert.IsTrue(dbc.MoveNextDuplicate(lck)); - Assert.IsTrue(dbc.MoveNextDuplicate( - new DatabaseEntry(kOffset, kLen), - new DatabaseEntry(dOffset, dLen), lck)); - } - } - - /* - * Move the cursor to next unique record in the database. - * The returning value should be true. - */ - public void MoveCursorToNextUnique(Cursor dbc, - uint kOffset, uint kLen, - uint dOffset, uint dLen, LockingInfo lck) - { - for (int i = 0; i < 5; i++) { - if (lck == null) { - Assert.IsTrue(dbc.MoveNextUnique()); - Assert.IsTrue(dbc.MoveNextUnique( - new DatabaseEntry(kOffset, kLen), - new DatabaseEntry(dOffset, dLen))); - } else { - Assert.IsTrue(dbc.MoveNextUnique(lck)); - Assert.IsTrue(dbc.MoveNextUnique( - new DatabaseEntry(kOffset, kLen), - new DatabaseEntry(dOffset, dLen), - lck)); - } - CheckPartial(dbc.Current.Key, kOffset, kLen, - dbc.Current.Value, dOffset, dLen); - } - } - - /* - * Move the cursor to previous record in the database. - * The returning value should be true; - */ - public void MoveCursorToPrev(Cursor dbc, - uint kOffset, uint kLen, - uint dOffset, uint dLen, LockingInfo lck) - { - if (lck == null) { - dbc.MoveLast(); - for (int i = 0; i < 5; i++) { - Assert.IsTrue(dbc.MovePrev()); - dbc.MoveNext(); - Assert.IsTrue(dbc.MovePrev( - new DatabaseEntry(kOffset, kLen), - new DatabaseEntry(dOffset, dLen))); - CheckPartial( - dbc.Current.Key, kOffset, kLen, - dbc.Current.Value, dOffset, dLen); - } - } else { - dbc.MoveLast(lck); - for (int i = 0; i < 5; i++) { - Assert.IsTrue(dbc.MovePrev(lck)); - dbc.MoveNext(); - Assert.IsTrue(dbc.MovePrev( - new DatabaseEntry(kOffset, kLen), - new DatabaseEntry(dOffset, dLen), - lck)); - CheckPartial( - dbc.Current.Key, kOffset, kLen, - dbc.Current.Value, dOffset, dLen); - } - } - } - - /* - * Move the cursor to a duplicate record and then to - * another duplicate one. And finally move to it previous - * one. Since the previous duplicate one exist, the return - * value of move previous duplicate record should be - * true; - */ - public void MoveCursorToPrevDuplicate(Cursor dbc, - uint kOffset, uint kLen, - uint dOffset, uint dLen, LockingInfo lck) - { - if (lck == null) { - dbc.Move(new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("key")), true); - dbc.MoveNextDuplicate(); - Assert.IsTrue(dbc.MovePrevDuplicate()); - - dbc.Move(new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("key")), true); - dbc.MoveNextDuplicate(); - Assert.IsTrue(dbc.MovePrevDuplicate( - new DatabaseEntry(kOffset, kLen), - new DatabaseEntry(dOffset, dLen))); - } else { - dbc.Move(new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("key")), - true, lck); - dbc.MoveNextDuplicate(lck); - Assert.IsTrue(dbc.MovePrevDuplicate(lck)); - - dbc.Move(new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("key")), - true, lck); - dbc.MoveNextDuplicate(lck); - Assert.IsTrue(dbc.MovePrevDuplicate( - new DatabaseEntry(kOffset, kLen), - new DatabaseEntry(dOffset, dLen), lck)); - } - - CheckPartial(dbc.Current.Key, kOffset, kLen, - dbc.Current.Value, dOffset, dLen); - } - - /* - * Move the cursor to previous unique record in a - * database with more than 2 records. The returning - * value should be true. - */ - public void MoveCursorToPrevUnique(Cursor dbc, - uint kOffset, uint kLen, - uint dOffset, uint dLen, LockingInfo lck) - { - DatabaseEntry key = new DatabaseEntry(kOffset, kLen); - DatabaseEntry data = new DatabaseEntry(dOffset, dLen); - for (int i = 0; i < 5; i++) { - if (lck == null) { - Assert.IsTrue(dbc.MovePrevUnique()); - Assert.IsTrue( - dbc.MovePrevUnique(key, data)); - } else { - Assert.IsTrue(dbc.MovePrevUnique(lck)); - Assert.IsTrue( - dbc.MovePrevUnique(key, data, lck)); - } - CheckPartial(dbc.Current.Key, kOffset, kLen, - dbc.Current.Value, dOffset, dLen); - } - } - - /* - * Move the cursor to current existing record. The returning - * value should be true. - */ - public void MoveCursorToCurrentRec(Cursor dbc, - uint kOffset, uint kLen, - uint dOffset, uint dLen, LockingInfo lck) - { - DatabaseEntry key, data; - - // Add a record to the database. - KeyValuePair pair; - key = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("key")); - data = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("data")); - pair = new KeyValuePair(key, data); - dbc.Add(pair); - - if (lck == null) - Assert.IsTrue(dbc.Refresh()); - else - Assert.IsTrue(dbc.Refresh(lck)); - Assert.AreEqual(key.Data, dbc.Current.Key.Data); - Assert.AreEqual(data.Data, dbc.Current.Value.Data); - - key = new DatabaseEntry(kOffset, kLen); - data = new DatabaseEntry(dOffset, dLen); - if (lck == null) - Assert.IsTrue(dbc.Refresh(key, data)); - else - Assert.IsTrue(dbc.Refresh(key, data, lck)); - CheckPartial(dbc.Current.Key, kOffset, kLen, - dbc.Current.Value, dOffset, dLen); - } - - public void CheckPartial( - DatabaseEntry key, uint kOffset, uint kLen, - DatabaseEntry data, uint dOffset, uint dLen) - { - if (key != null) { - Assert.IsTrue(key.Partial); - Assert.AreEqual(kOffset, key.PartialOffset); - Assert.AreEqual(kLen, key.PartialLen); - Assert.AreEqual(kLen, (uint)key.Data.Length); - } - - if (data != null) { - Assert.IsTrue(data.Partial); - Assert.AreEqual(dOffset, data.PartialOffset); - Assert.AreEqual(dLen, data.PartialLen); - Assert.AreEqual(dLen, (uint)data.Data.Length); - } - } - } -} diff --git a/test/csharp/DatabaseConfigTest.cs b/test/csharp/DatabaseConfigTest.cs deleted file mode 100644 index adb060fd6..000000000 --- a/test/csharp/DatabaseConfigTest.cs +++ /dev/null @@ -1,115 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections.Generic; -using System.Data; -using System.IO; -using System.Text; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class DatabaseConfigTest : CSharpTestFixture - { - - [TestFixtureSetUp] - public virtual void SetUpTestFixture() { - testFixtureName = "DatabaseConfigTest"; - base.SetUpTestfixture(); - } - - [Test] - virtual public void TestConfigWithoutEnv() - { - testName = "TestConfigWithoutEnv"; - SetUpTest(false); - XmlElement xmlElem = Configuration.TestSetUp( - testFixtureName, testName); - DatabaseConfig dbConfig = new DatabaseConfig(); - Config(xmlElem, ref dbConfig, true); - Confirm(xmlElem, dbConfig, true); - } - - public static void Config(XmlElement xmlElement, - ref DatabaseConfig dbConfig, bool compulsory) - { - uint pageSize = new uint(); - - Configuration.ConfigBool(xmlElement, "AutoCommit", - ref dbConfig.AutoCommit, compulsory); - Configuration.ConfigByteOrder(xmlElement, "ByteOrder", - ref dbConfig.ByteOrder, compulsory); - Configuration.ConfigCacheInfo(xmlElement, "CacheSize", - ref dbConfig.CacheSize, compulsory); - Configuration.ConfigBool(xmlElement, "DoChecksum", - ref dbConfig.DoChecksum, compulsory); - Configuration.ConfigString(xmlElement, "ErrorPrefix", - ref dbConfig.ErrorPrefix, compulsory); - Configuration.ConfigBool(xmlElement, "FreeThreaded", - ref dbConfig.FreeThreaded, compulsory); - Configuration.ConfigBool(xmlElement, "NoMMap", - ref dbConfig.NoMMap, compulsory); - Configuration.ConfigBool(xmlElement, "NonDurableTxns", - ref dbConfig.NonDurableTxns, compulsory); - if (Configuration.ConfigUint(xmlElement, "PageSize", - ref pageSize, compulsory)) - dbConfig.PageSize = pageSize; - Configuration.ConfigCachePriority(xmlElement, - "Priority", ref dbConfig.Priority, compulsory); - Configuration.ConfigBool(xmlElement, "ReadOnly", - ref dbConfig.ReadOnly, compulsory); - Configuration.ConfigBool(xmlElement, "ReadUncommitted", - ref dbConfig.ReadUncommitted, compulsory); - Configuration.ConfigEncryption(xmlElement, - "Encryption", dbConfig, compulsory); - Configuration.ConfigBool(xmlElement, "Truncate", - ref dbConfig.Truncate, compulsory); - Configuration.ConfigBool(xmlElement, "UseMVCC", - ref dbConfig.UseMVCC, compulsory); - } - - public static void Confirm(XmlElement xmlElement, - DatabaseConfig dbConfig, bool compulsory) - { - Configuration.ConfirmBool(xmlElement, "AutoCommit", - dbConfig.AutoCommit, compulsory); - Configuration.ConfirmByteOrder(xmlElement, "ByteOrder", - dbConfig.ByteOrder, compulsory); - Configuration.ConfirmCacheSize(xmlElement, "CacheSize", - dbConfig.CacheSize, compulsory); - Configuration.ConfirmBool(xmlElement, "DoChecksum", - dbConfig.DoChecksum, compulsory); - Configuration.ConfirmEncryption(xmlElement, "Encryption", - dbConfig.EncryptionPassword, - dbConfig.EncryptAlgorithm, compulsory); - Configuration.ConfirmString(xmlElement, "ErrorPrefix", - dbConfig.ErrorPrefix, compulsory); - Configuration.ConfirmBool(xmlElement, "FreeThreaded", - dbConfig.FreeThreaded, compulsory); - Configuration.ConfirmBool(xmlElement, "NoMMap", - dbConfig.NoMMap, compulsory); - Configuration.ConfirmBool(xmlElement, "NonDurableTxns", - dbConfig.NonDurableTxns, compulsory); - Configuration.ConfirmUint(xmlElement, "PageSize", - dbConfig.PageSize, compulsory); - Configuration.ConfirmCachePriority(xmlElement, - "Priority", dbConfig.Priority, compulsory); - Configuration.ConfirmBool(xmlElement, "ReadOnly", - dbConfig.ReadOnly, compulsory); - Configuration.ConfirmBool(xmlElement, "ReadUncommitted", - dbConfig.ReadUncommitted, compulsory); - Configuration.ConfirmBool(xmlElement, "Truncate", - dbConfig.Truncate, compulsory); - Configuration.ConfirmBool(xmlElement, "UseMVCC", - dbConfig.UseMVCC, compulsory); - } - } -} - diff --git a/test/csharp/DatabaseEnvironmentConfigTest.cs b/test/csharp/DatabaseEnvironmentConfigTest.cs deleted file mode 100644 index 1012a08a3..000000000 --- a/test/csharp/DatabaseEnvironmentConfigTest.cs +++ /dev/null @@ -1,297 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Xml; -using System.Xml.XPath; -using BerkeleyDB; -using NUnit.Framework; - -namespace CsharpAPITest -{ - [TestFixture] - public class DatabaseEnvironmentConfigTest : CSharpTestFixture - { - - [TestFixtureSetUp] - public void SetUpTestFixture() - { - testFixtureName = "DatabaseEnvironmentConfigTest"; - base.SetUpTestfixture(); - } - - [Test] - public void TestConfig() - { - testName = "TestConfig"; - SetUpTest(false); - XmlElement xmlElem = Configuration.TestSetUp( - testFixtureName, testName); - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - Config(xmlElem, ref envConfig, true); - Confirm(xmlElem, envConfig, true); - } - - public static void Confirm(XmlElement xmlElement, - DatabaseEnvironmentConfig envConfig, bool compulsory) - { - Configuration.ConfirmBool(xmlElement, "AutoCommit", - envConfig.AutoCommit, compulsory); - Configuration.ConfirmBool(xmlElement, "CDB_ALLDB", - envConfig.CDB_ALLDB, compulsory); - Configuration.ConfirmBool(xmlElement, "Create", - envConfig.Create, compulsory); - Configuration.ConfirmStringList(xmlElement, "DataDirs", - envConfig.DataDirs, compulsory); - Configuration.ConfirmString(xmlElement, "ErrorPrefix", - envConfig.ErrorPrefix, compulsory); - Configuration.ConfirmBool(xmlElement, "ForceFlush", - envConfig.ForceFlush, compulsory); - Configuration.ConfirmBool(xmlElement, "FreeThreaded", - envConfig.FreeThreaded, compulsory); - Configuration.ConfirmBool(xmlElement, "InitRegions", - envConfig.InitRegions, compulsory); - Configuration.ConfirmString(xmlElement, - "IntermediateDirMode", - envConfig.IntermediateDirMode, compulsory); - Configuration.ConfirmBool(xmlElement, "Lockdown", - envConfig.Lockdown, compulsory); - Configuration.ConfirmUint(xmlElement, "LockTimeout", - envConfig.LockTimeout, compulsory); - Configuration.ConfirmUint(xmlElement, "MaxTransactions", - envConfig.MaxTransactions, compulsory); - Configuration.ConfirmBool(xmlElement, "NoBuffer", - envConfig.NoBuffer, compulsory); - Configuration.ConfirmBool(xmlElement, "NoLocking", - envConfig.NoLocking, compulsory); - Configuration.ConfirmBool(xmlElement, "NoMMap", - envConfig.NoMMap, compulsory); - Configuration.ConfirmBool(xmlElement, "NoLocking", - envConfig.NoLocking, compulsory); - Configuration.ConfirmBool(xmlElement, "NoPanic", - envConfig.NoPanic, compulsory); - Configuration.ConfirmBool(xmlElement, "Overwrite", - envConfig.Overwrite, compulsory); - Configuration.ConfirmBool(xmlElement, "Private", - envConfig.Private, compulsory); - Configuration.ConfirmBool(xmlElement, "Register", - envConfig.Register, compulsory); - Configuration.ConfirmBool(xmlElement, "RunFatalRecovery", - envConfig.RunFatalRecovery, compulsory); - Configuration.ConfirmBool(xmlElement, "RunRecovery", - envConfig.RunRecovery, compulsory); - Configuration.ConfirmBool(xmlElement, "SystemMemory", - envConfig.SystemMemory, compulsory); - Configuration.ConfirmString(xmlElement, "TempDir", - envConfig.TempDir, compulsory); - Configuration.ConfirmBool(xmlElement, "TimeNotGranted", - envConfig.TimeNotGranted, compulsory); - Configuration.ConfirmBool(xmlElement, "TxnNoSync", - envConfig.TxnNoSync, compulsory); - Configuration.ConfirmBool(xmlElement, "TxnNoWait", - envConfig.TxnNoWait, compulsory); - Configuration.ConfirmBool(xmlElement, "TxnSnapshot", - envConfig.TxnSnapshot, compulsory); - Configuration.ConfirmDateTime(xmlElement,"TxnTimestamp", - envConfig.TxnTimestamp, compulsory); - Configuration.ConfirmBool(xmlElement, "TxnWriteNoSync", - envConfig.TxnWriteNoSync, compulsory); - Configuration.ConfirmBool(xmlElement, "UseCDB", - envConfig.UseCDB, compulsory); - Configuration.ConfirmBool(xmlElement, "UseLocking", - envConfig.UseLocking, compulsory); - Configuration.ConfirmBool(xmlElement, "UseLogging", - envConfig.UseLogging, compulsory); - Configuration.ConfirmBool(xmlElement, "UseMPool", - envConfig.UseMPool, compulsory); - Configuration.ConfirmBool(xmlElement, "UseMVCC", - envConfig.UseMVCC, compulsory); - Configuration.ConfirmBool(xmlElement, "UseReplication", - envConfig.UseReplication, compulsory); - Configuration.ConfirmBool(xmlElement, "UseTxns", - envConfig.UseTxns, compulsory); - envConfig.Verbosity = new VerboseMessages(); - Configuration.ConfirmVerboseMessages(xmlElement, - "Verbosity", envConfig.Verbosity, compulsory); - Configuration.ConfirmBool(xmlElement, "YieldCPU", - envConfig.YieldCPU, compulsory); - } - - public static void Config(XmlElement xmlElement, - ref DatabaseEnvironmentConfig envConfig, bool compulsory) - { - uint value = new uint(); - DateTime time = new DateTime(); - - Configuration.ConfigBool(xmlElement, "AutoCommit", - ref envConfig.AutoCommit, compulsory); - Configuration.ConfigBool(xmlElement, "CDB_ALLDB", - ref envConfig.CDB_ALLDB, compulsory); - Configuration.ConfigBool(xmlElement, "Create", - ref envConfig.Create, compulsory); - Configuration.ConfigString(xmlElement, "CreationDir", - ref envConfig.CreationDir, compulsory); - Configuration.ConfigStringList(xmlElement, "DataDirs", - ref envConfig.DataDirs, compulsory); - Configuration.ConfigString(xmlElement, "ErrorPrefix", - ref envConfig.ErrorPrefix, compulsory); - Configuration.ConfigBool(xmlElement, "ForceFlush", - ref envConfig.ForceFlush, compulsory); - Configuration.ConfigBool(xmlElement, "FreeThreaded", - ref envConfig.FreeThreaded, compulsory); - Configuration.ConfigBool(xmlElement, "InitRegions", - ref envConfig.InitRegions, compulsory); - Configuration.ConfigString(xmlElement, "IntermediateDirMode", - ref envConfig.IntermediateDirMode, compulsory); - Configuration.ConfigBool(xmlElement, "Lockdown", - ref envConfig.Lockdown, compulsory); - if (Configuration.ConfigUint(xmlElement, "LockTimeout", - ref value, compulsory)) - envConfig.LockTimeout = value; - if (Configuration.ConfigUint(xmlElement, "MaxTransactions", - ref value, compulsory)) - envConfig.MaxTransactions = value; - Configuration.ConfigBool(xmlElement, "NoBuffer", - ref envConfig.NoBuffer, compulsory); - Configuration.ConfigBool(xmlElement, "NoLocking", - ref envConfig.NoLocking, compulsory); - Configuration.ConfigBool(xmlElement, "NoMMap", - ref envConfig.NoMMap, compulsory); - Configuration.ConfigBool(xmlElement, "NoLocking", - ref envConfig.NoLocking, compulsory); - Configuration.ConfigBool(xmlElement, "NoPanic", - ref envConfig.NoPanic, compulsory); - Configuration.ConfigBool(xmlElement, "Overwrite", - ref envConfig.Overwrite, compulsory); - Configuration.ConfigBool(xmlElement, "Private", - ref envConfig.Private, compulsory); - Configuration.ConfigBool(xmlElement, "Register", - ref envConfig.Register, compulsory); - Configuration.ConfigBool(xmlElement, "RunFatalRecovery", - ref envConfig.RunFatalRecovery, compulsory); - Configuration.ConfigBool(xmlElement, "RunRecovery", - ref envConfig.RunRecovery, compulsory); - Configuration.ConfigBool(xmlElement, "SystemMemory", - ref envConfig.SystemMemory, compulsory); - Configuration.ConfigString(xmlElement, "TempDir", - ref envConfig.TempDir, compulsory); - Configuration.ConfigBool(xmlElement, "TimeNotGranted", - ref envConfig.TimeNotGranted, compulsory); - Configuration.ConfigBool(xmlElement, "TxnNoSync", - ref envConfig.TxnNoSync, compulsory); - Configuration.ConfigBool(xmlElement, "TxnNoWait", - ref envConfig.TxnNoWait, compulsory); - Configuration.ConfigBool(xmlElement, "TxnSnapshot", - ref envConfig.TxnSnapshot, compulsory); - if (Configuration.ConfigDateTime(xmlElement, "TxnTimestamp", - ref time, compulsory)) - envConfig.TxnTimestamp = time; - Configuration.ConfigBool(xmlElement, "TxnWriteNoSync", - ref envConfig.TxnWriteNoSync, compulsory); - Configuration.ConfigBool(xmlElement, "UseLocking", - ref envConfig.UseLocking, compulsory); - Configuration.ConfigBool(xmlElement, "UseLogging", - ref envConfig.UseLogging, compulsory); - Configuration.ConfigBool(xmlElement, "UseMPool", - ref envConfig.UseMPool, compulsory); - Configuration.ConfigBool(xmlElement, "UseMVCC", - ref envConfig.UseMVCC, compulsory); - Configuration.ConfigBool(xmlElement, "UseReplication", - ref envConfig.UseReplication, compulsory); - Configuration.ConfigBool(xmlElement, "UseTxns", - ref envConfig.UseTxns, compulsory); - envConfig.Verbosity = new VerboseMessages(); - Configuration.ConfigVerboseMessages(xmlElement, - "Verbosity", ref envConfig.Verbosity, compulsory); - Configuration.ConfigBool(xmlElement, "YieldCPU", - ref envConfig.YieldCPU, compulsory); - } - - [Test] - public void TestConfigLock() - { - testName = "TestConfigLock"; - SetUpTest(false); - XmlElement xmlElem = Configuration.TestSetUp( - testFixtureName, testName); - DatabaseEnvironmentConfig cfg = - new DatabaseEnvironmentConfig(); - cfg.LockSystemCfg = new LockingConfig(); - LockingConfigTest.Config(xmlElem, - ref cfg.LockSystemCfg, true); - LockingConfigTest.Confirm(xmlElem, - cfg.LockSystemCfg, true); - } - - [Test] - public void TestConfigLog() - { - testName = "TestConfigLog"; - SetUpTest(false); - XmlElement xmlElem = Configuration.TestSetUp( - testFixtureName, testName); - DatabaseEnvironmentConfig cfg = - new DatabaseEnvironmentConfig(); - cfg.LogSystemCfg = new LogConfig(); - LogConfigTest.Config(xmlElem, ref cfg.LogSystemCfg, true); - LogConfigTest.Confirm(xmlElem, cfg.LogSystemCfg, true); - } - - [Test] - public void TestConfigMutex() - { - testName = "TestConfigMutex"; - SetUpTest(false); - XmlElement xmlElem = Configuration.TestSetUp( - testFixtureName, testName); - DatabaseEnvironmentConfig cfg = - new DatabaseEnvironmentConfig(); - cfg.MutexSystemCfg = new MutexConfig(); - MutexConfigTest.Config(xmlElem, ref cfg.MutexSystemCfg, true); - MutexConfigTest.Confirm(xmlElem, cfg.MutexSystemCfg, true); - } - - [Test] - public void TestConfigReplication() - { - testName = "TestConfigReplication"; - SetUpTest(false); - XmlElement xmlElem = Configuration.TestSetUp( - testFixtureName, testName); - DatabaseEnvironmentConfig cfg = - new DatabaseEnvironmentConfig(); - cfg.RepSystemCfg = new ReplicationConfig(); - ReplicationConfigTest.Config(xmlElem, - ref cfg.RepSystemCfg, true); - ReplicationConfigTest.Confirm(xmlElem, - cfg.RepSystemCfg, true); - } - - [Test] - public void TestSetEncryption() - { - testName = "TestSetEncryption"; - SetUpTest(true); - - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.SetEncryption("key", EncryptionAlgorithm.AES); - Assert.AreEqual("key", envConfig.EncryptionPassword); - Assert.AreEqual(EncryptionAlgorithm.AES, envConfig.EncryptAlgorithm); - DatabaseEnvironment env = DatabaseEnvironment.Open( - testHome, envConfig); - Assert.AreEqual(EncryptionAlgorithm.AES, env.EncryptAlgorithm); - env.Close(); - } - - } -} - diff --git a/test/csharp/DatabaseEnvironmentTest.cs b/test/csharp/DatabaseEnvironmentTest.cs deleted file mode 100644 index 479b3e289..000000000 --- a/test/csharp/DatabaseEnvironmentTest.cs +++ /dev/null @@ -1,2248 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; -using System.Text; -using System.Threading; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - class BackupWriter : IBackup { - FileStream f; - public int Close(string dbname) { - f.Close(); - return 0; - } - public int Open(string dbname, string target) { - string path = Path.Combine(target, dbname); - f = new FileStream(path, FileMode.Create); - return 0; - } - public int Write(byte[] data, long offset, int count) { - f.Seek(offset, SeekOrigin.Begin); - f.Write(data, 0, count); - return 0; - } - } - [TestFixture] - public class DatabaseEnvironmentTest : CSharpTestFixture - { - - private DatabaseEnvironment testBeginTransactionEnv; - private BTreeDatabase testBeginTransactionDB; - - private DatabaseEnvironment testCheckpointEnv; - private BTreeDatabase testCheckpointDB; - - private DatabaseEnvironment testDetectDeadlocksEnv; - private BTreeDatabase testDetectDeadlocksDB; - - private DatabaseEnvironment testFailCheckEnv; - - private EventWaitHandle signal; - - [TestFixtureSetUp] - public void SetUp() - { - testFixtureName = "DatabaseEnvironmentTest"; - base.SetUpTestfixture(); - } - - [Test] - public void TestArchivableDatabaseFiles() - { - testName = "TestArchivableDatabaseFiles"; - SetUpTest(true); - string dbFileName1 = testName + "1.db"; - string dbFileName2 = testName + "2.db"; - - // Open an environment. - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.AutoCommit = true; - envConfig.Create = true; - envConfig.UseMPool = true; - envConfig.UseLogging = true; - envConfig.UseTxns = true; - DatabaseEnvironment env = DatabaseEnvironment.Open( - testHome, envConfig); - - // Open two databases. - BTreeDatabaseConfig dbConfig = - new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Env = env; - BTreeDatabase db1 = BTreeDatabase.Open(dbFileName1, dbConfig); - db1.Close(); - BTreeDatabase db2 = BTreeDatabase.Open(dbFileName2, dbConfig); - db2.Close(); - - /* - * Get all database files name in the environment. - * Two database file name should be returned and - * the same as the ones when opening the databases. - */ - List dbFiles = env.ArchivableDatabaseFiles(false); - Assert.AreEqual(2, dbFiles.Count); - Assert.IsTrue(dbFiles.Contains(dbFileName1)); - Assert.IsTrue(dbFiles.Contains(dbFileName2)); - - /* - * Get all database file's abosolute path in the - * environment. Confirm those files exist. - */ - List dbFilesPath = env.ArchivableDatabaseFiles(true); - Assert.IsTrue(File.Exists(dbFilesPath[0])); - Assert.IsTrue(File.Exists(dbFilesPath[1])); - - env.Close(); - } - - [Test] - public void TestArchivableLogFiles() - { - testName = "TestArchivableLogFiles"; - SetUpTest(true); - string dbFileName = testName + ".db"; - - // Open an environment. - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.AutoCommit = true; - envConfig.Create = true; - envConfig.UseMPool = true; - envConfig.UseLogging = true; - envConfig.UseTxns = true; - DatabaseEnvironment env = DatabaseEnvironment.Open( - testHome, envConfig); - - // Open a databases. - BTreeDatabaseConfig dbConfig = - new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Env = env; - BTreeDatabase db = BTreeDatabase.Open( - dbFileName, dbConfig); - - /* - * Put 1000 records into the database to generate - * more than one log files. - */ - byte[] byteArr = new byte[1024]; - for (int i = 0; i < 1000; i++) - db.Put(new DatabaseEntry( - BitConverter.GetBytes(i)), - new DatabaseEntry(byteArr)); - - db.Close(); - - List logFiles = env.ArchivableLogFiles(false); - - List logFilesPath = - env.ArchivableLogFiles(true); - for (int i = 0; i < logFilesPath.Count; i++) - Assert.IsTrue(File.Exists(logFilesPath[i])); - - env.Close(); - } - - [Test] - public void TestBackup() { - testName = "TestBackup"; - SetUpTest(true); - string dbFileName = testName + ".db"; - - // Open an environment. - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.AutoCommit = true; - envConfig.Create = true; - envConfig.UseMPool = true; - envConfig.UseLogging = true; - envConfig.UseTxns = true; - DatabaseEnvironment env = DatabaseEnvironment.Open( - testHome, envConfig); - - // Open a databases. - BTreeDatabaseConfig dbConfig = - new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Env = env; - BTreeDatabase db = BTreeDatabase.Open( - dbFileName, dbConfig); - - /* - * Put 1000 records into the database to generate - * more than one log file. - */ - byte[] byteArr = new byte[1024]; - for (int i = 0; i < 1000; i++) - db.Put(new DatabaseEntry( - BitConverter.GetBytes(i)), - new DatabaseEntry(byteArr)); - - db.Close(); - - /* Set up the target directory for the backup. */ - string target = testFixtureHome + "/" + testName + "_backup"; - Configuration.ClearDir(target); - - BackupOptions opt = new BackupOptions(); - opt.Creation = CreatePolicy.ALWAYS; - env.Backup(target, opt); - Assert.Greater(Directory.GetFiles(target).Length, 0); - Directory.Delete(target, true); - env.Close(); - } - - [Test] - public void TestBackupCallbacks() { - testName = "TestBackupCallbacks"; - SetUpTest(true); - string dbFileName = testName + ".db"; - - // Open an environment. - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.AutoCommit = true; - envConfig.Create = true; - envConfig.UseMPool = true; - envConfig.UseLogging = true; - envConfig.UseTxns = true; - DatabaseEnvironment env = DatabaseEnvironment.Open( - testHome, envConfig); - - // Open a databases. - BTreeDatabaseConfig dbConfig = - new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Env = env; - BTreeDatabase db = BTreeDatabase.Open( - dbFileName, dbConfig); - - /* - * Populate the database but keep it small, we're going to do a - * byte-by-byte comparison at the end. - */ - byte[] byteArr = new byte[1024]; - for (int i = 0; i < 10; i++) - db.Put(new DatabaseEntry( - BitConverter.GetBytes(i)), - new DatabaseEntry(byteArr)); - - db.Close(); - - /* Set up the target directory for the backup. */ - string target = testFixtureHome + "/" + testName + "_backup"; - Configuration.ClearDir(target); - - BackupWriter backup = new BackupWriter(); - env.BackupHandler = backup; - - BackupOptions opt = new BackupOptions(); - opt.Creation = CreatePolicy.ALWAYS; - env.Backup(target, opt); - Assert.Greater(Directory.GetFiles(target).Length, 0); - - /* Check that our callbacks wrote the file correctly. */ - FileStream orig = new FileStream(Path.Combine(testHome, dbFileName), FileMode.Open, FileAccess.Read); - FileStream bak = new FileStream(Path.Combine(target, dbFileName), FileMode.Open, FileAccess.Read); - int orig_byte, bak_byte; - do { - orig_byte = orig.ReadByte(); - bak_byte = bak.ReadByte(); - Assert.AreEqual(orig_byte, bak_byte); - } while (orig_byte != -1 && bak_byte != -1); - orig.Close(); - bak.Close(); - Directory.Delete(target, true); - env.Close(); - } - - [Test] - public void TestBackupDatabase() { - testName = "TestBackupDatabase"; - SetUpTest(true); - string dbFileName = testName + ".db"; - - // Open an environment. - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.AutoCommit = true; - envConfig.Create = true; - envConfig.UseMPool = true; - envConfig.UseLogging = true; - envConfig.UseTxns = true; - DatabaseEnvironment env = DatabaseEnvironment.Open( - testHome, envConfig); - - // Open a databases. - BTreeDatabaseConfig dbConfig = - new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Env = env; - BTreeDatabase db = BTreeDatabase.Open( - dbFileName, dbConfig); - - /* - * Put 1000 records into the database to generate - * more than one log file. - */ - byte[] byteArr = new byte[1024]; - for (int i = 0; i < 1000; i++) - db.Put(new DatabaseEntry( - BitConverter.GetBytes(i)), - new DatabaseEntry(byteArr)); - - db.Close(); - - /* Set up the target directory for the backup. */ - string target = testFixtureHome + "/" + testName + "_backup"; - Configuration.ClearDir(target); - - /* We should only copy one file, the database. */ - env.BackupDatabase(target, dbFileName, true); - Assert.AreEqual(Directory.GetFiles(target).Length, 1); - - Directory.Delete(target, true); - env.Close(); - } - - [Test] - public void TestBackupOptions() { - testName = "TestBackupOptions"; - SetUpTest(true); - - // Open an environment. - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.AutoCommit = true; - envConfig.Create = true; - envConfig.UseMPool = true; - envConfig.UseLogging = true; - envConfig.UseTxns = true; - DatabaseEnvironment env = DatabaseEnvironment.Open( - testHome, envConfig); - - /* - * Check that we can get and set backup options successfully. There - * are other tests to check that the backup options are obeyed. - */ - env.BackupBufferSize = (uint)1024; - Assert.AreEqual(env.BackupBufferSize, (uint)1024); - - env.BackupReadCount = (uint)4096; - Assert.AreEqual(env.BackupReadCount, (uint)4096); - - env.BackupReadSleepDuration = (uint)1000; - Assert.AreEqual(env.BackupReadSleepDuration, (uint)1000); - - env.BackupWriteDirect = true; - Assert.IsTrue(env.BackupWriteDirect); - } - - [Test] - public void TestBeginCDSGroup() - { - testName = "TestBeginCDSGroup"; - SetUpTest(true); - - DatabaseEnvironmentConfig cfg = - new DatabaseEnvironmentConfig(); - cfg.Create = true; - cfg.UseCDB = true; - cfg.UseMPool = true; - DatabaseEnvironment env = DatabaseEnvironment.Open(testHome, cfg); - Transaction txn = env.BeginCDSGroup(); - BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Env = env; - BTreeDatabase db = BTreeDatabase.Open( - testName + ".db", dbConfig, txn); - db.Put(new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("key")), - new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("data")), txn); - db.Close(); - txn.Commit(); - env.Close(); - } - - [Test] - public void TestBeginTransaction() - { - testName = "TestBeginTransaction"; - SetUpTest(true); - - // Open an environment. - DatabaseEnvironmentConfig cfg = - new DatabaseEnvironmentConfig(); - cfg.Create = true; - cfg.UseTxns = true; - cfg.UseMPool = true; - cfg.UseLogging = true; - cfg.UseLocking = true; - cfg.NoLocking = false; - cfg.FreeThreaded = true; - testBeginTransactionEnv = DatabaseEnvironment.Open(testHome, cfg); - testBeginTransactionEnv.DeadlockResolution = DeadlockPolicy.OLDEST; - - // Open btree database. - BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); - dbConfig.AutoCommit = true; - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Env = testBeginTransactionEnv; - dbConfig.Duplicates = DuplicatesPolicy.NONE; - dbConfig.FreeThreaded = true; - testBeginTransactionDB = BTreeDatabase.Open( - testName + ".db", dbConfig); - - testBeginTransactionDB.Put( - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key")), - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data"))); - - // Begin two threads to run dead lock detection. - Thread thread1 = new Thread(new ThreadStart( - DeadLockThreadWithLockTimeOut)); - Thread thread2 = new Thread(new ThreadStart( - DeadLockThreadWithTxnTimeout)); - signal = new EventWaitHandle(false, EventResetMode.ManualReset); - thread1.Start(); - thread2.Start(); - Thread.Sleep(1000); - signal.Set(); - thread1.Join(); - thread2.Join(); - - // Close all. - testBeginTransactionDB.Close(); - testBeginTransactionEnv.Close(); - } - - public void DeadLockThreadWithLockTimeOut() - { - // Configure and begin a transaction. - TransactionConfig txnConfig = new TransactionConfig(); - txnConfig.LockTimeout = 5000; - txnConfig.Name = "DeadLockThreadWithLockTimeOut"; - Transaction txn = - testBeginTransactionEnv.BeginTransaction(txnConfig, null); - try - { - testBeginTransactionDB.Put( - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key")), - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data"))); - signal.WaitOne(); - testBeginTransactionDB.Put( - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("newkey")), - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("newdata")), - txn); - txn.Commit(); - } - catch (DeadlockException) - { - try - { - txn.Abort(); - } - catch (DatabaseException) - { - throw new TestException(); - } - } - catch (DatabaseException) - { - try - { - txn.Abort(); - } - catch (DatabaseException) - { - throw new TestException(); - } - } - } - - public void DeadLockThreadWithTxnTimeout() - { - // Configure and begin a transaction. - TransactionConfig txnConfig = new TransactionConfig(); - txnConfig.TxnTimeout = 5000; - txnConfig.Name = "DeadLockThreadWithTxnTimeout"; - Transaction txn = - testBeginTransactionEnv.BeginTransaction(txnConfig, null); - try - { - testBeginTransactionDB.Put( - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key")), - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data"))); - signal.WaitOne(); - testBeginTransactionDB.Put( - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("newkey")), - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("newdata")), - txn); - txn.Commit(); - } - catch (DeadlockException) - { - try - { - txn.Abort(); - } - catch (DatabaseException) - { - throw new TestException(); - } - } - catch (DatabaseException) - { - try - { - txn.Abort(); - } - catch (DatabaseException) - { - throw new TestException(); - } - } - } - - [Test] - public void TestCheckpoint() - { - testName = "TestCheckpoint"; - SetUpTest(true); - - // Open an environment. - DatabaseEnvironmentConfig cfg = - new DatabaseEnvironmentConfig(); - cfg.Create = true; - cfg.UseTxns = true; - cfg.UseMPool = true; - cfg.UseLogging = true; - cfg.UseLocking = true; - cfg.NoLocking = false; - cfg.FreeThreaded = true; - testCheckpointEnv = DatabaseEnvironment.Open(testHome, cfg); - - // Open btree database. - BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); - dbConfig.AutoCommit = true; - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Env = testCheckpointEnv; - dbConfig.FreeThreaded = true; - testCheckpointDB = BTreeDatabase.Open(testName + ".db", dbConfig); - - // Run a thread to put records into database. - Thread thread1 = new Thread(new ThreadStart(PutRecordsThread)); - - /* - * Run a thread to do checkpoint periodically and - * finally do a checkpoint to flush all in memory pool - * to log files. - */ - Thread thread2 = new Thread(new ThreadStart(CheckpointThread)); - - thread1.Start(); - thread2.Start(); - thread1.Join(); - thread2.Join(); - - // Close all. - testCheckpointDB.Close(); - testCheckpointEnv.Close(); - } - - public void PutRecordsThread() - { - Transaction txn = testCheckpointEnv.BeginTransaction(); - byte[] byteArr = new byte[1024]; - for (int i = 0; i < 1000; i++) - testCheckpointDB.Put( - new DatabaseEntry(BitConverter.GetBytes(i)), - new DatabaseEntry(byteArr), txn); - txn.Commit(); - } - - public void CheckpointThread() - { - uint bytes = 64; - uint minutes = 1; - uint count = 1; - while (count < 3) - { - testCheckpointEnv.Checkpoint(bytes, minutes); - count++; - } - Thread.Sleep(500); - testCheckpointEnv.Checkpoint(); - } - - [Test] - public void TestClose() - { - testName = "TestClose"; - SetUpTest(true); - - DatabaseEnvironmentConfig cfg = - new DatabaseEnvironmentConfig(); - cfg.Create = true; - DatabaseEnvironment env = DatabaseEnvironment.Open( - testHome, cfg); - env.Close(); - } - - [Test] - public void TestConfigAll() - { - testName = "TestConfigAll"; - SetUpTest(true); - - XmlElement xmlElem = Configuration.TestSetUp( - testFixtureName, testName); - - /* - * Open a new environment with all properties, - * fields and subsystems configured. - */ - DatabaseEnvironmentConfig envConig = - new DatabaseEnvironmentConfig(); - Config(xmlElem, ref envConig, true, true, true, - true, true, true); - - // Configure with methods. - ReplicationHostAddress address = - new ReplicationHostAddress("127.0.0.0", 11111); - envConig.RepSystemCfg.Clockskew(102, 100); - envConig.RepSystemCfg.RetransmissionRequest(10, 100); - envConig.RepSystemCfg.TransmitLimit(1, 1024); - - // Open the environment. - DatabaseEnvironment env = DatabaseEnvironment.Open( - testHome, envConig); - - // Confirm environment status with its configuration. - Confirm(xmlElem, env, true, true, true, true, true, true); - - // Print statistics of the current environment. - env.PrintStats(true, true); - - // Print statistics of all subsytems. - env.PrintSubsystemStats(true, true); - - env.Close(); - } - - [Test] - public void TestDeadlockPolicy() - { - testName = "TestDeadlockPolicy"; - SetUpTest(true); - - DetectDeadlockPolicy(testHome + "_DEFAULT", - DeadlockPolicy.DEFAULT); - - DetectDeadlockPolicy(testHome + "_EXPIRE", - DeadlockPolicy.EXPIRE); - DetectDeadlockPolicy(testHome + "_MAX_LOCKS", - DeadlockPolicy.MAX_LOCKS); - DetectDeadlockPolicy(testHome + "_MAX_WRITE", - DeadlockPolicy.MAX_WRITE); - DetectDeadlockPolicy(testHome + "_MIN_LOCKS", - DeadlockPolicy.MIN_LOCKS); - DetectDeadlockPolicy(testHome + "_MIN_WRITE", - DeadlockPolicy.MIN_WRITE); - DetectDeadlockPolicy(testHome + "_OLDEST", - DeadlockPolicy.OLDEST); - DetectDeadlockPolicy(testHome + "_RANDOM", - DeadlockPolicy.RANDOM); - DetectDeadlockPolicy(testHome + "_YOUNGEST", - DeadlockPolicy.YOUNGEST); - } - - public void DetectDeadlockPolicy( - string home, DeadlockPolicy deadlock) - { - Configuration.ClearDir(home); - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseLocking = true; - envConfig.UseLogging = true; - envConfig.UseMPool = true; - envConfig.UseTxns = true; - DatabaseEnvironment env = DatabaseEnvironment.Open( - home, envConfig); - env.DeadlockResolution = deadlock; - Assert.AreEqual(deadlock, env.DeadlockResolution); - env.DetectDeadlocks(deadlock); - env.Close(); - } - - [Test] - public void TestDetectDeadlocks() - { - testName = "TestDetectDeadlocks"; - SetUpTest(true); - - // Open an environment. - DatabaseEnvironmentConfig cfg = - new DatabaseEnvironmentConfig(); - cfg.Create = true; - cfg.UseTxns = true; - cfg.UseMPool = true; - cfg.UseLogging = true; - cfg.UseLocking = true; - cfg.FreeThreaded = true; - testDetectDeadlocksEnv = DatabaseEnvironment.Open( - testHome, cfg); - - // Open btree database. - BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); - dbConfig.AutoCommit = true; - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Env = testDetectDeadlocksEnv; - dbConfig.Duplicates = DuplicatesPolicy.NONE; - dbConfig.FreeThreaded = true; - testDetectDeadlocksDB = BTreeDatabase.Open( - testName + ".db", dbConfig); - - // Put one record("key", "data") into database. - testDetectDeadlocksDB.Put( - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key")), - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data"))); - - // Begin two threads to read and write record. - Thread thread1 = new Thread(new ThreadStart(ReadAndPutRecordThread)); - Thread thread2 = new Thread(new ThreadStart(ReadAndPutRecordThread)); - signal = new EventWaitHandle(false, EventResetMode.ManualReset); - thread1.Start(); - thread2.Start(); - - // Give enough time for threads to read record. - Thread.Sleep(1000); - - /* - * Let the two threads apply for write lock - * synchronously. - */ - signal.Set(); - - // Confirm that there is deadlock in the environment. - Thread.Sleep(1000); - uint deadlockNum = testDetectDeadlocksEnv.DetectDeadlocks( - DeadlockPolicy.DEFAULT); - Assert.Less(0, deadlockNum); - - thread1.Join(); - thread2.Join(); - - // Close all. - testDetectDeadlocksDB.Close(false); - testDetectDeadlocksEnv.Close(); - } - - public void ReadAndPutRecordThread() - { - Transaction txn = - testDetectDeadlocksEnv.BeginTransaction(); - try - { - testDetectDeadlocksDB.GetBoth( - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key")), - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data")), txn); - signal.WaitOne(); - testDetectDeadlocksDB.Put( - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("newKey")), - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("newData")), - txn); - txn.Commit(); - } - catch (DeadlockException) - { - txn.Abort(); - } - } - - [Test] - public void TestEncryption() - { - testName = "TestEncryption"; - SetUpTest(true); - - // Open an encrypted environment. - DatabaseEnvironmentConfig envCfg = - new DatabaseEnvironmentConfig(); - envCfg.Create = true; - envCfg.UseMPool = true; - envCfg.UseLogging = true; - envCfg.UseTxns = true; - envCfg.SetEncryption("bdb", EncryptionAlgorithm.AES); - DatabaseEnvironment env = - DatabaseEnvironment.Open(testHome, envCfg); - Transaction txn = env.BeginTransaction(); - - // Open a non-encrypted database in the environment. - BTreeDatabaseConfig dbCfg = - new BTreeDatabaseConfig(); - dbCfg.Creation = CreatePolicy.IF_NEEDED; - dbCfg.Env = env; - BTreeDatabase db1 = BTreeDatabase.Open( - testName + "1.db", dbCfg, txn); - Assert.IsFalse(db1.Encrypted); - for (int i = 0; i < 10; i++) - db1.Put(new DatabaseEntry( - BitConverter.GetBytes(i)), - new DatabaseEntry( - BitConverter.GetBytes(i)), txn); - - // Open an encrypted database in the environment. - dbCfg.Encrypted = true; - BTreeDatabase db2 = BTreeDatabase.Open( - testName + "2.db", dbCfg, txn); - Assert.IsTrue(db2.Encrypted); - for (int i = 0; i < 10; i++) - db2.Put(new DatabaseEntry(BitConverter.GetBytes(i)), - new DatabaseEntry(BitConverter.GetBytes(i)), txn); - - // Close all. - txn.Commit(); - db1.Close(); - db2.Close(); - env.Close(); - - // Verify the environment is encrypted. - DatabaseEnvironmentConfig verifyEnvCfg = - new DatabaseEnvironmentConfig(); - verifyEnvCfg.Create = true; - verifyEnvCfg.UseMPool = true; - verifyEnvCfg.UseLogging = true; - verifyEnvCfg.UseTxns = true; - try { - env = DatabaseEnvironment.Open( - testHome, verifyEnvCfg); - // Do nothing - env.Close(); - throw new TestException(); - } catch (DatabaseException) { - } - - // Verify the non-encrypted database is not encrypted. - BTreeDatabaseConfig verifyDbCfg = - new BTreeDatabaseConfig(); - verifyDbCfg.Creation = CreatePolicy.IF_NEEDED; - - using (db1 = BTreeDatabase.Open( - testHome + "/" + testName + "1.db", verifyDbCfg)) { - for (int i = 0; i < 10; i++) - db1.Get(new DatabaseEntry( - BitConverter.GetBytes(i))); - }; - - /// Verify the encrypted database is encrypted. - verifyDbCfg.SetEncryption(envCfg.EncryptionPassword, - envCfg.EncryptAlgorithm); - verifyDbCfg.Encrypted = true; - using (db1 = BTreeDatabase.Open( - testHome + "/" + testName + "2.db", - verifyDbCfg)) { - for (int i = 0; i < 10; i++) - db1.Get(new DatabaseEntry( - BitConverter.GetBytes(i))); - }; - } - - [Test] - public void TestFailCheck() - { - testName = "TestFailCheck"; - SetUpTest(true); - - DatabaseEnvironmentConfig cfg = - new DatabaseEnvironmentConfig(); - cfg.Create = true; - cfg.UseTxns = true; - cfg.UseMPool = true; - cfg.UseLogging = true; - cfg.UseLocking = true; - cfg.FreeThreaded = true; - cfg.ThreadIsAlive = new ThreadIsAliveDelegate(ThrdAlive); - cfg.SetThreadID = new SetThreadIDDelegate(SetThrdID); - cfg.ThreadCount = 10; - testFailCheckEnv = DatabaseEnvironment.Open(testHome, cfg); - - Thread thread = new Thread(new ThreadStart(WriteThreadWithoutTxnCommit)); - thread.Start(); - thread.Join(); - testFailCheckEnv.FailCheck(); - testFailCheckEnv.Close(); - } - - public void WriteThreadWithoutTxnCommit() - { - Transaction txn = testFailCheckEnv.BeginTransaction(); - BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Env = testFailCheckEnv; - BTreeDatabase db = BTreeDatabase.Open("TestFailCheck.db", dbConfig, txn); - db.Close(); - txn.Commit(); - } - - public bool ThrdAlive(DbThreadID info, bool procOnly) - { - Process pcs = Process.GetProcessById(info.processID); - if (pcs.HasExited == true) - return false; - else if (procOnly) - return true; - ProcessThreadCollection thrds = pcs.Threads; - foreach (ProcessThread pcsThrd in thrds) - { - if (pcsThrd.Id == info.threadID) - { - /* - * We have to use the fully qualified name, ThreadState - * defaults to System.Threading.ThreadState. - */ - return (pcsThrd.ThreadState != - System.Diagnostics.ThreadState.Terminated); - } - } - // If we can't find the thread, we say it's not alive - return false; - } - - public DbThreadID SetThrdID() - { - DbThreadID threadID; - - int pid = Process.GetCurrentProcess().Id; - uint tid = (uint)AppDomain.GetCurrentThreadId(); - threadID = new DbThreadID(pid, tid); - return threadID; - } - - [Test] - public void TestFeedback() - { - testName = "TestFeedback"; - SetUpTest(true); - - // Open the environment. - DatabaseEnvironmentConfig cfg = - new DatabaseEnvironmentConfig(); - cfg.AutoCommit = true; - cfg.UseLocking = true; - cfg.UseLogging = true; - cfg.UseMPool = true; - cfg.UseTxns = true; - cfg.Create = true; - DatabaseEnvironment env = DatabaseEnvironment.Open(testHome, cfg); - - env.Feedback = new EnvironmentFeedbackDelegate( - EnvRecovery10PercentFeedback); - env.Feedback(EnvironmentFeedbackEvent.RECOVERY, 10); - - env.Close(); - } - - public void EnvRecovery10PercentFeedback( - EnvironmentFeedbackEvent opcode, int percent) - { - Assert.AreEqual(opcode, EnvironmentFeedbackEvent.RECOVERY); - Assert.AreEqual(10, percent); - } - - [Test] - public void TestMetadataDir() - { - testName = "TestMetadataDir"; - SetUpTest(true); - - DatabaseEnvironmentConfig cfg = - new DatabaseEnvironmentConfig(); - cfg.Create = true; - cfg.UseLocking = true; - cfg.UseLogging = true; - cfg.UseMPool = true; - cfg.UseReplication = true; - cfg.UseTxns = true; - - // Metadata Directory defaults to NULL - DatabaseEnvironment env = - DatabaseEnvironment.Open(testHome, cfg); - Assert.Null(env.MetadataDir); - env.Close(); - int cnt = Directory.GetFiles( - testHome, "__db.rep.system*").Length + - Directory.GetFiles( - testHome, "__db.rep.gen*").Length + - Directory.GetFiles( - testHome, "__db.rep.egen*").Length + - Directory.GetFiles( - testHome, "__db.rep.init*").Length; - Assert.Less(0, cnt); - - // Set Metadata Directory - Configuration.ClearDir(testHome); - string md = testHome + "/" + "Meta"; - Directory.CreateDirectory(md); - cfg.MetadataDir = "Meta"; - env = DatabaseEnvironment.Open(testHome, cfg); - Assert.AreEqual("Meta", env.MetadataDir); - env.Close(); - cnt = Directory.GetFiles( - testHome, "__db.rep.system*").Length + - Directory.GetFiles( - testHome, "__db.rep.gen*").Length + - Directory.GetFiles( - testHome, "__db.rep.egen*").Length + - Directory.GetFiles( - testHome, "__db.rep.init*").Length; - Assert.AreEqual(0, cnt); - cnt = - Directory.GetFiles(md, "__db.rep.system*").Length + - Directory.GetFiles(md, "__db.rep.gen*").Length + - Directory.GetFiles(md, "__db.rep.egen*").Length + - Directory.GetFiles(md, "__db.rep.init*").Length; - Assert.Less(0, cnt); - } - - [Test] - public void TestMutexSystemStats() - { - testName = "TestMutexSystemStats"; - SetUpTest(true); - - DatabaseEnvironmentConfig cfg = - new DatabaseEnvironmentConfig(); - cfg.Create = true; - cfg.UseLogging = true; - cfg.UseLocking = true; - cfg.UseMPool = true; - cfg.UseTxns = true; - cfg.MutexSystemCfg = new MutexConfig(); - cfg.MutexSystemCfg.Alignment = 512; - cfg.MutexSystemCfg.Increment = 128; - cfg.MutexSystemCfg.MaxMutexes = 150; - cfg.MutexSystemCfg.NumTestAndSetSpins = 10; - DatabaseEnvironment env = DatabaseEnvironment.Open(testHome, cfg); - - MutexStats stats = env.MutexSystemStats(); - env.PrintMutexSystemStats(true, true); - Assert.AreEqual(512, stats.Alignment); - Assert.AreEqual(stats.Count, stats.Available + stats.InUse); - Assert.LessOrEqual(stats.InUse, stats.MaxInUse); - Assert.AreNotEqual(0, stats.InitCount); - Assert.AreNotEqual(0, stats.Max); - Assert.AreNotEqual(0, stats.RegionMax); - Assert.AreNotEqual(0, stats.RegionSize); - Assert.AreEqual(0, stats.RegionWait); - Assert.AreEqual(10, stats.TASSpins); - ulong regionNoWait = stats.RegionNoWait; - - BTreeDatabaseConfig dbCfg = new BTreeDatabaseConfig(); - dbCfg.Creation = CreatePolicy.IF_NEEDED; - dbCfg.Env = env; - BTreeDatabase db = BTreeDatabase.Open(testName + ".db", dbCfg); - for (int i = 0; i < 1000; i++) - { - db.Put(new DatabaseEntry(BitConverter.GetBytes(i)), - new DatabaseEntry(BitConverter.GetBytes(i))); - stats = env.MutexSystemStats(); - } - Assert.LessOrEqual(regionNoWait, stats.RegionNoWait); - regionNoWait = stats.RegionNoWait; - - stats = env.MutexSystemStats(true); - env.PrintMutexSystemStats(); - stats = env.MutexSystemStats(); - Assert.GreaterOrEqual(regionNoWait, stats.RegionNoWait); - - db.Close(); - env.Close(); - } - - [Test] - public void TestLogFile() - { - testName = "TestLogFile"; - SetUpTest(true); - - // Open environment and configure logging subsystem. - DatabaseEnvironmentConfig cfg = - new DatabaseEnvironmentConfig(); - cfg.Create = true; - cfg.UseTxns = true; - cfg.AutoCommit = true; - cfg.UseLocking = true; - cfg.UseMPool = true; - cfg.UseLogging = true; - cfg.MPoolSystemCfg = new MPoolConfig(); - cfg.MPoolSystemCfg.CacheSize = - new CacheInfo(0, 1048576, 1); - cfg.LogSystemCfg = new LogConfig(); - cfg.LogSystemCfg.AutoRemove = false; - cfg.LogSystemCfg.BufferSize = 10240; - cfg.LogSystemCfg.Dir = "./"; - cfg.LogSystemCfg.FileMode = 755; - cfg.LogSystemCfg.ForceSync = true; - cfg.LogSystemCfg.InMemory = false; - cfg.LogSystemCfg.MaxFileSize = 1048576; - cfg.LogSystemCfg.NoBuffer = false; - cfg.LogSystemCfg.RegionSize = 204800; - cfg.LogSystemCfg.ZeroOnCreate = true; - DatabaseEnvironment env = DatabaseEnvironment.Open(testHome, cfg); - - // Open database. - Transaction allTxn = env.BeginTransaction(); - TransactionConfig txnConfig = new TransactionConfig(); - txnConfig.Name = "OpenTransaction"; - Transaction openTxn = env.BeginTransaction(txnConfig, allTxn); - BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Env = env; - BTreeDatabase db = BTreeDatabase.Open( - testName + ".db", dbConfig, openTxn); - - List activeTxns = - env.TransactionSystemStats().Transactions; - for (int i = 0; i < activeTxns.Count; i++) - if (activeTxns[i].Name == "OpenTransaction") - { - LSN lsn = new LSN( - activeTxns[i].Begun.LogFileNumber, - activeTxns[i].Begun.Offset); - env.LogFlush(lsn); - string fileName = env.LogFile(lsn); - } - - openTxn.Commit(); - - // Write "##" to log before putting data into database. - env.WriteToLog("##"); - - // Write 1000 records into database. - TransactionConfig writeTxnConfig = new TransactionConfig(); - writeTxnConfig.Name = "WriteTxn"; - Transaction writeTxn = env.BeginTransaction(writeTxnConfig, allTxn); - byte[] byteArr = new byte[1024]; - for (int i = 0; i < 1000; i++) - { - db.Put(new DatabaseEntry(BitConverter.GetBytes(i)), - new DatabaseEntry(byteArr), writeTxn); - env.LogFlush(); - env.WriteToLog("#" + i.ToString(), writeTxn); - } - - activeTxns = env.TransactionSystemStats().Transactions; - for (int i = 0; i < activeTxns.Count; i++) - if (activeTxns[i].Name == "WriteTxn") - { - LSN lsn = new LSN( - activeTxns[i].Begun.LogFileNumber, - activeTxns[i].Begun.Offset); - env.LogFlush(lsn); - string fileName = env.LogFile(lsn); - } - - writeTxn.Commit(); - db.Close(); - - // Write "##" after data has been put. - env.WriteToLog("##"); - - List logFiles = env.LogFiles(true); - - env.LogWrite(new DatabaseEntry(), true); - - env.RemoveUnusedLogFiles(); - - allTxn.Commit(); - env.Close(); - } - - [Test] - public void TestLogVerifyConfig() - { - testName = "TestLogVerifyConfig"; - SetUpTest(true); - - // Open enviornment. - DatabaseEnvironmentConfig cfg = - new DatabaseEnvironmentConfig(); - cfg.Create = true; - cfg.UseLocking = true; - cfg.UseLogging = true; - cfg.UseMPool = true; - cfg.UseTxns = true; - DatabaseEnvironment env = DatabaseEnvironment.Open( - testHome, cfg); - - // Open database. - Transaction txn = env.BeginTransaction(); - BTreeDatabaseConfig dbCfg = new BTreeDatabaseConfig(); - dbCfg.Env = env; - dbCfg.Creation = CreatePolicy.IF_NEEDED; - dbCfg.Duplicates = DuplicatesPolicy.UNSORTED; - BTreeDatabase db = BTreeDatabase.Open( - testName, dbCfg, txn); - txn.Commit(); - - // Do a lot of transactional operations to create logs. - DatabaseEntry key, data; - for (int i = 0; i < 5; i++) { - txn = env.BeginTransaction(); - switch (i) { - case 0: - /* - * Put 1000 records and commit the - * transaction. - */ - for (int j = 0; j < 1000;j++) { - key = new DatabaseEntry( - BitConverter.GetBytes(j)); - data = new DatabaseEntry( - BitConverter.GetBytes(j)); - db.Put(key, data, txn); - } - txn.Commit(); - break; - case 1: - /* - * Put 10 overflow records and commit - * the transaction. - */ - for (int j = 0; j < 10;j++) { - key = new DatabaseEntry( - BitConverter.GetBytes(j)); - data = new DatabaseEntry( - new byte[1024 * 4]); - db.Put(key, data, txn); - } - txn.Commit(); - break; - case 2: - /* - * Put 1000 records and abort the - * transaction. - */ - for (int j = 0; j < 1000;j++) { - key = new DatabaseEntry( - BitConverter.GetBytes(j)); - data = new DatabaseEntry( - BitConverter.GetBytes(j + 1)); - db.Put(key, data, txn); - } - txn.Abort(); - break; - case 3: - /* - * Put 10 overflow records and abort the - * transaction. - */ - for (int j = 10; j < 20;j++) { - key = new DatabaseEntry( - BitConverter.GetBytes(j)); - data = new DatabaseEntry( - new byte[1024 * 4]); - db.Put(key, data, txn); - } - txn.Abort(); - break; - case 4: - // Get 100 records. - for (int j = 100; j < 200; j++) { - key = new DatabaseEntry( - BitConverter.GetBytes(j)); - data = new DatabaseEntry( - BitConverter.GetBytes(j)); - db.GetBoth(key, data, txn); - } - txn.Commit(); - break; - } - } - - // Verfiy entire log. - string tempHome = testHome + "_tmp"; - Configuration.ClearDir(tempHome); - LogVerifyConfig logVerifyCfg = new LogVerifyConfig(); - logVerifyCfg.CacheSize = 1024 * 10; - logVerifyCfg.ContinueAfterFail = true; - logVerifyCfg.EnvHome = tempHome; - logVerifyCfg.Verbose = true; - Assert.AreEqual(0, env.LogVerify(logVerifyCfg)); - db.Close(); - env.Close(); - - // Verify specific database. - logVerifyCfg.DbFile = testName + ".db"; - logVerifyCfg.DbName = testName; - Assert.AreEqual(0, env.LogVerify(logVerifyCfg)); - - // Verify partial log. - DateTime time = DateTime.Now; - DateTime startTime = time.AddMinutes(-2); - DateTime endTime = time.AddMinutes(-1); - logVerifyCfg.StartTime = startTime; - logVerifyCfg.EndTime = endTime; - Assert.AreEqual(0, env.LogVerify(logVerifyCfg)); - - // Verfiy unexisting log. - logVerifyCfg.StartLsn = new LSN(1, 3); - logVerifyCfg.EndLsn = new LSN(2, 100); - Assert.AreEqual(0, env.LogVerify(logVerifyCfg)); - - Assert.AreEqual(logVerifyCfg.CacheSize, 1024 * 10); - Assert.AreEqual(logVerifyCfg.ContinueAfterFail, true); - Assert.AreEqual(logVerifyCfg.DbFile, testName + ".db"); - Assert.AreEqual(logVerifyCfg.DbName, testName); - Assert.AreEqual(logVerifyCfg.EndLsn.LogFileNumber, 2); - Assert.AreEqual(logVerifyCfg.EndLsn.Offset, 100); - Assert.AreEqual(logVerifyCfg.EndTime, endTime); - Assert.AreEqual(logVerifyCfg.EnvHome, testHome + "_tmp"); - Assert.AreEqual(logVerifyCfg.StartLsn.LogFileNumber, 1); - Assert.AreEqual(logVerifyCfg.StartLsn.Offset, 3); - Assert.AreEqual(logVerifyCfg.StartTime, startTime); - Assert.AreEqual(logVerifyCfg.Verbose, true); - } - - [Test] - public void TestOpen() - { - testName = "TestOpen"; - SetUpTest(true); - - DatabaseEnvironmentConfig cfg = - new DatabaseEnvironmentConfig(); - cfg.Create = true; - DatabaseEnvironment env = DatabaseEnvironment.Open(testHome, cfg); - - // Confirm that the environment is initialized. - Assert.IsNotNull(env); - - // Confirm the environment home directory. - Assert.AreEqual(testHome, env.Home); - - // Print statistics of the current environment. - env.PrintStats(); - - // Print statistics of all subsytems. - env.PrintSubsystemStats(); - - env.Close(); - } - - [Test] - public void TestMPoolSystemStats() - { - testName = "TestMPoolSystemStats"; - SetUpTest(true); - - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.AutoCommit = true; - envConfig.MPoolSystemCfg = new MPoolConfig(); - envConfig.MPoolSystemCfg.CacheSize = - new CacheInfo(0, 1048576, 3); - envConfig.Create = true; - envConfig.UseLocking = true; - envConfig.UseLogging = true; - envConfig.UseMPool = true; - envConfig.UseTxns = true; - - DatabaseEnvironment env = DatabaseEnvironment.Open( - testHome, envConfig); - - MPoolStats stats = env.MPoolSystemStats(); - env.PrintMPoolSystemStats(); - - Assert.AreEqual(0, stats.BlockedOperations); - Assert.AreEqual(0, stats.BucketsCheckedDuringAlloc); - Assert.AreEqual(3, stats.CacheRegions); - Assert.LessOrEqual(1048576, stats.CacheSettings.Bytes); - Assert.AreEqual(0, stats.CacheSettings.Gigabytes); - Assert.AreEqual(3, stats.CacheSettings.NCaches); - Assert.AreEqual(0, stats.CleanPages); - Assert.AreEqual(0, stats.CleanPagesEvicted); - Assert.AreEqual(0, stats.DirtyPages); - Assert.AreEqual(0, stats.DirtyPagesEvicted); - Assert.IsNotNull(stats.Files); - Assert.AreEqual(0, stats.FrozenBuffers); - Assert.AreEqual(0, stats.FrozenBuffersFreed); - Assert.LessOrEqual(stats.HashBuckets, - stats.HashBucketMutexes); - Assert.LessOrEqual(37, stats.HashBuckets); - Assert.LessOrEqual(0, stats.HashChainSearches); - Assert.AreEqual(0, stats.HashEntriesSearched); - Assert.AreEqual(0, stats.HashLockNoWait); - Assert.AreEqual(0, stats.HashLockWait); - Assert.AreEqual(0, stats.LongestHashChainSearch); - Assert.AreEqual(0, stats.MappedPages); - Assert.AreEqual(0, stats.MaxBucketsCheckedDuringAlloc); - Assert.AreEqual(0, stats.MaxBufferWrites); - Assert.AreEqual(0, stats.MaxBufferWritesSleep); - Assert.AreEqual(0, stats.MaxHashLockNoWait); - Assert.AreEqual(0, stats.MaxHashLockWait); - Assert.AreEqual(0, stats.MaxMMapSize); - Assert.AreEqual(0, stats.MaxOpenFileDescriptors); - Assert.AreEqual(0, stats.MaxPagesCheckedDuringAlloc); - Assert.AreEqual(0, stats.PageAllocations); - Assert.AreEqual(0, stats.Pages); - Assert.AreEqual(0, stats.PagesCheckedDuringAlloc); - Assert.LessOrEqual(0, stats.PagesCreatedInCache); - Assert.AreEqual(0, stats.PagesInCache); - Assert.AreEqual(0, stats.PagesNotInCache); - Assert.AreEqual(0, stats.PagesRead); - Assert.AreEqual(0, stats.PagesTrickled); - Assert.AreEqual(0, stats.PagesWritten); - Assert.AreNotEqual(0, stats.RegionLockNoWait); - Assert.AreEqual(0, stats.RegionLockWait); - Assert.LessOrEqual(0, stats.RegionSize); - Assert.AreNotEqual(0, stats.RegionMax); - Assert.AreEqual(0, stats.ThawedBuffers); - - Transaction txn = env.BeginTransaction(); - BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Env = env; - dbConfig.PageSize = 4096; - BTreeDatabase db = BTreeDatabase.Open( - testName + ".db", dbConfig, txn); - - stats = env.MPoolSystemStats(); - Assert.AreEqual(1, stats.Files.Count); - Assert.AreEqual(testName + ".db", stats.Files[0].FileName); - Assert.AreEqual(0, stats.Files[0].BackupSpins); - Assert.AreEqual(0, stats.Files[0].MappedPages); - Assert.AreEqual(0, stats.Files[0].PagesCreatedInCache); - Assert.AreEqual(0, stats.Files[0].PagesInCache); - Assert.AreEqual(4096, stats.Files[0].PageSize); - Assert.LessOrEqual(1, stats.Files[0].PagesNotInCache); - Assert.AreEqual(1, stats.Files[0].PagesRead); - Assert.AreEqual(0, stats.Files[0].PagesWritten); - - byte[] largeByte = new byte[1088576]; - for (int i = 0; i < 10; i++) - db.Put(new DatabaseEntry(BitConverter.GetBytes(i)), - new DatabaseEntry(largeByte), txn); - db.Put(new DatabaseEntry(largeByte), - new DatabaseEntry(largeByte), txn); - - stats = env.MPoolSystemStats(); - Assert.Less(0, stats.Files[0].PagesCreatedInCache); - Assert.Less(0, stats.Files[0].PagesInCache); - Assert.LessOrEqual(1, stats.Files[0].PagesNotInCache); - Assert.LessOrEqual(1, stats.Files[0].PagesRead); - Assert.Less(0, stats.Files[0].PagesWritten); - - txn.Commit(); - db.Close(); - - // Clean the stats after printing. - stats = env.MPoolSystemStats(true); - env.PrintMPoolSystemStats(true, true); - stats = env.MPoolSystemStats(); - env.PrintMPoolSystemStats(true, true, true); - - env.Close(); - } - - [Test] - public void TestRemove() - { - testName = "TestRemove"; - SetUpTest(true); - - // Open new environment. - DatabaseEnvironmentConfig envConig = - new DatabaseEnvironmentConfig(); - envConig.Create = true; - envConig.ErrorPrefix = testFixtureName + ":" + testName; - DatabaseEnvironment env = DatabaseEnvironment.Open( - testHome, envConig); - env.Close(); - - // Remove the existing environment. - DatabaseEnvironment.Remove(testHome); - - // Confirm that the __db.001 is removed. - Assert.IsFalse(File.Exists(testHome + "__db.001")); - } - - [Test] - public void TestRemoveCorruptedEnv() - { - testName = "TestRemoveCorruptedEnv"; - SetUpTest(true); - - // Open new environment. - DatabaseEnvironmentConfig envConig = - new DatabaseEnvironmentConfig(); - envConig.Create = true; - envConig.ErrorPrefix = testFixtureName + ":" + testName; - DatabaseEnvironment env = DatabaseEnvironment.Open(testHome, envConig); - - // Panic the environment. - env.Panic(); - - // Remove the corrupted environment. - DatabaseEnvironment.Remove(testHome, true); - - // Confirm that the __db.001 is removed. - Assert.IsFalse(File.Exists(testHome + "__db.001")); - } - - [Test, ExpectedException(typeof(ExpectedTestException))] - public void TestRenameDB() - { - testName = "TestRenameDB"; - SetUpTest(true); - - RenameDB(testHome, testName, false); - } - - [Test, ExpectedException(typeof(ExpectedTestException))] - public void TestRenameDBWithTxn() - { - testName = "TestRenameDBWithTxn"; - SetUpTest(true); - - RenameDB(testHome, testName, true); - } - - public void RenameDB(string home, string name, bool ifTxn) - { - string dbFileName = name + ".db"; - string dbName = "db1"; - string dbNewName = "db2"; - - Configuration.ClearDir(home); - - DatabaseEnvironmentConfig envConig = - new DatabaseEnvironmentConfig(); - envConig.Create = true; - envConig.UseTxns = true; - envConig.UseLogging = true; - envConig.UseMPool = true; - DatabaseEnvironment env = DatabaseEnvironment.Open( - home, envConig); - - Transaction openTxn = env.BeginTransaction(); - BTreeDatabaseConfig dbConfig = - new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Env = env; - BTreeDatabase db = BTreeDatabase.Open( - dbFileName, dbName, dbConfig, openTxn); - db.Close(); - openTxn.Commit(); - - // Open the database. - if (ifTxn == false) - env.RenameDB(dbFileName, dbName, dbNewName, true); - else - { - Transaction renameTxn = env.BeginTransaction(); - env.RenameDB(dbFileName, dbName, dbNewName, false, renameTxn); - renameTxn.Commit(); - } - - // Confirm that the database are renamed. - Transaction reopenTxn = env.BeginTransaction(); - try - { - Database db1 = Database.Open( - dbFileName, new DatabaseConfig()); - db1.Close(); - } - catch (DatabaseException) - { - throw new ExpectedTestException(); - } - finally - { - reopenTxn.Commit(); - env.Close(); - } - } - - [Test] - public void TestResetFileID() - { - testName = "TestResetFileID"; - SetUpTest(true); - string dbFileName = testName + ".db"; - string dbNewFileName = testName + "_new.db"; - - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseMPool = true; - DatabaseEnvironment env = DatabaseEnvironment.Open( - testHome, envConfig); - - // Opening a new database. - BTreeDatabaseConfig dbConfig = - new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Env = env; - BTreeDatabase db = BTreeDatabase.Open( - dbFileName, dbConfig); - db.Close(); - - // Copy the physical database file. - File.Copy(testHome + "/" + dbFileName, - testHome + "/" + dbNewFileName); - - // Reset the file ID. - env.ResetFileID(dbNewFileName, false); - - // Open the exisiting database in copied database file. - BTreeDatabaseConfig cfg = new BTreeDatabaseConfig(); - cfg.Creation = CreatePolicy.NEVER; - cfg.Env = env; - BTreeDatabase newDB = BTreeDatabase.Open( - dbNewFileName, cfg); - newDB.Close(); - env.Close(); - } - - [Test, ExpectedException(typeof(ExpectedTestException))] - public void TestRemoveDB() - { - testName = "TestRemoveDB"; - SetUpTest(true); - - RmDBWithoutTxn(testHome, testName, false); - } - - [Test, ExpectedException(typeof(ExpectedTestException))] - public void TestRemoveDBWithAutoCommit() - { - testName = "TestRemoveDBWithAutoCommit"; - SetUpTest(true); - - RmDBWithoutTxn(testHome, testName, true); - } - - [Test, ExpectedException(typeof(ExpectedTestException))] - public void TestRemoveDBWithinTxn() - { - testName = "TestRemoveDBWithinTxn"; - SetUpTest(true); - string dbFileName = testName + ".db"; - string dbName1 = testName + "1"; - string dbName2 = testName + "2"; - - // Open environment. - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseMPool = true; - envConfig.UseTxns = true; - envConfig.UseLogging = true; - DatabaseEnvironment env = DatabaseEnvironment.Open( - testHome, envConfig); - Transaction txn = env.BeginTransaction(); - - // Create two databases in the environment. - BTreeDatabaseConfig dbConfig = - new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Env = env; - BTreeDatabase btreeDB1 = BTreeDatabase.Open( - dbFileName, dbName1, dbConfig, txn); - btreeDB1.Close(); - BTreeDatabase btreeDB2 = BTreeDatabase.Open( - dbFileName, dbName2, dbConfig, txn); - btreeDB2.Close(); - - // Remove one database from the environment. - env.RemoveDB(dbFileName, dbName2, false, txn); - - // Try to open the existing database. - DatabaseConfig cfg = new DatabaseConfig(); - cfg.Env = env; - Database db1 = Database.Open(dbFileName, dbName1, cfg, txn); - db1.Close(); - - /* - * Attempting to open the removed database should - * cause error. - */ - try - { - Database db2 = Database.Open( - dbFileName, dbName2, cfg, txn); - db2.Close(); - } - catch (DatabaseException) - { - throw new ExpectedTestException(); - } - finally - { - txn.Commit(); - env.Close(); - } - } - - public void RmDBWithoutTxn(string home, string dbName, - bool ifAutoCommit) - { - string dbFileName = dbName + ".db"; - string dbName1 = dbName + "1"; - string dbName2 = dbName + "2"; - - // Open environment. - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseMPool = true; - if (ifAutoCommit == true) - { - envConfig.AutoCommit = true; - envConfig.UseTxns = true; - } - - DatabaseEnvironment env = DatabaseEnvironment.Open( - home, envConfig); - - // Create two databases in the environment. - BTreeDatabaseConfig dbConfig = - new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Env = env; - BTreeDatabase btreeDB1 = BTreeDatabase.Open( - dbFileName, dbName1, dbConfig); - btreeDB1.Close(); - BTreeDatabase btreeDB2 = BTreeDatabase.Open( - dbFileName, dbName2, dbConfig); - btreeDB2.Close(); - - // Remove one database from the environment. - env.RemoveDB(dbFileName, dbName2, false); - - // Try to open the existing database. - DatabaseConfig cfg = new DatabaseConfig(); - cfg.Env = env; - Database db1 = Database.Open(dbFileName, dbName1, cfg); - db1.Close(); - - /* - * Attempting to open the removed database should - * cause error. - */ - try - { - Database db2 = Database.Open( - dbFileName, dbName2, cfg); - db2.Close(); - } - catch (DatabaseException) - { - throw new ExpectedTestException(); - } - finally - { - env.Close(); - } - } - - [Test] - public void TestTransactionSystemStats() - { - testName = "TestTransactionSystemStats"; - SetUpTest(true); - - TransactionStats stats; - BTreeDatabase db; - Transaction openTxn = null; - - // Open an environment. - long dateTime; - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.MaxTransactions = 50; - envConfig.UseLogging = true; - envConfig.UseLocking = true; - envConfig.UseMPool = true; - envConfig.UseTxns = true; - envConfig.TxnNoSync = false; - envConfig.TxnNoWait = true; - envConfig.TxnSnapshot = true; - envConfig.TxnTimestamp = DateTime.Now; - envConfig.TxnWriteNoSync = false; - DatabaseEnvironment env = DatabaseEnvironment.Open( - testHome, envConfig); - - try - { - try - { - // Confirm initial transaction subsystem statistics. - stats = env.TransactionSystemStats(); - env.PrintTransactionSystemStats(true, true); - Assert.AreEqual(0, stats.Aborted); - Assert.AreEqual(0, stats.Active); - Assert.AreEqual(0, stats.Begun); - Assert.AreEqual(0, stats.Committed); - Assert.AreEqual(0, stats.InitTxns); - Assert.AreEqual(0, stats.LastCheckpoint.LogFileNumber); - Assert.AreEqual(0, stats.LastCheckpoint.Offset); - Assert.AreEqual(50, stats.MaxTransactions); - Assert.AreNotEqual(0, stats.RegionSize); - Assert.AreEqual(0, stats.Transactions.Count); - } - catch (AssertionException e) - { - throw e; - } - - try - { - //Begin a transaction called openTxn and open a database. - TransactionConfig openTxnCfg = new TransactionConfig(); - openTxnCfg.Name = "openTxn"; - openTxn = env.BeginTransaction(openTxnCfg); - openTxn.Priority = 50; - BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Env = env; - db = BTreeDatabase.Open(testName + ".db", dbConfig, openTxn); - } - catch (DatabaseException e) - { - if (openTxn != null) - openTxn.Abort(); - throw e; - } - - try - { - // At least there is one transaction that is alive. - env.Checkpoint(); - stats = env.TransactionSystemStats(); - env.PrintTransactionSystemStats(); - Assert.AreNotEqual(0, stats.Active); - Assert.AreNotEqual(0, stats.Transactions.Count); - Assert.AreNotEqual(0, stats.Transactions.Capacity); - Assert.AreNotEqual(0, stats.RegionLockNoWait); - dateTime = stats.LastCheckpointTime; - - // Begin an embedded transaction called putTxn. - TransactionConfig putTxnCfg = - new TransactionConfig(); - putTxnCfg.Name = "putTxn"; - putTxnCfg.NoWait = false; - Transaction putTxn = env.BeginTransaction( - putTxnCfg, openTxn); - putTxn.Priority = 50; - - try - { - // Put some records into database within putTxn. - for (int i = 0; i < 50; i++) - db.Put(new DatabaseEntry(BitConverter.GetBytes(i)), - new DatabaseEntry(BitConverter.GetBytes(i)), putTxn); - stats = env.TransactionSystemStats(); - Assert.AreNotEqual(0, stats.MaxActive); - Assert.AreNotEqual(0, stats.MaxTransactions); - Assert.AreEqual(0, stats.MaxSnapshot); - Assert.AreEqual(0, stats.Snapshot); - Assert.AreEqual(stats.Begun, - stats.Aborted + stats.Active + stats.Committed); - Assert.AreEqual(2, stats.Transactions.Count); - - /* - * Both of LogFileNumber and Offset in active transaction - * couldn't be 0. - */ - uint logFileNumbers = 0; - uint offSets = 0; - for (int i = 0; i < stats.Transactions.Count;i++) - { - logFileNumbers += stats.Transactions[i].Begun.LogFileNumber; - offSets += stats.Transactions[i].Begun.Offset; - } - Assert.AreNotEqual(0, logFileNumbers); - Assert.AreNotEqual(0, offSets); - - // All active transactions are run by the same process and thread. - - Assert.AreEqual(stats.Transactions[0].ThreadID, - stats.Transactions[1].ThreadID); - Assert.AreEqual(stats.Transactions[0].ProcessID, - stats.Transactions[1].ProcessID); - - Assert.AreEqual(50, stats.Transactions[0].Priority); - Assert.AreEqual(50, stats.Transactions[1].Priority); - - /* - * Find the openTxn in active transactions, which is the - * parent transaction of putTxn. - */ - int parentPos = 0; - if (stats.Transactions[0].Name == "putTxn") - parentPos = 1; - - // putTxn's parent id should be the openTxn. - Assert.AreEqual(stats.Transactions[parentPos].ID, - stats.Transactions[1 - parentPos].ParentID); - - // Other stats should be an positive integer. - for (int i = 0; i < stats.Transactions.Count - 1; i++) - { - Assert.LessOrEqual(0, - stats.Transactions[i].BufferCopiesInCache); - Assert.LessOrEqual(0, - stats.Transactions[i].SnapshotReads.LogFileNumber); - Assert.LessOrEqual(0, - stats.Transactions[i].SnapshotReads.Offset); - Assert.IsNotNull(stats.Transactions[i].GlobalID); - } - - // Commit putTxn. - putTxn.Commit(); - } - catch (DatabaseException e) - { - putTxn.Abort(); - throw e; - } - - stats = env.TransactionSystemStats(); - Assert.AreNotEqual(0, stats.LastCheckpoint.LogFileNumber); - Assert.AreNotEqual(0, stats.LastCheckpoint.Offset); - Assert.AreEqual(dateTime, stats.LastCheckpointTime); - - openTxn.Commit(); - } - catch (DatabaseException e) - { - openTxn.Abort(); - throw e; - } - finally - { - db.Close(); - } - } - finally - { - env.Close(); - } - } - - /* - * Configure an environment. Here only configure those that could be - * set before environment open. - */ - public void Config(XmlElement xmlElem, - ref DatabaseEnvironmentConfig envConfig, bool compulsory, - bool logging, bool locking, bool mutex, bool mpool, bool replication) - { - XmlNode childNode; - - // Configure environment without any subsystems. - DatabaseEnvironmentConfigTest.Config(xmlElem, ref envConfig, compulsory); - - // Configure environment with logging subsystem. - if (logging == true) - { - childNode = XMLReader.GetNode(xmlElem, "LogConfig"); - envConfig.LogSystemCfg = new LogConfig(); - LogConfigTest.Config((XmlElement)childNode, - ref envConfig.LogSystemCfg, compulsory); - } - - // Configure environment with locking subsystem. - if (locking == true) - { - childNode = XMLReader.GetNode(xmlElem, "LockingConfig"); - envConfig.LockSystemCfg = new LockingConfig(); - LockingConfigTest.Config((XmlElement)childNode, - ref envConfig.LockSystemCfg, compulsory); - } - - // Configure environment with mutex subsystem. - if (mutex == true) - { - childNode = XMLReader.GetNode(xmlElem, "MutexConfig"); - envConfig.MutexSystemCfg = new MutexConfig(); - MutexConfigTest.Config((XmlElement)childNode, - ref envConfig.MutexSystemCfg, compulsory); - } - - if (mpool == true) - { - childNode = XMLReader.GetNode(xmlElem, "MPoolConfig"); - envConfig.MPoolSystemCfg = new MPoolConfig(); - MPoolConfigTest.Config((XmlElement)childNode, - ref envConfig.MPoolSystemCfg, compulsory); - } - - // Configure environment with replication. - if (replication == true) - { - childNode = XMLReader.GetNode(xmlElem, "ReplicationConfig"); - envConfig.RepSystemCfg = new ReplicationConfig(); - ReplicationConfigTest.Config((XmlElement)childNode, - ref envConfig.RepSystemCfg, compulsory); - } - } - - /* - * Confirm the fields/properties in the environment. - * Those set by setting functions are not checked here. - */ - public static void Confirm(XmlElement xmlElement, - DatabaseEnvironment env, bool compulsory, - bool logging, bool locking, bool mutex, bool mpool, - bool replication) - { - XmlElement childElem; - CacheInfo cacheInfo = new CacheInfo(0, 0, 0); - - // Confirm environment configuration. - Configuration.ConfirmBool(xmlElement, "AutoCommit", - env.AutoCommit, compulsory); - Configuration.ConfirmBool(xmlElement, "CDB_ALLDB", - env.CDB_ALLDB, compulsory); - Configuration.ConfirmBool(xmlElement, "Create", - env.Create, compulsory); - Configuration.ConfirmStringList(xmlElement, "DataDirs", - env.DataDirs, compulsory); - Configuration.ConfirmString(xmlElement, "ErrorPrefix", - env.ErrorPrefix, compulsory); - Configuration.ConfirmBool(xmlElement, "ForceFlush", - env.ForceFlush, compulsory); - Configuration.ConfirmBool(xmlElement, "FreeThreaded", - env.FreeThreaded, compulsory); - Configuration.ConfirmBool(xmlElement, "InitRegions", - env.InitRegions, compulsory); - Configuration.ConfirmString(xmlElement, "IntermediateDirMode", - env.IntermediateDirMode, compulsory); - Configuration.ConfirmBool(xmlElement, "Lockdown", - env.Lockdown, compulsory); - Configuration.ConfirmUint(xmlElement, "LockTimeout", - env.LockTimeout, compulsory); - Configuration.ConfirmUint(xmlElement, "MaxTransactions", - env.MaxTransactions, compulsory); - Configuration.ConfirmBool(xmlElement, "NoBuffer", - env.NoBuffer, compulsory); - Configuration.ConfirmBool(xmlElement, "NoLocking", - env.NoLocking, compulsory); - Configuration.ConfirmBool(xmlElement, "NoMMap", - env.NoMMap, compulsory); - Configuration.ConfirmBool(xmlElement, "NoPanic", - env.NoPanic, compulsory); - Configuration.ConfirmBool(xmlElement, "Overwrite", - env.Overwrite, compulsory); - Configuration.ConfirmBool(xmlElement, "Private", - env.Private, compulsory); - Configuration.ConfirmBool(xmlElement, "Register", - env.Register, compulsory); - Configuration.ConfirmBool(xmlElement, "RunFatalRecovery", - env.RunFatalRecovery, compulsory); - Configuration.ConfirmBool(xmlElement, "RunRecovery", - env.RunRecovery, compulsory); - Configuration.ConfirmBool(xmlElement, "SystemMemory", - env.SystemMemory, compulsory); - Configuration.ConfirmString(xmlElement, "TempDir", - env.TempDir, compulsory); - Configuration.ConfirmBool(xmlElement, "TimeNotGranted", - env.TimeNotGranted, compulsory); - Configuration.ConfirmBool(xmlElement, "TxnNoSync", - env.TxnNoSync, compulsory); - Configuration.ConfirmBool(xmlElement, "TxnNoWait", - env.TxnNoWait, compulsory); - Configuration.ConfirmBool(xmlElement, "TxnSnapshot", - env.TxnSnapshot, compulsory); - Configuration.ConfirmDateTime(xmlElement, "TxnTimestamp", - env.TxnTimestamp, compulsory); - Configuration.ConfirmBool(xmlElement, "TxnWriteNoSync", - env.TxnWriteNoSync, compulsory); - Configuration.ConfirmBool(xmlElement, "UseMVCC", - env.UseMVCC, compulsory); - Configuration.ConfirmBool(xmlElement, "UseCDB", - env.UsingCDB, compulsory); - Configuration.ConfirmBool(xmlElement, "UseLocking", - env.UsingLocking, compulsory); - Configuration.ConfirmBool(xmlElement, "UseLogging", - env.UsingLogging, compulsory); - Configuration.ConfirmBool(xmlElement, "UseMPool", - env.UsingMPool, compulsory); - Configuration.ConfirmBool(xmlElement, "UseReplication", - env.UsingReplication, compulsory); - Configuration.ConfirmBool(xmlElement, "UseTxns", - env.UsingTxns, compulsory); - env.Verbosity = new VerboseMessages(); - Configuration.ConfirmVerboseMessages(xmlElement, - "Verbosity", env.Verbosity, compulsory); - Configuration.ConfirmBool(xmlElement, "YieldCPU", - env.YieldCPU, compulsory); - - /* - * If the locking subsystem is set, check the - * field/properties set in LockingConfig. - */ - if (locking == true) - { - childElem = (XmlElement)XMLReader.GetNode( - xmlElement, "LockingConfig"); - Configuration.ConfirmByteMatrix(childElem, - "Conflicts", env.LockConflictMatrix, - compulsory); - Configuration.ConfirmDeadlockPolicy( - childElem, "DeadlockResolution", - env.DeadlockResolution, compulsory); - Configuration.ConfirmUint(childElem, - "Partitions", env.LockPartitions, - compulsory); - Configuration.ConfirmUint(childElem, - "MaxLockers", env.MaxLockers, compulsory); - Configuration.ConfirmUint(childElem, - "MaxLocks", env.MaxLocks, compulsory); - Configuration.ConfirmUint(childElem, - "MaxObjects", env.MaxObjects, compulsory); - } - - /* - * If the locking subsystem is set, check the - * field/properties set in LogConfig. - */ - if (logging == true) - { - childElem = (XmlElement)XMLReader.GetNode( - xmlElement, "LogConfig"); - Configuration.ConfirmBool(childElem, - "AutoRemove", env.LogAutoRemove, - compulsory); - Configuration.ConfirmUint(childElem, - "BufferSize", env.LogBufferSize, - compulsory); - Configuration.ConfirmString(childElem, - "Dir", env.LogDir, compulsory); - Configuration.ConfirmInt(childElem, - "FileMode", env.LogFileMode, compulsory); - Configuration.ConfirmBool(childElem, - "ForceSync", env.LogForceSync, compulsory); - Configuration.ConfirmBool(childElem, - "InMemory", env.LogInMemory, compulsory); - Configuration.ConfirmBool(childElem, - "NoBuffer", env.LogNoBuffer, compulsory); - Configuration.ConfirmUint(childElem, - "RegionSize", env.LogRegionSize, - compulsory); - Configuration.ConfirmBool(childElem, - "ZeroOnCreate", env.LogZeroOnCreate, - compulsory); - Configuration.ConfirmUint(childElem, - "MaxFileSize", env.MaxLogFileSize, - compulsory); - } - - /* - * If the locking subsystem is set, check the - * field/properties set in MutexConfig. - */ - if (mutex == true) - { - childElem = (XmlElement)XMLReader.GetNode( - xmlElement, "MutexConfig"); - Configuration.ConfirmUint(childElem, - "Alignment", env.MutexAlignment, - compulsory); - Configuration.ConfirmUint(childElem, - "MaxMutexes", env.MaxMutexes, compulsory); - try - { - Configuration.ConfirmUint(childElem, - "Increment", env.MutexIncrement, - compulsory); - } - catch (AssertionException) - { - Assert.AreEqual(0, env.MutexIncrement); - } - - Configuration.ConfirmUint(childElem, - "NumTestAndSetSpins", - env.NumTestAndSetSpins, compulsory); - } - - if (mpool == true) - { - childElem = (XmlElement)XMLReader.GetNode( - xmlElement, "MPoolConfig"); - Configuration.ConfirmCacheSize(childElem, - "CacheSize", env.CacheSize, compulsory); - if (env.UsingMPool == false) - Configuration.ConfirmCacheSize(childElem, - "MaxCacheSize", env.MaxCacheSize, compulsory); - Configuration.ConfirmInt(childElem, - "MaxOpenFiles", env.MaxOpenFiles, compulsory); - Configuration.ConfirmMaxSequentialWrites(childElem, - "MaxSequentialWrites", env.SequentialWritePause, - env.MaxSequentialWrites, compulsory); - Configuration.ConfirmUint(childElem, - "MMapSize", env.MMapSize, compulsory); - } - - if (replication == true) - { - childElem = (XmlElement)XMLReader.GetNode( - xmlElement, "ReplicationConfig"); - Configuration.ConfirmUint(childElem, - "AckTimeout", env.RepAckTimeout, compulsory); - Configuration.ConfirmBool(childElem, - "BulkTransfer", env.RepBulkTransfer, compulsory); - Configuration.ConfirmUint(childElem, - "CheckpointDelay", env.RepCheckpointDelay, compulsory); - Configuration.ConfirmUint(childElem, - "ConnectionRetry", env.RepConnectionRetry, compulsory); - Configuration.ConfirmBool(childElem, - "DelayClientSync", env.RepDelayClientSync, compulsory); - Configuration.ConfirmUint(childElem, - "ElectionRetry", env.RepElectionRetry, compulsory); - Configuration.ConfirmUint(childElem, - "ElectionTimeout", env.RepElectionTimeout, compulsory); - Configuration.ConfirmUint(childElem, - "FullElectionTimeout", env.RepFullElectionTimeout,compulsory); - Configuration.ConfirmUint(childElem, - "HeartbeatMonitor", env.RepHeartbeatMonitor, compulsory); - Configuration.ConfirmUint(childElem, - "HeartbeatSend", env.RepHeartbeatSend, compulsory); - Configuration.ConfirmUint(childElem, - "LeaseTimeout", env.RepLeaseTimeout, compulsory); - Configuration.ConfirmBool(childElem, - "AutoInit", env.RepAutoInit, compulsory); - Configuration.ConfirmBool(childElem, - "NoBlocking", env.RepNoBlocking, compulsory); - Configuration.ConfirmUint(childElem, - "Priority", env.RepPriority, compulsory); - Configuration.ConfirmAckPolicy(childElem, - "RepMgrAckPolicy", env.RepMgrAckPolicy, compulsory); - Configuration.ConfirmBool(childElem, - "Strict2Site", env.RepStrict2Site, compulsory); - Configuration.ConfirmBool(childElem, - "UseMasterLeases", env.RepUseMasterLeases, compulsory); - } - } - } -} diff --git a/test/csharp/DatabaseExceptionTest.cs b/test/csharp/DatabaseExceptionTest.cs deleted file mode 100644 index af31b5e00..000000000 --- a/test/csharp/DatabaseExceptionTest.cs +++ /dev/null @@ -1,265 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Threading; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class DatabaseExceptionTest : CSharpTestFixture - { - [TestFixtureSetUp] - public void SetUpTestFixture() { - testFixtureName = "DatabaseExceptionTest"; - base.SetUpTestfixture(); - } - - [Test] - public void TestDB_REP_DUPMASTER() - { - testName = "TestDB_REP_DUPMASTER"; - SetUpTest(false); - DatabaseException.ThrowException(ErrorCodes.DB_REP_DUPMASTER); - } - - [Test] - public void TestDB_REP_HOLDELECTION() - { - testName = "TestDB_REP_HOLDELECTION"; - SetUpTest(false); - DatabaseException.ThrowException(ErrorCodes.DB_REP_HOLDELECTION); - } - - [Test] - public void TestDB_REP_IGNORE() - { - testName = "TestDB_REP_IGNORE"; - SetUpTest(false); - DatabaseException.ThrowException(ErrorCodes.DB_REP_IGNORE); - } - - [Test] - public void TestDB_REP_ISPERM() - { - testName = "TestDB_REP_ISPERM"; - SetUpTest(false); - DatabaseException.ThrowException(ErrorCodes.DB_REP_ISPERM); - } - - [Test] - public void TestDB_REP_JOIN_FAILURE() - { - testName = "TestDB_REP_JOIN_FAILURE"; - SetUpTest(false); - DatabaseException.ThrowException(ErrorCodes.DB_REP_JOIN_FAILURE); - } - - [Test] - public void TestDB_REP_NEWSITE() - { - testName = "TestDB_REP_NEWSITE"; - SetUpTest(false); - DatabaseException.ThrowException(ErrorCodes.DB_REP_NEWSITE); - } - - [Test] - public void TestDB_REP_NOTPERM() - { - testName = "TestDB_REP_NOTPERM"; - SetUpTest(false); - DatabaseException.ThrowException(ErrorCodes.DB_REP_NOTPERM); - } - - [Test] - public void TestDeadlockException() - { - testName = "TestDeadlockException"; - SetUpTest(false); - try - { - DatabaseException.ThrowException(ErrorCodes.DB_LOCK_DEADLOCK); - } - catch (DeadlockException e) - { - Assert.AreEqual(ErrorCodes.DB_LOCK_DEADLOCK, e.ErrorCode); - } - } - - [Test] - public void TestForeignConflictException() - { - testName = "TestForeignConflictException"; - SetUpTest(false); - try - { - DatabaseException.ThrowException(ErrorCodes.DB_FOREIGN_CONFLICT); - } - catch (ForeignConflictException e) - { - Assert.AreEqual(ErrorCodes.DB_FOREIGN_CONFLICT, e.ErrorCode); - } - } - - [Test] - public void TestKeyEmptyException() - { - testName = "TestKeyEmptyException"; - SetUpTest(false); - try - { - DatabaseException.ThrowException(ErrorCodes.DB_KEYEMPTY); - } - catch (KeyEmptyException e) - { - Assert.AreEqual(ErrorCodes.DB_KEYEMPTY, e.ErrorCode); - } - } - - [Test] - public void TestKeyExistException() - { - testName = "TestKeyExistException"; - SetUpTest(false); - try - { - DatabaseException.ThrowException(ErrorCodes.DB_KEYEXIST); - } - catch (KeyExistException e) - { - Assert.AreEqual(ErrorCodes.DB_KEYEXIST, e.ErrorCode); - } - } - - [Test] - public void TestLeaseExpiredException() - { - testName = "TestLeaseExpiredException"; - SetUpTest(false); - try - { - DatabaseException.ThrowException(ErrorCodes.DB_REP_LEASE_EXPIRED); - } - catch (LeaseExpiredException e) - { - Assert.AreEqual(ErrorCodes.DB_REP_LEASE_EXPIRED, e.ErrorCode); - } - } - - [Test] - public void TestLockNotGrantedException() - { - testName = "TestLockNotGrantedException"; - SetUpTest(false); - try - { - DatabaseException.ThrowException(ErrorCodes.DB_LOCK_NOTGRANTED); - } - catch (LockNotGrantedException e) - { - Assert.AreEqual(ErrorCodes.DB_LOCK_NOTGRANTED, e.ErrorCode); - } - } - - [Test] - public void TestNotFoundException() - { - testName = "TestNotFoundException"; - SetUpTest(false); - try - { - DatabaseException.ThrowException(ErrorCodes.DB_NOTFOUND); - } - catch (NotFoundException e) - { - Assert.AreEqual(ErrorCodes.DB_NOTFOUND, e.ErrorCode); - } - } - - [Test] - public void TestOldVersionException() - { - testName = "TestOldVersionException"; - SetUpTest(false); - try - { - DatabaseException.ThrowException(ErrorCodes.DB_OLD_VERSION); - } - catch (OldVersionException e) - { - Assert.AreEqual(ErrorCodes.DB_OLD_VERSION, e.ErrorCode); - } - } - - [Test] - public void TestPageNotFoundException() - { - testName = "TestPageNotFoundException"; - SetUpTest(false); - try - { - DatabaseException.ThrowException(ErrorCodes.DB_PAGE_NOTFOUND); - } - catch (PageNotFoundException e) - { - Assert.AreEqual(ErrorCodes.DB_PAGE_NOTFOUND, e.ErrorCode); - } - } - - [Test] - public void TestRunRecoveryException() - { - testName = "TestRunRecoveryException"; - SetUpTest(false); - try - { - DatabaseException.ThrowException(ErrorCodes.DB_RUNRECOVERY); - } - catch (RunRecoveryException e) - { - Assert.AreEqual(ErrorCodes.DB_RUNRECOVERY, e.ErrorCode); - } - - } - - [Test] - public void TestVerificationException() - { - testName = "TestVerificationException"; - SetUpTest(false); - try - { - DatabaseException.ThrowException(ErrorCodes.DB_VERIFY_BAD); - } - catch (VerificationException e) - { - Assert.AreEqual(ErrorCodes.DB_VERIFY_BAD, e.ErrorCode); - } - } - - [Test] - public void TestVersionMismatchException() - { - testName = "TestVersionMismatchException"; - SetUpTest(false); - try - { - DatabaseException.ThrowException(ErrorCodes.DB_VERSION_MISMATCH); - } - catch (VersionMismatchException e) - { - Assert.AreEqual(ErrorCodes.DB_VERSION_MISMATCH, e.ErrorCode); - } - } - } -} diff --git a/test/csharp/DatabaseTest.cs b/test/csharp/DatabaseTest.cs deleted file mode 100644 index 2bd1e4118..000000000 --- a/test/csharp/DatabaseTest.cs +++ /dev/null @@ -1,109 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - public class DatabaseTest : CSharpTestFixture - { - public static uint getDefaultCacheSizeBytes() - { - uint defaultCacheSizeBytes; - - string fixtureHome = "./TestOut/DatabaseTest"; - string dbName = fixtureHome + "/" + "getDefaultCacheSizeBytes" + ".db"; - - Configuration.ClearDir(fixtureHome); - - BTreeDatabaseConfig cfg = new BTreeDatabaseConfig(); - cfg.Creation = CreatePolicy.ALWAYS; - using (BTreeDatabase db = BTreeDatabase.Open(dbName, cfg)) - { - defaultCacheSizeBytes = db.CacheSize.Bytes; - } - - return defaultCacheSizeBytes; - } - - public static ByteOrder getMachineByteOrder() - { - string fixtureHome = "./TestOut/DatabaseTest"; - string dbName = fixtureHome + "/" + "getMachineByteOrder" + ".db"; - - Configuration.ClearDir(fixtureHome); - - ByteOrder byteOrder; - - BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); - dbConfig.ByteOrder = ByteOrder.MACHINE; - dbConfig.Creation = CreatePolicy.ALWAYS; - using (BTreeDatabase db = BTreeDatabase.Open(dbName, dbConfig)) - { - byteOrder = db.Endianness; - } - return byteOrder; - } - - public static void Confirm(XmlElement xmlElement, Database db, bool compulsory) - { - uint defaultBytes; - defaultBytes = getDefaultCacheSizeBytes(); - - Configuration.ConfirmBool(xmlElement, "AutoCommit", - db.AutoCommit, compulsory); - Configuration.ConfirmCacheSize(xmlElement, "CacheSize", - db.CacheSize, defaultBytes, compulsory); - Configuration.ConfirmCreatePolicy(xmlElement, "Creation", - db.Creation, compulsory); - Configuration.ConfirmString(xmlElement, "DatabaseName", - db.DatabaseName, compulsory); - Configuration.ConfirmBool(xmlElement, "DoChecksum", - db.DoChecksum, compulsory); - // Encrypted and EncryptWithAES? - Configuration.ConfirmByteOrder(xmlElement, "ByteOrder", - db.Endianness, compulsory); - Configuration.ConfirmString(xmlElement, "ErrorPrefix", - db.ErrorPrefix, compulsory); - // File name is confirmed in functiion, not here. - Configuration.ConfirmBool(xmlElement, "FreeThreaded", - db.FreeThreaded, compulsory); - Configuration.ConfirmBool(xmlElement, "HasMultiple", - db.HasMultiple, compulsory); - if (db.Endianness == getMachineByteOrder()) - Assert.IsTrue(db.InHostOrder); - else - Assert.IsFalse(db.InHostOrder); - Configuration.ConfirmBool(xmlElement, "NoMMap", - db.NoMMap, compulsory); - Configuration.ConfirmBool(xmlElement, "NonDurableTxns", - db.NonDurableTxns, compulsory); - Configuration.ConfirmUint(xmlElement, "PageSize", - db.Pagesize, compulsory); - Configuration.ConfirmCachePriority(xmlElement, - "Priority", db.Priority, compulsory); - Configuration.ConfirmBool(xmlElement, "ReadOnly", - db.ReadOnly, compulsory); - Configuration.ConfirmBool(xmlElement, "ReadUncommitted", - db.ReadUncommitted, compulsory); - /* - * Database.Truncated is the value set in - * DatabaseConfig.Truncate. - */ - Configuration.ConfirmBool(xmlElement, "Truncate", - db.Truncated, compulsory); - Configuration.ConfirmBool(xmlElement, "UseMVCC", - db.UseMVCC, compulsory); - } - } - -} diff --git a/test/csharp/DotNetTest.csproj b/test/csharp/DotNetTest.csproj deleted file mode 100644 index 87fd98755..000000000 --- a/test/csharp/DotNetTest.csproj +++ /dev/null @@ -1,120 +0,0 @@ - - - Debug - AnyCPU - 8.0.50727 - 2.0 - {08D940C7-AAF5-413A-95E3-58A46880DC4D} - Library - Properties - DotNetTest - DotNetTest - - - - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - - - - - - - - - - - - - - - - - - - - - - Code - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - {4696FB1E-1E5F-40B9-BD8C-A54D3BDA00F6} - db_dotnet - - - - - - - - - - -IF $(ConfigurationName) == Debug SET LIBEXT=d -IF $(ConfigurationName) == Release SET LIBEXT - -copy /B "$(SolutionDir)Win32\$(ConfigurationName)\libdb53%25LIBEXT%25.dll" "$(TargetDir)" -copy /B "$(SolutionDir)Win32\$(ConfigurationName)\libdb_csharp53%25LIBEXT%25.dll" "$(TargetDir)" -copy "$(ProjectDir)AllTestData.xml" "$(TargetDir)" - - - - - - - - - diff --git a/test/csharp/ForeignKeyTest.cs b/test/csharp/ForeignKeyTest.cs deleted file mode 100644 index 306b144c4..000000000 --- a/test/csharp/ForeignKeyTest.cs +++ /dev/null @@ -1,263 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Threading; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest { - [TestFixture] - public class ForeignKeyTest : CSharpTestFixture - { - [TestFixtureSetUp] - public void RunBeforeTests() { - testFixtureName = "ForeignKeyTest"; - base.SetUpTestfixture(); - } - - [Test] - public void TestAbortBTree() { - testName = "TestAbortBTree"; - TestForeignKeyDelete(DatabaseType.BTREE, ForeignKeyDeleteAction.ABORT); - } - [Test] - public void TestAbortHash() { - testName = "TestAbortHash"; - TestForeignKeyDelete(DatabaseType.HASH, ForeignKeyDeleteAction.ABORT); - } - [Test] - public void TestAbortQueue() { - testName = "TestAbortQueue"; - TestForeignKeyDelete(DatabaseType.QUEUE, ForeignKeyDeleteAction.ABORT); - } - [Test] - public void TestAbortRecno() { - testName = "TestAbortRecno"; - TestForeignKeyDelete(DatabaseType.RECNO, ForeignKeyDeleteAction.ABORT); - } - - [Test] - public void TestCascadeBTree() { - testName = "TestCascadeBTree"; - TestForeignKeyDelete(DatabaseType.BTREE, ForeignKeyDeleteAction.CASCADE); - } - - [Test] - public void TestCascadeHash() { - testName = "TestCascadeHash"; - TestForeignKeyDelete(DatabaseType.HASH, ForeignKeyDeleteAction.CASCADE); - } - - [Test] - public void TestCascadeQueue() { - testName = "TestCascadeQueue"; - TestForeignKeyDelete(DatabaseType.QUEUE, ForeignKeyDeleteAction.CASCADE); - } - - [Test] - public void TestCascadeRecno() { - testName = "TestCascadeRecno"; - TestForeignKeyDelete(DatabaseType.RECNO, ForeignKeyDeleteAction.CASCADE); - } - - [Test] - public void TestNullifyBTree() { - testName = "TestNullifyBTree"; - TestForeignKeyDelete(DatabaseType.BTREE, ForeignKeyDeleteAction.NULLIFY); - } - - [Test] - public void TestNullifyHash() { - testName = "TestNullifyHash"; - TestForeignKeyDelete(DatabaseType.HASH, ForeignKeyDeleteAction.NULLIFY); - } - - [Test] - public void TestNullifyQueue() { - testName = "TestNullifyQueue"; - TestForeignKeyDelete(DatabaseType.QUEUE, ForeignKeyDeleteAction.NULLIFY); - } - - [Test] - public void TestNullifyRecno() { - testName = "TestNullifyRecno"; - TestForeignKeyDelete(DatabaseType.RECNO, ForeignKeyDeleteAction.NULLIFY); - } - - public void TestForeignKeyDelete(DatabaseType dbtype, ForeignKeyDeleteAction action) { - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - string fdbFileName = testHome + "/" + testName + "foreign.db"; - string sdbFileName = testHome + "/" + testName + "sec.db"; - - Database primaryDB, fdb; - SecondaryDatabase secDB; - - // Open primary database. - if (dbtype == DatabaseType.BTREE) { - BTreeDatabaseConfig btConfig = new BTreeDatabaseConfig(); - btConfig.Creation = CreatePolicy.ALWAYS; - primaryDB = BTreeDatabase.Open(dbFileName, btConfig); - fdb = BTreeDatabase.Open(fdbFileName, btConfig); - } else if (dbtype == DatabaseType.HASH) { - HashDatabaseConfig hConfig = new HashDatabaseConfig(); - hConfig.Creation = CreatePolicy.ALWAYS; - primaryDB = HashDatabase.Open(dbFileName, hConfig); - fdb = HashDatabase.Open(fdbFileName, hConfig); - } else if (dbtype == DatabaseType.QUEUE) { - QueueDatabaseConfig qConfig = new QueueDatabaseConfig(); - qConfig.Creation = CreatePolicy.ALWAYS; - qConfig.Length = 4; - primaryDB = QueueDatabase.Open(dbFileName, qConfig); - fdb = QueueDatabase.Open(fdbFileName, qConfig); - } else if (dbtype == DatabaseType.RECNO) { - RecnoDatabaseConfig rConfig = new RecnoDatabaseConfig(); - rConfig.Creation = CreatePolicy.ALWAYS; - primaryDB = RecnoDatabase.Open(dbFileName, rConfig); - fdb = RecnoDatabase.Open(fdbFileName, rConfig); - } else { - throw new ArgumentException("Invalid DatabaseType"); - } - - // Open secondary database. - if (dbtype == DatabaseType.BTREE) { - SecondaryBTreeDatabaseConfig secbtConfig = - new SecondaryBTreeDatabaseConfig(primaryDB, - new SecondaryKeyGenDelegate(SecondaryKeyGen)); - secbtConfig.Creation = CreatePolicy.ALWAYS; - secbtConfig.Duplicates = DuplicatesPolicy.SORTED; - if (action == ForeignKeyDeleteAction.NULLIFY) - secbtConfig.SetForeignKeyConstraint(fdb, action, new ForeignKeyNullifyDelegate(Nullify)); - else - secbtConfig.SetForeignKeyConstraint(fdb, action); - secDB = SecondaryBTreeDatabase.Open(sdbFileName, secbtConfig); - } else if (dbtype == DatabaseType.HASH) { - SecondaryHashDatabaseConfig sechConfig = - new SecondaryHashDatabaseConfig(primaryDB, - new SecondaryKeyGenDelegate(SecondaryKeyGen)); - sechConfig.Creation = CreatePolicy.ALWAYS; - sechConfig.Duplicates = DuplicatesPolicy.SORTED; - if (action == ForeignKeyDeleteAction.NULLIFY) - sechConfig.SetForeignKeyConstraint(fdb, action, new ForeignKeyNullifyDelegate(Nullify)); - else - sechConfig.SetForeignKeyConstraint(fdb, action); - secDB = SecondaryHashDatabase.Open(sdbFileName, sechConfig); - } else if (dbtype == DatabaseType.QUEUE) { - SecondaryQueueDatabaseConfig secqConfig = - new SecondaryQueueDatabaseConfig(primaryDB, - new SecondaryKeyGenDelegate(SecondaryKeyGen)); - secqConfig.Creation = CreatePolicy.ALWAYS; - secqConfig.Length = 4; - if (action == ForeignKeyDeleteAction.NULLIFY) - secqConfig.SetForeignKeyConstraint(fdb, action, new ForeignKeyNullifyDelegate(Nullify)); - else - secqConfig.SetForeignKeyConstraint(fdb, action); - secDB = SecondaryQueueDatabase.Open(sdbFileName, secqConfig); - } else if (dbtype == DatabaseType.RECNO) { - SecondaryRecnoDatabaseConfig secrConfig = - new SecondaryRecnoDatabaseConfig(primaryDB, - new SecondaryKeyGenDelegate(SecondaryKeyGen)); - secrConfig.Creation = CreatePolicy.ALWAYS; - if (action == ForeignKeyDeleteAction.NULLIFY) - secrConfig.SetForeignKeyConstraint(fdb, action, new ForeignKeyNullifyDelegate(Nullify)); - else - secrConfig.SetForeignKeyConstraint(fdb, action); - secDB = SecondaryRecnoDatabase.Open(sdbFileName, secrConfig); - } else { - throw new ArgumentException("Invalid DatabaseType"); - } - - /* Use integer keys for Queue/Recno support. */ - fdb.Put(new DatabaseEntry(BitConverter.GetBytes(100)), - new DatabaseEntry(BitConverter.GetBytes(1001))); - fdb.Put(new DatabaseEntry(BitConverter.GetBytes(200)), - new DatabaseEntry(BitConverter.GetBytes(2002))); - fdb.Put(new DatabaseEntry(BitConverter.GetBytes(300)), - new DatabaseEntry(BitConverter.GetBytes(3003))); - - primaryDB.Put(new DatabaseEntry(BitConverter.GetBytes(1)), - new DatabaseEntry(BitConverter.GetBytes(100))); - primaryDB.Put(new DatabaseEntry(BitConverter.GetBytes(2)), - new DatabaseEntry(BitConverter.GetBytes(200))); - if (dbtype == DatabaseType.BTREE || dbtype == DatabaseType.HASH) - primaryDB.Put(new DatabaseEntry(BitConverter.GetBytes(3)), - new DatabaseEntry(BitConverter.GetBytes(100))); - - try { - fdb.Delete(new DatabaseEntry(BitConverter.GetBytes(100))); - } catch (ForeignConflictException) { - Assert.AreEqual(action, ForeignKeyDeleteAction.ABORT); - } - if (action == ForeignKeyDeleteAction.ABORT) { - Assert.IsTrue(secDB.Exists(new DatabaseEntry(BitConverter.GetBytes(100)))); - Assert.IsTrue(primaryDB.Exists(new DatabaseEntry(BitConverter.GetBytes(1)))); - Assert.IsTrue(fdb.Exists(new DatabaseEntry(BitConverter.GetBytes(100)))); - } else if (action == ForeignKeyDeleteAction.CASCADE) { - try { - Assert.IsFalse(secDB.Exists(new DatabaseEntry(BitConverter.GetBytes(100)))); - } catch (KeyEmptyException) { - Assert.IsTrue(dbtype == DatabaseType.QUEUE || dbtype == DatabaseType.RECNO); - } - try { - Assert.IsFalse(primaryDB.Exists(new DatabaseEntry(BitConverter.GetBytes(1)))); - } catch (KeyEmptyException) { - Assert.IsTrue(dbtype == DatabaseType.QUEUE || dbtype == DatabaseType.RECNO); - } - try { - Assert.IsFalse(fdb.Exists(new DatabaseEntry(BitConverter.GetBytes(100)))); - } catch (KeyEmptyException) { - Assert.IsTrue(dbtype == DatabaseType.QUEUE || dbtype == DatabaseType.RECNO); - } - } else if (action == ForeignKeyDeleteAction.NULLIFY) { - try { - Assert.IsFalse(secDB.Exists(new DatabaseEntry(BitConverter.GetBytes(100)))); - } catch (KeyEmptyException) { - Assert.IsTrue(dbtype == DatabaseType.QUEUE || dbtype == DatabaseType.RECNO); - } - Assert.IsTrue(primaryDB.Exists(new DatabaseEntry(BitConverter.GetBytes(1)))); - try { - Assert.IsFalse(fdb.Exists(new DatabaseEntry(BitConverter.GetBytes(100)))); - } catch (KeyEmptyException) { - Assert.IsTrue(dbtype == DatabaseType.QUEUE || dbtype == DatabaseType.RECNO); - } - } - - // Close secondary database. - secDB.Close(); - - // Close primary database. - primaryDB.Close(); - - // Close foreign database - fdb.Close(); - } - - public DatabaseEntry SecondaryKeyGen( - DatabaseEntry key, DatabaseEntry data) { - DatabaseEntry dbtGen; - - int skey = BitConverter.ToInt32(data.Data, 0); - // don't index secondary key of 0 - if (skey == 0) - return null; - - dbtGen = new DatabaseEntry(data.Data); - return dbtGen; - } - - public DatabaseEntry Nullify(DatabaseEntry key, DatabaseEntry data, DatabaseEntry fkey) { - DatabaseEntry ret = new DatabaseEntry(BitConverter.GetBytes(0)); - return ret; - } - - } -} \ No newline at end of file diff --git a/test/csharp/HashCursorTest.cs b/test/csharp/HashCursorTest.cs deleted file mode 100644 index 202ba03b2..000000000 --- a/test/csharp/HashCursorTest.cs +++ /dev/null @@ -1,239 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Threading; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class HashCursorTest : CSharpTestFixture - { - - [TestFixtureSetUp] - public void SetUpTestFixture() - { - testFixtureName = "HashCursorTest"; - base.SetUpTestfixture(); - } - - [Test] - public void TestAddToLoc() - { - HashDatabase db; - HashCursor cursor; - KeyValuePair pair; - - testName = "TestAddToLoc"; - SetUpTest(true); - - /* - * Open a hash database and cursor and then - * add record("key", "data") into database. - */ - GetHashDBAndCursor(testHome, testName, out db, out cursor); - AddOneByCursor(cursor); - - /* - * Add the new record("key","data1") as the first - * of the data item of "key". - */ - pair = new KeyValuePair( - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key")), - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data1"))); - cursor.Add(pair, Cursor.InsertLocation.FIRST); - - /* - * Confirm that the new record is added as the first of - * the data item of "key". - */ - cursor.Move(new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key")), true); - Assert.AreEqual(ASCIIEncoding.ASCII.GetBytes("data1"), - cursor.Current.Value.Data); - - cursor.Close(); - db.Close(); - } - - [Test] - public void TestAddUnique() - { - HashDatabase db; - HashCursor cursor; - KeyValuePair pair; - - testName = "TestAddUnique"; - SetUpTest(true); - - // Open a database and cursor. - HashDatabaseConfig dbConfig = new HashDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - - /* - * To put no duplicate data, the database should be - * set to be sorted. - */ - dbConfig.Duplicates = DuplicatesPolicy.SORTED; - db = HashDatabase.Open( - testHome + "/" + testName + ".db", dbConfig); - cursor = db.Cursor(); - - // Add record("key", "data") into database. - AddOneByCursor(cursor); - - /* - * Fail to add duplicate record("key","data"). - */ - pair = new KeyValuePair( - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key")), - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data"))); - try - { - cursor.AddUnique(pair); - } - catch (KeyExistException) - { - } - finally - { - cursor.Close(); - db.Close(); - } - } - - [Test] - public void TestDuplicate() - { - HashDatabase db; - HashCursor cursor, dupCursor; - KeyValuePair pair; - - testName = "TestDuplicate"; - SetUpTest(true); - - GetHashDBAndCursor(testHome, testName, - out db, out cursor); - - /* - * Add a record("key", "data") by cursor and move - * the cursor to the current record. - */ - AddOneByCursor(cursor); - cursor.Refresh(); - - //Duplicate a new cursor to the same position. - dupCursor = cursor.Duplicate(true); - - // Overwrite the record. - dupCursor.Overwrite(new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("newdata"))); - - // Confirm that the original data doesn't exist. - pair = new KeyValuePair( - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key")), - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data"))); - Assert.IsFalse(dupCursor.Move(pair, true)); - - dupCursor.Close(); - cursor.Close(); - db.Close(); - } - - [Test] - public void TestInsertToLoc() - { - HashDatabase db; - HashDatabaseConfig dbConfig; - HashCursor cursor; - DatabaseEntry data; - KeyValuePair pair; - string dbFileName; - - testName = "TestInsertToLoc"; - SetUpTest(true); - dbFileName = testHome + "/" + testName + ".db"; - - // Open database and cursor. - dbConfig = new HashDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - - /* - * The database should be set to be unsorted to - * insert before/after a certain record. - */ - dbConfig.Duplicates = DuplicatesPolicy.UNSORTED; - db = HashDatabase.Open(dbFileName, dbConfig); - cursor = db.Cursor(); - - // Add record("key", "data") into database. - AddOneByCursor(cursor); - - /* - * Insert the new record("key","data1") after the - * record("key", "data"). - */ - data = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data1")); - cursor.Insert(data, Cursor.InsertLocation.AFTER); - - /* - * Move the cursor to the record("key", "data") and - * confirm that the next record is the one just inserted. - */ - pair = new KeyValuePair( - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key")), - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data"))); - Assert.IsTrue(cursor.Move(pair, true)); - Assert.IsTrue(cursor.MoveNext()); - Assert.AreEqual(ASCIIEncoding.ASCII.GetBytes("key"), - cursor.Current.Key.Data); - Assert.AreEqual(ASCIIEncoding.ASCII.GetBytes("data1"), - cursor.Current.Value.Data); - - try { - try { - cursor.Insert(data, - Cursor.InsertLocation.FIRST); - throw new TestException(); - } catch (ArgumentException) { } - try - { - cursor.Insert(data, - Cursor.InsertLocation.LAST); - throw new TestException(); - } catch (ArgumentException) { } - } finally { - cursor.Close(); - db.Close(); - } - } - - public void GetHashDBAndCursor(string home, string name, - out HashDatabase db, out HashCursor cursor) - { - string dbFileName = home + "/" + name + ".db"; - HashDatabaseConfig dbConfig = new HashDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - db = HashDatabase.Open(dbFileName, dbConfig); - cursor = db.Cursor(); - } - - public void AddOneByCursor(HashCursor cursor) - { - DatabaseEntry key, data; - KeyValuePair pair; - key = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key")); - data = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data")); - pair = new KeyValuePair(key, data); - cursor.Add(pair); - } - } -} diff --git a/test/csharp/HashDatabaseConfigTest.cs b/test/csharp/HashDatabaseConfigTest.cs deleted file mode 100644 index 6db104e56..000000000 --- a/test/csharp/HashDatabaseConfigTest.cs +++ /dev/null @@ -1,80 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class HashDatabaseConfigTest : DatabaseConfigTest - { - - [TestFixtureSetUp] - public override void SetUpTestFixture() - { - testFixtureName = "HashDatabaseConfigTest"; - base.SetUpTestfixture(); - } - - [Test] - override public void TestConfigWithoutEnv() - { - testName = "TestConfigWithoutEnv"; - SetUpTest(false); - XmlElement xmlElem = Configuration.TestSetUp( - testFixtureName, testName); - HashDatabaseConfig hashConfig = new HashDatabaseConfig(); - Config(xmlElem, ref hashConfig, true); - Confirm(xmlElem, hashConfig, true); - } - - public static void Confirm(XmlElement xmlElement, - HashDatabaseConfig hashDBConfig, bool compulsory) - { - DatabaseConfig dbConfig = hashDBConfig; - Confirm(xmlElement, dbConfig, compulsory); - - // Confirm Hash database specific configuration. - Configuration.ConfirmCreatePolicy(xmlElement, - "Creation", hashDBConfig.Creation, compulsory); - Configuration.ConfirmDuplicatesPolicy(xmlElement, - "Duplicates", hashDBConfig.Duplicates, compulsory); - Configuration.ConfirmUint(xmlElement, "FillFactor", - hashDBConfig.FillFactor, compulsory); - Configuration.ConfirmUint(xmlElement, "NumElements", - hashDBConfig.TableSize, compulsory); - } - - public static void Config(XmlElement xmlElement, - ref HashDatabaseConfig hashDBConfig, bool compulsory) - { - uint fillFactor = new uint(); - uint numElements = new uint(); - DatabaseConfig dbConfig = hashDBConfig; - Config(xmlElement, ref dbConfig, compulsory); - - // Configure specific fields/properties of Hash db - Configuration.ConfigCreatePolicy(xmlElement, - "Creation", ref hashDBConfig.Creation, - compulsory); - Configuration.ConfigDuplicatesPolicy(xmlElement, - "Duplicates", ref hashDBConfig.Duplicates, - compulsory); - if (Configuration.ConfigUint(xmlElement, "FillFactor", - ref fillFactor, compulsory)) - hashDBConfig.FillFactor = fillFactor; - if (Configuration.ConfigUint(xmlElement, "NumElements", - ref numElements, compulsory)) - hashDBConfig.TableSize = numElements; - } - } -} diff --git a/test/csharp/HashDatabaseTest.cs b/test/csharp/HashDatabaseTest.cs deleted file mode 100644 index f830c97f4..000000000 --- a/test/csharp/HashDatabaseTest.cs +++ /dev/null @@ -1,509 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class HashDatabaseTest : CSharpTestFixture - { - - [TestFixtureSetUp] - public void SetUpTestFixture() - { - testFixtureName = "HashDatabaseTest"; - base.SetUpTestfixture(); - } - - [Test] - public void TestCompactWithoutTxn() { - int i, nRecs; - nRecs = 10000; - testName = "TestCompactWithoutTxn"; - SetUpTest(true); - string hashDBFileName = testHome + "/" + testName + ".db"; - - HashDatabaseConfig hashDBConfig = new HashDatabaseConfig(); - hashDBConfig.Creation = CreatePolicy.ALWAYS; - // The minimum page size - hashDBConfig.PageSize = 512; - hashDBConfig.HashComparison = - new EntryComparisonDelegate(dbIntCompare); - using (HashDatabase hashDB = HashDatabase.Open( - hashDBFileName, hashDBConfig)) { - DatabaseEntry key; - DatabaseEntry data; - - // Fill the database with entries from 0 to 9999 - for (i = 0; i < nRecs; i++) { - key = new DatabaseEntry(BitConverter.GetBytes(i)); - data = new DatabaseEntry(BitConverter.GetBytes(i)); - hashDB.Put(key, data); - } - - /* - * Delete entries below 500, between 3000 and - * 5000 and above 7000 - */ - for (i = 0; i < nRecs; i++) - if (i < 500 || i > 7000 || (i < 5000 && i > 3000)) { - key = new DatabaseEntry(BitConverter.GetBytes(i)); - hashDB.Delete(key); - } - - hashDB.Sync(); - long fileSize = new FileInfo(hashDBFileName).Length; - - // Compact database - CompactConfig cCfg = new CompactConfig(); - cCfg.FillPercentage = 30; - cCfg.Pages = 10; - cCfg.Timeout = 1000; - cCfg.TruncatePages = true; - cCfg.start = new DatabaseEntry(BitConverter.GetBytes(1)); - cCfg.stop = new DatabaseEntry(BitConverter.GetBytes(7000)); - CompactData compactData = hashDB.Compact(cCfg); - Assert.IsFalse((compactData.Deadlocks == 0) && - (compactData.Levels == 0) && - (compactData.PagesExamined == 0) && - (compactData.PagesFreed == 0) && - (compactData.PagesTruncated == 0)); - - hashDB.Sync(); - long compactedFileSize = new FileInfo(hashDBFileName).Length; - Assert.Less(compactedFileSize, fileSize); - } - } - - [Test] - public void TestHashComparison() - { - testName = "TestHashComparison"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - - HashDatabaseConfig dbConfig = new HashDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.HashComparison = new EntryComparisonDelegate(EntryComparison); - HashDatabase db = HashDatabase.Open(dbFileName,dbConfig); - int ret; - - /* - * Comparison gets the value that lowest byte of the - * former dbt minus that of the latter one. - */ - ret = db.Compare(new DatabaseEntry(BitConverter.GetBytes(2)), - new DatabaseEntry(BitConverter.GetBytes(2))); - Assert.AreEqual(0, ret); - - ret = db.Compare(new DatabaseEntry(BitConverter.GetBytes(256)), - new DatabaseEntry(BitConverter.GetBytes(1))); - Assert.Greater(0, ret); - - db.Close(); - } - - public int EntryComparison(DatabaseEntry dbt1, - DatabaseEntry dbt2) - { - return dbt1.Data[0] - dbt2.Data[0]; - } - - [Test] - public void TestHashFunction() - { - testName = "TestHashFunction"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - - HashDatabaseConfig dbConfig = new HashDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.HashFunction = new HashFunctionDelegate(HashFunction); - HashDatabase db = HashDatabase.Open(dbFileName, dbConfig); - - // Hash function will change the lowest byte to 0; - uint data = db.HashFunction(BitConverter.GetBytes(1)); - Assert.AreEqual(0, data); - db.Close(); - } - - public uint HashFunction(byte[] data) - { - data[0] = 0; - return BitConverter.ToUInt32(data, 0); - } - - [Test] - public void TestOpenExistingHashDB() - { - testName = "TestOpenExistingHashDB"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - - HashDatabaseConfig hashConfig = - new HashDatabaseConfig(); - hashConfig.Creation = CreatePolicy.ALWAYS; - HashDatabase hashDB = HashDatabase.Open(dbFileName, hashConfig); - hashDB.Close(); - - DatabaseConfig dbConfig = new DatabaseConfig(); - Database db = Database.Open(dbFileName, dbConfig); - Assert.AreEqual(db.Type, DatabaseType.HASH); - db.Close(); - } - - [Test] - public void TestOpenNewHashDB() - { - testName = "TestOpenNewHashDB"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - - XmlElement xmlElem = Configuration.TestSetUp(testFixtureName, testName); - HashDatabaseConfig hashConfig = new HashDatabaseConfig(); - HashDatabaseConfigTest.Config(xmlElem, ref hashConfig, true); - HashDatabase hashDB = HashDatabase.Open(dbFileName, hashConfig); - Confirm(xmlElem, hashDB, true); - hashDB.Close(); - } - - [Test, ExpectedException(typeof(ExpectedTestException))] - public void TestPutNoDuplicateWithUnsortedDuplicate() - { - testName = "TestPutNoDuplicateWithUnsortedDuplicate"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - - HashDatabaseConfig hashConfig = new HashDatabaseConfig(); - hashConfig.Creation = CreatePolicy.ALWAYS; - hashConfig.Duplicates = DuplicatesPolicy.UNSORTED; - hashConfig.ErrorPrefix = testName; - - HashDatabase hashDB = HashDatabase.Open(dbFileName, hashConfig); - DatabaseEntry key, data; - key = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("1")); - data = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("1")); - - try - { - hashDB.PutNoDuplicate(key, data); - } - catch (DatabaseException) - { - throw new ExpectedTestException(); - } - finally - { - hashDB.Close(); - } - } - - [Test, ExpectedException(typeof(ExpectedTestException))] - public void TestKeyExistException() - { - testName = "TestKeyExistException"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - - HashDatabaseConfig hashConfig = new HashDatabaseConfig(); - hashConfig.Creation = CreatePolicy.ALWAYS; - hashConfig.Duplicates = DuplicatesPolicy.SORTED; - HashDatabase hashDB = HashDatabase.Open(dbFileName, hashConfig); - - // Put the same record into db twice. - DatabaseEntry key, data; - key = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("1")); - data = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("1")); - try - { - hashDB.PutNoDuplicate(key, data); - hashDB.PutNoDuplicate(key, data); - } - catch (KeyExistException) - { - throw new ExpectedTestException(); - } - finally - { - hashDB.Close(); - } - } - - [Test] - public void TestPutNoDuplicate() - { - testName = "TestPutNoDuplicate"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - - HashDatabaseConfig hashConfig = - new HashDatabaseConfig(); - hashConfig.Creation = CreatePolicy.ALWAYS; - hashConfig.Duplicates = DuplicatesPolicy.SORTED; - hashConfig.TableSize = 20; - HashDatabase hashDB = HashDatabase.Open(dbFileName, hashConfig); - - DatabaseEntry key, data; - for (int i = 1; i <= 10; i++) - { - key = new DatabaseEntry(BitConverter.GetBytes(i)); - data = new DatabaseEntry(BitConverter.GetBytes(i)); - hashDB.PutNoDuplicate(key, data); - } - - Assert.IsTrue(hashDB.Exists( - new DatabaseEntry(BitConverter.GetBytes((int)5)))); - - hashDB.Close(); - } - - [Test, ExpectedException(typeof(ExpectedTestException))] - public void TestPutNoDuplicateWithTxn() - { - testName = "TestPutNoDuplicateWithTxn"; - SetUpTest(true); - - // Open an environment. - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseLogging = true; - envConfig.UseMPool = true; - envConfig.UseTxns = true; - DatabaseEnvironment env = DatabaseEnvironment.Open( - testHome, envConfig); - - // Open a hash database within a transaction. - Transaction txn = env.BeginTransaction(); - HashDatabaseConfig dbConfig = new HashDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Duplicates = DuplicatesPolicy.SORTED; - dbConfig.Env = env; - HashDatabase db = HashDatabase.Open(testName + ".db", dbConfig, txn); - - DatabaseEntry dbt = new DatabaseEntry(BitConverter.GetBytes((int)100)); - db.PutNoDuplicate(dbt, dbt, txn); - try - { - db.PutNoDuplicate(dbt, dbt, txn); - } - catch (KeyExistException) - { - throw new ExpectedTestException(); - } - finally - { - // Close all. - db.Close(); - txn.Commit(); - env.Close(); - } - } - - [Test] - public void TestStats() - { - testName = "TestStats"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - - HashDatabaseConfig dbConfig = new HashDatabaseConfig(); - ConfigCase1(dbConfig); - HashDatabase db = HashDatabase.Open(dbFileName, dbConfig); - - HashStats stats = db.Stats(); - HashStats fastStats = db.FastStats(); - ConfirmStatsPart1Case1(stats); - ConfirmStatsPart1Case1(fastStats); - - // Put 100 records into the database. - PutRecordCase1(db, null); - - stats = db.Stats(); - ConfirmStatsPart2Case1(stats); - - // Delete some data to get some free pages. - byte[] bigArray = new byte[262144]; - db.Delete(new DatabaseEntry(bigArray)); - stats = db.Stats(); - ConfirmStatsPart3Case1(stats); - - db.Close(); - } - - [Test] - public void TestStatsInTxn() - { - testName = "TestStatsInTxn"; - SetUpTest(true); - - StatsInTxn(testHome, testName, false); - } - - [Test] - public void TestStatsWithIsolation() - { - testName = "TestStatsWithIsolation"; - SetUpTest(true); - - StatsInTxn(testHome, testName, true); - } - - private int dbIntCompare(DatabaseEntry dbt1, DatabaseEntry dbt2) { - int a, b; - a = BitConverter.ToInt32(dbt1.Data, 0); - b = BitConverter.ToInt32(dbt2.Data, 0); - return a - b; - } - - public void StatsInTxn(string home, string name, bool ifIsolation) - { - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - EnvConfigCase1(envConfig); - DatabaseEnvironment env = DatabaseEnvironment.Open(home, envConfig); - - Transaction openTxn = env.BeginTransaction(); - HashDatabaseConfig dbConfig = new HashDatabaseConfig(); - ConfigCase1(dbConfig); - dbConfig.Env = env; - HashDatabase db = HashDatabase.Open(name + ".db", dbConfig, openTxn); - openTxn.Commit(); - - Transaction statsTxn = env.BeginTransaction(); - HashStats stats; - HashStats fastStats; - if (ifIsolation == false) - { - stats = db.Stats(statsTxn); - fastStats = db.Stats(statsTxn); - } - else - { - stats = db.Stats(statsTxn, Isolation.DEGREE_ONE); - fastStats = db.Stats(statsTxn, Isolation.DEGREE_ONE); - } - - ConfirmStatsPart1Case1(stats); - - // Put 100 records into the database. - PutRecordCase1(db, statsTxn); - - if (ifIsolation == false) - stats = db.Stats(statsTxn); - else - stats = db.Stats(statsTxn, Isolation.DEGREE_TWO); - ConfirmStatsPart2Case1(stats); - - // Delete some data to get some free pages. - byte[] bigArray = new byte[262144]; - db.Delete(new DatabaseEntry(bigArray), statsTxn); - if (ifIsolation == false) - stats = db.Stats(statsTxn); - else - stats = db.Stats(statsTxn, Isolation.DEGREE_THREE); - ConfirmStatsPart3Case1(stats); - - statsTxn.Commit(); - db.Close(); - env.Close(); - } - - public void EnvConfigCase1(DatabaseEnvironmentConfig cfg) - { - cfg.Create = true; - cfg.UseTxns = true; - cfg.UseMPool = true; - cfg.UseLogging = true; - } - - public void ConfigCase1(HashDatabaseConfig dbConfig) - { - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Duplicates = DuplicatesPolicy.UNSORTED; - dbConfig.FillFactor = 10; - dbConfig.TableSize = 20; - dbConfig.PageSize = 4096; - } - - public void PutRecordCase1(HashDatabase db, Transaction txn) - { - byte[] bigArray = new byte[262144]; - for (int i = 0; i < 50; i++) - { - if (txn == null) - db.Put(new DatabaseEntry(BitConverter.GetBytes(i)), - new DatabaseEntry(BitConverter.GetBytes(i))); - else - db.Put(new DatabaseEntry(BitConverter.GetBytes(i)), - new DatabaseEntry(BitConverter.GetBytes(i)), txn); - } - for (int i = 50; i < 100; i++) - { - if (txn == null) - db.Put(new DatabaseEntry(bigArray), - new DatabaseEntry(bigArray)); - else - db.Put(new DatabaseEntry(bigArray), - new DatabaseEntry(bigArray), txn); - } - } - - public void ConfirmStatsPart1Case1(HashStats stats) - { - Assert.AreEqual(10, stats.FillFactor); - Assert.AreEqual(4096, stats.PageSize); - Assert.AreNotEqual(0, stats.Version); - } - - public void ConfirmStatsPart2Case1(HashStats stats) - { - Assert.AreNotEqual(0, stats.BigPages); - Assert.AreNotEqual(0, stats.BigPagesFreeBytes); - Assert.AreNotEqual(0, stats.BucketPagesFreeBytes); - Assert.AreNotEqual(0, stats.DuplicatePages); - Assert.AreNotEqual(0, stats.DuplicatePagesFreeBytes); - Assert.AreNotEqual(0, stats.MagicNumber); - Assert.AreNotEqual(0, stats.MetadataFlags); - Assert.AreEqual(100, stats.nData); - Assert.AreNotEqual(0, stats.nHashBuckets); - Assert.AreEqual(51, stats.nKeys); - Assert.AreNotEqual(0, stats.nPages); - Assert.AreEqual(0, stats.OverflowPages); - Assert.AreEqual(0, stats.OverflowPagesFreeBytes); - } - - public void ConfirmStatsPart3Case1(HashStats stats) - { - Assert.AreNotEqual(0, stats.FreePages); - } - - public static void Confirm(XmlElement xmlElem, - HashDatabase hashDB, bool compulsory) - { - DatabaseTest.Confirm(xmlElem, hashDB, compulsory); - Configuration.ConfirmCreatePolicy(xmlElem, - "Creation", hashDB.Creation, compulsory); - Configuration.ConfirmDuplicatesPolicy(xmlElem, - "Duplicates", hashDB.Duplicates, compulsory); - Configuration.ConfirmUint(xmlElem, - "FillFactor", hashDB.FillFactor, compulsory); - Configuration.ConfirmUint(xmlElem, - "NumElements", hashDB.TableSize, - compulsory); - Assert.AreEqual(DatabaseType.HASH, hashDB.Type); - string type = hashDB.Type.ToString(); - Assert.IsNotNull(type); - } - } -} diff --git a/test/csharp/HeapDatabaseConfigTest.cs b/test/csharp/HeapDatabaseConfigTest.cs deleted file mode 100644 index fb06f0163..000000000 --- a/test/csharp/HeapDatabaseConfigTest.cs +++ /dev/null @@ -1,57 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2011, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest { - [TestFixture] - public class HeapDatabaseConfigTest : DatabaseConfigTest { - - [TestFixtureSetUp] - public override void SetUpTestFixture() { - testFixtureName = "HeapDatabaseConfigTest"; - base.SetUpTestfixture(); - } - - [Test] - new public void TestConfigWithoutEnv() { - testName = "TestConfigWithoutEnv"; - SetUpTest(false); - XmlElement xmlElem = Configuration.TestSetUp( - testFixtureName, testName); - HeapDatabaseConfig heapDBConfig = - new HeapDatabaseConfig(); - Config(xmlElem, ref heapDBConfig, true); - Confirm(xmlElem, heapDBConfig, true); - } - - public static void Confirm(XmlElement xmlElement, - HeapDatabaseConfig heapDBConfig, bool compulsory) { - DatabaseConfig dbConfig = heapDBConfig; - Confirm(xmlElement, dbConfig, compulsory); - - // Confirm Heap database specific configuration - Configuration.ConfirmCreatePolicy(xmlElement, "Creation", - heapDBConfig.Creation, compulsory); - } - - public static void Config(XmlElement xmlElement, - ref HeapDatabaseConfig heapDBConfig, bool compulsory) { - DatabaseConfig dbConfig = heapDBConfig; - Config(xmlElement, ref dbConfig, compulsory); - - // Configure specific fields/properties of Heap database - Configuration.ConfigCreatePolicy(xmlElement, "Creation", - ref heapDBConfig.Creation, compulsory); - } - } -} diff --git a/test/csharp/HeapDatabaseTest.cs b/test/csharp/HeapDatabaseTest.cs deleted file mode 100644 index b8bfbcc55..000000000 --- a/test/csharp/HeapDatabaseTest.cs +++ /dev/null @@ -1,461 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2011, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest { - [TestFixture] - public class HeapDatabaseTest : DatabaseTest { - - [TestFixtureSetUp] - public void SetUpTestFixture() { - testFixtureName = "HeapDatabaseTest"; - base.SetUpTestfixture(); - } - - [Test] - public void TestAppendWithoutTxn() { - testName = "TestAppendWithoutTxn"; - SetUpTest(true); - string heapDBFileName = testHome + "/" + testName + ".db"; - - - HeapDatabaseConfig heapConfig = new HeapDatabaseConfig(); - heapConfig.Creation = CreatePolicy.ALWAYS; - HeapDatabase heapDB = HeapDatabase.Open( - heapDBFileName, heapConfig); - - byte[] byteArr = new byte[4]; - byteArr = BitConverter.GetBytes((int)1); - DatabaseEntry data = new DatabaseEntry(byteArr); - HeapRecordId rid = heapDB.Append(data); - - // Confirm that the record lives on a page larger than 0. - Assert.AreNotEqual(0, rid.PageNumber); - - // Confirm that the record exists in the database. - DatabaseEntry key = new DatabaseEntry(rid.toArray()); - Assert.IsTrue(heapDB.Exists(key)); - heapDB.Close(); - } - - [Test] - public void TestAppendWithTxn() { - testName = "TestAppendWithTxn"; - SetUpTest(true); - string heapDBFileName = testHome + "/" + testName + ".db"; - string heapDBName = - Path.GetFileNameWithoutExtension(heapDBFileName); - - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseTxns = true; - envConfig.UseMPool = true; - - DatabaseEnvironment env = DatabaseEnvironment.Open( - testHome, envConfig); - Transaction txn = env.BeginTransaction(); - - HeapDatabaseConfig heapConfig = new HeapDatabaseConfig(); - heapConfig.Creation = CreatePolicy.ALWAYS; - heapConfig.Env = env; - - /* If environmnet home is set, the file name in - * Open() is the relative path. - */ - HeapDatabase heapDB = HeapDatabase.Open( - heapDBName, heapConfig, txn); - DatabaseEntry data; - int i = 1000; - try { - while (i > 0) { - data = new DatabaseEntry( - BitConverter.GetBytes(i)); - heapDB.Append(data, txn); - i--; - } - txn.Commit(); - } catch { - txn.Abort(); - } finally { - heapDB.Close(); - env.Close(); - } - - } - - [Test] - public void TestCursor() { - testName = "TestCursor"; - SetUpTest(true); - - GetCursur(testHome + "/" + testName + ".db", false); - } - - [Test] - public void TestCursorWithConfig() { - testName = "TestCursorWithConfig"; - SetUpTest(true); - - GetCursur(testHome + "/" + testName + ".db", true); - } - - public void GetCursur(string dbFileName, bool ifConfig) { - HeapDatabaseConfig dbConfig = new HeapDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - HeapDatabase db = HeapDatabase.Open(dbFileName, dbConfig); - HeapRecordId rid = db.Append( - new DatabaseEntry(BitConverter.GetBytes((int)1))); - Cursor cursor; - CursorConfig cursorConfig = new CursorConfig(); - cursorConfig.Priority = CachePriority.HIGH; - if (ifConfig == false) - cursor = db.Cursor(); - else - cursor = db.Cursor(cursorConfig); - cursor.Add(new KeyValuePair( - new DatabaseEntry(rid.toArray()), - new DatabaseEntry(BitConverter.GetBytes((int)2)))); - Cursor dupCursor = cursor.Duplicate(false); - Assert.IsNull(dupCursor.Current.Key); - if (ifConfig) - Assert.AreEqual(CachePriority.HIGH, dupCursor.Priority); - dupCursor.Close(); - cursor.Close(); - db.Close(); - } - - [Test] - public void TestExists() { - testName = "TestExists"; - SetUpTest(true); - - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseLocking = true; - envConfig.UseLogging = true; - envConfig.UseMPool = true; - envConfig.UseTxns = true; - DatabaseEnvironment env = DatabaseEnvironment.Open( - testHome, envConfig); - - HeapDatabase db; - try { - Transaction openTxn = env.BeginTransaction(); - try { - HeapDatabaseConfig heapConfig = - new HeapDatabaseConfig(); - heapConfig.Creation = CreatePolicy.IF_NEEDED; - heapConfig.Env = env; - db = HeapDatabase.Open(testName + ".db", - heapConfig, openTxn); - openTxn.Commit(); - } catch (DatabaseException e) { - openTxn.Abort(); - throw e; - } - - Transaction cursorTxn = env.BeginTransaction(); - Cursor cursor; - HeapRecordId rid; - try { - /* - * Put a record into heap database with - * cursor and abort the operation. - */ - rid = db.Append(new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("foo")), cursorTxn); - cursor = db.Cursor(cursorTxn); - KeyValuePair pair; - pair = new KeyValuePair( - new DatabaseEntry(rid.toArray()), - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("bar"))); - cursor.Add(pair); - cursor.Close(); - Assert.IsTrue(db.Exists( - new DatabaseEntry(rid.toArray()), cursorTxn)); - cursorTxn.Abort(); - } catch (DatabaseException e) { - cursorTxn.Abort(); - db.Close(); - throw e; - } - - Transaction delTxn = env.BeginTransaction(); - try { - /* - * The put operation is aborted in the heap - * database so querying if the record still exists - * throws KeyEmptyException. - */ - Assert.IsFalse(db.Exists( - new DatabaseEntry(rid.toArray()), delTxn)); - delTxn.Commit(); - } catch (DatabaseException e) { - delTxn.Abort(); - throw e; - } finally { - db.Close(); - } - } finally { - env.Close(); - } - } - - [Test] - public void TestOpenExistingHeapDB() { - testName = "TestOpenExistingHeapDB"; - SetUpTest(true); - string heapDBFileName = testHome + "/" + testName + ".db"; - - HeapDatabaseConfig heapConfig = new HeapDatabaseConfig(); - heapConfig.Creation = CreatePolicy.ALWAYS; - HeapDatabase heapDB = HeapDatabase.Open( - heapDBFileName, heapConfig); - heapDB.Close(); - - DatabaseConfig dbConfig = new DatabaseConfig(); - Database db = Database.Open(heapDBFileName, dbConfig); - Assert.AreEqual(db.Type, DatabaseType.HEAP); - db.Close(); - } - - [Test] - public void TestOpenNewHeapDB() { - testName = "TestOpenNewHeapDB"; - SetUpTest(true); - string heapDBFileName = testHome + "/" + testName + ".db"; - - // Configure all fields/properties in heap database. - XmlElement xmlElem = Configuration.TestSetUp( - testFixtureName, testName); - HeapDatabaseConfig heapConfig = new HeapDatabaseConfig(); - HeapDatabaseConfigTest.Config(xmlElem, ref heapConfig, true); - heapConfig.Feedback = new DatabaseFeedbackDelegate(DbFeedback); - - // Open the heap database with above configuration. - HeapDatabase heapDB = HeapDatabase.Open( - heapDBFileName, heapConfig); - - // Check the fields/properties in opened heap database. - Confirm(xmlElem, heapDB, true); - - heapDB.Close(); - } - - private void DbFeedback(DatabaseFeedbackEvent opcode, int percent) { - if (opcode == DatabaseFeedbackEvent.UPGRADE) - Console.WriteLine("Update for %d%", percent); - - if (opcode == DatabaseFeedbackEvent.VERIFY) - Console.WriteLine("Vertify for %d", percent); - } - - [Test] - public void TestPutHeap() { - KeyValuePair pair; - - testName = "TestPutHeap"; - SetUpTest(true); - string heapDBFileName = testHome + "/" + - testName + ".db"; - - HeapDatabaseConfig heapConfig = - new HeapDatabaseConfig(); - heapConfig.Creation = CreatePolicy.ALWAYS; - using (HeapDatabase heapDB = HeapDatabase.Open( - heapDBFileName, heapConfig)) { - DatabaseEntry data = new DatabaseEntry( - BitConverter.GetBytes((int)1)); - HeapRecordId rid = heapDB.Append(data); - DatabaseEntry key = new DatabaseEntry(rid.toArray()); - data = new DatabaseEntry(BitConverter.GetBytes((int)2)); - heapDB.Put(key, data); - pair = heapDB.GetBoth(key, data); - } - } - - [Test] - public void TestStats() { - testName = "TestStats"; - SetUpTest(true); - string dbFileName = testHome + "/" + - testName + ".db"; - - HeapDatabaseConfig dbConfig = - new HeapDatabaseConfig(); - ConfigCase1(dbConfig); - HeapDatabase db = HeapDatabase.Open(dbFileName, dbConfig); - - HeapStats stats = db.Stats(); - ConfirmStatsPart1Case1(stats); - db.PrintFastStats(true); - - // Put 500 records into the database. - PutRecordCase1(db, null); - - stats = db.Stats(); - ConfirmStatsPart2Case1(stats); - db.PrintFastStats(); - - db.Close(); - } - - [Test] - public void TestStatsInTxn() { - testName = "TestStatsInTxn"; - SetUpTest(true); - - StatsInTxn(testHome, testName, false); - } - - [Test] - public void TestStatsWithIsolation() { - testName = "TestStatsWithIsolation"; - SetUpTest(true); - - StatsInTxn(testHome, testName, true); - } - - [Test] - public void TestHeapRegionsize() { - /* - * Check that with a very small region size, the number of regions - * grows quickly. - */ - testName = "TestHeapRegionsize"; - SetUpTest(true); - - Random r = new Random(); - byte[] buf = new byte[1024]; - r.NextBytes(buf); - - HeapDatabaseConfig cfg = new HeapDatabaseConfig(); - cfg.Creation = CreatePolicy.ALWAYS; - cfg.PageSize = 512; - cfg.RegionSize = 4; - - HeapDatabase db = HeapDatabase.Open( - testHome + "/" + testName + ".db", cfg); - DatabaseEntry dbt = new DatabaseEntry(buf); - for (int i = 0; i < 10; i++) - db.Append(dbt); - - Assert.AreEqual(db.RegionSize, 4); - - HeapStats stats = db.Stats(); - db.Close(); - - Assert.AreEqual(stats.RegionSize, 4); - Assert.Greater(stats.nRegions, 1); - } - - public void StatsInTxn(string home, string name, bool ifIsolation) { - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - EnvConfigCase1(envConfig); - DatabaseEnvironment env = DatabaseEnvironment.Open( - home, envConfig); - - Transaction openTxn = env.BeginTransaction(); - HeapDatabaseConfig dbConfig = - new HeapDatabaseConfig(); - ConfigCase1(dbConfig); - dbConfig.Env = env; - HeapDatabase db = HeapDatabase.Open(name + ".db", - dbConfig, openTxn); - openTxn.Commit(); - - Transaction statsTxn = env.BeginTransaction(); - HeapStats stats; - if (ifIsolation == false) - stats = db.Stats(statsTxn); - else - stats = db.Stats(statsTxn, Isolation.DEGREE_ONE); - - ConfirmStatsPart1Case1(stats); - db.PrintStats(true); - - // Put 500 records into the database. - PutRecordCase1(db, statsTxn); - - if (ifIsolation == false) - stats = db.Stats(statsTxn); - else - stats = db.Stats(statsTxn, Isolation.DEGREE_TWO); - ConfirmStatsPart2Case1(stats); - db.PrintStats(); - - statsTxn.Commit(); - db.Close(); - env.Close(); - } - - public void EnvConfigCase1(DatabaseEnvironmentConfig cfg) { - cfg.Create = true; - cfg.UseTxns = true; - cfg.UseMPool = true; - cfg.UseLogging = true; - } - - public void ConfigCase1(HeapDatabaseConfig dbConfig) { - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.PageSize = 4096; - } - - public void PutRecordCase1(HeapDatabase db, Transaction txn) { - byte[] bigArray = new byte[4000]; - HeapRecordId rid = new HeapRecordId(2, 0); - for (int i = 1; i <= 100; i++) { - if (txn == null) - rid = db.Append( - new DatabaseEntry(BitConverter.GetBytes(i))); - else - rid = db.Append( - new DatabaseEntry(BitConverter.GetBytes(i)), txn); - } - DatabaseEntry key = new DatabaseEntry(rid.toArray()); - for (int i = 100; i <= 500; i++) { - if (txn == null) - db.Put(key, new DatabaseEntry(bigArray)); - else - db.Put(key, new DatabaseEntry(bigArray), txn); - } - } - - public void ConfirmStatsPart1Case1(HeapStats stats) { - Assert.AreNotEqual(0, stats.MagicNumber); - Assert.AreEqual(4096, stats.PageSize); - Assert.AreNotEqual(0, stats.RegionSize); - Assert.AreNotEqual(0, stats.Version); - } - - public void ConfirmStatsPart2Case1(HeapStats stats) { - Assert.AreNotEqual(0, stats.nPages); - Assert.AreEqual(0, stats.MetadataFlags); - Assert.AreEqual(100, stats.nRecords); - Assert.AreNotEqual(0, stats.nRegions); - } - - public static void Confirm(XmlElement xmlElem, - HeapDatabase heapDB, bool compulsory) { - DatabaseTest.Confirm(xmlElem, heapDB, compulsory); - - // Confirm heap database specific field/property - Assert.AreEqual(DatabaseType.HEAP, heapDB.Type); - string type = heapDB.Type.ToString(); - Assert.IsNotNull(type); - } - } -} diff --git a/test/csharp/JoinCursorTest.cs b/test/csharp/JoinCursorTest.cs deleted file mode 100644 index 1167a5ef3..000000000 --- a/test/csharp/JoinCursorTest.cs +++ /dev/null @@ -1,199 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Threading; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class JoinCursorTest : CSharpTestFixture - { - - [TestFixtureSetUp] - public void SetUpTestFixture() - { - testFixtureName = "JoinCursorTest"; - base.SetUpTestfixture(); - } - - [Test] - public void TestJoin() - { - testName = "TestJoin"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - string secFileName1 = testHome + "/" + "sec_" + testName + "1.db"; - string secFileName2 = testHome + "/" + "sec_" + testName + "2.db"; - - // Open a primary database. - BTreeDatabaseConfig dbCfg = new BTreeDatabaseConfig(); - dbCfg.Creation = CreatePolicy.IF_NEEDED; - BTreeDatabase db = BTreeDatabase.Open(dbFileName, dbCfg); - - for (int i = 0; i < 10; i++) - db.Put(new DatabaseEntry(BitConverter.GetBytes(i)), - new DatabaseEntry(BitConverter.GetBytes(i))); - - /* - * Open two databases, their secondary databases and - * secondary cursors. - */ - SecondaryBTreeDatabase secDB1, secDB2; - SecondaryCursor[] cursors = new SecondaryCursor[2]; - GetSecCursor(db, secFileName1, - new SecondaryKeyGenDelegate(KeyGenOnBigByte), - out secDB1, out cursors[0], false, null); - GetSecCursor(db, secFileName2, - new SecondaryKeyGenDelegate(KeyGenOnLittleByte), - out secDB2, out cursors[1], true, null); - - // Get join cursor. - JoinCursor joinCursor = db.Join(cursors, true); - Assert.IsNotNull(joinCursor.GetEnumerator().Current); - - // Close all. - joinCursor.Dispose(); - cursors[0].Close(); - cursors[1].Close(); - secDB1.Close(); - secDB2.Close(); - db.Close(); - } - - [Test] - public void TestMoveJoinCursor() - { - testName = "TestMoveJoinCursor"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - string secFileName1 = testHome + "/" + "sec_" + testName + "1.db"; - string secFileName2 = testHome + "/" + "sec_" + testName + "2.db"; - - // Open a primary database. - BTreeDatabaseConfig dbCfg = new BTreeDatabaseConfig(); - dbCfg.Creation = CreatePolicy.IF_NEEDED; - BTreeDatabase db = BTreeDatabase.Open(dbFileName, dbCfg); - - /* - * Open a secondary database on high byte of - * its data and another secondary database on - * little byte of its data. - */ - SecondaryBTreeDatabase secDB1, secDB2; - SecondaryCursor[] cursors = new SecondaryCursor[2]; - byte[] byteValue = new byte[1]; - - byteValue[0] = 0; - GetSecCursor(db, secFileName1, - new SecondaryKeyGenDelegate(KeyGenOnBigByte), - out secDB1, out cursors[0], false, - new DatabaseEntry(byteValue)); - - byteValue[0] = 1; - GetSecCursor(db, secFileName2, - new SecondaryKeyGenDelegate(KeyGenOnLittleByte), - out secDB2, out cursors[1], true, - new DatabaseEntry(byteValue)); - - // Get join cursor. - JoinCursor joinCursor = db.Join(cursors, true); - - /* - * MoveNextJoinItem do not use data value found - * in all of the cursor so the data in Current won't be - * changed. - */ - Assert.IsTrue(joinCursor.MoveNextItem()); - Assert.AreEqual(0, joinCursor.Current.Key.Data[ - joinCursor.Current.Key.Data.Length - 1]); - Assert.AreEqual(1, joinCursor.Current.Key.Data[0]); - Assert.IsNull(joinCursor.Current.Value.Data); - - // Iterate on join cursor. - foreach (KeyValuePair pair in joinCursor) - { - /* - * Confirm that the key got by join cursor has 0 at - * its highest byte and 1 at its lowest byte. - */ - Assert.AreEqual(0, pair.Key.Data[pair.Key.Data.Length - 1]); - Assert.AreEqual(1, pair.Key.Data[0]); - } - - Assert.IsFalse(joinCursor.MoveNext()); - - // Close all. - joinCursor.Close(); - cursors[0].Close(); - cursors[1].Close(); - secDB1.Close(); - secDB2.Close(); - db.Close(); - } - - public void GetSecCursor(BTreeDatabase db, - string secFileName, SecondaryKeyGenDelegate keyGen, - out SecondaryBTreeDatabase secDB, - out SecondaryCursor cursor, bool ifCfg, - DatabaseEntry data) - { - // Open secondary database. - SecondaryBTreeDatabaseConfig secCfg = - new SecondaryBTreeDatabaseConfig(db, keyGen); - secCfg.Creation = CreatePolicy.IF_NEEDED; - secCfg.Duplicates = DuplicatesPolicy.SORTED; - secDB = SecondaryBTreeDatabase.Open(secFileName, secCfg); - - int[] intArray = new int[4]; - intArray[0] = 0; - intArray[1] = 1; - intArray[2] = 2049; - intArray[3] = 65537; - for (int i = 0; i < 4; i++) - { - DatabaseEntry record = new DatabaseEntry( - BitConverter.GetBytes(intArray[i])); - db.Put(record, record); - } - - // Get secondary cursor on the secondary database. - if (ifCfg == false) - cursor = secDB.SecondaryCursor(); - else - cursor = secDB.SecondaryCursor(new CursorConfig()); - - // Position the cursor. - if (data != null) - Assert.IsTrue(cursor.Move(data, true)); - } - - public DatabaseEntry KeyGenOnLittleByte( - DatabaseEntry key, DatabaseEntry data) - { - byte[] byteArr = new byte[1]; - byteArr[0] = data.Data[0]; - DatabaseEntry dbtGen = new DatabaseEntry(byteArr); - return dbtGen; - } - - public DatabaseEntry KeyGenOnBigByte( - DatabaseEntry key, DatabaseEntry data) - { - byte[] byteArr = new byte[1]; - byteArr[0] = data.Data[data.Data.Length - 1]; - DatabaseEntry dbtGen = new DatabaseEntry(byteArr); - return dbtGen; - } - } -} diff --git a/test/csharp/LockTest.cs b/test/csharp/LockTest.cs deleted file mode 100644 index fa219aee4..000000000 --- a/test/csharp/LockTest.cs +++ /dev/null @@ -1,252 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Threading; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest { - [TestFixture] - public class LockTest : CSharpTestFixture - { - DatabaseEnvironment testLockStatsEnv; - BTreeDatabase testLockStatsDb; - int DeadlockDidPut = 0; - - [TestFixtureSetUp] - public void SetUpTestFixture() { - testFixtureName = "LockTest"; - base.SetUpTestfixture(); - } - - [Test] - public void TestLockStats() { - testName = "TestLockStats"; - SetUpTest(true); - - // Configure locking subsystem. - LockingConfig lkConfig = new LockingConfig(); - lkConfig.MaxLockers = 60; - lkConfig.MaxLocks = 50; - lkConfig.MaxObjects = 70; - lkConfig.Partitions = 20; - lkConfig.DeadlockResolution = DeadlockPolicy.DEFAULT; - - // Configure and open environment. - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.FreeThreaded = true; - envConfig.LockSystemCfg = lkConfig; - envConfig.LockTimeout = 1000; - envConfig.MPoolSystemCfg = new MPoolConfig(); - envConfig.MPoolSystemCfg.CacheSize = new CacheInfo(0, 104800, 1); - envConfig.NoLocking = false; - envConfig.TxnTimeout = 2000; - envConfig.UseLocking = true; - envConfig.UseMPool = true; - envConfig.UseTxns = true; - DatabaseEnvironment env = - DatabaseEnvironment.Open(testHome, envConfig); - - // Get and confirm locking subsystem statistics. - LockStats stats = env.LockingSystemStats(); - env.PrintLockingSystemStats(true, true); - Assert.AreEqual(0, stats.AllocatedLockers); - Assert.AreNotEqual(0, stats.AllocatedLocks); - Assert.AreNotEqual(0, stats.AllocatedObjects); - Assert.AreEqual(0, stats.InitLockers); - Assert.AreNotEqual(0, stats.InitLocks); - Assert.AreNotEqual(0, stats.InitObjects); - Assert.AreEqual(0, stats.LastAllocatedLockerID); - Assert.AreEqual(0, stats.LockConflictsNoWait); - Assert.AreEqual(0, stats.LockConflictsWait); - Assert.AreEqual(0, stats.LockDeadlocks); - Assert.AreEqual(0, stats.LockDowngrades); - Assert.AreEqual(0, stats.LockerNoWait); - Assert.AreEqual(0, stats.Lockers); - Assert.AreEqual(0, stats.LockerWait); - Assert.AreEqual(9, stats.LockModes); - Assert.AreEqual(0, stats.LockPuts); - Assert.AreEqual(0, stats.LockRequests); - Assert.AreEqual(0, stats.Locks); - Assert.AreEqual(0, stats.LockSteals); - Assert.AreEqual(1000, stats.LockTimeoutLength); - Assert.AreEqual(0, stats.LockTimeouts); - Assert.AreEqual(0, stats.LockUpgrades); - Assert.AreEqual(0, stats.MaxBucketLength); - Assert.AreEqual(0, stats.MaxLockers); - Assert.AreEqual(60, stats.MaxLockersInTable); - Assert.AreEqual(0, stats.MaxLocks); - Assert.AreEqual(0, stats.MaxLocksInBucket); - Assert.AreEqual(50, stats.MaxLocksInTable); - Assert.AreEqual(0, stats.MaxLockSteals); - Assert.AreEqual(0, stats.MaxObjects); - Assert.AreEqual(0, stats.MaxObjectsInBucket); - Assert.AreEqual(70, stats.MaxObjectsInTable); - Assert.AreEqual(0, stats.MaxObjectSteals); - Assert.AreEqual(0, stats.MaxPartitionLockNoWait); - Assert.AreEqual(0, stats.MaxPartitionLockWait); - Assert.AreNotEqual(0, stats.MaxUnusedID); - Assert.AreEqual(20, stats.nPartitions); - Assert.AreEqual(0, stats.ObjectNoWait); - Assert.AreEqual(0, stats.Objects); - Assert.AreEqual(0, stats.ObjectSteals); - Assert.AreEqual(0, stats.ObjectWait); - Assert.LessOrEqual(0, stats.PartitionLockNoWait); - Assert.AreEqual(0, stats.PartitionLockWait); - Assert.Less(0, stats.RegionNoWait); - Assert.AreNotEqual(0, stats.RegionSize); - Assert.AreEqual(0, stats.RegionWait); - Assert.AreNotEqual(0, stats.TableSize); - Assert.AreEqual(2000, stats.TxnTimeoutLength); - Assert.AreEqual(0, stats.TxnTimeouts); - - env.PrintLockingSystemStats(); - - Transaction txn = env.BeginTransaction(); - BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Env = env; - dbConfig.FreeThreaded = true; - BTreeDatabase db = BTreeDatabase.Open( - testName + ".db", dbConfig, txn); - txn.Commit(); - - testLockStatsEnv = env; - testLockStatsDb = db; - - // Use some locks, to ensure the stats work when populated. - txn = testLockStatsEnv.BeginTransaction(); - for (int i = 0; i < 500; i++) - { - testLockStatsDb.Put( - new DatabaseEntry(BitConverter.GetBytes(i)), - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes( - Configuration.RandomString(i))), txn); - testLockStatsDb.Sync(); - } - txn.Commit(); - - env.PrintLockingSystemStats(); - stats = env.LockingSystemStats(); - Assert.Less(0, stats.LastAllocatedLockerID); - Assert.Less(0, stats.LockDowngrades); - Assert.LessOrEqual(0, stats.LockerNoWait); - Assert.Less(0, stats.Lockers); - Assert.LessOrEqual(0, stats.LockerWait); - Assert.Less(0, stats.LockPuts); - Assert.Less(0, stats.LockRequests); - Assert.Less(0, stats.Locks); - Assert.LessOrEqual(0, stats.LockSteals); - Assert.LessOrEqual(0, stats.LockTimeouts); - Assert.LessOrEqual(0, stats.LockUpgrades); - Assert.Less(0, stats.MaxBucketLength); - Assert.Less(0, stats.MaxLockers); - Assert.Less(0, stats.MaxLocks); - Assert.Less(0, stats.MaxLocksInBucket); - Assert.LessOrEqual(0, stats.MaxLockSteals); - Assert.Less(0, stats.MaxObjects); - Assert.Less(0, stats.MaxObjectsInBucket); - Assert.LessOrEqual(0, stats.MaxObjectSteals); - Assert.LessOrEqual(0, stats.MaxPartitionLockNoWait); - Assert.LessOrEqual(0, stats.MaxPartitionLockWait); - Assert.Less(0, stats.MaxUnusedID); - Assert.LessOrEqual(0, stats.ObjectNoWait); - Assert.Less(0, stats.Objects); - Assert.LessOrEqual(0, stats.ObjectSteals); - Assert.LessOrEqual(0, stats.ObjectWait); - Assert.Less(0, stats.PartitionLockNoWait); - Assert.LessOrEqual(0, stats.PartitionLockWait); - Assert.Less(0, stats.RegionNoWait); - Assert.LessOrEqual(0, stats.RegionWait); - Assert.LessOrEqual(0, stats.TxnTimeouts); - - // Force a deadlock to ensure the stats work. - txn = testLockStatsEnv.BeginTransaction(); - testLockStatsDb.Put(new DatabaseEntry(BitConverter.GetBytes(10)), - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes( - Configuration.RandomString(200))), txn); - - Thread thread1 = new Thread(GenerateDeadlock); - thread1.Start(); - while (DeadlockDidPut == 0) - Thread.Sleep(10); - - try - { - testLockStatsDb.Get(new DatabaseEntry( - BitConverter.GetBytes(100)), txn); - } - catch (DeadlockException) { } - // Abort unconditionally - we don't care about the transaction - txn.Abort(); - thread1.Join(); - - stats = env.LockingSystemStats(); - Assert.Less(0, stats.LockConflictsNoWait); - Assert.LessOrEqual(0, stats.LockConflictsWait); - - db.Close(); - env.Close(); - } - - public void GenerateDeadlock() - { - Transaction txn = testLockStatsEnv.BeginTransaction(); - try - { - testLockStatsDb.Put( - new DatabaseEntry(BitConverter.GetBytes(100)), - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes( - Configuration.RandomString(200))), txn); - DeadlockDidPut = 1; - testLockStatsDb.Get(new DatabaseEntry( - BitConverter.GetBytes(10)), txn); - - } - catch (DeadlockException) { } - // Abort unconditionally - we don't care about the transaction - txn.Abort(); - } - - public static void LockingEnvSetUp(string testHome, - string testName, out DatabaseEnvironment env, - uint maxLock, uint maxLocker, uint maxObject, - uint partition) { - // Configure env and locking subsystem. - LockingConfig lkConfig = new LockingConfig(); - /* - * If the maximum number of locks/lockers/objects - * is given, then the LockingConfig is set. Unless, - * it is not set to any value. - */ - if (maxLock != 0) - lkConfig.MaxLocks = maxLock; - if (maxLocker != 0) - lkConfig.MaxLockers = maxLocker; - if (maxObject != 0) - lkConfig.MaxObjects = maxObject; - if (partition != 0) - lkConfig.Partitions = partition; - - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.LockSystemCfg = lkConfig; - envConfig.UseLocking = true; - envConfig.ErrorPrefix = testName; - - env = DatabaseEnvironment.Open(testHome, envConfig); - } - } -} diff --git a/test/csharp/LockingConfigTest.cs b/test/csharp/LockingConfigTest.cs deleted file mode 100644 index 8099f6907..000000000 --- a/test/csharp/LockingConfigTest.cs +++ /dev/null @@ -1,154 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class LockingConfigTest : CSharpTestFixture - { - - [TestFixtureSetUp] - public void SetUpTestFixture() - { - testFixtureName = "LockingConfigTest"; - base.SetUpTestfixture(); - } - - [Test] - public void TestConfig() - { - testName = "TestConfig"; - SetUpTest(false); - - // Configure the fields/properties. - LockingConfig lockingConfig = new LockingConfig(); - XmlElement xmlElem = Configuration.TestSetUp( - testFixtureName, testName); - - // Configure LockingConfig - Config(xmlElem, ref lockingConfig, true); - - // Confirm LockingConfig - Confirm(xmlElem, lockingConfig, true); - } - - [Test] - public void TestMaxLock() { - testName = "TestMaxLock"; - SetUpTest(true); - - DatabaseEnvironment env; - uint maxLocks; - DatabaseEntry obj; - - maxLocks = 1; - obj = new DatabaseEntry(); - - /* - * Initialize environment using locking subsystem. The max number - * of locks should be larger than environment's partitions. So in - * order to make the MaxLock work, the environment paritition is - * set to be the same value as MaxLock. - */ - LockTest.LockingEnvSetUp(testHome, testName, out env, - maxLocks, 0, 0, maxLocks); - Assert.AreEqual(maxLocks, env.MaxLocks); - - env.Close(); - } - - [Test] - public void TestMaxLocker() { - testName = "TestMaxLocker"; - SetUpTest(true); - - DatabaseEnvironment env; - uint maxLockers; - - maxLockers = 1; - LockTest.LockingEnvSetUp(testHome, testName, out env, - 0, maxLockers, 0, 0); - Assert.AreEqual(maxLockers, env.MaxLockers); - env.Close(); - } - - [Test] - public void TestMaxObjects() { - testName = "TestMaxObjects"; - SetUpTest(true); - - DatabaseEnvironment env; - uint maxObjects; - - maxObjects = 1; - - /* - * Initialize environment using locking subsystem. The max number - * of objects should be larger than environment's partitions. So - * in order to make the MaxObject work, the environment paritition - * is set to be the same value as MaxObject. - */ - LockTest.LockingEnvSetUp(testHome, testName, out env, 0, 0, - maxObjects, maxObjects); - Assert.AreEqual(maxObjects, env.MaxObjects); - env.Close(); - } - - public static void Confirm(XmlElement xmlElement, - LockingConfig lockingConfig, bool compulsory) - { - Configuration.ConfirmByteMatrix(xmlElement, "Conflicts", - lockingConfig.Conflicts, compulsory); - Configuration.ConfirmDeadlockPolicy(xmlElement, - "DeadlockResolution", - lockingConfig.DeadlockResolution, compulsory); - Configuration.ConfirmUint(xmlElement, "MaxLockers", - lockingConfig.MaxLockers, compulsory); - Configuration.ConfirmUint(xmlElement, "MaxLocks", - lockingConfig.MaxLocks, compulsory); - Configuration.ConfirmUint(xmlElement, "MaxObjects", - lockingConfig.MaxObjects, compulsory); - Configuration.ConfirmUint(xmlElement, "Partitions", - lockingConfig.Partitions, compulsory); - } - - public static void Config(XmlElement xmlElement, - ref LockingConfig lockingConfig, bool compulsory) - { - byte[,] matrix = new byte[6, 6]; - uint value = new uint(); - - if (Configuration.ConfigByteMatrix(xmlElement, "Conflicts", - ref matrix, compulsory) == true) - lockingConfig.Conflicts = matrix; - - Configuration.ConfigDeadlockPolicy(xmlElement, "DeadlockResolution", - ref lockingConfig.DeadlockResolution, compulsory); - if (Configuration.ConfigUint(xmlElement, "MaxLockers", - ref value, compulsory)) - lockingConfig.MaxLockers = value; - if (Configuration.ConfigUint(xmlElement, "MaxLocks", - ref value, compulsory)) - lockingConfig.MaxLocks = value; - if (Configuration.ConfigUint(xmlElement, "MaxObjects", - ref value, compulsory)) - lockingConfig.MaxObjects = value; - if (Configuration.ConfigUint(xmlElement, "Partitions", - ref value, compulsory)) - lockingConfig.Partitions = value; - } - - } -} diff --git a/test/csharp/LogConfigTest.cs b/test/csharp/LogConfigTest.cs deleted file mode 100644 index 337b2f787..000000000 --- a/test/csharp/LogConfigTest.cs +++ /dev/null @@ -1,292 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class LogConfigTest : CSharpTestFixture - { - - [TestFixtureSetUp] - public void SetUpTestFixture() - { - testFixtureName = "LogConfigTest"; - base.SetUpTestfixture(); - } - - [Test] - public void TestConfig() - { - testName = "TestConfig"; - SetUpTest(false); - /* - * Configure the fields/properties and see if - * they are updated successfully. - */ - LogConfig logConfig = new LogConfig(); - XmlElement xmlElem = Configuration.TestSetUp(testFixtureName, testName); - Config(xmlElem, ref logConfig, true); - Confirm(xmlElem, logConfig, true); - } - - [Test, ExpectedException(typeof(ExpectedTestException))] - public void TestFullLogBufferException() - { - testName = "TestFullLogBufferException"; - SetUpTest(true); - - // Open an environment and configured log subsystem. - DatabaseEnvironmentConfig cfg = - new DatabaseEnvironmentConfig(); - cfg.Create = true; - cfg.TxnNoSync = true; - cfg.UseTxns = true; - cfg.UseLocking = true; - cfg.UseMPool = true; - cfg.UseLogging = true; - cfg.LogSystemCfg = new LogConfig(); - cfg.LogSystemCfg.AutoRemove = false; - cfg.LogSystemCfg.BufferSize = 409600; - cfg.LogSystemCfg.MaxFileSize = 10480; - cfg.LogSystemCfg.NoBuffer = false; - cfg.LogSystemCfg.ZeroOnCreate = true; - cfg.LogSystemCfg.InMemory = true; - DatabaseEnvironment env = DatabaseEnvironment.Open(testHome, cfg); - - BTreeDatabase db; - try - { - Transaction openTxn = env.BeginTransaction(); - try - { - BTreeDatabaseConfig dbConfig = - new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Env = env; - db = BTreeDatabase.Open(testName + ".db", dbConfig, openTxn); - openTxn.Commit(); - } - catch (DatabaseException e) - { - openTxn.Abort(); - throw e; - } - - Transaction writeTxn = env.BeginTransaction(); - try - { - /* - * Writing 10 large records into in-memory logging - * database should throw FullLogBufferException since - * the amount of put data is larger than buffer size. - */ - byte[] byteArr = new byte[204800]; - for (int i = 0; i < 10; i++) - db.Put(new DatabaseEntry(BitConverter.GetBytes(i)), - new DatabaseEntry(byteArr), writeTxn); - writeTxn.Commit(); - } - catch (Exception e) - { - writeTxn.Abort(); - throw e; - } - finally - { - db.Close(true); - } - } - catch (FullLogBufferException e) - { - Assert.AreEqual(ErrorCodes.DB_LOG_BUFFER_FULL, e.ErrorCode); - throw new ExpectedTestException(); - } - finally - { - env.Close(); - } - } - - [Test] - public void TestLoggingSystemStats() - { - testName = "TestLoggingSystemStats"; - SetUpTest(true); - string logDir = "./"; - - Directory.CreateDirectory(testHome + "/" + logDir); - - DatabaseEnvironmentConfig cfg = - new DatabaseEnvironmentConfig(); - cfg.Create = true; - cfg.UseTxns = true; - cfg.AutoCommit = true; - cfg.UseLocking = true; - cfg.UseMPool = true; - cfg.UseLogging = true; - cfg.MPoolSystemCfg = new MPoolConfig(); - cfg.MPoolSystemCfg.CacheSize = new CacheInfo(0, 1048576, 1); - - cfg.LogSystemCfg = new LogConfig(); - cfg.LogSystemCfg.AutoRemove = false; - cfg.LogSystemCfg.BufferSize = 10240; - cfg.LogSystemCfg.Dir = logDir; - cfg.LogSystemCfg.FileMode = 755; - cfg.LogSystemCfg.ForceSync = true; - cfg.LogSystemCfg.InMemory = false; - cfg.LogSystemCfg.MaxFileSize = 1048576; - cfg.LogSystemCfg.NoBuffer = false; - cfg.LogSystemCfg.RegionSize = 204800; - cfg.LogSystemCfg.ZeroOnCreate = true; - - DatabaseEnvironment env = DatabaseEnvironment.Open(testHome, cfg); - - LogStats stats = env.LoggingSystemStats(); - env.PrintLoggingSystemStats(); - Assert.AreEqual(10240, stats.BufferSize); - Assert.AreEqual(1, stats.CurrentFile); - Assert.AreNotEqual(0, stats.CurrentOffset); - Assert.AreEqual(0, stats.FileId); - Assert.AreEqual(1048576, stats.FileSize); - Assert.AreEqual(0, stats.InitFileId); - Assert.AreNotEqual(0, stats.MagicNumber); - Assert.AreEqual(0, stats.MaxFileId); - Assert.AreNotEqual(0, stats.PermissionsMode); - Assert.AreEqual(1, stats.Records); - Assert.AreNotEqual(0, stats.RegionLockNoWait); - Assert.LessOrEqual(204800, stats.RegionSize); - Assert.AreNotEqual(0, stats.Version); - - Transaction openTxn = env.BeginTransaction(); - BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Env = env; - BTreeDatabase db = BTreeDatabase.Open(testName + ".db", dbConfig, openTxn); - openTxn.Commit(); - - Transaction writeTxn = env.BeginTransaction(); - byte[] byteArr = new byte[1024]; - for (int i = 0; i < 1000; i++) - db.Put(new DatabaseEntry(BitConverter.GetBytes(i)), - new DatabaseEntry(byteArr), writeTxn); - writeTxn.Commit(); - - stats = env.LoggingSystemStats(); - Assert.AreNotEqual(0, stats.Bytes); - Assert.AreNotEqual(0, stats.BytesSinceCheckpoint); - Assert.AreNotEqual(0, stats.DiskFileNumber); - Assert.AreNotEqual(0, stats.DiskOffset); - Assert.AreNotEqual(0, stats.MaxCommitsPerFlush); - Assert.AreNotEqual(0, stats.MBytes); - Assert.AreNotEqual(0, stats.MBytesSinceCheckpoint); - Assert.AreNotEqual(0, stats.MinCommitsPerFlush); - Assert.AreNotEqual(0, stats.OverflowWrites); - Assert.AreNotEqual(0, stats.Syncs); - Assert.AreNotEqual(0, stats.Writes); - Assert.AreEqual(0, stats.Reads); - Assert.AreEqual(0, stats.RegionLockWait); - - stats = env.LoggingSystemStats(true); - stats = env.LoggingSystemStats(); - Assert.AreEqual(0, stats.Bytes); - Assert.AreEqual(0, stats.BytesSinceCheckpoint); - Assert.AreEqual(0, stats.MaxCommitsPerFlush); - Assert.AreEqual(0, stats.MBytes); - Assert.AreEqual(0, stats.MBytesSinceCheckpoint); - Assert.AreEqual(0, stats.MinCommitsPerFlush); - Assert.AreEqual(0, stats.OverflowWrites); - Assert.AreEqual(0, stats.Syncs); - Assert.AreEqual(0, stats.Writes); - Assert.AreEqual(0, stats.Reads); - - env.PrintLoggingSystemStats(true, true); - - db.Close(); - env.Close(); - } - - [Test] - public void TestLsn() - { - testName = "TestLsn"; - SetUpTest(true); - - LSN lsn = new LSN(12, 411); - Assert.AreEqual(12, lsn.LogFileNumber); - Assert.AreEqual(411, lsn.Offset); - - LSN newLsn = new LSN(15, 410); - Assert.AreEqual(0, LSN.Compare(lsn, lsn)); - Assert.Greater(0, LSN.Compare(lsn, newLsn)); - } - - public static void Confirm(XmlElement - xmlElement, LogConfig logConfig, bool compulsory) - { - Configuration.ConfirmBool(xmlElement, "AutoRemove", - logConfig.AutoRemove, compulsory); - Configuration.ConfirmUint(xmlElement, "BufferSize", - logConfig.BufferSize, compulsory); - Configuration.ConfirmString(xmlElement, "Dir", - logConfig.Dir, compulsory); - Configuration.ConfirmInt(xmlElement, "FileMode", - logConfig.FileMode, compulsory); - Configuration.ConfirmBool(xmlElement, "ForceSync", - logConfig.ForceSync, compulsory); - Configuration.ConfirmBool(xmlElement, "InMemory", - logConfig.InMemory, compulsory); - Configuration.ConfirmUint(xmlElement, "MaxFileSize", - logConfig.MaxFileSize, compulsory); - Configuration.ConfirmBool(xmlElement, "NoBuffer", - logConfig.NoBuffer, compulsory); - Configuration.ConfirmUint(xmlElement, "RegionSize", - logConfig.RegionSize, compulsory); - Configuration.ConfirmBool(xmlElement, "ZeroOnCreate", - logConfig.ZeroOnCreate, compulsory); - } - - public static void Config(XmlElement - xmlElement, ref LogConfig logConfig, bool compulsory) - { - uint uintValue = new uint(); - int intValue = new int(); - - Configuration.ConfigBool(xmlElement, "AutoRemove", - ref logConfig.AutoRemove, compulsory); - if (Configuration.ConfigUint(xmlElement, "BufferSize", - ref uintValue, compulsory)) - logConfig.BufferSize = uintValue; - Configuration.ConfigString(xmlElement, "Dir", - ref logConfig.Dir, compulsory); - if (Configuration.ConfigInt(xmlElement, "FileMode", - ref intValue, compulsory)) - logConfig.FileMode = intValue; - Configuration.ConfigBool(xmlElement, "ForceSync", - ref logConfig.ForceSync, compulsory); - Configuration.ConfigBool(xmlElement, "InMemory", - ref logConfig.InMemory, compulsory); - if (Configuration.ConfigUint(xmlElement, "MaxFileSize", - ref uintValue, compulsory)) - logConfig.MaxFileSize = uintValue; - Configuration.ConfigBool(xmlElement, "NoBuffer", - ref logConfig.NoBuffer, compulsory); - if (Configuration.ConfigUint(xmlElement, "RegionSize", - ref uintValue, compulsory)) - logConfig.RegionSize = uintValue; - Configuration.ConfigBool(xmlElement, "ZeroOnCreate", - ref logConfig.ZeroOnCreate, compulsory); - } - } -} diff --git a/test/csharp/LogCursorTest.cs b/test/csharp/LogCursorTest.cs deleted file mode 100644 index 560df6d8b..000000000 --- a/test/csharp/LogCursorTest.cs +++ /dev/null @@ -1,315 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class LogCursorTest - { - DatabaseEnvironment env; - - [TestFixtureSetUp] - public void SetUpTestFixture() - { - testFixtureName = "LogCursorTest"; - base.SetUpTestfixture(); - - /* - * Delete existing test ouput directory and files specified - * for the current test fixture and then create a new one. - */ - } - - [Test] - public void TestClose() - { - testName = "TestClose"; - SetUpTest(true); - DatabaseEnvironment env; - LogCursor logCursor; - RecnoDatabase db; - - Logging(testHome, testName, out env, out db); - - // Get log cursor to read/write log. - logCursor = env.GetLogCursor(); - - // Close the log cursor and env. - logCursor.Close(); - db.Close(); - env.Close(); - } - - [Test] - public void TesCurrentLSN() - { - testName = "TesCurrentLSN"; - SetUpTest(true); - RecnoDatabase db; - - Logging(testHome, testName, out env, out db); - - // Get log cursor to read/write log. - LogCursor logCursor = env.GetLogCursor(); - - /* - * Move the cursor to the beginning of the #1 log file. - * Get the current LSN and confirm that is the position - * the cursor is moved to. - */ - LSN lsn = new LSN(1, 0); - logCursor.Move(lsn); - Assert.AreEqual(lsn.LogFileNumber, - logCursor.CurrentLSN.LogFileNumber); - Assert.AreEqual(lsn.Offset, logCursor.CurrentLSN.Offset); - - // Close all. - logCursor.Close(); - db.Close(); - env.Close(); - } - - [Test] - public void TestCurrentRecord() - { - testName = "TestCurrentRecord"; - SetUpTest(true); - DatabaseEnvironment env; - RecnoDatabase db; - - Logging(testHome, testName, out env, out db); - - // Get log cursor to read/write log. - LogCursor logCursor = env.GetLogCursor(); - - /* - * Move the cursor to the beginning of the #1 log file. - * Get the current LSN and confirm that is the position - * the cursor is moved to. - */ - LSN lsn = new LSN(1, 0); - logCursor.Move(lsn); - Assert.IsNotNull(logCursor.CurrentRecord.Data); - - // Close all. - logCursor.Close(); - db.Close(); - env.Close(); - } - - [Test] - public void TestMove() - { - testName = "TestMove"; - SetUpTest(true); - DatabaseEnvironment env; - RecnoDatabase db; - - Logging(testHome, testName, out env, out db); - - // Get log cursor to read/write log. - LogCursor logCursor = env.GetLogCursor(); - - // Move the cursor to specified location in log files. - LSN lsn = new LSN(1, 0); - Assert.IsTrue(logCursor.Move(lsn)); - - // Close all. - logCursor.Close(); - db.Close(); - env.Close(); - } - - /* - [Test] - public void TestMoveFirst() - { - testName = "TestMoveFirst"; - SetUpTest(true); - DatabaseEnvironment env; - RecnoDatabase db; - - Logging(testHome, testName, out env, out db); - - // Get log cursor to read/write log. - LogCursor logCursor = env.GetLogCursor(); - - // Move to the first LSN in log file. - Assert.IsTrue(logCursor.MoveFirst()); - - // Confirm offset of the fist LSN should be 0. - Assert.AreEqual(0, logCursor.CurrentLSN.Offset); - - // Close all. - logCursor.Close(); - db.Close(); - env.Close(); - } - */ - - [Test] - public void TestMoveLast() - { - testName = "TestMoveLast"; - SetUpTest(true); - DatabaseEnvironment env; - RecnoDatabase db; - - Logging(testHome, testName, out env, out db); - - // Get log cursor to read/write log. - LogCursor logCursor = env.GetLogCursor(); - - // Move to the last LSN in log file. - Assert.IsTrue(logCursor.MoveLast()); - - // The offset of last LSN shouldn't be 0. - Assert.AreNotEqual(0, logCursor.CurrentLSN.Offset); - - // Close all. - logCursor.Close(); - db.Close(); - env.Close(); - } - - [Test] - public void TestMoveNext() - { - testName = "TestMoveNext"; - SetUpTest(true); - DatabaseEnvironment env; - RecnoDatabase db; - - Logging(testHome, testName, out env, out db); - - // Get log cursor to read/write log. - LogCursor logCursor = env.GetLogCursor(); - - logCursor.MoveLast(); - DatabaseEntry curRec = logCursor.CurrentRecord; - for (int i = 0; i < 1000; i++) - db.Append(new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("new data"))); - - Assert.IsTrue(logCursor.MoveNext()); - - logCursor.MoveNext(); - - // Close the log cursor. - logCursor.Close(); - db.Close(); - env.Close(); - } - - [Test] - public void TestMovePrev() - { - testName = "TestMovePrev"; - SetUpTest(true); - DatabaseEnvironment env; - LSN lsn; - RecnoDatabase db; - - Logging(testHome, testName, out env, out db); - - // Get log cursor to read/write log. - LogCursor logCursor = env.GetLogCursor(); - - // Get the last two LSN in log file. - logCursor.MoveLast(); - Assert.IsTrue(logCursor.MovePrev()); - - // Close all. - logCursor.Close(); - db.Close(); - env.Close(); - } - - [Test] - public void TestRefresh() - { - testName = "TestRefresh"; - SetUpTest(true); - DatabaseEnvironment env; - LSN lsn; - RecnoDatabase db; - - Logging(testHome, testName, out env, out db); - - // Get log cursor to read/write log. - LogCursor logCursor = env.GetLogCursor(); - - // Move the cursor to the last record. - logCursor.MoveLast(); - DatabaseEntry curRec = logCursor.CurrentRecord; - - // Put some new records into database. - for (int i = 0; i < 10; i++) - db.Append(new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("new data"))); - - // Get the current record that cursor points to. - logCursor.Refresh(); - - // It shouldn't be changed. - Assert.AreEqual(curRec.Data, - logCursor.CurrentRecord.Data); - - // Close all. - logCursor.Close(); - db.Close(); - env.Close(); - } - - /* - * Open environment, database and write data into database. - * Generated log files are put under testHome. - */ - public void Logging(string home, string dbName, - out DatabaseEnvironment env, out RecnoDatabase recnoDB) - { - string dbFileName = dbName + ".db"; - - Configuration.ClearDir(home); - - // Open environment with logging subsystem. - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseLogging = true; - envConfig.LogSystemCfg = new LogConfig(); - envConfig.LogSystemCfg.FileMode = 755; - envConfig.LogSystemCfg.ZeroOnCreate = true; - envConfig.UseMPool = true; - env = DatabaseEnvironment.Open(home, envConfig); - - /* - * Open recno database, write 100000 records into - * the database and close it. - */ - RecnoDatabaseConfig recnoConfig = - new RecnoDatabaseConfig(); - recnoConfig.Creation = CreatePolicy.IF_NEEDED; - recnoConfig.Env = env; - // The db needs mpool to open. - recnoConfig.NoMMap = false; - recnoDB = RecnoDatabase.Open(dbFileName, - recnoConfig); - for (int i = 0; i < 1000; i++) - recnoDB.Append(new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("key"))); - } - } -} diff --git a/test/csharp/MPoolConfigTest.cs b/test/csharp/MPoolConfigTest.cs deleted file mode 100644 index 42cd8eb7d..000000000 --- a/test/csharp/MPoolConfigTest.cs +++ /dev/null @@ -1,83 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class MPoolConfigTest : CSharpTestFixture - { - [TestFixtureSetUp] - public void SetUpTestFixture() - { - testFixtureName = "MPoolConfigTest"; - base.SetUpTestfixture(); - } - - [Test] - public void TestConfig() - { - testName = "TestConfig"; - SetUpTest(false); - - // Config and confirm mpool subsystem configuration. - MPoolConfig mpoolConfig = new MPoolConfig(); - XmlElement xmlElem = Configuration.TestSetUp( - testFixtureName, testName); - Config(xmlElem, ref mpoolConfig, true); - Confirm(xmlElem, mpoolConfig, true); - } - - public static void Confirm(XmlElement - xmlElement, MPoolConfig mpoolConfig, bool compulsory) - { - Configuration.ConfirmCacheSize(xmlElement, - "CacheSize", mpoolConfig.CacheSize, compulsory); - Configuration.ConfirmCacheSize(xmlElement, - "MaxCacheSize", mpoolConfig.MaxCacheSize, - compulsory); - Configuration.ConfirmInt(xmlElement, "MaxOpenFiles", - mpoolConfig.MaxOpenFiles, compulsory); - Configuration.ConfirmUint(xmlElement, - "MMapSize", - mpoolConfig.MMapSize, compulsory); - Configuration.ConfirmMaxSequentialWrites(xmlElement, - "MaxSequentialWrites", - mpoolConfig.SequentialWritePause, - mpoolConfig.MaxSequentialWrites, compulsory); - } - - public static void Config(XmlElement - xmlElement, ref MPoolConfig mpoolConfig, bool compulsory) - { - uint uintValue = new uint(); - int intValue = new int(); - - Configuration.ConfigCacheInfo(xmlElement, - "CacheSize", ref mpoolConfig.CacheSize, compulsory); - Configuration.ConfigCacheInfo(xmlElement, - "MaxCacheSize", ref mpoolConfig.MaxCacheSize, - compulsory); - if (Configuration.ConfigInt(xmlElement, "MaxOpenFiles", - ref intValue, compulsory)) - mpoolConfig.MaxOpenFiles = intValue; - Configuration.ConfigMaxSequentialWrites( - xmlElement, "MaxSequentialWrites", mpoolConfig, - compulsory); - if (Configuration.ConfigUint(xmlElement, - "MMapSize", ref uintValue, compulsory)) - mpoolConfig.MMapSize = uintValue; - } - } -} diff --git a/test/csharp/MutexConfigTest.cs b/test/csharp/MutexConfigTest.cs deleted file mode 100644 index c0cb3e0cc..000000000 --- a/test/csharp/MutexConfigTest.cs +++ /dev/null @@ -1,78 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class MutexConfigTest : CSharpTestFixture - { - - [TestFixtureSetUp] - public void SetUpTestFixture() - { - testFixtureName = "MutexConfigTest"; - base.SetUpTestfixture(); - } - - [Test] - public void TestConfig() - { - testName = "TestConfig"; - SetUpTest(false); - - /* - * Configure the fields/properties and see if - * they are updated successfully. - */ - MutexConfig lockingConfig = new MutexConfig(); - XmlElement xmlElem = Configuration.TestSetUp( - testFixtureName, testName); - Config(xmlElem, ref lockingConfig, true); - Confirm(xmlElem, lockingConfig, true); - } - - public static void Confirm(XmlElement - xmlElement, MutexConfig mutexConfig, bool compulsory) - { - Configuration.ConfirmUint(xmlElement, "Alignment", - mutexConfig.Alignment, compulsory); - Configuration.ConfirmUint(xmlElement, "Increment", - mutexConfig.Increment, compulsory); - Configuration.ConfirmUint(xmlElement, "MaxMutexes", - mutexConfig.MaxMutexes, compulsory); - Configuration.ConfirmUint(xmlElement, - "NumTestAndSetSpins", - mutexConfig.NumTestAndSetSpins, compulsory); - } - - public static void Config(XmlElement - xmlElement, ref MutexConfig mutexConfig, bool compulsory) - { - uint value = new uint(); - if (Configuration.ConfigUint(xmlElement, "Alignment", - ref value, compulsory)) - mutexConfig.Alignment = value; - if (Configuration.ConfigUint(xmlElement, "Increment", - ref value, compulsory)) - mutexConfig.Increment = value; - if (Configuration.ConfigUint(xmlElement, "MaxMutexes", - ref value, compulsory)) - mutexConfig.MaxMutexes = value; - if (Configuration.ConfigUint(xmlElement, - "NumTestAndSetSpins", ref value, compulsory)) - mutexConfig.NumTestAndSetSpins = value; - } - } -} diff --git a/test/csharp/MutexTest.cs b/test/csharp/MutexTest.cs deleted file mode 100644 index 285442913..000000000 --- a/test/csharp/MutexTest.cs +++ /dev/null @@ -1,119 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Threading; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class MutexTest - { - - private BTreeDatabase TestDB; - private BerkeleyDB.Mutex TestMutex; - - [TestFixtureSetUp] - public void SetUpTestFixture() - { - testFixtureName = "MutexTest"; - base.SetUpTestfixture(); - - } - - [Test] - public void TestGetAndFreeMutex() - { - testName = "TestGetAndFreeMutex"; - SetUpTest(true); - - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseMPool = true; - DatabaseEnvironment env = DatabaseEnvironment.Open( - testHome, envConfig); - BerkeleyDB.Mutex mutex = env.GetMutex(true, true); - mutex.Dispose(); - env.Close(); - } - - [Test] - public void TestLockAndUnlockMutex() - { - testName = "TestLockAndUnlockMutex"; - SetUpTest(true); - - /* - * Open an environment without locking and - * deadlock detection. - */ - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.FreeThreaded = true; - envConfig.UseLogging = true; - envConfig.Create = true; - envConfig.UseMPool = true; - DatabaseEnvironment env = DatabaseEnvironment.Open( - testHome, envConfig); - - // Open a database. - BTreeDatabaseConfig dbConfig = - new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Env = env; - TestDB = BTreeDatabase.Open( - testName + ".db", dbConfig); - - // Get a mutex which will be used in two threads. - TestMutex = env.GetMutex(true, false); - - // Begin two threads to write records into database. - Thread mutexThread1 = new Thread( - new ThreadStart(MutexThread1)); - Thread mutexThread2 = new Thread( - new ThreadStart(MutexThread2)); - mutexThread1.Start(); - mutexThread2.Start(); - mutexThread1.Join(); - mutexThread2.Join(); - - // Free the mutex. - TestMutex.Dispose(); - - // Close all. - TestDB.Close(); - env.Close(); - } - - public void MutexThread1() - { - TestMutex.Lock(); - for (int i = 0; i < 100; i++) - TestDB.Put(new DatabaseEntry( - BitConverter.GetBytes(i)), - new DatabaseEntry(new byte[102400])); - TestMutex.Unlock(); - } - - public void MutexThread2() - { - TestMutex.Lock(); - for (int i = 0; i < 100; i++) - TestDB.Put(new DatabaseEntry( - BitConverter.GetBytes(i)), - new DatabaseEntry(new byte[102400])); - TestMutex.Unlock(); - } - } -} diff --git a/test/csharp/QueueDatabaseConfigTest.cs b/test/csharp/QueueDatabaseConfigTest.cs deleted file mode 100644 index 8d23aaef9..000000000 --- a/test/csharp/QueueDatabaseConfigTest.cs +++ /dev/null @@ -1,84 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class QueueDatabaseConfigTest : DatabaseConfigTest - { - - [TestFixtureSetUp] - public override void SetUpTestFixture() - { - testFixtureName = "QueueDatabaseConfigTest"; - base.SetUpTestfixture(); - } - - [Test] - new public void TestConfigWithoutEnv() - { - testName = "TestConfigWithoutEnv"; - SetUpTest(false); - XmlElement xmlElem = Configuration.TestSetUp( - testFixtureName, testName); - QueueDatabaseConfig queueDBConfig = - new QueueDatabaseConfig(); - Config(xmlElem, ref queueDBConfig, true); - Confirm(xmlElem, queueDBConfig, true); - } - - public static void Confirm(XmlElement xmlElement, - QueueDatabaseConfig queueDBConfig, bool compulsory) - { - DatabaseConfig dbConfig = queueDBConfig; - Confirm(xmlElement, dbConfig, compulsory); - - // Confirm Queue database specific configuration - Configuration.ConfirmBool(xmlElement, "ConsumeInOrder", - queueDBConfig.ConsumeInOrder, compulsory); - Configuration.ConfirmCreatePolicy(xmlElement, "Creation", - queueDBConfig.Creation, compulsory); - Configuration.ConfirmUint(xmlElement, "Length", - queueDBConfig.Length, compulsory); - Configuration.ConfirmInt(xmlElement, "PadByte", - queueDBConfig.PadByte, compulsory); - Configuration.ConfirmUint(xmlElement, "ExtentSize", - queueDBConfig.ExtentSize, compulsory); - } - - public static void Config(XmlElement xmlElement, - ref QueueDatabaseConfig queueDBConfig, bool compulsory) - { - uint uintValue = new uint(); - int intValue = new int(); - DatabaseConfig dbConfig = queueDBConfig; - Config(xmlElement, ref dbConfig, compulsory); - - // Configure specific fields/properties of Queue database - Configuration.ConfigBool(xmlElement, "ConsumeInOrder", - ref queueDBConfig.ConsumeInOrder, compulsory); - Configuration.ConfigCreatePolicy(xmlElement, "Creation", - ref queueDBConfig.Creation, compulsory); - if (Configuration.ConfigUint(xmlElement, "Length", - ref uintValue, compulsory)) - queueDBConfig.Length = uintValue; - if (Configuration.ConfigInt(xmlElement, "PadByte", - ref intValue, compulsory)) - queueDBConfig.PadByte = intValue; - if (Configuration.ConfigUint(xmlElement, "ExtentSize", - ref uintValue, compulsory)) - queueDBConfig.ExtentSize = uintValue; - } - } -} diff --git a/test/csharp/QueueDatabaseTest.cs b/test/csharp/QueueDatabaseTest.cs deleted file mode 100644 index 1291b2da3..000000000 --- a/test/csharp/QueueDatabaseTest.cs +++ /dev/null @@ -1,771 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class QueueDatabaseTest : DatabaseTest - { - - [TestFixtureSetUp] - public void SetUpTestFixture() - { - testFixtureName = "QueueDatabaseTest"; - base.SetUpTestfixture(); - } - - [Test] - public void TestAppendWithoutTxn() - { - testName = "TestAppendWithoutTxn"; - SetUpTest(true); - string queueDBFileName = testHome + "/" + testName + ".db"; - - - QueueDatabaseConfig queueConfig = new QueueDatabaseConfig(); - queueConfig.Creation = CreatePolicy.ALWAYS; - queueConfig.Length = 1000; - QueueDatabase queueDB = QueueDatabase.Open( - queueDBFileName, queueConfig); - - byte[] byteArr = new byte[4]; - byteArr = BitConverter.GetBytes((int)1); - DatabaseEntry data = new DatabaseEntry(byteArr); - uint recno = queueDB.Append(data); - - // Confirm that the recno is larger than 0. - Assert.AreNotEqual(0, recno); - - // Confirm that the record exists in the database. - byteArr = BitConverter.GetBytes(recno); - DatabaseEntry key = new DatabaseEntry(); - key.Data = byteArr; - Assert.IsTrue(queueDB.Exists(key)); - queueDB.Close(); - } - - [Test] - public void TestAppendWithTxn() - { - testName = "TestAppendWithTxn"; - SetUpTest(true); - string queueDBFileName = testHome + "/" + testName + ".db"; - string queueDBName = - Path.GetFileNameWithoutExtension(queueDBFileName); - - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseTxns = true; - envConfig.UseMPool = true; - - DatabaseEnvironment env = DatabaseEnvironment.Open( - testHome, envConfig); - Transaction txn = env.BeginTransaction(); - - QueueDatabaseConfig queueConfig = new QueueDatabaseConfig(); - queueConfig.Creation = CreatePolicy.ALWAYS; - queueConfig.Env = env; - queueConfig.Length = 1000; - - /* If environmnet home is set, the file name in - * Open() is the relative path. - */ - QueueDatabase queueDB = QueueDatabase.Open( - queueDBName, queueConfig, txn); - DatabaseEntry data; - int i = 1000; - try - { - while (i > 0) - { - data = new DatabaseEntry( - BitConverter.GetBytes(i)); - queueDB.Append(data, txn); - i--; - } - txn.Commit(); - } - catch - { - txn.Abort(); - } - finally - { - queueDB.Close(); - env.Close(); - } - - } - - [Test, ExpectedException(typeof(ExpectedTestException))] - public void TestConsumeWithTxn() - { - testName = "TestConsumeWithTxn"; - SetUpTest(true); - string queueDBFileName = testHome + "/" + testName + ".db"; - string queueDBName = Path.GetFileName(queueDBFileName); - - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseTxns = true; - envConfig.UseMPool = true; - - DatabaseEnvironment env = DatabaseEnvironment.Open( - testHome, envConfig); - Transaction txn = env.BeginTransaction(); - - QueueDatabaseConfig queueConfig = - new QueueDatabaseConfig(); - queueConfig.Creation = CreatePolicy.ALWAYS; - queueConfig.Env = env; - queueConfig.Length = 1000; - QueueDatabase queueDB = QueueDatabase.Open( - queueDBName, queueConfig, txn); - - int i = 1; - DatabaseEntry data; - DatabaseEntry getData = new DatabaseEntry(); - while (i <= 10) - { - data = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes(i.ToString())); - queueDB.Append(data, txn); - if (i == 5) - { - getData = data; - } - i++; - } - - KeyValuePair pair = queueDB.Consume(false, txn); - - queueDB.Close(); - txn.Commit(); - env.Close(); - - Database db = Database.Open(queueDBFileName, - new QueueDatabaseConfig()); - try - { - DatabaseEntry key = - new DatabaseEntry(BitConverter.GetBytes(pair.Key)); - db.Get(key); - } - catch (NotFoundException) - { - throw new ExpectedTestException(); - } - finally - { - db.Close(); - } - } - - [Test, ExpectedException(typeof(ExpectedTestException))] - public void TestConsumeWithoutTxn() - { - testName = "TestConsumeWithoutTxn"; - SetUpTest(true); - string queueDBFileName = testHome + "/" + - testName + ".db"; - - QueueDatabaseConfig queueConfig = - new QueueDatabaseConfig(); - queueConfig.Creation = CreatePolicy.ALWAYS; - queueConfig.ErrorPrefix = testName; - queueConfig.Length = 1000; - - QueueDatabase queueDB = QueueDatabase.Open( - queueDBFileName, queueConfig); - DatabaseEntry data = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("data")); - queueDB.Append(data); - - DatabaseEntry consumeData = new DatabaseEntry(); - KeyValuePair pair = queueDB.Consume(false); - try - { - DatabaseEntry key = - new DatabaseEntry(BitConverter.GetBytes(pair.Key)); - queueDB.Get(key); - } - catch (NotFoundException) - { - throw new ExpectedTestException(); - } - finally - { - queueDB.Close(); - } - } - - public void TestCursor() - { - testName = "TestCursor"; - SetUpTest(true); - - GetCursur(testHome + "/" + testName + ".db", false); - } - - public void TestCursorWithConfig() - { - testName = "TestCursorWithConfig"; - SetUpTest(true); - - GetCursur(testHome + "/" + testName + ".db", true); - } - - public void GetCursur(string dbFileName, bool ifConfig) - { - QueueDatabaseConfig dbConfig = new QueueDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Length = 100; - QueueDatabase db = QueueDatabase.Open(dbFileName, dbConfig); - Cursor cursor; - CursorConfig cursorConfig = new CursorConfig(); - cursorConfig.Priority = CachePriority.HIGH; - if (ifConfig == false) - cursor = db.Cursor(); - else - cursor = db.Cursor(cursorConfig); - cursor.Add(new KeyValuePair( - new DatabaseEntry(BitConverter.GetBytes((int)1)), - new DatabaseEntry(BitConverter.GetBytes((int)1)))); - Cursor dupCursor = cursor.Duplicate(false); - Assert.IsNull(dupCursor.Current.Key); - Assert.AreEqual(CachePriority.HIGH, dupCursor.Priority); - dupCursor.Close(); - cursor.Close(); - db.Close(); - } - - [Test] - public void TestDeleteMultiple() - { - testName = "TestDeleteMultiple"; - SetUpTest(true); - - QueueDatabaseConfig dbConfig = new QueueDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.ExtentSize = 1024; - dbConfig.Length = 270; - QueueDatabase db = QueueDatabase.Open(testHome + "/" + - testName + ".db", dbConfig); - - List rList = new List(); - List kList = new List(); - List vList = new List(); - DatabaseEntry key, data; - - for (uint i = 1; i <= 100; i++) { - key = new DatabaseEntry(BitConverter.GetBytes(i)); - data = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes( - "data" + i.ToString() + - Configuration.RandomString(256))); - rList.Add(i); - kList.Add(key); - vList.Add(data); - db.Put(key, data); - } - - // Bulk delete all records with recno in rList. - db.Delete(new MultipleDatabaseEntry(rList)); - Cursor cursor = db.Cursor(); - Assert.IsFalse(cursor.MoveFirst()); - - /* - * Bulk insert records whose key bulk buffer is - * constructed by recno lists, then delete all. - */ - db.Put(new MultipleDatabaseEntry(rList), - new MultipleDatabaseEntry(vList, false)); - Assert.IsTrue(cursor.MoveFirst()); - db.Delete(new MultipleDatabaseEntry(kList, true)); - Assert.IsFalse(cursor.MoveFirst()); - - /* - * Bulk insert records whose key bulk buffer is - * constructed by DatabaseEntry lists, then delete all. - */ - db.Put(new MultipleDatabaseEntry(kList, true), - new MultipleDatabaseEntry(vList, false)); - Assert.IsTrue(cursor.MoveFirst()); - db.Delete(new MultipleDatabaseEntry(kList, true)); - Assert.IsFalse(cursor.MoveFirst()); - - cursor.Close(); - db.Close(); - } - - [Test] - public void TestDeleteMultipleKey() - { - testName = "TestDeleteMultipleKey"; - SetUpTest(true); - - QueueDatabaseConfig dbConfig = new QueueDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.ExtentSize = 1024; - dbConfig.Length = 520; - QueueDatabase db = QueueDatabase.Open(testHome + "/" + - testName + ".db", dbConfig); - - List> pList = - new List>(); - DatabaseEntry key, data; - - for (uint i = 1; i <= 100; i++) - { - key = new DatabaseEntry( - BitConverter.GetBytes(i)); - data = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes( - "data" + i.ToString() + - Configuration.RandomString(512))); - pList.Add(new KeyValuePair< - DatabaseEntry, DatabaseEntry>(key, data)); - db.Put(key, data); - } - - // Create key/value pair bulk buffer and delete all. - db.Delete(new MultipleKeyDatabaseEntry(pList, true)); - // Verify that the database is empty. - Assert.AreEqual(0, db.Truncate()); - db.Close(); - } - - [Test, ExpectedException(typeof(ExpectedTestException))] - public void TestKeyEmptyException() - { - testName = "TestKeyEmptyException"; - SetUpTest(true); - - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseLocking = true; - envConfig.UseLogging = true; - envConfig.UseMPool = true; - envConfig.UseTxns = true; - DatabaseEnvironment env = DatabaseEnvironment.Open( - testHome, envConfig); - - QueueDatabase db; - try - { - Transaction openTxn = env.BeginTransaction(); - try - { - QueueDatabaseConfig queueConfig = - new QueueDatabaseConfig(); - queueConfig.Creation = CreatePolicy.IF_NEEDED; - queueConfig.Length = 10; - queueConfig.Env = env; - db = QueueDatabase.Open(testName + ".db", - queueConfig, openTxn); - openTxn.Commit(); - } - catch (DatabaseException e) - { - openTxn.Abort(); - throw e; - } - - Transaction cursorTxn = env.BeginTransaction(); - Cursor cursor; - try - { - /* - * Put a record into queue database with - * cursor and abort the operation. - */ - cursor = db.Cursor(cursorTxn); - KeyValuePair pair; - pair = new KeyValuePair( - new DatabaseEntry(BitConverter.GetBytes((int)10)), - new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data"))); - cursor.Add(pair); - cursor.Close(); - cursorTxn.Abort(); - } - catch (DatabaseException e) - { - cursorTxn.Abort(); - db.Close(); - throw e; - } - - Transaction delTxn = env.BeginTransaction(); - try - { - /* - * The put operation is aborted in the queue - * database so querying if the record still exists - * throws KeyEmptyException. - */ - db.Exists(new DatabaseEntry( - BitConverter.GetBytes((int)10)), delTxn); - delTxn.Commit(); - } - catch (DatabaseException e) - { - delTxn.Abort(); - throw e; - } - finally - { - db.Close(); - } - } - catch (KeyEmptyException) - { - throw new ExpectedTestException(); - } - finally - { - env.Close(); - } - } - - [Test] - public void TestOpenExistingQueueDB() - { - testName = "TestOpenExistingQueueDB"; - SetUpTest(true); - string queueDBFileName = testHome + "/" + testName + ".db"; - - QueueDatabaseConfig queueConfig = new QueueDatabaseConfig(); - queueConfig.Creation = CreatePolicy.ALWAYS; - QueueDatabase queueDB = QueueDatabase.Open( - queueDBFileName, queueConfig); - queueDB.Close(); - - DatabaseConfig dbConfig = new DatabaseConfig(); - Database db = Database.Open(queueDBFileName, dbConfig); - Assert.AreEqual(db.Type, DatabaseType.QUEUE); - db.Close(); - } - - [Test] - public void TestOpenNewQueueDB() - { - testName = "TestOpenNewQueueDB"; - SetUpTest(true); - string queueDBFileName = testHome + "/" + testName + ".db"; - - // Configure all fields/properties in queue database. - XmlElement xmlElem = Configuration.TestSetUp( - testFixtureName, testName); - QueueDatabaseConfig queueConfig = new QueueDatabaseConfig(); - QueueDatabaseConfigTest.Config(xmlElem, ref queueConfig, true); - queueConfig.Feedback = new DatabaseFeedbackDelegate(DbFeedback); - - // Open the queue database with above configuration. - QueueDatabase queueDB = QueueDatabase.Open( - queueDBFileName, queueConfig); - - // Check the fields/properties in opened queue database. - Confirm(xmlElem, queueDB, true); - - queueDB.Close(); - } - - private void DbFeedback(DatabaseFeedbackEvent opcode, int percent) - { - if (opcode == DatabaseFeedbackEvent.UPGRADE) - Console.WriteLine("Update for %d%", percent); - - if (opcode == DatabaseFeedbackEvent.VERIFY) - Console.WriteLine("Vertify for %d", percent); - } - - [Test, ExpectedException(typeof(NotFoundException))] - public void TestPutToQueue() - { - KeyValuePair pair; - - testName = "TestPutQueue"; - SetUpTest(true); - string queueDBFileName = testHome + "/" + - testName + ".db"; - - QueueDatabaseConfig queueConfig = - new QueueDatabaseConfig(); - queueConfig.Length = 512; - queueConfig.Creation = CreatePolicy.ALWAYS; - using (QueueDatabase queueDB = QueueDatabase.Open( - queueDBFileName, queueConfig)) - { - DatabaseEntry key = new DatabaseEntry(); - key.Data = BitConverter.GetBytes((int)100); - DatabaseEntry data = new DatabaseEntry( - BitConverter.GetBytes((int)1)); - queueDB.Put(key, data); - pair = queueDB.GetBoth(key, data); - } - } - - [Test] - public void TestPutMultiple() - { - testName = "TestPutMultiple"; - SetUpTest(true); - - QueueDatabaseConfig dbConfig = new QueueDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.ExtentSize = 1024; - dbConfig.Length = 520; - dbConfig.PadByte = 0; - QueueDatabase db = QueueDatabase.Open(testHome + "/" + - testName + ".db", dbConfig); - - List kList = new List(); - List vList = new List(); - DatabaseEntry key, data; - for (uint i = 1; i <= 9;i++) { - key = new DatabaseEntry(BitConverter.GetBytes(i)); - data = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("data" + i + - Configuration.RandomString(512))); - kList.Add(i); - vList.Add(data); - } - - // Create bulk buffer for recno based keys. - MultipleDatabaseEntry kBuff = - new MultipleDatabaseEntry(kList); - Assert.IsTrue(kBuff.Recno); - int val = 0; - foreach (DatabaseEntry dbt in kBuff) { - Assert.AreEqual( - BitConverter.GetBytes(kList[val]), - dbt.Data); - val++; - } - Assert.AreEqual(9, val); - - // Create bulk buffer for data. - MultipleDatabaseEntry vBuff = - new MultipleDatabaseEntry(vList, false); - - /* - * Create recno bulk buffer from another recno bulk - * buffer. - */ - MultipleDatabaseEntry kBuff1 = - new MultipleDatabaseEntry(kBuff.Data, kBuff.Recno); - val = 0; - foreach (DatabaseEntry dbt in kBuff1) { - Assert.AreEqual( - BitConverter.GetBytes(kList[val]), - dbt.Data); - val++; - } - Assert.AreEqual(9, val); - - // Bulk insert to database with key and value buffers. - db.Put(kBuff, vBuff); - Cursor cursor = db.Cursor(); - KeyValuePair pair; - val = 0; - while (cursor.MoveNext()) { - pair = cursor.Current; - Assert.AreEqual( - BitConverter.GetBytes(kList[val]), - pair.Key.Data); - for (int i = 0; i < 520; i++) { - if (i < vList[val].Data.Length) - Assert.AreEqual(vList[val].Data[i], - pair.Value.Data[i]); - else - // The pad byte is 0. - Assert.AreEqual(0, pair.Value.Data[i]); - } - Assert.IsFalse(cursor.MoveNextDuplicate()); - val++; - } - Assert.AreEqual(9, val); - - cursor.Close(); - db.Close(); - } - - [Test] - public void TestStats() - { - testName = "TestStats"; - SetUpTest(true); - string dbFileName = testHome + "/" + - testName + ".db"; - - QueueDatabaseConfig dbConfig = - new QueueDatabaseConfig(); - ConfigCase1(dbConfig); - QueueDatabase db = QueueDatabase.Open(dbFileName, dbConfig); - - QueueStats stats = db.Stats(); - ConfirmStatsPart1Case1(stats); - db.PrintFastStats(true); - - // Put 500 records into the database. - PutRecordCase1(db, null); - - stats = db.Stats(); - ConfirmStatsPart2Case1(stats); - db.PrintFastStats(); - - db.Close(); - } - - [Test] - public void TestStatsInTxn() - { - testName = "TestStatsInTxn"; - SetUpTest(true); - - StatsInTxn(testHome, testName, false); - } - - [Test] - public void TestStatsWithIsolation() - { - testName = "TestStatsWithIsolation"; - SetUpTest(true); - - StatsInTxn(testHome, testName, true); - } - - public void StatsInTxn(string home, string name, bool ifIsolation) - { - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - EnvConfigCase1(envConfig); - DatabaseEnvironment env = DatabaseEnvironment.Open( - home, envConfig); - - Transaction openTxn = env.BeginTransaction(); - QueueDatabaseConfig dbConfig = - new QueueDatabaseConfig(); - ConfigCase1(dbConfig); - dbConfig.Env = env; - QueueDatabase db = QueueDatabase.Open(name + ".db", - dbConfig, openTxn); - openTxn.Commit(); - - Transaction statsTxn = env.BeginTransaction(); - QueueStats stats; - if (ifIsolation == false) - stats = db.Stats(statsTxn); - else - stats = db.Stats(statsTxn, Isolation.DEGREE_ONE); - - ConfirmStatsPart1Case1(stats); - db.PrintStats(true); - - // Put 500 records into the database. - PutRecordCase1(db, statsTxn); - - if (ifIsolation == false) - stats = db.Stats(statsTxn); - else - stats = db.Stats(statsTxn, Isolation.DEGREE_TWO); - ConfirmStatsPart2Case1(stats); - db.PrintStats(); - - statsTxn.Commit(); - db.Close(); - env.Close(); - } - - public void EnvConfigCase1(DatabaseEnvironmentConfig cfg) - { - cfg.Create = true; - cfg.UseTxns = true; - cfg.UseMPool = true; - cfg.UseLogging = true; - } - - public void ConfigCase1(QueueDatabaseConfig dbConfig) - { - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.PageSize = 4096; - dbConfig.ExtentSize = 1024; - dbConfig.Length = 4000; - dbConfig.PadByte = 32; - } - - public void PutRecordCase1(QueueDatabase db, Transaction txn) - { - byte[] bigArray = new byte[4000]; - for (int i = 1; i <= 100; i++) - { - if (txn == null) - db.Put(new DatabaseEntry(BitConverter.GetBytes(i)), - new DatabaseEntry(BitConverter.GetBytes(i))); - else - db.Put(new DatabaseEntry(BitConverter.GetBytes(i)), - new DatabaseEntry(BitConverter.GetBytes(i)), txn); - } - DatabaseEntry key = new DatabaseEntry(BitConverter.GetBytes((int)100)); - for (int i = 100; i <= 500; i++) - { - if (txn == null) - db.Put(key, new DatabaseEntry(bigArray)); - else - db.Put(key, new DatabaseEntry(bigArray), txn); - } - } - - public void ConfirmStatsPart1Case1(QueueStats stats) - { - Assert.AreEqual(1, stats.FirstRecordNumber); - Assert.AreNotEqual(0, stats.MagicNumber); - Assert.AreEqual(1, stats.NextRecordNumber); - Assert.AreEqual(4096, stats.PageSize); - Assert.AreEqual(1024, stats.PagesPerExtent); - Assert.AreEqual(4000, stats.RecordLength); - Assert.AreEqual(32, stats.RecordPadByte); - Assert.AreEqual(4, stats.Version); - } - - public void ConfirmStatsPart2Case1(QueueStats stats) - { - Assert.AreNotEqual(0, stats.DataPages); - Assert.AreEqual(0, stats.DataPagesBytesFree); - Assert.AreEqual(0, stats.MetadataFlags); - Assert.AreEqual(100, stats.nData); - Assert.AreEqual(100, stats.nKeys); - } - - public static void Confirm(XmlElement xmlElem, - QueueDatabase queueDB, bool compulsory) - { - DatabaseTest.Confirm(xmlElem, queueDB, compulsory); - - // Confirm queue database specific field/property - Configuration.ConfirmUint(xmlElem, "ExtentSize", - queueDB.ExtentSize, compulsory); - Configuration.ConfirmBool(xmlElem, "ConsumeInOrder", - queueDB.InOrder, compulsory); - Configuration.ConfirmInt(xmlElem, "PadByte", - queueDB.PadByte, compulsory); - Assert.AreEqual(DatabaseType.QUEUE, queueDB.Type); - string type = queueDB.Type.ToString(); - Assert.IsNotNull(type); - } - } -} diff --git a/test/csharp/RecnoCursorTest.cs b/test/csharp/RecnoCursorTest.cs deleted file mode 100644 index 647eecf9a..000000000 --- a/test/csharp/RecnoCursorTest.cs +++ /dev/null @@ -1,241 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Threading; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class RecnoCursorTest : CSharpTestFixture - { - - [TestFixtureSetUp] - public void SetUpTestFixture() - { - testFixtureName = "RecnoCursorTest"; - base.SetUpTestfixture(); - } - - [Test] - public void TestCursor() - { - testName = "TestCursor"; - SetUpTest(true); - - GetCursorWithImplicitTxn(testHome, - testName + ".db", false); - } - - [Test] - public void TestCursorWithConfig() - { - testName = "TestCursorWithConfig"; - SetUpTest(true); - - GetCursorWithImplicitTxn(testHome, - testName + ".db", true); - } - - public void GetCursorWithImplicitTxn(string home, - string dbFile, bool ifConfig) - { - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseCDB = true; - envConfig.UseMPool = true; - DatabaseEnvironment env = DatabaseEnvironment.Open( - home, envConfig); - - RecnoDatabaseConfig dbConfig = - new RecnoDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Env = env; - RecnoDatabase db = RecnoDatabase.Open(dbFile, - dbConfig); - - RecnoCursor cursor; - if (ifConfig == false) - cursor = db.Cursor(); - else - cursor = db.Cursor(new CursorConfig()); - - cursor.Close(); - db.Close(); - env.Close(); - } - - [Test] - public void TestCursorInTxn() - { - testName = "TestCursorInTxn"; - SetUpTest(true); - - GetCursorWithExplicitTxn(testHome, - testName + ".db", false); - } - - [Test] - public void TestConfigedCursorInTxn() - { - testName = "TestConfigedCursorInTxn"; - SetUpTest(true); - - GetCursorWithExplicitTxn(testHome, - testName + ".db", true); - } - - public void GetCursorWithExplicitTxn(string home, - string dbFile, bool ifConfig) - { - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseTxns = true; - envConfig.UseMPool = true; - DatabaseEnvironment env = DatabaseEnvironment.Open( - home, envConfig); - - Transaction openTxn = env.BeginTransaction(); - RecnoDatabaseConfig dbConfig = - new RecnoDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Env = env; - RecnoDatabase db = RecnoDatabase.Open(dbFile, - dbConfig, openTxn); - openTxn.Commit(); - - Transaction cursorTxn = env.BeginTransaction(); - RecnoCursor cursor; - if (ifConfig == false) - cursor = db.Cursor(cursorTxn); - else - cursor = db.Cursor(new CursorConfig(), cursorTxn); - cursor.Close(); - cursorTxn.Commit(); - db.Close(); - env.Close(); - } - - [Test] - public void TestDuplicate() - { - KeyValuePair pair; - RecnoDatabase db; - RecnoDatabaseConfig dbConfig; - RecnoCursor cursor, dupCursor; - string dbFileName; - - testName = "TestDuplicate"; - SetUpTest(true); - dbFileName = testHome + "/" + testName + ".db"; - - dbConfig = new RecnoDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - db = RecnoDatabase.Open(dbFileName, dbConfig); - cursor = db.Cursor(); - - /* - * Add a record(1, 1) by cursor and move - * the cursor to the current record. - */ - AddOneByCursor(cursor); - cursor.Refresh(); - - //Duplicate a new cursor to the same position. - dupCursor = cursor.Duplicate(true); - - // Overwrite the record. - dupCursor.Overwrite(new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("newdata"))); - - // Confirm that the original data doesn't exist. - pair = new KeyValuePair( - new DatabaseEntry( - BitConverter.GetBytes((int)1)), - new DatabaseEntry( - BitConverter.GetBytes((int)1))); - Assert.IsFalse(dupCursor.Move(pair, true)); - - dupCursor.Close(); - cursor.Close(); - db.Close(); - } - - [Test] - public void TestInsertToLoc() - { - RecnoDatabase db; - RecnoDatabaseConfig dbConfig; - RecnoCursor cursor; - DatabaseEntry data; - KeyValuePair pair; - string dbFileName; - - testName = "TestInsertToLoc"; - SetUpTest(true); - dbFileName = testHome + "/" + testName + ".db"; - - // Open database and cursor. - dbConfig = new RecnoDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Renumber = true; - db = RecnoDatabase.Open(dbFileName, dbConfig); - cursor = db.Cursor(); - - // Add record(1,1) into database. - /* - * Add a record(1, 1) by cursor and move - * the cursor to the current record. - */ - AddOneByCursor(cursor); - cursor.Refresh(); - - /* - * Insert the new record(1,10) after the - * record(1,1). - */ - data = new DatabaseEntry( - BitConverter.GetBytes((int)10)); - cursor.Insert(data, Cursor.InsertLocation.AFTER); - - /* - * Move the cursor to the record(1,1) and - * confirm that the next record is the one just inserted. - */ - pair = new KeyValuePair( - new DatabaseEntry( - BitConverter.GetBytes((int)1)), - new DatabaseEntry( - BitConverter.GetBytes((int)1))); - Assert.IsTrue(cursor.Move(pair, true)); - Assert.IsTrue(cursor.MoveNext()); - Assert.AreEqual(BitConverter.GetBytes((int)10), - cursor.Current.Value.Data); - - cursor.Close(); - db.Close(); - } - - public void AddOneByCursor(RecnoCursor cursor) - { - KeyValuePair pair = - new KeyValuePair( - new DatabaseEntry( - BitConverter.GetBytes((int)1)), - new DatabaseEntry( - BitConverter.GetBytes((int)1))); - cursor.Add(pair); - } - } -} diff --git a/test/csharp/RecnoDatabaseConfigTest.cs b/test/csharp/RecnoDatabaseConfigTest.cs deleted file mode 100644 index 2c6a2cd97..000000000 --- a/test/csharp/RecnoDatabaseConfigTest.cs +++ /dev/null @@ -1,124 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class RecnoDatabaseConfigTest : DatabaseConfigTest - { - - [TestFixtureSetUp] - public override void SetUpTestFixture() - { - testFixtureName = "RecnoDatabaseConfigTest"; - base.SetUpTestfixture(); - } - - [Test] - override public void TestConfigWithoutEnv() - { - testName = "TestConfigWithoutEnv"; - SetUpTest(false); - XmlElement xmlElem = Configuration.TestSetUp( - testFixtureName, testName); - - RecnoDatabaseConfig recnoDBConfig = - new RecnoDatabaseConfig(); - Config(xmlElem, ref recnoDBConfig, true); - Confirm(xmlElem, recnoDBConfig, true); - } - - [Test] - public void TestAppend() - { - uint recno; - - testName = "TestAppend"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - - RecnoDatabaseConfig recnoConfig = - new RecnoDatabaseConfig(); - recnoConfig.Creation = CreatePolicy.IF_NEEDED; - recnoConfig.Append = new AppendRecordDelegate( - AppendRecord); - RecnoDatabase recnoDB = RecnoDatabase.Open( - dbFileName, recnoConfig); - recno = recnoDB.Append(new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("data"))); - - KeyValuePair pair; - pair = recnoDB.Get( - new DatabaseEntry(BitConverter.GetBytes(recno))); - Assert.AreEqual(ASCIIEncoding.ASCII.GetBytes("data"), - pair.Value.Data); - - recnoDB.Close(); - } - - public void AppendRecord(DatabaseEntry data, uint recno) - { - data.Data = BitConverter.GetBytes(recno); - } - - public static void Confirm(XmlElement xmlElement, - RecnoDatabaseConfig recnoDBConfig, bool compulsory) - { - DatabaseConfig dbConfig = recnoDBConfig; - Confirm(xmlElement, dbConfig, compulsory); - - // Confirm Recno database specific configuration - Configuration.ConfirmString(xmlElement, "BackingFile", - recnoDBConfig.BackingFile, compulsory); - Configuration.ConfirmCreatePolicy(xmlElement, - "Creation", recnoDBConfig.Creation, compulsory); - Configuration.ConfirmInt(xmlElement, "Delimiter", - recnoDBConfig.Delimiter, compulsory); - Configuration.ConfirmUint(xmlElement, "Length", - recnoDBConfig.Length, compulsory); - Configuration.ConfirmInt(xmlElement, "PadByte", - recnoDBConfig.PadByte, compulsory); - Configuration.ConfirmBool(xmlElement, "Renumber", - recnoDBConfig.Renumber, compulsory); - Configuration.ConfirmBool(xmlElement, "Snapshot", - recnoDBConfig.Snapshot, compulsory); - } - - public static void Config(XmlElement xmlElement, - ref RecnoDatabaseConfig recnoDBConfig, bool compulsory) - { - int intValue = new int(); - uint uintValue = new uint(); - DatabaseConfig dbConfig = recnoDBConfig; - Config(xmlElement, ref dbConfig, compulsory); - - // Configure specific fields/properties of Recno database - Configuration.ConfigCreatePolicy(xmlElement, "Creation", - ref recnoDBConfig.Creation, compulsory); - if (Configuration.ConfigInt(xmlElement, "Delimiter", - ref intValue, compulsory)) - recnoDBConfig.Delimiter = intValue; - if (Configuration.ConfigUint(xmlElement, "Length", - ref uintValue, compulsory)) - recnoDBConfig.Length = uintValue; - if (Configuration.ConfigInt(xmlElement, "PadByte", - ref intValue, compulsory)) - recnoDBConfig.PadByte = intValue; - Configuration.ConfigBool(xmlElement, "Renumber", - ref recnoDBConfig.Renumber, compulsory); - Configuration.ConfigBool(xmlElement, "Snapshot", - ref recnoDBConfig.Snapshot, compulsory); - } - } -} diff --git a/test/csharp/RecnoDatabaseTest.cs b/test/csharp/RecnoDatabaseTest.cs deleted file mode 100644 index df0070e9d..000000000 --- a/test/csharp/RecnoDatabaseTest.cs +++ /dev/null @@ -1,466 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class RecnoDatabaseTest : DatabaseTest - { - - [TestFixtureSetUp] - public void SetUpTestFixture() - { - testFixtureName = "RecnoDatabaseTest"; - base.SetUpTestfixture(); - } - - [Test] - public void TestOpenExistingRecnoDB() - { - testName = "TestOpenExistingRecnoDB"; - SetUpTest(true); - string recnoDBFileName = testHome + "/" + - testName + ".db"; - - RecnoDatabaseConfig recConfig = - new RecnoDatabaseConfig(); - recConfig.Creation = CreatePolicy.ALWAYS; - RecnoDatabase recDB = RecnoDatabase.Open( - recnoDBFileName, recConfig); - recDB.Close(); - - RecnoDatabaseConfig dbConfig = new RecnoDatabaseConfig(); - string backingFile = testHome + "/backingFile"; - File.Copy(recnoDBFileName, backingFile); - dbConfig.BackingFile = backingFile; - RecnoDatabase db = RecnoDatabase.Open(recnoDBFileName, dbConfig); - Assert.AreEqual(db.Type, DatabaseType.RECNO); - db.Close(); - } - - [Test] - public void TestOpenNewRecnoDB() - { - RecnoDatabase recnoDB; - RecnoDatabaseConfig recnoConfig; - - testName = "TestOpenNewRecnoDB"; - SetUpTest(true); - string recnoDBFileName = testHome + "/" + - testName + ".db"; - - XmlElement xmlElem = Configuration.TestSetUp( - testFixtureName, testName); - recnoConfig = new RecnoDatabaseConfig(); - RecnoDatabaseConfigTest.Config(xmlElem, - ref recnoConfig, true); - recnoDB = RecnoDatabase.Open(recnoDBFileName, - recnoConfig); - Confirm(xmlElem, recnoDB, true); - recnoDB.Close(); - } - - [Test] - public void TestAppendWithoutTxn() - { - testName = "TestAppendWithoutTxn"; - SetUpTest(true); - string recnoDBFileName = testHome + "/" + - testName + ".db"; - - RecnoDatabaseConfig recnoConfig = - new RecnoDatabaseConfig(); - recnoConfig.Creation = CreatePolicy.ALWAYS; - RecnoDatabase recnoDB = RecnoDatabase.Open( - recnoDBFileName, recnoConfig); - - DatabaseEntry data = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("data")); - uint num = recnoDB.Append(data); - DatabaseEntry key = new DatabaseEntry( - BitConverter.GetBytes(num)); - Assert.IsTrue(recnoDB.Exists(key)); - KeyValuePair record = - recnoDB.Get(key); - Assert.IsTrue(data.Data.Length == - record.Value.Data.Length); - for (int i = 0; i < data.Data.Length; i++) - Assert.IsTrue(data.Data[i] == - record.Value.Data[i]); - recnoDB.Close(); - } - - [Test] - public void TestCompact() - { - testName = "TestCompact"; - SetUpTest(true); - string recnoDBFileName = testHome + "/" + - testName + ".db"; - - RecnoDatabaseConfig recnoConfig = - new RecnoDatabaseConfig(); - recnoConfig.Creation = CreatePolicy.ALWAYS; - recnoConfig.Length = 512; - - DatabaseEntry key, data; - RecnoDatabase recnoDB; - using (recnoDB = RecnoDatabase.Open( - recnoDBFileName, recnoConfig)) - { - for (int i = 1; i <= 5000; i++) - { - data = new DatabaseEntry( - BitConverter.GetBytes(i)); - recnoDB.Append(data); - } - - for (int i = 1; i <= 5000; i++) - { - if (i > 500 && (i % 5 != 0)) - { - key = new DatabaseEntry( - BitConverter.GetBytes(i)); - recnoDB.Delete(key); - } - } - - int startInt = 1; - int stopInt = 2500; - DatabaseEntry start, stop; - - start = new DatabaseEntry( - BitConverter.GetBytes(startInt)); - stop = new DatabaseEntry( - BitConverter.GetBytes(stopInt)); - Assert.IsTrue(recnoDB.Exists(start)); - Assert.IsTrue(recnoDB.Exists(stop)); - - CompactConfig cCfg = new CompactConfig(); - cCfg.start = start; - cCfg.stop = stop; - cCfg.FillPercentage = 30; - cCfg.Pages = 1; - cCfg.returnEnd = true; - cCfg.Timeout = 5000; - cCfg.TruncatePages = true; - CompactData compactData = recnoDB.Compact(cCfg); - - Assert.IsNotNull(compactData.End); - Assert.AreNotEqual(0, compactData.PagesExamined); - } - } - - [Test] - public void TestStats() - { - testName = "TestStats"; - SetUpTest(true); - string dbFileName = testHome + "/" + - testName + ".db"; - - RecnoDatabaseConfig dbConfig = - new RecnoDatabaseConfig(); - ConfigCase1(dbConfig); - RecnoDatabase db = RecnoDatabase.Open(dbFileName, - dbConfig); - RecnoStats stats = db.Stats(); - ConfirmStatsPart1Case1(stats); - - // Put 1000 records into the database. - PutRecordCase1(db, null); - stats = db.Stats(); - ConfirmStatsPart2Case1(stats); - - // Delete 500 records. - for (int i = 250; i <= 750; i++) - db.Delete(new DatabaseEntry(BitConverter.GetBytes(i))); - stats = db.Stats(); - ConfirmStatsPart3Case1(stats); - - db.Close(); - } - - [Test] - public void TestStatsInTxn() - { - testName = "TestStatsInTxn"; - SetUpTest(true); - - StatsInTxn(testHome, testName, false); - } - - [Test] - public void TestStatsWithIsolation() - { - testName = "TestStatsWithIsolation"; - SetUpTest(true); - - StatsInTxn(testHome, testName, true); - } - - public void StatsInTxn(string home, string name, bool ifIsolation) - { - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - EnvConfigCase1(envConfig); - DatabaseEnvironment env = DatabaseEnvironment.Open( - home, envConfig); - - Transaction openTxn = env.BeginTransaction(); - RecnoDatabaseConfig dbConfig = - new RecnoDatabaseConfig(); - ConfigCase1(dbConfig); - dbConfig.Env = env; - RecnoDatabase db = RecnoDatabase.Open(name + ".db", - dbConfig, openTxn); - openTxn.Commit(); - - Transaction statsTxn = env.BeginTransaction(); - RecnoStats stats; - RecnoStats fastStats; - if (ifIsolation == false) - { - stats = db.Stats(statsTxn); - fastStats = db.FastStats(statsTxn); - } - else - { - stats = db.Stats(statsTxn, Isolation.DEGREE_ONE); - fastStats = db.FastStats(statsTxn, - Isolation.DEGREE_ONE); - } - ConfirmStatsPart1Case1(stats); - - // Put 1000 records into the database. - PutRecordCase1(db, statsTxn); - - if (ifIsolation == false) - { - stats = db.Stats(statsTxn); - fastStats = db.FastStats(statsTxn); - } - else - { - stats = db.Stats(statsTxn, Isolation.DEGREE_TWO); - fastStats = db.FastStats(statsTxn, - Isolation.DEGREE_TWO); - } - ConfirmStatsPart2Case1(stats); - - // Delete 500 records. - for (int i = 250; i <= 750; i++) - db.Delete(new DatabaseEntry(BitConverter.GetBytes(i)), - statsTxn); - - if (ifIsolation == false) - { - stats = db.Stats(statsTxn); - fastStats = db.FastStats(statsTxn); - } - else - { - stats = db.Stats(statsTxn, Isolation.DEGREE_THREE); - fastStats = db.FastStats(statsTxn, - Isolation.DEGREE_THREE); - } - ConfirmStatsPart3Case1(stats); - - statsTxn.Commit(); - db.Close(); - env.Close(); - } - - public void EnvConfigCase1(DatabaseEnvironmentConfig cfg) - { - cfg.Create = true; - cfg.UseTxns = true; - cfg.UseMPool = true; - cfg.UseLogging = true; - } - - public void ConfigCase1(RecnoDatabaseConfig dbConfig) - { - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.PageSize = 4096; - dbConfig.Length = 4000; - dbConfig.PadByte = 256; - } - - public void PutRecordCase1(RecnoDatabase db, Transaction txn) - { - for (int i = 1; i <= 1000; i++) - { - if (txn == null) - db.Put(new DatabaseEntry( - BitConverter.GetBytes(i)), - new DatabaseEntry(BitConverter.GetBytes(i))); - else - db.Put(new DatabaseEntry( - BitConverter.GetBytes(i)), - new DatabaseEntry( - BitConverter.GetBytes(i)), txn); - } - } - - public void ConfirmStatsPart1Case1(RecnoStats stats) - { - Assert.AreEqual(1, stats.EmptyPages); - Assert.AreEqual(1, stats.Levels); - Assert.AreNotEqual(0, stats.MagicNumber); - Assert.AreEqual(10, stats.MetadataFlags); - Assert.AreEqual(2, stats.MinKey); - Assert.AreEqual(2, stats.nPages); - Assert.AreEqual(4096, stats.PageSize); - Assert.AreEqual(4000, stats.RecordLength); - Assert.AreEqual(256, stats.RecordPadByte); - Assert.AreEqual(9, stats.Version); - } - - public void ConfirmStatsPart2Case1(RecnoStats stats) - { - Assert.AreEqual(0, stats.DuplicatePages); - Assert.AreEqual(0, stats.DuplicatePagesFreeBytes); - Assert.AreNotEqual(0, stats.InternalPages); - Assert.AreNotEqual(0, stats.InternalPagesFreeBytes); - Assert.AreNotEqual(0, stats.LeafPages); - Assert.AreNotEqual(0, stats.LeafPagesFreeBytes); - Assert.AreEqual(1000, stats.nData); - Assert.AreEqual(1000, stats.nKeys); - Assert.AreNotEqual(0, stats.OverflowPages); - Assert.AreNotEqual(0, stats.OverflowPagesFreeBytes); - } - - public void ConfirmStatsPart3Case1(RecnoStats stats) - { - Assert.AreNotEqual(0, stats.FreePages); - } - - [Test] - public void TestTruncateUnusedPages() - { - testName = "TestTruncateUnusedPages"; - SetUpTest(true); - - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseCDB = true; - envConfig.UseMPool = true; - DatabaseEnvironment env = DatabaseEnvironment.Open( - testHome, envConfig); - - RecnoDatabaseConfig dbConfig = - new RecnoDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Env = env; - dbConfig.PageSize = 512; - RecnoDatabase db = RecnoDatabase.Open( - testName + ".db", dbConfig); - - ModifyRecordsInDB(db, null); - Assert.Less(0, db.TruncateUnusedPages()); - - db.Close(); - env.Close(); - } - - [Test] - public void TestTruncateUnusedPagesInTxn() - { - testName = "TestTruncateUnusedPagesInTxn"; - SetUpTest(true); - - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseLogging = true; - envConfig.UseMPool = true; - envConfig.UseTxns = true; - DatabaseEnvironment env = DatabaseEnvironment.Open( - testHome, envConfig); - - Transaction openTxn = env.BeginTransaction(); - RecnoDatabaseConfig dbConfig = - new RecnoDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Env = env; - dbConfig.PageSize = 512; - RecnoDatabase db = RecnoDatabase.Open( - testName + ".db", dbConfig, openTxn); - openTxn.Commit(); - - Transaction modifyTxn = env.BeginTransaction(); - ModifyRecordsInDB(db, modifyTxn); - Assert.Less(0, db.TruncateUnusedPages(modifyTxn)); - modifyTxn.Commit(); - - db.Close(); - env.Close(); - } - - public void ModifyRecordsInDB(RecnoDatabase db, - Transaction txn) - { - uint[] recnos = new uint[100]; - - if (txn == null) - { - // Add a lot of records into database. - for (int i = 0; i < 100; i++) - recnos[i] = db.Append(new DatabaseEntry( - new byte[10240])); - - // Remove some records from database. - for (int i = 30; i < 100; i++) - db.Delete(new DatabaseEntry( - BitConverter.GetBytes(recnos[i]))); - } - else - { - // Add a lot of records into database in txn. - for (int i = 0; i < 100; i++) - recnos[i] = db.Append(new DatabaseEntry( - new byte[10240]), txn); - - // Remove some records from database in txn. - for (int i = 30; i < 100; i++) - db.Delete(new DatabaseEntry( - BitConverter.GetBytes(recnos[i])), txn); - } - } - - public static void Confirm(XmlElement xmlElem, - RecnoDatabase recnoDB, bool compulsory) - { - DatabaseTest.Confirm(xmlElem, recnoDB, compulsory); - - // Confirm recno database specific field/property - Configuration.ConfirmInt(xmlElem, "Delimiter", - recnoDB.RecordDelimiter, compulsory); - Configuration.ConfirmUint(xmlElem, "Length", - recnoDB.RecordLength, compulsory); - Configuration.ConfirmInt(xmlElem, "PadByte", - recnoDB.RecordPad, compulsory); - Configuration.ConfirmBool(xmlElem, "Renumber", - recnoDB.Renumber, compulsory); - Configuration.ConfirmBool(xmlElem, "Snapshot", - recnoDB.Snapshot, compulsory); - Assert.AreEqual(DatabaseType.RECNO, recnoDB.Type); - string type = recnoDB.Type.ToString(); - Assert.IsNotNull(type); - } - } -} - diff --git a/test/csharp/ReplicationConfigTest.cs b/test/csharp/ReplicationConfigTest.cs deleted file mode 100644 index a40b7b6ad..000000000 --- a/test/csharp/ReplicationConfigTest.cs +++ /dev/null @@ -1,188 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class ReplicationConfigTest : CSharpTestFixture - { - - [TestFixtureSetUp] - public void SetUpTestFixture() - { - testFixtureName = "ReplicationConfigTest"; - base.SetUpTestfixture(); - } - - [Test] - public void TestConfig() - { - testName = "TestConfig"; - SetUpTest(false); - - ReplicationConfig repConfig = new ReplicationConfig(); - XmlElement xmlElem = Configuration.TestSetUp( - testFixtureName, testName); - Config(xmlElem, ref repConfig, true); - Confirm(xmlElem, repConfig, true); - - repConfig.Clockskew(102, 100); - Assert.AreEqual(102, repConfig.ClockskewFast); - Assert.AreEqual(100, repConfig.ClockskewSlow); - - repConfig.TransmitLimit(1, 1024); - Assert.AreEqual(1, repConfig.TransmitLimitGBytes); - Assert.AreEqual(1024, repConfig.TransmitLimitBytes); - - repConfig.RetransmissionRequest(10, 100); - Assert.AreEqual(100, repConfig.RetransmissionRequestMax); - Assert.AreEqual(10, repConfig.RetransmissionRequestMin); - } - - [Test] - public void TestRepMgrAckPolicy() - { - testName = "TestRepMgrAckPolicy"; - SetUpTest(false); - - ReplicationConfig repConfig = new ReplicationConfig(); - repConfig.RepMgrAckPolicy = AckPolicy.ALL; - Assert.AreEqual(AckPolicy.ALL, - repConfig.RepMgrAckPolicy); - - repConfig.RepMgrAckPolicy = AckPolicy.ALL_PEERS; - Assert.AreEqual(AckPolicy.ALL_PEERS, - repConfig.RepMgrAckPolicy); - - repConfig.RepMgrAckPolicy = AckPolicy.NONE; - Assert.AreEqual(AckPolicy.NONE, - repConfig.RepMgrAckPolicy); - - repConfig.RepMgrAckPolicy = AckPolicy.ONE; - Assert.AreEqual(AckPolicy.ONE, - repConfig.RepMgrAckPolicy); - - repConfig.RepMgrAckPolicy = AckPolicy.ONE_PEER; - Assert.AreEqual(AckPolicy.ONE_PEER, - repConfig.RepMgrAckPolicy); - - repConfig.RepMgrAckPolicy = AckPolicy.QUORUM; - Assert.AreEqual(AckPolicy.QUORUM, - repConfig.RepMgrAckPolicy); - } - - public static void Confirm(XmlElement xmlElement, - ReplicationConfig cfg, bool compulsory) - { - Configuration.ConfirmUint(xmlElement, - "AckTimeout", cfg.AckTimeout, - compulsory); - Configuration.ConfirmBool(xmlElement, "BulkTransfer", - cfg.BulkTransfer, compulsory); - Configuration.ConfirmUint(xmlElement, "CheckpointDelay", - cfg.CheckpointDelay, compulsory); - Configuration.ConfirmUint(xmlElement, "ConnectionRetry", - cfg.ConnectionRetry, compulsory); - Configuration.ConfirmBool(xmlElement, "DelayClientSync", - cfg.DelayClientSync, compulsory); - Configuration.ConfirmUint(xmlElement, "ElectionRetry", - cfg.ElectionRetry, compulsory); - Configuration.ConfirmUint(xmlElement, "ElectionTimeout", - cfg.ElectionTimeout, compulsory); - Configuration.ConfirmUint(xmlElement, "FullElectionTimeout", - cfg.FullElectionTimeout, compulsory); - Configuration.ConfirmUint(xmlElement, "HeartbeatMonitor", - cfg.HeartbeatMonitor, compulsory); - Configuration.ConfirmUint(xmlElement, "HeartbeatSend", - cfg.HeartbeatSend, compulsory); - Configuration.ConfirmUint(xmlElement, "LeaseTimeout", - cfg.LeaseTimeout, compulsory); - Configuration.ConfirmBool(xmlElement, "AutoInit", - cfg.AutoInit, compulsory); - Configuration.ConfirmBool(xmlElement, "NoBlocking", - cfg.NoBlocking, compulsory); - Configuration.ConfirmUint(xmlElement, "Priority", - cfg.Priority, compulsory); - Configuration.ConfirmAckPolicy(xmlElement, - "RepMgrAckPolicy", cfg.RepMgrAckPolicy, compulsory); - if (cfg.RepmgrSitesConfig.Count > 0) - Configuration.ConfirmReplicationHostAddress( - xmlElement, "RepMgrLocalSite", - cfg.RepmgrSitesConfig[0], compulsory); - Configuration.ConfirmBool(xmlElement, "Strict2Site", - cfg.Strict2Site, compulsory); - Configuration.ConfirmBool(xmlElement, "UseMasterLeases", - cfg.UseMasterLeases, compulsory); - } - - public static void Config(XmlElement xmlElement, - ref ReplicationConfig cfg, bool compulsory) - { - uint uintValue = new uint(); - - if (Configuration.ConfigUint(xmlElement, "AckTimeout", - ref uintValue, compulsory)) - cfg.AckTimeout = uintValue; - Configuration.ConfigBool(xmlElement, "BulkTransfer", - ref cfg.BulkTransfer, compulsory); - if (Configuration.ConfigUint(xmlElement, "CheckpointDelay", - ref uintValue, compulsory)) - cfg.CheckpointDelay = uintValue; - if (Configuration.ConfigUint(xmlElement, "ConnectionRetry", - ref uintValue, compulsory)) - cfg.ConnectionRetry = uintValue; - Configuration.ConfigBool(xmlElement, "DelayClientSync", - ref cfg.DelayClientSync, compulsory); - if (Configuration.ConfigUint(xmlElement, "ElectionRetry", - ref uintValue, compulsory)) - cfg.ElectionRetry = uintValue; - if (Configuration.ConfigUint(xmlElement, "ElectionTimeout", - ref uintValue, compulsory)) - cfg.ElectionTimeout = uintValue; - if (Configuration.ConfigUint(xmlElement, "FullElectionTimeout", - ref uintValue, compulsory)) - cfg.FullElectionTimeout = uintValue; - if (Configuration.ConfigUint(xmlElement, "HeartbeatMonitor", - ref uintValue, compulsory)) - cfg.HeartbeatMonitor = uintValue; - if (Configuration.ConfigUint(xmlElement, "HeartbeatSend", - ref uintValue, compulsory)) - cfg.HeartbeatSend = uintValue; - if (Configuration.ConfigUint(xmlElement, "LeaseTimeout", - ref uintValue, compulsory)) - cfg.LeaseTimeout = uintValue; - Configuration.ConfigBool(xmlElement, "AutoInit", - ref cfg.AutoInit, compulsory); - Configuration.ConfigBool(xmlElement, "NoBlocking", - ref cfg.NoBlocking, compulsory); - if (Configuration.ConfigUint(xmlElement, "Priority", - ref uintValue, compulsory)) - cfg.Priority = uintValue; - Configuration.ConfigAckPolicy(xmlElement, - "RepMgrAckPolicy", ref cfg.RepMgrAckPolicy, - compulsory); - DbSiteConfig siteConfig = new DbSiteConfig(); - siteConfig.LocalSite = true; - Configuration.ConfigReplicationHostAddress(xmlElement, - "RepMgrLocalSite", ref siteConfig, compulsory); - cfg.RepmgrSitesConfig.Add(siteConfig); - Configuration.ConfigBool(xmlElement, "Strict2Site", - ref cfg.Strict2Site, compulsory); - Configuration.ConfigBool(xmlElement, "UseMasterLeases", - ref cfg.UseMasterLeases, compulsory); - } - - } -} diff --git a/test/csharp/ReplicationTest.cs b/test/csharp/ReplicationTest.cs deleted file mode 100644 index fd2c0cdd3..000000000 --- a/test/csharp/ReplicationTest.cs +++ /dev/null @@ -1,996 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; -using System.Net; -using System.Net.NetworkInformation; -using System.Text; -using System.Threading; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class ReplicationTest : CSharpTestFixture - { - private EventWaitHandle clientStartSignal; - private EventWaitHandle masterCloseSignal; - - private EventWaitHandle client1StartSignal; - private EventWaitHandle client2StartSignal; - private EventWaitHandle client3StartSignal; - private EventWaitHandle clientsElectionSignal; - private EventWaitHandle masterLeaveSignal; - - private uint startUpDone; - private bool electionDone; - - List ports = new List(); - - [TestFixtureSetUp] - public void SetUp() - { - testFixtureName = "ReplicationTest"; - base.SetUpTestfixture(); - } - - [Test] - public void TestRepMgrSite() - { - testName = "TestRepMgrSite"; - SetUpTest(true); - - string masterHome = testHome + "\\Master"; - Configuration.ClearDir(masterHome); - - string clientHome = testHome + "\\Client"; - Configuration.ClearDir(clientHome); - - ports.Clear(); - AvailablePorts portGen = new AvailablePorts(); - uint mPort = portGen.Current; - portGen.MoveNext(); - uint cPort = portGen.Current; - - // Open environment with replication configuration. - DatabaseEnvironmentConfig cfg = - new DatabaseEnvironmentConfig(); - cfg.Create = true; - cfg.RunRecovery = true; - cfg.UseLocking = true; - cfg.UseLogging = true; - cfg.UseMPool = true; - cfg.UseReplication = true; - cfg.FreeThreaded = true; - cfg.UseTxns = true; - cfg.EventNotify = new EventNotifyDelegate(stuffHappened); - - cfg.RepSystemCfg = new ReplicationConfig(); - cfg.RepSystemCfg.RepmgrSitesConfig.Add(new DbSiteConfig()); - cfg.RepSystemCfg.RepmgrSitesConfig[0].Host = "127.0.0.1"; - cfg.RepSystemCfg.RepmgrSitesConfig[0].Port = mPort; - cfg.RepSystemCfg.RepmgrSitesConfig[0].LocalSite = true; - cfg.RepSystemCfg.RepmgrSitesConfig[0].GroupCreator = true; - cfg.RepSystemCfg.Priority = 100; - - DatabaseEnvironment mEnv = DatabaseEnvironment.Open( - masterHome, cfg); - mEnv.DeadlockResolution = DeadlockPolicy.DEFAULT; - mEnv.RepMgrStartMaster(2); - - cfg.RepSystemCfg.RepmgrSitesConfig[0].Port = cPort; - cfg.RepSystemCfg.RepmgrSitesConfig[0].GroupCreator = false; - cfg.RepSystemCfg.Priority = 10; - cfg.RepSystemCfg.RepmgrSitesConfig.Add(new DbSiteConfig()); - cfg.RepSystemCfg.RepmgrSitesConfig[1].Host = "127.0.0.1"; - cfg.RepSystemCfg.RepmgrSitesConfig[1].Port = mPort; - cfg.RepSystemCfg.RepmgrSitesConfig[1].Helper = true; - DatabaseEnvironment cEnv = DatabaseEnvironment.Open( - clientHome, cfg); - cEnv.RepMgrStartClient(2, false); - - /* Wait for client to start up */ - int i = 0; - while (!cEnv.ReplicationSystemStats().ClientStartupComplete) { - if (i < 20) { - Thread.Sleep(1000); - i++; - } else - throw new TestException(); - } - - /* - * Verify the client info could be observed by master's - * remote site. - */ - Assert.AreEqual(1, mEnv.RepMgrRemoteSites.Length); - RepMgrSite rsite = mEnv.RepMgrRemoteSites[0]; - Assert.AreEqual("127.0.0.1", rsite.Address.Host); - Assert.AreEqual(cPort, rsite.Address.Port); - Assert.AreEqual(true, rsite.isConnected); - Assert.AreEqual(false, rsite.isPeer); - - DbSite site = mEnv.RepMgrSite("127.0.0.1", mPort); - Assert.AreEqual("127.0.0.1", site.Address.Host); - Assert.AreEqual(mPort, site.Address.Port); - Assert.LessOrEqual(0, site.EId); - Assert.AreEqual(true, site.GroupCreator); - Assert.AreEqual(true, site.LocalSite); - Assert.AreEqual(false, site.Helper); - Assert.AreEqual(false, site.Legacy); - Assert.AreEqual(false, site.Peer); - site.Close(); - - site = mEnv.RepMgrSite("127.0.0.1", cPort); - Assert.AreEqual("127.0.0.1", site.Address.Host); - Assert.AreEqual(cPort, site.Address.Port); - Assert.AreEqual(rsite.EId, site.EId); - Assert.AreEqual(false, site.GroupCreator); - Assert.AreEqual(false, site.LocalSite); - Assert.AreEqual(false, site.Helper); - Assert.AreEqual(false, site.Legacy); - Assert.AreEqual(false, site.Peer); - site.Remove(); - - cEnv.Close(); - mEnv.Close(); - - /* - * Update the repmgr site, and verify it is updated in - * unmanaged memory. - */ - rsite.Address = new ReplicationHostAddress( - "192.168.1.1", 1000); - rsite.EId = 1024; - rsite.isConnected = false; - rsite.isPeer = true; - Assert.AreEqual("192.168.1.1", rsite.Address.Host); - Assert.AreEqual(1000, rsite.Address.Port); - Assert.AreEqual(1024, rsite.EId); - Assert.AreEqual(false, rsite.isConnected); - Assert.AreEqual(true, rsite.isPeer); - } - - [Test] - public void TestRepMgr() - { - testName = "TestRepMgr"; - SetUpTest(true); - - // Initialize ports for master and client. - ports.Clear(); - AvailablePorts portGen = new AvailablePorts(); - ports.Insert(0, portGen.Current); - portGen.MoveNext(); - ports.Insert(1, portGen.Current); - - clientStartSignal = new AutoResetEvent(false); - masterCloseSignal = new AutoResetEvent(false); - - Thread thread1 = new Thread(new ThreadStart(Master)); - Thread thread2 = new Thread(new ThreadStart(Client)); - - // Start master thread before client thread. - thread1.Start(); - Thread.Sleep(1000); - thread2.Start(); - thread2.Join(); - thread1.Join(); - - clientStartSignal.Close(); - masterCloseSignal.Close(); - } - - public void Master() - { - string home = testHome + "/Master"; - string dbName = "rep.db"; - Configuration.ClearDir(home); - - /* - * Configure and open environment with replication - * application. - */ - DatabaseEnvironmentConfig cfg = - new DatabaseEnvironmentConfig(); - cfg.UseReplication = true; - cfg.MPoolSystemCfg = new MPoolConfig(); - cfg.MPoolSystemCfg.CacheSize = - new CacheInfo(0, 20485760, 1); - cfg.UseLocking = true; - cfg.UseTxns = true; - cfg.UseMPool = true; - cfg.Create = true; - cfg.UseLogging = true; - cfg.RunRecovery = true; - cfg.TxnNoSync = true; - cfg.FreeThreaded = true; - cfg.RepSystemCfg = new ReplicationConfig(); - cfg.RepSystemCfg.RepmgrSitesConfig.Add(new DbSiteConfig()); - cfg.RepSystemCfg.RepmgrSitesConfig[0].Host = "127.0.0.1"; - cfg.RepSystemCfg.RepmgrSitesConfig[0].Port = ports[0]; - cfg.RepSystemCfg.RepmgrSitesConfig[0].LocalSite = true; - cfg.RepSystemCfg.Priority = 100; - cfg.RepSystemCfg.BulkTransfer = true; - cfg.RepSystemCfg.AckTimeout = 2000; - cfg.RepSystemCfg.BulkTransfer = true; - cfg.RepSystemCfg.CheckpointDelay = 1500; - cfg.RepSystemCfg.Clockskew(102, 100); - cfg.RepSystemCfg.ConnectionRetry = 10; - cfg.RepSystemCfg.DelayClientSync = false; - cfg.RepSystemCfg.ElectionRetry = 5; - cfg.RepSystemCfg.ElectionTimeout = 3000; - cfg.RepSystemCfg.FullElectionTimeout = 5000; - cfg.RepSystemCfg.HeartbeatMonitor = 100; - cfg.RepSystemCfg.HeartbeatSend = 10; - cfg.RepSystemCfg.LeaseTimeout = 1300; - cfg.RepSystemCfg.AutoInit = true; - cfg.RepSystemCfg.NoBlocking = false; - cfg.RepSystemCfg.RepMgrAckPolicy = - AckPolicy.ALL_PEERS; - cfg.RepSystemCfg.RetransmissionRequest(10, 100); - cfg.RepSystemCfg.Strict2Site = true; - cfg.RepSystemCfg.UseMasterLeases = false; - cfg.EventNotify = new EventNotifyDelegate(stuffHappened); - DatabaseEnvironment env = DatabaseEnvironment.Open( - home, cfg); - - // Get initial replication stats. - ReplicationStats repStats = env.ReplicationSystemStats(); - env.PrintReplicationSystemStats(); - Assert.AreEqual(100, repStats.EnvPriority); - Assert.AreEqual(1, - repStats.CurrentElectionGenerationNumber); - Assert.AreEqual(0, repStats.CurrentGenerationNumber); - Assert.AreEqual(0, repStats.AppliedTransactions); - Assert.AreEqual(0, repStats.ElectionDataGeneration); - - // Start a master site with replication manager. - env.RepMgrStartMaster(3); - - // Open a btree database and write some data. - Transaction txn = env.BeginTransaction(); - BTreeDatabaseConfig dbConfig = - new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Env = env; - dbConfig.PageSize = 512; - BTreeDatabase db = BTreeDatabase.Open(dbName, - dbConfig, txn); - txn.Commit(); - txn = env.BeginTransaction(); - for (int i = 0; i < 5; i++) - db.Put(new DatabaseEntry(BitConverter.GetBytes(i)), - new DatabaseEntry(BitConverter.GetBytes(i)), txn); - txn.Commit(); - - Console.WriteLine( - "Master: Finished initialization and data#1."); - - // Client site could enter now. - clientStartSignal.Set(); - Console.WriteLine( - "Master: Wait for Client to join and get #1."); - - Console.WriteLine("..."); - - // Put some new data into master site. - txn = env.BeginTransaction(); - for (int i = 10; i < 15; i++) - db.Put(new DatabaseEntry(BitConverter.GetBytes(i)), - new DatabaseEntry(BitConverter.GetBytes(i)), - txn); - txn.Commit(); - Console.WriteLine( - "Master: Write something new, data #2."); - Console.WriteLine("Master: Wait for client to read #2..."); - - // Get the stats. - repStats = env.ReplicationSystemStats(true); - env.PrintReplicationSystemStats(); - Assert.LessOrEqual(0, repStats.AppliedTransactions); - Assert.LessOrEqual(0, repStats.AwaitedLSN.LogFileNumber); - Assert.LessOrEqual(0, repStats.AwaitedLSN.Offset); - Assert.LessOrEqual(0, repStats.AwaitedPage); - Assert.LessOrEqual(0, repStats.BadGenerationMessages); - Assert.LessOrEqual(0, repStats.BulkBufferFills); - Assert.LessOrEqual(0, repStats.BulkBufferOverflows); - Assert.LessOrEqual(0, repStats.BulkBufferTransfers); - Assert.LessOrEqual(0, repStats.BulkRecordsStored); - Assert.LessOrEqual(0, repStats.ClientServiceRequests); - Assert.LessOrEqual(0, repStats.ClientServiceRequestsMissing); - Assert.IsInstanceOf(typeof(bool), repStats.ClientStartupComplete); - Assert.AreEqual(2, repStats.CurrentElectionGenerationNumber); - Assert.AreEqual(1, repStats.CurrentGenerationNumber); - Assert.LessOrEqual(0, repStats.CurrentQueuedLogRecords); - Assert.LessOrEqual(0, repStats.CurrentWinner); - Assert.LessOrEqual(0, repStats.CurrentWinnerMaxLSN.LogFileNumber); - Assert.LessOrEqual(0, repStats.CurrentWinnerMaxLSN.Offset); - Assert.LessOrEqual(0, repStats.DuplicateLogRecords); - Assert.LessOrEqual(0, repStats.DuplicatePages); - Assert.LessOrEqual(0, repStats.DupMasters); - Assert.LessOrEqual(0, repStats.ElectionGenerationNumber); - Assert.LessOrEqual(0, repStats.ElectionPriority); - Assert.LessOrEqual(0, repStats.Elections); - Assert.LessOrEqual(0, repStats.ElectionStatus); - Assert.LessOrEqual(0, repStats.ElectionsWon); - Assert.LessOrEqual(0, repStats.ElectionTiebreaker); - Assert.LessOrEqual(0, repStats.ElectionTimeSec); - Assert.LessOrEqual(0, repStats.ElectionTimeUSec); - Assert.AreEqual(repStats.EnvID, repStats.MasterEnvID); - Assert.LessOrEqual(0, repStats.EnvPriority); - Assert.LessOrEqual(0, repStats.FailedMessageSends); - Assert.LessOrEqual(0, repStats.ForcedRerequests); - Assert.LessOrEqual(0, repStats.IgnoredMessages); - Assert.LessOrEqual(0, repStats.MasterChanges); - Assert.LessOrEqual(0, repStats.MasterEnvID); - Assert.LessOrEqual(0, repStats.MaxLeaseSec); - Assert.LessOrEqual(0, repStats.MaxLeaseUSec); - Assert.LessOrEqual(0, repStats.MaxPermanentLSN.Offset); - Assert.LessOrEqual(0, repStats.MaxQueuedLogRecords); - Assert.LessOrEqual(0, repStats.MessagesSent); - Assert.LessOrEqual(0, repStats.MissedLogRecords); - Assert.LessOrEqual(0, repStats.MissedPages); - Assert.LessOrEqual(0, repStats.NewSiteMessages); - Assert.LessOrEqual(repStats.MaxPermanentLSN.LogFileNumber, - repStats.NextLSN.LogFileNumber); - if (repStats.MaxPermanentLSN.LogFileNumber == - repStats.NextLSN.LogFileNumber) - Assert.Less(repStats.MaxPermanentLSN.Offset, - repStats.NextLSN.Offset); - Assert.LessOrEqual(0, repStats.NextPage); - Assert.LessOrEqual(0, repStats.Outdated); - Assert.LessOrEqual(0, repStats.QueuedLogRecords); - Assert.LessOrEqual(0, repStats.ReceivedLogRecords); - Assert.LessOrEqual(0, repStats.ReceivedMessages); - Assert.LessOrEqual(0, repStats.ReceivedPages); - Assert.LessOrEqual(0, repStats.RegisteredSites); - Assert.LessOrEqual(0, repStats.RegisteredSitesNeeded); - Assert.LessOrEqual(0, repStats.Sites); - Assert.LessOrEqual(0, repStats.StartSyncMessagesDelayed); - Assert.AreEqual(2, repStats.Status); - Assert.LessOrEqual(0, repStats.Throttled); - Assert.LessOrEqual(0, repStats.Votes); - - // Get replication manager statistics. - RepMgrStats repMgrStats = env.RepMgrSystemStats(true); - Assert.LessOrEqual(0, repMgrStats.DroppedConnections); - Assert.LessOrEqual(0, repMgrStats.DroppedMessages); - Assert.LessOrEqual(0, repMgrStats.FailedConnections); - Assert.LessOrEqual(0, repMgrStats.FailedMessages); - Assert.LessOrEqual(0, repMgrStats.QueuedMessages); - - // Print them out. - env.PrintRepMgrSystemStats(); - - // Wait until client has finished reading. - masterCloseSignal.WaitOne(); - Console.WriteLine("Master: Leave as well."); - - // Close all. - db.Close(false); - env.LogFlush(); - env.Close(); - } - - public void Client() - { - string home = testHome + "/Client"; - Configuration.ClearDir(home); - - clientStartSignal.WaitOne(); - Console.WriteLine("Client: Join the replication"); - - // Open a environment. - DatabaseEnvironmentConfig cfg = - new DatabaseEnvironmentConfig(); - cfg.UseReplication = true; - cfg.MPoolSystemCfg = new MPoolConfig(); - cfg.MPoolSystemCfg.CacheSize = - new CacheInfo(0, 20485760, 1); - cfg.UseLocking = true; - cfg.UseTxns = true; - cfg.UseMPool = true; - cfg.Create = true; - cfg.UseLogging = true; - cfg.RunRecovery = true; - cfg.TxnNoSync = true; - cfg.FreeThreaded = true; - cfg.LockTimeout = 50000; - cfg.RepSystemCfg = new ReplicationConfig(); - cfg.RepSystemCfg.RepmgrSitesConfig.Add(new DbSiteConfig()); - cfg.RepSystemCfg.RepmgrSitesConfig[0].Host = "127.0.0.1"; - cfg.RepSystemCfg.RepmgrSitesConfig[0].Port = ports[1]; - cfg.RepSystemCfg.RepmgrSitesConfig[0].LocalSite = true; - cfg.RepSystemCfg.Priority = 10; - cfg.RepSystemCfg.RepmgrSitesConfig.Add(new DbSiteConfig()); - cfg.RepSystemCfg.RepmgrSitesConfig[1].Host = "127.0.0.1"; - cfg.RepSystemCfg.RepmgrSitesConfig[1].Port = ports[0]; - cfg.RepSystemCfg.RepmgrSitesConfig[1].Helper = true; - cfg.EventNotify = new EventNotifyDelegate(stuffHappened); - DatabaseEnvironment env = DatabaseEnvironment.Open( - home, cfg); - - // Start a client site with replication manager. - env.RepMgrStartClient(3, false); - - // Leave enough time to sync. - Thread.Sleep(20000); - - // Open database. - BTreeDatabaseConfig dbConfig = - new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.NEVER; - dbConfig.AutoCommit = true; - dbConfig.Env = env; - dbConfig.PageSize = 512; - BTreeDatabase db = BTreeDatabase.Open("rep.db", - dbConfig); - - // Write data into database. - Console.WriteLine("Client: Start reading data #1."); - for (int i = 0; i < 5; i++) - db.GetBoth(new DatabaseEntry( - BitConverter.GetBytes(i)), new DatabaseEntry( - BitConverter.GetBytes(i))); - - // Leave sometime for client to read new data from master. - Thread.Sleep(20000); - - /* - * Read the data. All data exists in master site should - * appear in the client site. - */ - Console.WriteLine("Client: Start reading data #2."); - for (int i = 10; i < 15; i++) - db.GetBoth(new DatabaseEntry( - BitConverter.GetBytes(i)), new DatabaseEntry( - BitConverter.GetBytes(i))); - - // Get the latest replication subsystem statistics. - ReplicationStats repStats = env.ReplicationSystemStats(); - Assert.IsTrue(repStats.ClientStartupComplete); - Assert.LessOrEqual(0, repStats.DuplicateLogRecords); - Assert.LessOrEqual(0, repStats.EnvID); - Assert.LessOrEqual(0, repStats.NextPage); - Assert.LessOrEqual(0, repStats.ReceivedPages); - Assert.AreEqual(1, repStats.Status); - - // Close all. - db.Close(false); - env.LogFlush(); - env.Close(); - Console.WriteLine( - "Client: All data is read. Leaving the replication"); - - // The master is closed after client's close. - masterCloseSignal.Set(); - } - - private void stuffHappened(NotificationEvent eventCode, byte[] info) - { - switch (eventCode) - { - case NotificationEvent.REP_CLIENT: - Console.WriteLine("Event: CLIENT"); - break; - case NotificationEvent.REP_CONNECT_BROKEN: - Console.WriteLine("Event: REP_CONNECT_BROKEN"); - break; - case NotificationEvent.REP_CONNECT_ESTD: - Console.WriteLine("Event: REP_CONNECT_ESTD"); - break; - case NotificationEvent.REP_CONNECT_TRY_FAILED: - Console.WriteLine("Event: REP_CONNECT_TRY_FAILED"); - break; - case NotificationEvent.REP_MASTER: - Console.WriteLine("Event: MASTER"); - break; - case NotificationEvent.REP_NEWMASTER: - electionDone = true; - Console.WriteLine("Event: NEWMASTER"); - break; - case NotificationEvent.REP_LOCAL_SITE_REMOVED: - Console.WriteLine("Event: REP_LOCAL_SITE_REMOVED"); - break; - case NotificationEvent.REP_SITE_ADDED: - Console.WriteLine("Event: REP_SITE_ADDED"); - break; - case NotificationEvent.REP_SITE_REMOVED: - Console.WriteLine("Event: REP_SITE_REMOVED"); - break; - case NotificationEvent.REP_STARTUPDONE: - startUpDone++; - Console.WriteLine("Event: REP_STARTUPDONE"); - break; - case NotificationEvent.REP_PERM_FAILED: - Console.WriteLine("Event: Insufficient Acks."); - break; - default: - Console.WriteLine("Event: {0}", eventCode); - break; - } - } - - [Test] - public void TestElection() - { - testName = "TestElection"; - SetUpTest(true); - - // Initialize ports for one master, and three clients. - ports.Clear(); - AvailablePorts portGen = new AvailablePorts(); - ports.Insert(0, portGen.Current); - portGen.MoveNext(); - ports.Insert(1, portGen.Current); - portGen.MoveNext(); - ports.Insert(2, portGen.Current); - portGen.MoveNext(); - ports.Insert(3, portGen.Current); - - /* - * The *Signals are used as triggers to control the test - * work flow. The client1StartSignal would be set once - * the master is ready and notify the client1 to start. - * The client2StartSignal would be set once the client1 - * is ready and notify the client2 to start. The - * client3StartSignal is similar. The masterLeaveSignal - * would be set once the last client client3 is ready - * and notify the master to leave. The - * clientsElectionSignal would be set when the master - * has already left and new master would be elected. - */ - client1StartSignal = new AutoResetEvent(false); - client2StartSignal = new AutoResetEvent(false); - client3StartSignal = new AutoResetEvent(false); - clientsElectionSignal = new AutoResetEvent(false); - masterLeaveSignal = new AutoResetEvent(false); - // Count startup done event. - startUpDone = 0; - // Whether finish election. - electionDone = false; - - Thread thread1 = new Thread( - new ThreadStart(UnstableMaster)); - Thread thread2 = new Thread( - new ThreadStart(StableClient1)); - Thread thread3 = new Thread( - new ThreadStart(StableClient2)); - Thread thread4 = new Thread( - new ThreadStart(StableClient3)); - - try { - thread1.Start(); - - /* - * After start up is done at master, start - * client1. Wait for the signal for 50000 ms. - */ - if (!client1StartSignal.WaitOne(50000)) - throw new TestException(); - thread2.Start(); - - // Ready to start client2. - if (!client2StartSignal.WaitOne(50000)) - throw new TestException(); - thread3.Start(); - - // Ready to start client3. - if (!client3StartSignal.WaitOne(50000)) - throw new TestException(); - thread4.Start(); - - thread4.Join(); - thread3.Join(); - thread2.Join(); - thread1.Join(); - - Assert.IsTrue(electionDone); - } catch (TestException e) { - if (thread1.IsAlive) - thread1.Abort(); - if (thread2.IsAlive) - thread2.Abort(); - if (thread3.IsAlive) - thread3.Abort(); - if (thread4.IsAlive) - thread4.Abort(); - throw e; - } finally { - client1StartSignal.Close(); - client2StartSignal.Close(); - client3StartSignal.Close(); - clientsElectionSignal.Close(); - masterLeaveSignal.Close(); - } - } - - public void UnstableMaster() - { - string home = testHome + "/UnstableMaster"; - Configuration.ClearDir(home); - - // Open environment with replication configuration. - DatabaseEnvironmentConfig cfg = - new DatabaseEnvironmentConfig(); - cfg.UseReplication = true; - cfg.MPoolSystemCfg = new MPoolConfig(); - cfg.MPoolSystemCfg.CacheSize = - new CacheInfo(0, 20485760, 1); - cfg.UseLocking = true; - cfg.UseTxns = true; - cfg.UseMPool = true; - cfg.Create = true; - cfg.UseLogging = true; - cfg.RunRecovery = true; - cfg.TxnNoSync = true; - cfg.FreeThreaded = true; - cfg.RepSystemCfg = new ReplicationConfig(); - cfg.RepSystemCfg.RepmgrSitesConfig.Add( - new DbSiteConfig()); - cfg.RepSystemCfg.RepmgrSitesConfig[0].Host = "127.0.0.1"; - cfg.RepSystemCfg.RepmgrSitesConfig[0].Port = ports[0]; - cfg.RepSystemCfg.RepmgrSitesConfig[0].LocalSite = true; - cfg.RepSystemCfg.Priority = 200; - cfg.RepSystemCfg.ElectionRetry = 10; - cfg.RepSystemCfg.RepMgrAckPolicy = AckPolicy.ALL; - cfg.EventNotify = new EventNotifyDelegate(stuffHappened); - DatabaseEnvironment env = DatabaseEnvironment.Open( - home, cfg); - env.DeadlockResolution = DeadlockPolicy.DEFAULT; - - try { - // Start as master site. - env.RepMgrStartMaster(3); - Console.WriteLine( - "Master: Create a new repmgr group"); - - // Client1 could start now. - client1StartSignal.Set(); - - // Wait for initialization of all clients. - if (!masterLeaveSignal.WaitOne(50000)) - throw new TestException(); - - // Check remote sites are valid. - foreach (RepMgrSite site in - env.RepMgrRemoteSites) { - Assert.AreEqual("127.0.0.1", - site.Address.Host); - Assert.IsTrue(ports.Contains( - site.Address.Port)); - } - - // Close the current master. - Console.WriteLine("Master: Unexpected leave."); - env.LogFlush(); - } catch(Exception e) { - Console.WriteLine(e.Message); - } finally { - env.Close(); - /* - * Clean up electionDone and startUpDone to - * check election for new master and start-up - * done on clients. - */ - electionDone = false; - startUpDone = 0; - /* - * Need to set signals for three times, each - * site would wait for one. - */ - for (int i = 0; i < 3; i++) - clientsElectionSignal.Set(); - } - } - - public void StableClient1() - { - StableClient(testHome + "/StableClient1", 1, ports[1], - 10, ports[0], 0, client2StartSignal); - } - - public void StableClient2() - { - StableClient(testHome + "/StableClient2", 2, ports[2], - 20, ports[0], ports[3], client3StartSignal); - } - - public void StableClient3() - { - StableClient(testHome + "/StableClient3", 3, ports[3], - 80, ports[0], ports[2], masterLeaveSignal); - } - - public void StableClient(string home, int clientIdx, - uint localPort, uint priority, - uint helperPort, uint peerPort, - EventWaitHandle notifyHandle) - { - int timeout = 0; - - Configuration.ClearDir(home); - - Console.WriteLine( - "Client{0}: Join the replication", clientIdx); - - DatabaseEnvironmentConfig cfg = - new DatabaseEnvironmentConfig(); - cfg.Create = true; - cfg.FreeThreaded = true; - cfg.MPoolSystemCfg = new MPoolConfig(); - cfg.MPoolSystemCfg.CacheSize = - new CacheInfo(0, 20485760, 1); - cfg.RunRecovery = true; - cfg.TxnNoSync = true; - cfg.UseLocking = true; - cfg.UseMPool = true; - cfg.UseTxns = true; - cfg.UseReplication = true; - cfg.UseLogging = true; - cfg.RepSystemCfg = new ReplicationConfig(); - cfg.RepSystemCfg.RepmgrSitesConfig.Add( - new DbSiteConfig()); - cfg.RepSystemCfg.RepmgrSitesConfig[0].Host = "127.0.0.1"; - cfg.RepSystemCfg.RepmgrSitesConfig[0].Port = localPort; - cfg.RepSystemCfg.RepmgrSitesConfig[0].LocalSite = true; - cfg.RepSystemCfg.Priority = priority; - cfg.RepSystemCfg.RepmgrSitesConfig.Add( - new DbSiteConfig()); - cfg.RepSystemCfg.RepmgrSitesConfig[1].Host = "127.0.0.1"; - cfg.RepSystemCfg.RepmgrSitesConfig[1].Port = helperPort; - cfg.RepSystemCfg.RepmgrSitesConfig[1].Helper = true; - cfg.RepSystemCfg.ElectionRetry = 100; - cfg.RepSystemCfg.RepMgrAckPolicy = AckPolicy.NONE; - cfg.EventNotify = new EventNotifyDelegate(stuffHappened); - DatabaseEnvironment env = DatabaseEnvironment.Open( - home, cfg); - env.DeadlockResolution = DeadlockPolicy.DEFAULT; - - try { - // Start the client. - Assert.AreEqual(clientIdx - 1, startUpDone); - env.RepMgrStartClient(3, false); - while (startUpDone < clientIdx) { - if (timeout > 10) - throw new TestException(); - timeout++; - Thread.Sleep(1000); - } - - ReplicationStats repStats = - env.ReplicationSystemStats(); - Assert.LessOrEqual(0, repStats.Elections); - Assert.LessOrEqual(0, - repStats.ElectionTiebreaker); - Assert.LessOrEqual(0, repStats.ElectionTimeSec + - repStats.ElectionTimeUSec); - Assert.LessOrEqual(0, repStats.MasterChanges); - Assert.LessOrEqual(0, repStats.NewSiteMessages); - Assert.LessOrEqual(0, - repStats.ReceivedLogRecords); - Assert.LessOrEqual(0, repStats.ReceivedMessages); - Assert.LessOrEqual(0, repStats.ReceivedPages); - Assert.GreaterOrEqual(clientIdx + 1, - repStats.RegisteredSitesNeeded); - Assert.LessOrEqual(0, repStats.Sites); - - // Notify the next event. - notifyHandle.Set(); - - // Wait for master's leave. - if (!clientsElectionSignal.WaitOne(50000)) - throw new TestException(); - - timeout = 0; - while (!electionDone) { - if (timeout > 10) - throw new TestException(); - timeout++; - Thread.Sleep(1000); - } - - env.LogFlush(); - - timeout = 0; - // Start up done event happens on client. - while (startUpDone < 2) { - if (timeout > 10) - throw new TestException(); - timeout++; - Thread.Sleep(1000); - } - Thread.Sleep((int)priority * 100); - } catch (Exception e) { - Console.WriteLine(e.Message); - } finally { - env.Close(); - Console.WriteLine( - "Client{0}: Leaving the replication", - clientIdx); - } - } - - [Test] - public void TestAckPolicy() - { - testName = "TestAckPolicy"; - SetUpTest(true); - - SetRepMgrAckPolicy(testHome + "_ALL", AckPolicy.ALL); - SetRepMgrAckPolicy(testHome + "_ALL_AVAILABLE", - AckPolicy.ALL_AVAILABLE); - SetRepMgrAckPolicy(testHome + "_ALL_PEERS", - AckPolicy.ALL_PEERS); - SetRepMgrAckPolicy(testHome + "_NONE", - AckPolicy.NONE); - SetRepMgrAckPolicy(testHome + "_ONE", - AckPolicy.ONE); - SetRepMgrAckPolicy(testHome + "_ONE_PEER", - AckPolicy.ONE_PEER); - SetRepMgrAckPolicy(testHome + "_QUORUM", - AckPolicy.QUORUM); - SetRepMgrAckPolicy(testHome + "_NULL", null); - } - - public void SetRepMgrAckPolicy(string home, AckPolicy policy) - { - Configuration.ClearDir(home); - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseLocking = true; - envConfig.UseLogging = true; - envConfig.UseMPool = true; - envConfig.UseReplication = true; - envConfig.UseTxns = true; - DatabaseEnvironment env = DatabaseEnvironment.Open( - home, envConfig); - if (policy != null) - { - env.RepMgrAckPolicy = policy; - Assert.AreEqual(policy, env.RepMgrAckPolicy); - } - env.Close(); - } - - [Test] - public void TestLocalSite() { - testName = "TestLocalSite"; - SetUpTest(true); - Configuration.ClearDir(testHome); - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseLocking = true; - envConfig.UseLogging = true; - envConfig.UseMPool = true; - envConfig.UseReplication = true; - envConfig.UseTxns = true; - ReplicationHostAddress addr = - new ReplicationHostAddress("localhost:6060"); - ReplicationConfig repCfg = new ReplicationConfig(); - DbSiteConfig dbSiteConfig = new DbSiteConfig(); - dbSiteConfig.Host = addr.Host; - dbSiteConfig.Port = addr.Port; - dbSiteConfig.LocalSite = true; - repCfg.RepmgrSitesConfig.Add(dbSiteConfig); - envConfig.RepSystemCfg = repCfg; - DatabaseEnvironment env = - DatabaseEnvironment.Open(testHome, envConfig); - ReplicationHostAddress testAddr = - env.RepMgrLocalSite.Address; - Assert.AreEqual(addr.Host, testAddr.Host); - Assert.AreEqual(addr.Port, testAddr.Port); - env.Close(); - } - - public class AvailablePorts : IEnumerable - { - private List availablePort = new List(); - private List usingPort = new List(); - private int position = -1; - - public AvailablePorts() - { - // Initial usingPort array with all TCP ports being used. - IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties(); - foreach (IPEndPoint point in properties.GetActiveTcpListeners()) - usingPort.Add((uint)point.Port); - - // Initial availablePort array with available ports ranging from 10000 to 15000. - for (uint i = 10000; i <= 15000; i++) - { - if (!usingPort.Contains(i)) - availablePort.Add(i); - } - } - - IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } - - public IEnumerator GetEnumerator() - { - while (MoveNext()) - yield return availablePort[position]; - } - - public bool MoveNext() - { - position++; - return position < availablePort.Count; - } - - public uint Current - { - get { - if (position == -1) - position = 0; - return availablePort[position]; - } - } - } - - [Test] - public void TestSiteRepConfig() - { - testName = "TestSiteRepConfig"; - SetUpTest(true); - Configuration.ClearDir(testHome); - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseLocking = true; - envConfig.UseLogging = true; - envConfig.UseMPool = true; - envConfig.UseReplication = true; - envConfig.UseTxns = true; - ReplicationHostAddress addr = - new ReplicationHostAddress("localhost:6060"); - ReplicationConfig repCfg = new ReplicationConfig(); - DbSiteConfig dbSiteConfig = new DbSiteConfig(); - dbSiteConfig.Host = addr.Host; - dbSiteConfig.Port = addr.Port; - dbSiteConfig.LocalSite = true; - repCfg.RepmgrSitesConfig.Add(dbSiteConfig); - - // DatabaseEnvironment.RepInMemory defaults to false. - envConfig.RepSystemCfg = repCfg; - DatabaseEnvironment env = - DatabaseEnvironment.Open(testHome, envConfig); - Assert.AreEqual(false, env.RepInMemory); - env.Close(); - Assert.Less(0, - Directory.GetFiles(testHome, "__db.rep.*").Length); - - // Set RepInMemory as true. - Configuration.ClearDir(testHome); - repCfg.InMemory = true; - envConfig.RepSystemCfg = repCfg; - env = DatabaseEnvironment.Open(testHome, envConfig); - Assert.AreEqual(true, env.RepInMemory); - env.Close(); - Assert.AreEqual(0, - Directory.GetFiles(testHome, "__db.rep.*").Length); - - // Set RepInMemory as false. - Configuration.ClearDir(testHome); - repCfg.InMemory = false; - envConfig.RepSystemCfg = repCfg; - env = DatabaseEnvironment.Open(testHome, envConfig); - Assert.AreEqual(false, env.RepInMemory); - env.Close(); - Assert.Less(0, - Directory.GetFiles(testHome, "__db.rep.*").Length); - } - } -} diff --git a/test/csharp/SecondaryBTreeDatabaseConfigTest.cs b/test/csharp/SecondaryBTreeDatabaseConfigTest.cs deleted file mode 100644 index b0ae74ee5..000000000 --- a/test/csharp/SecondaryBTreeDatabaseConfigTest.cs +++ /dev/null @@ -1,108 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class SecondaryBTreeDatabaseConfigTest : CSharpTestFixture - { - - [TestFixtureSetUp] - public void SetUpTestFixture() - { - testFixtureName = "SecondaryBTreeDatabaseConfigTest"; - base.SetUpTestfixture(); - } - - [Test] - public void TestConfig() - { - testName = "TestConfig"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - - XmlElement xmlElem = Configuration.TestSetUp( - testFixtureName, testName); - - // Open a primary btree database. - BTreeDatabaseConfig btreeDBConfig = - new BTreeDatabaseConfig(); - btreeDBConfig.Creation = CreatePolicy.IF_NEEDED; - BTreeDatabase btreeDB = BTreeDatabase.Open( - dbFileName, btreeDBConfig); - - SecondaryBTreeDatabaseConfig secDBConfig = - new SecondaryBTreeDatabaseConfig(btreeDB, null); - - Config(xmlElem, ref secDBConfig, true); - Confirm(xmlElem, secDBConfig, true); - - // Close the primary btree database. - btreeDB.Close(); - } - - public static void Confirm(XmlElement xmlElement, - SecondaryBTreeDatabaseConfig secBtreeDBConfig, - bool compulsory) - { - SecondaryDatabaseConfig secDBConfig = - secBtreeDBConfig; - SecondaryDatabaseConfigTest.Confirm(xmlElement, - secDBConfig, compulsory); - - // Confirm secondary btree database specific configuration. - Configuration.ConfirmCreatePolicy(xmlElement, - "Creation", secBtreeDBConfig.Creation, compulsory); - Configuration.ConfirmDuplicatesPolicy(xmlElement, - "Duplicates", secBtreeDBConfig.Duplicates, compulsory); - Configuration.ConfirmUint(xmlElement, "MinKeysPerPage", - secBtreeDBConfig.MinKeysPerPage, compulsory); - Configuration.ConfirmBool(xmlElement, - "NoReverseSplitting", - secBtreeDBConfig.NoReverseSplitting, compulsory); - Configuration.ConfirmBool(xmlElement, - "UseRecordNumbers", - secBtreeDBConfig.UseRecordNumbers, - compulsory); - } - - public static void Config(XmlElement xmlElement, - ref SecondaryBTreeDatabaseConfig secBtreeDBConfig, - bool compulsory) - { - uint minKeysPerPage = new uint(); - - SecondaryDatabaseConfig secDBConfig = secBtreeDBConfig; - SecondaryDatabaseConfigTest.Config(xmlElement, - ref secDBConfig, compulsory); - - // Configure specific fields/properties of Btree db - Configuration.ConfigCreatePolicy(xmlElement, - "Creation", ref secBtreeDBConfig.Creation, compulsory); - Configuration.ConfigDuplicatesPolicy(xmlElement, - "Duplicates", ref secBtreeDBConfig.Duplicates, - compulsory); - if (Configuration.ConfigUint(xmlElement, - "MinKeysPerPage", ref minKeysPerPage, compulsory)) - secBtreeDBConfig.MinKeysPerPage = minKeysPerPage; - Configuration.ConfigBool(xmlElement, - "NoReverseSplitting", - ref secBtreeDBConfig.NoReverseSplitting, compulsory); - Configuration.ConfigBool(xmlElement, - "UseRecordNumbers", - ref secBtreeDBConfig.UseRecordNumbers, compulsory); - } - } -} diff --git a/test/csharp/SecondaryBTreeDatabaseTest.cs b/test/csharp/SecondaryBTreeDatabaseTest.cs deleted file mode 100644 index da2213cc0..000000000 --- a/test/csharp/SecondaryBTreeDatabaseTest.cs +++ /dev/null @@ -1,295 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Threading; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class SecondaryBTreeDatabaseTest : CSharpTestFixture - { - - [TestFixtureSetUp] - public void SetUpTestFixture() - { - testFixtureName = "SecondaryBTreeDatabaseTest"; - base.SetUpTestfixture(); - } - - [Test] - public void TestOpen() - { - testName = "TestOpen"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - string dbSecFileName = testHome + "/" + - testName + "_sec.db"; - - XmlElement xmlElem = Configuration.TestSetUp( - testFixtureName, testName); - - // Open a primary btree database. - BTreeDatabaseConfig btreeDBConfig = - new BTreeDatabaseConfig(); - btreeDBConfig.Creation = CreatePolicy.IF_NEEDED; - BTreeDatabase btreeDB = BTreeDatabase.Open( - dbFileName, btreeDBConfig); - - // Open a secondary btree database. - SecondaryBTreeDatabaseConfig secDBConfig = - new SecondaryBTreeDatabaseConfig(btreeDB, null); - - SecondaryBTreeDatabaseConfigTest.Config(xmlElem, - ref secDBConfig, true); - SecondaryBTreeDatabase secDB = - SecondaryBTreeDatabase.Open(dbSecFileName, - secDBConfig); - - // Confirm its flags configured in secDBConfig. - Confirm(xmlElem, secDB, true); - - secDB.Close(); - btreeDB.Close(); - } - - [Test] - public void TestCompare() - { - testName = "TestCompare"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - - // Open a primary btree database. - BTreeDatabaseConfig btreeDBConfig = - new BTreeDatabaseConfig(); - btreeDBConfig.Creation = CreatePolicy.ALWAYS; - BTreeDatabase btreeDB = BTreeDatabase.Open( - dbFileName, btreeDBConfig); - - // Open a secondary btree database. - SecondaryBTreeDatabaseConfig secBtreeDBConfig = - new SecondaryBTreeDatabaseConfig(null, null); - secBtreeDBConfig.Primary = btreeDB; - secBtreeDBConfig.Compare = - new EntryComparisonDelegate( - SecondaryEntryComparison); - secBtreeDBConfig.KeyGen = - new SecondaryKeyGenDelegate(SecondaryKeyGen); - SecondaryBTreeDatabase secDB = - SecondaryBTreeDatabase.Open( - dbFileName, secBtreeDBConfig); - - /* - * Get the compare function set in the configuration - * and run it in a comparison to see if it is alright. - */ - EntryComparisonDelegate cmp = - secDB.Compare; - DatabaseEntry dbt1, dbt2; - dbt1 = new DatabaseEntry( - BitConverter.GetBytes((int)257)); - dbt2 = new DatabaseEntry( - BitConverter.GetBytes((int)255)); - Assert.Less(0, cmp(dbt1, dbt2)); - - - for (int i = 0; i < 1000; i++) - btreeDB.Put(new DatabaseEntry( - BitConverter.GetBytes(i)), new DatabaseEntry( - BitConverter.GetBytes(i))); - - secDB.Close(); - btreeDB.Close(); - } - - [Test] - public void TestDupCompare() - { - testName = "TestDupCompare"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - - BTreeDatabaseConfig cfg = - new BTreeDatabaseConfig(); - cfg.Creation = CreatePolicy.ALWAYS; - BTreeDatabase pDb = BTreeDatabase.Open( - dbFileName, "p", cfg); - - SecondaryBTreeDatabaseConfig sCfg = - new SecondaryBTreeDatabaseConfig(pDb, - new SecondaryKeyGenDelegate(SecondaryKeyGen)); - sCfg.Creation = CreatePolicy.IF_NEEDED; - - DatabaseEntry dbt1, dbt2; - dbt1 = new DatabaseEntry( - BitConverter.GetBytes((Int32)1)); - dbt2 = new DatabaseEntry( - BitConverter.GetBytes((Int64)4294967297)); - - // Do not set the duplicate comparison delegate. - using (SecondaryBTreeDatabase sDb = - SecondaryBTreeDatabase.Open(dbFileName, "s1", sCfg)) { - try { - int ret = sDb.DupCompare(dbt1, dbt2); - throw new TestException(); - } catch (NullReferenceException) { - } - } - - sCfg.DuplicateCompare = new EntryComparisonDelegate( - SecondaryEntryComparison); - using (SecondaryBTreeDatabase sDb = - SecondaryBTreeDatabase.Open(dbFileName, "s2", sCfg)) { - /* - * Get the dup compare function set in the - * configuration and run it in a comparison to - * see if it is alright. - */ - int ret = 1 - BitConverter.ToInt32( - BitConverter.GetBytes((Int64)4294967297), 0); - - Assert.AreEqual(ret, sDb.DupCompare(dbt1, dbt2)); - } - pDb.Close(); - } - - [Test] - public void TestDuplicates() - { - testName = "TestDuplicates"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - string dbSecFileName = testHome + "/" + testName - + "_sec.db"; - - // Open a primary btree database. - BTreeDatabaseConfig btreeDBConfig = - new BTreeDatabaseConfig(); - btreeDBConfig.Creation = CreatePolicy.IF_NEEDED; - BTreeDatabase btreeDB = BTreeDatabase.Open( - dbFileName, btreeDBConfig); - - // Open a secondary btree database. - SecondaryBTreeDatabaseConfig secBtreeDBConfig = - new SecondaryBTreeDatabaseConfig(btreeDB, null); - secBtreeDBConfig.Creation = CreatePolicy.IF_NEEDED; - secBtreeDBConfig.Duplicates = DuplicatesPolicy.SORTED; - SecondaryBTreeDatabase secDB = - SecondaryBTreeDatabase.Open(dbSecFileName, - secBtreeDBConfig); - Assert.AreEqual(DuplicatesPolicy.SORTED, secDB.Duplicates); - - secDB.Close(); - btreeDB.Close(); - } - - [Test] - public void TestPrefixCompare() - { - testName = "TestPrefixCompare"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - - // Open a primary btree database. - BTreeDatabaseConfig btreeDBConfig = - new BTreeDatabaseConfig(); - btreeDBConfig.Creation = CreatePolicy.ALWAYS; - BTreeDatabase btreeDB = BTreeDatabase.Open( - dbFileName, btreeDBConfig); - - // Open a secondary btree database. - SecondaryBTreeDatabaseConfig secBtreeDBConfig = - new SecondaryBTreeDatabaseConfig(btreeDB, null); - secBtreeDBConfig.Primary = btreeDB; - secBtreeDBConfig.Compare = - new EntryComparisonDelegate( - SecondaryEntryComparison); - secBtreeDBConfig.PrefixCompare = - new EntryPrefixComparisonDelegate( - SecondaryPrefixEntryComparison); - secBtreeDBConfig.KeyGen = - new SecondaryKeyGenDelegate( - SecondaryKeyGen); - SecondaryBTreeDatabase secDB = - SecondaryBTreeDatabase.Open( - dbFileName, secBtreeDBConfig); - - /* - * Get the prefix compare function set in the - * configuration and run it in a comparison to - * see if it is alright. - */ - EntryPrefixComparisonDelegate cmp = - secDB.PrefixCompare; - DatabaseEntry dbt1, dbt2; - dbt1 = new DatabaseEntry( - BitConverter.GetBytes((Int32)1)); - dbt2 = new DatabaseEntry( - BitConverter.GetBytes((Int64)4294967297)); - Assert.AreEqual(5, cmp(dbt1, dbt2)); - - secDB.Close(); - btreeDB.Close(); - } - - public int SecondaryEntryComparison( - DatabaseEntry dbt1, DatabaseEntry dbt2) - { - int a, b; - a = BitConverter.ToInt32(dbt1.Data, 0); - b = BitConverter.ToInt32(dbt2.Data, 0); - return a - b; - } - - private uint SecondaryPrefixEntryComparison(DatabaseEntry dbt1, - DatabaseEntry dbt2) - { - uint cnt, len; - - len = Math.Min((uint)dbt1.Data.Length, (uint)dbt2.Data.Length); - for (cnt = 0; cnt < len; cnt++) - { - if (dbt1.Data[cnt] != dbt2.Data[cnt]) - return cnt + 1; - } - if (dbt1.Data.Length > dbt2.Data.Length) - return (uint)dbt2.Data.Length + 1; - else if (dbt1.Data.Length < dbt2.Data.Length) - return (uint)dbt1.Data.Length + 1; - else - return (uint)dbt1.Data.Length; - } - - public DatabaseEntry SecondaryKeyGen( - DatabaseEntry key, DatabaseEntry data) - { - DatabaseEntry dbtGen; - dbtGen = new DatabaseEntry(data.Data); - return dbtGen; - } - - public static void Confirm(XmlElement xmlElem, - SecondaryBTreeDatabase secDB, bool compulsory) - { - Configuration.ConfirmDuplicatesPolicy(xmlElem, - "Duplicates", secDB.Duplicates, compulsory); - Configuration.ConfirmUint(xmlElem, "MinKeysPerPage", - secDB.MinKeysPerPage, compulsory); - Configuration.ConfirmBool(xmlElem, "NoReverseSplitting", - secDB.ReverseSplit, compulsory); - Configuration.ConfirmBool(xmlElem, "UseRecordNumbers", - secDB.RecordNumbers, compulsory); - } - } -} diff --git a/test/csharp/SecondaryCursorTest.cs b/test/csharp/SecondaryCursorTest.cs deleted file mode 100644 index 7180bf9bc..000000000 --- a/test/csharp/SecondaryCursorTest.cs +++ /dev/null @@ -1,1534 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Threading; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class SecondaryCursorTest : CSharpTestFixture - { - - [TestFixtureSetUp] - public void SetUpTestFixture() - { - testFixtureName = "SecondaryCursorTest"; - base.SetUpTestfixture(); - } - - [Test] - public void TestDuplicate() - { - testName = "TestDuplicate"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - string dbSecFileName = testHome + "/" + testName + - "_sec.db"; - - // Open a primary database. - BTreeDatabaseConfig dbConfig = - new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - BTreeDatabase db = BTreeDatabase.Open( - dbFileName, dbConfig); - - // Open a secondary database. - SecondaryBTreeDatabaseConfig secConfig = - new SecondaryBTreeDatabaseConfig(db, - new SecondaryKeyGenDelegate(SecondaryKeyGen)); - secConfig.Creation = CreatePolicy.IF_NEEDED; - secConfig.Duplicates = DuplicatesPolicy.UNSORTED; - SecondaryBTreeDatabase secDB = - SecondaryBTreeDatabase.Open(dbSecFileName, - secConfig); - - // Put a pair of key and data into the database. - DatabaseEntry key, data; - key = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("key")); - data = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("data")); - db.Put(key, data); - - // Create a cursor. - SecondaryCursor cursor = secDB.SecondaryCursor(); - cursor.Move(key, true); - - // Duplicate the cursor. - SecondaryCursor dupCursor; - dupCursor = cursor.Duplicate(true); - - /* - * Confirm that the duplicate cursor has the same - * position as the original one. - */ - Assert.AreEqual(cursor.Current.Key, - dupCursor.Current.Key); - Assert.AreEqual(cursor.Current.Value, - dupCursor.Current.Value); - - // Close the cursor and the duplicate cursor. - dupCursor.Close(); - cursor.Close(); - - // Close secondary and primary database. - secDB.Close(); - db.Close(); - } - - [Test] - public void TestDelete() - { - testName = "TestDelete"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - string dbSecFileName = testHome + "/" + testName + - "_sec.db"; - - // Open a primary database and its secondary database. - BTreeDatabase db; - SecondaryBTreeDatabase secDB; - OpenSecDB(dbFileName, dbSecFileName, out db, out secDB); - - // Put a pair of key and data into database. - DatabaseEntry key, data; - key = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("key")); - data = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("data")); - db.Put(key, data); - - // Delete the pair with cursor. - SecondaryCursor secCursor = secDB.SecondaryCursor(); - Assert.IsTrue(secCursor.MoveFirst()); - secCursor.Delete(); - - // Confirm that the pair is deleted. - Assert.IsFalse(db.Exists(key)); - - // Close all databases. - secDB.Close(); - db.Close(); - } - - [Test] - public void TestCurrent() - { - testName = "TestCurrent"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - string dbSecFileName = testHome + "/" + testName + - "_sec.db"; - - // Open a primary database and its secondary database. - BTreeDatabase db; - SecondaryBTreeDatabase secDB; - OpenSecDB(dbFileName, dbSecFileName, out db, out secDB); - - // Put a pair of key and data into database. - DatabaseEntry key, data; - key = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("key")); - data = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("data")); - db.Put(key, data); - - // Delete the pair with cursor. - SecondaryCursor secCursor = secDB.SecondaryCursor(); - Assert.IsTrue(secCursor.Move(data, true)); - - // Confirm that the current is the one we put into database. - Assert.AreEqual(data.Data, secCursor.Current.Key.Data); - - // Close all databases. - secDB.Close(); - db.Close(); - } - - [Test] - public void TestGetEnumerator() - { - testName = "TestGetEnumerator"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - string dbSecFileName = testHome + "/" + testName + - "_sec.db"; - - // Open a primary database and its secondary database. - BTreeDatabase db; - SecondaryBTreeDatabase secDB; - OpenSecDB(dbFileName, dbSecFileName, out db, - out secDB); - - // Write ten records into the database. - WriteRecords(db); - - /* - * Get all records from the secondary database and see - * if the records with key other than 10 have the same as - * their primary key. - */ - SecondaryCursor secCursor = secDB.SecondaryCursor(); - foreach (KeyValuePair> secData in secCursor) - { - if (BitConverter.ToInt32(secData.Key.Data, 0) != 10) - Assert.AreEqual(secData.Value.Key.Data, - secData.Value.Value.Data); - } - - // Close all cursors and databases. - secCursor.Close(); - secDB.Close(); - db.Close(); - } - - [Test] - public void TestMoveToKey() - { - testName = "TestMoveToKey"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - string dbSecFileName = testHome + "/" + testName + - "_sec.db"; - - MoveToPos(dbFileName, dbSecFileName, false); - } - - [Test] - public void TestMoveToPair() - { - testName = "TestMoveToPair"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - string dbSecFileName = testHome + "/" + testName + - "_sec.db"; - - MoveToPos(dbFileName, dbSecFileName, true); - } - - public void MoveToPos(string dbFileName, - string dbSecFileName, bool ifPair) - { - // Open a primary database and its secondary database. - BTreeDatabase db; - SecondaryBTreeDatabase secDB; - OpenSecDB(dbFileName, dbSecFileName, out db, - out secDB); - - // Write ten records into the database. - WriteRecords(db); - - SecondaryCursor secCursor = secDB.SecondaryCursor(); - DatabaseEntry key = new DatabaseEntry( - BitConverter.GetBytes((int)0)); - DatabaseEntry notExistingKey = new DatabaseEntry( - BitConverter.GetBytes((int)100)); - if (ifPair == false) { - Assert.IsTrue(secCursor.Move(key, true)); - Assert.IsFalse(secCursor.Move(notExistingKey, true)); - - Assert.IsTrue(secCursor.Move(key, - new DatabaseEntry(), - new DatabaseEntry(2, 2), true)); - CheckPartial(null, 0, 0, null, 0, 0, - secCursor.Current.Value.Value, 2, 2); - - /* Disable the test. - Assert.IsTrue(secCursor.Move(key, - new DatabaseEntry(1, 1), - new DatabaseEntry(2, 2), true)); - CheckPartial(null, 0, 0, - secCursor.Current.Value.Key, 1, 1, - secCursor.Current.Value.Value, 2, 2);*/ - - byte[] partBytes = new byte[] {key.Data[1]}; - key = new DatabaseEntry(partBytes, 1, 1); - Assert.IsTrue(secCursor.Move(key, false)); - CheckPartial(secCursor.Current.Key, 1, 1, - null, 0, 0, null, 0, 0); - } else { - KeyValuePair> pair = - new KeyValuePair>(key, - new KeyValuePair( - key, key)); - - KeyValuePair> notExistingPair; - - notExistingPair = new KeyValuePair>( - notExistingKey, new KeyValuePair< - DatabaseEntry, DatabaseEntry>( - notExistingKey, notExistingKey)); - Assert.IsTrue(secCursor.Move(pair, true)); - Assert.IsFalse(secCursor.Move(notExistingPair, true)); - } - - secCursor.Close(); - secDB.Close(); - db.Close(); - } - - [Test] - public void TestMoveToKeyWithLockingInfo() - { - testName = "TestMoveToKeyWithLockingInfo"; - SetUpTest(true); - string dbFileName = testName + ".db"; - string dbSecFileName = testName + "_sec.db"; - - MoveToPosWithLockingInfo(testHome, dbFileName, - dbSecFileName, false); - } - - [Test] - public void TestMoveToPairWithLockingInfo() - { - testName = "TestMoveToPairWithLockingInfo"; - SetUpTest(true); - string dbFileName = testName + ".db"; - string dbSecFileName = testName + "_sec.db"; - - MoveToPosWithLockingInfo(testHome, dbFileName, - dbSecFileName, true); - } - - public void MoveToPosWithLockingInfo(string home, - string dbFileName, string dbSecFileName, bool ifPair) - { - // Open a primary database and its secondary database. - BTreeDatabase db; - DatabaseEnvironment env; - SecondaryBTreeDatabase secDB; - OpenSecDBInTxn(home, dbFileName, dbSecFileName, - out env, out db, out secDB); - - // Write ten records into the database. - WriteRecordsInTxn(db, env); - - // Create an secondary cursor. - Transaction cursorTxn = env.BeginTransaction(); - SecondaryCursor secCursor = secDB.SecondaryCursor( - cursorTxn); - DatabaseEntry key = new DatabaseEntry( - BitConverter.GetBytes((int)0)); - LockingInfo lockingInfo = new LockingInfo(); - lockingInfo.IsolationDegree = Isolation.DEGREE_THREE; - lockingInfo.ReadModifyWrite = true; - if (ifPair == false) { - Assert.IsTrue(secCursor.Move( - key, true, lockingInfo)); - - Assert.IsTrue(secCursor.Move(key, - new DatabaseEntry(), - new DatabaseEntry(2, 2), true)); - CheckPartial(null, 0, 0, null, 0, 0, - secCursor.Current.Value.Value, 2, 2); - - /* Disable the test. - Assert.IsTrue(secCursor.Move(key, - new DatabaseEntry(1, 1), - new DatabaseEntry(2, 2), true)); - CheckPartial(null, 0, 0, - secCursor.Current.Value.Key, 1, 1, - secCursor.Current.Value.Value, 2, 2);*/ - - byte[] partByte = new byte[] {key.Data[2]}; - key = new DatabaseEntry(partByte, 2, 1); - Assert.IsTrue(secCursor.Move(key, false)); - CheckPartial(secCursor.Current.Key, 2, 1, - null, 0, 0, null, 0, 0); - } else { - KeyValuePair> pair; - - pair = new KeyValuePair>(key, - new KeyValuePair( - key, key)); - Assert.IsTrue(secCursor.Move(pair, true, lockingInfo)); - } - secCursor.Close(); - cursorTxn.Commit(); - - secDB.Close(); - db.Close(); - env.Close(); - } - - [Test] - public void TestMoveFirst() - { - testName = "TestMoveFirst"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - string dbSecFileName = testHome + "/" + testName + - "_sec.db"; - - // Open primary and secondary database. - BTreeDatabase db; - SecondaryBTreeDatabase secDB; - OpenSecDB(dbFileName, dbSecFileName, out db, - out secDB); - - // Write ten records into the database. - WriteRecords(db); - - // Move the cursor to the first record(0,0). - SecondaryCursor cursor = secDB.SecondaryCursor(); - Assert.IsTrue(cursor.MoveFirst()); - Assert.AreEqual(BitConverter.GetBytes((int)0), - cursor.Current.Key.Data); - - Assert.IsTrue(cursor.MoveFirst( - new DatabaseEntry(0, 1), - new DatabaseEntry(), - new DatabaseEntry(2, 2))); - CheckPartial(cursor.Current.Key, 0, 1, - null, 0, 0, - cursor.Current.Value.Value, 2, 2); - - /* Disable the test. - Assert.IsTrue(cursor.MoveFirst( - new DatabaseEntry(0, 1), - new DatabaseEntry(1, 1), - new DatabaseEntry(2, 2))); - CheckPartial(cursor.Current.Key, 0, 1, - cursor.Current.Value.Key, 1, 1, - cursor.Current.Value.Value, 2, 2);*/ - - // Close all. - cursor.Close(); - secDB.Close(); - db.Close(); - } - - [Test] - public void TestMoveLast() - { - testName = "TestMoveLast"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - string dbSecFileName = testHome + "/" + testName + - "_sec.db"; - - BTreeDatabase db; - SecondaryBTreeDatabase secDB; - OpenSecDB(dbFileName, dbSecFileName, out db, - out secDB); - WriteRecords(db); - - SecondaryCursor cursor = secDB.SecondaryCursor(); - Assert.IsTrue(cursor.MoveLast()); - Assert.AreEqual(BitConverter.GetBytes((int)10), - cursor.Current.Key.Data); - - Assert.IsTrue(cursor.MoveLast( - new DatabaseEntry(0, 1), - new DatabaseEntry(), - new DatabaseEntry(2, 2))); - CheckPartial(cursor.Current.Key, 0, 1, null, 0, 0, - cursor.Current.Value.Value, 2, 2); - - /* Disable the test. - Assert.IsTrue(cursor.MoveLast( - new DatabaseEntry(0, 1), - new DatabaseEntry(1, 1), - new DatabaseEntry(2, 2))); - CheckPartial(cursor.Current.Key, 0, 1, - cursor.Current.Value.Key, 1, 1, - cursor.Current.Value.Value, 2, 2);*/ - - cursor.Close(); - secDB.Close(); - db.Close(); - } - - [Test] - public void TestMoveNext() - { - testName = "TestMoveNext"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - string dbSecFileName = testHome + "/" + testName + - "_sec.db"; - - BTreeDatabase db; - SecondaryBTreeDatabase secDB; - OpenSecDB(dbFileName, dbSecFileName, out db, - out secDB); - WriteRecords(db); - - SecondaryCursor cursor = secDB.SecondaryCursor(); - cursor.MoveFirst(); - for (int i = 0; i < 5; i++) { - Assert.IsTrue(cursor.MoveNext()); - Assert.IsTrue(cursor.MovePrev()); - - Assert.IsTrue(cursor.MoveNext( - new DatabaseEntry(0, 1), - new DatabaseEntry(), - new DatabaseEntry(2, 2))); - CheckPartial(cursor.Current.Key, 0, 1, null, - 0, 0, cursor.Current.Value.Value, 2, 2); - - /* Disable the test. - Assert.IsTrue(cursor.MoveNext( - new DatabaseEntry(0, 1), - new DatabaseEntry(1, 1), - new DatabaseEntry(2, 2))); - CheckPartial(cursor.Current.Key, 0, 1, - cursor.Current.Value.Key, 1, 1, - cursor.Current.Value.Value, 2, 2);*/ - } - - cursor.Close(); - secDB.Close(); - db.Close(); - } - - [Test] - public void TestMoveNextDuplicate() - { - testName = "TestMoveNextDuplicate"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - string dbSecFileName = testHome + "/" + testName + - "_sec.db"; - - BTreeDatabase db; - SecondaryBTreeDatabase secDB; - OpenSecDB(dbFileName, dbSecFileName, out db, - out secDB); - WriteRecords(db); - - // Create a cursor and move the cursor to duplicate record. - SecondaryCursor cursor = secDB.SecondaryCursor(); - cursor.Move(new DatabaseEntry( - BitConverter.GetBytes((int)10)), true); - Assert.IsTrue(cursor.MoveNextDuplicate()); - Assert.AreEqual(BitConverter.GetBytes((int)10), - cursor.Current.Key.Data); - - cursor.MovePrevDuplicate(); - - Assert.IsTrue(cursor.MoveNextDuplicate( - new DatabaseEntry(0, 1), - new DatabaseEntry(), - new DatabaseEntry(2, 2))); - CheckPartial(cursor.Current.Key, 0, 1, null, 0, 0, - cursor.Current.Value.Value, 2, 2); - - /* Disable the test. - Assert.IsTrue(cursor.MoveNextDuplicate( - new DatabaseEntry(0, 1), - new DatabaseEntry(1, 1), - new DatabaseEntry(2, 2))); - CheckPartial(cursor.Current.Key, 0, 1, - cursor.Current.Value.Key, 1, 1, - cursor.Current.Value.Value, 2, 2);*/ - - cursor.Close(); - secDB.Close(); - db.Close(); - } - - [Test] - public void TestMoveNextUnique() - { - testName = "TestMoveNextUnique"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - string dbSecFileName = testHome + "/" + testName + - "_sec.db"; - - BTreeDatabase db; - SecondaryBTreeDatabase secDB; - OpenSecDB(dbFileName, dbSecFileName, out db, - out secDB); - WriteRecords(db); - - /* - * Move cursor to duplicate record. Since the duplicate - * record has the largest key, moving to the next - * unique record should fail. - */ - SecondaryCursor cursor = secDB.SecondaryCursor(); - cursor.Move(new DatabaseEntry( - BitConverter.GetBytes((int)1)), true); - Assert.IsTrue(cursor.MoveNextUnique()); - - cursor.Move(new DatabaseEntry( - BitConverter.GetBytes((int)10)), true); - Assert.IsFalse(cursor.MoveNextUnique()); - - cursor.Move(new DatabaseEntry( - BitConverter.GetBytes((int)4)), true); - - Assert.IsTrue(cursor.MoveNextUnique( - new DatabaseEntry(0, 1), - new DatabaseEntry(), - new DatabaseEntry(2, 2))); - CheckPartial(cursor.Current.Key, 0, 1, null, 0, 0, - cursor.Current.Value.Value, 2, 2); - - /* Disable the test. - Assert.IsTrue(cursor.MoveNextUnique( - new DatabaseEntry(0, 1), - new DatabaseEntry(1, 1), - new DatabaseEntry(2, 2))); - CheckPartial(cursor.Current.Key, 0, 1, - cursor.Current.Value.Key, 1, 1, - cursor.Current.Value.Value, 2, 2);*/ - - cursor.Close(); - secDB.Close(); - db.Close(); - } - - [Test] - public void TestMovePrev() - { - testName = "TestMovePrev"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - string dbSecFileName = testHome + "/" + testName + - "_sec.db"; - - BTreeDatabase db; - SecondaryBTreeDatabase secDB; - OpenSecDB(dbFileName, dbSecFileName, out db, - out secDB); - WriteRecords(db); - - SecondaryCursor cursor = secDB.SecondaryCursor(); - cursor.MoveLast(); - for (int i = 0; i < 5; i++) { - Assert.IsTrue(cursor.MovePrev()); - - cursor.MoveNext(); - - Assert.IsTrue(cursor.MovePrev( - new DatabaseEntry(0, 1), - new DatabaseEntry(), - new DatabaseEntry(2, 2))); - CheckPartial(cursor.Current.Key, 0, 1, null, 0, 0, - cursor.Current.Value.Value, 2, 2); - - /* Disable the test. - Assert.IsTrue(cursor.MovePrev( - new DatabaseEntry(0, 1), - new DatabaseEntry(1, 1), - new DatabaseEntry(2, 2))); - CheckPartial(cursor.Current.Key, 0, 1, - cursor.Current.Value.Key, 1, 1, - cursor.Current.Value.Value, 2, 2);*/ - } - - cursor.Close(); - secDB.Close(); - db.Close(); - } - - [Test] - public void TestMovePrevDuplicate() - { - testName = "TestMovePrevDuplicate"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - string dbSecFileName = testHome + "/" + testName + - "_sec.db"; - - BTreeDatabase db; - SecondaryBTreeDatabase secDB; - OpenSecDB(dbFileName, dbSecFileName, out db, - out secDB); - WriteRecords(db); - - SecondaryCursor cursor = secDB.SecondaryCursor(); - KeyValuePair> pair; - DatabaseEntry pKey, pData; - pKey = new DatabaseEntry(BitConverter.GetBytes((int)6)); - pData = new DatabaseEntry(BitConverter.GetBytes((int)10)); - pair = new KeyValuePair>(pData, - new KeyValuePair( - pKey, pData)); - cursor.Move(pair, true); - Assert.IsTrue(cursor.MovePrevDuplicate()); - Assert.AreEqual(BitConverter.GetBytes((int)10), - cursor.Current.Key.Data); - - cursor.Move(pair, true); - Assert.IsTrue(cursor.MovePrevDuplicate( - new DatabaseEntry(0, 1), - new DatabaseEntry(), - new DatabaseEntry(2, 2))); - CheckPartial(cursor.Current.Key, 0, 1, null, 0, 0, - cursor.Current.Value.Value, 2, 2); - - /* Disable the test - cursor.Move(pair, true); - Assert.IsTrue(cursor.MovePrevDuplicate( - new DatabaseEntry(0, 1), - new DatabaseEntry(1, 1), - new DatabaseEntry(2, 2))); - CheckPartial(cursor.Current.Key, 0, 1, - cursor.Current.Value.Key, 1, 1, - cursor.Current.Value.Value, 2, 2);*/ - - cursor.Close(); - secDB.Close(); - db.Close(); - } - - [Test] - public void TestMovePrevUnique() - { - testName = "TestMovePrevUnique"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - string dbSecFileName = testHome + "/" + testName + - "_sec.db"; - - BTreeDatabase db; - SecondaryBTreeDatabase secDB; - OpenSecDB(dbFileName, dbSecFileName, out db, - out secDB); - WriteRecords(db); - - SecondaryCursor cursor = secDB.SecondaryCursor(); - KeyValuePair> pair; - DatabaseEntry pKey, pData; - pKey = new DatabaseEntry(BitConverter.GetBytes((int)6)); - pData = new DatabaseEntry(BitConverter.GetBytes((int)10)); - pair = new KeyValuePair>(pData, - new KeyValuePair( - pKey, pData)); - cursor.Move(pair, true); - Assert.IsTrue(cursor.MovePrevUnique()); - Assert.AreNotEqual(BitConverter.GetBytes((int)10), - cursor.Current.Key.Data); - - cursor.Move(pair, true); - Assert.IsTrue(cursor.MovePrevUnique( - new DatabaseEntry(0, 1), - new DatabaseEntry(), - new DatabaseEntry(2, 2))); - CheckPartial(cursor.Current.Key, 0, 1, null, 0, 0, - cursor.Current.Value.Value, 2, 2); - - /* Disable the test. - Assert.IsTrue(cursor.MovePrevUnique( - new DatabaseEntry(0, 1), - new DatabaseEntry(1, 1), - new DatabaseEntry(2, 2))); - CheckPartial(cursor.Current.Key, 0, 1, - cursor.Current.Value.Key, 1, 1, - cursor.Current.Value.Value, 2, 2);*/ - - cursor.Close(); - secDB.Close(); - db.Close(); - } - - [Test] - public void TestRefresh() - { - testName = "TestRefresh"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - string dbSecFileName = testHome + "/" + testName + - "_sec.db"; - - BTreeDatabase db; - SecondaryBTreeDatabase secDB; - OpenSecDB(dbFileName, dbSecFileName, out db, - out secDB); - WriteRecords(db); - - SecondaryCursor cursor = secDB.SecondaryCursor(); - KeyValuePair> pair; - DatabaseEntry pKey, pData; - pKey = new DatabaseEntry(BitConverter.GetBytes((int)6)); - pData = new DatabaseEntry(BitConverter.GetBytes((int)10)); - pair = new KeyValuePair>(pData, - new KeyValuePair( - pKey, pData)); - cursor.Move(pair, true); - Assert.IsTrue(cursor.Refresh()); - Assert.AreEqual(pData.Data, cursor.Current.Key.Data); - Assert.AreEqual(pKey.Data, cursor.Current.Value.Key.Data); - - Assert.IsTrue(cursor.Refresh( - new DatabaseEntry(0, 1), - new DatabaseEntry(), - new DatabaseEntry(2, 2))); - CheckPartial(cursor.Current.Key, 0, 1, null, 0, 0, - cursor.Current.Value.Value, 2, 2); - - /* Disable the test. - Assert.IsTrue(cursor.Refresh( - new DatabaseEntry(0, 1), - new DatabaseEntry(1, 1), - new DatabaseEntry(2, 2))); - CheckPartial(cursor.Current.Key, 0, 1, - cursor.Current.Value.Key, 1, 1, - cursor.Current.Value.Value, 2, 2);*/ - - cursor.Close(); - secDB.Close(); - db.Close(); - } - - [Test] - public void TestMoveFirstWithLockingInfo() - { - testName = "TestMoveFirstWithLockingInfo"; - SetUpTest(true); - string dbFileName = testName + ".db"; - string dbSecFileName = testName + "_sec.db"; - - /* - * Open environment, primary database and - * secondary database. - */ - BTreeDatabase db; - DatabaseEnvironment env; - SecondaryBTreeDatabase secDB; - OpenSecDBInTxn(testHome, dbFileName, dbSecFileName, - out env, out db, out secDB); - - // Write ten records into the database. - WriteRecordsInTxn(db, env); - - // Move the cursor to the first record(0, 0). - Transaction cursorTxn = env.BeginTransaction(); - SecondaryCursor cursor = - secDB.SecondaryCursor(cursorTxn); - LockingInfo lockingInfo = new LockingInfo(); - lockingInfo.IsolationDegree = Isolation.DEGREE_THREE; - lockingInfo.ReadModifyWrite = true; - Assert.IsTrue(cursor.MoveFirst(lockingInfo)); - Assert.AreEqual(BitConverter.GetBytes((int)0), - cursor.Current.Key.Data); - - Assert.IsTrue(cursor.MoveFirst( - new DatabaseEntry(0, 1), - new DatabaseEntry(), - new DatabaseEntry(2, 2), lockingInfo)); - CheckPartial(cursor.Current.Key, 0, 1, null, 0, 0, - cursor.Current.Value.Value, 2, 2); - - /* Disable the test. - Assert.IsTrue(cursor.MoveFirst( - new DatabaseEntry(0, 1), - new DatabaseEntry(1, 1), - new DatabaseEntry(2, 2), lockingInfo)); - CheckPartial(cursor.Current.Key, 0, 1, - cursor.Current.Value.Key, 1, 1, - cursor.Current.Value.Value, 2, 2);*/ - - cursor.Close(); - cursorTxn.Commit(); - - // Close all. - secDB.Close(); - db.Close(); - env.Close(); - } - - [Test] - public void TestMoveLastWithLockingInfo() - { - testName = "TestMoveLastWithLockingInfo"; - SetUpTest(true); - string dbFileName = testName + ".db"; - string dbSecFileName = testName + "_sec.db"; - - /* - * Open environment, primary database and - * secondary database. - */ - BTreeDatabase db; - DatabaseEnvironment env; - SecondaryBTreeDatabase secDB; - OpenSecDBInTxn(testHome, dbFileName, dbSecFileName, - out env, out db, out secDB); - - // Write ten records into the database. - WriteRecordsInTxn(db, env); - - /* - * Move the cursor to the last record(10, 6), that is - * record(6, 10) in primary database. - */ - Transaction cursorTxn = env.BeginTransaction(); - SecondaryCursor cursor = - secDB.SecondaryCursor(cursorTxn); - LockingInfo lockingInfo = new LockingInfo(); - lockingInfo.IsolationDegree = Isolation.DEGREE_THREE; - lockingInfo.ReadModifyWrite = true; - Assert.IsTrue(cursor.MoveLast(lockingInfo)); - Assert.AreEqual(BitConverter.GetBytes((int)10), - cursor.Current.Key.Data); - - Assert.IsTrue(cursor.MoveLast( - new DatabaseEntry(0, 1), - new DatabaseEntry(), - new DatabaseEntry(2, 2), lockingInfo)); - CheckPartial(cursor.Current.Key, 0, 1, null, 0, 0, - cursor.Current.Value.Value, 2, 2); - - /* Disable the test. - Assert.IsTrue(cursor.MoveLast( - new DatabaseEntry(0, 1), - new DatabaseEntry(1, 1), - new DatabaseEntry(2, 2), lockingInfo)); - CheckPartial(cursor.Current.Key, 0, 1, - cursor.Current.Value.Key, 1, 1, - cursor.Current.Value.Value, 2, 2);*/ - - cursor.Close(); - cursorTxn.Commit(); - - // Close all. - secDB.Close(); - db.Close(); - env.Close(); - } - - [Test] - public void TestMoveNextWithLockingInfo() - { - testName = "TestMoveNextWithLockingInfo"; - SetUpTest(true); - string dbFileName = testName + ".db"; - string dbSecFileName = testName + "_sec.db"; - - /* - * Open environment, primary database and - * secondary database. - */ - BTreeDatabase db; - DatabaseEnvironment env; - SecondaryBTreeDatabase secDB; - OpenSecDBInTxn(testHome, dbFileName, dbSecFileName, - out env, out db, out secDB); - - // Write ten records into the database. - WriteRecordsInTxn(db, env); - - /* - * Move cursor to the first record and move to next - * record for five times. - */ - Transaction cursorTxn = env.BeginTransaction(); - SecondaryCursor cursor = - secDB.SecondaryCursor(cursorTxn); - LockingInfo lockingInfo = new LockingInfo(); - lockingInfo.IsolationDegree = Isolation.DEGREE_THREE; - lockingInfo.ReadModifyWrite = true; - cursor.MoveFirst(lockingInfo); - for (int i = 0; i < 5; i++) { - Assert.IsTrue(cursor.MoveNext(lockingInfo)); - - cursor.MovePrev(lockingInfo); - - Assert.IsTrue(cursor.MoveNext( - new DatabaseEntry(0, 1), - new DatabaseEntry(), - new DatabaseEntry(2, 2), lockingInfo)); - CheckPartial(cursor.Current.Key, 0, 1, null, 0, - 0, cursor.Current.Value.Value, 2, 2); - - /* Disable the test. - Assert.IsTrue(cursor.MoveNext( - new DatabaseEntry(0, 1), - new DatabaseEntry(1, 1), - new DatabaseEntry(2, 2), lockingInfo)); - CheckPartial(cursor.Current.Key, 0, 1, - cursor.Current.Value.Key, 1, 1, - cursor.Current.Value.Value, 2, 2);*/ - } - - cursor.Close(); - cursorTxn.Commit(); - - // Close all. - secDB.Close(); - db.Close(); - env.Close(); - } - - [Test] - public void TestMoveNextDuplicateWithLockingInfo() - { - testName = "TestMoveNextDuplicateWithLockingInfo"; - SetUpTest(true); - string dbFileName = testName + ".db"; - string dbSecFileName = testName + "_sec.db"; - - /* - * Open environment, primary database and - * secondary database. - */ - BTreeDatabase db; - DatabaseEnvironment env; - SecondaryBTreeDatabase secDB; - OpenSecDBInTxn(testHome, dbFileName, - dbSecFileName, out env, out db, out secDB); - - // Write ten records into the database. - WriteRecordsInTxn(db, env); - - /* - * Create a cursor and move the cursor to duplicate - * record(10, 5), that is record(5,10) in primary database. - * Then move the cursor to the next duplicate record - * (10, 6), that is record(6,10) in primary database. - */ - Transaction cursorTxn = env.BeginTransaction(); - SecondaryCursor cursor = - secDB.SecondaryCursor(cursorTxn); - LockingInfo lockingInfo = new LockingInfo(); - lockingInfo.IsolationDegree = Isolation.DEGREE_THREE; - lockingInfo.ReadModifyWrite = true; - cursor.Move(new DatabaseEntry( - BitConverter.GetBytes((int)10)), true, lockingInfo); - Assert.IsTrue(cursor.MoveNextDuplicate(lockingInfo)); - Assert.AreEqual(BitConverter.GetBytes((int)10), - cursor.Current.Key.Data); - - cursor.MovePrevDuplicate(lockingInfo); - - Assert.IsTrue(cursor.MoveNextDuplicate( - new DatabaseEntry(0, 1), - new DatabaseEntry(), - new DatabaseEntry(2, 2), lockingInfo)); - CheckPartial(cursor.Current.Key, 0, 1, null, 0, 0, - cursor.Current.Value.Value, 2, 2); - - /* Disable the test. - Assert.IsTrue(cursor.MoveNextDuplicate( - new DatabaseEntry(0, 1), - new DatabaseEntry(1, 1), - new DatabaseEntry(2, 2), lockingInfo)); - CheckPartial(cursor.Current.Key, 0, 1, - cursor.Current.Value.Key, 1, 1, - cursor.Current.Value.Value, 2, 2);*/ - - cursor.Close(); - cursorTxn.Commit(); - - // Close all. - secDB.Close(); - db.Close(); - env.Close(); - } - - [Test] - public void TestMoveNextUniqueWithLockingInfo() - { - testName = "TestMoveNextUniqueWithLockingInfo"; - SetUpTest(true); - string dbFileName = testName + ".db"; - string dbSecFileName = testName + "_sec.db"; - - /* - * Open environment, primary database and - * secondary database. - */ - BTreeDatabase db; - DatabaseEnvironment env; - SecondaryBTreeDatabase secDB; - OpenSecDBInTxn(testHome, dbFileName, - dbSecFileName, out env, out db, out secDB); - - // Write ten records into the database. - WriteRecordsInTxn(db, env); - - /* - * Move cursor to duplicate record. Since the duplicate - * record has the largest key, moving to the next - * unique record should fail. - */ - Transaction cursorTxn = env.BeginTransaction(); - SecondaryCursor cursor = - secDB.SecondaryCursor(cursorTxn); - LockingInfo lockingInfo = new LockingInfo(); - lockingInfo.IsolationDegree = Isolation.DEGREE_THREE; - lockingInfo.ReadModifyWrite = true; - cursor.Move(new DatabaseEntry( - BitConverter.GetBytes((int)10)), true); - Assert.IsFalse(cursor.MoveNextUnique(lockingInfo)); - - cursor.Move(new DatabaseEntry( - BitConverter.GetBytes((int)3)), true, lockingInfo); - - Assert.IsTrue(cursor.MoveNextUnique( - new DatabaseEntry(0, 1), - new DatabaseEntry(), - new DatabaseEntry(2, 2), lockingInfo)); - CheckPartial(cursor.Current.Key, 0, 1, null, 0, 0, - cursor.Current.Value.Value, 2, 2); - - /* Disable the test. - Assert.IsTrue(cursor.MoveNextUnique( - new DatabaseEntry(0, 1), - new DatabaseEntry(1, 1), - new DatabaseEntry(2, 2), lockingInfo)); - CheckPartial(cursor.Current.Key, 0, 1, - cursor.Current.Value.Key, 1, 1, - cursor.Current.Value.Value, 2, 2);*/ - - cursor.Close(); - cursorTxn.Commit(); - - // Close all. - secDB.Close(); - db.Close(); - env.Close(); - } - - [Test] - public void TestMovePrevWithLockingInfo() - { - testName = "TestMovePrevWithLockingInfo"; - SetUpTest(true); - string dbFileName = testName + ".db"; - string dbSecFileName = testName + "_sec.db"; - - /* - * Open environment, primary database and - * secondary database. - */ - BTreeDatabase db; - DatabaseEnvironment env; - SecondaryBTreeDatabase secDB; - OpenSecDBInTxn(testHome, dbFileName, - dbSecFileName, out env, out db, out secDB); - - // Write ten records into the database. - WriteRecordsInTxn(db, env); - - /* - * Move the cursor to the last record and move to its - * previous record for five times. - */ - Transaction cursorTxn = env.BeginTransaction(); - SecondaryCursor cursor = - secDB.SecondaryCursor(cursorTxn); - LockingInfo lockingInfo = new LockingInfo(); - lockingInfo.IsolationDegree = Isolation.DEGREE_TWO; - lockingInfo.ReadModifyWrite = true; - cursor.MoveLast(lockingInfo); - for (int i = 0; i < 5; i++) { - Assert.IsTrue(cursor.MovePrev(lockingInfo)); - cursor.MoveNext(lockingInfo); - - Assert.IsTrue(cursor.MovePrev( - new DatabaseEntry(0, 1), - new DatabaseEntry(), - new DatabaseEntry(2, 2), lockingInfo)); - CheckPartial(cursor.Current.Key, 0, 1, null, 0, - 0, cursor.Current.Value.Value, 2, 2); - - /* Disable the test. - Assert.IsTrue(cursor.MovePrev( - new DatabaseEntry(0, 1), - new DatabaseEntry(1, 1), - new DatabaseEntry(2, 2), lockingInfo)); - CheckPartial(cursor.Current.Key, 0, 1, - cursor.Current.Value.Key, 1, 1, - cursor.Current.Value.Value, 2, 2);*/ - } - cursor.Close(); - cursorTxn.Commit(); - - // Close all. - secDB.Close(); - db.Close(); - env.Close(); - } - - [Test] - public void TestMovePrevDuplicateWithLockingInfo() - { - testName = "TestMovePrevDuplicateWithLockingInfo"; - SetUpTest(true); - string dbFileName = testName + ".db"; - string dbSecFileName = testName + "_sec.db"; - - /* - * Open environment, primary database and - * secondary database. - */ - BTreeDatabase db; - DatabaseEnvironment env; - SecondaryBTreeDatabase secDB; - OpenSecDBInTxn(testHome, dbFileName, - dbSecFileName, out env, out db, out secDB); - - // Write ten records into the database. - WriteRecordsInTxn(db, env); - - Transaction cursorTxn = env.BeginTransaction(); - SecondaryCursor cursor = - secDB.SecondaryCursor(cursorTxn); - LockingInfo lockingInfo = new LockingInfo(); - lockingInfo.IsolationDegree = Isolation.DEGREE_TWO; - lockingInfo.ReadModifyWrite = true; - - /* - * Move the cursor to the record(10,6), that is the - * record(6, 10) in the primary database. Move to - * its previous duplicate record, that is (10,5). - */ - KeyValuePair> pair; - DatabaseEntry pKey, pData; - pKey = new DatabaseEntry(BitConverter.GetBytes((int)6)); - pData = new DatabaseEntry(BitConverter.GetBytes((int)10)); - pair = new KeyValuePair>(pData, - new KeyValuePair( - pKey, pData)); - cursor.Move(pair, true, lockingInfo); - Assert.IsTrue(cursor.MovePrevDuplicate(lockingInfo)); - Assert.AreEqual(BitConverter.GetBytes((int)10), - cursor.Current.Key.Data); - - cursor.MoveNextDuplicate(lockingInfo); - - Assert.IsTrue(cursor.MovePrevDuplicate( - new DatabaseEntry(0, 1), - new DatabaseEntry(), - new DatabaseEntry(2, 2), lockingInfo)); - CheckPartial(cursor.Current.Key, 0, 1, null, 0, 0, - cursor.Current.Value.Value, 2, 2); - - /* Disable the test. - Assert.IsTrue(cursor.MovePrevDuplicate( - new DatabaseEntry(0, 1), - new DatabaseEntry(1, 1), - new DatabaseEntry(2, 2), lockingInfo)); - CheckPartial(cursor.Current.Key, 0, 1, - cursor.Current.Value.Key, 1, 1, - cursor.Current.Value.Value, 2, 2);*/ - - cursor.Close(); - cursorTxn.Commit(); - - // Close all. - secDB.Close(); - db.Close(); - env.Close(); - } - - [Test] - public void TestMovePrevUniqueWithLockingInfo() - { - testName = "TestMovePrevUniqueWithLockingInfo"; - SetUpTest(true); - string dbFileName = testName + ".db"; - string dbSecFileName = testName + "_sec.db"; - - /* - * Open environment, primary database and - * secondary database. - */ - BTreeDatabase db; - DatabaseEnvironment env; - SecondaryBTreeDatabase secDB; - OpenSecDBInTxn(testHome, dbFileName, - dbSecFileName, out env, out db, out secDB); - - // Write ten records into the database. - WriteRecordsInTxn(db, env); - - Transaction cursorTxn = env.BeginTransaction(); - SecondaryCursor cursor = - secDB.SecondaryCursor(cursorTxn); - LockingInfo lockingInfo = new LockingInfo(); - lockingInfo.IsolationDegree = Isolation.DEGREE_TWO; - lockingInfo.ReadModifyWrite = true; - - /* - * Move the cursor to the record(10, 6) and move to the - * previous unique record which has different key from - * the record(10,6). - */ - KeyValuePair> pair; - DatabaseEntry pKey, pData; - pKey = new DatabaseEntry(BitConverter.GetBytes((int)6)); - pData = new DatabaseEntry(BitConverter.GetBytes((int)10)); - pair = new KeyValuePair>(pData, - new KeyValuePair( - pKey, pData)); - cursor.Move(pair, true, lockingInfo); - Assert.IsTrue(cursor.MovePrevUnique(lockingInfo)); - Assert.AreNotEqual(BitConverter.GetBytes((int)10), - cursor.Current.Key.Data); - - cursor.Move(new DatabaseEntry(BitConverter.GetBytes( - (int)3)), true, lockingInfo); - - Assert.IsTrue(cursor.MovePrevUnique( - new DatabaseEntry(0, 1), - new DatabaseEntry(), - new DatabaseEntry(2, 2), lockingInfo)); - CheckPartial(cursor.Current.Key, 0, 1, null, 0, 0, - cursor.Current.Value.Value, 2, 2); - - /* Disable the test. - Assert.IsTrue(cursor.MovePrevUnique( - new DatabaseEntry(0, 1), - new DatabaseEntry(1, 1), - new DatabaseEntry(2, 2), lockingInfo)); - CheckPartial(cursor.Current.Key, 0, 1, - cursor.Current.Value.Key, 1, 1, - cursor.Current.Value.Value, 2, 2);*/ - - cursor.Close(); - cursorTxn.Commit(); - - // Close all. - secDB.Close(); - db.Close(); - env.Close(); - } - - [Test] - public void TestRefreshWithLockingInfo() - { - testName = "TestRefreshWithLockingInfo"; - SetUpTest(true); - string dbFileName = testName + ".db"; - string dbSecFileName = testName + "_sec.db"; - - /* - * Open environment, primary database and - * secondary database. - */ - BTreeDatabase db; - DatabaseEnvironment env; - SecondaryBTreeDatabase secDB; - OpenSecDBInTxn(testHome, dbFileName, - dbSecFileName, out env, out db, out secDB); - - // Write ten records into the database. - WriteRecordsInTxn(db, env); - - Transaction cursorTxn = env.BeginTransaction(); - SecondaryCursor cursor = - secDB.SecondaryCursor(cursorTxn); - LockingInfo lockingInfo = new LockingInfo(); - lockingInfo.IsolationDegree = Isolation.DEGREE_TWO; - lockingInfo.ReadModifyWrite = true; - - // Move cursor to a record and refresh it. - KeyValuePair> pair; - DatabaseEntry pKey, pData; - pKey = new DatabaseEntry(BitConverter.GetBytes((int)6)); - pData = new DatabaseEntry(BitConverter.GetBytes((int)10)); - pair = new KeyValuePair>(pData, - new KeyValuePair( - pKey, pData)); - cursor.Move(pair, true, lockingInfo); - Assert.IsTrue(cursor.Refresh(lockingInfo)); - Assert.AreEqual(pData.Data, cursor.Current.Key.Data); - Assert.AreEqual(pKey.Data, cursor.Current.Value.Key.Data); - - Assert.IsTrue(cursor.Refresh( - new DatabaseEntry(0, 1), - new DatabaseEntry(), - new DatabaseEntry(2, 2), lockingInfo)); - CheckPartial(cursor.Current.Key, 0, 1, null, 0, 0, - cursor.Current.Value.Value, 2, 2); - - /* Disable the test. - Assert.IsTrue(cursor.Refresh( - new DatabaseEntry(0, 1), - new DatabaseEntry(1, 1), - new DatabaseEntry(2, 2), lockingInfo)); - CheckPartial(cursor.Current.Key, 0, 1, - cursor.Current.Value.Key, 1, 1, - cursor.Current.Value.Value, 2, 2);*/ - - cursor.Close(); - cursorTxn.Commit(); - - // Close all. - secDB.Close(); - db.Close(); - env.Close(); - } - - public void OpenSecDBInTxn(string home, string dbFileName, - string dbSecFileName, out DatabaseEnvironment env, - out BTreeDatabase db, out SecondaryBTreeDatabase secDB) - { - // Open environment. - DatabaseEnvironmentConfig envCfg = - new DatabaseEnvironmentConfig(); - envCfg.Create = true; - envCfg.UseLocking = true; - envCfg.UseLogging = true; - envCfg.UseMPool = true; - envCfg.UseTxns = true; - env = DatabaseEnvironment.Open( - home, envCfg); - - // Open primary and secondary database in a transaction. - Transaction openTxn = env.BeginTransaction(); - BTreeDatabaseConfig dbConfig = - new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Env = env; - dbConfig.PageSize = 4096; - dbConfig.Duplicates = DuplicatesPolicy.NONE; - dbConfig.ReadUncommitted = true; - db = BTreeDatabase.Open(dbFileName, dbConfig, - openTxn); - openTxn.Commit(); - - openTxn = env.BeginTransaction(); - SecondaryBTreeDatabaseConfig secConfig = - new SecondaryBTreeDatabaseConfig(db, - new SecondaryKeyGenDelegate(SecondaryKeyGen)); - secConfig.Creation = CreatePolicy.IF_NEEDED; - secConfig.Duplicates = DuplicatesPolicy.SORTED; - secConfig.Env = env; - secConfig.ReadUncommitted = true; - secDB = SecondaryBTreeDatabase.Open(dbSecFileName, - secConfig, openTxn); - openTxn.Commit(); - } - - public void WriteRecords(BTreeDatabase db) - { - /* - * Write ten records into the database. The records - * from 1st to 5th and 8th to 10th are unique in the - * database. The data in the 6th and 7th records - * are the same. - */ - for (int i = 0; i < 10; i++) - { - if (i == 5 || i == 6) - db.Put(new DatabaseEntry( - BitConverter.GetBytes(i)), - new DatabaseEntry( - BitConverter.GetBytes((int)10))); - else - db.Put(new DatabaseEntry( - BitConverter.GetBytes(i)), - new DatabaseEntry(BitConverter.GetBytes(i))); - } - } - - public void WriteRecordsInTxn(BTreeDatabase db, - DatabaseEnvironment env) - { - Transaction txn = env.BeginTransaction(); - /* - * Write ten records into the database. The records - * from 1st to 5th and 8th to 10th are unique in the - * database. The data in the 6th and 7th records - * are the same. - */ - for (int i = 0; i < 10; i++) - { - if (i == 5 || i == 6) - db.Put(new DatabaseEntry( - BitConverter.GetBytes(i)), - new DatabaseEntry( - BitConverter.GetBytes((int)10)), txn); - else - db.Put(new DatabaseEntry( - BitConverter.GetBytes(i)), - new DatabaseEntry(BitConverter.GetBytes(i)), txn); - } - - txn.Commit(); - } - - public void OpenSecDB(string dbFileName, - string dbSecFileName, out BTreeDatabase db, - out SecondaryBTreeDatabase secDB) - { - // Open a primary database. - BTreeDatabaseConfig dbConfig = - new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - db = BTreeDatabase.Open(dbFileName, dbConfig); - - // Open a secondary database. - SecondaryBTreeDatabaseConfig secConfig = - new SecondaryBTreeDatabaseConfig(db, - new SecondaryKeyGenDelegate(SecondaryKeyGen)); - secConfig.Creation = CreatePolicy.IF_NEEDED; - secConfig.Duplicates = DuplicatesPolicy.SORTED; - secDB = SecondaryBTreeDatabase.Open(dbSecFileName, - secConfig); - } - - public DatabaseEntry SecondaryKeyGen( - DatabaseEntry key, DatabaseEntry data) - { - DatabaseEntry dbtGen; - dbtGen = new DatabaseEntry(data.Data); - return dbtGen; - } - - public void CheckPartial( - DatabaseEntry key, uint kOffset, uint kLen, - DatabaseEntry pkey, uint pkOffset, uint pkLen, - DatabaseEntry data, uint dOffset, uint dLen) { - if (key != null) { - Assert.IsTrue(key.Partial); - Assert.AreEqual(kOffset, key.PartialOffset); - Assert.AreEqual(kLen, key.PartialLen); - Assert.AreEqual(kLen, (uint)key.Data.Length); - } - - if (pkey != null) { - Assert.IsTrue(pkey.Partial); - Assert.AreEqual(pkOffset, pkey.PartialOffset); - Assert.AreEqual(pkLen, pkey.PartialLen); - Assert.AreEqual(pkLen, (uint)pkey.Data.Length); - } - - if (data != null) { - Assert.IsTrue(data.Partial); - Assert.AreEqual(dOffset, data.PartialOffset); - Assert.AreEqual(dLen, data.PartialLen); - Assert.AreEqual(dLen, (uint)data.Data.Length); - } - } - } -} diff --git a/test/csharp/SecondaryDatabaseConfigTest.cs b/test/csharp/SecondaryDatabaseConfigTest.cs deleted file mode 100644 index c00f156a7..000000000 --- a/test/csharp/SecondaryDatabaseConfigTest.cs +++ /dev/null @@ -1,74 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Threading; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class SecondaryDatabaseConfigTest : CSharpTestFixture - { - - [TestFixtureSetUp] - public void SetUpTestFixture() - { - testFixtureName = "SecondaryDatabaseConfigTest"; - base.SetUpTestfixture(); - } - - [Test] - virtual public void TestConfig() - { - testName = "TestConfig"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName; - - XmlElement xmlElem = Configuration.TestSetUp( - testFixtureName, testName); - - // Open a primary btree database. - BTreeDatabaseConfig btreeDBConfig = - new BTreeDatabaseConfig(); - btreeDBConfig.Creation = CreatePolicy.IF_NEEDED; - BTreeDatabase btreeDB = BTreeDatabase.Open( - dbFileName, btreeDBConfig); - - SecondaryDatabaseConfig secDBConfig = - new SecondaryDatabaseConfig(btreeDB, null); - Config(xmlElem, ref secDBConfig, true); - Confirm(xmlElem, secDBConfig, true); - btreeDB.Close(); - } - - public static void Config(XmlElement xmlElement, - ref SecondaryDatabaseConfig secDBConfig, - bool compulsory) - { - Configuration.ConfigBool(xmlElement, "ImmutableKey", - ref secDBConfig.ImmutableKey, compulsory); - Configuration.ConfigBool(xmlElement, "Populate", - ref secDBConfig.Populate, compulsory); - } - - public static void Confirm(XmlElement xmlElement, - SecondaryDatabaseConfig secDBConfig, - bool compulsory) - { - Configuration.ConfirmBool(xmlElement, "ImmutableKey", - secDBConfig.ImmutableKey, compulsory); - Configuration.ConfirmBool(xmlElement, "Populate", - secDBConfig.Populate, compulsory); - } - } -} diff --git a/test/csharp/SecondaryDatabaseTest.cs b/test/csharp/SecondaryDatabaseTest.cs deleted file mode 100644 index 9fbf14e7c..000000000 --- a/test/csharp/SecondaryDatabaseTest.cs +++ /dev/null @@ -1,792 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Threading; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class SecondaryDatabaseTest : CSharpTestFixture - { - - [TestFixtureSetUp] - public void SetUpTestFixture() - { - testFixtureName = "SecondaryDatabaseTest"; - base.SetUpTestfixture(); - } - - [Test] - public void TestDeleteMultiple() - { - testName = "TestDeleteMultiple"; - SetUpTest(true); - DeleteMultipleAndMultipleKey( - testHome + "/" + testName + ".db", - DatabaseType.BTREE.ToString(), - DatabaseType.BTREE, false); - DeleteMultipleAndMultipleKey( - testHome + "/" + testName + ".db", - DatabaseType.HASH.ToString(), - DatabaseType.HASH, false); - DeleteMultipleAndMultipleKey( - testHome + "/" + testName + ".db", - DatabaseType.QUEUE.ToString(), - DatabaseType.QUEUE, false); - DeleteMultipleAndMultipleKey( - testHome + "/" + testName + ".db", - DatabaseType.RECNO.ToString(), - DatabaseType.RECNO, false); - } - - [Test] - public void TestDeleteMultipleKey() - { - testName = "TestDeleteMultipleKey"; - SetUpTest(true); - DeleteMultipleAndMultipleKey( - testHome + "/" + testName + ".db", - DatabaseType.BTREE.ToString(), - DatabaseType.BTREE, true); - DeleteMultipleAndMultipleKey( - testHome + "/" + testName + ".db", - DatabaseType.HASH.ToString(), - DatabaseType.HASH, true); - DeleteMultipleAndMultipleKey( - testHome + "/" + testName + ".db", - DatabaseType.QUEUE.ToString(), - DatabaseType.QUEUE, true); - DeleteMultipleAndMultipleKey( - testHome + "/" + testName + ".db", - DatabaseType.RECNO.ToString(), - DatabaseType.RECNO, true); - } - - private void DeleteMultipleAndMultipleKey(string dbFileName, - string dbName, DatabaseType type, bool mulKey) - { - List kList = new List(); - List rList = new List(); - List> pList = - new List>(); - DatabaseEntry key; - Database db; - SecondaryDatabase secDb; - - Configuration.ClearDir(testHome); - - if (type == DatabaseType.BTREE) { - BTreeDatabaseConfig dbConfig = - new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - db = BTreeDatabase.Open( - dbFileName, dbName, dbConfig); - SecondaryBTreeDatabaseConfig secDbConfig = - new SecondaryBTreeDatabaseConfig(db, null); - secDbConfig.Creation = CreatePolicy.IF_NEEDED; - secDbConfig.Duplicates = DuplicatesPolicy.SORTED; - secDbConfig.KeyGen = - new SecondaryKeyGenDelegate(SecondaryKeyGen); - secDb = SecondaryBTreeDatabase.Open( - dbFileName, dbName + "_sec", secDbConfig); - } else if (type == DatabaseType.HASH) { - HashDatabaseConfig dbConfig = - new HashDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - db = HashDatabase.Open( - dbFileName, dbName, dbConfig); - SecondaryHashDatabaseConfig secDbConfig = - new SecondaryHashDatabaseConfig(db, null); - secDbConfig.Creation = CreatePolicy.IF_NEEDED; - secDbConfig.Duplicates = DuplicatesPolicy.SORTED; - secDbConfig.KeyGen = - new SecondaryKeyGenDelegate(SecondaryKeyGen); - secDb = SecondaryHashDatabase.Open( - dbFileName, dbName + "_sec", secDbConfig); - } else if (type == DatabaseType.QUEUE) { - QueueDatabaseConfig dbConfig = - new QueueDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Length = 4; - db = QueueDatabase.Open(dbFileName, dbConfig); - SecondaryQueueDatabaseConfig secDbConfig = - new SecondaryQueueDatabaseConfig(db, null); - secDbConfig.Creation = CreatePolicy.IF_NEEDED; - secDbConfig.Length = 4; - secDbConfig.KeyGen = - new SecondaryKeyGenDelegate(SecondaryKeyGen); - secDb = SecondaryQueueDatabase.Open( - dbFileName + "_sec", secDbConfig); - } else if (type == DatabaseType.RECNO) { - RecnoDatabaseConfig dbConfig = - new RecnoDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - db = RecnoDatabase.Open( - dbFileName, dbName, dbConfig); - SecondaryRecnoDatabaseConfig secDbConfig = - new SecondaryRecnoDatabaseConfig(db, null); - secDbConfig.Creation = CreatePolicy.IF_NEEDED; - secDbConfig.KeyGen = - new SecondaryKeyGenDelegate(SecondaryKeyGen); - secDb = SecondaryRecnoDatabase.Open( - dbFileName, dbName + "_sec", secDbConfig); - } else - throw new TestException(); - - for (uint i = 1; i <= 100; i++) { - key = new DatabaseEntry( - BitConverter.GetBytes(i)); - if (i >= 50 && i < 60) - kList.Add(key); - else if (i > 80) - pList.Add(new KeyValuePair< - DatabaseEntry, DatabaseEntry>( - key, key)); - else if (type == DatabaseType.QUEUE - || type == DatabaseType.RECNO) - rList.Add(i); - - db.Put(key, key); - } - - if (mulKey) { - // Create bulk buffer for key/value pairs. - MultipleKeyDatabaseEntry pBuff; - if (type == DatabaseType.BTREE) - pBuff = new MultipleKeyDatabaseEntry( - pList, false); - else if (type == DatabaseType.HASH) - pBuff = new MultipleKeyDatabaseEntry( - pList, false); - else if (type == DatabaseType.QUEUE) - pBuff = new MultipleKeyDatabaseEntry( - pList, true); - else - pBuff = new MultipleKeyDatabaseEntry( - pList, true); - - // Bulk delete with the key/value pair bulk buffer. - secDb.Delete(pBuff); - foreach (KeyValuePairpair in pList) { - try { - db.GetBoth(pair.Key, pair.Value); - throw new TestException(); - } catch (NotFoundException e1) { - if (type == DatabaseType.QUEUE) - throw e1; - } catch (KeyEmptyException e2) { - if (type == DatabaseType.BTREE || - type == DatabaseType.HASH || - type == DatabaseType.RECNO) - throw e2; - } - } - - /* - * Dump the database to verify that 80 records - * remain after bulk delete. - */ - Assert.AreEqual(80, db.Truncate()); - } else { - // Create bulk buffer for key. - MultipleDatabaseEntry kBuff; - if (type == DatabaseType.BTREE) - kBuff = new MultipleDatabaseEntry( - kList, false); - else if (type == DatabaseType.HASH) - kBuff = new MultipleDatabaseEntry( - kList, false); - else if (type == DatabaseType.QUEUE) - kBuff = new MultipleDatabaseEntry( - kList, true); - else - kBuff = new MultipleDatabaseEntry( - kList, true); - - /* - * Bulk delete in secondary database with key - * buffer. Primary records that the deleted - * records in secondar database should be - * deleted as well. - */ - secDb.Delete(kBuff); - foreach (DatabaseEntry dbt in kList) { - try { - db.Get(dbt); - throw new TestException(); - } catch (NotFoundException e1) { - if (type == DatabaseType.QUEUE || - type == DatabaseType.RECNO) - throw e1; - } catch (KeyEmptyException e2) { - if (type == DatabaseType.BTREE || - type == DatabaseType.HASH) - throw e2; - } - } - - /* - * Bulk delete in secondary database with recno - * based key buffer. - */ - if (type == DatabaseType.QUEUE || - type == DatabaseType.RECNO) { - MultipleDatabaseEntry rBuff = - new MultipleDatabaseEntry(rList); - secDb.Delete(rBuff); - Assert.AreEqual(20, db.Truncate()); - } - } - - secDb.Close(); - db.Close(); - } - - [Test] - public void TestKeyGen() - { - testName = "TestKeyGen"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - - // Open primary database. - BTreeDatabaseConfig primaryDBConfig = - new BTreeDatabaseConfig(); - primaryDBConfig.Creation = CreatePolicy.IF_NEEDED; - BTreeDatabase primaryDB = - BTreeDatabase.Open(dbFileName, primaryDBConfig); - - // Open secondary database. - SecondaryBTreeDatabaseConfig secDBConfig = - new SecondaryBTreeDatabaseConfig(primaryDB, - new SecondaryKeyGenDelegate(SecondaryKeyGen)); - SecondaryBTreeDatabase secDB = - SecondaryBTreeDatabase.Open(dbFileName, - secDBConfig); - - primaryDB.Put(new DatabaseEntry( - BitConverter.GetBytes((int)1)), - new DatabaseEntry(BitConverter.GetBytes((int)11))); - - KeyValuePair pair; - pair = secDB.Get(new DatabaseEntry( - BitConverter.GetBytes((int)11))); - Assert.IsNotNull(pair.Value); - - // Close secondary database. - secDB.Close(); - - // Close primary database. - primaryDB.Close(); - } - - [Test] - public void TestMultiKeyGen() { - testName = "TestPrefixCompare"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - - // Open a primary btree database. - BTreeDatabaseConfig btreeDBConfig = - new BTreeDatabaseConfig(); - btreeDBConfig.Creation = CreatePolicy.ALWAYS; - BTreeDatabase btreeDB = BTreeDatabase.Open( - dbFileName, btreeDBConfig); - - // Open a secondary btree database. - SecondaryBTreeDatabaseConfig secBtreeDBConfig = - new SecondaryBTreeDatabaseConfig(btreeDB, null); - secBtreeDBConfig.Primary = btreeDB; - secBtreeDBConfig.KeyGen = - new SecondaryKeyGenDelegate( - MultipleKeyGen); - SecondaryBTreeDatabase secDB = - SecondaryBTreeDatabase.Open( - dbFileName, secBtreeDBConfig); - - /* Check that multiple secondary keys work */ - DatabaseEntry key, data; - String keyStr = "key"; - key = new DatabaseEntry(); - Configuration.dbtFromString(key, keyStr); - - data = new DatabaseEntry(); - String[] dataStrs = { "abc", "def", "ghi", "jkl" }; - String dataStr = String.Join(",", dataStrs); - Configuration.dbtFromString(data, dataStr); - btreeDB.Put(key, data); - - foreach (String skeyStr in dataStrs) { - DatabaseEntry skey = new DatabaseEntry(); - Configuration.dbtFromString(skey, skeyStr); - Assert.IsTrue(secDB.Exists(skey)); - } - - /* - * Check that a single secondary key, returned in a - * MultipleDatabaseEntry works. - */ - keyStr = "key2"; - key = new DatabaseEntry(); - Configuration.dbtFromString(key, keyStr); - - data = new DatabaseEntry(); - dataStrs = new string[1]{ "abcdefghijkl" }; - dataStr = String.Join(",", dataStrs); - Configuration.dbtFromString(data, dataStr); - btreeDB.Put(key, data); - - // Check that secondary keys work - foreach (String skeyStr in dataStrs) { - DatabaseEntry skey = new DatabaseEntry(); - Configuration.dbtFromString(skey, skeyStr); - Assert.IsTrue(secDB.Exists(skey)); - } - } - - public DatabaseEntry MultipleKeyGen(DatabaseEntry key, DatabaseEntry data) { - LinkedList skeys = new LinkedList(); - String dataStr = Configuration.strFromDBT(data); - foreach (String s in dataStr.Split(',')) { - DatabaseEntry tmp = new DatabaseEntry(); - Configuration.dbtFromString(tmp, s); - skeys.AddLast(tmp); - } - return new MultipleDatabaseEntry(skeys, false); - } - - [Test] - public void TestSecondaryCursor() - { - testName = "TestSecondaryCursor"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - - // Open primary database. - BTreeDatabaseConfig primaryDBConfig = - new BTreeDatabaseConfig(); - primaryDBConfig.Creation = CreatePolicy.IF_NEEDED; - BTreeDatabase primaryDB = - BTreeDatabase.Open(dbFileName, primaryDBConfig); - - // Open secondary database. - SecondaryBTreeDatabaseConfig secDBConfig = - new SecondaryBTreeDatabaseConfig(primaryDB, - new SecondaryKeyGenDelegate(SecondaryKeyGen)); - SecondaryBTreeDatabase secDB = - SecondaryBTreeDatabase.Open(dbFileName, - secDBConfig); - - primaryDB.Put(new DatabaseEntry( - BitConverter.GetBytes((int)1)), - new DatabaseEntry(BitConverter.GetBytes((int)11))); - - SecondaryCursor cursor = secDB.SecondaryCursor(); - cursor.Move(new DatabaseEntry( - BitConverter.GetBytes((int)11)), true); - Assert.AreEqual(BitConverter.GetBytes((int)11), - cursor.Current.Key.Data); - - // Close the cursor. - cursor.Close(); - - // Close secondary database. - secDB.Close(); - - // Close primary database. - primaryDB.Close(); - } - - [Test] - public void TestSecondaryCursorWithConfig() - { - testName = "TestSecondaryCursorWithConfig"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - - BTreeDatabase db; - SecondaryBTreeDatabase secDB; - OpenPrimaryAndSecondaryDB(dbFileName, out db, out secDB); - - for (int i = 0; i < 10; i++) - db.Put(new DatabaseEntry(BitConverter.GetBytes(i)), - new DatabaseEntry(BitConverter.GetBytes((int)i))); - - CursorConfig cursorConfig = new CursorConfig(); - cursorConfig.WriteCursor = false; - SecondaryCursor cursor = - secDB.SecondaryCursor(cursorConfig); - - cursor.Move(new DatabaseEntry( - BitConverter.GetBytes((int)5)), true); - - Assert.AreEqual(1, cursor.Count()); - - // Close the cursor. - cursor.Close(); - - // Close secondary database. - secDB.Close(); - - // Close primary database. - db.Close(); - } - - [Test] - public void TestSecondaryCursorWithTxn() - { - testName = "TestSecondaryCursorWithTxn"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - - GetSecondaryCursurWithTxn(testHome, testName, false); - } - - [Test] - public void TestSecondaryCursorWithConfigAndTxn() - { - testName = "TestSecondaryCursorWithConfigAndTxn"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - - GetSecondaryCursurWithTxn(testHome, testName, true); - } - - public void GetSecondaryCursurWithTxn(string home, - string name, bool ifCfg) - { - string dbFileName = name + ".db"; - SecondaryCursor cursor; - - // Open env. - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseTxns = true; - envConfig.UseMPool = true; - DatabaseEnvironment env = DatabaseEnvironment.Open(home, - envConfig); - - // Open primary/secondary database. - Transaction txn = env.BeginTransaction(); - BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Env = env; - BTreeDatabase db = BTreeDatabase.Open(dbFileName, - dbConfig, txn); - - SecondaryBTreeDatabaseConfig secDBConfig = new - SecondaryBTreeDatabaseConfig(db, - new SecondaryKeyGenDelegate(SecondaryKeyGen)); - secDBConfig.Env = env; - SecondaryBTreeDatabase secDB = - SecondaryBTreeDatabase.Open(dbFileName, - secDBConfig, txn); - - for (int i = 0; i < 10; i++) - db.Put(new DatabaseEntry(BitConverter.GetBytes(i)), - new DatabaseEntry(BitConverter.GetBytes((int)i)), txn); - - // Create secondary cursor. - if (ifCfg == false) - secDB.SecondaryCursor(txn); - else if (ifCfg == true) - { - CursorConfig cursorConfig = new CursorConfig(); - cursorConfig.WriteCursor = false; - cursor = secDB.SecondaryCursor(cursorConfig, txn); - cursor.Close(); - } - - secDB.Close(); - db.Close(); - txn.Commit(); - env.Close(); - } - - [Test] - public void TestOpen() - { - testName = "TestOpen"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - string dbSecFileName = testHome + "/" + - testName + "_sec.db"; - - OpenSecQueueDB(dbFileName, dbSecFileName, false); - } - - [Test] - public void TestOpenWithDBName() - { - testName = "TestOpenWithDBName"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - string dbSecFileName = testHome + "/" + - testName + "_sec.db"; - - OpenSecQueueDB(dbFileName, dbSecFileName, true); - } - - public void OpenSecQueueDB(string dbFileName, - string dbSecFileName, bool ifDBName) - { - // Open a primary btree database. - BTreeDatabaseConfig primaryDBConfig = - new BTreeDatabaseConfig(); - primaryDBConfig.Creation = CreatePolicy.IF_NEEDED; - BTreeDatabase primaryDB; - - /* - * If secondary database name is given, the primary - * database is also opened with database name. - */ - if (ifDBName == false) - primaryDB = BTreeDatabase.Open(dbFileName, - primaryDBConfig); - else - primaryDB = BTreeDatabase.Open(dbFileName, - "primary", primaryDBConfig); - - try - { - // Open a new secondary database. - SecondaryBTreeDatabaseConfig secBTDBConfig = - new SecondaryBTreeDatabaseConfig( - primaryDB, null); - secBTDBConfig.Creation = - CreatePolicy.IF_NEEDED; - - SecondaryBTreeDatabase secBTDB; - if (ifDBName == false) - secBTDB = SecondaryBTreeDatabase.Open( - dbSecFileName, secBTDBConfig); - else - secBTDB = SecondaryBTreeDatabase.Open( - dbSecFileName, "secondary", - secBTDBConfig); - - // Close the secondary database. - secBTDB.Close(); - - // Open the existing secondary database. - SecondaryDatabaseConfig secDBConfig = - new SecondaryDatabaseConfig( - primaryDB, null); - - SecondaryDatabase secDB; - if (ifDBName == false) - secDB = SecondaryBTreeDatabase.Open( - dbSecFileName, secDBConfig); - else - secDB = SecondaryBTreeDatabase.Open( - dbSecFileName, "secondary", secDBConfig); - - // Close secondary database. - secDB.Close(); - } - catch (DatabaseException) - { - throw new TestException(); - } - finally - { - // Close primary database. - primaryDB.Close(); - } - } - - [Test] - public void TestOpenWithinTxn() - { - testName = "TestOpenWithinTxn"; - SetUpTest(true); - string dbFileName = testName + ".db"; - string dbSecFileName = testName + "_sec.db"; - - OpenSecDBWithinTxn(testHome, dbFileName, - dbSecFileName, false); - } - - [Test] - public void TestOpenDBNameWithinTxn() - { - testName = "TestOpenDBNameWithinTxn"; - SetUpTest(true); - string dbFileName = testName + ".db"; - string dbSecFileName = testName + "_sec.db"; - - OpenSecDBWithinTxn(testHome, dbFileName, - dbSecFileName, true); - } - - public void OpenSecDBWithinTxn(string home, - string dbFileName, string dbSecFileName, bool ifDbName) - { - // Open an environment. - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseTxns = true; - envConfig.UseMPool = true; - envConfig.UseLogging = true; - envConfig.UseLocking = true; - DatabaseEnvironment env = DatabaseEnvironment.Open( - home, envConfig); - - // Open a primary btree database. - Transaction openDBTxn = env.BeginTransaction(); - BTreeDatabaseConfig dbConfig = - new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Env = env; - BTreeDatabase db = BTreeDatabase.Open( - dbFileName, dbConfig, openDBTxn); - - // Open a secondary btree database. - SecondaryBTreeDatabaseConfig secDBConfig = - new SecondaryBTreeDatabaseConfig(db, - new SecondaryKeyGenDelegate(SecondaryKeyGen)); - secDBConfig.Env = env; - secDBConfig.Creation = CreatePolicy.IF_NEEDED; - - SecondaryBTreeDatabase secDB; - if (ifDbName == false) - secDB = SecondaryBTreeDatabase.Open( - dbSecFileName, secDBConfig, openDBTxn); - else - secDB = SecondaryBTreeDatabase.Open( - dbSecFileName, "secondary", secDBConfig, - openDBTxn); - openDBTxn.Commit(); - secDB.Close(); - - // Open the existing secondary database. - Transaction secTxn = env.BeginTransaction(); - SecondaryDatabaseConfig secConfig = - new SecondaryDatabaseConfig(db, - new SecondaryKeyGenDelegate(SecondaryKeyGen)); - secConfig.Env = env; - - SecondaryDatabase secExDB; - if (ifDbName == false) - secExDB = SecondaryBTreeDatabase.Open( - dbSecFileName, secConfig, secTxn); - else - secExDB = SecondaryBTreeDatabase.Open( - dbSecFileName, "secondary", secConfig, - secTxn); - secExDB.Close(); - secTxn.Commit(); - - db.Close(); - env.Close(); - } - - public void OpenPrimaryAndSecondaryDB(string dbFileName, - out BTreeDatabase primaryDB, - out SecondaryBTreeDatabase secDB) - { - // Open primary database. - BTreeDatabaseConfig primaryDBConfig = - new BTreeDatabaseConfig(); - primaryDBConfig.Creation = CreatePolicy.IF_NEEDED; - primaryDB = - BTreeDatabase.Open(dbFileName, primaryDBConfig); - - // Open secondary database. - SecondaryBTreeDatabaseConfig secDBConfig = - new SecondaryBTreeDatabaseConfig(primaryDB, - new SecondaryKeyGenDelegate(SecondaryKeyGen)); - secDB = SecondaryBTreeDatabase.Open(dbFileName, - secDBConfig); - } - - [Test, ExpectedException(typeof(ExpectedTestException))] - public void TestBadSecondaryException() - { - testName = "TestBadSecondaryException"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - string secDBFileName = testHome + "/" + - testName + "_sec.db"; - - // Open primary database. - BTreeDatabaseConfig btreeDBConfig = - new BTreeDatabaseConfig(); - btreeDBConfig.Creation = CreatePolicy.IF_NEEDED; - BTreeDatabase btreeDB = - BTreeDatabase.Open(dbFileName, btreeDBConfig); - - // Open secondary database. - SecondaryBTreeDatabaseConfig secBtDbConfig = - new SecondaryBTreeDatabaseConfig(btreeDB, - new SecondaryKeyGenDelegate(SecondaryKeyGen)); - secBtDbConfig.Creation = CreatePolicy.IF_NEEDED; - SecondaryBTreeDatabase secBtDb = - SecondaryBTreeDatabase.Open(secDBFileName, - secBtDbConfig); - - // Put some data into primary database. - for (int i = 0; i < 10; i++) - btreeDB.Put(new DatabaseEntry( - BitConverter.GetBytes(i)), - new DatabaseEntry(BitConverter.GetBytes(i))); - - // Close the secondary database. - secBtDb.Close(); - - // Delete record(5, 5) in primary database. - btreeDB.Delete(new DatabaseEntry( - BitConverter.GetBytes((int)5))); - - // Reopen the secondary database. - SecondaryDatabase secDB = SecondaryDatabase.Open( - secDBFileName, - new SecondaryDatabaseConfig(btreeDB, - new SecondaryKeyGenDelegate(SecondaryKeyGen))); - - /* - * Getting record(5, 5) by secondary database should - * throw BadSecondaryException since it has been - * deleted in the primary database. - */ - try - { - secDB.Exists(new DatabaseEntry( - BitConverter.GetBytes((int)5))); - } - catch (BadSecondaryException) - { - throw new ExpectedTestException(); - } - finally - { - secDB.Close(); - btreeDB.Close(); - } - } - - public DatabaseEntry SecondaryKeyGen( - DatabaseEntry key, DatabaseEntry data) - { - DatabaseEntry dbtGen; - dbtGen = new DatabaseEntry(data.Data); - return dbtGen; - } - - } - -} \ No newline at end of file diff --git a/test/csharp/SecondaryHashDatabaseConfigTest.cs b/test/csharp/SecondaryHashDatabaseConfigTest.cs deleted file mode 100644 index 139b4d591..000000000 --- a/test/csharp/SecondaryHashDatabaseConfigTest.cs +++ /dev/null @@ -1,102 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Threading; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class SecondaryHashDatabaseConfigTest : CSharpTestFixture - { - - [TestFixtureSetUp] - public void SetUpTestFixture() - { - testFixtureName = "SecondaryHashDatabaseConfigTest"; - base.SetUpTestfixture(); - } - - [Test] - public void TestConfig() - { - testName = "TestConfig"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - - XmlElement xmlElem = Configuration.TestSetUp( - testFixtureName, testName); - // Open a primary btree database. - HashDatabaseConfig hashDBConfig = - new HashDatabaseConfig(); - hashDBConfig.Creation = CreatePolicy.IF_NEEDED; - HashDatabase hashDB = HashDatabase.Open( - dbFileName, hashDBConfig); - - SecondaryHashDatabaseConfig secDBConfig = - new SecondaryHashDatabaseConfig(hashDB, null); - - Config(xmlElem, ref secDBConfig, true); - Confirm(xmlElem, secDBConfig, true); - - // Close the primary btree database. - hashDB.Close(); - } - - public static void Confirm(XmlElement xmlElement, - SecondaryHashDatabaseConfig secHashDBConfig, - bool compulsory) - { - SecondaryDatabaseConfig secDBConfig = - secHashDBConfig; - SecondaryDatabaseConfigTest.Confirm(xmlElement, - secDBConfig, compulsory); - - // Confirm secondary hash database specific configuration. - Configuration.ConfirmCreatePolicy(xmlElement, - "Creation", secHashDBConfig.Creation, compulsory); - Configuration.ConfirmDuplicatesPolicy(xmlElement, - "Duplicates", secHashDBConfig.Duplicates, compulsory); - Configuration.ConfirmUint(xmlElement, "FillFactor", - secHashDBConfig.FillFactor, compulsory); - Configuration.ConfirmUint(xmlElement, - "NumElements", - secHashDBConfig.TableSize, compulsory); - } - - public static void Config(XmlElement xmlElement, - ref SecondaryHashDatabaseConfig secHashDBConfig, - bool compulsory) - { - uint fillFactor = new uint(); - uint numElements = new uint(); - - SecondaryDatabaseConfig secDBConfig = secHashDBConfig; - SecondaryDatabaseConfigTest.Config(xmlElement, - ref secDBConfig, compulsory); - - // Configure specific fields/properties of hash db - Configuration.ConfigCreatePolicy(xmlElement, - "Creation", ref secHashDBConfig.Creation, compulsory); - Configuration.ConfigDuplicatesPolicy(xmlElement, - "Duplicates", ref secHashDBConfig.Duplicates, compulsory); - if (Configuration.ConfigUint(xmlElement, "FillFactor", - ref fillFactor, compulsory)) - secHashDBConfig.FillFactor = fillFactor; - if (Configuration.ConfigUint(xmlElement, "NumElements", - ref numElements, compulsory)) - secHashDBConfig.TableSize = numElements; - } - } -} - diff --git a/test/csharp/SecondaryHashDatabaseTest.cs b/test/csharp/SecondaryHashDatabaseTest.cs deleted file mode 100644 index 24b1aa2ed..000000000 --- a/test/csharp/SecondaryHashDatabaseTest.cs +++ /dev/null @@ -1,382 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Threading; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class SecondaryHashDatabaseTest : CSharpTestFixture - { - - [TestFixtureSetUp] - public void SetUpTestFixture() - { - testFixtureName = "SecondaryHashDatabaseTest"; - base.SetUpTestfixture(); - } - - [Test] - public void TestHashFunction() - { - testName = "TestHashFunction"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - string dbSecFileName = testHome + "/" + - testName + "_sec.db"; - - // Open a primary hash database. - HashDatabaseConfig dbConfig = - new HashDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - HashDatabase hashDB = HashDatabase.Open( - dbFileName, dbConfig); - - /* - * Define hash function and open a secondary - * hash database. - */ - SecondaryHashDatabaseConfig secDBConfig = - new SecondaryHashDatabaseConfig(hashDB, null); - secDBConfig.HashFunction = - new HashFunctionDelegate(HashFunction); - secDBConfig.Creation = CreatePolicy.IF_NEEDED; - SecondaryHashDatabase secDB = - SecondaryHashDatabase.Open(dbSecFileName, - secDBConfig); - - /* - * Confirm the hash function defined in the configuration. - * Call the hash function and the one from secondary - * database. If they return the same value, then the hash - * function is configured successfully. - */ - uint data = secDB.HashFunction(BitConverter.GetBytes(1)); - Assert.AreEqual(0, data); - - // Close all. - secDB.Close(); - hashDB.Close(); - } - - public uint HashFunction(byte[] data) - { - data[0] = 0; - return BitConverter.ToUInt32(data, 0); - } - - [Test] - public void TestCompare() - { - testName = "TestCompare"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - string dbSecFileName = testHome + "/" + testName + - "_sec.db"; - - // Open a primary hash database. - HashDatabaseConfig dbConfig = - new HashDatabaseConfig(); - dbConfig.Creation = CreatePolicy.ALWAYS; - HashDatabase db = HashDatabase.Open( - dbFileName, dbConfig); - - // Open a secondary hash database. - SecondaryHashDatabaseConfig secConfig = - new SecondaryHashDatabaseConfig(null, null); - secConfig.Creation = CreatePolicy.IF_NEEDED; - secConfig.Primary = db; - secConfig.Compare = - new EntryComparisonDelegate(SecondaryEntryComparison); - SecondaryHashDatabase secDB = - SecondaryHashDatabase.Open(dbSecFileName, secConfig); - - /* - * Get the compare function set in the configuration - * and run it in a comparison to see if it is alright. - */ - DatabaseEntry dbt1, dbt2; - dbt1 = new DatabaseEntry( - BitConverter.GetBytes((int)257)); - dbt2 = new DatabaseEntry( - BitConverter.GetBytes((int)255)); - Assert.Less(0, secDB.Compare(dbt1, dbt2)); - - secDB.Close(); - db.Close(); - } - - [Test] - public void TestDuplicates() - { - testName = "TestDuplicates"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - string dbSecFileName = testHome + "/" + testName - + "_sec.db"; - - // Open a primary hash database. - HashDatabaseConfig dbConfig = - new HashDatabaseConfig(); - dbConfig.Creation = CreatePolicy.ALWAYS; - HashDatabase db = HashDatabase.Open( - dbFileName, dbConfig); - - // Open a secondary hash database. - SecondaryHashDatabaseConfig secConfig = - new SecondaryHashDatabaseConfig(null, null); - secConfig.Primary = db; - secConfig.Duplicates = DuplicatesPolicy.SORTED; - secConfig.Creation = CreatePolicy.IF_NEEDED; - SecondaryHashDatabase secDB = - SecondaryHashDatabase.Open( - dbSecFileName, secConfig); - - // Confirm the duplicate in opened secondary database. - Assert.AreEqual(DuplicatesPolicy.SORTED, - secDB.Duplicates); - - secDB.Close(); - db.Close(); - } - - /* - * Tests to all Open() share the same configuration in - * AllTestData.xml. - */ - [Test] - public void TestOpen() - { - testName = "TestOpen"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - string dbSecFileName = testHome + "/" + - testName + "_sec.db"; - - OpenSecHashDB(testFixtureName, "TestOpen", - dbFileName, dbSecFileName, false); - } - - [Test] - public void TestOpenWithDBName() - { - testName = "TestOpenWithDBName"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - string dbSecFileName = testHome + "/" + - testName + "_sec.db"; - - OpenSecHashDB(testFixtureName, "TestOpen", - dbFileName, dbSecFileName, true); - } - - public void OpenSecHashDB(string className, - string funName, string dbFileName, string dbSecFileName, - bool ifDBName) - { - XmlElement xmlElem = Configuration.TestSetUp( - className, funName); - - // Open a primary recno database. - HashDatabaseConfig primaryDBConfig = - new HashDatabaseConfig(); - primaryDBConfig.Creation = CreatePolicy.IF_NEEDED; - HashDatabase primaryDB; - - /* - * If secondary database name is given, the primary - * database is also opened with database name. - */ - if (ifDBName == false) - primaryDB = HashDatabase.Open(dbFileName, - primaryDBConfig); - else - primaryDB = HashDatabase.Open(dbFileName, - "primary", primaryDBConfig); - - try - { - // Open a new secondary database. - SecondaryHashDatabaseConfig secHashDBConfig = - new SecondaryHashDatabaseConfig( - primaryDB, null); - SecondaryHashDatabaseConfigTest.Config( - xmlElem, ref secHashDBConfig, false); - secHashDBConfig.Creation = - CreatePolicy.IF_NEEDED; - - SecondaryHashDatabase secHashDB; - if (ifDBName == false) - secHashDB = SecondaryHashDatabase.Open( - dbSecFileName, secHashDBConfig); - else - secHashDB = SecondaryHashDatabase.Open( - dbSecFileName, "secondary", - secHashDBConfig); - - // Close the secondary database. - secHashDB.Close(); - - // Open the existing secondary database. - SecondaryDatabaseConfig secDBConfig = - new SecondaryDatabaseConfig( - primaryDB, null); - - SecondaryDatabase secDB; - if (ifDBName == false) - secDB = SecondaryHashDatabase.Open( - dbSecFileName, secDBConfig); - else - secDB = SecondaryHashDatabase.Open( - dbSecFileName, "secondary", secDBConfig); - - // Close secondary database. - secDB.Close(); - } - catch (DatabaseException) - { - throw new TestException(); - } - finally - { - // Close primary database. - primaryDB.Close(); - } - } - - [Test] - public void TestOpenWithinTxn() - { - testName = "TestOpenWithinTxn"; - SetUpTest(true); - string dbFileName = testName + ".db"; - string dbSecFileName = testName + "_sec.db"; - - OpenSecHashDBWithinTxn(testFixtureName, - "TestOpen", testHome, dbFileName, - dbSecFileName, false); - } - - [Test] - public void TestOpenDBNameWithinTxn() - { - testName = "TestOpenDBNameWithinTxn"; - SetUpTest(true); - string dbFileName = testName + ".db"; - string dbSecFileName = testName + "_sec.db"; - - OpenSecHashDBWithinTxn(testFixtureName, - "TestOpen", testHome, dbFileName, - dbSecFileName, true); - } - - public void OpenSecHashDBWithinTxn(string className, - string funName, string home, string dbFileName, - string dbSecFileName, bool ifDbName) - { - XmlElement xmlElem = Configuration.TestSetUp( - className, funName); - - // Open an environment. - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseTxns = true; - envConfig.UseMPool = true; - envConfig.UseLogging = true; - DatabaseEnvironment env = DatabaseEnvironment.Open( - home, envConfig); - - // Open a primary hash database. - Transaction openDBTxn = env.BeginTransaction(); - HashDatabaseConfig dbConfig = - new HashDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Env = env; - HashDatabase db = HashDatabase.Open( - dbFileName, dbConfig, openDBTxn); - openDBTxn.Commit(); - - // Open a secondary hash database. - Transaction openSecTxn = env.BeginTransaction(); - SecondaryHashDatabaseConfig secDBConfig = - new SecondaryHashDatabaseConfig(db, - new SecondaryKeyGenDelegate(SecondaryKeyGen)); - SecondaryHashDatabaseConfigTest.Config(xmlElem, - ref secDBConfig, false); - secDBConfig.HashFunction = null; - secDBConfig.Env = env; - SecondaryHashDatabase secDB; - if (ifDbName == false) - secDB = SecondaryHashDatabase.Open( - dbSecFileName, secDBConfig, openSecTxn); - else - secDB = SecondaryHashDatabase.Open( - dbSecFileName, "secondary", secDBConfig, - openSecTxn); - openSecTxn.Commit(); - - // Confirm its flags configured in secDBConfig. - Confirm(xmlElem, secDB, true); - secDB.Close(); - - // Open the existing secondary database. - Transaction secTxn = env.BeginTransaction(); - SecondaryDatabaseConfig secConfig = - new SecondaryDatabaseConfig(db, - new SecondaryKeyGenDelegate(SecondaryKeyGen)); - secConfig.Env = env; - - SecondaryDatabase secExDB; - if (ifDbName == false) - secExDB = SecondaryHashDatabase.Open( - dbSecFileName, secConfig, secTxn); - else - secExDB = SecondaryHashDatabase.Open( - dbSecFileName, "secondary", secConfig, - secTxn); - secExDB.Close(); - secTxn.Commit(); - - db.Close(); - env.Close(); - } - - public DatabaseEntry SecondaryKeyGen( - DatabaseEntry key, DatabaseEntry data) - { - DatabaseEntry dbtGen; - dbtGen = new DatabaseEntry(data.Data); - return dbtGen; - } - - public int SecondaryEntryComparison( - DatabaseEntry dbt1, DatabaseEntry dbt2) - { - int a, b; - a = BitConverter.ToInt32(dbt1.Data, 0); - b = BitConverter.ToInt32(dbt2.Data, 0); - return a - b; - } - - public static void Confirm(XmlElement xmlElem, - SecondaryHashDatabase secDB, bool compulsory) - { - Configuration.ConfirmUint(xmlElem, "FillFactor", - secDB.FillFactor, compulsory); - Configuration.ConfirmUint(xmlElem, "NumElements", - secDB.TableSize, compulsory); - } - } -} diff --git a/test/csharp/SecondaryQueueDatabaseConfigTest.cs b/test/csharp/SecondaryQueueDatabaseConfigTest.cs deleted file mode 100644 index d97733ff0..000000000 --- a/test/csharp/SecondaryQueueDatabaseConfigTest.cs +++ /dev/null @@ -1,102 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Threading; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class SecondaryQueueDatabaseConfigTest : CSharpTestFixture - { - - [TestFixtureSetUp] - public void SetUpTestFixture() - { - testFixtureName = "SecondaryQueueDatabaseConfigTest"; - base.SetUpTestfixture(); - } - - [Test] - public void TestConfig() - { - testName = "TestConfig"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - - XmlElement xmlElem = Configuration.TestSetUp( - testFixtureName, testName); - - // Open a primary btree database. - QueueDatabaseConfig queueDBConfig = - new QueueDatabaseConfig(); - queueDBConfig.Creation = CreatePolicy.IF_NEEDED; - QueueDatabase queueDB = QueueDatabase.Open( - dbFileName, queueDBConfig); - - SecondaryQueueDatabaseConfig secDBConfig = - new SecondaryQueueDatabaseConfig(queueDB, null); - - Config(xmlElem, ref secDBConfig, true); - Confirm(xmlElem, secDBConfig, true); - - // Close the primary btree database. - queueDB.Close(); - } - - public static void Confirm(XmlElement xmlElement, - SecondaryQueueDatabaseConfig secQueueDBConfig, - bool compulsory) - { - SecondaryDatabaseConfig secDBConfig = - secQueueDBConfig; - SecondaryDatabaseConfigTest.Confirm(xmlElement, - secDBConfig, compulsory); - - // Confirm secondary hash database specific configuration. - Configuration.ConfirmCreatePolicy(xmlElement, - "Creation", secQueueDBConfig.Creation, compulsory); - Configuration.ConfirmUint(xmlElement, - "ExtentSize", secQueueDBConfig.ExtentSize, compulsory); - Configuration.ConfirmUint(xmlElement, "Length", - secQueueDBConfig.Length, compulsory); - Configuration.ConfirmInt(xmlElement, "PadByte", - secQueueDBConfig.PadByte, compulsory); - } - - public static void Config(XmlElement xmlElement, - ref SecondaryQueueDatabaseConfig secQueueDBConfig, - bool compulsory) - { - uint uintValue = new uint(); - int intValue = new int(); - SecondaryDatabaseConfig secConfig = secQueueDBConfig; - SecondaryDatabaseConfigTest.Config(xmlElement, - ref secConfig, compulsory); - - // Configure specific fields/properties of Queue database - Configuration.ConfigCreatePolicy(xmlElement, "Creation", - ref secQueueDBConfig.Creation, compulsory); - if (Configuration.ConfigUint(xmlElement, "Length", - ref uintValue, compulsory)) - secQueueDBConfig.Length = uintValue; - if (Configuration.ConfigInt(xmlElement, "PadByte", - ref intValue, compulsory)) - secQueueDBConfig.PadByte = intValue; - if (Configuration.ConfigUint(xmlElement, "ExtentSize", - ref uintValue, compulsory)) - secQueueDBConfig.ExtentSize = uintValue; - } - } -} - diff --git a/test/csharp/SecondaryQueueDatabaseTest.cs b/test/csharp/SecondaryQueueDatabaseTest.cs deleted file mode 100644 index 803cf380a..000000000 --- a/test/csharp/SecondaryQueueDatabaseTest.cs +++ /dev/null @@ -1,215 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Threading; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class SecondaryQueueDatabaseTest : CSharpTestFixture - { - - [TestFixtureSetUp] - public void SetUpTestFixture() - { - testFixtureName = "SecondaryQueueDatabaseTest"; - base.SetUpTestfixture(); - } - - /* - * Tests to all Open() share the same configuration in - * AllTestData.xml. - */ - [Test] - public void TestOpen() - { - testName = "TestOpen"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - string dbSecFileName = testHome + "/" + - testName + "_sec.db"; - - OpenSecQueueDB(testFixtureName, "TestOpen", - dbFileName, dbSecFileName); - } - - public void OpenSecQueueDB(string className, - string funName, string dbFileName, string dbSecFileName) - { - XmlElement xmlElem = Configuration.TestSetUp( - className, funName); - - // Open a primary queue database. - QueueDatabaseConfig primaryDBConfig = - new QueueDatabaseConfig(); - primaryDBConfig.Creation = CreatePolicy.IF_NEEDED; - QueueDatabase primaryDB; - - /* - * If secondary database name is given, the primary - * database is also opened with database name. - */ - primaryDB = QueueDatabase.Open(dbFileName, - primaryDBConfig); - - try - { - // Open a new secondary database. - SecondaryQueueDatabaseConfig secQueueDBConfig = - new SecondaryQueueDatabaseConfig( - primaryDB, null); - SecondaryQueueDatabaseConfigTest.Config( - xmlElem, ref secQueueDBConfig, false); - secQueueDBConfig.Creation = - CreatePolicy.IF_NEEDED; - - SecondaryQueueDatabase secQueueDB; - secQueueDB = SecondaryQueueDatabase.Open( - dbSecFileName, secQueueDBConfig); - - // Close the secondary database. - secQueueDB.Close(); - - // Open the existing secondary database. - SecondaryDatabaseConfig secDBConfig = - new SecondaryQueueDatabaseConfig( - primaryDB, null); - - SecondaryDatabase secDB; - secDB = SecondaryQueueDatabase.Open( - dbSecFileName, secDBConfig); - - // Close secondary database. - secDB.Close(); - } - catch (DatabaseException) - { - throw new TestException(); - } - finally - { - // Close primary database. - primaryDB.Close(); - } - } - - [Test] - public void TestOpenWithinTxn() - { - testName = "TestOpenWithinTxn"; - SetUpTest(true); - string dbFileName = testName + ".db"; - string dbSecFileName = testName + "_sec.db"; - - OpenSecQueueDBWithinTxn(testFixtureName, - "TestOpen", testHome, dbFileName, - dbSecFileName); - } - - [Test] - public void TestOpenDBNameWithinTxn() - { - testName = "TestOpenDBNameWithinTxn"; - SetUpTest(true); - string dbFileName = testName + ".db"; - string dbSecFileName = testName + "_sec.db"; - - OpenSecQueueDBWithinTxn(testFixtureName, - "TestOpen", testHome, dbFileName, - dbSecFileName); - } - - public void OpenSecQueueDBWithinTxn(string className, - string funName, string home, string dbFileName, - string dbSecFileName) - { - XmlElement xmlElem = Configuration.TestSetUp( - className, funName); - - // Open an environment. - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseTxns = true; - envConfig.UseMPool = true; - envConfig.UseLogging = true; - DatabaseEnvironment env = DatabaseEnvironment.Open( - home, envConfig); - - // Open a primary queue database. - Transaction openDBTxn = env.BeginTransaction(); - QueueDatabaseConfig dbConfig = - new QueueDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Env = env; - QueueDatabase db = QueueDatabase.Open( - dbFileName, dbConfig, openDBTxn); - openDBTxn.Commit(); - - // Open a secondary queue database. - Transaction openSecTxn = env.BeginTransaction(); - SecondaryQueueDatabaseConfig secDBConfig = - new SecondaryQueueDatabaseConfig(db, - new SecondaryKeyGenDelegate(SecondaryKeyGen)); - SecondaryQueueDatabaseConfigTest.Config(xmlElem, - ref secDBConfig, true); - secDBConfig.Env = env; - SecondaryQueueDatabase secDB; - secDB = SecondaryQueueDatabase.Open( - dbSecFileName, secDBConfig, openSecTxn); - - openSecTxn.Commit(); - - // Confirm its flags configured in secDBConfig. - Confirm(xmlElem, secDB, true); - secDB.Close(); - - // Open the existing secondary database. - Transaction secTxn = env.BeginTransaction(); - SecondaryDatabaseConfig secConfig = - new SecondaryDatabaseConfig(db, - new SecondaryKeyGenDelegate(SecondaryKeyGen)); - secConfig.Env = env; - - SecondaryDatabase secExDB; - secExDB = SecondaryQueueDatabase.Open( - dbSecFileName, secConfig, secTxn); - - secExDB.Close(); - secTxn.Commit(); - - db.Close(); - env.Close(); - } - - public DatabaseEntry SecondaryKeyGen( - DatabaseEntry key, DatabaseEntry data) - { - DatabaseEntry dbtGen; - dbtGen = new DatabaseEntry(data.Data); - return dbtGen; - } - - public static void Confirm(XmlElement xmlElem, - SecondaryQueueDatabase secDB, bool compulsory) - { - Configuration.ConfirmUint(xmlElem, - "ExtentSize", secDB.ExtentSize, compulsory); - Configuration.ConfirmUint(xmlElem, "Length", - secDB.Length, compulsory); - Configuration.ConfirmInt(xmlElem, "PadByte", - secDB.PadByte, compulsory); - } - } -} diff --git a/test/csharp/SecondaryRecnoDatabaseConfigTest.cs b/test/csharp/SecondaryRecnoDatabaseConfigTest.cs deleted file mode 100644 index 2e32d5543..000000000 --- a/test/csharp/SecondaryRecnoDatabaseConfigTest.cs +++ /dev/null @@ -1,112 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Threading; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class SecondaryRecnoDatabaseConfigTest : CSharpTestFixture - { - - [TestFixtureSetUp] - public void SetUpTestFixture() - { - testFixtureName = "SecondaryRecnoDatabaseConfigTest"; - base.SetUpTestfixture(); - } - - [Test] - public void TestConfig() - { - testName = "TestConfig"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - - XmlElement xmlElem = Configuration.TestSetUp( - testFixtureName, testName); - - // Open a primary btree database. - RecnoDatabaseConfig recDBConfig = - new RecnoDatabaseConfig(); - recDBConfig.Creation = CreatePolicy.IF_NEEDED; - RecnoDatabase recDB = RecnoDatabase.Open( - dbFileName, recDBConfig); - - SecondaryRecnoDatabaseConfig secDBConfig = - new SecondaryRecnoDatabaseConfig(recDB, null); - - Config(xmlElem, ref secDBConfig, true); - Confirm(xmlElem, secDBConfig, true); - - // Close the primary btree database. - recDB.Close(); - } - - public static void Confirm(XmlElement xmlElement, - SecondaryRecnoDatabaseConfig secRecDBConfig, - bool compulsory) - { - SecondaryDatabaseConfig secDBConfig = - secRecDBConfig; - SecondaryDatabaseConfigTest.Confirm(xmlElement, - secDBConfig, compulsory); - - // Confirm secondary hash database specific configuration. - Configuration.ConfirmString(xmlElement, "BackingFile", - secRecDBConfig.BackingFile, compulsory); - Configuration.ConfirmCreatePolicy(xmlElement, "Creation", - secRecDBConfig.Creation, compulsory); - Configuration.ConfirmInt(xmlElement, "Delimiter", - secRecDBConfig.Delimiter, compulsory); - Configuration.ConfirmUint(xmlElement, "Length", - secRecDBConfig.Length, compulsory); - Configuration.ConfirmInt(xmlElement, "PadByte", - secRecDBConfig.PadByte, compulsory); - Configuration.ConfirmBool(xmlElement, "Renumber", - secRecDBConfig.Renumber, compulsory); - Configuration.ConfirmBool(xmlElement, "Snapshot", - secRecDBConfig.Snapshot, compulsory); - } - - public static void Config(XmlElement xmlElement, - ref SecondaryRecnoDatabaseConfig secRecDBConfig, - bool compulsory) - { - int intValue = new int(); - uint uintValue = new uint(); - SecondaryDatabaseConfig secDBConfig = secRecDBConfig; - SecondaryDatabaseConfigTest.Config(xmlElement, - ref secDBConfig, compulsory); - - // Configure specific fields/properties of Recno database - Configuration.ConfigCreatePolicy(xmlElement, "Creation", - ref secRecDBConfig.Creation, compulsory); - if (Configuration.ConfigInt(xmlElement, "Delimiter", - ref intValue, compulsory)) - secRecDBConfig.Delimiter = intValue; - if (Configuration.ConfigUint(xmlElement, "Length", - ref uintValue, compulsory)) - secRecDBConfig.Length = uintValue; - if (Configuration.ConfigInt(xmlElement, "PadByte", - ref intValue, compulsory)) - secRecDBConfig.PadByte = intValue; - Configuration.ConfigBool(xmlElement, "Renumber", - ref secRecDBConfig.Renumber, compulsory); - Configuration.ConfigBool(xmlElement, "Snapshot", - ref secRecDBConfig.Snapshot, compulsory); - } - } -} - diff --git a/test/csharp/SecondaryRecnoDatabaseTest.cs b/test/csharp/SecondaryRecnoDatabaseTest.cs deleted file mode 100644 index f2fe1d67a..000000000 --- a/test/csharp/SecondaryRecnoDatabaseTest.cs +++ /dev/null @@ -1,255 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Threading; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class SecondaryRecnoDatabaseTest : CSharpTestFixture - { - - [TestFixtureSetUp] - public void SetUpTestFixture() - { - testFixtureName = "SecondaryRecnoDatabaseTest"; - base.SetUpTestfixture(); - } - - /* - * Tests to all Open() share the same configuration in - * AllTestData.xml. - */ - [Test] - public void TestOpen() - { - testName = "TestOpen"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - string dbSecFileName = testHome + "/" + - testName + "_sec.db"; - - OpenSecRecnoDB(testFixtureName, "TestOpen", - dbFileName, dbSecFileName, false); - } - - [Test] - public void TestOpenWithDBName() - { - testName = "TestOpenWithDBName"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - string dbSecFileName = testHome + "/" + - testName + "_sec.db"; - - OpenSecRecnoDB(testFixtureName, "TestOpen", - dbFileName, dbSecFileName, true); - } - - public void OpenSecRecnoDB(string className, - string funName, string dbFileName, string dbSecFileName, - bool ifDBName) - { - XmlElement xmlElem = Configuration.TestSetUp( - className, funName); - - // Open a primary recno database. - RecnoDatabaseConfig primaryDBConfig = - new RecnoDatabaseConfig(); - primaryDBConfig.Creation = CreatePolicy.IF_NEEDED; - RecnoDatabase primaryDB; - - /* - * If secondary database name is given, the primary - * database is also opened with database name. - */ - if (ifDBName == false) - primaryDB = RecnoDatabase.Open(dbFileName, - primaryDBConfig); - else - primaryDB = RecnoDatabase.Open(dbFileName, - "primary", primaryDBConfig); - - try - { - // Open a new secondary database. - SecondaryRecnoDatabaseConfig secRecnoDBConfig = - new SecondaryRecnoDatabaseConfig( - primaryDB, null); - SecondaryRecnoDatabaseConfigTest.Config( - xmlElem, ref secRecnoDBConfig, false); - secRecnoDBConfig.Creation = - CreatePolicy.IF_NEEDED; - - SecondaryRecnoDatabase secRecnoDB; - if (ifDBName == false) - secRecnoDB = SecondaryRecnoDatabase.Open( - dbSecFileName, secRecnoDBConfig); - else - secRecnoDB = SecondaryRecnoDatabase.Open( - dbSecFileName, "secondary", - secRecnoDBConfig); - - // Close the secondary database. - secRecnoDB.Close(); - - // Open the existing secondary database. - SecondaryDatabaseConfig secDBConfig = - new SecondaryDatabaseConfig( - primaryDB, null); - - SecondaryDatabase secDB; - if (ifDBName == false) - secDB = SecondaryRecnoDatabase.Open( - dbSecFileName, secDBConfig); - else - secDB = SecondaryRecnoDatabase.Open( - dbSecFileName, "secondary", secDBConfig); - - // Close secondary database. - secDB.Close(); - } - catch (DatabaseException) - { - throw new TestException(); - } - finally - { - // Close primary database. - primaryDB.Close(); - } - } - - [Test] - public void TestOpenWithinTxn() - { - testName = "TestOpenWithinTxn"; - SetUpTest(true); - string dbFileName = testName + ".db"; - string dbSecFileName = testName + "_sec.db"; - - OpenSecRecnoDBWithinTxn(testFixtureName, - "TestOpen", testHome, dbFileName, - dbSecFileName, false); - } - - [Test] - public void TestOpenDBNameWithinTxn() - { - testName = "TestOpenDBNameWithinTxn"; - SetUpTest(true); - string dbFileName = testName + ".db"; - string dbSecFileName = testName + "_sec.db"; - - OpenSecRecnoDBWithinTxn(testFixtureName, - "TestOpen", testHome, dbFileName, - dbSecFileName, true); - } - - public void OpenSecRecnoDBWithinTxn(string className, - string funName, string home, string dbFileName, - string dbSecFileName, bool ifDbName) - { - XmlElement xmlElem = Configuration.TestSetUp( - className, funName); - - // Open an environment. - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseTxns = true; - envConfig.UseMPool = true; - envConfig.UseLogging = true; - DatabaseEnvironment env = DatabaseEnvironment.Open( - home, envConfig); - - // Open a primary recno database. - Transaction openDBTxn = env.BeginTransaction(); - RecnoDatabaseConfig dbConfig = - new RecnoDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - - dbConfig.Env = env; - RecnoDatabase db = RecnoDatabase.Open( - dbFileName, dbConfig, openDBTxn); - openDBTxn.Commit(); - - // Open a secondary recno database. - Transaction openSecTxn = env.BeginTransaction(); - SecondaryRecnoDatabaseConfig secDBConfig = - new SecondaryRecnoDatabaseConfig(db, - new SecondaryKeyGenDelegate(SecondaryKeyGen)); - SecondaryRecnoDatabaseConfigTest.Config(xmlElem, - ref secDBConfig, false); - secDBConfig.Env = env; - SecondaryRecnoDatabase secDB; - if (ifDbName == false) - secDB = SecondaryRecnoDatabase.Open( - dbSecFileName, secDBConfig, openSecTxn); - else - secDB = SecondaryRecnoDatabase.Open( - dbSecFileName, "secondary", secDBConfig, - openSecTxn); - openSecTxn.Commit(); - - // Confirm its flags configured in secDBConfig. - Confirm(xmlElem, secDB, true); - secDB.Close(); - - // Open the existing secondary database. - Transaction secTxn = env.BeginTransaction(); - SecondaryDatabaseConfig secConfig = - new SecondaryDatabaseConfig(db, - new SecondaryKeyGenDelegate(SecondaryKeyGen)); - secConfig.Env = env; - - SecondaryDatabase secExDB; - if (ifDbName == false) - secExDB = SecondaryRecnoDatabase.Open( - dbSecFileName, secConfig, secTxn); - else - secExDB = SecondaryRecnoDatabase.Open( - dbSecFileName, "secondary", secConfig, - secTxn); - secExDB.Close(); - secTxn.Commit(); - - db.Close(); - env.Close(); - } - - public DatabaseEntry SecondaryKeyGen( - DatabaseEntry key, DatabaseEntry data) - { - DatabaseEntry dbtGen; - dbtGen = new DatabaseEntry(data.Data); - return dbtGen; - } - - public static void Confirm(XmlElement xmlElem, - SecondaryRecnoDatabase secDB, bool compulsory) - { - Configuration.ConfirmInt(xmlElem, "Delimiter", - secDB.Delimiter, compulsory); - Configuration.ConfirmUint(xmlElem, "Length", - secDB.Length, compulsory); - Configuration.ConfirmInt(xmlElem, "PadByte", - secDB.PadByte, compulsory); - Configuration.ConfirmBool(xmlElem, - "Renumber", secDB.Renumber, compulsory); - Configuration.ConfirmBool(xmlElem, "Snapshot", - secDB.Snapshot, compulsory); - } - } -} diff --git a/test/csharp/SequenceConfigTest.cs b/test/csharp/SequenceConfigTest.cs deleted file mode 100644 index 8d997d6ca..000000000 --- a/test/csharp/SequenceConfigTest.cs +++ /dev/null @@ -1,123 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class SequenceConfigTest : CSharpTestFixture - { - - [TestFixtureSetUp] - public void SetUpTestFixture() - { - testFixtureName = "SequenceConfigTest"; - base.SetUpTestfixture(); - } - - [Test] - public void TestConfig() - { - testName = "TestConfig"; - SetUpTest(false); - SequenceConfig seqConfig = new SequenceConfig(); - XmlElement xmlElem = Configuration.TestSetUp( - testFixtureName, testName); - Config(xmlElem, ref seqConfig, true); - Confirm(xmlElem, seqConfig, true); - } - - [Test] - public void TestConfigObj() - { - testName = "TestConfigObj"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - - // Open a database. - BTreeDatabaseConfig btreeDBConfig = - new BTreeDatabaseConfig(); - btreeDBConfig.Creation = CreatePolicy.IF_NEEDED; - BTreeDatabase btreeDB = BTreeDatabase.Open( - dbFileName, btreeDBConfig); - - /* Configure and initialize sequence. */ - SequenceConfig seqConfig = new SequenceConfig(); - seqConfig.BackingDatabase = btreeDB; - seqConfig.Creation = CreatePolicy.IF_NEEDED; - seqConfig.key = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("key")); - seqConfig.SetRange(Int64.MinValue, Int64.MaxValue); - Sequence seq = new Sequence(seqConfig); - - // Confirm the objects set in SequenceConfig. - Assert.AreEqual(dbFileName, - seq.BackingDatabase.FileName); - Assert.AreEqual(ASCIIEncoding.ASCII.GetBytes("key"), - seq.Key.Data); - Assert.AreEqual(Int64.MinValue, seq.Min); - Assert.AreEqual(Int64.MaxValue, seq.Max); - - /* Close sequence, database and environment. */ - seq.Close(); - btreeDB.Close(); - } - - public static void Confirm(XmlElement xmlElement, - SequenceConfig seqConfig, bool compulsory) - { - Configuration.ConfirmInt(xmlElement, "CacheSize", - seqConfig.CacheSize, compulsory); - Configuration.ConfirmCreatePolicy(xmlElement, "Creation", - seqConfig.Creation, compulsory); - Configuration.ConfirmBool(xmlElement, "Decrement", - seqConfig.Decrement, compulsory); - Configuration.ConfirmBool(xmlElement, "FreeThreaded", - seqConfig.FreeThreaded, compulsory); - Configuration.ConfirmBool(xmlElement, "Increment", - seqConfig.Increment, compulsory); - Configuration.ConfirmLong(xmlElement, "InitialValue", - seqConfig.InitialValue, compulsory); - Configuration.ConfirmBool(xmlElement, "Wrap", - seqConfig.Wrap, compulsory); - } - - public static void Config(XmlElement xmlElement, - ref SequenceConfig seqConfig, bool compulsory) - { - int intValue = new int(); - bool boolValue = new bool(); - long longValue = new long(); - - if (Configuration.ConfigInt(xmlElement, "CacheSize", - ref intValue, compulsory)) - seqConfig.CacheSize = intValue; - Configuration.ConfigCreatePolicy(xmlElement, "Creation", - ref seqConfig.Creation, compulsory); - if (Configuration.ConfigBool(xmlElement, "Decrement", - ref boolValue, compulsory)) - seqConfig.Decrement = boolValue; - Configuration.ConfigBool(xmlElement, "FreeThreaded", - ref seqConfig.FreeThreaded, compulsory); - if (Configuration.ConfigBool(xmlElement, "Increment", - ref boolValue, compulsory)) - seqConfig.Increment = boolValue; - if (Configuration.ConfigLong(xmlElement, "InitialValue", - ref longValue, compulsory)) - seqConfig.InitialValue = longValue; - Configuration.ConfigBool(xmlElement, "Wrap", - ref seqConfig.Wrap, compulsory); - } - } -} diff --git a/test/csharp/SequenceTest.cs b/test/csharp/SequenceTest.cs deleted file mode 100644 index 91d53b57a..000000000 --- a/test/csharp/SequenceTest.cs +++ /dev/null @@ -1,378 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Threading; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class SequenceTest : CSharpTestFixture - { - - [TestFixtureSetUp] - public void SetUpTestFixture() - { - testFixtureName = "SequenceTest"; - base.SetUpTestfixture(); - } - - [Test] - public void TestConfig() - { - testName = "TestConfig"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - XmlElement xmlElem = Configuration.TestSetUp( - testFixtureName, testName); - - // Open a database. - BTreeDatabaseConfig btreeDBConfig = - new BTreeDatabaseConfig(); - btreeDBConfig.Creation = CreatePolicy.IF_NEEDED; - BTreeDatabase btreeDB = BTreeDatabase.Open( - dbFileName, btreeDBConfig); - - // Configure and initialize sequence. - SequenceConfig seqConfig = new SequenceConfig(); - seqConfig.BackingDatabase = btreeDB; - seqConfig.Creation = CreatePolicy.IF_NEEDED; - seqConfig.key = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("key")); - seqConfig.SetRange(Int64.MinValue, Int64.MaxValue); - SequenceConfigTest.Config(xmlElem, ref seqConfig, true); - Sequence seq = new Sequence(seqConfig); - - /* - * Confirm that the squence is opened with the - * configuration that we set. - */ - Confirm(xmlElem, seq, true); - - /* Close sequence, database and environment. */ - seq.Close(); - btreeDB.Close(); - } - - [Test] - public void TestClose() - { - testName = "TestClose"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - - BTreeDatabase db; - Sequence seq; - OpenNewSequence(dbFileName, out db, out seq); - seq.Close(); - db.Close(); - } - - [Test] - public void TestDispose() - { - testName = "TestDispose"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - - BTreeDatabase db; - Sequence seq; - OpenNewSequence(dbFileName, out db, out seq); - seq.Dispose(); - db.Close(); - } - - [Test] - public void TestGet() - { - testName = "TestGet"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - - // Open a database and an increase sequence. - BTreeDatabase db; - Sequence seq; - OpenNewSequence(dbFileName, out db, out seq); - - /* - * Check the delta of two sequence number get - * from sequence. - */ - int delta = 100; - long seqNum1 = seq.Get(delta); - long seqNum2 = seq.Get(delta); - Assert.AreEqual(delta, seqNum2 - seqNum1); - - // Close all. - seq.Close(); - db.Close(); - } - - [Test] - public void TestGetWithNoSync() - { - testName = "TestGetWithNoSync"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - - // Open a database and an increase sequence. - BTreeDatabase db; - DatabaseEnvironment env; - Sequence seq; - OpenNewSequenceInEnv(testHome, testName, out env, - out db, out seq); - - /* - * Check the delta of two sequence number get - * from sequence. - */ - int delta = 100; - long seqNum1 = seq.Get(delta, true); - long seqNum2 = seq.Get(delta, true); - Assert.AreEqual(delta, seqNum2 - seqNum1); - - // Close all. - seq.Close(); - db.Close(); - env.Close(); - } - - [Test] - public void TestGetWithinTxn() - { - testName = "TestGetWithinTxn"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - - // Open a database and an increase sequence. - BTreeDatabase db; - DatabaseEnvironment env; - Sequence seq; - OpenNewSequenceInEnv(testHome, testName, out env, - out db, out seq); - - /* - * Check the delta of two sequence number get - * from sequence. - */ - int delta = 100; - Transaction txn = env.BeginTransaction(); - long seqNum1 = seq.Get(delta, txn); - long seqNum2 = seq.Get(delta, txn); - Assert.AreEqual(delta, seqNum2 - seqNum1); - txn.Commit(); - - // Close all. - seq.Close(); - db.Close(); - env.Close(); - } - - [Test] - public void TestRemove() - { - testName = "TestRemove"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - - // Open a database and an increase sequence. - BTreeDatabase db; - Sequence seq; - OpenNewSequence(dbFileName, out db, out seq); - - /* - * Remove the sequence. The sequence handle can not - * be accessed again. - */ - seq.Remove(); - db.Close(); - } - - [Test] - public void TestRemoveWithNoSync() - { - testName = "TestRemoveWithNoSync"; - SetUpTest(true); - string dbFileName = testName + ".db"; - - // Open a database and an increase sequence. - DatabaseEnvironmentConfig envConfig = new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseLogging = true; - envConfig.UseMPool = true; - envConfig.UseTxns = true; - DatabaseEnvironment env = DatabaseEnvironment.Open(testHome, envConfig); - - /* Configure and open sequence's database. */ - BTreeDatabaseConfig btreeDBConfig = new BTreeDatabaseConfig(); - btreeDBConfig.AutoCommit = true; - btreeDBConfig.Creation = CreatePolicy.IF_NEEDED; - btreeDBConfig.Env = env; - BTreeDatabase btreeDB = BTreeDatabase.Open(dbFileName, btreeDBConfig); - - /* Configure and initialize sequence. */ - SequenceConfig seqConfig = new SequenceConfig(); - seqConfig.BackingDatabase = btreeDB; - seqConfig.Creation = CreatePolicy.IF_NEEDED; - seqConfig.Increment = true; - seqConfig.InitialValue = Int64.MaxValue; - seqConfig.key = new DatabaseEntry(); - seqConfig.SetRange(Int64.MinValue, Int64.MaxValue); - seqConfig.Wrap = true; - seqConfig.key = new DatabaseEntry(); - seqConfig.key.Data = ASCIIEncoding.ASCII.GetBytes("ex_csharp_sequence"); - Sequence seq = new Sequence(seqConfig); - - /* - * Remove the sequence. The sequence handle can not - * be accessed again. - */ - seq.Remove(true); - btreeDB.Close(); - env.Close(); - } - - [Test] - public void TestRemoveWithInTxn() - { - testName = "TestRemoveWithInTxn"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - - // Open a database and an increase sequence. - BTreeDatabase db; - DatabaseEnvironment env; - Sequence seq; - OpenNewSequenceInEnv(testHome, testName, out env, - out db, out seq); - - //Remove the sequence. - Transaction txn = env.BeginTransaction(); - seq.Remove(txn); - txn.Commit(); - db.Close(); - env.Close(); - } - - [Test] - public void TestStats() - { - testName = "TestStats"; - SetUpTest(true); - string dbFileName = testHome + "/" + testName + ".db"; - - // Open a database and an increase sequence. - BTreeDatabase db; - Sequence seq; - OpenNewSequence(dbFileName, out db, out seq); - - // Get a value from sequence. - seq.Get(100); - - // Get sequence statistics. - SequenceStats stats = seq.Stats(); - seq.PrintStats(true); - Assert.AreEqual(200, stats.CachedValue); - Assert.AreEqual(1000, stats.CacheSize); - Assert.AreNotEqual(0, stats.Flags); - Assert.AreEqual(1099, stats.LastCachedValue); - Assert.AreEqual(Int64.MaxValue, stats.Max); - Assert.AreEqual(Int64.MinValue, stats.Min); - Assert.AreEqual(1100, stats.StoredValue); - - stats = seq.Stats(true); - seq.PrintStats(); - stats = seq.Stats(); - Assert.AreEqual(0, stats.LockNoWait); - Assert.AreEqual(0, stats.LockWait); - - seq.Close(); - db.Close(); - } - - public void OpenNewSequence(string dbFileName, - out BTreeDatabase db, out Sequence seq) - { - // Open a database. - BTreeDatabaseConfig btreeDBConfig = - new BTreeDatabaseConfig(); - btreeDBConfig.Creation = CreatePolicy.IF_NEEDED; - db = BTreeDatabase.Open(dbFileName, btreeDBConfig); - - // Configure and initialize sequence. - SequenceConfig seqConfig = new SequenceConfig(); - seqConfig.BackingDatabase = db; - seqConfig.CacheSize = 1000; - seqConfig.Creation = CreatePolicy.ALWAYS; - seqConfig.Decrement = false; - seqConfig.FreeThreaded = true; - seqConfig.Increment = true; - seqConfig.InitialValue = 100; - seqConfig.key = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("key")); - seqConfig.SetRange(Int64.MinValue, Int64.MaxValue); - seqConfig.Wrap = true; - seq = new Sequence(seqConfig); - } - - public void OpenNewSequenceInEnv(string home, string dbname, - out DatabaseEnvironment env, out BTreeDatabase db, - out Sequence seq) - { - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseTxns = true; - envConfig.UseMPool = true; - envConfig.UseLogging = true; - env = DatabaseEnvironment.Open(home, envConfig); - - Transaction openTxn = env.BeginTransaction(); - BTreeDatabaseConfig dbConfig = - new BTreeDatabaseConfig(); - dbConfig.Creation = CreatePolicy.IF_NEEDED; - dbConfig.Env = env; - db = BTreeDatabase.Open(dbname + ".db", dbConfig, - openTxn); - openTxn.Commit(); - - Transaction seqTxn = env.BeginTransaction(); - SequenceConfig seqConfig = new SequenceConfig(); - seqConfig.BackingDatabase = db; - seqConfig.Creation = CreatePolicy.ALWAYS; - seqConfig.Decrement = false; - seqConfig.FreeThreaded = true; - seqConfig.Increment = true; - seqConfig.InitialValue = 0; - seqConfig.key = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("key")); - seqConfig.SetRange(Int64.MinValue, Int64.MaxValue); - seqConfig.Wrap = true; - seq = new Sequence(seqConfig); - seqTxn.Commit(); - } - - public static void Confirm(XmlElement xmlElement, - Sequence seq, bool compulsory) - { - Configuration.ConfirmInt(xmlElement, "CacheSize", - seq.Cachesize, compulsory); - Configuration.ConfirmBool(xmlElement, "Decrement", - seq.Decrement, compulsory); - Configuration.ConfirmBool(xmlElement, "Increment", - seq.Increment, compulsory); - Configuration.ConfirmBool(xmlElement, "Wrap", - seq.Wrap, compulsory); - } - } -} diff --git a/test/csharp/TestException.cs b/test/csharp/TestException.cs deleted file mode 100644 index d58fc696f..000000000 --- a/test/csharp/TestException.cs +++ /dev/null @@ -1,49 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections.Generic; -using System.Text; - -namespace CsharpAPITest -{ - - public class TestException : ApplicationException - { - public TestException(string name) - : base(name) - { - } - - public TestException() : base() - { - } - } - - public class ConfigNotFoundException : TestException - { - public ConfigNotFoundException(string name) - : base(name) - { - } - } - - public class InvalidConfigException : TestException - { - public InvalidConfigException(string name) - : base(name) - { - } - } - - public class ExpectedTestException : TestException - { - public ExpectedTestException() - : base() - { - } - } -} diff --git a/test/csharp/TransactionCommitTokenTest.cs b/test/csharp/TransactionCommitTokenTest.cs deleted file mode 100644 index 25392325e..000000000 --- a/test/csharp/TransactionCommitTokenTest.cs +++ /dev/null @@ -1,261 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2010, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections.Generic; -using System.Text; -using System.Threading; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class TransactionCommitTokenTest : CSharpTestFixture - { - private uint masterPort; - private uint clientPort; - private string ip = "127.0.0.1"; - - [TestFixtureSetUp] - public void SetUpTestFixture() - { - testFixtureName = "TransactionCommitTokenTest"; - base.SetUpTestfixture(); - ReplicationTest.AvailablePorts portGen = - new ReplicationTest.AvailablePorts(); - masterPort = portGen.Current; - portGen.MoveNext(); - clientPort = portGen.Current; - } - - public DatabaseEnvironment SetUpEnv(String home, uint priority, uint port, bool isMaster) - { - try { - Configuration.ClearDir(home); - } catch (Exception e) { - Console.WriteLine(e.Message); - throw new TestException("Please clean the directory"); - } - - /* Configure and open environment with replication - * application. - */ - DatabaseEnvironmentConfig cfg = - new DatabaseEnvironmentConfig(); - cfg.UseReplication = true; - cfg.MPoolSystemCfg = new MPoolConfig(); - cfg.MPoolSystemCfg.CacheSize = - new CacheInfo(0, 20485760, 1); - cfg.UseLocking = true; - cfg.UseTxns = true; - cfg.UseMPool = true; - cfg.Create = true; - cfg.UseLogging = true; - cfg.RunRecovery = true; - cfg.TxnNoSync = true; - cfg.FreeThreaded = true; - - cfg.RepSystemCfg = new ReplicationConfig(); - DbSiteConfig dbSiteConfig = new DbSiteConfig(); - dbSiteConfig.Host = ip; - dbSiteConfig.Port = port; - dbSiteConfig.LocalSite = true; - cfg.RepSystemCfg.RepmgrSitesConfig.Add(dbSiteConfig); - cfg.RepSystemCfg.Priority = priority; - if (!isMaster) { - DbSiteConfig dbSiteConfig1 = new DbSiteConfig(); - dbSiteConfig1.Host = ip; - dbSiteConfig1.Port = masterPort; - dbSiteConfig1.Helper = true; - cfg.RepSystemCfg.RepmgrSitesConfig.Add(dbSiteConfig1); - } - cfg.RepSystemCfg.BulkTransfer = false; - cfg.RepSystemCfg.AckTimeout = 5000; - - cfg.RepSystemCfg.RepMgrAckPolicy = - AckPolicy.ALL_PEERS; - - DatabaseEnvironment env = DatabaseEnvironment.Open( - home, cfg); - - return env; - } - - public BTreeDatabase Open(DatabaseEnvironment env, bool isMaster) - { - string dbName = "rep.db"; - - // Set up the database. - BTreeDatabaseConfig dbCfg = new BTreeDatabaseConfig(); - dbCfg.Env = env; - if (isMaster) - dbCfg.Creation = CreatePolicy.IF_NEEDED; - - dbCfg.AutoCommit = true; - dbCfg.FreeThreaded = true; - - /* - * Open the database. Do not provide a txn handle. This - * Open is autocommitted because BTreeDatabaseConfig.AutoCommit - * is true. - */ - return BTreeDatabase.Open(dbName, dbCfg); - } - - [Test] - public void TestCommitSuccess() - { - testName = "TestCommitSuccess"; - SetUpTest(true); - string[] keys = {"key 1", "key 2", "key 3", "key 4", - "key 5", "key 6", "key 7", "key 8", "key 9", "key 10"}; - - DatabaseEnvironment master = SetUpEnv(testHome + "/master", 100, masterPort, true); - DatabaseEnvironment client = SetUpEnv(testHome + "/client", 0, clientPort, false); - - master.RepMgrStartMaster(2); - Thread.Sleep(2000); - client.RepMgrStartClient(2); - - BTreeDatabase db1 = Open(master, true); - BTreeDatabase db2 = null; - for (; ; ) { - if (db2 == null) - try { - db2 = Open(client, false); - break; - } catch (DatabaseException) { - if (db2 != null) { - db2.Close(true); - db2 = null; - } - System.Threading.Thread.Sleep(1000); - continue; - } - } - - try { - for (int i = 0; i < 3; i++) { - Transaction txn = master.BeginTransaction(); - - // Get the key. - DatabaseEntry key, data; - key = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes(keys[i])); - - data = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes(keys[i])); - - db1.Put(key, data, txn); - txn.Commit(); - - byte[] token = txn.CommitToken; - Assert.AreEqual(master.IsTransactionApplied(token, 5000), TransactionAppliedStatus.APPLIED); - Assert.AreEqual(client.IsTransactionApplied(token, 200000), TransactionAppliedStatus.APPLIED); - } - } - finally { - db1.Close(); - db2.Close(); - master.Close(); - client.Close(); - } - } - - [Test] - public void TestNullArgument() - { - testName = "TestNullArgument"; - SetUpTest(true); - DatabaseEnvironment master = SetUpEnv(testHome + "/master", 100, 8000, true); - Assert.Throws( - delegate { master.IsTransactionApplied(null, 0); }); - master.Close(); - } - - [Test] - public void TestValidArgument() - { - testName = "TestValidArgument"; - SetUpTest(true); - DatabaseEnvironment master = SetUpEnv(testHome + "/master", 100, 8000, true); - Assert.Throws( - delegate { master.IsTransactionApplied(new byte[10], 0); }); - master.Close(); - } - - [Test] - public void TestEmptyException() { - testName = "TestEmptyException"; - SetUpTest(true); - DatabaseEnvironment master = SetUpEnv(testHome + "/master", 100, 8000, true); - Transaction txn = master.BeginTransaction(); - txn.Commit(); - byte[] token = txn.CommitToken; - Assert.AreEqual(TransactionAppliedStatus.EMPTY_TRANSACTION, - master.IsTransactionApplied(token, 1000)); - master.Close(); - } - - [Test] - public void TestInvalidToken() { - testName = "TestInvalidToken"; - SetUpTest(true); - DatabaseEntry key, data; - DatabaseEnvironment master = SetUpEnv(testHome + "/master", 100, 8000, true); - BTreeDatabase db = Open(master,true); - Transaction txn = master.BeginTransaction(); - key = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("Key")); - data = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("Data")); - db.Put(key, data, txn); - txn.Commit(); - byte[] token = txn.CommitToken; - token[19] += 123; - Assert.Throws(delegate { master.IsTransactionApplied(token, 1000); }); - db.Close(); - master.Close(); - } - - [Test] - public void TestOperationOrder() { - testName = "TestOperationOrder"; - SetUpTest(true); - DatabaseEnvironment master = SetUpEnv(testHome + "/master", 100, 8000, true); - Transaction txn = master.BeginTransaction(); - txn.Abort(); - byte[] token; - Assert.Throws(delegate { token = txn.CommitToken; }); - master.Close(); - } - - [Test] - public void TestNestedTXN() { - testName = "TestNestedTXN"; - SetUpTest(true); - DatabaseEntry key, data; - DatabaseEnvironment master = SetUpEnv(testHome + "/master", 100, 8000, true); - BTreeDatabase db = Open(master, true); - TransactionConfig txnconfig = new TransactionConfig(); - Transaction txn = master.BeginTransaction(); - Transaction txn1 = master.BeginTransaction(txnconfig, txn); - - key = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("Key")); - data = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("Data")); - db.Put(key, data, txn1); - txn1.Commit(); - txn.Commit(); - - byte[] token; - Assert.Throws(delegate { token = txn1.CommitToken; }); - master.Close(); - } - } -} diff --git a/test/csharp/TransactionConfigTest.cs b/test/csharp/TransactionConfigTest.cs deleted file mode 100644 index 94f8e7c20..000000000 --- a/test/csharp/TransactionConfigTest.cs +++ /dev/null @@ -1,73 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class TransactionConfigTest : CSharpTestFixture - { - - [TestFixtureSetUp] - public void SetUpTestFixture() - { - testFixtureName = "TransactionConfigTest"; - base.SetUpTestfixture(); - } - - [Test] - public void TestConfig() - { - testName = "TestConfig"; - SetUpTest(false); - /* - * Configure the fields/properties and see if - * they are updated successfully. - */ - TransactionConfig txnConfig = new TransactionConfig(); - XmlElement xmlElem = Configuration.TestSetUp( - testFixtureName, testName); - Config(xmlElem, ref txnConfig, true); - Confirm(xmlElem, txnConfig, true); - } - - public static void Confirm(XmlElement xmlElement, - TransactionConfig txnConfig, bool compulsory) - { - Configuration.ConfirmIsolation(xmlElement, - "IsolationDegree", txnConfig.IsolationDegree, - compulsory); - Configuration.ConfirmBool(xmlElement, "NoWait", - txnConfig.NoWait, compulsory); - Configuration.ConfirmBool(xmlElement, "Snapshot", - txnConfig.Snapshot, compulsory); - Configuration.ConfirmLogFlush(xmlElement, "SyncAction", - txnConfig.SyncAction, compulsory); - } - - public static void Config(XmlElement xmlElement, - ref TransactionConfig txnConfig, bool compulsory) - { - Configuration.ConfigIsolation(xmlElement, - "IsolationDegree", ref txnConfig.IsolationDegree, - compulsory); - Configuration.ConfigBool(xmlElement, "NoWait", - ref txnConfig.NoWait, compulsory); - Configuration.ConfigBool(xmlElement, "Snapshot", - ref txnConfig.Snapshot, compulsory); - Configuration.ConfigLogFlush(xmlElement, "SyncAction", - ref txnConfig.SyncAction, compulsory); - } - } -} diff --git a/test/csharp/TransactionTest.cs b/test/csharp/TransactionTest.cs deleted file mode 100644 index 8198d2a72..000000000 --- a/test/csharp/TransactionTest.cs +++ /dev/null @@ -1,429 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Threading; -using System.Xml; -using NUnit.Framework; -using BerkeleyDB; - -namespace CsharpAPITest -{ - [TestFixture] - public class TransactionTest : CSharpTestFixture - { - - private DatabaseEnvironment deadLockEnv; - - [TestFixtureSetUp] - public void SetUpTestFixture() - { - testFixtureName = "TransactionTest"; - base.SetUpTestfixture(); - - DatabaseEnvironment.Remove(testFixtureHome); - } - - [Test, ExpectedException(typeof(ExpectedTestException))] - public void TestAbort() - { - testName = "TestAbort"; - SetUpTest(true); - - DatabaseEnvironment env; - Transaction txn; - BTreeDatabase db; - - /* - * Open an environment and begin a transaction. Open - * a db and write a record the db within this transaction. - */ - PutRecordWithTxn(out env, testHome, testName, out txn); - - // Abort the transaction. - txn.Abort(); - - /* - * Undo all operations in the transaction so the - * database couldn't be reopened. - */ - try - { - OpenBtreeDBInEnv(testName + ".db", env, - out db, false, null); - } - catch (DatabaseException) - { - throw new ExpectedTestException(); - } - finally - { - env.Close(); - } - } - - [Test] - public void TestCommit() - { - testName = "TestCommit"; - SetUpTest(true); - - DatabaseEnvironment env; - Transaction txn; - BTreeDatabase db; - - /* - * Open an environment and begin a transaction. Open - * a db and write a record the db within this transaction. - */ - PutRecordWithTxn(out env, testHome, testName, out txn); - - // Commit the transaction. - txn.Commit(); - - // Reopen the database. - OpenBtreeDBInEnv(testName + ".db", env, - out db, false, null); - - /* - * Confirm that the record("key", "data") exists in the - * database. - */ - try - { - db.GetBoth(new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("key")), - new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("data"))); - } - catch (DatabaseException) - { - throw new TestException(); - } - finally - { - db.Close(); - env.Close(); - } - } - - [Test] - public void TestDiscard() - { - DatabaseEnvironment env; - byte[] gid; - - testName = "TestDiscard"; - SetUpTest(true); - - /* - * Open an environment and begin a transaction - * called "transaction". Within the transacion, open a - * database, write a record and close it. Then prepare - * the transaction and panic the environment. - */ - PanicPreparedTxn(testHome, testName, out env, out gid); - - /* - * Recover the environment. Log and db files are not - * destoyed so run normal recovery. Recovery should - * use DB_CREATE and DB_INIT_TXN flags when - * opening the environment. - */ - DatabaseEnvironmentConfig envConfig - = new DatabaseEnvironmentConfig(); - envConfig.RunRecovery = true; - envConfig.Create = true; - envConfig.UseTxns = true; - envConfig.UseMPool = true; - env = DatabaseEnvironment.Open(testHome, envConfig); - - PreparedTransaction[] preparedTxns - = new PreparedTransaction[10]; - preparedTxns = env.Recover(10, true); - - Assert.AreEqual(gid, preparedTxns[0].GlobalID); - preparedTxns[0].Txn.Discard(); - try { - preparedTxns[0].Txn.Commit(); - throw new TestException(); - } catch (AccessViolationException) { - } finally { - env.Close(); - } - } - - [Test] - public void TestPrepare() - { - testName = "TestPrepare"; - SetUpTest(true); - - DatabaseEnvironment env; - byte[] gid; - - /* - * Open an environment and begin a transaction - * called "transaction". Within the transacion, open a - * database, write a record and close it. Then prepare - * the transaction and panic the environment. - */ - PanicPreparedTxn(testHome, testName, out env, out gid); - - /* - * Recover the environment. Log and db files are not - * destoyed so run normal recovery. Recovery should - * use DB_CREATE and DB_INIT_TXN flags when - * opening the environment. - */ - DatabaseEnvironmentConfig envConfig - = new DatabaseEnvironmentConfig(); - envConfig.RunRecovery = true; - envConfig.Create = true; - envConfig.UseTxns = true; - envConfig.UseMPool = true; - env = DatabaseEnvironment.Open(testHome, envConfig); - - // Reopen the database. - BTreeDatabase db; - OpenBtreeDBInEnv(testName + ".db", env, out db, - false, null); - - /* - * Confirm that record("key", "data") exists in the - * database. - */ - DatabaseEntry key, data; - key = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("key")); - data = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("data")); - try - { - db.GetBoth(key, data); - } - catch (DatabaseException) - { - throw new TestException(); - } - finally - { - db.Close(); - env.Close(); - } - - } - - public void PanicPreparedTxn(string home, string dbName, - out DatabaseEnvironment env, out byte[] globalID) - { - Transaction txn; - - // Put record into database within transaction. - PutRecordWithTxn(out env, home, dbName, out txn); - - /* - * Generate global ID for the transaction. Copy - * transaction ID to the first 4 tyes in global ID. - */ - globalID = new byte[Transaction.GlobalIdLength]; - byte[] txnID = new byte[4]; - txnID = BitConverter.GetBytes(txn.Id); - for (int i = 0; i < txnID.Length; i++) - globalID[i] = txnID[i]; - - // Prepare the transaction. - txn.Prepare(globalID); - - // Panic the environment. - env.Panic(); - - } - - [Test] - public void TestTxnName() - { - DatabaseEnvironment env; - Transaction txn; - - testName = "TestTxnName"; - SetUpTest(true); - - SetUpTransactionalEnv(testHome, out env); - txn = env.BeginTransaction(); - txn.Name = testName; - Assert.AreEqual(testName, txn.Name); - txn.Commit(); - env.Close(); - } - - [Test] - public void TestTxnPriority() { - DatabaseEnvironment env; - Transaction txn; - - testName = "TestTxnPriority"; - SetUpTest(true); - - SetUpTransactionalEnv(testHome, out env); - txn = env.BeginTransaction(); - txn.Priority = 555; - Assert.AreEqual(555, txn.Priority); - txn.Commit(); - env.Close(); - } - - [Test] - public void TestSetLockTimeout() - { - testName = "TestSetLockTimeout"; - SetUpTest(true); - - // Set lock timeout. - TestTimeOut(true); - - } - - [Test] - public void TestSetTxnTimeout() - { - testName = "TestSetTxnTimeout"; - SetUpTest(true); - - // Set transaction time out. - TestTimeOut(false); - - } - - /* - * ifSetLock is used to indicate which timeout function - * is used, SetLockTimeout or SetTxnTimeout. - */ - public void TestTimeOut(bool ifSetLock) - { - // Open environment and begin transaction. - Transaction txn; - deadLockEnv = null; - SetUpEnvWithTxnAndLocking(testHome, - out deadLockEnv, out txn, 0, 0, 0, 0); - - // Define deadlock detection and resolve policy. - deadLockEnv.DeadlockResolution = - DeadlockPolicy.YOUNGEST; - if (ifSetLock == true) - txn.SetLockTimeout(10); - else - txn.SetTxnTimeout(10); - - txn.Commit(); - deadLockEnv.Close(); - } - - public static void SetUpEnvWithTxnAndLocking(string envHome, - out DatabaseEnvironment env, out Transaction txn, - uint maxLock, uint maxLocker, uint maxObject, uint partition) - { - // Configure env and locking subsystem. - LockingConfig lkConfig = new LockingConfig(); - - /* - * If the maximum number of locks/lockers/objects - * is given, then the LockingConfig is set. Unless, - * it is not set to any value. - */ - if (maxLock != 0) - lkConfig.MaxLocks = maxLock; - if (maxLocker != 0) - lkConfig.MaxLockers = maxLocker; - if (maxObject != 0) - lkConfig.MaxObjects = maxObject; - if (partition != 0) - lkConfig.Partitions = partition; - - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseTxns = true; - envConfig.UseMPool = true; - envConfig.LockSystemCfg = lkConfig; - envConfig.UseLocking = true; - envConfig.NoLocking = false; - env = DatabaseEnvironment.Open(envHome, envConfig); - txn = env.BeginTransaction(); - } - - public void PutRecordWithTxn(out DatabaseEnvironment env, - string home, string dbName, out Transaction txn) - { - BTreeDatabase db; - - // Open a new environment and begin a transaction. - SetUpTransactionalEnv(home, out env); - TransactionConfig txnConfig = new TransactionConfig(); - txnConfig.Name = "Transaction"; - txn = env.BeginTransaction(txnConfig); - Assert.AreEqual("Transaction", txn.Name); - - // Open a new database within the transaction. - OpenBtreeDBInEnv(dbName + ".db", env, out db, true, txn); - - // Write to the database within the transaction. - WriteOneIntoBtreeDBWithTxn(db, txn); - - // Close the database. - db.Close(); - } - - public void SetUpTransactionalEnv(string home, - out DatabaseEnvironment env) - { - DatabaseEnvironmentConfig envConfig = - new DatabaseEnvironmentConfig(); - envConfig.Create = true; - envConfig.UseLogging = true; - envConfig.UseLocking = true; - envConfig.UseMPool = true; - envConfig.UseTxns = true; - env = DatabaseEnvironment.Open( - home, envConfig); - } - - public void OpenBtreeDBInEnv(string dbName, - DatabaseEnvironment env, out BTreeDatabase db, - bool create, Transaction txn) - { - BTreeDatabaseConfig btreeDBConfig = - new BTreeDatabaseConfig(); - btreeDBConfig.Env = env; - if (create == true) - btreeDBConfig.Creation = CreatePolicy.IF_NEEDED; - else - btreeDBConfig.Creation = CreatePolicy.NEVER; - if (txn == null) - db = BTreeDatabase.Open(dbName, - btreeDBConfig); - else - db = BTreeDatabase.Open(dbName, - btreeDBConfig, txn); - } - - public void WriteOneIntoBtreeDBWithTxn(BTreeDatabase db, - Transaction txn) - { - DatabaseEntry key, data; - - key = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("key")); - data = new DatabaseEntry( - ASCIIEncoding.ASCII.GetBytes("data")); - db.Put(key, data, txn); - } - } -} diff --git a/test/csharp/XMLReader.cs b/test/csharp/XMLReader.cs deleted file mode 100644 index 07dcf6af3..000000000 --- a/test/csharp/XMLReader.cs +++ /dev/null @@ -1,48 +0,0 @@ -/*- - * See the file LICENSE for redistribution information. - * - * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. - * - */ -using System; -using System.Collections.Generic; -using System.Text; -using System.Xml; -using System.Xml.XPath; - -namespace CsharpAPITest -{ - public class XMLReader - { - private static string path; - - public XMLReader(string XmlFileName) - { - path = XmlFileName; - } - - public XmlElement GetXmlElement(string className, string testName) - { - XmlDocument doc = new XmlDocument(); - doc.Load(path); - - string xpath = string.Format("/Assembly/TestFixture[@name=\"{0}\"]/Test[@name=\"{1}\"]", className, testName); - XmlElement testCase = doc.SelectSingleNode(xpath) as XmlElement; - if (testCase == null) - return null; - else - return testCase; - } - - public static XmlNode GetNode(XmlElement xmlElement, - string nodeName) - { - XmlNodeList xmlNodeList = xmlElement.SelectNodes(nodeName); - if (xmlNodeList.Count > 1) - throw new Exception(nodeName + " Configuration Error"); - else - return xmlNodeList.Item(0); - } - - } -} diff --git a/test/csharp/bdb4.7.db b/test/db/fixtures/bdb4.7.db similarity index 100% rename from test/csharp/bdb4.7.db rename to test/db/fixtures/bdb4.7.db diff --git a/test/db/run_upgrade.sh b/test/db/run_upgrade.sh index eaeaed342..ec82b04d6 100644 --- a/test/db/run_upgrade.sh +++ b/test/db/run_upgrade.sh @@ -60,7 +60,7 @@ # # Optional environment: # BUILD build_unix dir holding db_upgrade/db_verify (default: .) -# FIXTURE committed old-format fixture (default: ../test/csharp/bdb4.7.db) +# FIXTURE committed old-format fixture (default: ../test/db/fixtures/bdb4.7.db) # TCL_LIB libdb_tcl-*.so (auto-detected under $BUILD/.libs) # TCLSH tclsh binary (default: tclsh8.6 if present, else tclsh) # PYTHON python3 used to rewrite metadata pages (default: python3) @@ -71,7 +71,7 @@ set -e BUILD=${BUILD:-.} -FIXTURE=${FIXTURE:-../test/csharp/bdb4.7.db} +FIXTURE=${FIXTURE:-../test/db/fixtures/bdb4.7.db} WORK=${WORK:-UPGTEST} TIMEOUT=${TIMEOUT:-60} PYTHON=${PYTHON:-python3}