示例#1
0
        /// <summary>
        /// Position the cursor at a specific key/data pair in the database, and
        /// store the key/data pair in <see cref="Cursor.Current"/>.
        /// </summary>
        /// <param name="recno">
        /// The specific numbered record of the database at which to position
        /// the cursor.
        /// </param>
        /// <param name="info">The locking behavior to use</param>
        /// <returns>
        /// True if the cursor was positioned successfully, false otherwise.
        /// </returns>
        public bool Move(uint recno, LockingInfo info) {
            DatabaseEntry key = new DatabaseEntry();
            key.Data = BitConverter.GetBytes(recno);
            DatabaseEntry data = new DatabaseEntry();

            return base.Get(key, data, DbConstants.DB_SET_RECNO, info);
        }
        public DatabaseEntry SecondaryKeyGen(
		    DatabaseEntry key, DatabaseEntry data)
        {
            DatabaseEntry dbtGen;
            dbtGen = new DatabaseEntry(data.Data);
            return dbtGen;
        }
示例#3
0
        public void GetSecCursor(BTreeDatabase db,
		    string secFileName, SecondaryKeyGenDelegate keyGen,
		    out SecondaryBTreeDatabase secDB,
		    out SecondaryCursor cursor, bool ifCfg,
		    DatabaseEntry data)
        {
            // Open secondary database.
            SecondaryBTreeDatabaseConfig secCfg =
                new SecondaryBTreeDatabaseConfig(db, keyGen);
            secCfg.Creation = CreatePolicy.IF_NEEDED;
            secCfg.Duplicates = DuplicatesPolicy.SORTED;
            secDB = SecondaryBTreeDatabase.Open(secFileName, secCfg);

            int[] intArray = new int[4];
            intArray[0] = 0;
            intArray[1] = 1;
            intArray[2] = 2049;
            intArray[3] = 65537;
            for (int i = 0; i < 4; i++)
            {
                DatabaseEntry record = new DatabaseEntry(
                    BitConverter.GetBytes(intArray[i]));
                db.Put(record, record);
            }

            // Get secondary cursor on the secondary database.
            if (ifCfg == false)
                cursor = secDB.SecondaryCursor();
            else
                cursor = secDB.SecondaryCursor(new CursorConfig());

            // Position the cursor.
            if (data != null)
                Assert.IsTrue(cursor.Move(data, true));
        }
示例#4
0
 /// <summary>
 /// Send a message on the message channel. The message is sent 
 /// asynchronously. The method does not wait for a response before
 /// returning. It usually completes quickly because it only waits for
 /// local TCP implementation to accept the bytes into its network data 
 /// buffer. However, this message could block briefly for longer
 /// messages, and/or if the network data buffer is nearly full. 
 /// It could even block indefinitely if the remote site is slow
 /// to read.
 /// </summary>
 /// <remarks>
 /// <para>
 /// To block while waiting for a response from a remote site, use 
 /// <see cref="SendRequest"/> instead of this method.
 /// </para>
 /// <para>
 /// The sent message is received and handled at remote sites using a
 /// message dispatch callback, which is configured using
 /// <see cref="DatabaseEnvironment.RepMessageDispatch"/>. This
 /// method may be used within the message dispatch callback on the
 /// remote site to send a reply or acknowledgement for messages that it
 /// receives and is handling.
 /// </para>
 /// <para>
 /// This method may be used on channels opened to any destination. See
 /// <see cref="DatabaseEnvironment.RepMgrChannel"/> for a list of 
 /// potential destinations.
 /// </para>
 /// </remarks>
 /// <param name="msg">
 /// An array of DatabaseEntry objects. Any flags for the DatabaseEntry
 /// objects are ignored.
 /// </param>
 public void SendMessage(DatabaseEntry[] msg) {
     int size = msg.Length;
     IntPtr[] dbts = new IntPtr[size];
     for (int i = 0; i < size; i++)
         dbts[i] = DBT.getCPtr(DatabaseEntry.getDBT(msg[i])).Handle;
     channel.send_msg(dbts, (uint)size, 0); 
 }
示例#5
0
        public void CheckPartial(
		    DatabaseEntry key, uint kOffset, uint kLen,
		    DatabaseEntry pkey, uint pkOffset, uint pkLen,
		    DatabaseEntry data, uint dOffset, uint dLen)
        {
            if (key != null) {
                Assert.IsTrue(key.Partial);
                Assert.AreEqual(kOffset, key.PartialOffset);
                Assert.AreEqual(kLen, key.PartialLen);
                Assert.AreEqual(kLen, (uint)key.Data.Length);
            }

            if (pkey != null) {
                Assert.IsTrue(pkey.Partial);
                Assert.AreEqual(pkOffset, pkey.PartialOffset);
                Assert.AreEqual(pkLen, pkey.PartialLen);
                Assert.AreEqual(pkLen, (uint)pkey.Data.Length);
            }

            if (data != null) {
                Assert.IsTrue(data.Partial);
                Assert.AreEqual(dOffset, data.PartialOffset);
                Assert.AreEqual(dLen, data.PartialLen);
                Assert.AreEqual(dLen, (uint)data.Data.Length);
            }
        }
示例#6
0
        public void Set(DatabaseEntry key, DatabaseEntry value, bool flush = false)
        {
            _btreeDb.Put(key, value);

            if (flush)
                _btreeDb.Sync();
        }
示例#7
0
        public DatabaseEntry KeyGenOnBigByte(
		    DatabaseEntry key, DatabaseEntry data)
        {
            byte[] byteArr = new byte[1];
            byteArr[0] = data.Data[data.Data.Length - 1];
            DatabaseEntry dbtGen = new DatabaseEntry(byteArr);
            return dbtGen;
        }
示例#8
0
        public DatabaseEntry KeyGenOnLittleByte(
		    DatabaseEntry key, DatabaseEntry data)
        {
            byte[] byteArr = new byte[1];
            byteArr[0] = data.Data[0];
            DatabaseEntry dbtGen = new DatabaseEntry(byteArr);
            return dbtGen;
        }
        public int SecondaryEntryComparison(
		    DatabaseEntry dbt1, DatabaseEntry dbt2)
        {
            int a, b;
            a = BitConverter.ToInt32(dbt1.Data, 0);
            b = BitConverter.ToInt32(dbt2.Data, 0);
            return a - b;
        }
 public void AddOneByCursor(HashCursor cursor)
 {
     DatabaseEntry key, data;
     KeyValuePair<DatabaseEntry, DatabaseEntry> pair;
     key = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key"));
     data = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data"));
     pair = new KeyValuePair<DatabaseEntry,DatabaseEntry>(key, data);
     cursor.Add(pair);
 }
 public DatabaseEntry MultipleKeyGen(DatabaseEntry key, DatabaseEntry data)
 {
     LinkedList<DatabaseEntry> skeys = new LinkedList<DatabaseEntry>();
     String dataStr = Configuration.strFromDBT(data);
     foreach (String s in dataStr.Split(',')) {
         DatabaseEntry tmp = new DatabaseEntry();
         Configuration.dbtFromString(tmp, s);
         skeys.AddLast(tmp);
     }
     return new MultipleDatabaseEntry(skeys, false);
 }
示例#12
0
        public DatabaseEntry SecondaryKeyGen(
            DatabaseEntry key, DatabaseEntry data)
        {
            DatabaseEntry dbtGen;

            int skey = BitConverter.ToInt32(data.Data, 0);
            // don't index secondary key of 0
            if (skey == 0)
                return null;

            dbtGen = new DatabaseEntry(data.Data);
            return dbtGen;
        }
示例#13
0
        public static void AddOneByCursor(Database db, Cursor cursor)
        {
            DatabaseEntry key, data;
            KeyValuePair<DatabaseEntry, DatabaseEntry> pair;

            // Add a record to db via cursor.
            key = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("key"));
            data = new DatabaseEntry(ASCIIEncoding.ASCII.GetBytes("data"));
            pair = new KeyValuePair<DatabaseEntry,DatabaseEntry>(key, data);
            cursor.Add(pair);

            // Confirm that the record has been put to the database.
            Assert.IsTrue(db.Exists(key));
        }
示例#14
0
        static void Main( string[] args )
        {
            try {
                var cfg = new HashDatabaseConfig();
                cfg.Duplicates = DuplicatesPolicy.UNSORTED;
                cfg.Creation = CreatePolicy.IF_NEEDED;
                cfg.CacheSize = new CacheInfo( 0, 64 * 1024, 1 );
                cfg.PageSize = 8 * 1024;

                Database db = HashDatabase.Open( "d:\\test.db", "hat_db", cfg );

                Console.WriteLine("db opened");

                var key = new DatabaseEntry();
                var data = new DatabaseEntry();

                key.Data = System.Text.Encoding.ASCII.GetBytes("key1");
                data.Data = System.Text.Encoding.ASCII.GetBytes("val1");

                try {
                    db.Put( key, data );
                    db.Put( key, data );
                }
                catch ( Exception ex ) {
                    Console.WriteLine( ex.Message );
                }

                using ( var dbc = db.Cursor() ) {

                    System.Text.ASCIIEncoding decode = new ASCIIEncoding();

                    /* Walk through the database and print out key/data pairs. */
                    Console.WriteLine( "All key : data pairs:" );
                    foreach ( KeyValuePair<DatabaseEntry, DatabaseEntry> p in dbc )
                        Console.WriteLine( "{0}::{1}",
                            decode.GetString( p.Key.Data ), decode.GetString( p.Value.Data ) );
                }

                db.Close();
                Console.WriteLine( "db closed" );
            }
            catch ( Exception ex ) {
                Console.WriteLine( ex.Message );
            }

            Console.ReadLine();
        }
        public void MoveToPos(string dbFileName, 
		    string dbSecFileName, bool ifPair)
        {
            // Open a primary database and its secondary database.
            BTreeDatabase db;
            SecondaryBTreeDatabase secDB;
            OpenSecDB(dbFileName, dbSecFileName, out db,
                out secDB);

            // Write ten records into the database.
            WriteRecords(db);

            SecondaryCursor secCursor = secDB.SecondaryCursor();
            DatabaseEntry key = new DatabaseEntry(
                BitConverter.GetBytes((int)0));
            DatabaseEntry notExistingKey = new DatabaseEntry(
                BitConverter.GetBytes((int)100));
            if (ifPair == false)
            {
                Assert.IsTrue(secCursor.Move(key, true));
                Assert.IsFalse(secCursor.Move(notExistingKey, true));
            }
            else
            {
                KeyValuePair<DatabaseEntry, KeyValuePair<
                    DatabaseEntry, DatabaseEntry>> pair =
                    new KeyValuePair<DatabaseEntry,
                    KeyValuePair<DatabaseEntry, DatabaseEntry>>(key,
                    new KeyValuePair<DatabaseEntry, DatabaseEntry>(
                    key, key));

                KeyValuePair<DatabaseEntry, KeyValuePair<
                    DatabaseEntry, DatabaseEntry>> notExistingPair;

                notExistingPair = new KeyValuePair<DatabaseEntry,
                    KeyValuePair<DatabaseEntry, DatabaseEntry>>(
                    notExistingKey, new KeyValuePair<
                    DatabaseEntry, DatabaseEntry>(
                    notExistingKey, notExistingKey));
                Assert.IsTrue(secCursor.Move(pair, true));
                Assert.IsFalse(secCursor.Move(notExistingPair, true));
            }

            secCursor.Close();
            secDB.Close();
            db.Close();
        }
示例#16
0
        public void LoadInventoryDB(string dataDir)
        {
            DatabaseEntry key, data;
            string inventory_text_file = dataDir + "\\" + "inventory.txt";

            if (!File.Exists(inventory_text_file)) {
                Console.WriteLine("{0} does not exist.", inventory_text_file);
                return;
            }

            using (StreamReader sr = File.OpenText(inventory_text_file)) {
                Inventory inventory = new Inventory();
                string input;

                /* Text file fields are delimited by #, just read them in. */
                while ((input=sr.ReadLine())!=null) {
                    char [] delimiterPound = {'#'};
                    string [] fields = input.Split(delimiterPound);
            #if TEST_DEBUG
                    System.Console.WriteLine(input);
            #endif
                    inventory.Itemname = fields[0];
                    inventory.Sku = fields[1];
                    inventory.Price = float.Parse(fields[2]);
                    inventory.Quantity = int.Parse(fields[3]);
                    inventory.Category = fields[4];
                    inventory.Vendor = fields[5];

                    /* Insert key/data pairs into database. */
                    key = new DatabaseEntry();
                    key.Data = System.Text.Encoding.ASCII.GetBytes(
                        inventory.Sku);

                    byte [] bytes = inventory.getBytes();
                    data = new DatabaseEntry(bytes);

                    try {
                        myDbs.InventoryDB.Put(key, data);
                    } catch(Exception e) {
                        Console.WriteLine("LoadInventoryDB Error.");
                        Console.WriteLine(e.Message);
                        throw e;
                    }
                }
            }
        }
示例#17
0
        public void showItem(string locateItem)
        {
            SecondaryCursor secCursor;

            /* searchKey is the key to find in the secondary db. */
            DatabaseEntry searchKey = new DatabaseEntry();
            searchKey.Data = System.Text.Encoding.ASCII.GetBytes(locateItem);

            /* Open a secondary cursor. */
            secCursor = this.myDbs.ItemSecbtreeDB.SecondaryCursor();

            /*
             * Search for the secondary. The primary key/data pair
             * associated with the given secondary key is stored in Current.
             * In the presence of duplicate key values, the first data
             * item in the set of duplicates is stored in Current.
             */
            if (secCursor.Move(searchKey, true)) {
                Inventory theInventory = new Inventory(
                    secCursor.Current.Value.Value.Data);

                /* Display the record. */
                displayInventory(theInventory);

                /* Get each duplicate. */
                while (secCursor.MoveNextDuplicate()) {
                    theInventory = new Inventory(
                        secCursor.Current.Value.Value.Data);
                    displayInventory(theInventory);
                }
            } else {
                Console.WriteLine("Could not find itemname {0} ", locateItem);
                Console.WriteLine();
                Console.WriteLine();
                return;
            }

            /* Close the cursor and the duplicate cursor. */
            secCursor.Close();
        }
示例#18
0
        static public DatabaseEntry GetNextDBEntry(DatabaseEntry key)
        {

            long FirstPart;
            if (key.Data != null)
            {
                using (var ms = new MemoryStream(key.Data))
                {
                    using (var bw = new BinaryReader(ms))
                    {
                        FirstPart = bw.ReadInt64();
                        bw.Close();
                    }
                    ms.Close();
                }
            }
            else
            {
                FirstPart = 0;
            }
            int count = sizeof(long) + sizeof(byte) + sizeof(byte);
            var data = new byte[count];

            using (var ms = new MemoryStream(data))
            {
                using (var bw = new BinaryWriter(ms))
                {
                    bw.Write((FirstPart+1));
                    bw.Write((byte)0);
                    bw.Write((byte)0);
                    bw.Close();
                }
                ms.Close();
            }
            return new DatabaseEntry(data);

        }
示例#19
0
 /// <summary>
 /// Store the key/data pair in the database, only if the key does not
 /// already appear in the database.
 /// </summary>
 /// <remarks>
 /// This enforcement of uniqueness of keys applies only to the primary
 /// key, the behavior of insertions into secondary databases is not
 /// affected. In particular, the insertion of a record that would result
 /// in the creation of a duplicate key in a secondary database that
 /// allows duplicates would not be prevented by the use of this flag.
 /// </remarks>
 /// <param name="key">The key to store in the database</param>
 /// <param name="data">The data item to store in the database</param>
 public void PutNoOverwrite(DatabaseEntry key, DatabaseEntry data)
 {
     PutNoOverwrite(key, data, null);
 }
示例#20
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);
 }
示例#21
0
        /// <summary>
        /// Return the record number associated with the cursor's current
        /// position.
        /// </summary>
        /// <param name="info">The locking behavior to use</param>
        /// <returns>The record number associated with the cursor.</returns>
        public uint Recno(LockingInfo info)
        {
            DatabaseEntry key = new DatabaseEntry();
            DatabaseEntry data = new DatabaseEntry();
            base.Get(key, data, DbConstants.DB_GET_RECNO, info);

            return BitConverter.ToUInt32(base.Current.Value.Data, 0);
        }
示例#22
0
 /// <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);
 }
示例#23
0
 /// <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>
 /// <exception cref="NotFoundException">
 /// A NotFoundException is thrown if <paramref name="key"/> is not in
 /// the database. 
 /// </exception>
 /// <exception cref="KeyEmptyException">
 /// A KeyEmptyException is thrown if the database is a
 /// <see cref="QueueDatabase"/> or <see cref="RecnoDatabase"/> 
 /// database and <paramref name="key"/> exists, but was never explicitly
 /// created by the application or was later deleted.
 /// </exception>
 /// <returns>
 /// A <see cref="KeyValuePair{T,T}"/>
 /// whose Key parameter is <paramref name="key"/> and whose Value
 /// parameter is the retrieved data items.
 /// </returns>
 public KeyValuePair<DatabaseEntry, MultipleDatabaseEntry> GetMultiple(
     DatabaseEntry key, int BufferSize)
 {
     return GetMultiple(key, BufferSize, null, null);
 }
示例#24
0
 /// <summary>
 /// Append the data item to the end of the database.
 /// </summary>
 /// <param name="data">The data item to store in the database</param>
 /// <returns>The heap record id allocated to the record</returns>
 public HeapRecordId Append(DatabaseEntry data)
 {
     return(Append(data, null));
 }
示例#25
0
        private void Config(HashDatabaseConfig cfg)
        {
            base.Config(cfg);

            /*
             * Database.Config calls set_flags, but that does not get the Hash
             * specific flags.  No harm in calling it again.
             */
            db.set_flags(cfg.flags);

            if (cfg.BlobDir != null && cfg.Env == null)
            {
                db.set_blob_dir(cfg.BlobDir);
            }

            if (cfg.blobThresholdIsSet)
            {
                db.set_blob_threshold(cfg.BlobThreshold, 0);
            }

            if (cfg.HashFunction != null)
            {
                HashFunction = cfg.HashFunction;
            }
            // The duplicate comparison function cannot change.
            if (cfg.DuplicateCompare != null)
            {
                DupCompare = cfg.DuplicateCompare;
            }
            if (cfg.fillFactorIsSet)
            {
                db.set_h_ffactor(cfg.FillFactor);
            }
            if (cfg.nelemIsSet)
            {
                db.set_h_nelem(cfg.TableSize);
            }
            if (cfg.HashComparison != null)
            {
                Compare = cfg.HashComparison;
            }

            if (cfg.partitionIsSet)
            {
                nparts    = cfg.NParts;
                Partition = cfg.Partition;
                if (Partition == null)
                {
                    doPartitionRef = null;
                }
                else
                {
                    doPartitionRef = new BDB_PartitionDelegate(doPartition);
                }
                partitionKeys = cfg.PartitionKeys;
                IntPtr[] ptrs = null;
                if (partitionKeys != null)
                {
                    int size = (int)nparts - 1;
                    ptrs = new IntPtr[size];
                    for (int i = 0; i < size; i++)
                    {
                        ptrs[i] = DBT.getCPtr(
                            DatabaseEntry.getDBT(partitionKeys[i])).Handle;
                    }
                }
                db.set_partition(nparts, ptrs, doPartitionRef);
            }
        }
示例#26
0
        /// <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);
        }
示例#27
0
 /// <summary>
 /// Return an estimate of the proportion of keys that are less than,
 /// equal to, and greater than the specified key.
 /// </summary>
 /// <param name="key">The key to search for</param>
 /// <returns>
 /// An estimate of the proportion of keys that are less than, equal to,
 /// and greater than the specified key.
 /// </returns>
 public KeyRange KeyRange(DatabaseEntry key)
 {
     return(KeyRange(key, null));
 }
示例#28
0
        /// <summary>
        /// Protected method to call the key generation function.
        /// </summary>
        /// <param name="dbp">Secondary DB Handle</param>
        /// <param name="keyp">Primary Key</param>
        /// <param name="datap">Primary Data</param>
        /// <param name="skeyp">Scondary Key</param>
        /// <returns>0 on success, !0 on failure</returns>
        protected static int doAssociate(
            IntPtr dbp, IntPtr keyp, IntPtr datap, IntPtr skeyp)
        {
            DB     db = new DB(dbp, false);
            DBT    key = new DBT(keyp, false);
            DBT    data = new DBT(datap, false);
            DBT    skey = new DBT(skeyp, false);
            IntPtr dataPtr, sdataPtr;
            int    nrecs, dbt_sz;

            DatabaseEntry s =
                ((SecondaryDatabase)db.api_internal).KeyGen(
                    DatabaseEntry.fromDBT(key), DatabaseEntry.fromDBT(data));

            if (s == null)
            {
                return(DbConstants.DB_DONOTINDEX);
            }
            if (s is MultipleDatabaseEntry)
            {
                MultipleDatabaseEntry mde = (MultipleDatabaseEntry)s;
                nrecs = mde.nRecs;

                /*
                 * Allocate an array of nrecs DBT in native memory.  The call
                 * returns sizeof(DBT), so that we know where one DBT ends and
                 * the next begins.
                 */
                dbt_sz = (int)libdb_csharp.alloc_dbt_arr(null, nrecs, out sdataPtr);

                /*
                 * We need a managed array to copy each DBT into and then we'll
                 * copy the managed array to the native array we just allocated.
                 * We are not able to copy native -> native.
                 */
                byte[] arr = new byte[nrecs * dbt_sz];
                IntPtr p;
                int    off = 0;
                /* Copy each DBT into the array. */
                foreach (DatabaseEntry dbt in mde)
                {
                    /* Allocate room for the data in native memory. */
                    dataPtr = libdb_csharp.__os_umalloc(null, dbt.size);
                    Marshal.Copy(dbt.Data, 0, dataPtr, (int)dbt.size);
                    dbt.dbt.dataPtr = dataPtr;
                    dbt.flags      |= DbConstants.DB_DBT_APPMALLOC;

                    p = DBT.getCPtr(DatabaseEntry.getDBT(dbt)).Handle;
                    Marshal.Copy(p, arr, off, dbt_sz);
                    off += dbt_sz;
                }
                Marshal.Copy(arr, 0, sdataPtr, nrecs * dbt_sz);
                skey.dataPtr = sdataPtr;
                skey.size    = (uint)mde.nRecs;
                skey.flags   = DbConstants.DB_DBT_MULTIPLE | DbConstants.DB_DBT_APPMALLOC;
            }
            else
            {
                skey.data = s.Data;
            }
            return(0);
        }
示例#29
0
 /// <summary>
 /// Store the key/data pair in the database only if it does not already
 /// appear in the database.
 /// </summary>
 /// <param name="key">The key to store in the database</param>
 /// <param name="data">The data item to store in the database</param>
 public void PutNoDuplicate(DatabaseEntry key, DatabaseEntry data)
 {
     PutNoDuplicate(key, data, null);
 }
示例#30
0
 /// <summary>
 /// Store the key/data pair in the database only if it does not already
 /// appear in the database.
 /// </summary>
 /// <param name="key">The key to store in the database</param>
 /// <param name="data">The data item to store in the database</param>
 /// <param name="txn">
 /// If the operation is part of an application-specified transaction,
 /// <paramref name="txn"/> is a Transaction object returned from
 /// <see cref="DatabaseEnvironment.BeginTransaction"/>; if
 /// the operation is part of a Berkeley DB Concurrent Data Store group,
 /// <paramref name="txn"/> is a handle returned from
 /// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null.
 /// </param>
 public void PutNoDuplicate(
     DatabaseEntry key, DatabaseEntry data, Transaction txn)
 {
     Put(key, data, txn, DbConstants.DB_NODUPDATA);
 }
示例#31
0
 /// <summary>
 /// Insert the data element as a duplicate element of the key to which
 /// the cursor refers.
 /// </summary>
 /// <param name="data">The data element to insert</param>
 /// <param name="loc">
 /// Specify whether to insert the data item immediately before or
 /// immediately after the cursor's current position.
 /// </param>
 public new void Insert(DatabaseEntry data, InsertLocation loc)
 {
     base.Insert(data, loc);
 }
示例#32
0
 /// <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);
 }
示例#33
0
        /// <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);
        }
示例#34
0
 /// <summary>
 /// If a key/data pair in the database matches <paramref name="key"/>
 /// and <paramref name="data"/>, return the key and all duplicate data
 /// items.
 /// </summary>
 /// <param name="key">The key to search for</param>
 /// <param name="data">The data to search for</param>
 /// <exception cref="NotFoundException">
 /// A NotFoundException is thrown if <paramref name="key"/> and
 /// <paramref name="data"/> are not in the database. 
 /// </exception>
 /// <exception cref="KeyEmptyException">
 /// A KeyEmptyException is thrown if the database is a
 /// <see cref="QueueDatabase"/> or <see cref="RecnoDatabase"/> 
 /// database and <paramref name="key"/> exists, but was never explicitly
 /// created by the application or was later deleted.
 /// </exception>
 /// <returns>
 /// A <see cref="KeyValuePair{T,T}"/>
 /// whose Key parameter is <paramref name="key"/> and whose Value
 /// parameter is the retrieved data items.
 /// </returns>
 public KeyValuePair<DatabaseEntry, MultipleDatabaseEntry> GetBothMultiple(
     DatabaseEntry key, DatabaseEntry data)
 {
     /*
      * Make sure we pass a buffer that is big enough to hold data.Data
      * and is a multiple of the page size.  Cache this.Pagesize to avoid
      * multiple P/Invoke calls.
      */
     uint pgsz = Pagesize;
     uint npgs = (data.size / pgsz) + 1;
     return GetBothMultiple(key, data, (int)(npgs * pgsz), null, null);
 }
示例#35
0
 /// <summary>
 /// Append the data item to the end of the database.
 /// </summary>
 /// <param name="data">The data item to store in the database</param>
 /// <returns>The record number allocated to the record</returns>
 public uint Append(DatabaseEntry data)
 {
     return(Append(data, null));
 }
示例#36
0
 /// <summary>
 /// Retrieve a key and all duplicate data items from the database.
 /// </summary>
 /// <param name="key">The key to search for</param>
 /// <exception cref="NotFoundException">
 /// A NotFoundException is thrown if <paramref name="key"/> is not in
 /// the database. 
 /// </exception>
 /// <exception cref="KeyEmptyException">
 /// A KeyEmptyException is thrown if the database is a
 /// <see cref="QueueDatabase"/> or <see cref="RecnoDatabase"/> 
 /// database and <paramref name="key"/> exists, but was never explicitly
 /// created by the application or was later deleted.
 /// </exception>
 /// <returns>
 /// A <see cref="KeyValuePair{T,T}"/>
 /// whose Key parameter is <paramref name="key"/> and whose Value
 /// parameter is the retrieved data items.
 /// </returns>
 public KeyValuePair<DatabaseEntry, MultipleDatabaseEntry> GetMultiple(
     DatabaseEntry key)
 {
     return GetMultiple(key, (int)Pagesize, null, null);
 }
示例#37
0
        private void Config(BTreeDatabaseConfig cfg)
        {
            base.Config(cfg);

            /*
             * Database.Config calls set_flags, but that does not get the BTree
             * specific flags.  No harm in calling it again.
             */
            db.set_flags(cfg.flags);

            if (cfg.BlobDir != null && cfg.Env == null)
            {
                db.set_ext_file_dir(cfg.BlobDir);
            }

            if (cfg.ExternalFileDir != null && cfg.Env == null)
            {
                db.set_ext_file_dir(cfg.ExternalFileDir);
            }

            if (cfg.blobThresholdIsSet)
            {
                db.set_ext_file_threshold(cfg.BlobThreshold, 0);
            }

            if (cfg.BTreeCompare != null)
            {
                Compare = cfg.BTreeCompare;
            }

            if (cfg.BTreePrefixCompare != null)
            {
                PrefixCompare = cfg.BTreePrefixCompare;
            }

            // The duplicate comparison function cannot change.
            if (cfg.DuplicateCompare != null)
            {
                DupCompare = cfg.DuplicateCompare;
            }

            if (cfg.minkeysIsSet)
            {
                db.set_bt_minkey(cfg.MinKeysPerPage);
            }

            if (cfg.compressionIsSet)
            {
                Compress   = cfg.Compress;
                Decompress = cfg.Decompress;
                if (Compress == null)
                {
                    doCompressRef = null;
                }
                else
                {
                    doCompressRef = new BDB_CompressDelegate(doCompress);
                }
                if (Decompress == null)
                {
                    doDecompressRef = null;
                }
                else
                {
                    doDecompressRef = new BDB_DecompressDelegate(doDecompress);
                }
                db.set_bt_compress(doCompressRef, doDecompressRef);
            }

            if (cfg.partitionIsSet)
            {
                nparts    = cfg.NParts;
                Partition = cfg.Partition;
                if (Partition == null)
                {
                    doPartitionRef = null;
                }
                else
                {
                    doPartitionRef = new BDB_PartitionDelegate(doPartition);
                }
                partitionKeys = cfg.PartitionKeys;
                IntPtr[] ptrs = null;
                if (partitionKeys != null)
                {
                    int size = (int)nparts - 1;
                    ptrs = new IntPtr[size];
                    for (int i = 0; i < size; i++)
                    {
                        ptrs[i] = DBT.getCPtr(
                            DatabaseEntry.getDBT(partitionKeys[i])).Handle;
                    }
                }
                db.set_partition(nparts, ptrs, doPartitionRef);
            }
        }
示例#38
0
 /// <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);
 }
示例#39
0
 internal CompactData(DB_COMPACT dbcompact, DatabaseEntry end)
 {
     cdata = dbcompact;
     _end  = end;
 }
示例#40
0
 /// <summary>
 /// Store the key/data pair in the database, replacing any previously
 /// existing key if duplicates are disallowed, or adding a duplicate
 /// data item if duplicates are allowed.
 /// </summary>
 /// <overloads>
 /// <para>
 /// If the database supports duplicates, add the new data value at the
 /// end of the duplicate set. If the database supports sorted
 /// duplicates, the new data value is inserted at the correct sorted
 /// location.
 /// </para>
 /// </overloads>
 /// <param name="key">The key to store in the database</param>
 /// <param name="data">The data item to store in the database</param>
 /// <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)
 {
     Put(key, data, null);
 }
示例#41
0
 internal static DBT getDBT(DatabaseEntry ent)
 {
     return(ent == null ? null : ent.dbt);
 }