Exemplo n.º 1
0
        public void CanCreateBufferableFromMultiple()
        {
            var b = new Bufferable(42, Guid.NewGuid(), 1969L);

            Assert.Equal(42, BitConverter.ToInt32(b.Buffer, 0));
            Assert.Equal(1969L, BitConverter.ToInt64(b.Buffer, 4 + 16));
        }
Exemplo n.º 2
0
        public void CanSaveAndReadDup()
        {
            var key   = 42L;
            var value = Guid.NewGuid();
            var i     = 1969L;

            using (var db = _env.OpenDatabase(DatabaseName, new DatabaseConfig(DbFlags.Create | DbFlags.DuplicatesSort)
            {
                DupSortPrefix = 64
            }))
            {
                using (var tx = _env.BeginTransaction())
                {
                    tx.Put(db, key, new Bufferable(i, value), TransactionPutOptions.AppendDuplicateData);
                    tx.Put(db, key, new Bufferable(i + 1, value), TransactionPutOptions.AppendDuplicateData);
                    tx.Put(db, key, new Bufferable(i + 2, value), TransactionPutOptions.AppendDuplicateData);
                    tx.Put(db, key, new Bufferable(i + 3, value), TransactionPutOptions.AppendDuplicateData);
                    tx.Commit();
                }

                using (var tx = _env.BeginReadOnlyTransaction())
                {
                    Bufferable b = new Bufferable(i + 2);

                    var success = tx.TryGetDuplicate(db, key, ref b);
                    Assert.True(success);
                    Assert.Equal(i + 2, BitConverter.ToInt64(b.Buffer, 0));
                }
            }
        }
Exemplo n.º 3
0
        public void ItIsSafeToUseUnsafeCopy()
        {
            var r  = new Random();
            var bb = new byte[128];

            r.NextBytes(bb);
            var b  = new Bufferable(bb);
            var b2 = b.PrefixWithIndexAndTerm(42, 122);

            Assert.Equal(42, BitConverter.ToInt64(b2.Buffer, 0));
            Assert.Equal(122, BitConverter.ToInt64(b2.Buffer, 8));
            Assert.Equal(bb, b2.Buffer.Skip(16));
        }
Exemplo n.º 4
0
        public void CanDeleteUpTo()
        {
            var key   = 42L;
            var value = Guid.NewGuid();
            var total = 1000_000L;
            var upto  = 100_000L;

            using (var db = _env.OpenDatabase(DatabaseName, new DatabaseConfig(DbFlags.Create | DbFlags.DuplicatesSort)
            {
                DupSortPrefix = 64
            }))
            {
                using (var tx = _env.BeginTransaction())
                {
                    for (long i = 0; i < total; i++)
                    {
                        tx.Put(db, key, new Bufferable(i, value), TransactionPutOptions.AppendDuplicateData);
                    }

                    tx.Commit();
                }

                Assert.Equal(total, db.GetEntriesCount());

                using (var tx2 = _env.BeginTransaction())
                {
                    Assert.True(tx2.DeleteUpToValue(db, key, upto));
                    tx2.Commit();
                }

                Assert.Equal(total - upto, db.GetEntriesCount());

                using (var tx3 = _env.BeginReadOnlyTransaction())
                {
                    Bufferable checkValue = 1000L;
                    tx3.TryGetDuplicate(db, key, ref checkValue);

                    checkValue = 100_000L;
                    Assert.True(tx3.TryGetDuplicate(db, key, ref checkValue));
                }
            }
        }
Exemplo n.º 5
0
        public void CanSaveAndReadLong()
        {
            var key   = 42L;
            var value = 1969L;

            using (var db = _env.OpenDatabase(DatabaseName, new DatabaseConfig(DbFlags.Create)))
            {
                using (var tx = _env.BeginTransaction())
                {
                    tx.Put(db, key, value);
                    tx.Commit();
                }

                using (var tx = _env.BeginReadOnlyTransaction())
                {
                    Bufferable b       = default;
                    var        success = tx.TryGet(db, key, out b);
                    Assert.True(success);
                    Assert.Equal(value, (long)b);
                }
            }
        }
Exemplo n.º 6
0
        /// <inheritdocs/>
        public LogEntry[] GetEntries(long index, int count)
        {
            if (index < LogOffset)
            {
                throw new EntriesNotAvailableAnymoreException(index, LogOffset);
            }

            if (index + count - 1 > LastIndex)
            {
                throw new InvalidOperationException($"We do not have these entries yet. index: {index}, count: {count} and LastIndex: {LastIndex}");
            }

            var list = new LogEntry[count];

            using (var tx = _env.BeginReadOnlyTransaction())
            {
                for (long i = index; i < index + count; i++)
                {
                    Bufferable b = i;
                    if (!tx.TryGetDuplicate(_logDb, LogKey, ref b))
                    {
                        throw new InvalidOperationException($"Could not find index {i} in the logs.");
                    }
                    StoredLogEntry s = b.Buffer;
                    if (s.Index != i)
                    {
                        throw new InvalidDataException($"Corruption in the highest. Supposedly loaded {index} but came out {s.Index}");
                    }

                    list[i - index] = new LogEntry()
                    {
                        Body = s.Body,
                        Term = s.Term
                    };
                }
            }

            return(list);
        }
Exemplo n.º 7
0
        public void CanCreateBufferableFromOne()
        {
            var b = new Bufferable(1969L);

            Assert.Equal(1969L, BitConverter.ToInt64(b.Buffer, 0));
        }