Exemplo n.º 1
0
        public void Delete(List <byte[]> keys)
        {
            List <DatabaseEntry> lst = new List <DatabaseEntry>(keys.Count);

            foreach (byte[] k in keys)
            {
                DatabaseEntry entry = new DatabaseEntry(k);
                lst.Add(entry);
            }
            MultipleDatabaseEntry mk = new MultipleDatabaseEntry(lst, false);

            db.Delete(mk);
        }
Exemplo n.º 2
0
        /* Perform bulk delete in primary db */
        public void BulkDelete(int value)
        {
            Transaction txn = env.BeginTransaction();

            try {
                if (dups == 0)
                {
                    /* Delete a set of key/data pairs */
                    List <KeyValuePair <DatabaseEntry, DatabaseEntry> > pList =
                        new List <KeyValuePair <DatabaseEntry, DatabaseEntry> >();
                    for (int i = 0; i < value; i++)
                    {
                        pList.Add(
                            new KeyValuePair <DatabaseEntry, DatabaseEntry>(
                                new DatabaseEntry(BitConverter.GetBytes(i)),
                                new DatabaseEntry(getBytes(new DataVal(i)))));
                    }

                    MultipleKeyDatabaseEntry pairs =
                        new MultipleKeyDatabaseEntry(pList, false);
                    pdb.Delete(pairs, txn);
                }
                else
                {
                    /* Delete a set of keys */
                    List <DatabaseEntry> kList = new List <DatabaseEntry>();
                    for (int i = 0; i < value; i++)
                    {
                        kList.Add(new DatabaseEntry(
                                      BitConverter.GetBytes(i)));
                    }
                    MultipleDatabaseEntry keySet =
                        new MultipleDatabaseEntry(kList, false);
                    pdb.Delete(keySet, txn);
                }
                txn.Commit();
            } catch (DatabaseException e) {
                txn.Abort();
                throw e;
            }
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
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);
        }
        private void DeleteMultipleAndMultipleKey(string dbFileName,
		    string dbName, DatabaseType type, bool mulKey)
        {
            List<DatabaseEntry> kList = new List<DatabaseEntry>();
            List<uint> rList = new List<uint>();
            List<KeyValuePair<DatabaseEntry, DatabaseEntry>> pList =
                new List<KeyValuePair<DatabaseEntry, DatabaseEntry>>();
            DatabaseEntry key;
            Database db;
            SecondaryDatabase secDb;

            Configuration.ClearDir(testHome);

            if (type == DatabaseType.BTREE) {
                BTreeDatabaseConfig dbConfig =
                    new BTreeDatabaseConfig();
                dbConfig.Creation = CreatePolicy.IF_NEEDED;
                db = BTreeDatabase.Open(
                    dbFileName, dbName, dbConfig);
                SecondaryBTreeDatabaseConfig secDbConfig =
                    new SecondaryBTreeDatabaseConfig(db, null);
                secDbConfig.Creation = CreatePolicy.IF_NEEDED;
                secDbConfig.Duplicates = DuplicatesPolicy.SORTED;
                secDbConfig.KeyGen =
                    new SecondaryKeyGenDelegate(SecondaryKeyGen);
                secDb = SecondaryBTreeDatabase.Open(
                    dbFileName, dbName + "_sec", secDbConfig);
            } else if (type == DatabaseType.HASH) {
                HashDatabaseConfig dbConfig =
                    new HashDatabaseConfig();
                dbConfig.Creation = CreatePolicy.IF_NEEDED;
                db = HashDatabase.Open(
                    dbFileName, dbName, dbConfig);
                SecondaryHashDatabaseConfig secDbConfig =
                    new SecondaryHashDatabaseConfig(db, null);
                secDbConfig.Creation = CreatePolicy.IF_NEEDED;
                secDbConfig.Duplicates = DuplicatesPolicy.SORTED;
                secDbConfig.KeyGen =
                    new SecondaryKeyGenDelegate(SecondaryKeyGen);
                secDb = SecondaryHashDatabase.Open(
                    dbFileName, dbName + "_sec", secDbConfig);
            } else if (type == DatabaseType.QUEUE) {
                QueueDatabaseConfig dbConfig =
                    new QueueDatabaseConfig();
                dbConfig.Creation = CreatePolicy.IF_NEEDED;
                dbConfig.Length = 4;
                db = QueueDatabase.Open(dbFileName, dbConfig);
                SecondaryQueueDatabaseConfig secDbConfig =
                    new SecondaryQueueDatabaseConfig(db, null);
                secDbConfig.Creation = CreatePolicy.IF_NEEDED;
                secDbConfig.Length = 4;
                secDbConfig.KeyGen =
                    new SecondaryKeyGenDelegate(SecondaryKeyGen);
                secDb = SecondaryQueueDatabase.Open(
                    dbFileName + "_sec", secDbConfig);
            } else if (type == DatabaseType.RECNO) {
                RecnoDatabaseConfig dbConfig =
                    new RecnoDatabaseConfig();
                dbConfig.Creation = CreatePolicy.IF_NEEDED;
                db = RecnoDatabase.Open(
                    dbFileName, dbName, dbConfig);
                SecondaryRecnoDatabaseConfig secDbConfig =
                    new SecondaryRecnoDatabaseConfig(db, null);
                secDbConfig.Creation = CreatePolicy.IF_NEEDED;
                secDbConfig.KeyGen =
                    new SecondaryKeyGenDelegate(SecondaryKeyGen);
                secDb = SecondaryRecnoDatabase.Open(
                    dbFileName, dbName + "_sec", secDbConfig);
            } else
                throw new TestException();

            for (uint i = 1; i <= 100; i++) {
                key = new DatabaseEntry(
                    BitConverter.GetBytes(i));
                if (i >= 50 && i < 60)
                    kList.Add(key);
                else if (i > 80)
                    pList.Add(new KeyValuePair<
                        DatabaseEntry, DatabaseEntry>(
                        key, key));
                else if (type == DatabaseType.QUEUE
                    || type == DatabaseType.RECNO)
                    rList.Add(i);

                db.Put(key, key);
            }

            if (mulKey) {
                // Create bulk buffer for key/value pairs.
                MultipleKeyDatabaseEntry pBuff;
                if (type == DatabaseType.BTREE)
                    pBuff = new MultipleKeyDatabaseEntry(
                        pList, false);
                else if (type == DatabaseType.HASH)
                    pBuff = new MultipleKeyDatabaseEntry(
                        pList, false);
                else if (type == DatabaseType.QUEUE)
                    pBuff = new MultipleKeyDatabaseEntry(
                        pList, true);
                else
                    pBuff = new MultipleKeyDatabaseEntry(
                        pList, true);

                // Bulk delete with the key/value pair bulk buffer.
                secDb.Delete(pBuff);
                foreach (KeyValuePair<DatabaseEntry,
                    DatabaseEntry>pair in pList) {
                    try {
                        db.GetBoth(pair.Key, pair.Value);
                        throw new TestException();
                    } catch (NotFoundException e1) {
                        if (type == DatabaseType.QUEUE)
                            throw e1;
                    } catch (KeyEmptyException e2) {
                        if (type == DatabaseType.BTREE ||
                            type == DatabaseType.HASH ||
                            type == DatabaseType.RECNO)
                            throw e2;
                    }
                }

                /*
                 * Dump the database to verify that 80 records
                 * remain after bulk delete.
                 */
                Assert.AreEqual(80, db.Truncate());
            } else {
                // Create bulk buffer for key.
                MultipleDatabaseEntry kBuff;
                if (type == DatabaseType.BTREE)
                    kBuff = new MultipleDatabaseEntry(
                        kList, false);
                else if (type == DatabaseType.HASH)
                    kBuff = new MultipleDatabaseEntry(
                        kList, false);
                else if (type == DatabaseType.QUEUE)
                    kBuff = new MultipleDatabaseEntry(
                        kList, true);
                else
                    kBuff = new MultipleDatabaseEntry(
                        kList, true);

                /*
                 * Bulk delete in secondary database with key
                 * buffer. Primary records that the deleted
                 * records in secondar database should be
                 * deleted as well.
                 */
                secDb.Delete(kBuff);
                foreach (DatabaseEntry dbt in kList) {
                    try {
                        db.Get(dbt);
                        throw new TestException();
                    } catch (NotFoundException e1) {
                        if (type == DatabaseType.QUEUE ||
                            type == DatabaseType.RECNO)
                            throw e1;
                    } catch (KeyEmptyException e2) {
                        if (type == DatabaseType.BTREE ||
                            type == DatabaseType.HASH)
                            throw e2;
                    }
                }

                /*
                 * Bulk delete in secondary database with recno
                 * based key buffer.
                 */
                if (type == DatabaseType.QUEUE ||
                    type == DatabaseType.RECNO) {
                    MultipleDatabaseEntry rBuff =
                        new MultipleDatabaseEntry(rList);
                    secDb.Delete(rBuff);
                    Assert.AreEqual(20, db.Truncate());
                }
            }

            secDb.Close();
            db.Close();
        }
Exemplo n.º 6
0
        private void DeleteMultipleAndMultipleKey(string dbFileName,
                                                  string dbName, DatabaseType type, bool mulKey)
        {
            List <DatabaseEntry> kList = new List <DatabaseEntry>();
            List <uint>          rList = new List <uint>();
            List <KeyValuePair <DatabaseEntry, DatabaseEntry> > pList =
                new List <KeyValuePair <DatabaseEntry, DatabaseEntry> >();
            DatabaseEntry     key;
            Database          db;
            SecondaryDatabase secDb;

            Configuration.ClearDir(testHome);

            if (type == DatabaseType.BTREE)
            {
                BTreeDatabaseConfig dbConfig =
                    new BTreeDatabaseConfig();
                dbConfig.Creation = CreatePolicy.IF_NEEDED;
                db = BTreeDatabase.Open(
                    dbFileName, dbName, dbConfig);
                SecondaryBTreeDatabaseConfig secDbConfig =
                    new SecondaryBTreeDatabaseConfig(db, null);
                secDbConfig.Creation   = CreatePolicy.IF_NEEDED;
                secDbConfig.Duplicates = DuplicatesPolicy.SORTED;
                secDbConfig.KeyGen     =
                    new SecondaryKeyGenDelegate(SecondaryKeyGen);
                secDb = SecondaryBTreeDatabase.Open(
                    dbFileName, dbName + "_sec", secDbConfig);
            }
            else if (type == DatabaseType.HASH)
            {
                HashDatabaseConfig dbConfig =
                    new HashDatabaseConfig();
                dbConfig.Creation = CreatePolicy.IF_NEEDED;
                db = HashDatabase.Open(
                    dbFileName, dbName, dbConfig);
                SecondaryHashDatabaseConfig secDbConfig =
                    new SecondaryHashDatabaseConfig(db, null);
                secDbConfig.Creation   = CreatePolicy.IF_NEEDED;
                secDbConfig.Duplicates = DuplicatesPolicy.SORTED;
                secDbConfig.KeyGen     =
                    new SecondaryKeyGenDelegate(SecondaryKeyGen);
                secDb = SecondaryHashDatabase.Open(
                    dbFileName, dbName + "_sec", secDbConfig);
            }
            else if (type == DatabaseType.QUEUE)
            {
                QueueDatabaseConfig dbConfig =
                    new QueueDatabaseConfig();
                dbConfig.Creation = CreatePolicy.IF_NEEDED;
                dbConfig.Length   = 4;
                db = QueueDatabase.Open(dbFileName, dbConfig);
                SecondaryQueueDatabaseConfig secDbConfig =
                    new SecondaryQueueDatabaseConfig(db, null);
                secDbConfig.Creation = CreatePolicy.IF_NEEDED;
                secDbConfig.Length   = 4;
                secDbConfig.KeyGen   =
                    new SecondaryKeyGenDelegate(SecondaryKeyGen);
                secDb = SecondaryQueueDatabase.Open(
                    dbFileName + "_sec", secDbConfig);
            }
            else if (type == DatabaseType.RECNO)
            {
                RecnoDatabaseConfig dbConfig =
                    new RecnoDatabaseConfig();
                dbConfig.Creation = CreatePolicy.IF_NEEDED;
                db = RecnoDatabase.Open(
                    dbFileName, dbName, dbConfig);
                SecondaryRecnoDatabaseConfig secDbConfig =
                    new SecondaryRecnoDatabaseConfig(db, null);
                secDbConfig.Creation = CreatePolicy.IF_NEEDED;
                secDbConfig.KeyGen   =
                    new SecondaryKeyGenDelegate(SecondaryKeyGen);
                secDb = SecondaryRecnoDatabase.Open(
                    dbFileName, dbName + "_sec", secDbConfig);
            }
            else
            {
                throw new TestException();
            }

            for (uint i = 1; i <= 100; i++)
            {
                key = new DatabaseEntry(
                    BitConverter.GetBytes(i));
                if (i >= 50 && i < 60)
                {
                    kList.Add(key);
                }
                else if (i > 80)
                {
                    pList.Add(new KeyValuePair <
                                  DatabaseEntry, DatabaseEntry>(
                                  key, key));
                }
                else if (type == DatabaseType.QUEUE ||
                         type == DatabaseType.RECNO)
                {
                    rList.Add(i);
                }

                db.Put(key, key);
            }

            if (mulKey)
            {
                // Create bulk buffer for key/value pairs.
                MultipleKeyDatabaseEntry pBuff;
                if (type == DatabaseType.BTREE)
                {
                    pBuff = new MultipleKeyDatabaseEntry(
                        pList, false);
                }
                else if (type == DatabaseType.HASH)
                {
                    pBuff = new MultipleKeyDatabaseEntry(
                        pList, false);
                }
                else if (type == DatabaseType.QUEUE)
                {
                    pBuff = new MultipleKeyDatabaseEntry(
                        pList, true);
                }
                else
                {
                    pBuff = new MultipleKeyDatabaseEntry(
                        pList, true);
                }

                // Bulk delete with the key/value pair bulk buffer.
                secDb.Delete(pBuff);
                foreach (KeyValuePair <DatabaseEntry,
                                       DatabaseEntry> pair in pList)
                {
                    try {
                        db.GetBoth(pair.Key, pair.Value);
                        throw new TestException();
                    } catch (NotFoundException e1) {
                        if (type == DatabaseType.QUEUE)
                        {
                            throw e1;
                        }
                    } catch (KeyEmptyException e2) {
                        if (type == DatabaseType.BTREE ||
                            type == DatabaseType.HASH ||
                            type == DatabaseType.RECNO)
                        {
                            throw e2;
                        }
                    }
                }

                /*
                 * Dump the database to verify that 80 records
                 * remain after bulk delete.
                 */
                Assert.AreEqual(80, db.Truncate());
            }
            else
            {
                // Create bulk buffer for key.
                MultipleDatabaseEntry kBuff;
                if (type == DatabaseType.BTREE)
                {
                    kBuff = new MultipleDatabaseEntry(
                        kList, false);
                }
                else if (type == DatabaseType.HASH)
                {
                    kBuff = new MultipleDatabaseEntry(
                        kList, false);
                }
                else if (type == DatabaseType.QUEUE)
                {
                    kBuff = new MultipleDatabaseEntry(
                        kList, true);
                }
                else
                {
                    kBuff = new MultipleDatabaseEntry(
                        kList, true);
                }

                /*
                 * Bulk delete in secondary database with key
                 * buffer. Primary records that the deleted
                 * records in secondar database should be
                 * deleted as well.
                 */
                secDb.Delete(kBuff);
                foreach (DatabaseEntry dbt in kList)
                {
                    try {
                        db.Get(dbt);
                        throw new TestException();
                    } catch (NotFoundException e1) {
                        if (type == DatabaseType.QUEUE ||
                            type == DatabaseType.RECNO)
                        {
                            throw e1;
                        }
                    } catch (KeyEmptyException e2) {
                        if (type == DatabaseType.BTREE ||
                            type == DatabaseType.HASH)
                        {
                            throw e2;
                        }
                    }
                }

                /*
                 * Bulk delete in secondary database with recno
                 * based key buffer.
                 */
                if (type == DatabaseType.QUEUE ||
                    type == DatabaseType.RECNO)
                {
                    MultipleDatabaseEntry rBuff =
                        new MultipleDatabaseEntry(rList);
                    secDb.Delete(rBuff);
                    Assert.AreEqual(20, db.Truncate());
                }
            }

            secDb.Close();
            db.Close();
        }
Exemplo n.º 7
0
        public void TestPutMultiple()
        {
            testName = "TestPutMultiple";
            SetUpTest(true);

            QueueDatabaseConfig dbConfig = new QueueDatabaseConfig();
            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.ExtentSize = 1024;
            dbConfig.Length = 520;
            dbConfig.PadByte = 0;
            QueueDatabase db = QueueDatabase.Open(testHome + "/" +
                testName + ".db", dbConfig);

            List<uint> kList = new List<uint>();
            List<DatabaseEntry> vList = new List<DatabaseEntry>();
            DatabaseEntry key, data;
            for (uint i = 1; i <= 9;i++) {
                key = new DatabaseEntry(BitConverter.GetBytes(i));
                data = new DatabaseEntry(
                    ASCIIEncoding.ASCII.GetBytes("data" + i +
                    Configuration.RandomString(512)));
                kList.Add(i);
                vList.Add(data);
            }

            // Create bulk buffer for recno based keys.
            MultipleDatabaseEntry kBuff =
                new MultipleDatabaseEntry(kList);
            Assert.IsTrue(kBuff.Recno);
            int val = 0;
            foreach (DatabaseEntry dbt in kBuff) {
                Assert.AreEqual(
                    BitConverter.GetBytes(kList[val]),
                    dbt.Data);
                val++;
            }
            Assert.AreEqual(9, val);

            // Create bulk buffer for data.
            MultipleDatabaseEntry vBuff =
                new MultipleDatabaseEntry(vList, false);

            /*
             * Create recno bulk buffer from another recno bulk
             * buffer.
             */
            MultipleDatabaseEntry kBuff1 =
                new MultipleDatabaseEntry(kBuff.Data, kBuff.Recno);
            val = 0;
            foreach (DatabaseEntry dbt in kBuff1) {
                Assert.AreEqual(
                    BitConverter.GetBytes(kList[val]),
                    dbt.Data);
                val++;
            }
            Assert.AreEqual(9, val);

            // Bulk insert to database with key and value buffers.
            db.Put(kBuff, vBuff);
            Cursor cursor = db.Cursor();
            KeyValuePair<DatabaseEntry, DatabaseEntry> pair;
            val = 0;
            while (cursor.MoveNext()) {
                pair = cursor.Current;
                Assert.AreEqual(
                    BitConverter.GetBytes(kList[val]),
                    pair.Key.Data);
                for (int i = 0; i < 520; i++) {
                    if (i < vList[val].Data.Length)
                        Assert.AreEqual(vList[val].Data[i],
                            pair.Value.Data[i]);
                    else
                        // The pad byte is 0.
                        Assert.AreEqual(0, pair.Value.Data[i]);
                }
                Assert.IsFalse(cursor.MoveNextDuplicate());
                val++;
            }
            Assert.AreEqual(9, val);

            cursor.Close();
            db.Close();
        }
Exemplo n.º 8
0
        /* Perform bulk delete in secondary db */
        public int BulkSecondaryDelete(byte value, int deletePair)
        {
            DatabaseEntry key, data;

            byte[] tstrBytes = ASCIIEncoding.ASCII.GetBytes(DataVal.tstring);
            int    i         = 0;
            int    count     = 0;

            Transaction txn = env.BeginTransaction();

            try {
                if (deletePair == 1)
                {
                    /*
                     * Delete the given key and all keys prior to it,
                     * together with their duplicate data.
                     */
                    List <KeyValuePair <DatabaseEntry, DatabaseEntry> > pList =
                        new List <KeyValuePair <DatabaseEntry, DatabaseEntry> >();
                    do
                    {
                        int j   = 0;
                        int idx = 0;
                        key         = new DatabaseEntry();
                        key.Data    = new byte[1];
                        key.Data[0] = tstrBytes[i];

                        while (j < this.num / DataVal.tstring.Length)
                        {
                            idx  = j * DataVal.tstring.Length + i;
                            data = new DatabaseEntry(
                                getBytes(new DataVal(idx)));
                            pList.Add(new KeyValuePair <
                                          DatabaseEntry, DatabaseEntry>(key, data));
                            j++;
                        }
                        if (i < this.num % DataVal.tstring.Length)
                        {
                            idx  = j * DataVal.tstring.Length + i;
                            data = new DatabaseEntry(
                                getBytes(new DataVal(idx)));
                            pList.Add(new KeyValuePair <
                                          DatabaseEntry, DatabaseEntry>(
                                          key, data));
                            j++;
                        }
                        count += j;
                    } while (value != tstrBytes[i++]);
                    MultipleKeyDatabaseEntry pairs =
                        new MultipleKeyDatabaseEntry(pList, false);
                    sdb.Delete(pairs, txn);
                }
                else
                {
                    List <DatabaseEntry> kList = new List <DatabaseEntry>();
                    /* Delete the given key and all keys prior to it */
                    do
                    {
                        key         = new DatabaseEntry();
                        key.Data    = new byte[1];
                        key.Data[0] = tstrBytes[i];
                        kList.Add(key);
                    } while (value != tstrBytes[i++]);
                    MultipleDatabaseEntry keySet =
                        new MultipleDatabaseEntry(kList, false);
                    sdb.Delete(keySet, txn);
                    count = this.num / DataVal.tstring.Length * i;
                    if (i < this.num % DataVal.tstring.Length)
                    {
                        count += i;
                    }
                }
                txn.Commit();
                return(count);
            } catch (DatabaseException e) {
                txn.Abort();
                throw e;
            }
        }
Exemplo n.º 9
0
        /* Perform bulk delete in secondary db */
        public int BulkSecondaryDelete(byte value, int deletePair)
        {
            DatabaseEntry key, data;
            byte[] tstrBytes = ASCIIEncoding.ASCII.GetBytes(DataVal.tstring);
            int i = 0;
            int count = 0;

            Transaction txn = env.BeginTransaction();

            try {
                if (deletePair == 1) {
                    /*
                     * Delete the given key and all keys prior to it,
                     * together with their duplicate data.
                     */
                    List<KeyValuePair<DatabaseEntry, DatabaseEntry>> pList =
                        new List<KeyValuePair<DatabaseEntry, DatabaseEntry>>();
                    do {
                        int j = 0;
                        int idx = 0;
                        key = new DatabaseEntry();
                        key.Data = new byte[1];
                        key.Data[0] = tstrBytes[i];

                        while (j < this.num / DataVal.tstring.Length) {
                            idx = j * DataVal.tstring.Length + i;
                            data = new DatabaseEntry(
                                getBytes(new DataVal(idx)));
                            pList.Add(new KeyValuePair<
                                DatabaseEntry, DatabaseEntry>(key, data));
                            j++;
                        }
                        if (i < this.num % DataVal.tstring.Length) {
                            idx = j * DataVal.tstring.Length + i;
                            data = new DatabaseEntry(
                                getBytes(new DataVal(idx)));
                            pList.Add(new KeyValuePair<
                                DatabaseEntry, DatabaseEntry>(
                                key, data));
                            j++;
                        }
                        count += j;
                    } while (value != tstrBytes[i++]);
                    MultipleKeyDatabaseEntry pairs =
                        new MultipleKeyDatabaseEntry(pList, false);
                    sdb.Delete(pairs, txn);
                } else {
                    List<DatabaseEntry> kList = new List<DatabaseEntry>();
                    /* Delete the given key and all keys prior to it */
                    do {
                        key = new DatabaseEntry();
                        key.Data = new byte[1];
                        key.Data[0] = tstrBytes[i];
                        kList.Add(key);
                    } while (value != tstrBytes[i++]);
                    MultipleDatabaseEntry keySet =
                        new MultipleDatabaseEntry(kList, false);
                    sdb.Delete(keySet, txn);
                    count = this.num / DataVal.tstring.Length * i;
                    if (i < this.num % DataVal.tstring.Length)
                        count += i;
                }
                txn.Commit();
                return count;
            } catch (DatabaseException e) {
                txn.Abort();
                throw e;
            }
        }
Exemplo n.º 10
0
        /* Perform bulk delete in primary db */
        public void BulkDelete(int value)
        {
            Transaction txn = env.BeginTransaction();

            try {
                if (dups == 0) {
                    /* Delete a set of key/data pairs */
                    List<KeyValuePair<DatabaseEntry, DatabaseEntry>> pList =
                    new List<KeyValuePair<DatabaseEntry, DatabaseEntry>>();
                    for (int i = 0; i < value; i++)
                        pList.Add(
                            new KeyValuePair<DatabaseEntry, DatabaseEntry>(
                            new DatabaseEntry(BitConverter.GetBytes(i)),
                            new DatabaseEntry(getBytes(new DataVal(i)))));

                    MultipleKeyDatabaseEntry pairs =
                        new MultipleKeyDatabaseEntry(pList, false);
                    pdb.Delete(pairs, txn);
                } else {
                    /* Delete a set of keys */
                    List<DatabaseEntry> kList = new List<DatabaseEntry>();
                    for (int i = 0; i < value; i++)
                        kList.Add(new DatabaseEntry(
                            BitConverter.GetBytes(i)));
                    MultipleDatabaseEntry keySet =
                        new MultipleDatabaseEntry(kList, false);
                    pdb.Delete(keySet, txn);
                }
                txn.Commit();
            } catch (DatabaseException e) {
                txn.Abort();
                throw e;
            }
        }
Exemplo n.º 11
0
        public void TestPutMultiple()
        {
            testName = "TestPutMultiple";
            SetUpTest(true);

            QueueDatabaseConfig dbConfig = new QueueDatabaseConfig();

            dbConfig.Creation   = CreatePolicy.IF_NEEDED;
            dbConfig.ExtentSize = 1024;
            dbConfig.Length     = 520;
            dbConfig.PadByte    = 0;
            QueueDatabase db = QueueDatabase.Open(testHome + "/" +
                                                  testName + ".db", dbConfig);

            List <uint>          kList = new List <uint>();
            List <DatabaseEntry> vList = new List <DatabaseEntry>();
            DatabaseEntry        key, data;

            for (uint i = 1; i <= 9; i++)
            {
                key  = new DatabaseEntry(BitConverter.GetBytes(i));
                data = new DatabaseEntry(
                    ASCIIEncoding.ASCII.GetBytes("data" + i +
                                                 Configuration.RandomString(512)));
                kList.Add(i);
                vList.Add(data);
            }

            // Create bulk buffer for recno based keys.
            MultipleDatabaseEntry kBuff =
                new MultipleDatabaseEntry(kList);

            Assert.IsTrue(kBuff.Recno);
            int val = 0;

            foreach (DatabaseEntry dbt in kBuff)
            {
                Assert.AreEqual(
                    BitConverter.GetBytes(kList[val]),
                    dbt.Data);
                val++;
            }
            Assert.AreEqual(9, val);

            // Create bulk buffer for data.
            MultipleDatabaseEntry vBuff =
                new MultipleDatabaseEntry(vList, false);

            /*
             * Create recno bulk buffer from another recno bulk
             * buffer.
             */
            MultipleDatabaseEntry kBuff1 =
                new MultipleDatabaseEntry(kBuff.Data, kBuff.Recno);

            val = 0;
            foreach (DatabaseEntry dbt in kBuff1)
            {
                Assert.AreEqual(
                    BitConverter.GetBytes(kList[val]),
                    dbt.Data);
                val++;
            }
            Assert.AreEqual(9, val);

            // Bulk insert to database with key and value buffers.
            db.Put(kBuff, vBuff);
            Cursor cursor = db.Cursor();
            KeyValuePair <DatabaseEntry, DatabaseEntry> pair;

            val = 0;
            while (cursor.MoveNext())
            {
                pair = cursor.Current;
                Assert.AreEqual(
                    BitConverter.GetBytes(kList[val]),
                    pair.Key.Data);
                for (int i = 0; i < 520; i++)
                {
                    if (i < vList[val].Data.Length)
                    {
                        Assert.AreEqual(vList[val].Data[i],
                                        pair.Value.Data[i]);
                    }
                    else
                    {
                        // The pad byte is 0.
                        Assert.AreEqual(0, pair.Value.Data[i]);
                    }
                }
                Assert.IsFalse(cursor.MoveNextDuplicate());
                val++;
            }
            Assert.AreEqual(9, val);

            cursor.Close();
            db.Close();
        }
        private void PutMultiple(DatabaseEnvironment env)
        {
            List<DatabaseEntry> kList = new List<DatabaseEntry>();
            List<DatabaseEntry> vList = new List<DatabaseEntry>();
            BTreeDatabase db;
            DatabaseEntry key, value;
            Transaction txn;
            string dbFileName = (env == null) ? testHome +
                "/" + testName + ".db" : testName + ".db";
            int i;

            BTreeDatabaseConfig dbConfig = new BTreeDatabaseConfig();
            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            if (env != null) {
                dbConfig.Env = env;
                txn = env.BeginTransaction();
                db = BTreeDatabase.Open(
                    dbFileName, dbConfig, txn);
                txn.Commit();
            } else
                db = BTreeDatabase.Open(dbFileName, dbConfig);

            for (i = 0; i < 100; i++) {
                key = new DatabaseEntry(
                    BitConverter.GetBytes(i));
                value = new DatabaseEntry(
                    ASCIIEncoding.ASCII.GetBytes("data" + i +
                    Configuration.RandomString(512)));
                kList.Add(key);
                vList.Add(value);
            }

            // Create bulk buffer for non-recno based keys.
            MultipleDatabaseEntry keyBuff =
                new MultipleDatabaseEntry(kList, false);
            Assert.IsFalse(keyBuff.Recno);

            // Create bulk buffer for values.
            MultipleDatabaseEntry valBuff =
                new MultipleDatabaseEntry(vList, false);
            i = 0;
            foreach (DatabaseEntry dbt in valBuff) {
                Assert.AreEqual(vList[i].Data, dbt.Data);
                i++;
            }
            Assert.AreEqual(100, i);

            // Create bulk buffer from another key buffer.
            MultipleDatabaseEntry keyBuff1 =
                new MultipleDatabaseEntry(
                keyBuff.Data, keyBuff.Recno);
            i = 0;
            foreach (DatabaseEntry dbt in keyBuff1) {
                Assert.AreEqual(kList[i].Data, dbt.Data);
                i++;
            }
            Assert.AreEqual(100, i);

            if (env != null) {
                txn = env.BeginTransaction();
                db.Put(keyBuff, valBuff, txn);
                Assert.AreEqual(100, db.Truncate(txn));
                txn.Commit();
            } else {
                /*
                 * Bulk insert to database with key and value
                 * buffers.
                 */
                db.Put(keyBuff, valBuff);

                // Verify all records exist as expected.
                Cursor cursor = db.Cursor();
                i = 99;
                Assert.IsTrue(cursor.MoveLast());
                Assert.AreEqual(kList[i].Data,
                    cursor.Current.Key.Data);
                Assert.AreEqual(vList[i].Data,
                    cursor.Current.Value.Data);
                while (cursor.MovePrev()) {
                    i--;
                    Assert.AreEqual(kList[i].Data,
                        cursor.Current.Key.Data);
                    Assert.AreEqual(vList[i].Data,
                        cursor.Current.Value.Data);
                }
                Assert.AreEqual(0, i);
                cursor.Close();
                /*
                 * Dumped all records. The number of records
                 * should be 100.
                 */
                Assert.AreEqual(100, db.Truncate());

                /*
                 * Bulk insert to database with a copied key
                 * buffer and a value buffer.
                 */
                db.Put(keyBuff1, valBuff);
                cursor = db.Cursor();
                Assert.IsTrue(cursor.MoveLast());
                i = 99;
                Assert.AreEqual(kList[i].Data,
                    cursor.Current.Key.Data);
                Assert.AreEqual(vList[i].Data,
                    cursor.Current.Value.Data);
                while (cursor.MovePrev()) {
                    i--;
                    Assert.AreEqual(kList[i].Data,
                        cursor.Current.Key.Data);
                    Assert.AreEqual(vList[i].Data,
                        cursor.Current.Value.Data);
                }
                cursor.Close();
                Assert.AreEqual(0, i);
                /*
                 * Dumped all records. The number of records
                 * should be 100.
                 */
                Assert.AreEqual(100, db.Truncate());
            }

            db.Close();
        }
Exemplo n.º 13
0
 /// <summary>
 /// Send a message on the message channel. The message is sent 
 /// synchronously. The method blocks waiting for a response before
 /// returning. If a response is not received within the timeout value
 /// configured for this request, this method returns with an error
 /// condition.
 /// </summary>
 /// <remarks>
 /// <para>
 /// To avoid block while waiting for a response from a remote site,
 /// use <see cref="SendMessage"/>
 /// </para>
 /// <para>
 /// The message sent by this method is received and handled at remote
 /// sites using a message dispatch callback, which is configured using
 /// <see cref="DatabaseEnvironment.RepMessageDispatch"/>
 /// </para>
 /// </remarks>
 /// <param name="request">
 /// DatabaseEntry objects array. Any flags for the DatabaseEntry objects
 /// are ignored.
 /// </param>
 /// <param name="bufferSize">Size of bulk buffer</param>
 /// <param name="timeout">
 /// The amount of time that may elapse while this method waits for a 
 /// response from the remote site. The timeout value must be specified
 /// as an unsigned 32-bit number of microseconds, limiting the maximum
 /// timeout to roughly 71 minutes. A timeout value of 0 indicates that
 /// the channel's default timeout value should be used. This default is
 /// configured using <see cref="Timeout"/>.
 /// </param>
 /// <returns>Multiple responses from the remote site.</returns>
 public MultipleDatabaseEntry SendRequest(
     DatabaseEntry[] request, int bufferSize, uint timeout)
 {
     int size = request.Length;
     IntPtr[] dbts = new IntPtr[size];
     for (int i = 0; i < size; i++)
         dbts[i] = DBT.getCPtr(DatabaseEntry.getDBT(request[i])).Handle;
     DatabaseEntry data = new DatabaseEntry();
     data.UserData = new byte[bufferSize];
     channel.send_request(dbts, (uint)size, data, timeout,
         DbConstants.DB_MULTIPLE);
     MultipleDatabaseEntry response = new MultipleDatabaseEntry(data);
     return response;
 }