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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2009 Oracle. All rights reserved.
*
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using BerkeleyDB.Internal;
namespace BerkeleyDB {
/// <summary>
/// A class representing a Berkeley DB database, a base class for access
/// method specific classes.
/// </summary>
public class Database : BaseDatabase, IDisposable {
private static BDB_FileWriteDelegate writeToFileRef;
#region Constructors
/// <summary>
/// Protected constructor
/// </summary>
/// <param name="env">
/// The environment in which to create this database
/// </param>
/// <param name="flags">Flags to pass to the DB->create() method</param>
protected Database(DatabaseEnvironment env, uint flags)
: base(env, flags) {
}
/// <summary>
/// Create a new database object with the same underlying DB handle as
/// <paramref name="clone"/>. Used during Database.Open to get an
/// object of the correct DBTYPE.
/// </summary>
/// <param name="clone">Database to clone</param>
protected Database(BaseDatabase clone) : base(clone) { }
internal static Database fromDB(DB dbp) {
try {
return (Database)dbp.api_internal;
} catch { }
return null;
}
/// <summary>
/// Instantiate a new Database object and open the database represented
/// by <paramref name="Filename"/>. The file specified by
/// <paramref name="Filename"/> must exist.
/// </summary>
/// <remarks>
/// <para>
/// If <see cref="DatabaseConfig.AutoCommit"/> is set, the operation
/// will be implicitly transaction protected. Note that transactionally
/// protected operations on a datbase object requires the object itself
/// be transactionally protected during its open.
/// </para>
/// </remarks>
/// <param name="Filename">
/// The name of an underlying file that will be used to back the
/// database.
/// </param>
/// <param name="cfg">The database's configuration</param>
/// <returns>A new, open database object</returns>
public static Database Open(string Filename, DatabaseConfig cfg) {
return Open(Filename, null, cfg, null);
}
/// <summary>
/// Instantiate a new Database object and open the database represented
/// by <paramref name="Filename"/> and <paramref name="DatabaseName"/>.
/// The file specified by <paramref name="Filename"/> must exist.
/// </summary>
/// <remarks>
/// <para>
/// If <paramref name="Filename"/> is null and
/// <paramref name="DatabaseName"/> is non-null, the database can be
/// opened by other threads of control and will be replicated to client
/// sites in any replication group.
/// </para>
/// <para>
/// If <see cref="DatabaseConfig.AutoCommit"/> is set, the operation
/// will be implicitly transaction protected. Note that transactionally
/// protected operations on a datbase object requires the object itself
/// be transactionally protected during its open.
/// </para>
/// </remarks>
/// <param name="Filename">
/// The name of an underlying file that will be used to back the
/// database. In-memory databases never intended to be preserved on disk
/// may be created by setting this parameter to null.</param>
/// <param name="DatabaseName">
/// This parameter allows applications to have multiple databases in a
/// single file. Although no DatabaseName needs to be specified, it is
/// an error to attempt to open a second database in a file that was not
/// initially created using a database name.
/// </param>
/// <param name="cfg">The database's configuration</param>
/// <returns>A new, open database object</returns>
public static Database Open(
string Filename, string DatabaseName, DatabaseConfig cfg) {
return Open(Filename, DatabaseName, cfg, null);
}
/// <summary>
/// Instantiate a new Database object and open the database represented
/// by <paramref name="Filename"/>. The file specified by
/// <paramref name="Filename"/> must exist.
/// </summary>
/// <remarks>
/// <para>
/// If <paramref name="Filename"/> is null, the database is strictly
/// temporary and cannot be opened by any other thread of control, thus
/// the database can only be accessed by sharing the single database
/// object that created it, in circumstances where doing so is safe.
/// </para>
/// <para>
/// If <paramref name="txn"/> is null, but
/// <see cref="DatabaseConfig.AutoCommit"/> is set, the operation will
/// be implicitly transaction protected. Note that transactionally
/// protected operations on a datbase object requires the object itself
/// be transactionally protected during its open. Also note that the
/// transaction must be committed before the object is closed.
/// </para>
/// </remarks>
/// <param name="Filename">
/// The name of an underlying file that will be used to back the
/// database. In-memory databases never intended to be preserved on disk
/// may be created by setting this parameter to null.
/// </param>
/// <param name="cfg">The database's configuration</param>
/// <param name="txn">
/// If the operation is part of an application-specified transaction,
/// <paramref name="txn"/> is a Transaction object returned from
/// <see cref="DatabaseEnvironment.BeginTransaction"/>; if
/// the operation is part of a Berkeley DB Concurrent Data Store group,
/// <paramref name="txn"/> is a handle returned from
/// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null.
/// </param>
/// <returns>A new, open database object</returns>
public static Database Open(
string Filename, DatabaseConfig cfg, Transaction txn) {
return Open(Filename, null, cfg, txn);
}
/// <summary>
/// Instantiate a new Database object and open the database represented
/// by <paramref name="Filename"/> and <paramref name="DatabaseName"/>.
/// The file specified by <paramref name="Filename"/> must exist.
/// </summary>
/// <remarks>
/// <para>
/// If both <paramref name="Filename"/> and
/// <paramref name="DatabaseName"/> are null, the database is strictly
/// temporary and cannot be opened by any other thread of control, thus
/// the database can only be accessed by sharing the single database
/// object that created it, in circumstances where doing so is safe. If
/// <paramref name="Filename"/> is null and
/// <paramref name="DatabaseName"/> is non-null, the database can be
/// opened by other threads of control and will be replicated to client
/// sites in any replication group.
/// </para>
/// <para>
/// If <paramref name="txn"/> is null, but
/// <see cref="DatabaseConfig.AutoCommit"/> is set, the operation will
/// be implicitly transaction protected. Note that transactionally
/// protected operations on a datbase object requires the object itself
/// be transactionally protected during its open. Also note that the
/// transaction must be committed before the object is closed.
/// </para>
/// </remarks>
/// <param name="Filename">
/// The name of an underlying file that will be used to back the
/// database. In-memory databases never intended to be preserved on disk
/// may be created by setting this parameter to null.
/// </param>
/// <param name="DatabaseName">
/// This parameter allows applications to have multiple databases in a
/// single file. Although no DatabaseName needs to be specified, it is
/// an error to attempt to open a second database in a file that was not
/// initially created using a database name.
/// </param>
/// <param name="cfg">The database's configuration</param>
/// <param name="txn">
/// If the operation is part of an application-specified transaction,
/// <paramref name="txn"/> is a Transaction object returned from
/// <see cref="DatabaseEnvironment.BeginTransaction"/>; if
/// the operation is part of a Berkeley DB Concurrent Data Store group,
/// <paramref name="txn"/> is a handle returned from
/// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null.
/// </param>
/// <returns>A new, open database object</returns>
public static new Database Open(string Filename,
string DatabaseName, DatabaseConfig cfg, Transaction txn) {
Database ret;
BaseDatabase db = BaseDatabase.Open(
Filename, DatabaseName, cfg, txn);
switch (db.Type.getDBTYPE()) {
case DBTYPE.DB_BTREE:
ret = new BTreeDatabase(db);
break;
case DBTYPE.DB_HASH:
ret = new HashDatabase(db);
break;
case DBTYPE.DB_QUEUE:
ret = new QueueDatabase(db);
break;
case DBTYPE.DB_RECNO:
ret = new RecnoDatabase(db);
break;
default:
throw new DatabaseException(0);
}
db.Dispose();
ret.isOpen = true;
return ret;
}
#endregion Constructor
private static int writeToFile(TextWriter OutputStream, string data) {
OutputStream.Write(data);
return 0;
}
#region Methods
/// <summary>
/// If a key/data pair in the database matches <paramref name="key"/>
/// and <paramref name="data"/>, return the key and all duplicate data
/// items.
/// </summary>
/// <param name="key">The key to search for</param>
/// <param name="data">The data to search for</param>
/// <exception cref="NotFoundException">
/// A NotFoundException is thrown if <paramref name="key"/> and
/// <paramref name="data"/> are not in the database.
/// </exception>
/// <exception cref="KeyEmptyException">
/// A KeyEmptyException is thrown if the database is a
/// <see cref="QueueDatabase"/> or <see cref="RecnoDatabase"/>
/// database and <paramref name="key"/> exists, but was never explicitly
/// created by the application or was later deleted.
/// </exception>
/// <returns>
/// A <see cref="KeyValuePair{T,T}"/>
/// whose Key parameter is <paramref name="key"/> and whose Value
/// parameter is the retrieved data items.
/// </returns>
public
KeyValuePair<DatabaseEntry, MultipleDatabaseEntry> GetBothMultiple(
DatabaseEntry key, DatabaseEntry data) {
/*
* Make sure we pass a buffer that's big enough to hold data.Data
* and is a multiple of the page size. Cache this.Pagesize to avoid
* multiple P/Invoke calls.
*/
uint pgsz = Pagesize;
uint npgs = (data.size / pgsz) + 1;
return GetBothMultiple(key, data, (int)(npgs * pgsz), null, null);
}
/// <summary>
/// If a key/data pair in the database matches <paramref name="key"/>
/// and <paramref name="data"/>, return the key and all duplicate data
/// items.
/// </summary>
/// <param name="key">The key to search for</param>
/// <param name="data">The data to search for</param>
/// <param name="BufferSize">
/// The initial size of the buffer to fill with duplicate data items. If
/// the buffer is not large enough, it will be automatically resized.
/// </param>
/// <exception cref="NotFoundException">
/// A NotFoundException is thrown if <paramref name="key"/> and
/// <paramref name="data"/> are not in the database.
/// </exception>
/// <exception cref="KeyEmptyException">
/// A KeyEmptyException is thrown if the database is a
/// <see cref="QueueDatabase"/> or <see cref="RecnoDatabase"/>
/// database and <paramref name="key"/> exists, but was never explicitly
/// created by the application or was later deleted.
/// </exception>
/// <returns>
/// A <see cref="KeyValuePair{T,T}"/>
/// whose Key parameter is <paramref name="key"/> and whose Value
/// parameter is the retrieved data items.
/// </returns>
public
KeyValuePair<DatabaseEntry, MultipleDatabaseEntry> GetBothMultiple(
DatabaseEntry key, DatabaseEntry data, int BufferSize) {
return GetBothMultiple(key, data, BufferSize, null, null);
}
/// <summary>
/// If a key/data pair in the database matches <paramref name="key"/>
/// and <paramref name="data"/>, return the key and all duplicate data
/// items.
/// </summary>
/// <param name="key">The key to search for</param>
/// <param name="data">The data to search for</param>
/// <param name="BufferSize">
/// The initial size of the buffer to fill with duplicate data items. If
/// the buffer is not large enough, it will be automatically resized.
/// </param>
/// <param name="txn">
/// <paramref name="txn"/> is a Transaction object returned from
/// <see cref="DatabaseEnvironment.BeginTransaction"/>; if
/// the operation is part of a Berkeley DB Concurrent Data Store group,
/// <paramref name="txn"/> is a handle returned from
/// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null.
/// </param>
/// <exception cref="NotFoundException">
/// A NotFoundException is thrown if <paramref name="key"/> and
/// <paramref name="data"/> are not in the database.
/// </exception>
/// <exception cref="KeyEmptyException">
/// A KeyEmptyException is thrown if the database is a
/// <see cref="QueueDatabase"/> or <see cref="RecnoDatabase"/>
/// database and <paramref name="key"/> exists, but was never explicitly
/// created by the application or was later deleted.
/// </exception>
/// <returns>
/// A <see cref="KeyValuePair{T,T}"/>
/// whose Key parameter is <paramref name="key"/> and whose Value
/// parameter is the retrieved data items.
/// </returns>
public
KeyValuePair<DatabaseEntry, MultipleDatabaseEntry> GetBothMultiple(
DatabaseEntry key,
DatabaseEntry data, int BufferSize, Transaction txn) {
return GetBothMultiple(key, data, BufferSize, txn, null);
}
/// <summary>
/// If a key/data pair in the database matches <paramref name="key"/>
/// and <paramref name="data"/>, return the key and all duplicate data
/// items.
/// </summary>
/// <param name="key">The key to search for</param>
/// <param name="data">The data to search for</param>
/// <param name="BufferSize">
/// The initial size of the buffer to fill with duplicate data items. If
/// the buffer is not large enough, it will be automatically resized.
/// </param>
/// <param name="txn">
/// <paramref name="txn"/> is a Transaction object returned from
/// <see cref="DatabaseEnvironment.BeginTransaction"/>; if
/// the operation is part of a Berkeley DB Concurrent Data Store group,
/// <paramref name="txn"/> is a handle returned from
/// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null.
/// </param>
/// <param name="info">The locking behavior to use.</param>
/// <exception cref="NotFoundException">
/// A NotFoundException is thrown if <paramref name="key"/> and
/// <paramref name="data"/> are not in the database.
/// </exception>
/// <exception cref="KeyEmptyException">
/// A KeyEmptyException is thrown if the database is a
/// <see cref="QueueDatabase"/> or <see cref="RecnoDatabase"/>
/// database and <paramref name="key"/> exists, but was never explicitly
/// created by the application or was later deleted.
/// </exception>
/// <returns>
/// A <see cref="KeyValuePair{T,T}"/>
/// whose Key parameter is <paramref name="key"/> and whose Value
/// parameter is the retrieved data items.
/// </returns>
public
KeyValuePair<DatabaseEntry, MultipleDatabaseEntry> GetBothMultiple(
DatabaseEntry key, DatabaseEntry data,
int BufferSize, Transaction txn, LockingInfo info) {
KeyValuePair<DatabaseEntry, DatabaseEntry> kvp;
int datasz = (int)data.Data.Length;
for (; ; ) {
byte[] udata = new byte[BufferSize];
Array.Copy(data.Data, udata, datasz);
data.UserData = udata;
data.size = (uint)datasz;
try {
kvp = Get(key, data, txn, info,
DbConstants.DB_MULTIPLE | DbConstants.DB_GET_BOTH);
break;
} catch (MemoryException) {
int sz = (int)data.size;
if (sz > BufferSize)
BufferSize = sz;
else
BufferSize *= 2;
}
}
MultipleDatabaseEntry dbe = new MultipleDatabaseEntry(kvp.Value);
return new KeyValuePair<DatabaseEntry, MultipleDatabaseEntry>(
kvp.Key, dbe);
}
/// <summary>
/// Retrieve a key and all duplicate data items from the database.
/// </summary>
/// <param name="key">The key to search for</param>
/// <exception cref="NotFoundException">
/// A NotFoundException is thrown if <paramref name="key"/> is not in
/// the database.
/// </exception>
/// <exception cref="KeyEmptyException">
/// A KeyEmptyException is thrown if the database is a
/// <see cref="QueueDatabase"/> or <see cref="RecnoDatabase"/>
/// database and <paramref name="key"/> exists, but was never explicitly
/// created by the application or was later deleted.
/// </exception>
/// <returns>
/// A <see cref="KeyValuePair{T,T}"/>
/// whose Key parameter is <paramref name="key"/> and whose Value
/// parameter is the retrieved data items.
/// </returns>
public KeyValuePair<DatabaseEntry, MultipleDatabaseEntry> GetMultiple(
DatabaseEntry key) {
return GetMultiple(key, (int)Pagesize, null, null);
}
/// <summary>
/// Retrieve a key and all duplicate data items from the database.
/// </summary>
/// <param name="key">The key to search for</param>
/// <param name="BufferSize">
/// The initial size of the buffer to fill with duplicate data items. If
/// the buffer is not large enough, it will be automatically resized.
/// </param>
/// <exception cref="NotFoundException">
/// A NotFoundException is thrown if <paramref name="key"/> is not in
/// the database.
/// </exception>
/// <exception cref="KeyEmptyException">
/// A KeyEmptyException is thrown if the database is a
/// <see cref="QueueDatabase"/> or <see cref="RecnoDatabase"/>
/// database and <paramref name="key"/> exists, but was never explicitly
/// created by the application or was later deleted.
/// </exception>
/// <returns>
/// A <see cref="KeyValuePair{T,T}"/>
/// whose Key parameter is <paramref name="key"/> and whose Value
/// parameter is the retrieved data items.
/// </returns>
public KeyValuePair<DatabaseEntry, MultipleDatabaseEntry> GetMultiple(
DatabaseEntry key, int BufferSize) {
return GetMultiple(key, BufferSize, null, null);
}
/// <summary>
/// Retrieve a key and all duplicate data items from the database.
/// </summary>
/// <param name="key">The key to search for</param>
/// <param name="BufferSize">
/// The initial size of the buffer to fill with duplicate data items. If
/// the buffer is not large enough, it will be automatically resized.
/// </param>
/// <param name="txn">
/// <paramref name="txn"/> is a Transaction object returned from
/// <see cref="DatabaseEnvironment.BeginTransaction"/>; if
/// the operation is part of a Berkeley DB Concurrent Data Store group,
/// <paramref name="txn"/> is a handle returned from
/// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null.
/// </param>
/// <returns>
/// A <see cref="KeyValuePair{T,T}"/>
/// whose Key parameter is <paramref name="key"/> and whose Value
/// parameter is the retrieved data items.
/// </returns>
public KeyValuePair<DatabaseEntry, MultipleDatabaseEntry> GetMultiple(
DatabaseEntry key, int BufferSize, Transaction txn) {
return GetMultiple(key, BufferSize, txn, null);
}
/// <summary>
/// Retrieve a key and all duplicate data items from the database.
/// </summary>
/// <param name="key">The key to search for</param>
/// <param name="BufferSize">
/// The initial size of the buffer to fill with duplicate data items. If
/// the buffer is not large enough, it will be automatically resized.
/// </param>
/// <param name="txn">
/// <paramref name="txn"/> is a Transaction object returned from
/// <see cref="DatabaseEnvironment.BeginTransaction"/>; if
/// the operation is part of a Berkeley DB Concurrent Data Store group,
/// <paramref name="txn"/> is a handle returned from
/// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null.
/// </param>
/// <param name="info">The locking behavior to use.</param>
/// <returns>
/// A <see cref="KeyValuePair{T,T}"/>
/// whose Key parameter is <paramref name="key"/> and whose Value
/// parameter is the retrieved data items.
/// </returns>
public KeyValuePair<DatabaseEntry, MultipleDatabaseEntry> GetMultiple(
DatabaseEntry key,
int BufferSize, Transaction txn, LockingInfo info) {
KeyValuePair<DatabaseEntry, DatabaseEntry> kvp;
DatabaseEntry data = new DatabaseEntry();
for (; ; ) {
data.UserData = new byte[BufferSize];
try {
kvp = Get(key, data, txn, info, DbConstants.DB_MULTIPLE);
break;
} catch (MemoryException) {
int sz = (int)data.size;
if (sz > BufferSize)
BufferSize = sz;
else
BufferSize *= 2;
}
}
MultipleDatabaseEntry dbe = new MultipleDatabaseEntry(kvp.Value);
return new KeyValuePair<DatabaseEntry, MultipleDatabaseEntry>(
kvp.Key, dbe);
}
/// <summary>
/// Create a specialized join cursor for use in performing equality or
/// natural joins on secondary indices.
/// </summary>
/// <remarks>
/// <para>
/// Once the cursors have been passed as part of <paramref name="lst"/>,
/// they should not be accessed or modified until the newly created
/// <see cref="JoinCursor"/>has been closed, or else inconsistent
/// results may be returned.
/// </para>
/// <para>
/// Joined values are retrieved by doing a sequential iteration over the
/// first cursor in <paramref name="lst"/>, and a nested iteration over
/// each secondary cursor in the order they are specified in the
/// curslist parameter. This requires database traversals to search for
/// the current datum in all the cursors after the first. For this
/// reason, the best join performance normally results from sorting the
/// cursors from the one that refers to the least number of data items
/// to the one that refers to the most.
/// </para>
/// </remarks>
/// <param name="lst">
/// An array of SecondaryCursors. Each cursor must have been initialized
/// to refer to the key on which the underlying database should be
/// joined.
/// </param>
/// <param name="sortCursors">
/// If true, sort the cursors from the one that refers to the least
/// number of data items to the one that refers to the most. If the
/// data are structured so that cursors with many data items also share
/// many common elements, higher performance will result from listing
/// those cursors before cursors with fewer data items; that is, a sort
/// order other than the default. A setting of false permits
/// applications to perform join optimization prior to calling Join.
/// </param>
/// <returns>
/// A specialized join cursor for use in performing equality or natural
/// joins on secondary indices.
/// </returns>
public JoinCursor Join(SecondaryCursor[] lst, bool sortCursors) {
int i;
IntPtr[] cursList = new IntPtr[lst.Length + 1];
for (i = 0; i < lst.Length; i++)
cursList[i] = DBC.getCPtr(
SecondaryCursor.getDBC(lst[i])).Handle;
cursList[i] = IntPtr.Zero;
return new JoinCursor(db.join(cursList,
sortCursors ? 0 : DbConstants.DB_JOIN_NOSORT));
}
/// <summary>
/// Store the key/data pair in the database, replacing any previously
/// existing key if duplicates are disallowed, or adding a duplicate
/// data item if duplicates are allowed.
/// </summary>
/// <overloads>
/// <para>
/// If the database supports duplicates, add the new data value at the
/// end of the duplicate set. If the database supports sorted
/// duplicates, the new data value is inserted at the correct sorted
/// location.
/// </para>
/// </overloads>
/// <param name="key">The key to store in the database</param>
/// <param name="data">The data item to store in the database</param>
public void Put(DatabaseEntry key, DatabaseEntry data) {
Put(key, data, null);
}
/// <summary>
/// Store the key/data pair in the database, replacing any previously
/// existing key if duplicates are disallowed, or adding a duplicate
/// data item if duplicates are allowed.
/// </summary>
/// <param name="key">The key to store in the database</param>
/// <param name="data">The data item to store in the database</param>
/// <param name="txn">
/// If the operation is part of an application-specified transaction,
/// <paramref name="txn"/> is a Transaction object returned from
/// <see cref="DatabaseEnvironment.BeginTransaction"/>; if
/// the operation is part of a Berkeley DB Concurrent Data Store group,
/// <paramref name="txn"/> is a handle returned from
/// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null.
/// </param>
public void Put(
DatabaseEntry key, DatabaseEntry data, Transaction txn) {
Put(key, data, txn, 0);
}
/// <summary>
/// Store the key/data pair in the database, only if the key does not
/// already appear in the database.
/// </summary>
/// <remarks>
/// This enforcement of uniqueness of keys applies only to the primary
/// key, the behavior of insertions into secondary databases is not
/// affected. In particular, the insertion of a record that would result
/// in the creation of a duplicate key in a secondary database that
/// allows duplicates would not be prevented by the use of this flag.
/// </remarks>
/// <param name="key">The key to store in the database</param>
/// <param name="data">The data item to store in the database</param>
public void PutNoOverwrite(DatabaseEntry key, DatabaseEntry data) {
PutNoOverwrite(key, data, null);
}
/// <summary>
/// Store the key/data pair in the database, only if the key does not
/// already appear in the database.
/// </summary>
/// <remarks>
/// This enforcement of uniqueness of keys applies only to the primary
/// key, the behavior of insertions into secondary databases is not
/// affected. In particular, the insertion of a record that would result
/// in the creation of a duplicate key in a secondary database that
/// allows duplicates would not be prevented by the use of this flag.
/// </remarks>
/// <param name="key">The key to store in the database</param>
/// <param name="data">The data item to store in the database</param>
/// <param name="txn">
/// If the operation is part of an application-specified transaction,
/// <paramref name="txn"/> is a Transaction object returned from
/// <see cref="DatabaseEnvironment.BeginTransaction"/>; if
/// the operation is part of a Berkeley DB Concurrent Data Store group,
/// <paramref name="txn"/> is a handle returned from
/// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null.
/// </param>
public void PutNoOverwrite(
DatabaseEntry key, DatabaseEntry data, Transaction txn) {
Put(key, data, txn, DbConstants.DB_NOOVERWRITE);
}
/// <summary>
/// Protected wrapper for DB->put. Used by subclasses for access method
/// specific operations.
/// </summary>
/// <param name="key">The key to store in the database</param>
/// <param name="data">The data item to store in the database</param>
/// <param name="txn">Transaction with which to protect the put</param>
/// <param name="flags">Flags to pass to DB->put</param>
protected void Put(DatabaseEntry key,
DatabaseEntry data, Transaction txn, uint flags) {
db.put(Transaction.getDB_TXN(txn), key, data, flags);
}
/// <summary>
/// Write the key/data pairs from all databases in the file to
/// <see cref="Console.Out"/>. Key values are written for Btree, Hash
/// and Queue databases, but not for Recno databases.
/// </summary>
/// <param name="file">
/// The physical file in which the databases to be salvaged are found.
/// </param>
/// <param name="cfg">
/// Configuration parameters for the databases to be salvaged.
/// </param>
public static void Salvage(string file, DatabaseConfig cfg) {
Salvage(file, cfg, false, false, null);
}
/// <summary>
/// Write the key/data pairs from all databases in the file to
/// <see cref="Console.Out"/>. Key values are written for Btree, Hash
/// and Queue databases, but not for Recno databases.
/// </summary>
/// <param name="file">
/// The physical file in which the databases to be salvaged are found.
/// </param>
/// <param name="cfg">
/// Configuration parameters for the databases to be salvaged.
/// </param>
/// <param name="Printable">
/// If true and characters in either the key or data items are printing
/// characters (as defined by isprint(3)), use printing characters to
/// represent them. This setting permits users to use standard text
/// editors and tools to modify the contents of databases or selectively
/// remove data from salvager output.
/// </param>
public static void Salvage(
string file, DatabaseConfig cfg, bool Printable) {
Salvage(file, cfg, Printable, false, null);
}
/// <summary>
/// Write the key/data pairs from all databases in the file to
/// <paramref name="OutputStream"/>. Key values are written for Btree,
/// Hash and Queue databases, but not for Recno databases.
/// </summary>
/// <param name="file">
/// The physical file in which the databases to be salvaged are found.
/// </param>
/// <param name="cfg">
/// Configuration parameters for the databases to be salvaged.
/// </param>
/// <param name="OutputStream">
/// The TextWriter to which the databases' key/data pairs are written.
/// If null, <see cref="Console.Out"/> will be used.
/// </param>
public static void Salvage(
string file, DatabaseConfig cfg, TextWriter OutputStream) {
Salvage(file, cfg, false, false, OutputStream);
}
/// <summary>
/// Write the key/data pairs from all databases in the file to
/// <paramref name="OutputStream"/>. Key values are written for Btree,
/// Hash and Queue databases, but not for Recno databases.
/// </summary>
/// <param name="file">
/// The physical file in which the databases to be salvaged are found.
/// </param>
/// <param name="cfg">
/// Configuration parameters for the databases to be salvaged.
/// </param>
/// <param name="Printable">
/// If true and characters in either the key or data items are printing
/// characters (as defined by isprint(3)), use printing characters to
/// represent them. This setting permits users to use standard text
/// editors and tools to modify the contents of databases or selectively
/// remove data from salvager output.
/// </param>
/// <param name="OutputStream">
/// The TextWriter to which the databases' key/data pairs are written.
/// If null, <see cref="Console.Out"/> will be used.
/// </param>
public static void Salvage(string file,
DatabaseConfig cfg, bool Printable, TextWriter OutputStream) {
Salvage(file, cfg, Printable, false, OutputStream);
}
/// <summary>
/// Write the key/data pairs from all databases in the file to
/// <see cref="Console.Out"/>. Key values are written for Btree, Hash
/// and Queue databases, but not for Recno databases.
/// </summary>
/// <param name="file">
/// The physical file in which the databases to be salvaged are found.
/// </param>
/// <param name="cfg">
/// Configuration parameters for the databases to be salvaged.
/// </param>
/// <param name="Printable">
/// If true and characters in either the key or data items are printing
/// characters (as defined by isprint(3)), use printing characters to
/// represent them. This setting permits users to use standard text
/// editors and tools to modify the contents of databases or selectively
/// remove data from salvager output.
/// </param>
/// <param name="Aggressive">
/// If true, output all the key/data pairs in the file that can be
/// found. Corruption will be assumed and key/data pairs that are
/// corrupted or have been deleted may appear in the output (even if the
/// file being salvaged is in no way corrupt), and the output will
/// almost certainly require editing before being loaded into a
/// database.
/// </param>
public static void Salvage(string file,
DatabaseConfig cfg, bool Printable, bool Aggressive) {
Salvage(file, cfg, Printable, Aggressive, null);
}
/// <summary>
/// Write the key/data pairs from all databases in the file to
/// <paramref name="OutputStream"/>. Key values are written for Btree,
/// Hash and Queue databases, but not for Recno databases.
/// </summary>
/// <param name="file">
/// The physical file in which the databases to be salvaged are found.
/// </param>
/// <param name="cfg">
/// Configuration parameters for the databases to be salvaged.
/// </param>
/// <param name="Printable">
/// If true and characters in either the key or data items are printing
/// characters (as defined by isprint(3)), use printing characters to
/// represent them. This setting permits users to use standard text
/// editors and tools to modify the contents of databases or selectively
/// remove data from salvager output.
/// </param>
/// <param name="Aggressive">
/// If true, output all the key/data pairs in the file that can be
/// found. Corruption will be assumed and key/data pairs that are
/// corrupted or have been deleted may appear in the output (even if the
/// file being salvaged is in no way corrupt), and the output will
/// almost certainly require editing before being loaded into a
/// database.
/// </param>
/// <param name="OutputStream">
/// The TextWriter to which the databases' key/data pairs are written.
/// If null, <see cref="Console.Out"/> will be used.
/// </param>
public static void Salvage(string file, DatabaseConfig cfg,
bool Printable, bool Aggressive, TextWriter OutputStream) {
using (Database db = new Database(cfg.Env, 0)) {
db.Config(cfg);
if (OutputStream == null)
OutputStream = Console.Out;
uint flags = DbConstants.DB_SALVAGE;
flags |= Aggressive ? DbConstants.DB_AGGRESSIVE : 0;
flags |= Printable ? DbConstants.DB_PRINTABLE : 0;
writeToFileRef = new BDB_FileWriteDelegate(writeToFile);
db.db.verify(file, null, OutputStream, writeToFileRef, flags);
}
}
/// <summary>
/// Upgrade all of the databases included in the file
/// <paramref name="file"/>, if necessary. If no upgrade is necessary,
/// Upgrade always returns successfully.
/// </summary>
/// <param name="file">
/// The physical file containing the databases to be upgraded.
/// </param>
/// <param name="cfg">
/// Configuration parameters for the databases to be upgraded.
/// </param>
public static void Upgrade(string file, DatabaseConfig cfg) {
Upgrade(file, cfg, false);
}
/// <summary>
/// Upgrade all of the databases included in the file
/// <paramref name="file"/>, if necessary. If no upgrade is necessary,
/// Upgrade always returns successfully.
/// </summary>
/// <overloads>
/// Database upgrades are done in place and are destructive. For
/// example, if pages need to be allocated and no disk space is
/// available, the database may be left corrupted. Backups should be
/// made before databases are upgraded. See Upgrading databases in the
/// Programmer's Reference Guide for more information.
/// </overloads>
/// <remarks>
/// <para>
/// As part of the upgrade from the Berkeley DB 3.0 release to the 3.1
/// release, the on-disk format of duplicate data items changed. To
/// correctly upgrade the format requires applications to specify
/// whether duplicate data items in the database are sorted or not.
/// Specifying <paramref name="dupSortUpgraded"/> informs Upgrade that
/// the duplicates are sorted; otherwise they are assumed to be
/// unsorted. Incorrectly specifying the value of this flag may lead to
/// database corruption.
/// </para>
/// <para>
/// Further, because this method upgrades a physical file (including all
/// the databases it contains), it is not possible to use Upgrade to
/// upgrade files in which some of the databases it includes have sorted
/// duplicate data items, and some of the databases it includes have
/// unsorted duplicate data items. If the file does not have more than a
/// single database, if the databases do not support duplicate data
/// items, or if all of the databases that support duplicate data items
/// support the same style of duplicates (either sorted or unsorted),
/// Upgrade will work correctly as long as
/// <paramref name="dupSortUpgraded"/> is correctly specified.
/// Otherwise, the file cannot be upgraded using Upgrade it must be
/// upgraded manually by dumping and reloading the databases.
/// </para>
/// </remarks>
/// <param name="file">
/// The physical file containing the databases to be upgraded.
/// </param>
/// <param name="cfg">
/// Configuration parameters for the databases to be upgraded.
/// </param>
/// <param name="dupSortUpgraded">
/// If true, the duplicates in the upgraded database are sorted;
/// otherwise they are assumed to be unsorted. This setting is only
/// meaningful when upgrading databases from releases before the
/// Berkeley DB 3.1 release.
/// </param>
public static void Upgrade(
string file, DatabaseConfig cfg, bool dupSortUpgraded) {
Database db = new Database(cfg.Env, 0);
db.Config(cfg);
db.db.upgrade(file, dupSortUpgraded ? DbConstants.DB_DUPSORT : 0);
}
/// <summary>
/// Specifies the type of verification to perform
/// </summary>
public enum VerifyOperation {
/// <summary>
/// Perform database checks and check sort order
/// </summary>
DEFAULT,
/// <summary>
/// Perform the database checks for btree and duplicate sort order
/// and for hashing
/// </summary>
ORDER_CHECK_ONLY,
/// <summary>
/// Skip the database checks for btree and duplicate sort order and
/// for hashing.
/// </summary>
NO_ORDER_CHECK
};
/// <summary>
/// Verify the integrity of all databases in the file specified by
/// <paramref name="file"/>.
/// </summary>
/// <overloads>
/// Verify does not perform any locking, even in Berkeley DB
/// environments that are configured with a locking subsystem. As such,
/// it should only be used on files that are not being modified by
/// another thread of control.
/// </overloads>
/// <param name="file">
/// The physical file in which the databases to be verified are found.
/// </param>
/// <param name="cfg">
/// Configuration parameters for the databases to be verified.
/// </param>
public static void Verify(string file, DatabaseConfig cfg) {
Verify(file, null, cfg, VerifyOperation.DEFAULT);
}
/// <summary>
/// Verify the integrity of all databases in the file specified by
/// <paramref name="file"/>.
/// </summary>
/// <remarks>
/// <para>
/// Berkeley DB normally verifies that btree keys and duplicate items
/// are correctly sorted, and hash keys are correctly hashed. If the
/// file being verified contains multiple databases using differing
/// sorting or hashing algorithms, some of them must necessarily fail
/// database verification because only one sort order or hash function
/// can be specified in <paramref name="cfg"/>. To verify files with
/// multiple databases having differing sorting orders or hashing
/// functions, first perform verification of the file as a whole by
/// using <see cref="VerifyOperation.NO_ORDER_CHECK"/>, and then
/// individually verify the sort order and hashing function for each
/// database in the file using
/// <see cref="VerifyOperation.ORDER_CHECK_ONLY"/>.
/// </para>
/// </remarks>
/// <param name="file">
/// The physical file in which the databases to be verified are found.
/// </param>
/// <param name="cfg">
/// Configuration parameters for the databases to be verified.
/// </param>
/// <param name="op">The extent of verification</param>
public static void Verify(
string file, DatabaseConfig cfg, VerifyOperation op) {
Verify(file, null, cfg, op);
}
/// <summary>
/// Verify the integrity of the database specified by
/// <paramref name="file"/> and <paramref name="database"/>.
/// </summary>
/// <remarks>
/// <para>
/// Berkeley DB normally verifies that btree keys and duplicate items
/// are correctly sorted, and hash keys are correctly hashed. If the
/// file being verified contains multiple databases using differing
/// sorting or hashing algorithms, some of them must necessarily fail
/// database verification because only one sort order or hash function
/// can be specified in <paramref name="cfg"/>. To verify files with
/// multiple databases having differing sorting orders or hashing
/// functions, first perform verification of the file as a whole by
/// using <see cref="VerifyOperation.NO_ORDER_CHECK"/>, and then
/// individually verify the sort order and hashing function for each
/// database in the file using
/// <see cref="VerifyOperation.ORDER_CHECK_ONLY"/>.
/// </para>
/// </remarks>
/// <param name="file">
/// The physical file in which the databases to be verified are found.
/// </param>
/// <param name="database">
/// The database in <paramref name="file"/> on which the database checks
/// for btree and duplicate sort order and for hashing are to be
/// performed. A non-null value for database is only allowed with
/// <see cref="VerifyOperation.ORDER_CHECK_ONLY"/>.
/// </param>
/// <param name="cfg">
/// Configuration parameters for the databases to be verified.
/// </param>
/// <param name="op">The extent of verification</param>
public static void Verify(string file,
string database, DatabaseConfig cfg, VerifyOperation op) {
using (Database db = new Database(cfg.Env, 0)) {
db.Config(cfg);
uint flags;
switch (op) {
case VerifyOperation.NO_ORDER_CHECK:
flags = DbConstants.DB_NOORDERCHK;
break;
case VerifyOperation.ORDER_CHECK_ONLY:
flags = DbConstants.DB_ORDERCHKONLY;
break;
case VerifyOperation.DEFAULT:
default:
flags = 0;
break;
}
db.db.verify(file, database, null, null, flags);
}
}
#endregion Methods
}
}
|