1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
/*-
* 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<DatabaseEntry, DatabaseEntry> 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;
}
}
}
|