public void BulkDiplicateTest()
 {
     using (var bdb = CreateDbDup())
     using (var cursor = new BerkeleyBulkDuplicateCursor(bdb, GetPageSize(bdb)))
     {
         bool blobExists;
         HashSet<KeyValuePair<int, DateTime>> data = BulkDuplicateWrite(cursor, out blobExists);
         BulkDuplicateRead(cursor, data, blobExists);
         data = BulkDuplicateDelete(cursor, data, ref blobExists);
         BulkDuplicateRead(cursor, data, blobExists);
     }
 }
        private static void BulkDuplicateRead(BerkeleyBulkDuplicateCursor cursor, HashSet<KeyValuePair<int, DateTime>> data, bool blobExists)
        {
            var checkData = new HashSet<KeyValuePair<int, DateTime>>(data);
            bool blobFound = false;
            DateTime d1 = new DateTime(2014, 1, 1);

            foreach (int keyIndex in data.Select(d => d.Key).Distinct())
            {
                Byte[] key = Encoding.UTF8.GetBytes(keyIndex.ToString());
                for (BerkeleyBulkEnumerator buffer = cursor.ReadAsync(key, BerkeleyDbOperation.DB_SET).Result;
                    !buffer.NotFound;
                    buffer = cursor.ReadAsync(null, BerkeleyDbOperation.DB_NEXT_DUP).Result)
                {
                    buffer.Error.ThrowIfError();

                    foreach (BerkeleyKeyValueBulk keyValue in buffer)
                    {
                        String value = Encoding.UTF8.GetString(keyValue.Value.ToArray());
                        DateTime d;
                        if (DateTime.TryParseExact(value, "dddd dd MMMM yyyy", null, DateTimeStyles.None, out d))
                            Assert.IsTrue(checkData.Remove(new KeyValuePair<int, DateTime>(keyIndex, d)), "key/value: " + "key" + keyIndex + "/" + value + " not exists");
                        else
                        {
                            blobFound = keyValue.Value.Count == 34123 && CheckByteArray(keyValue.Value);
                            Assert.IsTrue(blobFound, "blod data is bad");
                        }
                    }
                }
            }

            Assert.AreEqual(checkData.Count, 0, "not all values found");
            Assert.IsTrue(blobFound == blobExists, "blob value not found");
        }
        private static HashSet<KeyValuePair<int, DateTime>> BulkDuplicateWrite(BerkeleyBulkDuplicateCursor cursor, out bool blobExists)
        {
            var data = new HashSet<KeyValuePair<int, DateTime>>();

            DateTime d1 = new DateTime(2014, 1, 1);
            for (int i = 0; i < 1000; i++)
            {
                int keyIndex = (i / 100) + 1;
                Byte[] key = Encoding.UTF8.GetBytes(keyIndex.ToString());
                if (i == 500)
                    cursor.AddWrite(key, GenerateByteArray(34123));

                String svalue = d1.AddDays(i).ToString("dddd dd MMMM yyyy");
                Byte[] value = Encoding.UTF8.GetBytes(svalue);
                cursor.AddWrite(key, value);

                data.Add(new KeyValuePair<int, DateTime>(keyIndex, d1.AddDays(i)));
            }

            var error = cursor.WriteAsync(BerkeleyDbWriteMode.DB_OVERWRITE_DUP).Result;
            error.ThrowIfError();

            blobExists = true;
            return data;
        }
        private static HashSet<KeyValuePair<int, DateTime>> BulkDuplicateDelete(BerkeleyBulkDuplicateCursor cursor, HashSet<KeyValuePair<int, DateTime>> data, ref bool blobExists)
        {
            var checkData = new HashSet<KeyValuePair<int, DateTime>>();
            Byte[] key, value;
            foreach (KeyValuePair<int, DateTime> keyValue in data)
            {
                if ((keyValue.Value.Day % 3) == 0)
                {
                    key = Encoding.UTF8.GetBytes(keyValue.Key.ToString());
                    value = Encoding.UTF8.GetBytes(keyValue.Value.ToString("dddd dd MMMM yyyy"));
                    cursor.AddDelete(key, value);
                }
                else
                    checkData.Add(keyValue);
            }

            key = Encoding.UTF8.GetBytes((500 / 100 + 1).ToString());
            value = GenerateByteArray(34123);
            cursor.AddDelete(key, value);

            BerkeleyError error = cursor.DeleteAsync().Result;
            error.ThrowIfError();
            blobExists = false;
            return checkData;
        }