public double EstimatedQueryCost(Transaction transaction) { var endOfKeyRange = string.Concat(Key, new string(char.MaxValue, 1)); return (managedIndex.Index.KeyRange(new DatabaseEntry(managedIndex.KeyAsByteArray(endOfKeyRange)), transaction).Less - managedIndex.Index.KeyRange(new DatabaseEntry(managedIndex.KeyAsByteArray(Key)), transaction).Less) * managedIndex.Index.FastStats().nPages / pageSizeBufferMultipler * (double)QueryCostScale; }
public IEnumerable<InternalId> Execute(Transaction transaction) { var cursor = managedIndex.Index.Cursor(new CursorConfig(), transaction); try { var comparer = managedIndex.Comparer; var keyAsBytes = managedIndex.KeyAsByteArray(Key); var bufferSize = (int)managedIndex.Index.Pagesize * pageSizeBufferMultipler; if(cursor.MoveMultipleKey(new DatabaseEntry(keyAsBytes), false, bufferSize)) { do { //Each char is 2 bytes. Only take the start of the key data to the length of the query key. foreach(var internalId in cursor.CurrentMultipleKey .Select(_ => new {key = _.Key.Data.Take(Key.Length * 2).ToArray(), value = _.Value.Data.AsInternalId()}) .TakeWhile(pair => comparer.Compare(managedIndex.ByteArrayAsKey(pair.key), Key) == 0) .Select(_ => _.value)) { yield return internalId; } } while(cursor.MoveNextMultipleKey(bufferSize) && comparer.Compare(managedIndex.ByteArrayAsKey(cursor.CurrentMultipleKey.First().Key.Data), Key) == 0); } } finally { cursor.Close(); } }
public static void SetUpEnvWithTxnAndLocking(string envHome, out DatabaseEnvironment env, out Transaction txn, uint maxLock, uint maxLocker, uint maxObject, uint partition) { // Configure env and locking subsystem. LockingConfig lkConfig = new LockingConfig(); /* * If the maximum number of locks/lockers/objects * is given, then the LockingConfig is set. Unless, * it is not set to any value. */ if (maxLock != 0) lkConfig.MaxLocks = maxLock; if (maxLocker != 0) lkConfig.MaxLockers = maxLocker; if (maxObject != 0) lkConfig.MaxObjects = maxObject; if (partition != 0) lkConfig.Partitions = partition; DatabaseEnvironmentConfig envConfig = new DatabaseEnvironmentConfig(); envConfig.Create = true; envConfig.UseTxns = true; envConfig.UseMPool = true; envConfig.LockSystemCfg = lkConfig; envConfig.UseLocking = true; envConfig.NoLocking = false; env = DatabaseEnvironment.Open(envHome, envConfig); txn = env.BeginTransaction(); }
public IEnumerable<InternalId> Execute(Transaction transaction) { var cursor = managedIndex.ReverseIndex.Cursor(); while(cursor.MoveNextUnique()) { yield return cursor.Current.Key.Data.AsInternalId(); } }
public IEnumerable<InternalId> ExecuteInsideIntersect(Transaction transaction, IEnumerable<InternalId> joinConstraint) { if(joinConstraint.Count() > EstimatedQueryCost(transaction)) return Execute(transaction); var cursor = managedIndex.ReverseIndex.Cursor(); return joinConstraint .Where(joinMatched => cursor.Move(new DatabaseEntry(joinMatched.AsByteArray()), true)); }
/// <summary> /// Instantiate a new Sequence object. /// </summary> /// <remarks> /// If <paramref name="txn"/> is null and the operation occurs in a /// transactional database, the operation will be implicitly transaction /// protected. /// </remarks> /// <param name="cfg">Configuration parameters for the Sequence</param> /// <param name="txn"> /// If the operation is part of an application-specified transaction, /// <paramref name="txn"/> is a Transaction object returned from /// <see cref="DatabaseEnvironment.BeginTransaction"/>; if /// the operation is part of a Berkeley DB Concurrent Data Store group, /// <paramref name="txn"/> is a handle returned from /// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null. /// </param> public Sequence(SequenceConfig cfg, Transaction txn) { seq = new DB_SEQUENCE(cfg.BackingDatabase.db, 0); if (cfg.initialValIsSet) seq.initial_value(cfg.InitialValue); seq.set_flags(cfg.flags); if (cfg.rangeIsSet) seq.set_range(cfg.Min, cfg.Max); if (cfg.cacheSzIsSet) seq.set_cachesize(cfg.CacheSize); seq.open(Transaction.getDB_TXN(txn), cfg.key, cfg.openFlags); isOpen = true; }
public void OpenBtreeDBInEnv(string dbName, DatabaseEnvironment env, out BTreeDatabase db, bool create, Transaction txn) { BTreeDatabaseConfig btreeDBConfig = new BTreeDatabaseConfig(); btreeDBConfig.Env = env; if (create == true) btreeDBConfig.Creation = CreatePolicy.IF_NEEDED; else btreeDBConfig.Creation = CreatePolicy.NEVER; if (txn == null) db = BTreeDatabase.Open(dbName, btreeDBConfig); else db = BTreeDatabase.Open(dbName, btreeDBConfig, txn); }
/// <summary> /// Instantiate a new SecondaryDatabase object, open the database /// represented by <paramref name="Filename"/> and associate the /// database with the <see cref="SecondaryDatabaseConfig.Primary"> /// primary index</see>. The file specified by /// <paramref name="Filename"/> must exist. /// </summary> /// <remarks> /// <para> /// If <see cref="DatabaseConfig.AutoCommit"/> is set, the operation /// is implicitly transaction protected. Transactionally /// protected operations on a database object requires the object itself /// be transactionally protected during its open. /// </para> /// </remarks> /// <param name="Filename"> /// The name of an underlying file used to back the /// database. /// </param> /// <param name="cfg">The database's configuration</param> /// <param name="txn"> /// If the operation is part of an application-specified transaction, /// <paramref name="txn"/> is a Transaction object returned from /// <see cref="DatabaseEnvironment.BeginTransaction"/>; if /// the operation is part of a Berkeley DB Concurrent Data Store group, /// <paramref name="txn"/> is a handle returned from /// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null. /// </param> /// <returns>A new, open database object</returns> public static SecondaryDatabase Open(string Filename, SecondaryDatabaseConfig cfg, Transaction txn) { return(Open(Filename, null, cfg, txn)); }
/// <summary> /// Instantiate a new RecnoDatabase object and open the database /// represented by <paramref name="Filename"/>. /// </summary> /// <remarks> /// <para> /// If <paramref name="Filename"/> is null, the database is strictly /// temporary and cannot be opened by any other thread of control, thus /// the database can only be accessed by sharing the single database /// object that created it, in circumstances where doing so is safe. /// </para> /// <para> /// If <paramref name="txn"/> is null, but /// <see cref="DatabaseConfig.AutoCommit"/> is set, the operation will /// be implicitly transaction protected. Note that transactionally /// protected operations on a datbase object requires the object itself /// be transactionally protected during its open. Also note that the /// transaction must be committed before the object is closed. /// </para> /// </remarks> /// <param name="Filename"> /// The name of an underlying file that will be used to back the /// database. In-memory databases never intended to be preserved on disk /// may be created by setting this parameter to null. /// </param> /// <param name="cfg">The database's configuration</param> /// <param name="txn"> /// If the operation is part of an application-specified transaction, /// <paramref name="txn"/> is a Transaction object returned from /// <see cref="DatabaseEnvironment.BeginTransaction"/>; if /// the operation is part of a Berkeley DB Concurrent Data Store group, /// <paramref name="txn"/> is a handle returned from /// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null. /// </param> /// <returns>A new, open database object</returns> public static RecnoDatabase Open( string Filename, RecnoDatabaseConfig cfg, Transaction txn) { return(Open(Filename, null, cfg, txn)); }
/// <summary> /// Store the key/data pair in the database, replacing any previously /// existing key if duplicates are disallowed, or adding a duplicate /// data item if duplicates are allowed. /// </summary> /// <remarks> /// <para> /// When partial put to a duplicate database, a /// <see cref="DatabaseException"/> is thrown. /// </para> /// </remarks> /// <param name="key">The key to store in the database</param> /// <param name="data">The data item to store in the database</param> /// <param name="txn"> /// If the operation is part of an application-specified transaction, /// <paramref name="txn"/> is a Transaction object returned from /// <see cref="DatabaseEnvironment.BeginTransaction"/>; if /// the operation is part of a Berkeley DB Concurrent Data Store group, /// <paramref name="txn"/> is a handle returned from /// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null. /// </param> /// <exception cref="DatabaseException"> /// Partial put to a duplicate database, or <see cref="QueueDatabase"/> /// or <see cref="RecnoDatabase"/> with fixed-length records. /// </exception> public void Put( DatabaseEntry key, DatabaseEntry data, Transaction txn) { Put(key, data, txn, 0); }
/// <summary> /// Store the key/data pair in the database, only if the key does not /// already appear in the database. /// </summary> /// <remarks> /// This enforcement of uniqueness of keys applies only to the primary /// key, the behavior of insertions into secondary databases is not /// affected. In particular, the insertion of a record that would result /// in the creation of a duplicate key in a secondary database that /// allows duplicates would not be prevented by the use of this flag. /// </remarks> /// <param name="key">The key to store in the database</param> /// <param name="data">The data item to store in the database</param> /// <param name="txn"> /// If the operation is part of an application-specified transaction, /// <paramref name="txn"/> is a Transaction object returned from /// <see cref="DatabaseEnvironment.BeginTransaction"/>; if /// the operation is part of a Berkeley DB Concurrent Data Store group, /// <paramref name="txn"/> is a handle returned from /// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null. /// </param> public void PutNoOverwrite( DatabaseEntry key, DatabaseEntry data, Transaction txn) { Put(key, data, txn, DbConstants.DB_NOOVERWRITE); }
/// <summary> /// Return the database statistical information for this database. /// </summary> /// <param name="txn"> /// If the operation is part of an application-specified transaction, /// <paramref name="txn"/> is a Transaction object returned from /// <see cref="DatabaseEnvironment.BeginTransaction"/>; if /// the operation is part of a Berkeley DB Concurrent Data Store group, /// <paramref name="txn"/> is a handle returned from /// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null. /// </param> /// <returns>Database statistical information.</returns> public HeapStats Stats(Transaction txn) { return Stats(txn, false, Isolation.DEGREE_THREE); }
private HeapStats Stats(Transaction txn, bool fast, Isolation isoDegree) { uint flags = 0; flags |= fast ? DbConstants.DB_FAST_STAT : 0; switch (isoDegree) { case Isolation.DEGREE_ONE: flags |= DbConstants.DB_READ_UNCOMMITTED; break; case Isolation.DEGREE_TWO: flags |= DbConstants.DB_READ_COMMITTED; break; } HeapStatStruct st = db.stat_heap(Transaction.getDB_TXN(txn), flags); return new HeapStats(st); }
/// <summary> /// Return the database statistical information which does not require /// traversal of the database. /// </summary> /// <param name="txn"> /// If the operation is part of an application-specified transaction, /// <paramref name="txn"/> is a Transaction object returned from /// <see cref="DatabaseEnvironment.BeginTransaction"/>; if /// the operation is part of a Berkeley DB Concurrent Data Store group, /// <paramref name="txn"/> is a handle returned from /// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null. /// </param> /// <returns> /// The database statistical information which does not require /// traversal of the database. /// </returns> public RecnoStats FastStats(Transaction txn) { return(Stats(txn, true, Isolation.DEGREE_THREE)); }
/// <summary> /// Return the database statistical information which does not require /// traversal of the database. /// </summary> /// <param name="txn"> /// If the operation is part of an application-specified transaction, /// <paramref name="txn"/> is a Transaction object returned from /// <see cref="DatabaseEnvironment.BeginTransaction"/>; if /// the operation is part of a Berkeley DB Concurrent Data Store group, /// <paramref name="txn"/> is a handle returned from /// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null. /// </param> /// <returns> /// The database statistical information which does not require /// traversal of the database. /// </returns> public HeapStats FastStats(Transaction txn) { return Stats(txn, true, Isolation.DEGREE_THREE); }
/// <summary> /// Return the database statistical information for this database. /// </summary> /// <param name="txn"> /// If the operation is part of an application-specified transaction, /// <paramref name="txn"/> is a Transaction object returned from /// <see cref="DatabaseEnvironment.BeginTransaction"/>; if /// the operation is part of a Berkeley DB Concurrent Data Store group, /// <paramref name="txn"/> is a handle returned from /// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null. /// </param> /// <returns>Database statistical information.</returns> public RecnoStats Stats(Transaction txn) { return(Stats(txn, false, Isolation.DEGREE_THREE)); }
/// <summary> /// Return the database statistical information which does not require /// traversal of the database. /// </summary> /// <overloads> /// <para> /// Among other things, this method makes it possible for applications /// to request key and record counts without incurring the performance /// penalty of traversing the entire database. /// </para> /// <para> /// The statistical information is described by the /// <see cref="BTreeStats"/>, <see cref="HashStats"/>, /// <see cref="QueueStats"/>, and <see cref="RecnoStats"/> classes. /// </para> /// </overloads> /// <param name="txn"> /// If the operation is part of an application-specified transaction, /// <paramref name="txn"/> is a Transaction object returned from /// <see cref="DatabaseEnvironment.BeginTransaction"/>; if /// the operation is part of a Berkeley DB Concurrent Data Store group, /// <paramref name="txn"/> is a handle returned from /// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null. /// </param> /// <param name="isoDegree"> /// The level of isolation for database reads. /// <see cref="Isolation.DEGREE_ONE"/> will be silently ignored for /// databases which did not specify /// <see cref="DatabaseConfig.ReadUncommitted"/>. /// </param> /// <returns> /// The database statistical information which does not require /// traversal of the database. /// </returns> public RecnoStats FastStats(Transaction txn, Isolation isoDegree) { return(Stats(txn, true, isoDegree)); }
/// <summary> /// Create a transactionally protected secondary database cursor with /// the given configuration. /// </summary> /// <param name="cfg"> /// The configuration properties for the cursor. /// </param> /// <param name="txn"> /// The transaction context in which the cursor may be used. /// </param> /// <returns>A newly created cursor</returns> public SecondaryCursor SecondaryCursor( CursorConfig cfg, Transaction txn) { return(new SecondaryCursor( db.cursor(Transaction.getDB_TXN(txn), cfg.flags))); }
/// <summary> /// Create a transactionally protected secondary database cursor. /// </summary> /// <param name="txn"> /// The transaction context in which the cursor may be used. /// </param> /// <returns>A newly created cursor</returns> public SecondaryCursor SecondaryCursor(Transaction txn) { return(SecondaryCursor(new CursorConfig(), txn)); }
/// <summary> /// Instantiate a new SecondaryDatabase object, open the database /// represented by <paramref name="Filename"/> and associate the /// database with the <see cref="SecondaryDatabaseConfig.Primary"> /// primary index</see>. The file specified by /// <paramref name="Filename"/> must exist. /// </summary> /// <remarks> /// <para> /// If <paramref name="Filename"/> is null and /// <paramref name="DatabaseName"/> is non-null, the database can be /// opened by other threads of control and will be replicated to client /// sites in any replication group. /// </para> /// <para> /// If <see cref="DatabaseConfig.AutoCommit"/> is set, the operation /// is implicitly transaction protected. Transactionally /// protected operations on a database object requires the object itself /// be transactionally protected during its open. /// </para> /// </remarks> /// <param name="Filename"> /// The name of an underlying file used to back the /// database. /// </param> /// <param name="DatabaseName"> /// This parameter allows applications to have multiple databases in a /// single file. Although no DatabaseName needs to be specified, it is /// an error to attempt to open a second database in a file that was not /// initially created using a database name. /// </param> /// <param name="cfg">The database's configuration</param> /// <param name="txn"> /// If the operation is part of an application-specified transaction, /// <paramref name="txn"/> is a Transaction object returned from /// <see cref="DatabaseEnvironment.BeginTransaction"/>; if /// the operation is part of a Berkeley DB Concurrent Data Store group, /// <paramref name="txn"/> is a handle returned from /// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null. /// </param> /// <returns>A new, open database object</returns> public static SecondaryDatabase Open(string Filename, string DatabaseName, SecondaryDatabaseConfig cfg, Transaction txn) { if (cfg.DbType == DatabaseType.BTREE) { return(SecondaryBTreeDatabase.Open(Filename, DatabaseName, (SecondaryBTreeDatabaseConfig)cfg, txn)); } else if (cfg.DbType == DatabaseType.HASH) { return(SecondaryHashDatabase.Open(Filename, DatabaseName, (SecondaryHashDatabaseConfig)cfg, txn)); } SecondaryDatabase ret = new SecondaryDatabase(cfg.Env, 0); ret.Config(cfg); ret.db.open(Transaction.getDB_TXN(txn), Filename, DatabaseName, cfg.DbType.getDBTYPE(), cfg.openFlags, 0); ret.doAssocRef = new BDB_AssociateDelegate(doAssociate); cfg.Primary.db.associate(Transaction.getDB_TXN(txn), ret.db, ret.doAssocRef, cfg.assocFlags); if (cfg.ForeignKeyDatabase != null) { if (cfg.OnForeignKeyDelete == ForeignKeyDeleteAction.NULLIFY) { ret.doNullifyRef = new BDB_AssociateForeignDelegate(doNullify); } else { ret.doNullifyRef = null; } cfg.ForeignKeyDatabase.db.associate_foreign(ret.db, ret.doNullifyRef, cfg.foreignFlags); } return(ret); }
/// <summary> /// Instantiate a new HeapDatabase object and open the database /// represented by <paramref name="Filename"/>. /// </summary> /// <remarks> /// <para> /// If <paramref name="Filename"/> is null, the database is strictly /// temporary and cannot be opened by any other thread of control, thus /// the database can only be accessed by sharing the single database /// object that created it, in circumstances where doing so is safe. /// </para> /// <para> /// If <paramref name="txn"/> is null, but /// <see cref="DatabaseConfig.AutoCommit"/> is set, the operation will /// be implicitly transaction protected. Note that transactionally /// protected operations on a datbase object requires the object itself /// be transactionally protected during its open. Also note that the /// transaction must be committed before the object is closed. /// </para> /// </remarks> /// <param name="Filename"> /// The name of an underlying file that will be used to back the /// database. In-memory databases never intended to be preserved on disk /// may be created by setting this parameter to null. /// </param> /// <param name="cfg">The database's configuration</param> /// <param name="txn"> /// If the operation is part of an application-specified transaction, /// <paramref name="txn"/> is a Transaction object returned from /// <see cref="DatabaseEnvironment.BeginTransaction"/>; if /// the operation is part of a Berkeley DB Concurrent Data Store group, /// <paramref name="txn"/> is a handle returned from /// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null. /// </param> /// <returns>A new, open database object</returns> public static HeapDatabase Open( string Filename, HeapDatabaseConfig cfg, Transaction txn) { HeapDatabase ret = new HeapDatabase(cfg.Env, 0); ret.Config(cfg); ret.db.open(Transaction.getDB_TXN(txn), Filename, null, DBTYPE.DB_HEAP, cfg.openFlags, 0); ret.isOpen = true; return ret; }
/// <summary> /// Create a transactionally protected database cursor. /// </summary> /// <param name="txn"> /// The transaction context in which the cursor may be used. /// </param> /// <returns>A newly created cursor</returns> public new RecnoCursor Cursor(Transaction txn) { return(Cursor(new CursorConfig(), txn)); }
/// <summary> /// Append the data item to the end of the database. /// </summary> /// <param name="data">The data item to store in the database</param> /// <param name="txn"> /// If the operation is part of an application-specified transaction, /// <paramref name="txn"/> is a Transaction object returned from /// <see cref="DatabaseEnvironment.BeginTransaction"/>; if /// the operation is part of a Berkeley DB Concurrent Data Store group, /// <paramref name="txn"/> is a handle returned from /// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null. /// </param> /// <returns>The heap record id allocated to the record</returns> public HeapRecordId Append(DatabaseEntry data, Transaction txn) { DatabaseEntry key = new DatabaseEntry(); Put(key, data, txn, DbConstants.DB_APPEND); return HeapRecordId.fromArray(key.Data); }
/// <summary> /// Instantiate a new RecnoDatabase object and open the database /// represented by <paramref name="Filename"/> and /// <paramref name="DatabaseName"/>. /// </summary> /// <remarks> /// <para> /// If both <paramref name="Filename"/> and /// <paramref name="DatabaseName"/> are null, the database is strictly /// temporary and cannot be opened by any other thread of control, thus /// the database can only be accessed by sharing the single database /// object that created it, in circumstances where doing so is safe. If /// <paramref name="Filename"/> is null and /// <paramref name="DatabaseName"/> is non-null, the database can be /// opened by other threads of control and will be replicated to client /// sites in any replication group. /// </para> /// <para> /// If <paramref name="txn"/> is null, but /// <see cref="DatabaseConfig.AutoCommit"/> is set, the operation will /// be implicitly transaction protected. Note that transactionally /// protected operations on a datbase object requires the object itself /// be transactionally protected during its open. Also note that the /// transaction must be committed before the object is closed. /// </para> /// </remarks> /// <param name="Filename"> /// The name of an underlying file that will be used to back the /// database. In-memory databases never intended to be preserved on disk /// may be created by setting this parameter to null. /// </param> /// <param name="DatabaseName"> /// This parameter allows applications to have multiple databases in a /// single file. Although no DatabaseName needs to be specified, it is /// an error to attempt to open a second database in a file that was not /// initially created using a database name. /// </param> /// <param name="cfg">The database's configuration</param> /// <param name="txn"> /// If the operation is part of an application-specified transaction, /// <paramref name="txn"/> is a Transaction object returned from /// <see cref="DatabaseEnvironment.BeginTransaction"/>; if /// the operation is part of a Berkeley DB Concurrent Data Store group, /// <paramref name="txn"/> is a handle returned from /// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null. /// </param> /// <returns>A new, open database object</returns> public static RecnoDatabase Open(string Filename, string DatabaseName, RecnoDatabaseConfig cfg, Transaction txn) { RecnoDatabase ret = new RecnoDatabase(cfg.Env, 0); ret.Config(cfg); ret.db.open(Transaction.getDB_TXN(txn), Filename, DatabaseName, DBTYPE.DB_RECNO, cfg.openFlags, 0); ret.isOpen = true; return(ret); }
/// <summary> /// Return the database statistical information which does not require /// traversal of the database. /// </summary> /// <overloads> /// <para> /// Among other things, this method makes it possible for applications /// to request key and record counts without incurring the performance /// penalty of traversing the entire database. /// </para> /// <para> /// The statistical information is described by the /// <see cref="BTreeStats"/>, <see cref="HashStats"/>, /// <see cref="HeapStats"/>, <see cref="QueueStats"/>, and /// <see cref="RecnoStats"/> classes. /// </para> /// </overloads> /// <param name="txn"> /// If the operation is part of an application-specified transaction, /// <paramref name="txn"/> is a Transaction object returned from /// <see cref="DatabaseEnvironment.BeginTransaction"/>; if /// the operation is part of a Berkeley DB Concurrent Data Store group, /// <paramref name="txn"/> is a handle returned from /// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null. /// </param> /// <param name="isoDegree"> /// The level of isolation for database reads. /// <see cref="Isolation.DEGREE_ONE"/> will be silently ignored for /// databases which did not specify /// <see cref="DatabaseConfig.ReadUncommitted"/>. /// </param> /// <returns> /// The database statistical information which does not require /// traversal of the database. /// </returns> public HeapStats FastStats(Transaction txn, Isolation isoDegree) { return Stats(txn, true, isoDegree); }
/// <summary> /// Instantiate a new Database object and open the database represented /// by <paramref name="Filename"/> and <paramref name="DatabaseName"/>. /// The file specified by <paramref name="Filename"/> must exist. /// </summary> /// <remarks> /// <para> /// If both <paramref name="Filename"/> and /// <paramref name="DatabaseName"/> are null, the database is strictly /// temporary and cannot be opened by any other thread of control, thus /// the database can only be accessed by sharing the single database /// object that created it, in circumstances where doing so is safe. If /// <paramref name="Filename"/> is null and /// <paramref name="DatabaseName"/> is non-null, the database can be /// opened by other threads of control and be replicated to client /// sites in any replication group. /// </para> /// <para> /// If <paramref name="txn"/> is null, but /// <see cref="DatabaseConfig.AutoCommit"/> is set, the operation /// is implicitly transaction protected. Transactionally /// protected operations on a database object requires the object itself /// be transactionally protected during its open. The /// transaction must be committed before the object is closed. /// </para> /// </remarks> /// <param name="Filename"> /// The name of an underlying file used to back the /// database. In-memory databases never intended to be preserved on disk /// may be created by setting this parameter to null. /// </param> /// <param name="DatabaseName"> /// This parameter allows applications to have multiple databases in a /// single file. Although no DatabaseName needs to be specified, it is /// an error to attempt to open a second database in a file that was not /// initially created using a database name. /// </param> /// <param name="cfg">The database's configuration</param> /// <param name="txn"> /// If the operation is part of an application-specified transaction, /// <paramref name="txn"/> is a Transaction object returned from /// <see cref="DatabaseEnvironment.BeginTransaction"/>; if /// the operation is part of a Berkeley DB Concurrent Data Store group, /// <paramref name="txn"/> is a handle returned from /// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null. /// </param> /// <returns>A new, open database object</returns> public static new Database Open(string Filename, string DatabaseName, DatabaseConfig cfg, Transaction txn) { Database ret; BaseDatabase db = BaseDatabase.Open( Filename, DatabaseName, cfg, txn); switch (db.Type.getDBTYPE()) { case DBTYPE.DB_BTREE: ret = new BTreeDatabase(db); break; case DBTYPE.DB_HASH: ret = new HashDatabase(db); break; case DBTYPE.DB_HEAP: ret = new HeapDatabase(db); break; case DBTYPE.DB_QUEUE: ret = new QueueDatabase(db); break; case DBTYPE.DB_RECNO: ret = new RecnoDatabase(db); break; default: throw new DatabaseException(0); } db.Dispose(); ret.isOpen = true; return ret; }
/// <summary> /// Return the database statistical information for this database. /// </summary> /// <overloads> /// The statistical information is described by /// <see cref="HeapStats"/>. /// </overloads> /// <param name="txn"> /// If the operation is part of an application-specified transaction, /// <paramref name="txn"/> is a Transaction object returned from /// <see cref="DatabaseEnvironment.BeginTransaction"/>; if /// the operation is part of a Berkeley DB Concurrent Data Store group, /// <paramref name="txn"/> is a handle returned from /// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null. /// </param> /// <param name="isoDegree"> /// The level of isolation for database reads. /// <see cref="Isolation.DEGREE_ONE"/> will be silently ignored for /// databases which did not specify /// <see cref="DatabaseConfig.ReadUncommitted"/>. /// </param> /// <returns>Database statistical information.</returns> public HeapStats Stats(Transaction txn, Isolation isoDegree) { return Stats(txn, false, isoDegree); }
/// <summary> /// Retrieve a key and all duplicate data items from the database. /// </summary> /// <param name="key">The key to search for</param> /// <param name="BufferSize"> /// The initial size of the buffer to fill with duplicate data items. If /// the buffer is not large enough, it is automatically resized. /// </param> /// <param name="txn"> /// <paramref name="txn"/> is a Transaction object returned from /// <see cref="DatabaseEnvironment.BeginTransaction"/>; if /// the operation is part of a Berkeley DB Concurrent Data Store group, /// <paramref name="txn"/> is a handle returned from /// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null. /// </param> /// <returns> /// A <see cref="KeyValuePair{T,T}"/> /// whose Key parameter is <paramref name="key"/> and whose Value /// parameter is the retrieved data items. /// </returns> public KeyValuePair<DatabaseEntry, MultipleDatabaseEntry> GetMultiple( DatabaseEntry key, int BufferSize, Transaction txn) { return GetMultiple(key, BufferSize, txn, null); }
/// <summary> /// Store multiple data items using keys from the buffer to which the /// key parameter refers and data values from the buffer to which the /// data parameter refers. A successful bulk operation is logically /// equivalent to a loop through each key/data pair, performing a Put /// for each one. /// </summary> /// <param name="key">Multiple key/data pairs to store in the database /// </param> /// <param name="txn"> /// If the operation is part of an application-specified transaction, /// <paramref name="txn"/> is a Transaction object returned from /// <see cref="DatabaseEnvironment.BeginTransaction"/>; if /// the operation is part of a Berkeley DB Concurrent Data Store group, /// <paramref name="txn"/> is a handle returned from /// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null. /// </param> public void Put(MultipleKeyDatabaseEntry key, Transaction txn) { Put(key, null, txn, 0); }
/// <summary> /// Instantiate a new SecondaryDatabase object, open the database /// represented by <paramref name="Filename"/> and associate the /// database with the <see cref="SecondaryDatabaseConfig.Primary"> /// primary index</see>. The file specified by /// <paramref name="Filename"/> must exist. /// </summary> /// <remarks> /// <para> /// If <see cref="DatabaseConfig.AutoCommit"/> is set, the operation /// is implicitly transaction protected. Transactionally /// protected operations on a database object requires the object itself /// be transactionally protected during its open. /// </para> /// </remarks> /// <param name="Filename"> /// The name of an underlying file used to back the /// database. /// </param> /// <param name="cfg">The database's configuration</param> /// <param name="txn"> /// If the operation is part of an application-specified transaction, /// <paramref name="txn"/> is a Transaction object returned from /// <see cref="DatabaseEnvironment.BeginTransaction"/>; if /// the operation is part of a Berkeley DB Concurrent Data Store group, /// <paramref name="txn"/> is a handle returned from /// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null. /// </param> /// <returns>A new, open database object</returns> public static SecondaryDatabase Open(string Filename, SecondaryDatabaseConfig cfg, Transaction txn) { return Open(Filename, null, cfg, txn); }
/// <summary> /// Store the key/data pairs in the database, only if the key does not /// already appear in the database. /// </summary> /// <param name="key">Key/data pairs to store in the database</param> /// <param name="txn"> /// If the operation is part of an application-specified transaction, /// <paramref name="txn"/> is a Transaction object returned from /// <see cref="DatabaseEnvironment.BeginTransaction"/>; if /// the operation is part of a Berkeley DB Concurrent Data Store group, /// <paramref name="txn"/> is a handle returned from /// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null. /// </param> public void PutNoOverwrite( MultipleKeyDatabaseEntry key, Transaction txn) { Put(key, null, txn, DbConstants.DB_NOOVERWRITE); }
/// <summary> /// Create a transactionally protected secondary database cursor. /// </summary> /// <param name="txn"> /// The transaction context in which the cursor may be used. /// </param> /// <returns>A newly created cursor</returns> public SecondaryCursor SecondaryCursor(Transaction txn) { return SecondaryCursor(new CursorConfig(), txn); }
/// <summary> /// Protected wrapper for DB->put. Used by subclasses for access method /// specific operations. /// </summary> /// <param name="key">The key to store in the database</param> /// <param name="data">The data item to store in the database</param> /// <param name="txn">Transaction with which to protect the put</param> /// <param name="flags">Flags to pass to DB->put</param> protected void Put(DatabaseEntry key, DatabaseEntry data, Transaction txn, uint flags) { System.Type type = key.GetType(); if (type == typeof(MultipleDatabaseEntry)) flags |= DbConstants.DB_MULTIPLE; else if (type == typeof(MultipleKeyDatabaseEntry)) flags |= DbConstants.DB_MULTIPLE_KEY; db.put(Transaction.getDB_TXN(txn), key, data, flags); }
/// <summary> /// Return the database statistical information for this database. /// </summary> /// <overloads> /// The statistical information is described by /// <see cref="BTreeStats"/>. /// </overloads> /// <param name="txn"> /// If the operation is part of an application-specified transaction, /// <paramref name="txn"/> is a Transaction object returned from /// <see cref="DatabaseEnvironment.BeginTransaction"/>; if /// the operation is part of a Berkeley DB Concurrent Data Store group, /// <paramref name="txn"/> is a handle returned from /// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null. /// </param> /// <param name="isoDegree"> /// The level of isolation for database reads. /// <see cref="Isolation.DEGREE_ONE"/> will be silently ignored for /// databases which did not specify /// <see cref="DatabaseConfig.ReadUncommitted"/>. /// </param> /// <returns>Database statistical information.</returns> public RecnoStats Stats(Transaction txn, Isolation isoDegree) { return(Stats(txn, false, isoDegree)); }
/// <summary> /// If a key/data pair in the database matches <paramref name="key"/> /// and <paramref name="data"/>, return the key and all duplicate data /// items. /// </summary> /// <param name="key">The key to search for</param> /// <param name="data">The data to search for</param> /// <param name="BufferSize"> /// The initial size of the buffer to fill with duplicate data items. If /// the buffer is not large enough, it is automatically resized. /// </param> /// <param name="txn"> /// <paramref name="txn"/> is a Transaction object returned from /// <see cref="DatabaseEnvironment.BeginTransaction"/>; if /// the operation is part of a Berkeley DB Concurrent Data Store group, /// <paramref name="txn"/> is a handle returned from /// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null. /// </param> /// <param name="info">The locking behavior to use.</param> /// <exception cref="NotFoundException"> /// A NotFoundException is thrown if <paramref name="key"/> and /// <paramref name="data"/> are not in the database. /// </exception> /// <exception cref="KeyEmptyException"> /// A KeyEmptyException is thrown if the database is a /// <see cref="QueueDatabase"/> or <see cref="RecnoDatabase"/> /// database and <paramref name="key"/> exists, but was never explicitly /// created by the application or was later deleted. /// </exception> /// <returns> /// A <see cref="KeyValuePair{T,T}"/> /// whose Key parameter is <paramref name="key"/> and whose Value /// parameter is the retrieved data items. /// </returns> public KeyValuePair<DatabaseEntry, MultipleDatabaseEntry> GetBothMultiple( DatabaseEntry key, DatabaseEntry data, int BufferSize, Transaction txn, LockingInfo info) { KeyValuePair<DatabaseEntry, DatabaseEntry> kvp; int datasz = (int)data.Data.Length; for (; ; ) { byte[] udata = new byte[BufferSize]; Array.Copy(data.Data, udata, datasz); data.UserData = udata; data.size = (uint)datasz; try { kvp = Get(key, data, txn, info, DbConstants.DB_MULTIPLE | DbConstants.DB_GET_BOTH); break; } catch (MemoryException) { int sz = (int)data.size; if (sz > BufferSize) BufferSize = sz; else BufferSize *= 2; } } MultipleDatabaseEntry dbe = new MultipleDatabaseEntry(kvp.Value); return new KeyValuePair<DatabaseEntry, MultipleDatabaseEntry>( kvp.Key, dbe); }
public void UpdateTxn() { int int32Value; DatabaseEntry data; // Get a new transaction for updating the db. TransactionConfig txnConfig = new TransactionConfig(); txnConfig.IsolationDegree = Isolation.DEGREE_THREE; updateTxn = paramEnv.BeginTransaction(txnConfig); // Continually putting record to db. BTreeCursor cursor = paramDB.Cursor(updateTxn); // Move the cursor to the first record. Assert.IsTrue(cursor.MoveFirst()); int i = 0; try { do { int32Value = BitConverter.ToInt32( cursor.Current.Value.Data, 0); data = new DatabaseEntry( BitConverter.GetBytes(int32Value - 1)); cursor.Overwrite(data); } while (i <= 1000 && cursor.MoveNext()); } catch (DeadlockException) { } finally { cursor.Close(); } }
/// <summary> /// Retrieve a key and all duplicate data items from the database. /// </summary> /// <param name="key">The key to search for</param> /// <param name="BufferSize"> /// The initial size of the buffer to fill with duplicate data items. If /// the buffer is not large enough, it is automatically resized. /// </param> /// <param name="txn"> /// <paramref name="txn"/> is a Transaction object returned from /// <see cref="DatabaseEnvironment.BeginTransaction"/>; if /// the operation is part of a Berkeley DB Concurrent Data Store group, /// <paramref name="txn"/> is a handle returned from /// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null. /// </param> /// <param name="info">The locking behavior to use.</param> /// <returns> /// A <see cref="KeyValuePair{T,T}"/> /// whose Key parameter is <paramref name="key"/> and whose Value /// parameter is the retrieved data items. /// </returns> public KeyValuePair<DatabaseEntry, MultipleDatabaseEntry> GetMultiple( DatabaseEntry key, int BufferSize, Transaction txn, LockingInfo info) { KeyValuePair<DatabaseEntry, DatabaseEntry> kvp; DatabaseEntry data = new DatabaseEntry(); for (; ; ) { data.UserData = new byte[BufferSize]; try { kvp = Get(key, data, txn, info, DbConstants.DB_MULTIPLE); break; } catch (MemoryException) { int sz = (int)data.size; if (sz > BufferSize) BufferSize = sz; else BufferSize *= 2; } } MultipleDatabaseEntry dbe = new MultipleDatabaseEntry(kvp.Value); return new KeyValuePair<DatabaseEntry, MultipleDatabaseEntry>( kvp.Key, dbe); }
/* * Configure a transactional cursor to have degree 1 * isolation. The cursor's read operations could return * modified but not yet commited data. */ public void CursorReadUncommited( DatabaseEnvironment env, BTreeDatabase db, Cursor cursor, Transaction txn) { Console.WriteLine("CursorReadUncommited"); }
/// <summary> /// Instantiate a new SecondaryDatabase object, open the database /// represented by <paramref name="Filename"/> and associate the /// database with the <see cref="SecondaryDatabaseConfig.Primary"> /// primary index</see>. The file specified by /// <paramref name="Filename"/> must exist. /// </summary> /// <remarks> /// <para> /// If <paramref name="Filename"/> is null and /// <paramref name="DatabaseName"/> is non-null, the database can be /// opened by other threads of control and will be replicated to client /// sites in any replication group. /// </para> /// <para> /// If <see cref="DatabaseConfig.AutoCommit"/> is set, the operation /// is implicitly transaction protected. Transactionally /// protected operations on a database object requires the object itself /// be transactionally protected during its open. /// </para> /// </remarks> /// <param name="Filename"> /// The name of an underlying file used to back the /// database. /// </param> /// <param name="DatabaseName"> /// This parameter allows applications to have multiple databases in a /// single file. Although no DatabaseName needs to be specified, it is /// an error to attempt to open a second database in a file that was not /// initially created using a database name. /// </param> /// <param name="cfg">The database's configuration</param> /// <param name="txn"> /// If the operation is part of an application-specified transaction, /// <paramref name="txn"/> is a Transaction object returned from /// <see cref="DatabaseEnvironment.BeginTransaction"/>; if /// the operation is part of a Berkeley DB Concurrent Data Store group, /// <paramref name="txn"/> is a handle returned from /// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null. /// </param> /// <returns>A new, open database object</returns> public static SecondaryDatabase Open(string Filename, string DatabaseName, SecondaryDatabaseConfig cfg, Transaction txn) { if (cfg.DbType == DatabaseType.BTREE) { return SecondaryBTreeDatabase.Open(Filename, DatabaseName, (SecondaryBTreeDatabaseConfig)cfg, txn); } else if (cfg.DbType == DatabaseType.HASH) { return SecondaryHashDatabase.Open(Filename, DatabaseName, (SecondaryHashDatabaseConfig)cfg, txn); } SecondaryDatabase ret = new SecondaryDatabase(cfg.Env, 0); ret.Config(cfg); ret.db.open(Transaction.getDB_TXN(txn), Filename, DatabaseName, cfg.DbType.getDBTYPE(), cfg.openFlags, 0); ret.doAssocRef = new BDB_AssociateDelegate(doAssociate); cfg.Primary.db.associate(Transaction.getDB_TXN(txn), ret.db, ret.doAssocRef, cfg.assocFlags); if (cfg.ForeignKeyDatabase != null) { if (cfg.OnForeignKeyDelete == ForeignKeyDeleteAction.NULLIFY) ret.doNullifyRef = new BDB_AssociateForeignDelegate(doNullify); else ret.doNullifyRef = null; cfg.ForeignKeyDatabase.db.associate_foreign(ret.db, ret.doNullifyRef, cfg.foreignFlags); } return ret; }
public void ReadTxn() { // Get a new transaction for reading the db. TransactionConfig txnConfig = new TransactionConfig(); txnConfig.Snapshot = true; readTxn = paramEnv.BeginTransaction( txnConfig); // Get a new cursor for putting record into db. CursorConfig cursorConfig = new CursorConfig(); cursorConfig.WriteCursor = false; BTreeCursor cursor = paramDB.Cursor( cursorConfig, readTxn); // Continually reading record from db. try { Assert.IsTrue(cursor.MoveFirst()); int i = 0; do { Assert.AreEqual( BitConverter.ToInt32( cursor.Current.Key.Data, 0), BitConverter.ToInt32( cursor.Current.Value.Data, 0)); } while (i <= 1000 && cursor.MoveNext()); } catch (DeadlockException) { } finally { cursor.Close(); } }
/// <summary> /// Create a transactionally protected secondary database cursor with /// the given configuration. /// </summary> /// <param name="cfg"> /// The configuration properties for the cursor. /// </param> /// <param name="txn"> /// The transaction context in which the cursor may be used. /// </param> /// <returns>A newly created cursor</returns> public SecondaryCursor SecondaryCursor( CursorConfig cfg, Transaction txn) { return new SecondaryCursor( db.cursor(Transaction.getDB_TXN(txn), cfg.flags)); }
internal PreparedTransaction(DB_PREPLIST prep) { trans = new Transaction(prep.txn); txnid = prep.gid; }