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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2009, 2015 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);
}
}
}
|