2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 2009, 2012 Oracle and/or its affiliates. All rights reserved.
8 using System.Collections.Generic;
9 using System.Runtime.InteropServices;
11 using BerkeleyDB.Internal;
13 namespace BerkeleyDB {
15 /// A class representing a Berkeley DB database environment - a collection
16 /// including support for some or all of caching, locking, logging and
17 /// transaction subsystems, as well as databases and log files.
19 public class DatabaseEnvironment {
20 internal DB_ENV dbenv;
21 private IBackup backupObj;
22 private ErrorFeedbackDelegate errFeedbackHandler;
23 private EnvironmentFeedbackDelegate feedbackHandler;
24 private ThreadIsAliveDelegate isAliveHandler;
25 private EventNotifyDelegate notifyHandler;
26 private MessageDispatchDelegate messageDispatchHandler;
27 private ReplicationTransportDelegate transportHandler;
28 private SetThreadIDDelegate threadIDHandler;
29 private SetThreadNameDelegate threadNameHandler;
31 private DBTCopyDelegate CopyDelegate;
32 private BDB_BackupCloseDelegate doBackupCloseRef;
33 private BDB_BackupOpenDelegate doBackupOpenRef;
34 private BDB_BackupWriteDelegate doBackupWriteRef;
35 private BDB_ErrcallDelegate doErrFeedbackRef;
36 private BDB_EnvFeedbackDelegate doFeedbackRef;
37 private BDB_EventNotifyDelegate doNotifyRef;
38 private BDB_IsAliveDelegate doIsAliveRef;
39 private BDB_MessageDispatchDelegate doMessageDispatchRef;
40 private BDB_RepTransportDelegate doRepTransportRef;
41 private BDB_ThreadIDDelegate doThreadIDRef;
42 private BDB_ThreadNameDelegate doThreadNameRef;
44 private static long GIGABYTE = (long)(1 << 30);
47 private static int doBackupClose(IntPtr env, string dbname, IntPtr handle) {
48 DB_ENV dbenv = new DB_ENV(env, false);
49 return dbenv.api2_internal.backupObj.Close(dbname);
51 private static int doBackupOpen(IntPtr env, string dbname, string target, IntPtr handle) {
52 DB_ENV dbenv = new DB_ENV(env, false);
53 return dbenv.api2_internal.backupObj.Open(dbname, target);
55 private static int doBackupWrite(IntPtr env, uint off_gbytes, uint off_bytes, uint usize, IntPtr buf, IntPtr handle) {
57 long offset = off_gbytes * GIGABYTE + off_bytes;
58 DB_ENV dbenv = new DB_ENV(env, false);
59 if (usize > Int32.MaxValue)
60 size = Int32.MaxValue;
63 byte[] data = new byte[size];
64 Marshal.Copy(buf, data, 0, (int)size);
65 ret = dbenv.api2_internal.backupObj.Write(data, offset, (int)size);
66 if (ret == 0 && usize > Int32.MaxValue) {
67 size = (int)(usize - Int32.MaxValue);
69 * There's no need to re-allocate data, it's already as large as
70 * we could possibly need it to be. Advance buf beyond what was
71 * just copied and write the remaining data.
73 buf = new IntPtr(buf.ToInt64() + Int32.MaxValue);
74 Marshal.Copy(buf, data, 0, (int)size);
75 ret = dbenv.api2_internal.backupObj.Write(data, offset, (int)size);
79 private static void doNotify(IntPtr env, uint eventcode, byte[] event_info) {
80 DB_ENV dbenv = new DB_ENV(env, false);
82 dbenv.api2_internal.notifyHandler(
83 (NotificationEvent)eventcode, event_info);
85 private static void doErrFeedback(IntPtr env, string pfx, string msg) {
86 DB_ENV dbenv = new DB_ENV(env, false);
87 dbenv.api2_internal.errFeedbackHandler(
88 dbenv.api2_internal._pfx, msg);
90 private static void doFeedback(IntPtr env, int opcode, int percent) {
91 DB_ENV dbenv = new DB_ENV(env, false);
92 dbenv.api2_internal.feedbackHandler(
93 (EnvironmentFeedbackEvent)opcode, percent);
95 private static int doIsAlive(IntPtr env, int pid, uint tid, uint flags) {
96 DB_ENV dbenv = new DB_ENV(env, false);
97 DbThreadID id = new DbThreadID(pid, tid);
98 bool procOnly = (flags == DbConstants.DB_MUTEX_PROCESS_ONLY);
99 return dbenv.api2_internal.isAliveHandler(id, procOnly) ? 1 : 0;
101 private static void doMessageDispatch(IntPtr env, IntPtr channel,
102 IntPtr requestp, uint nrequest, uint cb_flags) {
103 DB_ENV dbenv = new DB_ENV(env, false);
104 DbChannel dbchannel = new DbChannel(new DB_CHANNEL(channel, false));
106 (cb_flags == DbConstants.DB_REPMGR_NEED_RESPONSE);
107 IntPtr[] reqp = new IntPtr[nrequest];
108 Marshal.Copy(requestp, reqp, 0, (int)nrequest);
109 DatabaseEntry[] requests = new DatabaseEntry[nrequest];
110 for (int i = 0; i < nrequest; i++) {
111 requests[i] = DatabaseEntry.fromDBT(new DBT(reqp[i], false));
113 dbenv.api2_internal.messageDispatchHandler(
114 dbchannel, ref requests, out nrequest, need_response);
116 private static int doRepTransport(IntPtr envp,
117 IntPtr controlp, IntPtr recp, IntPtr lsnp, int envid, uint flags) {
118 DB_ENV dbenv = new DB_ENV(envp, false);
119 DBT control = new DBT(controlp, false);
120 DBT rec = new DBT(recp, false);
121 DB_LSN tmplsn = new DB_LSN(lsnp, false);
122 LSN dblsn = new LSN(tmplsn.file, tmplsn.offset);
123 return dbenv.api2_internal.transportHandler(
124 DatabaseEntry.fromDBT(control),
125 DatabaseEntry.fromDBT(rec), dblsn, envid, flags);
127 private static void doThreadID(IntPtr env, IntPtr pid, IntPtr tid) {
128 DB_ENV dbenv = new DB_ENV(env, false);
129 DbThreadID id = dbenv.api2_internal.threadIDHandler();
132 * Sometimes the library doesn't care about either pid or tid
133 * (usually tid) and will pass NULL instead of a valid pointer.
135 if (pid != IntPtr.Zero)
136 Marshal.WriteInt32(pid, id.processID);
137 if (tid != IntPtr.Zero)
138 Marshal.WriteInt32(tid, (int)id.threadID);
140 private static string doThreadName(IntPtr env,
141 int pid, uint tid, ref string buf) {
142 DB_ENV dbenv = new DB_ENV(env, false);
143 DbThreadID id = new DbThreadID(pid, tid);
145 dbenv.api2_internal.threadNameHandler(id);
148 } catch (NullReferenceException) {
150 * The library may give us a NULL pointer in buf and there's no
151 * good way to test for that. Just ignore the exception if
152 * we're not able to set buf.
159 private DatabaseEnvironment(uint flags) {
160 dbenv = new DB_ENV(flags);
164 /* Called by Databases with private environments. */
165 internal DatabaseEnvironment(DB_ENV dbenvp) {
170 private void initialize() {
171 dbenv.api2_internal = this;
172 CopyDelegate = new DBTCopyDelegate(DatabaseEntry.dbt_usercopy);
173 dbenv.set_usercopy(CopyDelegate);
176 private void Config(DatabaseEnvironmentConfig cfg) {
177 //Alpha by dbenv function call
178 foreach (string dirname in cfg.DataDirs)
179 dbenv.add_data_dir(dirname);
180 if (cfg.CreationDir != null)
181 dbenv.set_create_dir(cfg.CreationDir);
182 if (cfg.encryptionIsSet)
184 cfg.EncryptionPassword, (uint)cfg.EncryptAlgorithm);
185 if (cfg.MetadataDir != null)
186 dbenv.set_metadata_dir(cfg.MetadataDir);
187 if (cfg.ErrorFeedback != null)
188 ErrorFeedback = cfg.ErrorFeedback;
189 ErrorPrefix = cfg.ErrorPrefix;
190 if (cfg.EventNotify != null)
191 EventNotify = cfg.EventNotify;
192 if (cfg.Feedback != null)
193 Feedback = cfg.Feedback;
194 if (cfg.IntermediateDirMode != null)
195 IntermediateDirMode = cfg.IntermediateDirMode;
196 if (cfg.ThreadIsAlive != null)
197 ThreadIsAlive = cfg.ThreadIsAlive;
198 if (cfg.threadCntIsSet)
199 ThreadCount = cfg.ThreadCount;
200 if (cfg.SetThreadID != null)
201 SetThreadID = cfg.SetThreadID;
202 if (cfg.ThreadName != null)
203 threadNameHandler = cfg.ThreadName;
204 if (cfg.lckTimeoutIsSet)
205 LockTimeout = cfg.LockTimeout;
206 if (cfg.txnTimeoutIsSet)
207 TxnTimeout = cfg.TxnTimeout;
208 if (cfg.TempDir != null)
209 TempDir = cfg.TempDir;
210 if (cfg.maxTxnsIsSet)
211 MaxTransactions = cfg.MaxTransactions;
212 if (cfg.txnTimestampIsSet)
213 TxnTimestamp = cfg.TxnTimestamp;
214 if (cfg.Verbosity != null)
215 Verbosity = cfg.Verbosity;
217 dbenv.set_flags(cfg.flags, 1);
218 if (cfg.initThreadCountIsSet)
219 InitThreadCount = cfg.InitThreadCount;
220 if (cfg.initTxnCountIsSet)
221 InitTxnCount = cfg.InitTxnCount;
223 if (cfg.LockSystemCfg != null) {
224 if (cfg.LockSystemCfg.Conflicts != null)
225 LockConflictMatrix = cfg.LockSystemCfg.Conflicts;
226 if (cfg.LockSystemCfg.DeadlockResolution != null)
227 DeadlockResolution = cfg.LockSystemCfg.DeadlockResolution;
228 if (cfg.LockSystemCfg.initLockerCountIsSet)
229 InitLockerCount = cfg.LockSystemCfg.InitLockerCount;
230 if (cfg.LockSystemCfg.initLockCountIsSet)
231 InitLockCount = cfg.LockSystemCfg.InitLockCount;
232 if (cfg.LockSystemCfg.initLockObjectCountIsSet)
233 InitLockObjectCount = cfg.LockSystemCfg.InitLockObjectCount;
234 if (cfg.LockSystemCfg.maxLockersIsSet)
235 MaxLockers = cfg.LockSystemCfg.MaxLockers;
236 if (cfg.LockSystemCfg.maxLocksIsSet)
237 MaxLocks = cfg.LockSystemCfg.MaxLocks;
238 if (cfg.LockSystemCfg.maxObjectsIsSet)
239 MaxObjects = cfg.LockSystemCfg.MaxObjects;
240 if (cfg.LockSystemCfg.partitionsIsSet)
241 LockPartitions = cfg.LockSystemCfg.Partitions;
242 if (cfg.LockSystemCfg.tablesizeIsSet)
243 LockTableSize = cfg.LockSystemCfg.TableSize;
246 if (cfg.LogSystemCfg != null) {
247 if (cfg.LogSystemCfg.bsizeIsSet)
248 LogBufferSize = cfg.LogSystemCfg.BufferSize;
249 if (cfg.LogSystemCfg.Dir != null)
250 LogDir = cfg.LogSystemCfg.Dir;
251 if (cfg.LogSystemCfg.initLogIdCountIsSet)
252 InitLogIdCount = cfg.LogSystemCfg.InitLogIdCount;
253 if (cfg.LogSystemCfg.modeIsSet)
254 LogFileMode = cfg.LogSystemCfg.FileMode;
255 if (cfg.LogSystemCfg.maxSizeIsSet)
256 MaxLogFileSize = cfg.LogSystemCfg.MaxFileSize;
257 if (cfg.LogSystemCfg.regionSizeIsSet)
258 LogRegionSize = cfg.LogSystemCfg.RegionSize;
259 if (cfg.LogSystemCfg.ConfigFlags != 0)
260 dbenv.log_set_config(cfg.LogSystemCfg.ConfigFlags, 1);
263 if (cfg.MPoolSystemCfg != null) {
264 if (cfg.MPoolSystemCfg.CacheSize != null)
265 CacheSize = cfg.MPoolSystemCfg.CacheSize;
266 if (cfg.MPoolSystemCfg.MaxCacheSize != null)
267 MaxCacheSize = cfg.MPoolSystemCfg.MaxCacheSize;
268 if (cfg.MPoolSystemCfg.maxOpenFDIsSet)
269 MaxOpenFiles = cfg.MPoolSystemCfg.MaxOpenFiles;
270 if (cfg.MPoolSystemCfg.maxSeqWriteIsSet)
271 SetMaxSequentialWrites(
272 cfg.MPoolSystemCfg.MaxSequentialWrites,
273 cfg.MPoolSystemCfg.SequentialWritePause);
274 if (cfg.MPoolSystemCfg.mmapSizeSet)
275 MMapSize = cfg.MPoolSystemCfg.MMapSize;
278 if (cfg.MutexSystemCfg != null) {
279 if (cfg.MutexSystemCfg.alignmentIsSet)
280 MutexAlignment = cfg.MutexSystemCfg.Alignment;
282 * Setting max after increment ensures that the value of max
283 * will win if both max and increment are set. This is the
284 * behavior we document in MutexConfig.
286 if (cfg.MutexSystemCfg.incrementIsSet)
287 MutexIncrement = cfg.MutexSystemCfg.Increment;
288 if (cfg.MutexSystemCfg.initMutexesIsSet)
289 InitMutexes = cfg.MutexSystemCfg.InitMutexes;
290 if (cfg.MutexSystemCfg.maxMutexesIsSet)
291 MaxMutexes = cfg.MutexSystemCfg.MaxMutexes;
292 if (cfg.MutexSystemCfg.numTASIsSet)
293 NumTestAndSetSpins = cfg.MutexSystemCfg.NumTestAndSetSpins;
296 if (cfg.RepSystemCfg != null) {
297 if (cfg.RepSystemCfg.ackTimeoutIsSet)
298 RepAckTimeout = cfg.RepSystemCfg.AckTimeout;
299 if (cfg.RepSystemCfg.BulkTransfer)
300 RepBulkTransfer = true;
301 if (cfg.RepSystemCfg.checkpointDelayIsSet)
302 RepCheckpointDelay = cfg.RepSystemCfg.CheckpointDelay;
303 if (cfg.RepSystemCfg.clockskewIsSet)
304 RepSetClockskew(cfg.RepSystemCfg.ClockskewFast,
305 cfg.RepSystemCfg.ClockskewSlow);
306 if (cfg.RepSystemCfg.connectionRetryIsSet)
307 RepConnectionRetry = cfg.RepSystemCfg.ConnectionRetry;
308 if (cfg.RepSystemCfg.DelayClientSync)
309 RepDelayClientSync = true;
310 if (cfg.RepSystemCfg.electionRetryIsSet)
311 RepElectionRetry = cfg.RepSystemCfg.ElectionRetry;
312 if (cfg.RepSystemCfg.electionTimeoutIsSet)
313 RepElectionTimeout = cfg.RepSystemCfg.ElectionTimeout;
314 if (cfg.RepSystemCfg.fullElectionTimeoutIsSet)
315 RepFullElectionTimeout =
316 cfg.RepSystemCfg.FullElectionTimeout;
317 if (cfg.RepSystemCfg.heartbeatMonitorIsSet)
318 RepHeartbeatMonitor = cfg.RepSystemCfg.HeartbeatMonitor;
319 if (cfg.RepSystemCfg.heartbeatSendIsSet)
320 RepHeartbeatSend = cfg.RepSystemCfg.HeartbeatSend;
321 if (cfg.RepSystemCfg.InMemory)
322 dbenv.rep_set_config(DbConstants.DB_REP_CONF_INMEM, 1);
323 if (cfg.RepSystemCfg.leaseTimeoutIsSet)
324 RepLeaseTimeout = cfg.RepSystemCfg.LeaseTimeout;
325 if (!cfg.RepSystemCfg.AutoInit)
327 if (cfg.RepSystemCfg.NoBlocking)
328 RepNoBlocking = true;
329 if (cfg.RepSystemCfg.nsitesIsSet)
330 RepNSites = cfg.RepSystemCfg.NSites;
331 for (int i = 0; i < cfg.RepSystemCfg.RepmgrSitesConfig.Count; i++)
332 RepMgrSiteConfig(cfg.RepSystemCfg.RepmgrSitesConfig[i]);
333 if (cfg.RepSystemCfg.priorityIsSet)
334 RepPriority = cfg.RepSystemCfg.Priority;
335 if (cfg.RepSystemCfg.RepMgrAckPolicy != null)
336 RepMgrAckPolicy = cfg.RepSystemCfg.RepMgrAckPolicy;
337 if (cfg.RepSystemCfg.retransmissionRequestIsSet)
338 RepSetRetransmissionRequest(
339 cfg.RepSystemCfg.RetransmissionRequestMin,
340 cfg.RepSystemCfg.RetransmissionRequestMax);
341 if (cfg.RepSystemCfg.Strict2Site)
342 RepStrict2Site = true;
343 if (cfg.RepSystemCfg.transmitLimitIsSet)
345 cfg.RepSystemCfg.TransmitLimitGBytes,
346 cfg.RepSystemCfg.TransmitLimitBytes);
347 if (cfg.RepSystemCfg.UseMasterLeases)
348 RepUseMasterLeases = true;
349 if (!cfg.RepSystemCfg.Elections)
350 RepMgrRunElections = false;
357 /// If true, database operations for which no explicit transaction
358 /// handle was specified, and which modify databases in the database
359 /// environment, will be automatically enclosed within a transaction.
361 public bool AutoCommit {
364 dbenv.get_flags(ref flags);
365 return (flags & DbConstants.DB_AUTO_COMMIT) != 0;
368 dbenv.set_flags(DbConstants.DB_AUTO_COMMIT, value ? 1 : 0);
373 /// The size of the buffer, in bytes, to read from the database during a
376 public uint BackupBufferSize {
379 dbenv.get_backup_config(DbConstants.DB_BACKUP_SIZE, ref ret);
383 dbenv.set_backup_config(DbConstants.DB_BACKUP_SIZE, value);
387 /// Sets the <see cref="IBackup"/> interface to be used when performing
390 /// This interface is used to override the default behavior used by the
391 /// <see cref="DatabaseEnvironment.Backup"/> and
392 /// <see cref="DatabaseEnvironment.BackupDatabase"/> methods.
395 public IBackup BackupHandler {
396 get { return backupObj; }
399 dbenv.set_backup_callbacks(null, null, null);
400 } else if (backupObj == null) {
401 if (doBackupCloseRef == null)
403 new BDB_BackupCloseDelegate(doBackupClose);
404 if (doBackupOpenRef == null)
406 new BDB_BackupOpenDelegate(doBackupOpen);
407 if (doBackupWriteRef == null)
409 new BDB_BackupWriteDelegate(doBackupWrite);
410 dbenv.set_backup_callbacks(
411 doBackupOpenRef, doBackupWriteRef, doBackupCloseRef);
418 /// The number of pages to read before pausing during the hot backup.
420 /// Increasing this value increases the amount of I/O the backup process
421 /// performs for any given time interval. If your application is already
422 /// heavily I/O bound, setting this value to a lower number may help to
423 /// improve your overall data throughput by reducing the I/O demands
424 /// placed on your system. By default, all pages are read without a
428 public uint BackupReadCount {
431 dbenv.get_backup_config(DbConstants.DB_BACKUP_READ_COUNT, ref ret);
435 dbenv.set_backup_config(DbConstants.DB_BACKUP_READ_COUNT, value);
439 /// The number of microseconds to sleep between batches of reads during
442 /// Increasing this value decreases the amount of I/O the backup process
443 /// performs for any given time interval. If your application is already
444 /// heavily I/O bound, setting this value to a higher number may help to
445 /// improve your overall data throughput by reducing the I/O demands
446 /// placed on your system.
449 public uint BackupReadSleepDuration {
452 dbenv.get_backup_config(DbConstants.DB_BACKUP_READ_SLEEP, ref ret);
456 dbenv.set_backup_config(DbConstants.DB_BACKUP_READ_SLEEP, value);
460 /// If true, direct I/O is used when writing pages to the disk during a
463 /// For some environments, direct I/O can provide faster write
464 /// throughput, but usually it is slower because the OS buffer pool
465 /// offers asynchronous activity.
468 public bool BackupWriteDirect {
471 dbenv.get_backup_config(DbConstants.DB_BACKUP_WRITE_DIRECT, ref ret);
475 dbenv.set_backup_config(DbConstants.DB_BACKUP_WRITE_DIRECT, (uint)(value ? 1 : 0));
480 /// The size of the shared memory buffer pool -- that is, the cache.
484 /// The cache should be the size of the normal working data set of the
485 /// application, with some small amount of additional memory for unusual
486 /// situations. (Note: the working set is not the same as the number of
487 /// pages accessed simultaneously, and is usually much larger.)
490 /// The default cache size is 256KB, and may not be specified as less
491 /// than 20KB. Any cache size less than 500MB is automatically increased
492 /// by 25% to account for buffer pool overhead; cache sizes larger than
493 /// 500MB are used as specified. The maximum size of a single cache is
494 /// 4GB on 32-bit systems and 10TB on 64-bit systems. (All sizes are in
495 /// powers-of-two, that is, 256KB is 2^18 not 256,000.) For information
496 /// on tuning the Berkeley DB cache size, see Selecting a cache size in
497 /// the Programmer's Reference Guide.
500 public CacheInfo CacheSize {
505 dbenv.get_cachesize(ref gb, ref b, ref n);
506 return new CacheInfo(gb, b, n);
511 value.Gigabytes, value.Bytes, value.NCaches);
515 /// If true, Berkeley DB Concurrent Data Store applications will perform
516 /// locking on an environment-wide basis rather than on a per-database
519 public bool CDB_ALLDB {
522 dbenv.get_flags(ref flags);
523 return (flags & DbConstants.DB_CDB_ALLDB) != 0;
527 /// If true, Berkeley DB subsystems will create any underlying files, as
533 dbenv.get_open_flags(ref flags);
534 return (flags & DbConstants.DB_CREATE) != 0;
538 /// The array of directories where database files are stored.
540 public List<string> DataDirs { get { return dbenv.get_data_dirs(); } }
543 /// The deadlock detector configuration, specifying what lock request(s)
544 /// should be rejected. As transactions acquire locks on behalf of a
545 /// single locker ID, rejecting a lock request associated with a
546 /// transaction normally requires the transaction be aborted.
548 public DeadlockPolicy DeadlockResolution {
551 dbenv.get_lk_detect(ref mode);
552 return DeadlockPolicy.fromPolicy(mode);
556 dbenv.set_lk_detect(value.policy);
558 dbenv.set_lk_detect(DeadlockPolicy.DEFAULT.policy);
562 /// The algorithm used by the Berkeley DB library to perform encryption
565 public EncryptionAlgorithm EncryptAlgorithm {
568 dbenv.get_encrypt_flags(ref flags);
569 return (EncryptionAlgorithm)Enum.ToObject(
570 typeof(EncryptionAlgorithm), flags);
574 /// The mechanism for reporting detailed error messages to the
579 /// When an error occurs in the Berkeley DB library, a
580 /// <see cref="DatabaseException"/>, or subclass of DatabaseException,
581 /// is thrown. In some cases, however, the exception may be insufficient
582 /// to completely describe the cause of the error, especially during
583 /// initial application debugging.
586 /// In some cases, when an error occurs, Berkeley DB will call the given
587 /// delegate with additional error information. It is up to the delegate
588 /// to display the error message in an appropriate manner.
591 /// Setting ErrorFeedback to NULL unconfigures the callback interface.
594 /// This error-logging enhancement does not slow performance or
595 /// significantly increase application size, and may be run during
596 /// normal operation as well as during application debugging.
599 public ErrorFeedbackDelegate ErrorFeedback {
600 get { return errFeedbackHandler; }
603 dbenv.set_errcall(null);
604 else if (errFeedbackHandler == null) {
605 if (doErrFeedbackRef == null)
606 doErrFeedbackRef = new BDB_ErrcallDelegate(doErrFeedback);
607 dbenv.set_errcall(doErrFeedbackRef);
609 errFeedbackHandler = value;
613 /// The prefix string that appears before error messages issued by
618 /// For databases opened inside of a DatabaseEnvironment, setting
619 /// ErrorPrefix affects the entire environment and is equivalent to
620 /// setting <see cref="DatabaseEnvironment.ErrorPrefix"/>.
623 /// Setting ErrorPrefix configures operations performed using the
624 /// specified object, not all operations performed on the underlying
628 public string ErrorPrefix {
630 set { _pfx = value; }
634 /// A delegate which is called to notify the process of specific
635 /// Berkeley DB events.
637 public EventNotifyDelegate EventNotify {
638 get { return notifyHandler; }
641 dbenv.set_event_notify(null);
642 else if (notifyHandler == null) {
643 if (doNotifyRef == null)
644 doNotifyRef = new BDB_EventNotifyDelegate(doNotify);
645 dbenv.set_event_notify(doNotifyRef);
648 notifyHandler = value;
652 /// Monitor progress within long running operations.
656 /// Some operations performed by the Berkeley DB library can take
657 /// non-trivial amounts of time. The Feedback delegate can be used by
658 /// applications to monitor progress within these operations. When an
659 /// operation is likely to take a long time, Berkeley DB will call the
660 /// specified delegate with progress information.
663 /// It is up to the delegate to display this information in an
664 /// appropriate manner.
667 public EnvironmentFeedbackDelegate Feedback {
668 get { return feedbackHandler; }
671 dbenv.set_feedback(null);
672 else if (feedbackHandler == null) {
673 if (doFeedbackRef == null)
674 doFeedbackRef = new BDB_EnvFeedbackDelegate(doFeedback);
675 dbenv.set_feedback(doFeedbackRef);
677 feedbackHandler = value;
681 /// If true, flush database writes to the backing disk before returning
682 /// from the write system call, rather than flushing database writes
683 /// explicitly in a separate system call, as necessary.
686 /// This flag may result in inaccurate file modification times and other
687 /// file-level information for Berkeley DB database files. This flag
688 /// will almost certainly result in a performance decrease on most
691 public bool ForceFlush {
694 dbenv.get_flags(ref flags);
695 return (flags & DbConstants.DB_DSYNC_DB) != 0;
698 dbenv.set_flags(DbConstants.DB_DSYNC_DB, value ? 1 : 0);
702 /// If true, the object is free-threaded; that is, concurrently usable
703 /// by multiple threads in the address space.
705 public bool FreeThreaded {
708 dbenv.get_open_flags(ref flags);
709 return (flags & DbConstants.DB_THREAD) != 0;
713 /// The database environment home directory.
718 dbenv.get_home(out dir);
723 /// Whether there is any hot backup in progress.
725 public bool HotbackupInProgress {
728 dbenv.get_flags(ref flags);
729 return (flags & DbConstants.DB_HOTBACKUP_IN_PROGRESS) != 0;
733 DbConstants.DB_HOTBACKUP_IN_PROGRESS, value ? 1 : 0);
737 /// The number of locks allocated when the environment is created
739 public uint InitLockCount {
742 dbenv.get_memory_init(DbConstants.DB_MEM_LOCK, ref ret);
746 dbenv.set_memory_init(DbConstants.DB_MEM_LOCK, value);
750 /// The number of lock objects allocated when the environment is created
752 public uint InitLockObjectCount {
755 dbenv.get_memory_init(DbConstants.DB_MEM_LOCKOBJECT, ref ret);
759 dbenv.set_memory_init(DbConstants.DB_MEM_LOCKOBJECT, value);
763 /// The number of lockers allocated when the environment is created
765 public uint InitLockerCount {
768 dbenv.get_memory_init(DbConstants.DB_MEM_LOCKER, ref ret);
772 dbenv.set_memory_init(DbConstants.DB_MEM_LOCKER, value);
776 /// The number of log identifier objects allocated when the
777 /// environment is created
779 public uint InitLogIdCount {
782 dbenv.get_memory_init(DbConstants.DB_MEM_LOGID, ref ret);
786 dbenv.set_memory_init(DbConstants.DB_MEM_LOGID, value);
790 /// The number of thread objects allocated when the environment is
793 public uint InitThreadCount {
796 dbenv.get_memory_init(DbConstants.DB_MEM_THREAD, ref ret);
800 dbenv.set_memory_init(DbConstants.DB_MEM_THREAD, value);
804 /// The number of transaction objects allocated when the environment is
807 public uint InitTxnCount {
810 dbenv.get_memory_init(DbConstants.DB_MEM_TRANSACTION, ref ret);
814 dbenv.set_memory_init(DbConstants.DB_MEM_TRANSACTION, value);
818 /// The initial number of mutexes allocated
820 public uint InitMutexes {
823 dbenv.mutex_get_init(ref ret);
827 dbenv.mutex_set_init(value);
831 /// If true, Berkeley DB will page-fault shared regions into memory when
832 /// initially creating or joining a Berkeley DB environment.
836 /// In some applications, the expense of page-faulting the underlying
837 /// shared memory regions can affect performance. (For example, if the
838 /// page-fault occurs while holding a lock, other lock requests can
839 /// convoy, and overall throughput may decrease.)
842 /// In addition to page-faulting, Berkeley DB will write the shared
843 /// regions when creating an environment, forcing the underlying virtual
844 /// memory and filesystems to instantiate both the necessary memory and
845 /// the necessary disk space. This can also avoid out-of-disk space
846 /// failures later on.
849 public bool InitRegions {
852 dbenv.get_flags(ref flags);
853 return (flags & DbConstants.DB_REGION_INIT) != 0;
856 dbenv.set_flags(DbConstants.DB_REGION_INIT, value ? 1 : 0);
860 /// The intermediate directory permissions.
862 public string IntermediateDirMode {
865 dbenv.get_intermediate_dir_mode(out ret);
869 dbenv.set_intermediate_dir_mode(value);
874 /// The current lock conflicts array.
876 public byte[,] LockConflictMatrix {
879 dbenv.get_lk_conflicts_nmodes(ref sz);
880 byte [,] ret = new byte[sz, sz];
881 dbenv.get_lk_conflicts(ret);
885 // Matrix dimensions checked in LockingConfig.
886 dbenv.set_lk_conflicts(value, (int)Math.Sqrt(value.Length));
890 /// If true, lock shared Berkeley DB environment files and memory-mapped
891 /// databases into memory.
893 public bool Lockdown {
896 dbenv.get_open_flags(ref flags);
897 return (flags & DbConstants.DB_LOCKDOWN) != 0;
901 /// The size of the lock table in the Berkeley DB environment.
903 public uint LockTableSize {
906 dbenv.get_lk_tablesize(ref ret);
910 dbenv.set_lk_tablesize(value);
914 /// The number of lock table partitions used in the Berkeley DB
917 public uint LockPartitions {
920 dbenv.get_lk_partitions(ref ret);
924 dbenv.set_lk_partitions(value);
928 /// A value, in microseconds, representing lock timeouts.
932 /// All timeouts are checked whenever a thread of control blocks on a
933 /// lock or when deadlock detection is performed. As timeouts are only
934 /// checked when the lock request first blocks or when deadlock
935 /// detection is performed, the accuracy of the timeout depends on how
936 /// often deadlock detection is performed.
939 /// Timeout values specified for the database environment may be
940 /// overridden on a per-transaction basis, see
941 /// <see cref="Transaction.SetLockTimeout"/>.
944 public uint LockTimeout {
947 dbenv.get_timeout(ref timeout, DbConstants.DB_SET_LOCK_TIMEOUT);
951 dbenv.set_timeout(value, DbConstants.DB_SET_LOCK_TIMEOUT);
955 /// The size of the in-memory log buffer, in bytes
957 public uint LogBufferSize {
960 dbenv.get_lg_bsize(ref ret);
964 dbenv.set_lg_bsize(value);
968 /// The path of a directory to be used as the location of logging files.
969 /// Log files created by the Log Manager subsystem will be created in
972 public string LogDir {
975 dbenv.get_lg_dir(out ret);
979 dbenv.set_lg_dir(value);
983 /// The absolute file mode for created log files. This property is only
984 /// useful for the rare Berkeley DB application that does not control
988 /// Normally, if Berkeley DB applications set their umask appropriately,
989 /// all processes in the application suite will have read permission on
990 /// the log files created by any process in the application suite.
991 /// However, if the Berkeley DB application is a library, a process
992 /// using the library might set its umask to a value preventing other
993 /// processes in the application suite from reading the log files it
994 /// creates. In this rare case, this property can be used to set the
995 /// mode of created log files to an absolute value.
997 public int LogFileMode {
1000 dbenv.get_lg_filemode(ref ret);
1004 dbenv.set_lg_filemode(value);
1008 /// If true, system buffering is turned off for Berkeley DB log files to
1009 /// avoid double caching.
1011 public bool LogNoBuffer {
1014 dbenv.log_get_config(DbConstants.DB_LOG_DIRECT, ref onoff);
1015 return (onoff != 0);
1018 dbenv.log_set_config(DbConstants.DB_LOG_DIRECT, value ? 1 : 0);
1022 /// If true, Berkeley DB will flush log writes to the backing disk
1023 /// before returning from the write system call, rather than flushing
1024 /// log writes explicitly in a separate system call, as necessary.
1026 public bool LogForceSync {
1029 dbenv.log_get_config(DbConstants.DB_LOG_DSYNC, ref onoff);
1030 return (onoff != 0);
1033 dbenv.log_set_config(DbConstants.DB_LOG_DSYNC, value ? 1 : 0);
1037 /// If true, Berkeley DB will automatically remove log files that are no
1040 public bool LogAutoRemove {
1043 dbenv.log_get_config(DbConstants.DB_LOG_AUTO_REMOVE, ref onoff);
1044 return (onoff != 0);
1047 dbenv.log_set_config(
1048 DbConstants.DB_LOG_AUTO_REMOVE, value ? 1 : 0);
1052 /// If true, transaction logs are maintained in memory rather than on
1053 /// disk. This means that transactions exhibit the ACI (atomicity,
1054 /// consistency, and isolation) properties, but not D (durability).
1056 public bool LogInMemory {
1059 dbenv.log_get_config(DbConstants.DB_LOG_IN_MEMORY, ref onoff);
1060 return (onoff != 0);
1064 /// If true, all pages of a log file are zeroed when that log file is
1067 public bool LogZeroOnCreate {
1070 dbenv.log_get_config(DbConstants.DB_LOG_ZERO, ref onoff);
1071 return (onoff != 0);
1075 /// The size of the underlying logging area of the Berkeley DB
1076 /// environment, in bytes.
1078 public uint LogRegionSize {
1081 dbenv.get_lg_regionmax(ref ret);
1085 dbenv.set_lg_regionmax(value);
1089 /// The maximum cache size
1091 public CacheInfo MaxCacheSize {
1095 dbenv.get_cache_max(ref gb, ref b);
1096 return new CacheInfo(gb, b, 0);
1099 dbenv.set_cache_max(value.Gigabytes, value.Bytes);
1103 /// The maximum size of a single file in the log, in bytes. Because
1104 /// <see cref="LSN.Offset">LSN Offsets</see> are unsigned four-byte
1105 /// values, the size may not be larger than the maximum unsigned
1106 /// four-byte value.
1110 /// When the logging subsystem is configured for on-disk logging, the
1111 /// default size of a log file is 10MB.
1114 /// When the logging subsystem is configured for in-memory logging, the
1115 /// default size of a log file is 256KB. In addition, the configured log
1116 /// buffer size must be larger than the log file size. (The logging
1117 /// subsystem divides memory configured for in-memory log records into
1118 /// "files", as database environments configured for in-memory log
1119 /// records may exchange log records with other members of a replication
1120 /// group, and those members may be configured to store log records
1121 /// on-disk.) When choosing log buffer and file sizes for in-memory
1122 /// logs, applications should ensure the in-memory log buffer size is
1123 /// large enough that no transaction will ever span the entire buffer,
1124 /// and avoid a state where the in-memory buffer is full and no space
1125 /// can be freed because a transaction that started in the first log
1126 /// "file" is still active.
1129 /// See Log File Limits in the Programmer's Reference Guide for more
1133 /// If no size is specified by the application, the size last specified
1134 /// for the database region will be used, or if no database region
1135 /// previously existed, the default will be used.
1138 public uint MaxLogFileSize {
1141 dbenv.get_lg_max(ref ret);
1145 dbenv.set_lg_max(value);
1149 /// The maximum number of locking entities supported by the Berkeley DB
1152 public uint MaxLockers {
1155 dbenv.get_lk_max_lockers(ref ret);
1159 dbenv.set_lk_max_lockers(value);
1163 /// The maximum number of locks supported by the Berkeley DB
1166 public uint MaxLocks {
1169 dbenv.get_lk_max_locks(ref ret);
1173 dbenv.set_lk_max_locks(value);
1177 /// The total number of mutexes allocated
1179 public uint MaxMutexes {
1182 dbenv.mutex_get_max(ref ret);
1186 dbenv.mutex_set_max(value);
1190 /// The maximum number of locked objects
1192 public uint MaxObjects {
1195 dbenv.get_lk_max_objects(ref ret);
1199 dbenv.set_lk_max_objects(value);
1203 /// The number of file descriptors the library will open concurrently
1204 /// when flushing dirty pages from the cache.
1206 public int MaxOpenFiles {
1209 dbenv.get_mp_max_openfd(ref ret);
1213 dbenv.set_mp_max_openfd(value);
1217 /// The number of sequential write operations scheduled by the library
1218 /// when flushing dirty pages from the cache.
1220 public int MaxSequentialWrites {
1224 dbenv.get_mp_max_write(ref ret, ref tmp);
1229 /// The number of active transactions supported by the environment. This
1230 /// value bounds the size of the memory allocated for transactions.
1231 /// Child transactions are counted as active until they either commit or
1236 /// Transactions that update multiversion databases are not freed until
1237 /// the last page version that the transaction created is flushed from
1238 /// cache. This means that applications using multi-version concurrency
1239 /// control may need a transaction for each page in cache, in the
1243 /// When all of the memory available in the database environment for
1244 /// transactions is in use, calls to <see cref="BeginTransaction"/> will
1245 /// fail (until some active transactions complete). If MaxTransactions
1246 /// is never set, the database environment is configured to support at
1247 /// least 100 active transactions.
1250 public uint MaxTransactions {
1253 dbenv.get_tx_max(ref ret);
1257 dbenv.set_tx_max(value);
1261 /// The path of directory to store the persistent metadata.
1265 /// By default, metadata is stored in the environment home directory.
1266 /// See Berkeley DB File Naming in the Programmer's Reference Guide for
1267 /// more information.
1270 /// When used in a replicated application, the metadata directory must
1271 /// be the same location for all sites within a replication group.
1274 public string MetadataDir {
1277 dbenv.get_metadata_dir(out mddir);
1282 /// The maximum file size, in bytes, for a file to be mapped into the
1283 /// process address space. If no value is specified, it defaults to
1287 /// Files that are opened read-only in the cache (and that satisfy a few
1288 /// other criteria) are, by default, mapped into the process address
1289 /// space instead of being copied into the local cache. This can result
1290 /// in better-than-usual performance because available virtual memory is
1291 /// normally much larger than the local cache, and page faults are
1292 /// faster than page copying on many systems. However, it can cause
1293 /// resource starvation in the presence of limited virtual memory, and
1294 /// it can result in immense process sizes in the presence of large
1297 public uint MMapSize {
1300 dbenv.get_mp_mmapsize(ref ret);
1304 dbenv.set_mp_mmapsize(value);
1308 /// The mutex alignment, in bytes.
1310 public uint MutexAlignment {
1313 dbenv.mutex_get_align(ref ret);
1317 dbenv.mutex_set_align(value);
1321 /// The number of additional mutexes allocated.
1323 public uint MutexIncrement {
1326 dbenv.mutex_get_increment(ref ret);
1330 dbenv.mutex_set_increment(value);
1334 /// If true, turn off system buffering of Berkeley DB database files to
1335 /// avoid double caching.
1337 public bool NoBuffer {
1340 dbenv.get_flags(ref flags);
1341 return (flags & DbConstants.DB_DIRECT_DB) != 0;
1344 dbenv.set_flags(DbConstants.DB_DIRECT_DB, value ? 1 : 0);
1348 /// If true, Berkeley DB will grant all requested mutual exclusion
1349 /// mutexes and database locks without regard for their actual
1350 /// availability. This functionality should never be used for purposes
1351 /// other than debugging.
1353 public bool NoLocking {
1356 dbenv.get_flags(ref flags);
1357 return (flags & DbConstants.DB_NOLOCKING) != 0;
1360 dbenv.set_flags(DbConstants.DB_NOLOCKING, value ? 1 : 0);
1364 /// If true, Berkeley DB will copy read-only database files into the
1365 /// local cache instead of potentially mapping them into process memory.
1367 /// <seealso cref="MMapSize"/>
1368 public bool NoMMap {
1371 dbenv.get_flags(ref flags);
1372 return (flags & DbConstants.DB_NOMMAP) != 0;
1375 dbenv.set_flags(DbConstants.DB_NOMMAP, value ? 1 : 0);
1379 /// If true, Berkeley DB will ignore any panic state in the database
1380 /// environment. (Database environments in a panic state normally refuse
1381 /// all attempts to call Berkeley DB functions, throwing
1382 /// <see cref="RunRecoveryException"/>.) This functionality should never
1383 /// be used for purposes other than debugging.
1385 public bool NoPanic {
1388 dbenv.get_flags(ref flags);
1389 return (flags & DbConstants.DB_NOPANIC) != 0;
1392 dbenv.set_flags(DbConstants.DB_NOPANIC, value ? 1 : 0);
1396 /// The number of times that test-and-set mutexes should spin without
1397 /// blocking. The value defaults to 1 on uniprocessor systems and to 50
1398 /// times the number of processors on multiprocessor systems.
1400 public uint NumTestAndSetSpins {
1403 dbenv.mutex_get_tas_spins(ref ret);
1407 dbenv.mutex_set_tas_spins(value);
1411 /// If true, overwrite files stored in encrypted formats before deleting
1415 /// Berkeley DB overwrites files using alternating 0xff, 0x00 and 0xff
1416 /// byte patterns. For file overwriting to be effective, the underlying
1417 /// file must be stored on a fixed-block filesystem. Systems with
1418 /// journaling or logging filesystems will require operating system
1419 /// support and probably modification of the Berkeley DB sources.
1421 public bool Overwrite {
1424 dbenv.get_flags(ref flags);
1425 return (flags & DbConstants.DB_OVERWRITE) != 0;
1428 dbenv.set_flags(DbConstants.DB_OVERWRITE, value ? 1 : 0);
1432 /// If true, allocate region memory from the heap instead of from memory
1433 /// backed by the filesystem or system shared memory.
1435 public bool Private {
1438 dbenv.get_open_flags(ref flags);
1439 return (flags & DbConstants.DB_PRIVATE) != 0;
1443 /// The gigabytes component of the byte-count limit on the amount of
1444 /// memory to be used by shared structures in the main environment
1445 /// region. These are the structures other than mutexes and the page
1446 /// cache (memory pool).
1448 /// <returns>The maximum number of gigabytes used by the main region.
1450 public uint RegionMemoryLimitGBytes {
1454 dbenv.get_memory_max(ref gb, ref b);
1459 /// The bytes component of the byte-count limit on the amount of
1460 /// memory to be used by shared structures in the main environment
1461 /// region. These are the structures other than mutexes and the page
1462 /// cache (memory pool).
1464 /// <returns>The maximum number of bytes used by the main region.
1466 public uint RegionMemoryLimitBytes {
1470 dbenv.get_memory_max(ref gb, ref b);
1475 /// The amount of memory to be used by shared structures in the main
1476 /// environment region. These are structures other than mutexes and
1477 /// the page cache (memory pool).
1479 /// <param name="GBytes">
1480 /// The number of gigabytes to allocate for the main memory region.
1481 /// The gigabytes allocated will be added to the bytes input.
1483 /// <param name="Bytes">
1484 /// The number of gigabytes to allocate for the main memory region.
1485 /// The bytes allocated will be added to the gigabytes input.
1487 public void RegionSetMemoryLimit(uint GBytes, uint Bytes) {
1488 dbenv.set_memory_max(GBytes, Bytes);
1491 /// If true, Berkeley DB will have checked to see if recovery needed to
1492 /// be performed before opening the database environment.
1494 public bool Register {
1497 dbenv.get_open_flags(ref flags);
1498 return (flags & DbConstants.DB_REGISTER) != 0;
1502 /// The amount of time the replication manager's transport function
1503 /// waits to collect enough acknowledgments from replication group
1504 /// clients, before giving up and returning a failure indication. The
1505 /// default wait time is 1 second.
1507 public uint RepAckTimeout {
1508 get { return getRepTimeout(DbConstants.DB_REP_ACK_TIMEOUT); }
1510 dbenv.rep_set_timeout(
1511 DbConstants.DB_REP_ACK_TIMEOUT, value);
1515 /// If true, the replication master sends groups of records to the
1516 /// clients in a single network transfer
1518 public bool RepBulkTransfer {
1519 get { return getRepConfig(DbConstants.DB_REP_CONF_BULK); }
1521 dbenv.rep_set_config(
1522 DbConstants.DB_REP_CONF_BULK, value ? 1 : 0);
1526 /// The amount of time a master site will delay between completing a
1527 /// checkpoint and writing a checkpoint record into the log.
1530 /// This delay allows clients to complete their own checkpoints before
1531 /// the master requires completion of them. The default is 30 seconds.
1532 /// If all databases in the environment, and the environment's
1533 /// transaction log, are configured to reside in memory (never preserved
1534 /// to disk), then, although checkpoints are still necessary, the delay
1535 /// is not useful and should be set to 0.
1537 public uint RepCheckpointDelay {
1538 get { return getRepTimeout(DbConstants.DB_REP_CHECKPOINT_DELAY); }
1540 dbenv.rep_set_timeout(
1541 DbConstants.DB_REP_CHECKPOINT_DELAY, value);
1545 /// The value, relative to <see cref="RepClockskewSlow"/>, of the
1546 /// fastest clock in the group of sites.
1548 public uint RepClockskewFast {
1552 dbenv.rep_get_clockskew(ref fast, ref slow);
1557 /// The value of the slowest clock in the group of sites.
1559 public uint RepClockskewSlow {
1563 dbenv.rep_get_clockskew(ref fast, ref slow);
1568 /// Set the clock skew ratio among replication group members based on
1569 /// the fastest and slowest measurements among the group for use with
1574 /// Calling this method is optional, the default values for clock skew
1575 /// assume no skew. The user must also configure leases via
1576 /// <see cref="RepUseMasterLeases"/>. Additionally, the user must also
1577 /// set the master lease timeout via <see cref="RepLeaseTimeout"/> and
1578 /// the number of sites in the replication group via
1579 /// <see cref="RepNSites"/>. These settings may be configured in any
1580 /// order. For a description of the clock skew values, see Clock skew
1581 /// in the Berkeley DB Programmer's Reference Guide. For a description
1582 /// of master leases, see Master leases in the Berkeley DB Programmer's
1583 /// Reference Guide.
1586 /// These arguments can be used to express either raw measurements of a
1587 /// clock timing experiment or a percentage across machines. For
1588 /// instance a group of sites have a 2% variance, then
1589 /// <paramref name="fast"/> should be set to 102, and
1590 /// <paramref name="slow"/> should be set to 100. Or, for a 0.03%
1591 /// difference, you can use 10003 and 10000 respectively.
1594 /// <param name="fast">
1595 /// The value, relative to <paramref name="slow"/>, of the fastest clock
1596 /// in the group of sites.
1598 /// <param name="slow">
1599 /// The value of the slowest clock in the group of sites.
1601 public void RepSetClockskew(uint fast, uint slow) {
1602 dbenv.rep_set_clockskew(fast, slow);
1605 /// The amount of time the replication manager will wait before trying
1606 /// to re-establish a connection to another site after a communication
1607 /// failure. The default wait time is 30 seconds.
1609 public uint RepConnectionRetry {
1610 get { return getRepTimeout(DbConstants.DB_REP_CONNECTION_RETRY); }
1612 dbenv.rep_set_timeout(
1613 DbConstants.DB_REP_CONNECTION_RETRY, value);
1617 /// If true, the client should delay synchronizing to a newly declared
1618 /// master (defaults to false). Clients configured in this way will remain
1619 /// unsynchronized until the application calls <see cref="RepSync"/>.
1621 public bool RepDelayClientSync {
1622 get { return getRepConfig(DbConstants.DB_REP_CONF_DELAYCLIENT); }
1624 dbenv.rep_set_config(
1625 DbConstants.DB_REP_CONF_DELAYCLIENT, value ? 1 : 0);
1629 /// Configure the amount of time the replication manager will wait
1630 /// before retrying a failed election. The default wait time is 10
1633 public uint RepElectionRetry {
1634 get { return getRepTimeout(DbConstants.DB_REP_ELECTION_RETRY); }
1636 dbenv.rep_set_timeout(DbConstants.DB_REP_ELECTION_RETRY, value);
1640 /// The timeout period for an election. The default timeout is 2
1643 public uint RepElectionTimeout {
1644 get { return getRepTimeout(DbConstants.DB_REP_ELECTION_TIMEOUT); }
1646 dbenv.rep_set_timeout(
1647 DbConstants.DB_REP_ELECTION_TIMEOUT, value);
1651 /// An optional configuration timeout period to wait for full election
1652 /// participation the first time the replication group finds a master.
1653 /// By default this option is turned off and normal election timeouts
1654 /// are used. (See the Elections section in the Berkeley DB Reference
1655 /// Guide for more information.)
1657 public uint RepFullElectionTimeout {
1659 return getRepTimeout(
1660 DbConstants.DB_REP_FULL_ELECTION_TIMEOUT);
1663 dbenv.rep_set_timeout(
1664 DbConstants.DB_REP_FULL_ELECTION_TIMEOUT, value);
1668 /// The amount of time the replication manager, running at a client
1669 /// site, waits for some message activity on the connection from the
1670 /// master (heartbeats or other messages) before concluding that the
1671 /// connection has been lost. When 0 (the default), no monitoring is
1674 public uint RepHeartbeatMonitor {
1675 get { return getRepTimeout(DbConstants.DB_REP_HEARTBEAT_MONITOR); }
1677 dbenv.rep_set_timeout(
1678 DbConstants.DB_REP_HEARTBEAT_MONITOR, value);
1682 /// The frequency at which the replication manager, running at a master
1683 /// site, broadcasts a heartbeat message in an otherwise idle system.
1684 /// When 0 (the default), no heartbeat messages will be sent.
1686 public uint RepHeartbeatSend {
1687 get { return getRepTimeout(DbConstants.DB_REP_HEARTBEAT_SEND); }
1689 dbenv.rep_set_timeout(DbConstants.DB_REP_HEARTBEAT_SEND, value);
1693 /// If true, replication only stores the internal information in-memory
1694 /// and cannot keep persistent state across a site crash or reboot. By
1695 /// default, it is false and replication creates files in the
1696 /// environment home directory to preserve the internal information.
1698 public bool RepInMemory {
1699 get { return getRepConfig(DbConstants.DB_REP_CONF_INMEM); }
1702 /// The amount of time a client grants its master lease to a master.
1703 /// When using master leases all sites in a replication group must use
1704 /// the same lease timeout value. There is no default value. If leases
1705 /// are desired, this method must be called prior to calling
1706 /// <see cref="RepStartClient"/> or <see cref="RepStartMaster"/>.
1708 public uint RepLeaseTimeout {
1709 get { return getRepTimeout(DbConstants.DB_REP_LEASE_TIMEOUT); }
1711 dbenv.rep_set_timeout(DbConstants.DB_REP_LEASE_TIMEOUT, value);
1715 /// Set the message dispatch function. It is responsible for receiving
1716 /// messages sent from remote sites using either
1717 /// <see cref="DbChannel.SendMessage"/> or <see cref="DbChannel.SendRequest"/>.
1718 /// If the message received by this function was sent using
1719 /// <see cref="DbChannel.SendMessage"/>, then no response is required.
1720 /// If the message was sent using <see cref="DbChannel.SendRequest"/>,
1721 /// then this function must send a response using
1722 /// <see cref="DbChannel.SendMessage"/>.
1725 /// It should be called before the Replication Manager has been started.
1727 public MessageDispatchDelegate RepMessageDispatch {
1728 get { return messageDispatchHandler; }
1731 dbenv.repmgr_msg_dispatch(null, 0);
1732 else if (messageDispatchHandler == null) {
1733 if (doMessageDispatchRef == null)
1734 doMessageDispatchRef = new BDB_MessageDispatchDelegate(
1736 dbenv.repmgr_msg_dispatch(doMessageDispatchRef, 0);
1738 messageDispatchHandler = value;
1742 /// Specify how master and client sites will handle acknowledgment of
1743 /// replication messages which are necessary for "permanent" records.
1744 /// The current implementation requires all sites in a replication group
1745 /// configure the same acknowledgement policy.
1747 /// <seealso cref="RepAckTimeout"/>
1748 public AckPolicy RepMgrAckPolicy {
1751 dbenv.repmgr_get_ack_policy(ref policy);
1752 return AckPolicy.fromInt(policy);
1754 set { dbenv.repmgr_set_ack_policy(value.Policy); }
1757 /// Create DbChannel with given eid.
1759 /// <param name="eid">
1760 /// Environment id. If the eid is <see cref="EnvironmentID.EID_MASTER"/>,
1761 /// create channel sending to master site only.
1763 public DbChannel RepMgrChannel(int eid) {
1764 DB_CHANNEL dbChannel;
1765 dbChannel = dbenv.repmgr_channel(eid, 0);
1766 return new DbChannel(dbChannel);
1769 /// If true, Replication Manager automatically runs elections to
1770 /// choose a new master when the old master appears to
1771 /// have become disconnected (defaults to true).
1773 public bool RepMgrRunElections {
1774 get { return getRepConfig(DbConstants.DB_REPMGR_CONF_ELECTIONS); }
1776 dbenv.rep_set_config(
1777 DbConstants.DB_REPMGR_CONF_ELECTIONS, value ? 1 : 0);
1781 /// The local site of the replication manager. Returns null if the
1782 /// local site has not been configured.
1784 public DbSite RepMgrLocalSite {
1788 site = dbenv.repmgr_local_site();
1789 } catch (NotFoundException) {
1790 // Local site wasn't set.
1793 return new DbSite(site);
1797 /// The status of the sites currently known by the replication manager.
1799 public RepMgrSite[] RepMgrRemoteSites {
1800 get { return dbenv.repmgr_site_list(); }
1803 /// If true, the replication master will automatically re-initialize
1804 /// outdated clients (defaults to true).
1806 public bool RepAutoInit {
1807 get { return getRepConfig(DbConstants.DB_REP_CONF_AUTOINIT); }
1809 dbenv.rep_set_config(
1810 DbConstants.DB_REP_CONF_AUTOINIT, value ? 1 : 0);
1814 /// If true, Berkeley DB method calls that would normally block while
1815 /// clients are in recovery will return errors immediately (defaults to
1818 public bool RepNoBlocking {
1819 get { return getRepConfig(DbConstants.DB_REP_CONF_NOWAIT); }
1821 dbenv.rep_set_config(
1822 DbConstants.DB_REP_CONF_NOWAIT, value ? 1 : 0);
1826 /// The total number of sites in the replication group.
1830 /// This setting is typically used by applications which use the
1831 /// Berkeley DB library "replication manager" support. (However, see
1832 /// also <see cref="RepHoldElection"/>, the description of the nsites
1836 public uint RepNSites {
1839 dbenv.rep_get_nsites(ref ret);
1842 set { dbenv.rep_set_nsites(value); }
1845 /// The database environment's priority in replication group elections.
1846 /// A special value of 0 indicates that this environment cannot be a
1847 /// replication group master. If not configured, then a default value
1850 public uint RepPriority {
1853 dbenv.rep_get_priority(ref ret);
1856 set { dbenv.rep_set_priority(value); }
1859 /// The minimum number of microseconds a client waits before requesting
1862 public uint RepRetransmissionRequestMin {
1866 dbenv.rep_get_request(ref min, ref max);
1871 /// The maximum number of microseconds a client waits before requesting
1874 public uint RepRetransmissionRequestMax {
1878 dbenv.rep_get_request(ref min, ref max);
1883 /// Set a threshold for the minimum and maximum time that a client waits
1884 /// before requesting retransmission of a missing message.
1888 /// If the client detects a gap in the sequence of incoming log records
1889 /// or database pages, Berkeley DB will wait for at least
1890 /// <paramref name="min"/> microseconds before requesting retransmission
1891 /// of the missing record. Berkeley DB will double that amount before
1892 /// requesting the same missing record again, and so on, up to a
1893 /// maximum threshold of <paramref name="max"/> microseconds.
1896 /// These values are thresholds only. Since Berkeley DB has no thread
1897 /// available in the library as a timer, the threshold is only checked
1898 /// when a thread enters the Berkeley DB library to process an incoming
1899 /// replication message. Any amount of time may have passed since the
1900 /// last message arrived and Berkeley DB only checks whether the amount
1901 /// of time since a request was made is beyond the threshold value or
1905 /// By default the minimum is 40000 and the maximum is 1280000 (1.28
1906 /// seconds). These defaults are fairly arbitrary and the application
1907 /// likely needs to adjust these. The values should be based on expected
1908 /// load and performance characteristics of the master and client host
1909 /// platforms and transport infrastructure as well as round-trip message
1911 /// </para></remarks>
1912 /// <param name="min">
1913 /// The minimum number of microseconds a client waits before requesting
1916 /// <param name="max">
1917 /// The maximum number of microseconds a client waits before requesting
1920 public void RepSetRetransmissionRequest(uint min, uint max) {
1921 dbenv.rep_set_request(min, max);
1924 /// Replication Manager observes the strict "majority" rule in managing
1925 /// elections, even in a group with only 2 sites. This means the client
1926 /// in a 2-site group will be unable to take over as master if the
1927 /// original master fails or becomes disconnected. (See the Elections
1928 /// section in the Berkeley DB Reference Guide for more information.)
1929 /// Both sites in the replication group should have the same value for
1932 public bool RepStrict2Site {
1934 return getRepConfig(DbConstants.DB_REPMGR_CONF_2SITE_STRICT);
1937 dbenv.rep_set_config(
1938 DbConstants.DB_REPMGR_CONF_2SITE_STRICT, value ? 1 : 0);
1942 /// The gigabytes component of the byte-count limit on the amount of
1943 /// data that will be transmitted from a site in response to a single
1944 /// message processed by <see cref="RepProcessMessage"/>.
1946 public uint RepTransmitLimitGBytes {
1950 dbenv.rep_get_limit(ref gb, ref b);
1955 /// The bytes component of the byte-count limit on the amount of data
1956 /// that will be transmitted from a site in response to a single
1957 /// message processed by <see cref="RepProcessMessage"/>.
1959 public uint RepTransmitLimitBytes {
1963 dbenv.rep_get_limit(ref gb, ref b);
1968 /// Set a byte-count limit on the amount of data that will be
1969 /// transmitted from a site in response to a single message processed by
1970 /// <see cref="RepProcessMessage"/>. The limit is not a hard limit, and
1971 /// the record that exceeds the limit is the last record to be sent.
1975 /// Record transmission throttling is turned on by default with a limit
1979 /// If both <paramref name="GBytes"/> and <paramref name="Bytes"/> are
1980 /// zero, then the transmission limit is turned off.
1983 /// <param name="GBytes">
1984 /// The number of gigabytes which, when added to
1985 /// <paramref name="Bytes"/>, specifies the maximum number of bytes that
1986 /// will be sent in a single call to <see cref="RepProcessMessage"/>.
1988 /// <param name="Bytes">
1989 /// The number of bytes which, when added to
1990 /// <paramref name="GBytes"/>, specifies the maximum number of bytes
1991 /// that will be sent in a single call to
1992 /// <see cref="RepProcessMessage"/>.
1994 public void RepSetTransmitLimit(uint GBytes, uint Bytes) {
1995 dbenv.rep_set_limit(GBytes, Bytes);
1998 /// The delegate used to transmit data using the replication
1999 /// application's communication infrastructure.
2001 public ReplicationTransportDelegate RepTransport {
2002 get { return transportHandler; }
2005 /// Initialize the communication infrastructure for a database
2006 /// environment participating in a replicated application.
2009 /// RepSetTransport is not called by most replication applications. It
2010 /// should only be called by applications implementing their own network
2011 /// transport layer, explicitly holding replication group elections and
2012 /// handling replication messages outside of the replication manager
2015 /// <param name="envid">
2016 /// The local environment's ID. It must be a non-negative integer and
2017 /// uniquely identify this Berkeley DB database environment (see
2018 /// Replication environment IDs in the Programmer's Reference Guide for
2019 /// more information).
2021 /// <param name="transport">
2022 /// The delegate used to transmit data using the replication
2023 /// application's communication infrastructure.
2025 public void RepSetTransport(int envid,
2026 ReplicationTransportDelegate transport) {
2027 if (transport == null)
2028 dbenv.rep_set_transport(envid, null);
2029 else if (transportHandler == null) {
2030 if (doRepTransportRef == null)
2031 doRepTransportRef = new BDB_RepTransportDelegate(doRepTransport);
2032 dbenv.rep_set_transport(envid, doRepTransportRef);
2034 transportHandler = transport;
2037 /// If true, master leases will be used for this site (defaults to
2041 /// Configuring this option may result in a
2042 /// <see cref="LeaseExpiredException"/> when attempting to read entries
2043 /// from a database after the site's master lease has expired.
2045 public bool RepUseMasterLeases {
2046 get { return getRepConfig(DbConstants.DB_REP_CONF_LEASE); }
2048 dbenv.rep_set_config(
2049 DbConstants.DB_REP_CONF_LEASE, value ? 1 : 0);
2053 /// If true, catastrophic recovery was run on this environment before
2054 /// opening it for normal use.
2056 public bool RunFatalRecovery {
2059 dbenv.get_open_flags(ref flags);
2060 return (flags & DbConstants.DB_RECOVER_FATAL) != 0;
2064 /// If true, normal recovery was run on this environment before opening
2065 /// it for normal use.
2067 public bool RunRecovery {
2070 dbenv.get_open_flags(ref flags);
2071 return (flags & DbConstants.DB_RECOVER) != 0;
2075 /// The number of microseconds the thread of control will pause before
2076 /// scheduling further write operations.
2078 public uint SequentialWritePause {
2082 dbenv.get_mp_max_write(ref tmp, ref ret);
2087 /// A delegate that returns a unique identifier pair for the current
2088 /// thread of control.
2091 /// This delegate supports <see cref="FailCheck"/>. For more
2092 /// information, see Architecting Data Store and Concurrent Data Store
2093 /// applications, and Architecting Transactional Data Store
2094 /// applications, both in the Berkeley DB Programmer's Reference Guide.
2096 public SetThreadIDDelegate SetThreadID {
2097 get { return threadIDHandler; }
2100 dbenv.set_thread_id(null);
2101 else if (threadIDHandler == null) {
2102 if (doThreadIDRef == null)
2103 doThreadIDRef = new BDB_ThreadIDDelegate(doThreadID);
2104 dbenv.set_thread_id(doThreadIDRef);
2106 threadIDHandler = value;
2110 /// A delegate that formats a process ID and thread ID identifier pair.
2112 public SetThreadNameDelegate SetThreadName {
2113 get { return threadNameHandler; }
2116 dbenv.set_thread_id_string(null);
2117 else if (threadNameHandler == null) {
2118 if (doThreadNameRef == null)
2120 new BDB_ThreadNameDelegate(doThreadName);
2121 dbenv.set_thread_id_string(doThreadNameRef);
2123 threadNameHandler = value;
2127 /// If true, allocate region memory from system shared memory instead of
2128 /// from heap memory or memory backed by the filesystem.
2130 public bool SystemMemory {
2133 dbenv.get_open_flags(ref flags);
2134 return (flags & DbConstants.DB_SYSTEM_MEM) != 0;
2138 /// The path of a directory to be used as the location of temporary
2143 /// The files created to back in-memory access method databases will be
2144 /// created relative to this path. These temporary files can be quite
2145 /// large, depending on the size of the database.
2148 /// If no directories are specified, the following alternatives are
2149 /// checked in the specified order. The first existing directory path is
2150 /// used for all temporary files.
2152 /// <list type="number">
2153 /// <item>The value of the environment variable TMPDIR.</item>
2154 /// <item>The value of the environment variable TEMP.</item>
2155 /// <item>The value of the environment variable TMP.</item>
2156 /// <item>The value of the environment variable TempFolder.</item>
2157 /// <item>The value returned by the GetTempPath interface.</item>
2158 /// <item>The directory /var/tmp.</item>
2159 /// <item>The directory /usr/tmp.</item>
2160 /// <item>The directory /temp.</item>
2161 /// <item>The directory /tmp.</item>
2162 /// <item>The directory C:/temp.</item>
2163 /// <item>The directory C:/tmp.</item>
2166 /// Environment variables are only checked if
2167 /// <see cref="UseEnvironmentVars"/> is true.
2170 public string TempDir {
2173 dbenv.get_tmp_dir(out ret);
2176 set { dbenv.set_tmp_dir(value); }
2179 /// An approximate number of threads in the database environment.
2181 public uint ThreadCount {
2184 dbenv.get_thread_count(ref ret);
2187 private set { dbenv.set_thread_count(value); }
2190 /// A delegate that returns if a thread of control (either a true thread
2191 /// or a process) is still running.
2193 public ThreadIsAliveDelegate ThreadIsAlive {
2194 get { return isAliveHandler; }
2197 dbenv.set_isalive(null);
2198 else if (isAliveHandler == null) {
2199 if (doIsAliveRef == null)
2200 doIsAliveRef = new BDB_IsAliveDelegate(doIsAlive);
2201 dbenv.set_isalive(doIsAliveRef);
2203 isAliveHandler = value;
2207 /// If true, database calls timing out based on lock or transaction
2208 /// timeout values will throw <see cref="LockNotGrantedException"/>
2209 /// instead of <see cref="DeadlockException"/>.
2212 /// If true, this allows applications to distinguish between operations
2213 /// which have deadlocked and operations which have exceeded their time
2216 public bool TimeNotGranted {
2219 dbenv.get_flags(ref flags);
2220 return (flags & DbConstants.DB_TIME_NOTGRANTED) != 0;
2223 dbenv.set_flags(DbConstants.DB_TIME_NOTGRANTED, value ? 1 : 0);
2227 /// If true, Berkeley DB will not write or synchronously flush the log
2228 /// on transaction commit.
2231 /// This means that transactions exhibit the ACI (atomicity,
2232 /// consistency, and isolation) properties, but not D (durability); that
2233 /// is, database integrity will be maintained, but if the application or
2234 /// system fails, it is possible some number of the most recently
2235 /// committed transactions may be undone during recovery. The number of
2236 /// transactions at risk is governed by how many log updates can fit
2237 /// into the log buffer, how often the operating system flushes dirty
2238 /// buffers to disk, and how often the log is checkpointed.
2240 public bool TxnNoSync {
2243 dbenv.get_flags(ref flags);
2244 return (flags & DbConstants.DB_TXN_NOSYNC) != 0;
2246 set { dbenv.set_flags(DbConstants.DB_TXN_NOSYNC, value ? 1 : 0); }
2249 /// If true and a lock is unavailable for any Berkeley DB operation
2250 /// performed in the context of a transaction, cause the operation to
2251 /// throw <see cref="DeadlockException"/> (or
2252 /// <see cref="LockNotGrantedException"/> if configured with
2253 /// <see cref="TimeNotGranted"/>).
2255 public bool TxnNoWait {
2258 dbenv.get_flags(ref flags);
2259 return (flags & DbConstants.DB_TXN_NOWAIT) != 0;
2261 set { dbenv.set_flags(DbConstants.DB_TXN_NOWAIT, value ? 1 : 0); }
2264 /// If true, all transactions in the environment will be started as if
2265 /// <see cref="TransactionConfig.Snapshot"/> was passed to
2266 /// <see cref="BeginTransaction"/>, and all non-transactional cursors
2267 /// will be opened as if <see cref="CursorConfig.SnapshotIsolation"/>
2268 /// was passed to <see cref="BaseDatabase.Cursor"/>.
2270 public bool TxnSnapshot {
2273 dbenv.get_flags(ref flags);
2274 return (flags & DbConstants.DB_TXN_SNAPSHOT) != 0;
2276 set { dbenv.set_flags(DbConstants.DB_TXN_SNAPSHOT, value ? 1 : 0); }
2279 /// A value, in microseconds, representing transaction timeouts.
2283 /// All timeouts are checked whenever a thread of control blocks on a
2284 /// lock or when deadlock detection is performed. As timeouts are only
2285 /// checked when the lock request first blocks or when deadlock
2286 /// detection is performed, the accuracy of the timeout depends on how
2287 /// often deadlock detection is performed.
2290 /// Timeout values specified for the database environment may be
2291 /// overridden on a per-transaction basis, see
2292 /// <see cref="Transaction.SetTxnTimeout"/>.
2295 public uint TxnTimeout {
2298 dbenv.get_timeout(ref timeout, DbConstants.DB_SET_TXN_TIMEOUT);
2302 dbenv.set_timeout(value, DbConstants.DB_SET_TXN_TIMEOUT);
2306 /// The recovery timestamp
2308 public DateTime TxnTimestamp {
2311 dbenv.get_tx_timestamp(ref secs);
2312 DateTime epoch = new DateTime(1970, 1, 1);
2313 DateTime ret = epoch.AddSeconds(secs);
2317 if (value != null) {
2318 TimeSpan ts = value - new DateTime(1970, 1, 1);
2319 long secs = (long)ts.TotalSeconds;
2320 dbenv.set_tx_timestamp(ref secs);
2325 /// If true, Berkeley DB will write, but will not synchronously flush,
2326 /// the log on transaction commit.
2329 /// This means that transactions exhibit the ACI (atomicity,
2330 /// consistency, and isolation) properties, but not D (durability); that
2331 /// is, database integrity will be maintained, but if the system fails,
2332 /// it is possible some number of the most recently committed
2333 /// transactions may be undone during recovery. The number of
2334 /// transactions at risk is governed by how often the system flushes
2335 /// dirty buffers to disk and how often the log is checkpointed.
2337 public bool TxnWriteNoSync {
2340 dbenv.get_flags(ref flags);
2341 return (flags & DbConstants.DB_TXN_WRITE_NOSYNC) != 0;
2344 dbenv.set_flags(DbConstants.DB_TXN_WRITE_NOSYNC, value ? 1 : 0);
2348 /// If true, all databases in the environment will be opened as if
2349 /// <see cref="DatabaseConfig.UseMVCC"/> was set.
2352 /// This flag will be ignored for queue databases for which MVCC is not
2355 public bool UseMVCC {
2358 dbenv.get_flags(ref flags);
2359 return (flags & DbConstants.DB_MULTIVERSION) != 0;
2361 set { dbenv.set_flags(DbConstants.DB_MULTIVERSION, value ? 1 : 0); }
2364 /// If true, locking for the Berkeley DB Concurrent Data Store product
2365 /// was initialized.
2367 public bool UsingCDB {
2370 dbenv.get_open_flags(ref flags);
2371 return (flags & DbConstants.DB_INIT_CDB) != 0;
2375 /// If true, the locking subsystem was initialized.
2377 public bool UsingLocking {
2380 dbenv.get_open_flags(ref flags);
2381 return (flags & DbConstants.DB_INIT_LOCK) != 0;
2385 /// If true, the logging subsystem was initialized.
2387 public bool UsingLogging {
2390 dbenv.get_open_flags(ref flags);
2391 return (flags & DbConstants.DB_INIT_LOG) != 0;
2395 /// If true, the shared memory buffer pool subsystem was initialized.
2397 public bool UsingMPool {
2400 dbenv.get_open_flags(ref flags);
2401 return (flags & DbConstants.DB_INIT_MPOOL) != 0;
2405 /// If true, the replication subsystem was initialized.
2407 public bool UsingReplication {
2410 dbenv.get_open_flags(ref flags);
2411 return (flags & DbConstants.DB_INIT_REP) != 0;
2415 /// If true, the transaction subsystem was initialized.
2417 public bool UsingTxns {
2420 dbenv.get_open_flags(ref flags);
2421 return (flags & DbConstants.DB_INIT_TXN) != 0;
2425 /// Specific additional informational and debugging messages in the
2426 /// Berkeley DB message output.
2428 public VerboseMessages Verbosity {
2431 dbenv.get_verbose(ref flags);
2432 return VerboseMessages.FromFlags(flags);
2435 if (value.MessagesOff != 0)
2436 dbenv.set_verbose(value.MessagesOff, 0);
2437 if (value.MessagesOn != 0)
2438 dbenv.set_verbose(value.MessagesOn, 1);
2442 /// If true, Berkeley DB will yield the processor immediately after each
2443 /// page or mutex acquisition.
2446 /// This functionality should never be used for purposes other than
2449 public bool YieldCPU {
2452 dbenv.get_flags(ref flags);
2453 return (flags & DbConstants.DB_YIELDCPU) != 0;
2456 dbenv.set_flags(DbConstants.DB_YIELDCPU, value ? 1 : 0);
2459 #endregion Properties
2462 /// Instantiate a new DatabaseEnvironment object and open the Berkeley
2463 /// DB environment represented by <paramref name="home"/>.
2465 /// <param name="home">
2466 /// The database environment's home directory. For more information on
2467 /// home, and filename resolution in general, see Berkeley DB File
2468 /// Naming in the Programmer's Reference Guide.
2470 /// <param name="cfg">The environment's configuration</param>
2471 /// <returns>A new, open DatabaseEnvironment object</returns>
2472 public static DatabaseEnvironment Open(
2473 String home, DatabaseEnvironmentConfig cfg) {
2474 DatabaseEnvironment env = new DatabaseEnvironment(0);
2476 env.dbenv.open(home, cfg.openFlags, 0);
2481 /// Destroy a Berkeley DB environment if it is not currently in use.
2485 /// The environment regions, including any backing files, are removed.
2486 /// Any log or database files and the environment directory are not
2490 /// If there are processes that have called <see cref="Open"/> without
2491 /// calling <see cref="Close"/> (that is, there are processes currently
2492 /// using the environment), Remove will fail without further action.
2495 /// Calling Remove should not be necessary for most applications because
2496 /// the Berkeley DB environment is cleaned up as part of normal database
2497 /// recovery procedures. However, applications may want to call Remove
2498 /// as part of application shut down to free up system resources. For
2499 /// example, if <see cref="DatabaseEnvironmentConfig.SystemMemory"/> was
2500 /// specified to <see cref="Open"/>, it may be useful to call Remove in
2501 /// order to release system shared memory segments that have been
2502 /// allocated. Or, on architectures in which mutexes require allocation
2503 /// of underlying system resources, it may be useful to call Remove in
2504 /// order to release those resources. Alternatively, if recovery is not
2505 /// required because no database state is maintained across failures,
2506 /// and no system resources need to be released, it is possible to clean
2507 /// up an environment by simply removing all the Berkeley DB files in
2508 /// the database environment's directories.
2511 /// In multithreaded applications, only a single thread may call Remove.
2514 /// <param name="db_home">
2515 /// The database environment to be removed.
2517 public static void Remove(string db_home) {
2518 Remove(db_home, false, false, false);
2521 /// Destroy a Berkeley DB environment if it is not currently in use.
2525 /// Generally, <paramref name="force"/> is specified only when
2526 /// applications were unable to shut down cleanly, and there is a risk
2527 /// that an application may have died holding a Berkeley DB lock.)
2530 /// The result of attempting to forcibly destroy the environment when it
2531 /// is in use is unspecified. Processes using an environment often
2532 /// maintain open file descriptors for shared regions within it. On UNIX
2533 /// systems, the environment removal will usually succeed, and processes
2534 /// that have already joined the region will continue to run in that
2535 /// region without change. However, processes attempting to join the
2536 /// environment will either fail or create new regions. On other systems
2537 /// in which the unlink(2) system call will fail if any process has an
2538 /// open file descriptor for the file (for example Windows/NT), the
2539 /// region removal will fail.
2542 /// <param name="db_home">
2543 /// The database environment to be removed.
2545 /// <param name="force">
2546 /// If true, the environment is removed, regardless of any processes
2547 /// that may still using it, and no locks are acquired during this
2550 public static void Remove(string db_home, bool force) {
2551 Remove(db_home, force, false, false);
2553 private static void Remove(string db_home,
2554 bool force, bool USE_ENVIRON, bool USE_ENVIRON_ROOT) {
2555 DatabaseEnvironment env = new DatabaseEnvironment(0);
2558 flags |= force ? DbConstants.DB_FORCE : 0;
2559 flags |= USE_ENVIRON ? DbConstants.DB_USE_ENVIRON : 0;
2560 flags |= USE_ENVIRON_ROOT ? DbConstants.DB_USE_ENVIRON_ROOT : 0;
2561 env.dbenv.remove(db_home, flags);
2565 /// Perform a hot back up of the open environment.
2567 /// All files used by the environment are backed up, so long as the
2568 /// normal rules for file placement are followed. For information on how
2569 /// files are normally placed relative to the environment directory, see
2570 /// the "Berkeley DB File Naming" section in the Berkeley DB Reference
2574 /// By default, data directories and the log directory specified
2575 /// relative to the home directory will be recreated relative to the
2576 /// target directory. If absolute path names are used, then use the
2577 /// <see cref="BackupOptions.SingleDir"/> method.
2580 /// This method provides the same functionality as the db_hotbackup
2581 /// utility. However, this method does not perform the housekeeping
2582 /// actions performed by that utility. In particular, you may want to
2583 /// run a checkpoint before calling this method. To run a checkpoint,
2584 /// use the <see cref="DatabaseEnvironment.Checkpoint"/> method. For
2585 /// more information on checkpoints, see the "Checkpoint" section in the
2586 /// Berkeley DB Reference Guide.
2589 /// To back up a single database file within the environment, use the
2590 /// <see cref="DatabaseEnvironment.BackupDatabase"/> method.
2593 /// In addition to the configuration options available using the
2594 /// <see cref="BackupOptions"/> class, additional tuning modifications
2595 /// can be made using the <see cref="DatabaseEnvironment.BackupReadCount"/>,
2596 /// <see cref="DatabaseEnvironment.BackupReadSleepDuration"/>,
2597 /// <see cref="DatabaseEnvironment.BackupBufferSize"/>, and
2598 /// <see cref="DatabaseEnvironment.BackupWriteDirect"/> properties.
2599 /// Alternatively, you can write your own custom hot back up facility
2600 /// using the <see cref="IBackup"/> interface.
2603 /// <param name="target">Identifies the directory in which the back up
2604 /// will be placed. Any subdirectories required to contain the back up
2605 /// must be placed relative to this directory. Note that if an
2606 /// <see cref="IBackup"/> is configured for the environment, then the
2607 /// value specified to this parameter is passed on to the
2608 /// <see cref="IBackup.Open"/> method. If this parameter is null, then
2609 /// the target must be specified to the <see cref="IBackup.Open"/>
2612 /// This directory, and any required subdirectories, will be created for
2613 /// you if you specify <see cref="CreatePolicy.IF_NEEDED"/> or
2614 /// <see cref="CreatePolicy.ALWAYS"/> for the
2615 /// <see cref="BackupOptions.Creation"/> property.
2618 /// <param name="opt">The <see cref="BackupOptions"/> instance used to
2619 /// configure the hot back up.</param>
2620 public void Backup(string target, BackupOptions opt) {
2621 dbenv.backup(target, opt.flags);
2624 /// Perform a hot back up of a single database file contained within the
2627 /// To back up the entire environment, use the
2628 /// <see cref="DatabaseEnvironment.Backup"/> method.
2631 /// You can make some tuning modifications to the backup process using
2632 /// the <see cref="DatabaseEnvironment.BackupReadCount"/>,
2633 /// <see cref="DatabaseEnvironment.BackupReadSleepDuration"/>,
2634 /// <see cref="DatabaseEnvironment.BackupBufferSize"/>, and
2635 /// <see cref="DatabaseEnvironment.BackupWriteDirect"/> properties.
2636 /// Alternatively, you can write your own custom hot back up facility
2637 /// using the <see cref="IBackup"/> interface.
2640 /// <param name="target">Identifies the directory in which the back up
2641 /// will be placed.</param>
2642 /// <param name="database">The database file that you want to back up.
2644 /// <param name="must_create">If true, then if the target file exists,
2645 /// this method throws an exception.</param>
2646 public void BackupDatabase(
2647 string target, string database, bool must_create) {
2649 database, target, must_create ? DbConstants.DB_EXCL : 0);
2653 /// Hold an election for the master of a replication group.
2655 public void RepHoldElection() {
2656 RepHoldElection(0, 0);
2659 /// Hold an election for the master of a replication group.
2661 /// <param name="nsites">
2662 /// The number of replication sites expected to participate in the
2663 /// election. Once the current site has election information from that
2664 /// many sites, it will short-circuit the election and immediately cast
2665 /// its vote for a new master. If an application is using master leases,
2666 /// then the value must be 0 and <see cref="RepNSites"/> must be used.
2668 public void RepHoldElection(uint nsites) {
2669 RepHoldElection(nsites, 0);
2672 /// Hold an election for the master of a replication group.
2676 /// RepHoldElection is not called by most replication applications. It
2677 /// should only be called by applications implementing their own network
2678 /// transport layer, explicitly holding replication group elections and
2679 /// handling replication messages outside of the replication manager
2683 /// If the election is successful, Berkeley DB will notify the
2684 /// application of the results of the election by means of either the
2685 /// <see cref="NotificationEvent.REP_ELECTED"/> or
2686 /// <see cref="NotificationEvent.REP_NEWMASTER"/> events (see
2687 /// <see cref="EventNotify"/>for more information). The application is
2688 /// responsible for adjusting its relationship to the other database
2689 /// environments in the replication group, including directing all
2690 /// database updates to the newly selected master, in accordance with
2691 /// the results of the election.
2694 /// The thread of control that calls RepHoldElection must not be the
2695 /// thread of control that processes incoming messages; processing the
2696 /// incoming messages is necessary to successfully complete an election.
2699 /// Before calling this method, the <see cref="RepTransport"/> delegate
2700 /// must already have been configured to send replication messages.
2703 /// <param name="nsites">
2704 /// The number of replication sites expected to participate in the
2705 /// election. Once the current site has election information from that
2706 /// many sites, it will short-circuit the election and immediately cast
2707 /// its vote for a new master. This parameter must be no less than
2708 /// <paramref name="nvotes"/>, or 0 if the election should use
2709 /// <see cref="RepNSites"/>. If an application is using master leases,
2710 /// then the value must be 0 and <see cref="RepNSites"/> must be used.
2712 /// <param name="nvotes">
2713 /// The minimum number of replication sites from which the current site
2714 /// must have election information, before the current site will cast a
2715 /// vote for a new master. This parameter must be no greater than
2716 /// <paramref name="nsites"/>, or 0 if the election should use the value
2717 /// ((<paramref name="nsites"/> / 2) + 1).
2719 public void RepHoldElection(uint nsites, uint nvotes) {
2720 dbenv.rep_elect(nsites, nvotes, 0);
2724 /// Configure a site in the replication manager.
2726 /// <param name="siteConfig">The configuration of a site</param>
2727 public void RepMgrSiteConfig(DbSiteConfig siteConfig) {
2729 dbSite = dbenv.repmgr_site(siteConfig.Host, siteConfig.Port);
2730 if (siteConfig.helperIsSet)
2731 dbSite.set_config(DbConstants.DB_BOOTSTRAP_HELPER,
2732 Convert.ToUInt32(siteConfig.Helper));
2733 if (siteConfig.groupCreatorIsSet)
2734 dbSite.set_config(DbConstants.DB_GROUP_CREATOR,
2735 Convert.ToUInt32(siteConfig.GroupCreator));
2736 if (siteConfig.legacyIsSet)
2737 dbSite.set_config(DbConstants.DB_LEGACY,
2738 Convert.ToUInt32(siteConfig.Legacy));
2739 if (siteConfig.localSiteIsSet)
2740 dbSite.set_config(DbConstants.DB_LOCAL_SITE,
2741 Convert.ToUInt32(siteConfig.LocalSite));
2742 if (siteConfig.peerIsSet)
2743 dbSite.set_config(DbConstants.DB_REPMGR_PEER,
2744 Convert.ToUInt32(siteConfig.Peer));
2749 /// Create DbSite with the given eid.
2752 /// It is only possible to use this method after env open, because EID
2753 /// values are not established before that time.
2755 /// <param name="eid">The environment id</param>
2756 public DbSite RepMgrSite(int eid) {
2758 dbSite = dbenv.repmgr_site_by_eid(eid);
2759 return new DbSite(dbSite);
2763 /// Create DbSite with the given host and port.
2765 /// <param name="host">The host address</param>
2766 /// <param name="port">The port</param>
2767 public DbSite RepMgrSite(string host, uint port) {
2769 dbSite = dbenv.repmgr_site(host, port);
2770 return new DbSite(dbSite);
2774 /// Start the replication manager as a client site, and do not call for
2779 /// There are two ways to build Berkeley DB replication applications:
2780 /// the most common approach is to use the Berkeley DB library
2781 /// "replication manager" support, where the Berkeley DB library manages
2782 /// the replication group, including network transport, all replication
2783 /// message processing and acknowledgment, and group elections.
2784 /// Applications using the replication manager support generally make
2785 /// the following calls:
2787 /// <list type="number">
2789 /// Configure the local site in the replication group,
2790 /// <see cref="RepMgrLocalSite"/>.
2793 /// Call <see cref="RepMgrSiteConfig"/> to configure the remote
2794 /// site(s) in the replication group.
2796 /// <item>Configure the message acknowledgment policy
2797 /// (<see cref="RepMgrAckPolicy"/>) which provides the replication group's
2798 /// transactional needs.
2801 /// Configure the local site's election priority,
2802 /// <see cref="RepPriority"/>.
2805 /// Call <see cref="RepMgrStartClient"/> or
2806 /// <see cref="RepMgrStartMaster"/> to start the replication
2811 /// For more information on building replication manager applications,
2812 /// please see the Replication Getting Started Guide included in the
2813 /// Berkeley DB documentation.
2816 /// Applications with special needs (for example, applications using
2817 /// network protocols not supported by the Berkeley DB replication
2818 /// manager), must perform additional configuration and call other
2819 /// Berkeley DB replication methods. For more information on building
2820 /// advanced replication applications, please see the Base Replication
2821 /// API section in the Berkeley DB Programmer's Reference Guide for more
2825 /// Starting the replication manager consists of opening the TCP/IP
2826 /// listening socket to accept incoming connections, and starting all
2827 /// necessary background threads. When multiple processes share a
2828 /// database environment, only one process can open the listening
2829 /// socket; <see cref="RepMgrStartClient"/> (and
2830 /// <see cref="RepMgrStartMaster"/>) automatically open the socket in
2831 /// the first process to call it, and skips this step in the later calls
2832 /// from other processes.
2835 /// <param name="nthreads">
2836 /// Specify the number of threads of control created and dedicated to
2837 /// processing replication messages. In addition to these message
2838 /// processing threads, the replication manager creates and manages a
2839 /// few of its own threads of control.
2841 public void RepMgrStartClient(int nthreads) {
2842 RepMgrStartClient(nthreads, false);
2845 /// Start the replication manager as a client site, and optionally call
2846 /// for an election.
2848 /// <param name="nthreads">
2849 /// Specify the number of threads of control created and dedicated to
2850 /// processing replication messages. In addition to these message
2851 /// processing threads, the replication manager creates and manages a
2852 /// few of its own threads of control.
2854 /// <param name="holdElection">
2855 /// If true, start as a client, and call for an election if no master is
2858 public void RepMgrStartClient(int nthreads, bool holdElection) {
2859 dbenv.repmgr_start(nthreads,
2861 DbConstants.DB_REP_ELECTION : DbConstants.DB_REP_CLIENT);
2864 /// Start the replication manager as a master site, and do not call for
2869 /// There are two ways to build Berkeley DB replication applications:
2870 /// the most common approach is to use the Berkeley DB library
2871 /// "replication manager" support, where the Berkeley DB library manages
2872 /// the replication group, including network transport, all replication
2873 /// message processing and acknowledgment, and group elections.
2874 /// Applications using the replication manager support generally make
2875 /// the following calls:
2877 /// <list type="number">
2879 /// Configure the local site in the replication group,
2880 /// <see cref="RepMgrLocalSite"/>.
2883 /// Call <see cref="RepMgrSiteConfig"/> to configure the remote
2884 /// site(s) in the replication group.
2886 /// <item>Configure the message acknowledgment policy
2887 /// (<see cref="RepMgrAckPolicy"/>) which provides the replication group's
2888 /// transactional needs.
2891 /// Configure the local site's election priority,
2892 /// <see cref="RepPriority"/>.
2895 /// Call <see cref="RepMgrStartClient"/> or
2896 /// <see cref="RepMgrStartMaster"/> to start the replication
2901 /// For more information on building replication manager applications,
2902 /// please see the Replication Getting Started Guide included in the
2903 /// Berkeley DB documentation.
2906 /// Applications with special needs (for example, applications using
2907 /// network protocols not supported by the Berkeley DB replication
2908 /// manager), must perform additional configuration and call other
2909 /// Berkeley DB replication methods. For more information on building
2910 /// advanced replication applications, please see the Base Replication
2911 /// API section in the Berkeley DB Programmer's Reference Guide for more
2915 /// Starting the replication manager consists of opening the TCP/IP
2916 /// listening socket to accept incoming connections, and starting all
2917 /// necessary background threads. When multiple processes share a
2918 /// database environment, only one process can open the listening
2919 /// socket; <see cref="RepMgrStartMaster"/> (and
2920 /// <see cref="RepMgrStartClient"/>) automatically open the socket in
2921 /// the first process to call it, and skips this step in the later calls
2922 /// from other processes.
2925 /// <param name="nthreads">
2926 /// Specify the number of threads of control created and dedicated to
2927 /// processing replication messages. In addition to these message
2928 /// processing threads, the replication manager creates and manages a
2929 /// few of its own threads of control.
2931 public void RepMgrStartMaster(int nthreads) {
2932 dbenv.repmgr_start(nthreads, DbConstants.DB_REP_MASTER);
2936 /// Process an incoming replication message sent by a member of the
2937 /// replication group to the local database environment.
2941 /// RepProcessMessage is not called by most replication applications. It
2942 /// should only be called by applications implementing their own network
2943 /// transport layer, explicitly holding replication group elections and
2944 /// handling replication messages outside of the replication manager
2948 /// For implementation reasons, all incoming replication messages must
2949 /// be processed using the same <see cref="DatabaseEnvironment"/>
2950 /// object. It is not required that a single thread of control process
2951 /// all messages, only that all threads of control processing messages
2952 /// use the same object.
2955 /// Before calling this method, the <see cref="RepTransport"/> delegate
2956 /// must already have been configured to send replication messages.
2959 /// <param name="control">
2960 /// A copy of the control parameter specified by Berkeley DB on the
2961 /// sending environment.
2963 /// <param name="rec">
2964 /// A copy of the rec parameter specified by Berkeley DB on the sending
2967 /// <param name="envid">
2968 /// The local identifier that corresponds to the environment that sent
2969 /// the message to be processed (see Replication environment IDs in the
2970 /// Programmer's Reference Guide for more information)..
2972 /// <returns>The result of processing a message</returns>
2973 public RepProcMsgResult RepProcessMessage(
2974 DatabaseEntry control, DatabaseEntry rec, int envid) {
2975 DB_LSN dblsn = new DB_LSN();
2976 int ret = dbenv.rep_process_message(control,
2978 LSN lsnp = new LSN(dblsn.file, dblsn.offset);
2979 RepProcMsgResult result = new RepProcMsgResult(ret, lsnp);
2980 if (result.Result == RepProcMsgResult.ProcMsgResult.ERROR)
2981 DatabaseException.ThrowException(ret);
2986 /// Configure the database environment as a client in a group of
2987 /// replicated database environments.
2989 public void RepStartClient() {
2990 RepStartClient(null);
2993 /// Configure the database environment as a client in a group of
2994 /// replicated database environments.
2998 /// RepStartClient is not called by most replication applications. It
2999 /// should only be called by applications implementing their own network
3000 /// transport layer, explicitly holding replication group elections and
3001 /// handling replication messages outside of the replication manager
3005 /// Replication master environments are the only database environments
3006 /// where replicated databases may be modified. Replication client
3007 /// environments are read-only as long as they are clients. Replication
3008 /// client environments may be upgraded to be replication master
3009 /// environments in the case that the current master fails or there is
3010 /// no master present. If master leases are in use, this method cannot
3011 /// be used to appoint a master, and should only be used to configure a
3012 /// database environment as a master as the result of an election.
3015 /// Before calling this method, the <see cref="RepTransport"/> delegate
3016 /// must already have been configured to send replication messages.
3019 /// <param name="cdata">
3020 /// An opaque data item that is sent over the communication
3021 /// infrastructure when the client comes online (see Connecting to a new
3022 /// site in the Programmer's Reference Guide for more information). If
3023 /// no such information is useful, cdata should be null.
3025 public void RepStartClient(DatabaseEntry cdata) {
3027 cdata, DbConstants.DB_REP_CLIENT);
3030 /// Configure the database environment as a master in a group of
3031 /// replicated database environments.
3033 public void RepStartMaster() {
3034 RepStartMaster(null);
3037 /// Configure the database environment as a master in a group of
3038 /// replicated database environments.
3042 /// RepStartMaster is not called by most replication applications. It
3043 /// should only be called by applications implementing their own network
3044 /// transport layer, explicitly holding replication group elections and
3045 /// handling replication messages outside of the replication manager
3049 /// Replication master environments are the only database environments
3050 /// where replicated databases may be modified. Replication client
3051 /// environments are read-only as long as they are clients. Replication
3052 /// client environments may be upgraded to be replication master
3053 /// environments in the case that the current master fails or there is
3054 /// no master present. If master leases are in use, this method cannot
3055 /// be used to appoint a master, and should only be used to configure a
3056 /// database environment as a master as the result of an election.
3059 /// Before calling this method, the <see cref="RepTransport"/> delegate
3060 /// must already have been configured to send replication messages.
3063 /// <param name="cdata">
3064 /// An opaque data item that is sent over the communication
3065 /// infrastructure when the client comes online (see Connecting to a new
3066 /// site in the Programmer's Reference Guide for more information). If
3067 /// no such information is useful, cdata should be null.
3069 public void RepStartMaster(DatabaseEntry cdata) {
3071 cdata, DbConstants.DB_REP_MASTER);
3075 /// Force master synchronization to begin for this client.
3079 /// This method is the other half of setting
3080 /// <see cref="RepDelayClientSync"/>.
3083 /// If an application has configured delayed master synchronization, the
3084 /// application must synchronize explicitly (otherwise the client will
3085 /// remain out-of-date and will ignore all database changes forwarded
3086 /// from the replication group master). RepSync may be called any time
3087 /// after the client application learns that the new master has been
3088 /// established (by receiving
3089 /// <see cref="NotificationEvent.REP_NEWMASTER"/>).
3092 /// Before calling this method, the <see cref="RepTransport"/> delegate
3093 /// must already have been configured to send replication messages.
3096 public void RepSync() {
3101 /// The names of all of the log files that are no longer in use (for
3102 /// example, that are no longer involved in active transactions), and
3103 /// that may safely be archived for catastrophic recovery and then
3104 /// removed from the system.
3108 /// The Berkeley DB interfaces to the database environment logging
3109 /// subsystem (for example, <see cref="Transaction.Abort"/>) may
3110 /// allocate log cursors and have open file descriptors for log files
3111 /// as well. On operating systems where filesystem related system calls
3112 /// (for example, rename and unlink on Windows/NT) can fail if a process
3113 /// has an open file descriptor for the affected file, attempting to
3114 /// move or remove the log files listed by ArchivableLogFiles may fail.
3115 /// All Berkeley DB internal use of log cursors operates on active log
3116 /// files only and furthermore, is short-lived in nature. So, an
3117 /// application seeing such a failure should be restructured to retry
3118 /// the operation until it succeeds. (Although this is not likely to be
3119 /// necessary; it is hard to imagine a reason to move or rename a log
3120 /// file in which transactions are being logged or aborted.)
3123 /// When Replication Manager is in use, log archiving will be performed
3124 /// in a replication group-aware manner such that the log file status of
3125 /// other sites in the group will be considered to determine if a log
3126 /// file could be in use.
3129 /// See the db_archive utility for more information on database archival
3133 /// <param name="AbsolutePaths">
3134 /// If true, all pathnames are returned as absolute pathnames, instead
3135 /// of relative to the database home directory.
3138 /// The names of all of the log files that are no longer in use
3140 public List<string> ArchivableLogFiles(bool AbsolutePaths) {
3142 flags |= AbsolutePaths ? DbConstants.DB_ARCH_ABS : 0;
3143 return dbenv.log_archive(flags);
3146 /// The database files that need to be archived in order to recover the
3147 /// database from catastrophic failure. If any of the database files
3148 /// have not been accessed during the lifetime of the current log files,
3149 /// they will not included in this list. It is also possible that some
3150 /// of the files referred to by the log have since been deleted from the
3155 /// When Replication Manager is in use, log archiving will be performed
3156 /// in a replication group-aware manner such that the log file status of
3157 /// other sites in the group will be considered to determine if a log
3158 /// file could be in use.
3161 /// See the db_archive utility for more information on database archival
3165 /// <param name="AbsolutePaths">
3166 /// If true, all pathnames are returned as absolute pathnames, instead
3167 /// of relative to the database home directory.
3170 /// The database files that need to be archived in order to recover the
3171 /// database from catastrophic failure.
3173 public List<string> ArchivableDatabaseFiles(bool AbsolutePaths) {
3174 uint flags = DbConstants.DB_ARCH_DATA;
3175 flags |= AbsolutePaths ? DbConstants.DB_ARCH_ABS : 0;
3176 return dbenv.log_archive(flags);
3179 /// The names of all of the log files
3183 /// The Berkeley DB interfaces to the database environment logging
3184 /// subsystem (for example, <see cref="Transaction.Abort"/>) may
3185 /// allocate log cursors and have open file descriptors for log files
3186 /// as well. On operating systems where filesystem related system calls
3187 /// (for example, rename and unlink on Windows/NT) can fail if a process
3188 /// has an open file descriptor for the affected file, attempting to
3189 /// move or remove the log files listed by LogFiles may fail. All
3190 /// Berkeley DB internal use of log cursors operates on active log files
3191 /// only and furthermore, is short-lived in nature. So, an application
3192 /// seeing such a failure should be restructured to retry the operation
3193 /// until it succeeds. (Although this is not likely to be necessary; it
3194 /// is hard to imagine a reason to move or rename a log file in which
3195 /// transactions are being logged or aborted.)
3198 /// See the db_archive utility for more information on database archival
3202 /// <param name="AbsolutePaths">
3203 /// If true, all pathnames are returned as absolute pathnames, instead
3204 /// of relative to the database home directory.
3207 /// All the log filenames, regardless of whether or not they are in use.
3209 public List<string> LogFiles(bool AbsolutePaths) {
3210 uint flags = DbConstants.DB_ARCH_LOG;
3211 flags |= AbsolutePaths ? DbConstants.DB_ARCH_ABS : 0;
3212 return dbenv.log_archive(flags);
3215 /// Remove log files that are no longer needed. Automatic log file
3216 /// removal is likely to make catastrophic recovery impossible.
3220 /// When Replication Manager is in use, log archiving will be performed
3221 /// in a replication group-aware manner such that the log file status of
3222 /// other sites in the group will be considered to determine if a log
3223 /// file could be in use.
3226 public void RemoveUnusedLogFiles() {
3227 dbenv.log_archive(DbConstants.DB_ARCH_REMOVE);
3231 /// Allocate a locker ID in an environment configured for Berkeley DB
3232 /// Concurrent Data Store applications.
3236 /// Calling <see cref="Transaction.Commit"/> will discard the allocated
3240 /// See Berkeley DB Concurrent Data Store applications in the
3241 /// Programmer's Reference Guide for more information about when this is
3246 /// A Transaction object that uniquely identifies the locker ID
3248 public Transaction BeginCDSGroup() {
3249 return new Transaction(dbenv.cdsgroup_begin());
3253 /// Create a new transaction in the environment, with the default
3256 /// <returns>A new transaction object</returns>
3257 public Transaction BeginTransaction() {
3258 return BeginTransaction(new TransactionConfig(), null);
3261 /// Create a new transaction in the environment.
3263 /// <param name="cfg">
3264 /// The configuration properties for the transaction
3266 /// <returns>A new transaction object</returns>
3267 public Transaction BeginTransaction(TransactionConfig cfg) {
3268 return BeginTransaction(cfg, null);
3271 /// Create a new transaction in the environment.
3274 /// In the presence of distributed transactions and two-phase commit,
3275 /// only the parental transaction, that is a transaction without a
3276 /// parent specified, should be passed as an parameter to
3277 /// <see cref="Transaction.Prepare"/>.
3279 /// <param name="cfg">
3280 /// The configuration properties for the transaction
3282 /// <param name="parent">
3283 /// If the non-null, the new transaction will be a nested transaction,
3284 /// with <paramref name="parent"/> as the new transaction's parent.
3285 /// Transactions may be nested to any level.
3287 /// <returns>A new transaction object</returns>
3288 public Transaction BeginTransaction(
3289 TransactionConfig cfg, Transaction parent) {
3290 DB_TXN dbtxn = dbenv.txn_begin(
3291 Transaction.getDB_TXN(parent), cfg.flags);
3292 Transaction txn = new Transaction(dbtxn);
3293 if (cfg.lockTimeoutIsSet)
3294 txn.SetLockTimeout(cfg.LockTimeout);
3296 txn.Name = cfg.Name;
3297 if (cfg.txnTimeoutIsSet)
3298 txn.SetTxnTimeout(cfg.TxnTimeout);
3304 /// Flush the underlying memory pool, write a checkpoint record to the
3305 /// log, and then flush the log, even if there has been no activity
3306 /// since the last checkpoint.
3308 public void Checkpoint() {
3309 dbenv.txn_checkpoint(0, 0, DbConstants.DB_FORCE);
3312 /// If there has been any logging activity in the database environment
3313 /// since the last checkpoint, flush the underlying memory pool, write a
3314 /// checkpoint record to the log, and then flush the log.
3316 /// <param name="kbytesWritten">
3317 /// A checkpoint will be done if more than kbytesWritten kilobytes of
3318 /// log data have been written since the last checkpoint.
3320 /// <param name="minutesElapsed">
3321 /// A checkpoint will be done if more than minutesElapsed minutes have
3322 /// passed since the last checkpoint.
3324 public void Checkpoint(uint kbytesWritten, uint minutesElapsed) {
3325 dbenv.txn_checkpoint(kbytesWritten, minutesElapsed, 0);
3329 /// By closing the Berkeley DB environment you can free allocated resources
3330 /// and close any open databases along with the underlying subsystems.
3334 /// The object should not be closed while any other handle that refers
3335 /// to it is not yet closed; for example, database environment handles
3336 /// must not be closed while transactions in the environment have
3337 /// not yet been committed or aborted.
3338 /// When you close each database handle, by default, the database is not synchronized.
3339 /// To synchronize all open databases ensure that the last environment object is closed by the CloseForceSync() method.
3340 /// When the close operation fails, the method returns a non-zero error value for the first instance of such error,
3341 /// and continues to close the rest of the environment objects.
3344 /// Where the environment was configured with
3345 /// <see cref="DatabaseEnvironmentConfig.UseTxns"/>, calling CloseForceSync
3346 /// aborts any unresolved transactions. Applications should not depend
3347 /// on this behavior for transactions involving Berkeley DB databases;
3348 /// all such transactions should be explicitly resolved. The problem
3349 /// with depending on this semantic is that aborting an unresolved
3350 /// transaction involving database operations requires a database
3351 /// handle. Because the database handles should have been closed before
3352 /// calling CloseForceSync, it will not be possible to abort the
3353 /// transaction, and recovery will have to be run on the Berkeley DB
3354 /// environment before further operations are done.
3357 /// In multithreaded applications, only a single thread may call
3361 public void Close() {
3366 /// Close the Berkeley DB environment, freeing any allocated resources,
3367 /// closing any open databases as well as underlying subsystems.
3371 /// The object should not be closed while any other handle that refers
3372 /// to it is not yet closed; for example, database environment handles
3373 /// must not be closed while transactions in the environment have
3374 /// not yet been committed or aborted. If there are open database
3375 /// handles, they are all closed, and each of them will be synced on
3376 /// close. The first error in the close operations, if any, is
3377 /// returned at last, and the environment close procedures will
3378 /// carry on anyway.
3381 /// Where the environment was configured with
3382 /// <see cref="DatabaseEnvironmentConfig.UseTxns"/>, calling CloseForceSync
3383 /// aborts any unresolved transactions. Applications should not depend
3384 /// on this behavior for transactions involving Berkeley DB databases;
3385 /// all such transactions should be explicitly resolved. The problem
3386 /// with depending on this semantic is that aborting an unresolved
3387 /// transaction involving database operations requires a database
3388 /// handle. Because the database handles should have been closed before
3389 /// calling CloseForceSync, it will not be possible to abort the
3390 /// transaction, and recovery will have to be run on the Berkeley DB
3391 /// environment before further operations are done.
3394 /// In multithreaded applications, only a single thread may call
3398 public void CloseForceSync() {
3399 dbenv.close(DbConstants.DB_FORCESYNC);
3403 /// Run one iteration of the deadlock detector. The deadlock detector
3404 /// traverses the lock table and marks one of the participating lock
3405 /// requesters for rejection in each deadlock it finds.
3407 /// <param name="atype">Specify which lock request(s) to reject</param>
3408 /// <returns>The number of lock requests that were rejected.</returns>
3409 public uint DetectDeadlocks(DeadlockPolicy atype) {
3410 uint rejectCount = 0;
3412 atype = DeadlockPolicy.DEFAULT;
3413 dbenv.lock_detect(0, atype.policy, ref rejectCount);
3418 /// Check for threads of control (either a true thread or a process)
3419 /// that have exited while manipulating Berkeley DB library data
3420 /// structures, while holding a logical database lock, or with an
3421 /// unresolved transaction (that is, a transaction that was never
3422 /// aborted or committed).
3426 /// For more information, see Architecting Data Store and Concurrent
3427 /// Data Store applications, and Architecting Transactional Data Store
3428 /// applications, both in the Berkeley DB Programmer's Reference Guide.
3431 /// FailCheck is based on the <see cref="SetThreadID"/> and
3432 /// <see cref="ThreadIsAlive"/> delegates. Applications calling
3433 /// FailCheck must have already set <see cref="ThreadIsAlive"/>, and
3434 /// must have configured <see cref="ThreadCount"/>.
3437 /// If FailCheck determines a thread of control exited while holding
3438 /// database read locks, it will release those locks. If FailCheck
3439 /// determines a thread of control exited with an unresolved
3440 /// transaction, the transaction will be aborted. In either of these
3441 /// cases, FailCheck will return successfully and the application may
3442 /// continue to use the database environment.
3445 /// In either of these cases, FailCheck will also report the process and
3446 /// thread IDs associated with any released locks or aborted
3447 /// transactions. The information is printed to a specified output
3448 /// channel (see <see cref="Verbosity"/> for more information), or
3449 /// passed to an application delegate (see <see cref="Feedback"/> for
3450 /// more information).
3453 /// If FailCheck determines a thread of control has exited such that
3454 /// database environment recovery is required, it will throw
3455 /// <see cref="RunRecoveryException"/>. In this case, the application
3456 /// should not continue to use the database environment. For a further
3457 /// description as to the actions the application should take when this
3458 /// failure occurs, see Handling failure in Data Store and Concurrent
3459 /// Data Store applications, and Handling failure in Transactional Data
3460 /// Store applications, both in the Berkeley DB Programmer's Reference
3464 public void FailCheck() {
3469 /// This method checks to see if a specified transaction has been replicated from
3470 /// the master of a replication group. It may be called by applications using either
3471 /// the Base API or the Replication Manager.
3473 /// <param name="token">
3474 /// The commit token from a transaction previously written at a master
3475 /// site in the replication group. Commit tokens are retrieved using
3476 /// the <see cref="Transaction.CommitToken"/> method.
3478 /// <param name="timeout">
3479 /// The maximum time to wait for the transaction to arrive by replication, expressed in
3480 /// microseconds. To check the status of the transaction without waiting, the timeout
3481 /// may be specified as 0.
3484 /// This method returns TransactionAppliedStatus.APPLIED to indicate that the specified
3485 /// transaction has indeed been applied at the local site. TransactionAppliedStatus.TIMEOUT
3486 /// will be returned if the specified transaction has not yet arrived at the calling site,
3487 /// but can be expected to arrive soon. TransactionAppliedStatus.NOTFOUND will be returned
3488 /// if the transaction has not been applied at the local site, and it can be determined that
3489 /// the transaction has been rolled back due to a master takeover, and is therefore never
3490 /// expected to arrive. TransactionAppliedStatus.EMPTY_TRANSACTION will be return if the specified
3491 /// token was generated by a transaction that did not modify the database environment
3492 /// (e.g., a read-only transaction).
3494 public TransactionAppliedStatus IsTransactionApplied(byte[] token, uint timeout)
3497 throw new ArgumentNullException("The token cannot be null.");
3498 if (token.Length != DbConstants.DB_TXN_TOKEN_SIZE)
3500 throw new ArgumentOutOfRangeException("The token size must be "
3501 + DbConstants.DB_TXN_TOKEN_SIZE);
3503 DB_TXN_TOKEN txn_token = new DB_TXN_TOKEN(token);
3504 int ret = dbenv.is_transaction_applied(txn_token, timeout, 0);
3508 return TransactionAppliedStatus.APPLIED;
3509 case DbConstants.DB_TIMEOUT:
3510 return TransactionAppliedStatus.TIMEOUT;
3511 case DbConstants.DB_NOTFOUND:
3512 return TransactionAppliedStatus.NOTFOUND;
3513 case DbConstants.DB_KEYEMPTY:
3514 return TransactionAppliedStatus.EMPTY_TRANSACTION;
3516 throw new DatabaseException(ret);
3521 /// Map an LSN object to a log filename
3523 /// <param name="logSeqNum">
3524 /// The DB_LSN structure for which a filename is wanted.
3527 /// The name of the file containing the record named by
3528 /// <paramref name="logSeqNum"/>.
3530 public string LogFile(LSN logSeqNum) {
3531 return dbenv.log_file(LSN.getDB_LSN(logSeqNum));
3535 /// Write all log records to disk.
3537 public void LogFlush() {
3541 /// Write log records to disk.
3543 /// <param name="logSeqNum">
3544 /// All log records with LSN values less than or equal to
3545 /// <paramref name="logSeqNum"/> are written to disk. If null, all
3546 /// records in the log are flushed.
3548 public void LogFlush(LSN logSeqNum) {
3549 dbenv.log_flush(LSN.getDB_LSN(logSeqNum));
3553 /// Verify log records of this environment.
3555 /// <param name="config">
3556 /// Log verification configuration object.
3558 public int LogVerify(LogVerifyConfig config) {
3559 String dbfile, dbname, home;
3562 uint efile, eoffset, sfile, soffset;
3563 int caf, ret, verbose;
3565 caf = ret = verbose = 0;
3567 home = config.EnvHome;
3568 dbfile = config.DbFile;
3569 dbname = config.DbName;
3571 etime = (int)config.EndTime.ToFileTimeUtc();
3572 stime = (int)config.StartTime.ToFileTimeUtc();
3573 } catch (Exception){}
3575 efile = config.EndLsn.LogFileNumber;
3576 eoffset = config.EndLsn.Offset;
3577 sfile = config.StartLsn.LogFileNumber;
3578 soffset = config.StartLsn.Offset;
3579 cachesize = config.CacheSize;
3582 if (config.ContinueAfterFail)
3585 ret = dbenv.log_verify(home, cachesize, dbfile, dbname,
3586 stime, etime, sfile, soffset, efile, eoffset, caf, verbose);
3587 } catch (Exception){}
3593 /// Append a record to the log
3595 /// <param name="dbt">The record to write to the log.</param>
3596 /// <param name="flush">
3597 /// If true, the log is forced to disk after this record is written,
3598 /// guaranteeing that all records with LSN values less than or equal to
3599 /// the one being "put" are on disk before LogWrite returns.
3601 /// <returns>The LSN of the written record</returns>
3602 public LSN LogWrite(DatabaseEntry dbt, bool flush) {
3603 DB_LSN lsn = new DB_LSN();
3606 dbt, flush ? DbConstants.DB_FLUSH : 0);
3607 return new LSN(lsn.file, lsn.offset);
3611 /// Set the panic state for the database environment. (Database
3612 /// environments in a panic state normally refuse all attempts to call
3613 /// Berkeley DB functions, throwing <see cref="RunRecoveryException"/>.)
3615 public void Panic() {
3616 dbenv.set_flags(DbConstants.DB_PANIC_ENVIRONMENT, 1);
3620 /// Restore transactions that were prepared, but not yet resolved at the
3621 /// time of the system shut down or crash, to their state prior to the
3622 /// shut down or crash, including any locks previously held.
3625 /// Calls to Recover from different threads of control should not be
3626 /// intermixed in the same environment.
3628 /// <param name="count">
3629 /// The maximum number of <see cref="PreparedTransaction"/> objects
3632 /// <param name="resume">
3633 /// If true, continue returning a list of prepared, but not yet resolved
3634 /// transactions, starting where the last call to Recover left off. If
3635 /// false, begins a new pass over all prepared, but not yet completed
3636 /// transactions, regardless of whether they have already been returned
3637 /// in previous calls to Recover.
3639 /// <returns>A list of the prepared transactions</returns>
3640 public PreparedTransaction[] Recover(int count, bool resume) {
3642 flags |= resume ? DbConstants.DB_NEXT : DbConstants.DB_FIRST;
3644 return dbenv.txn_recover(count, flags);
3648 /// Remove the underlying file represented by <paramref name="file"/>,
3649 /// incidentally removing all of the databases it contained.
3651 /// <param name="file">The physical file to be removed.</param>
3652 /// <param name="autoCommit">
3653 /// If true, enclose RemoveDB within a transaction. If the call
3654 /// succeeds, changes made by the operation will be recoverable. If the
3655 /// call fails, the operation will have made no changes.
3657 public void RemoveDB(string file, bool autoCommit) {
3658 RemoveDB(file, null, autoCommit, null);
3661 /// Remove the database specified by <paramref name="file"/> and
3662 /// <paramref name="database"/>. If no database is specified, the
3663 /// underlying file represented by <paramref name="file"/> is removed,
3664 /// incidentally removing all of the databases it contained.
3666 /// <param name="file">
3667 /// The physical file which contains the database(s) to be removed.
3669 /// <param name="database">The database to be removed.</param>
3670 /// <param name="autoCommit">
3671 /// If true, enclose RemoveDB within a transaction. If the call
3672 /// succeeds, changes made by the operation will be recoverable. If the
3673 /// call fails, the operation will have made no changes.
3675 public void RemoveDB(string file, string database, bool autoCommit) {
3676 RemoveDB(file, database, autoCommit, null);
3679 /// Remove the underlying file represented by <paramref name="file"/>,
3680 /// incidentally removing all of the databases it contained.
3682 /// <param name="file">The physical file to be removed.</param>
3683 /// <param name="autoCommit">
3684 /// If true, enclose RemoveDB within a transaction. If the call
3685 /// succeeds, changes made by the operation will be recoverable. If the
3686 /// call fails, the operation will have made no changes.
3688 /// <param name="txn">
3689 /// If the operation is part of an application-specified transaction,
3690 /// <paramref name="txn"/> is a Transaction object returned from
3691 /// <see cref="DatabaseEnvironment.BeginTransaction"/>; if
3692 /// the operation is part of a Berkeley DB Concurrent Data Store group,
3693 /// <paramref name="txn"/> is a handle returned from
3694 /// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null. If
3695 /// null, but <paramref name="autoCommit"/> or <see cref="AutoCommit"/>
3696 /// is true, the operation will be implicitly transaction protected.
3698 public void RemoveDB(string file, bool autoCommit, Transaction txn) {
3699 RemoveDB(file, null, autoCommit, txn);
3702 /// Remove the database specified by <paramref name="file"/> and
3703 /// <paramref name="database"/>. If no database is specified, the
3704 /// underlying file represented by <paramref name="file"/> is removed,
3705 /// incidentally removing all of the databases it contained.
3707 /// <param name="file">
3708 /// The physical file which contains the database(s) to be removed.
3710 /// <param name="database">The database to be removed.</param>
3711 /// <param name="autoCommit">
3712 /// If true, enclose RemoveDB within a transaction. If the call
3713 /// succeeds, changes made by the operation will be recoverable. If the
3714 /// call fails, the operation will have made no changes.
3716 /// <param name="txn">
3717 /// If the operation is part of an application-specified transaction,
3718 /// <paramref name="txn"/> is a Transaction object returned from
3719 /// <see cref="DatabaseEnvironment.BeginTransaction"/>; if
3720 /// the operation is part of a Berkeley DB Concurrent Data Store group,
3721 /// <paramref name="txn"/> is a handle returned from
3722 /// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null. If
3723 /// null, but <paramref name="autoCommit"/> or <see cref="AutoCommit"/>
3724 /// is true, the operation will be implicitly transaction protected.
3726 public void RemoveDB(
3727 string file, string database, bool autoCommit, Transaction txn) {
3728 dbenv.dbremove(Transaction.getDB_TXN(txn),
3729 file, database, autoCommit ? DbConstants.DB_AUTO_COMMIT : 0);
3733 /// Rename the underlying file represented by <paramref name="file"/>
3734 /// using the value supplied to <paramref name="newname"/>, incidentally
3735 /// renaming all of the databases it contained.
3737 /// <param name="file">The physical file to be renamed.</param>
3738 /// <param name="newname">The new name of the database or file.</param>
3739 /// <param name="autoCommit">
3740 /// If true, enclose RenameDB within a transaction. If the call
3741 /// succeeds, changes made by the operation will be recoverable. If the
3742 /// call fails, the operation will have made no changes.
3744 public void RenameDB(string file, string newname, bool autoCommit) {
3745 RenameDB(file, null, newname, autoCommit, null);
3748 /// Rename the database specified by <paramref name="file"/> and
3749 /// <paramref name="database"/> to <paramref name="newname"/>. If no
3750 /// database is specified, the underlying file represented by
3751 /// <paramref name="file"/> is renamed using the value supplied to
3752 /// <paramref name="newname"/>, incidentally renaming all of the
3753 /// databases it contained.
3755 /// <param name="file">
3756 /// The physical file which contains the database(s) to be renamed.
3758 /// <param name="database">The database to be renamed.</param>
3759 /// <param name="newname">The new name of the database or file.</param>
3760 /// <param name="autoCommit">
3761 /// If true, enclose RenameDB within a transaction. If the call
3762 /// succeeds, changes made by the operation will be recoverable. If the
3763 /// call fails, the operation will have made no changes.
3765 public void RenameDB(
3766 string file, string database, string newname, bool autoCommit) {
3767 RenameDB(file, database, newname, autoCommit, null);
3770 /// Rename the underlying file represented by <paramref name="file"/>
3771 /// using the value supplied to <paramref name="newname"/>, incidentally
3772 /// renaming all of the databases it contained.
3774 /// <param name="file">The physical file to be renamed.</param>
3775 /// <param name="newname">The new name of the database or file.</param>
3776 /// <param name="autoCommit">
3777 /// If true, enclose RenameDB within a transaction. If the call
3778 /// succeeds, changes made by the operation will be recoverable. If the
3779 /// call fails, the operation will have made no changes.
3781 /// <param name="txn">
3782 /// If the operation is part of an application-specified transaction,
3783 /// <paramref name="txn"/> is a Transaction object returned from
3784 /// <see cref="DatabaseEnvironment.BeginTransaction"/>; if
3785 /// the operation is part of a Berkeley DB Concurrent Data Store group,
3786 /// <paramref name="txn"/> is a handle returned from
3787 /// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null. If
3788 /// null, but <paramref name="autoCommit"/> or <see cref="AutoCommit"/>
3789 /// is true, the operation will be implicitly transaction protected.
3791 public void RenameDB(string file,
3792 string newname, bool autoCommit, Transaction txn) {
3793 RenameDB(file, null, newname, autoCommit, txn);
3796 /// Rename the database specified by <paramref name="file"/> and
3797 /// <paramref name="database"/> to <paramref name="newname"/>. If no
3798 /// database is specified, the underlying file represented by
3799 /// <paramref name="file"/> is renamed using the value supplied to
3800 /// <paramref name="newname"/>, incidentally renaming all of the
3801 /// databases it contained.
3803 /// <param name="file">
3804 /// The physical file which contains the database(s) to be renamed.
3806 /// <param name="database">The database to be renamed.</param>
3807 /// <param name="newname">The new name of the database or file.</param>
3808 /// <param name="autoCommit">
3809 /// If true, enclose RenameDB within a transaction. If the call
3810 /// succeeds, changes made by the operation will be recoverable. If the
3811 /// call fails, the operation will have made no changes.
3813 /// <param name="txn">
3814 /// If the operation is part of an application-specified transaction,
3815 /// <paramref name="txn"/> is a Transaction object returned from
3816 /// <see cref="DatabaseEnvironment.BeginTransaction"/>; if
3817 /// the operation is part of a Berkeley DB Concurrent Data Store group,
3818 /// <paramref name="txn"/> is a handle returned from
3819 /// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null. If
3820 /// null, but <paramref name="autoCommit"/> or <see cref="AutoCommit"/>
3821 /// is true, the operation will be implicitly transaction protected.
3823 public void RenameDB(string file,
3824 string database, string newname, bool autoCommit, Transaction txn) {
3825 dbenv.dbrename(Transaction.getDB_TXN(txn), file,
3826 database, newname, autoCommit ? DbConstants.DB_AUTO_COMMIT : 0);
3830 /// Allow database files to be copied, and then the copy used in the
3831 /// same database environment as the original.
3835 /// All databases contain an ID string used to identify the database in
3836 /// the database environment cache. If a physical database file is
3837 /// copied, and used in the same environment as another file with the
3838 /// same ID strings, corruption can occur. ResetFileID creates new ID
3839 /// strings for all of the databases in the physical file.
3842 /// ResetFileID modifies the physical file, in-place. Applications
3843 /// should not reset IDs in files that are currently in use.
3846 /// <param name="file">
3847 /// The name of the physical file in which new file IDs are to be created.
3849 /// <param name="encrypted">
3850 /// If true, he file contains encrypted databases.
3852 public void ResetFileID(string file, bool encrypted) {
3853 dbenv.fileid_reset(file, encrypted ? DbConstants.DB_ENCRYPT : 0);
3857 /// Allow database files to be moved from one transactional database
3858 /// environment to another.
3862 /// Database pages in transactional database environments contain
3863 /// references to the environment's log files (that is, log sequence
3864 /// numbers, or <see cref="LSN"/>s). Copying or moving a database file
3865 /// from one database environment to another, and then modifying it, can
3866 /// result in data corruption if the LSNs are not first cleared.
3869 /// Note that LSNs should be reset before moving or copying the database
3870 /// file into a new database environment, rather than moving or copying
3871 /// the database file and then resetting the LSNs. Berkeley DB has
3872 /// consistency checks that may be triggered if an application calls
3873 /// ResetLSN on a database in a new environment when the database LSNs
3874 /// still reflect the old environment.
3877 /// The ResetLSN method modifies the physical file, in-place.
3878 /// Applications should not reset LSNs in files that are currently in
3882 /// <param name="file"></param>
3883 /// <param name="encrypted"></param>
3884 public void ResetLSN(string file, bool encrypted) {
3885 dbenv.lsn_reset(file, encrypted ? DbConstants.DB_ENCRYPT : 0);
3889 /// Limit the number of sequential write operations scheduled by the
3890 /// library when flushing dirty pages from the cache.
3892 /// <param name="maxWrites">
3893 /// The maximum number of sequential write operations scheduled by the
3894 /// library when flushing dirty pages from the cache, or 0 if there is
3895 /// no limitation on the number of sequential write operations.
3897 /// <param name="pause">
3898 /// The number of microseconds the thread of control should pause before
3899 /// scheduling further write operations. It must be specified as an
3900 /// unsigned 32-bit number of microseconds, limiting the maximum pause
3901 /// to roughly 71 minutes.
3903 public void SetMaxSequentialWrites(int maxWrites, uint pause) {
3904 dbenv.set_mp_max_write(maxWrites, pause);
3908 /// Flush all modified pages in the cache to their backing files.
3911 /// Pages in the cache that cannot be immediately written back to disk
3912 /// (for example, pages that are currently in use by another thread of
3913 /// control) are waited for and written to disk as soon as it is
3914 /// possible to do so.
3916 public void SyncMemPool() {
3920 /// Flush modified pages in the cache with log sequence numbers less
3921 /// than <paramref name="minLSN"/> to their backing files.
3924 /// Pages in the cache that cannot be immediately written back to disk
3925 /// (for example, pages that are currently in use by another thread of
3926 /// control) are waited for and written to disk as soon as it is
3927 /// possible to do so.
3929 /// <param name="minLSN">
3930 /// All modified pages with a log sequence number less than the minLSN
3931 /// parameter are written to disk. If null, all modified pages in the
3932 /// cache are written to disk.
3934 public void SyncMemPool(LSN minLSN) {
3935 dbenv.memp_sync(LSN.getDB_LSN(minLSN));
3939 /// Ensure that a specified percent of the pages in the cache are clean,
3940 /// by writing dirty pages to their backing files.
3942 /// <param name="pctClean">
3943 /// The percent of the pages in the cache that should be clean.
3946 /// The number of pages written to reach the specified percentage is
3949 public int TrickleCleanMemPool(int pctClean) {
3951 dbenv.memp_trickle(pctClean, ref ret);
3956 /// Append an informational message to the Berkeley DB database
3957 /// environment log files.
3960 /// WriteToLog allows applications to include information in the
3961 /// database environment log files, for later review using the
3962 /// db_printlog utility. This method is intended for debugging and
3963 /// performance tuning.
3965 /// <param name="str">The message to append to the log files</param>
3966 public void WriteToLog(string str) {
3967 dbenv.log_printf(null, str);
3970 /// Append an informational message to the Berkeley DB database
3971 /// environment log files.
3974 /// WriteToLog allows applications to include information in the
3975 /// database environment log files, for later review using the
3976 /// db_printlog utility. This method is intended for debugging and
3977 /// performance tuning.
3979 /// <param name="str">The message to append to the log files</param>
3980 /// <param name="txn">
3981 /// If the operation is part of an application-specified transaction,
3982 /// <paramref name="txn"/> is a Transaction object returned from
3983 /// <see cref="DatabaseEnvironment.BeginTransaction"/>;
3986 public void WriteToLog(string str, Transaction txn) {
3987 dbenv.log_printf(Transaction.getDB_TXN(txn), str);
3992 /// The locking subsystem statistics
3994 /// <returns>The locking subsystem statistics</returns>
3995 public LockStats LockingSystemStats() {
3996 return LockingSystemStats(false);
3999 /// The locking subsystem statistics
4001 /// <param name="clearStats">
4002 /// If true, reset statistics after returning their values.
4004 /// <returns>The locking subsystem statistics</returns>
4005 public LockStats LockingSystemStats(bool clearStats) {
4007 flags |= clearStats ? DbConstants.DB_STAT_CLEAR : 0;
4008 LockStatStruct st = dbenv.lock_stat(flags);
4009 return new LockStats(st);
4012 /// The logging subsystem statistics
4014 /// <returns>The logging subsystem statistics</returns>
4015 public LogStats LoggingSystemStats() {
4016 return LoggingSystemStats(false);
4019 /// The logging subsystem statistics
4021 /// <param name="clearStats">
4022 /// If true, reset statistics after returning their values.
4024 /// <returns>The logging subsystem statistics</returns>
4025 public LogStats LoggingSystemStats(bool clearStats) {
4027 flags |= clearStats ? DbConstants.DB_STAT_CLEAR : 0;
4028 LogStatStruct st = dbenv.log_stat(flags);
4029 return new LogStats(st);
4032 /// The memory pool (that is, the buffer cache) subsystem statistics
4034 /// <returns>The memory pool subsystem statistics</returns>
4035 public MPoolStats MPoolSystemStats() {
4036 return MPoolSystemStats(false);
4039 /// The memory pool (that is, the buffer cache) subsystem statistics
4041 /// <param name="clearStats">
4042 /// If true, reset statistics after returning their values.
4044 /// <returns>The memory pool subsystem statistics</returns>
4045 public MPoolStats MPoolSystemStats(bool clearStats) {
4047 flags |= clearStats ? DbConstants.DB_STAT_CLEAR : 0;
4048 MempStatStruct st = dbenv.memp_stat(flags);
4049 return new MPoolStats(st);
4052 /// The mutex subsystem statistics
4054 /// <returns>The mutex subsystem statistics</returns>
4055 public MutexStats MutexSystemStats() {
4056 return MutexSystemStats(false);
4059 /// The mutex subsystem statistics
4061 /// <param name="clearStats">
4062 /// If true, reset statistics after returning their values.
4064 /// <returns>The mutex subsystem statistics</returns>
4065 public MutexStats MutexSystemStats(bool clearStats) {
4067 flags |= clearStats ? DbConstants.DB_STAT_CLEAR : 0;
4068 MutexStatStruct st = dbenv.mutex_stat(flags);
4069 return new MutexStats(st);
4072 /// The replication manager statistics
4074 /// <returns>The replication manager statistics</returns>
4075 public RepMgrStats RepMgrSystemStats() {
4076 return RepMgrSystemStats(false);
4079 /// The replication manager statistics
4081 /// <param name="clearStats">
4082 /// If true, reset statistics after returning their values.
4084 /// <returns>The replication manager statistics</returns>
4085 public RepMgrStats RepMgrSystemStats(bool clearStats) {
4087 flags |= clearStats ? DbConstants.DB_STAT_CLEAR : 0;
4088 RepMgrStatStruct st = dbenv.repmgr_stat(flags);
4089 return new RepMgrStats(st);
4092 /// The replication subsystem statistics
4094 /// <returns>The replication subsystem statistics</returns>
4095 public ReplicationStats ReplicationSystemStats() {
4096 return ReplicationSystemStats(false);
4099 /// The replication subsystem statistics
4101 /// <param name="clearStats">
4102 /// If true, reset statistics after returning their values.
4104 /// <returns>The replication subsystem statistics</returns>
4105 public ReplicationStats ReplicationSystemStats(bool clearStats) {
4107 flags |= clearStats ? DbConstants.DB_STAT_CLEAR : 0;
4108 ReplicationStatStruct st = dbenv.rep_stat(flags);
4109 return new ReplicationStats(st);
4112 /// The transaction subsystem statistics
4114 /// <returns>The transaction subsystem statistics</returns>
4115 public TransactionStats TransactionSystemStats() {
4116 return TransactionSystemStats(false);
4119 /// The transaction subsystem statistics
4121 /// <param name="clearStats">
4122 /// If true, reset statistics after returning their values.
4124 /// <returns>The transaction subsystem statistics</returns>
4125 public TransactionStats TransactionSystemStats(bool clearStats) {
4127 flags |= clearStats ? DbConstants.DB_STAT_CLEAR : 0;
4128 TxnStatStruct st = dbenv.txn_stat(flags);
4129 return new TransactionStats(st);
4135 /// Display the locking subsystem statistical information, as described
4136 /// by <see cref="LockStats"/>.
4138 public void PrintLockingSystemStats() {
4139 PrintLockingSystemStats(false, false, false, false, false, false);
4142 /// Display the locking subsystem statistical information, as described
4143 /// by <see cref="LockStats"/>.
4145 /// <param name="PrintAll">
4146 /// If true, display all available information.
4148 /// <param name="ClearStats">
4149 /// If true, reset statistics after displaying their values.
4151 public void PrintLockingSystemStats(bool PrintAll, bool ClearStats) {
4152 PrintLockingSystemStats(
4153 PrintAll, ClearStats, false, false, false, false);
4156 /// Display the locking subsystem statistical information, as described
4157 /// by <see cref="LockStats"/>.
4159 /// <param name="PrintAll">
4160 /// If true, display all available information.
4162 /// <param name="ClearStats">
4163 /// If true, reset statistics after displaying their values.
4165 /// <param name="ConflictMatrix">
4166 /// If true, display the lock conflict matrix.
4168 /// <param name="Lockers">
4169 /// If true, Display the lockers within hash chains.
4171 /// <param name="Objects">
4172 /// If true, display the lock objects within hash chains.
4174 /// <param name="Parameters">
4175 /// If true, display the locking subsystem parameters.
4177 public void PrintLockingSystemStats(bool PrintAll, bool ClearStats,
4178 bool ConflictMatrix, bool Lockers, bool Objects, bool Parameters) {
4180 flags |= PrintAll ? DbConstants.DB_STAT_ALL : 0;
4181 flags |= ClearStats ? DbConstants.DB_STAT_CLEAR : 0;
4182 flags |= ConflictMatrix ? DbConstants.DB_STAT_LOCK_CONF : 0;
4183 flags |= Lockers ? DbConstants.DB_STAT_LOCK_LOCKERS : 0;
4184 flags |= Objects ? DbConstants.DB_STAT_LOCK_OBJECTS : 0;
4185 flags |= Parameters ? DbConstants.DB_STAT_LOCK_PARAMS : 0;
4187 dbenv.lock_stat_print(flags);
4191 /// Display the logging subsystem statistical information, as described
4192 /// by <see cref="LogStats"/>.
4194 public void PrintLoggingSystemStats() {
4195 PrintLoggingSystemStats(false, false);
4198 /// Display the logging subsystem statistical information, as described
4199 /// by <see cref="LogStats"/>.
4201 /// <param name="PrintAll">
4202 /// If true, display all available information.
4204 /// <param name="ClearStats">
4205 /// If true, reset statistics after displaying their values.
4207 public void PrintLoggingSystemStats(bool PrintAll, bool ClearStats) {
4209 flags |= PrintAll ? DbConstants.DB_STAT_ALL : 0;
4210 flags |= ClearStats ? DbConstants.DB_STAT_CLEAR : 0;
4212 dbenv.log_stat_print(flags);
4216 /// Display the memory pool (that is, buffer cache) subsystem
4217 /// statistical information, as described by <see cref="MPoolStats"/>.
4219 public void PrintMPoolSystemStats() {
4220 PrintMPoolSystemStats(false, false, false);
4223 /// Display the memory pool (that is, buffer cache) subsystem
4224 /// statistical information, as described by <see cref="MPoolStats"/>.
4226 /// <param name="PrintAll">
4227 /// If true, display all available information.
4229 /// <param name="ClearStats">
4230 /// If true, reset statistics after displaying their values.
4232 public void PrintMPoolSystemStats(bool PrintAll, bool ClearStats) {
4233 PrintMPoolSystemStats(PrintAll, ClearStats, false);
4236 /// Display the memory pool (that is, buffer cache) subsystem
4237 /// statistical information, as described by <see cref="MPoolStats"/>.
4239 /// <param name="PrintAll">
4240 /// If true, display all available information.
4242 /// <param name="ClearStats">
4243 /// If true, reset statistics after displaying their values.
4245 /// <param name="HashChains">
4246 /// If true, display the buffers with hash chains.
4248 public void PrintMPoolSystemStats(
4249 bool PrintAll, bool ClearStats, bool HashChains) {
4251 flags |= PrintAll ? DbConstants.DB_STAT_ALL : 0;
4252 flags |= ClearStats ? DbConstants.DB_STAT_CLEAR : 0;
4253 flags |= HashChains ? DbConstants.DB_STAT_MEMP_HASH : 0;
4255 dbenv.memp_stat_print(flags);
4259 /// Display the mutex subsystem statistical information, as described
4260 /// by <see cref="MutexStats"/>.
4262 public void PrintMutexSystemStats() {
4263 PrintMutexSystemStats(false, false);
4266 /// Display the mutex subsystem statistical information, as described
4267 /// by <see cref="MutexStats"/>.
4269 /// <param name="PrintAll">
4270 /// If true, display all available information.
4272 /// <param name="ClearStats">
4273 /// If true, reset statistics after displaying their values.
4275 public void PrintMutexSystemStats(bool PrintAll, bool ClearStats) {
4277 flags |= PrintAll ? DbConstants.DB_STAT_ALL : 0;
4278 flags |= ClearStats ? DbConstants.DB_STAT_CLEAR : 0;
4280 dbenv.mutex_stat_print(flags);
4284 /// Display the replication manager statistical information, as
4285 /// described by <see cref="RepMgrStats"/>.
4287 public void PrintRepMgrSystemStats() {
4288 PrintRepMgrSystemStats(false, false);
4291 /// Display the replication manager statistical information, as
4292 /// described by <see cref="RepMgrStats"/>.
4294 /// <param name="PrintAll">
4295 /// If true, display all available information.
4297 /// <param name="ClearStats">
4298 /// If true, reset statistics after displaying their values.
4300 public void PrintRepMgrSystemStats(bool PrintAll, bool ClearStats) {
4302 flags |= PrintAll ? DbConstants.DB_STAT_ALL : 0;
4303 flags |= ClearStats ? DbConstants.DB_STAT_CLEAR : 0;
4305 dbenv.repmgr_stat_print(flags);
4309 /// Display the replication subsystem statistical information, as
4310 /// described by <see cref="ReplicationStats"/>.
4312 public void PrintReplicationSystemStats() {
4313 PrintReplicationSystemStats(false, false);
4316 /// Display the replication subsystem statistical information, as
4317 /// described by <see cref="ReplicationStats"/>.
4319 /// <param name="PrintAll">
4320 /// If true, display all available information.
4322 /// <param name="ClearStats">
4323 /// If true, reset statistics after displaying their values.
4326 PrintReplicationSystemStats(bool PrintAll, bool ClearStats) {
4328 flags |= PrintAll ? DbConstants.DB_STAT_ALL : 0;
4329 flags |= ClearStats ? DbConstants.DB_STAT_CLEAR : 0;
4331 dbenv.rep_stat_print(flags);
4335 /// Display the locking subsystem statistical information, as described
4336 /// by <see cref="LockStats"/>.
4338 public void PrintStats() {
4339 PrintStats(false, false, false);
4342 /// Display the locking subsystem statistical information, as described
4343 /// by <see cref="LockStats"/>.
4345 /// <param name="PrintAll">
4346 /// If true, display all available information.
4348 /// <param name="ClearStats">
4349 /// If true, reset statistics after displaying their values.
4351 public void PrintStats(bool PrintAll, bool ClearStats) {
4352 PrintStats(PrintAll, ClearStats, false);
4355 /// Display the locking subsystem statistical information, as described
4356 /// by <see cref="LockStats"/>.
4358 public void PrintSubsystemStats() {
4359 PrintStats(false, false, true);
4362 /// Display the locking subsystem statistical information, as described
4363 /// by <see cref="LockStats"/>.
4365 /// <param name="PrintAll">
4366 /// If true, display all available information.
4368 /// <param name="ClearStats">
4369 /// If true, reset statistics after displaying their values.
4371 public void PrintSubsystemStats(bool PrintAll, bool ClearStats) {
4372 PrintStats(PrintAll, ClearStats, true);
4375 /// Display the locking subsystem statistical information, as described
4376 /// by <see cref="LockStats"/>.
4378 private void PrintStats(bool all, bool clear, bool subs) {
4380 flags |= all ? DbConstants.DB_STAT_ALL : 0;
4381 flags |= clear ? DbConstants.DB_STAT_CLEAR : 0;
4382 flags |= subs ? DbConstants.DB_STAT_SUBSYSTEM : 0;
4383 dbenv.stat_print(flags);
4387 /// Display the transaction subsystem statistical information, as
4388 /// described by <see cref="TransactionStats"/>.
4390 public void PrintTransactionSystemStats() {
4391 PrintTransactionSystemStats(false, false);
4394 /// Display the transaction subsystem statistical information, as
4395 /// described by <see cref="TransactionStats"/>.
4397 /// <param name="PrintAll">
4398 /// If true, display all available information.
4400 /// <param name="ClearStats">
4401 /// If true, reset statistics after displaying their values.
4404 PrintTransactionSystemStats(bool PrintAll, bool ClearStats) {
4406 flags |= PrintAll ? DbConstants.DB_STAT_ALL : 0;
4407 flags |= ClearStats ? DbConstants.DB_STAT_CLEAR : 0;
4409 dbenv.txn_stat_print(flags);
4411 #endregion Print Stats
4413 private uint getRepTimeout(int which) {
4415 dbenv.rep_get_timeout(which, ref ret);
4418 private bool getRepConfig(uint which) {
4420 dbenv.rep_get_config(which, ref onoff);
4421 return (onoff != 0);
4424 #region Unsupported Subsystem Methods
4426 /// The Berkeley DB process' environment may be permitted to specify
4427 /// information to be used when naming files; see Berkeley DB File
4428 /// Naming in the Programmer's Reference Guide for more information.
4430 public bool UseEnvironmentVars {
4433 dbenv.get_open_flags(ref flags);
4434 return (flags & DbConstants.DB_USE_ENVIRON) != 0;
4437 private bool USE_ENVIRON_ROOT {
4440 dbenv.get_open_flags(ref flags);
4441 return (flags & DbConstants.DB_USE_ENVIRON_ROOT) != 0;
4444 private uint CreateLockerID() {
4446 dbenv.lock_id(ref ret);
4449 private void FreeLockerID(uint locker) {
4450 dbenv.lock_id_free(locker);
4453 private Lock GetLock(
4454 uint locker, bool wait, DatabaseEntry obj, LockMode mode) {
4455 return new Lock(dbenv.lock_get(locker,
4456 wait ? 0 : DbConstants.DB_LOCK_NOWAIT,
4457 obj, LockMode.GetMode(mode)));
4460 private Mutex GetMutex(bool SelfBlock, bool SingleProcess) {
4463 flags |= SelfBlock ? DbConstants.DB_MUTEX_SELF_BLOCK : 0;
4464 flags |= SingleProcess ? DbConstants.DB_MUTEX_PROCESS_ONLY : 0;
4465 dbenv.mutex_alloc(flags, ref m);
4466 return new Mutex(this, m);
4469 private void LockMany(uint locker, bool wait, LockRequest[] vec) {
4470 LockMany(locker, wait, vec, null);
4472 private void LockMany(
4473 uint locker, bool wait, LockRequest[] vec, LockRequest failedReq) {
4474 IntPtr[] reqList = new IntPtr[vec.Length];
4475 DB_LOCKREQ[] lst = new DB_LOCKREQ[vec.Length];
4477 for (int i = 0; i < vec.Length; i++) {
4478 reqList[i] = DB_LOCKREQ.getCPtr(
4479 LockRequest.get_DB_LOCKREQ(vec[i])).Handle;
4480 lst[i] = LockRequest.get_DB_LOCKREQ(vec[i]);
4483 dbenv.lock_vec(locker, wait ? 0 : DbConstants.DB_TXN_NOWAIT,
4484 reqList, vec.Length, LockRequest.get_DB_LOCKREQ(failedReq));
4487 private void PutLock(Lock lck) {
4488 dbenv.lock_put(Lock.GetDB_LOCK(lck));