Example #1
0
        public void SuccessRate_BloomFilters_2(int count, double expectedSuccessRate, long?version)
        {
            using (var context = new TransactionOperationContext(Env, 1024, 1024, 32 * 1024, SharedMultipleUseFlag.None))
            {
                var added = 0;

                SetUpCollectionOfBloomFilters(context, version);

                using (var tx = context.OpenWriteTransaction())
                {
                    using (var filter = CollectionOfBloomFilters.Load(CollectionOfBloomFilters.Mode.X64, context))
                    {
                        for (var i = 0; i < count; i++)
                        {
                            var key = context.GetLazyString($"orders/{i:D19}-A");
                            if (filter.Add(key))
                            {
                                added++;
                            }
                        }
                    }
                }

                var successRate = added / (double)count;
                Assert.True(successRate >= expectedSuccessRate, $"{successRate} >= {expectedSuccessRate}");
            }
        }
Example #2
0
        private void SetVersion(TransactionOperationContext context, long?version)
        {
            using (var tx = context.OpenWriteTransaction())
            {
                var tree = tx.InnerTransaction.ReadTree("BloomFilters");

                if (version.HasValue)
                {
                    tree.Add(CollectionOfBloomFilters.VersionSlice, version.Value);
                }
                else
                {
                    tree.Delete(CollectionOfBloomFilters.VersionSlice);
                }

                tx.Commit();
            }

            using (var tx = context.OpenWriteTransaction())
            {
                using (var filter = CollectionOfBloomFilters.Load(CollectionOfBloomFilters.Mode.X64, context))
                {
                    Assert.Equal(version ?? BloomFilterVersion.BaseVersion, filter.Version);
                }

                tx.Commit();
            }
        }
Example #3
0
        public void WillExpand()
        {
            using (var context = new TransactionOperationContext(Env, 1024, 1024, SharedMultipleUseFlag.None))
            {
                using (var tx = context.OpenWriteTransaction())
                {
                    using (var collection = CollectionOfBloomFilters.Load(CollectionOfBloomFilters.Mode.X86, context))
                    {
                        Assert.Equal(1, collection.Count);

                        for (var i = 0; i < CollectionOfBloomFilters.BloomFilter32.MaxCapacity * 1.2; i++)
                        {
                            var key = context.GetLazyString("orders/" + i);
                            collection.Add(key);
                        }

                        Assert.Equal(2, collection.Count);
                    }

                    tx.Commit();
                }

                using (context.OpenWriteTransaction())
                {
                    using (var collection = CollectionOfBloomFilters.Load(CollectionOfBloomFilters.Mode.X86, context))
                    {
                        Assert.Equal(2, collection.Count);
                    }
                }
            }
        }
Example #4
0
        public void CheckReadonly()
        {
            using (var context = new TransactionOperationContext(Env, 1024, 1024, SharedMultipleUseFlag.None))
            {
                using (var tx = context.OpenWriteTransaction())
                {
                    var tree = context.Transaction.InnerTransaction.CreateTree("BloomFilters");
                    tree.Increment("Count64", 2);

                    using (var filter = new CollectionOfBloomFilters.BloomFilter64(0, tree, writable: true, allocator: context.Allocator))
                    {
                        Assert.False(filter.ReadOnly);
                        filter.MakeReadOnly();
                        Assert.True(filter.ReadOnly);
                    }

                    tx.Commit();
                }

                using (context.OpenWriteTransaction())
                {
                    using (var collection = CollectionOfBloomFilters.Load(CollectionOfBloomFilters.Mode.X64, context))
                    {
                        Assert.Equal(2, collection.Count);

                        Assert.True(collection[0].ReadOnly);
                        Assert.False(collection[1].ReadOnly);
                    }
                }
            }
        }
Example #5
0
        public void WillGetConsumedFromStorage()
        {
            using (var context = new TransactionOperationContext(Env, 1024, 1024, 32 * 1024, SharedMultipleUseFlag.None))
            {
                using (var tx = context.OpenWriteTransaction())
                {
                    using (var collection = CollectionOfBloomFilters.Load(CollectionOfBloomFilters.Mode.X86, context))
                    {
                        var i = 0;
                        while (collection.Consumed == false)
                        {
                            var key = context.GetLazyString($"orders/{i++}");
                            collection.Add(key);
                        }

                        Assert.Equal(20, collection.Count);
                        collection.Flush();
                        Assert.Equal(0, collection.Count);
                    }

                    tx.Commit();
                }

                using (context.OpenWriteTransaction())
                {
                    using (var collection = CollectionOfBloomFilters.Load(CollectionOfBloomFilters.Mode.X86, context))
                    {
                        Assert.Equal(0, collection.Count);
                        Assert.True(collection.Consumed);
                    }
                }
            }
        }
Example #6
0
        public void WillExpand()
        {
            using (var context = new TransactionOperationContext(Env, 1024, 1024))
            {
                var key1 = context.GetLazyString("orders/1");
                var key2 = context.GetLazyString("orders/2");

                using (var tx = context.OpenWriteTransaction())
                {
                    var collection = CollectionOfBloomFilters.Load(2, context);
                    Assert.Equal(1, collection.Count);

                    Assert.True(collection.Add(key1));
                    Assert.Equal(1, collection.Count);

                    Assert.True(collection.Add(key2));
                    Assert.Equal(2, collection.Count);

                    tx.Commit();
                }

                using (context.OpenWriteTransaction())
                {
                    var collection = CollectionOfBloomFilters.Load(2, context);
                    Assert.Equal(2, collection.Count);
                }
            }
        }
Example #7
0
        public void CheckReadonly()
        {
            Slice key1, key2;

            using (var context = new TransactionOperationContext(Env, 1024, 1024))
                using (Slice.From(context.Allocator, "0000", out key1))
                    using (Slice.From(context.Allocator, "0001", out key2))
                    {
                        using (var tx = context.OpenWriteTransaction())
                        {
                            var tree = context.Transaction.InnerTransaction.CreateTree("IndexedDocs");
                            var ptr  = tree.DirectAdd(key1, CollectionOfBloomFilters.BloomFilter.PtrSize); // filter 1
                            tree.DirectAdd(key2, CollectionOfBloomFilters.BloomFilter.PtrSize);            // filter 2

                            var filter = new CollectionOfBloomFilters.BloomFilter(key1, ptr, tree, writeable: true);
                            Assert.False(filter.ReadOnly);
                            filter.MakeReadOnly();
                            Assert.True(filter.ReadOnly);

                            tx.Commit();
                        }

                        using (context.OpenWriteTransaction())
                        {
                            var collection = CollectionOfBloomFilters.Load(1024, context);
                            Assert.Equal(2, collection.Count);

                            Assert.True(collection[0].ReadOnly);
                            Assert.False(collection[1].ReadOnly);
                        }
                    }
        }
Example #8
0
        public void BloomFilter_Version()
        {
            using (var context = new TransactionOperationContext(Env, 1024, 1024, 32 * 1024, SharedMultipleUseFlag.None))
            {
                using (var tx = context.OpenWriteTransaction())
                {
                    using (var filter = CollectionOfBloomFilters.Load(CollectionOfBloomFilters.Mode.X64, context))
                    {
                        Assert.Equal(BloomFilterVersion.CurrentVersion, filter.Version);
                    }

                    tx.Commit();
                }

                using (var tx = context.OpenWriteTransaction())
                {
                    using (var filter = CollectionOfBloomFilters.Load(CollectionOfBloomFilters.Mode.X64, context))
                    {
                        Assert.Equal(BloomFilterVersion.CurrentVersion, filter.Version);
                    }

                    tx.Commit();
                }

                using (var tx = context.OpenWriteTransaction())
                {
                    using (var filter = CollectionOfBloomFilters.Load(CollectionOfBloomFilters.Mode.X64, context))
                    {
                        Assert.Equal(BloomFilterVersion.CurrentVersion, filter.Version);
                    }
                }

                SetVersion(context, BloomFilterVersion.BaseVersion);

                using (var tx = context.OpenWriteTransaction())
                {
                    using (var filter = CollectionOfBloomFilters.Load(CollectionOfBloomFilters.Mode.X64, context))
                    {
                        Assert.Equal(BloomFilterVersion.BaseVersion, filter.Version);
                    }
                }

                SetVersion(context, version: null);

                using (var tx = context.OpenWriteTransaction())
                {
                    using (var filter = CollectionOfBloomFilters.Load(CollectionOfBloomFilters.Mode.X64, context))
                    {
                        Assert.Equal(BloomFilterVersion.BaseVersion, filter.Version);
                    }
                }
            }
        }
Example #9
0
        private void SetUpCollectionOfBloomFilters(TransactionOperationContext context, long?version)
        {
            using (var tx = context.OpenWriteTransaction())
            {
                using (var filter = CollectionOfBloomFilters.Load(CollectionOfBloomFilters.Mode.X64, context))
                {
                }

                tx.Commit();
            }

            SetVersion(context, version);
        }
Example #10
0
        public void CannotMixFilters()
        {
            using (var context = new TransactionOperationContext(Env, 1024, 1024, SharedMultipleUseFlag.None))
            {
                using (var tx = context.OpenWriteTransaction())
                {
                    using (var collection = CollectionOfBloomFilters.Load(CollectionOfBloomFilters.Mode.X86, context))
                    {
                        Assert.Equal(1, collection.Count);

                        collection.AddFilter(collection.CreateNewFilter(1, CollectionOfBloomFilters.Mode.X86)); // should not throw

                        Assert.Throws <InvalidOperationException>(() => collection.AddFilter(collection.CreateNewFilter(2, CollectionOfBloomFilters.Mode.X64)));
                    }
                }
            }
        }