示例#1
0
        public void Can_put_with_tag()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    actions.Put(new PutRequest
                    {
                        Bytes = new byte[] { 1, 2, 3, 4 },
                        Key   = "test",
                        Tag   = 1
                    });

                    actions.Commit();
                });

                table.Batch(actions =>
                {
                    var values = actions.Get(new GetRequest()
                    {
                        Key = "test",
                    });

                    Assert.Equal(1, values[0].Tag);

                    actions.Commit();
                });
            }
        }
示例#2
0
        public void Save_several_items_for_same_version_will_save_all_of_them()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    actions.Put(new PutRequest
                    {
                        Key            = "test",
                        ParentVersions = new ValueVersion[0],
                        Bytes          = new byte[] { 1 }
                    });
                    actions.Put(new PutRequest
                    {
                        Key            = "test",
                        ParentVersions = new ValueVersion[0],
                        Bytes          = new byte[] { 2 }
                    });
                    Value[] values = actions.Get(new GetRequest
                    {
                        Key = "test"
                    });
                    Assert.Equal(2, values.Length);

                    Assert.Equal(1, values[0].Version.Number);
                    Assert.Equal(new byte[] { 1 }, values[0].Data);

                    Assert.Equal(2, values[1].Version.Number);
                    Assert.Equal(new byte[] { 2 }, values[1].Data);
                });
            }
        }
示例#3
0
        public void Can_check_existance_of_tag()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    actions.Put(new PutRequest
                    {
                        Bytes = new byte[] { 1, 2, 3, 4 },
                        Key   = "test",
                        Tag   = 1
                    });

                    actions.Commit();
                });

                table.Batch(actions =>
                {
                    Assert.True(actions.HasTag(1));
                    Assert.False(actions.HasTag(2));

                    actions.Commit();
                });
            }
        }
示例#4
0
        public void Can_use_optimistic_concurrency()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    actions.Put(new PutRequest
                    {
                        Key            = "test",
                        ParentVersions = new ValueVersion[0],
                        Bytes          = new byte[] { 1 }
                    });
                    PutResult put = actions.Put(new PutRequest
                    {
                        Key                   = "test",
                        ParentVersions        = new ValueVersion[0],
                        Bytes                 = new byte[] { 2 },
                        OptimisticConcurrency = true
                    });
                    Assert.True(put.ConflictExists);

                    actions.Commit();

                    Assert.Equal(1, actions.Get(new GetRequest
                    {
                        Key = "test"
                    }).Length);
                });
            }
        }
示例#5
0
        public void Writing_identical_data_to_existing_vlaue_will_not_create_conflict()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    PutResult put1 = actions.Put(new PutRequest
                    {
                        Key            = "test",
                        ParentVersions = new ValueVersion[0],
                        Bytes          = new byte[] { 1 }
                    });
                    PutResult put2 = actions.Put(new PutRequest
                    {
                        Key            = "test",
                        ParentVersions = new ValueVersion[0],
                        Bytes          = new byte[] { 1 }
                    });
                    Value[] values = actions.Get(new GetRequest {
                        Key = "test"
                    });
                    Assert.Equal(1, values.Length);
                    Assert.Equal(put1.Version.Number, put2.Version.Number);
                    Assert.Equal(put1.Version.InstanceId, put2.Version.InstanceId);
                });
            }
        }
示例#6
0
        public void Can_get_item_in_specific_version()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    PutResult version1 = actions.Put(new PutRequest
                    {
                        Key            = "test",
                        ParentVersions = new ValueVersion[0],
                        Bytes          = new byte[] { 1 }
                    });
                    actions.Put(new PutRequest
                    {
                        Key            = "test",
                        ParentVersions = new ValueVersion[0],
                        Bytes          = new byte[] { 2 }
                    });
                    Value[] value = actions.Get(new GetRequest
                    {
                        Key = "test",
                        SpecifiedVersion = version1.Version
                    });
                    Assert.Equal(new byte[] { 1 }, value[0].Data);
                });
            }
        }
        public void After_item_expires_it_will_be_removed_on_next_commit()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    actions.Put(new PutRequest
                    {
                        Key = "abc1",
                        ParentVersions = new ValueVersion[0],
                        Bytes = new byte[] { 1 },
                        ExpiresAt = DateTime.Now,
                    });

                    actions.Commit();
                });

                table.Batch(actions => actions.Commit());

                int numRecords = -1;
                table.Batch(actions =>
                {
                    Api.JetSetCurrentIndex(actions.Session, actions.Keys, null); //primary
                    Api.JetIndexRecordCount(actions.Session, actions.Keys, out numRecords, 0);
                });

                Assert.Equal(0, numRecords);
            }
        }
        public DistributedHashTable(
            string database,
            Uri url,
            IEndpointRouter endpointRouter,
            IServiceBus bus,
            Node metadata)
        {
            Url = url;
            this.endpointRouter = endpointRouter;
            this.bus = bus;
            Metadata = metadata;

            if (Metadata != null) // sole node in the network, probably
            {
                Metadata.ExecuteSync(uri =>
                {
                    ServiceUtil.Execute<IDistributedHashTableMetaDataProvider>(uri, srv =>
                    {
                        failOver = srv.GetNodeByUri(url);
                    });
                });
            }
            try
            {
                hashTable = new PersistentHashTable(database);
                hashTable.Initialize();
            }
            catch (Exception)
            {
                hashTable.Dispose();
                throw;
            }
        }
示例#9
0
        public void FillUpTableTest()
        {
            const string        tableName      = "FillUpHashTable";
            const int           tableSize      = 25;
            const int           keySize        = 4;
            const int           valueSize      = 4;
            const int           userHeaderSize = 6;
            PersistentHashTable hashTable      = InitTable(tableName, tableSize, keySize, valueSize, userHeaderSize);

            try
            {
                // fill the hash table
                for (int i = 1; i < tableSize + 1; i++)
                {
                    hashTable.Put(i.ToBytes(), i.ToBytes());
                }

                //add one more than allowed
                try
                {
                    hashTable.Put(new byte[] { byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue }, new byte[] { byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue });
                    Assert.Fail("Should throw exception");
                }
                catch (IndexOutOfRangeException) {}
            }
            finally
            {
                hashTable.Close();
            }
        }
示例#10
0
        public void Can_get_hash_from_cache()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    byte[] bytes = Encoding.UTF8.GetBytes("abcdefgiklmnqrstwxyz");
                    actions.Put(new PutRequest
                    {
                        Key            = "test",
                        ParentVersions = new ValueVersion[0],
                        Bytes          = bytes
                    });
                    Value[] values = actions.Get(new GetRequest
                    {
                        Key = "test"
                    });
                    Assert.Equal(
                        SHA256.Create().ComputeHash(bytes),
                        values[0].Sha256Hash
                        );
                });
            }
        }
示例#11
0
        public void Cannot_query_with_partial_item_key()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    actions.Put(new PutRequest
                    {
                        Key            = "abc1",
                        ParentVersions = new ValueVersion[0],
                        Bytes          = new byte[] { 1 }
                    });
                    Value[] values = actions.Get(new GetRequest {
                        Key = "abc10"
                    });
                    Assert.Equal(0, values.Length);

                    values = actions.Get(new GetRequest {
                        Key = "abc1"
                    });
                    Assert.NotEqual(0, values.Length);
                });
            }
        }
示例#12
0
        public void Can_check_existance_of_tag()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    actions.Put(new PutRequest
                    {
                        Bytes = new byte[] { 1, 2, 3, 4 },
                        Key = "test",
                        Tag = 1
                    });

                    actions.Commit();
                });

                table.Batch(actions =>
                {
                    Assert.True(actions.HasTag(1));
                    Assert.False(actions.HasTag(2));

                    actions.Commit();
                });
            }
        }
示例#13
0
        public void RemoveTest()
        {
            const int           keySize   = 4;
            const int           valueSize = 4;
            PersistentHashTable hashTable = InitTable("HashTableRemove", 15, keySize, valueSize, 6);

            try
            {
                RemoveTestAssert(hashTable, valueSize, new byte[keySize] {
                    1, 2, 3, 4
                });
                RemoveTestAssert(hashTable, valueSize, new byte[keySize] {
                    1, 0, 0, 0
                });
                RemoveTestAssert(hashTable, valueSize, new byte[keySize] {
                    0, 0, 0, 1
                });
                RemoveTestAssert(hashTable, valueSize, new byte[keySize] {
                    byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue
                });
            }
            finally
            {
                hashTable.Close();
            }
        }
示例#14
0
        public void Can_remove_without_version()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    actions.Put(new PutRequest
                    {
                        Bytes          = new byte[] { 1, 2, },
                        Key            = "test",
                        ParentVersions = new ValueVersion[0]
                    });


                    actions.Commit();
                });

                table.Batch(actions =>
                {
                    actions.Remove(new RemoveRequest {
                        Key = "test"
                    });
                    Value[] values = actions.Get(new GetRequest {
                        Key = "test"
                    });

                    Assert.Empty(values);
                    actions.Commit();
                });
            }
        }
示例#15
0
        public void When_setting_the_same_value_on_the_key_twice_will_not_create_new_version()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    PutResult version1 = actions.Put(new PutRequest
                    {
                        Key            = "abc1",
                        ParentVersions = new ValueVersion[0],
                        Bytes          = new byte[] { 6 }
                    });

                    var version2 = actions.Put(new PutRequest
                    {
                        Key            = "abc1",
                        ParentVersions = new[] { version1.Version },
                        Bytes          = new byte[] { 6 }
                    });

                    Assert.False(version2.ConflictExists);
                    Assert.Equal(version1.Version.InstanceId, version2.Version.InstanceId);
                    Assert.Equal(version1.Version.Number, version2.Version.Number);
                });
            }
        }
示例#16
0
        public void will_generate_sha256_for_new_values()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    actions.Put(new PutRequest
                    {
                        Bytes          = new byte[] { 1, 2, },
                        Key            = "test",
                        ParentVersions = new ValueVersion[0]
                    });


                    actions.Commit();
                });

                table.Batch(actions =>
                {
                    Value[] values = actions.Get(new GetRequest {
                        Key = "test"
                    });

                    Assert.NotNull(values[0].Sha256Hash);
                    Assert.NotEmpty(values[0].Sha256Hash);
                    actions.Commit();
                });
            }
        }
示例#17
0
        public void After_item_expires_it_will_be_removed_on_next_commit()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    actions.Put(new PutRequest
                    {
                        Key            = "abc1",
                        ParentVersions = new ValueVersion[0],
                        Bytes          = new byte[] { 1 },
                        ExpiresAt      = DateTime.Now,
                    });

                    actions.Commit();
                });

                table.Batch(actions => actions.Commit());

                int numRecords = -1;
                table.Batch(actions =>
                {
                    Api.JetSetCurrentIndex(actions.Session, actions.Keys, null);                     //primary
                    Api.JetIndexRecordCount(actions.Session, actions.Keys, out numRecords, 0);
                });

                Assert.Equal(0, numRecords);
            }
        }
示例#18
0
        public void Can_insert_same_value_to_pht_in_two_seperate_instances()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    actions.Put(new PutRequest
                    {
                        Key   = "test",
                        Bytes = new byte[] { 1 }
                    });

                    actions.Commit();
                });
            }

            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    actions.Put(new PutRequest
                    {
                        Key   = "test",
                        Bytes = new byte[] { 1 }
                    });

                    actions.Commit();
                });
            }
        }
示例#19
0
        public void Can_remove_item_from_table_when_there_is_more_than_single_item()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                ValueVersion versionOfA = null, versionOfC = null;

                table.Batch(actions =>
                {
                    versionOfA = actions.Put(
                        new PutRequest
                    {
                        Key            = "a",
                        ParentVersions = new ValueVersion[0],
                        Bytes          = new byte[] { 1 }
                    }
                        ).Version;
                    actions.Put(new PutRequest
                    {
                        Key            = "b",
                        ParentVersions = new ValueVersion[0],
                        Bytes          = new byte[] { 1 }
                    });
                    versionOfC = actions.Put(
                        new PutRequest
                    {
                        Key            = "c",
                        ParentVersions = new ValueVersion[0],
                        Bytes          = new byte[] { 1 }
                    }).Version;
                    actions.Put(
                        new PutRequest
                    {
                        Key            = "d",
                        ParentVersions = new ValueVersion[0],
                        Bytes          = new byte[] { 1 }
                    });

                    actions.Commit();
                });

                table.Batch(actions =>
                {
                    bool removed = actions.Remove(new RemoveRequest
                    {
                        Key             = "a",
                        SpecificVersion = versionOfA
                    });
                    Assert.True(removed);
                    removed = actions.Remove(new RemoveRequest
                    {
                        Key             = "c",
                        SpecificVersion = versionOfC
                    });
                    Assert.True(removed);
                    actions.Commit();
                });
            }
        }
示例#20
0
        public void Can_get_timestamp_from_pht()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    actions.Put(new PutRequest
                    {
                        Bytes          = new byte[] { 1, 2, },
                        Key            = "test",
                        ParentVersions = new ValueVersion[0]
                    });


                    actions.Commit();
                });

                table.Batch(actions =>
                {
                    Value[] values = actions.Get(new GetRequest {
                        Key = "test"
                    });

                    Assert.NotEqual(DateTime.MinValue, values[0].Timestamp);

                    actions.Commit();
                });
            }
        }
示例#21
0
        public void Can_set_the_replication_version_and_get_it_back()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();
                Guid guid = Guid.NewGuid();
                table.Batch(actions =>
                {
                    PutResult result = actions.Put(new PutRequest
                    {
                        Key                = "test",
                        ParentVersions     = new ValueVersion[0],
                        Bytes              = new byte[] { 3 },
                        ReplicationVersion = new ValueVersion
                        {
                            InstanceId = guid,
                            Number     = 53
                        }
                    });

                    Assert.Equal(53, result.Version.Number);
                    Assert.Equal(guid, result.Version.InstanceId);

                    actions.Commit();
                });
            }
        }
示例#22
0
 private static void RemoveTestAssert(PersistentHashTable hashTable, int valueSize, byte[] key)
 {
     hashTable.Put(key, new byte[valueSize]);
     hashTable.Remove(key);
     try
     {
         hashTable.Get(key);
     }
     catch (KeyNotFoundException) {}
 }
示例#23
0
        public void CanPutDifferentKeysIntoPHTConcurrently()
        {
            var exceptions = new List <Exception>();

            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();
                int count = 0;

                const int threadCount = 50;
                for (int i = 0; i < threadCount; i++)
                {
                    new Thread(state =>
                    {
                        var j = (int)state;

                        try
                        {
                            for (int k = 0; k < 10; k++)
                            {
                                table.Batch(actions =>
                                {
                                    actions.Put(new PutRequest
                                    {
                                        Key   = k + "-" + j.ToString(),
                                        Bytes = new byte[] { 4, 3, 5 }
                                    });

                                    actions.Commit();
                                });
                            }
                        }
                        catch (Exception e)
                        {
                            lock (exceptions)
                                exceptions.Add(e);
                        }
                        finally
                        {
                            Interlocked.Increment(ref count);
                        }
                    }, i)
                    {
                        IsBackground = true
                    }.Start(i);
                }

                while (Thread.VolatileRead(ref count) != threadCount)
                {
                    Thread.Sleep(100);
                }

                Assert.Empty(exceptions);
            }
        }
示例#24
0
        public void CanPutDifferentKeysIntoPHTConcurrently()
        {
            var exceptions = new List<Exception>();
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();
                int count = 0;

                const int threadCount = 50;
                for (int i = 0; i < threadCount; i++)
                {
                    new Thread(state =>
                    {
                        var j = (int) state;

                        try
                        {
                            for (int k = 0; k < 10; k++)
                            {
                                table.Batch(actions =>
                                {
                                    actions.Put(new PutRequest
                                    {
                                        Key = k + "-" + j.ToString(),
                                        Bytes = new byte[] { 4, 3, 5 }
                                    });

                                    actions.Commit();
                                });
                            }
                        }
                        catch (Exception e)
                        {
                            lock(exceptions)
                                exceptions.Add(e);
                        }
                        finally
                        {
                            Interlocked.Increment(ref count);
                        }
                    },i)
                    {
                        IsBackground = true
                    }.Start(i);
                }

                while(Thread.VolatileRead(ref count) != threadCount)
                {
                    Thread.Sleep(100);
                }

                Assert.Empty(exceptions);
            }
        }
示例#25
0
        public void Can_query_for_item_history()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    PutResult version1 = actions.Put(new PutRequest
                    {
                        Key            = "abc1",
                        ParentVersions = new ValueVersion[0],
                        Bytes          = new byte[] { 6 }
                    });

                    actions.Put(new PutRequest
                    {
                        Key            = "abc1",
                        ParentVersions = new[] { version1.Version },
                        Bytes          = new byte[] { 1 }
                    });
                    actions.Put(new PutRequest
                    {
                        Key            = "abc1",
                        ParentVersions = new[] { version1.Version },
                        Bytes          = new byte[] { 2 }
                    });
                    actions.Put(new PutRequest
                    {
                        Key            = "abc1",
                        ParentVersions = new[]
                        {
                            new ValueVersion
                            {
                                InstanceId = version1.Version.InstanceId,
                                Number     = 3
                            },
                        },
                        Bytes = new byte[] { 3 }
                    });

                    Value[] values = actions.Get(new GetRequest {
                        Key = "abc1"
                    });
                    Assert.Equal(3, values.Length);

                    Assert.Equal(new[] { 1 }, values[0].ParentVersions.Select(x => x.Number).ToArray());
                    Assert.Equal(new[] { 1 }, values[1].ParentVersions.Select(x => x.Number).ToArray());
                    Assert.Equal(new[] { 3 }, values[2].ParentVersions.Select(x => x.Number).ToArray());
                });
            }
        }
示例#26
0
        public void CloseTest()
        {
            PersistentHashTable hashTable = InitTable("Close", 88, 3, 10, 6);

            try
            {
                hashTable.Close();
            }
            finally
            {
                hashTable.Close();
            }
        }
示例#27
0
        public void GetUserHeaderSizeTest()
        {
            const int           userHeaderSize = 6;
            PersistentHashTable hashTable      = InitTable("GetHashTableUserHeaderSize", 3, 2, 2, userHeaderSize);

            try
            {
                Assert.AreEqual(userHeaderSize, hashTable.GetUserHeaderSize());
            }
            finally
            {
                hashTable.Close();
            }
        }
        public void Can_get_item_in_specific_version()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    var version1 = actions.Put("test", new int[0], new byte[] { 1 });
                    actions.Put("test", new int[0], new byte[] { 2 });
                    var value = actions.Get("test", version1);
                    Assert.Equal(new byte[]{1}, value.Data);
                });
            }
        }
示例#29
0
        public void ExportToCsv_exports_database_table_contents_to_csv_file_to_specified_path()
        {
            var dbRootPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, testDatabase);
            var dbFilePath = Path.Combine(dbRootPath, Path.GetFileName(testDatabase));
            var exportPath = Path.Combine(dbRootPath, "export");

            //Clear previous test results.
            if (Directory.Exists(exportPath))
            {
                Directory.Delete(exportPath);
            }
            Directory.CreateDirectory(exportPath);

            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();
                table.Batch(actions =>
                {
                    actions.Put(new PutRequest
                    {
                        Key   = "abc1",
                        Bytes = new byte[] { 1 }
                    });
                    actions.Put(new PutRequest
                    {
                        Key   = "abc2",
                        Bytes = new byte[] { 2 }
                    });
                    actions.Commit();
                });
            }

            var export = new ExportToCsv(dbFilePath, exportPath);

            export.Execute();

            Assert.True(Directory.Exists(exportPath));
            Assert.True(File.Exists(Path.Combine(exportPath, "keys.csv")));
            Assert.True(File.Exists(Path.Combine(exportPath, "data.csv")));
            Assert.True(File.Exists(Path.Combine(exportPath, "details.csv")));
            Assert.True(File.Exists(Path.Combine(exportPath, "lists.csv")));

            var linesInKeys = File.ReadAllLines(Path.Combine(exportPath, "keys.csv"));
            var linesInData = File.ReadAllLines(Path.Combine(exportPath, "data.csv"));

            Assert.True(linesInKeys.Length > 0);
            Assert.True(linesInData.Length > 0);
        }
        public void After_resolving_conflict_will_remove_old_version_of_data()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    PutResult version1 = actions.Put(new PutRequest
                    {
                        Key = "test",
                        ParentVersions = new ValueVersion[0],
                        Bytes = new byte[] { 1 }
                    });
                    PutResult version2 = actions.Put(new PutRequest
                    {
                        Key = "test",
                        ParentVersions = new ValueVersion[0],
                        Bytes = new byte[] { 2 }
                    });
                    Value[] value = actions.Get(new GetRequest
                    {
                        Key = "test",
                        SpecifiedVersion = version1.Version
                    });
                    Assert.Equal(new byte[] { 1 }, value[0].Data);

                    actions.Put(new PutRequest
                    {
                        Key = "test",
                        ParentVersions = new[]
                        {
                            version1.Version,
                            version2.Version
                        },
                        Bytes = new byte[] { 3 }
                    });

                    actions.Commit();

                    Assert.Empty(actions.Get(new GetRequest
                    {
                        Key = "test",
                        SpecifiedVersion = version1.Version
                    }));
                });
            }
        }
示例#31
0
        public void After_resolving_conflict_will_remove_old_version_of_data()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    PutResult version1 = actions.Put(new PutRequest
                    {
                        Key            = "test",
                        ParentVersions = new ValueVersion[0],
                        Bytes          = new byte[] { 1 }
                    });
                    PutResult version2 = actions.Put(new PutRequest
                    {
                        Key            = "test",
                        ParentVersions = new ValueVersion[0],
                        Bytes          = new byte[] { 2 }
                    });
                    Value[] value = actions.Get(new GetRequest
                    {
                        Key = "test",
                        SpecifiedVersion = version1.Version
                    });
                    Assert.Equal(new byte[] { 1 }, value[0].Data);

                    actions.Put(new PutRequest
                    {
                        Key            = "test",
                        ParentVersions = new[]
                        {
                            version1.Version,
                            version2.Version
                        },
                        Bytes = new byte[] { 3 }
                    });

                    actions.Commit();

                    Assert.Empty(actions.Get(new GetRequest
                    {
                        Key = "test",
                        SpecifiedVersion = version1.Version
                    }));
                });
            }
        }
示例#32
0
        public void Can_add_item_to_list()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    var id = actions.AddItem(new AddItemRequest
                    {
                        Data = new byte[] { 1, 2, 4 },
                        Key  = "test"
                    });
                });
            }
        }
示例#33
0
        public void Can_add_item_to_list()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    var id = actions.AddItem(new AddItemRequest
                    {
                        Data = new byte[] { 1, 2, 4 },
                        Key = "test"
                    });
                });
            }
        }
示例#34
0
        private PersistentHashTable InitTable(string hashtableName, int tableSize, int keySize, int valueSize, int userHeaderSize)
        {
            PersistentHashTable ht;

            try
            {
                ht = new PersistentHashTable(hashtableName, tableSize, keySize, valueSize, userHeaderSize);
            }
            catch (FileNameConflictException)
            {
                ht = new PersistentHashTable(hashtableName);
                ht.Delete();
                ht = new PersistentHashTable(hashtableName, tableSize, keySize, valueSize, userHeaderSize);
            }
            return(ht);
        }
示例#35
0
        public void Can_get_all_rows_with_tag()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    actions.Put(new PutRequest
                    {
                        Bytes = new byte[] { 1, 2, 3, 4 },
                        Key   = "test",
                        Tag   = 1
                    });

                    actions.Put(new PutRequest
                    {
                        Bytes = new byte[] { 1, 2, 3, 4 },
                        Key   = "test2",
                        Tag   = 1
                    });

                    actions.Put(new PutRequest
                    {
                        Bytes = new byte[] { 1, 2, 3, 4 },
                        Key   = "test3",
                        Tag   = 2
                    });

                    actions.Commit();
                });

                table.Batch(actions =>
                {
                    var results = actions.GetKeysForTag(1).ToArray();
                    Assert.Equal(2, results.Length);
                    Assert.Equal("test", results[0].Key);
                    Assert.Equal("test2", results[1].Key);

                    results = actions.GetKeysForTag(2).ToArray();
                    Assert.Equal(1, results.Length);
                    Assert.Equal("test3", results[0].Key);

                    actions.Commit();
                });
            }
        }
示例#36
0
        public void Can_get_all_rows_with_tag()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    actions.Put(new PutRequest
                    {
                        Bytes = new byte[] { 1, 2, 3, 4 },
                        Key = "test",
                        Tag = 1
                    });

                    actions.Put(new PutRequest
                    {
                        Bytes = new byte[] { 1, 2, 3, 4 },
                        Key = "test2",
                        Tag = 1
                    });

                    actions.Put(new PutRequest
                    {
                        Bytes = new byte[] { 1, 2, 3, 4 },
                        Key = "test3",
                        Tag = 2
                    });

                    actions.Commit();
                });

                table.Batch(actions =>
                {
                    var results = actions.GetKeysForTag(1).ToArray();
                    Assert.Equal(2, results.Length);
                    Assert.Equal("test", results[0].Key);
                    Assert.Equal("test2", results[1].Key);

                    results = actions.GetKeysForTag(2).ToArray();
                    Assert.Equal(1, results.Length);
                    Assert.Equal("test3", results[0].Key);

                    actions.Commit();
                });
            }
        }
        public void Cannot_query_with_partial_item_key()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    actions.Put("abc1", new int[0], new byte[] { 1 });
                    var values = actions.Get("abc10");
                    Assert.Equal(0, values.Length);

                    values = actions.Get("abc1");
                    Assert.NotEqual(0, values.Length);
                });
            }
        }
 public DistributedHashTable(string database, Action<InstanceParameters> configure)
 {
     try
     {
         hashTable = new PersistentHashTable(database)
         {
             Configure = configure
         };
         hashTable.Initialize();
     }
     catch (Exception)
     {
         if (hashTable != null)
             hashTable.Dispose();
         throw;
     }
 }
示例#39
0
        public void Unloading_app_domain_will_free_esent()
        {
            var domain = AppDomain.CreateDomain("test",null,new AppDomainSetup
            {
                ApplicationBase = AppDomain.CurrentDomain.BaseDirectory
            });
            var unwrap = (UncleanAndUnpleasant)domain.CreateInstanceAndUnwrap(
                typeof(UncleanAndUnpleasant).Assembly.GetName().Name,
                typeof(UncleanAndUnpleasant).FullName);
            unwrap.Start();

            AppDomain.Unload(domain);

            using(var table = new PersistentHashTable("test.esent"))
            {
                table.Initialize();
            }
        }
示例#40
0
        public void CtorTest()
        {
            const int           tableSize = 88;
            const int           keySize   = 3;
            const int           valueSize = 10;
            PersistentHashTable hashTable = InitTable("ctor", tableSize, keySize, valueSize, 4);

            try
            {
                Assert.AreEqual(tableSize, hashTable.GetTableSize());
                Assert.AreEqual(keySize, hashTable.GetKeySize());
                Assert.AreEqual(valueSize, hashTable.GetValueSize());
            }
            finally
            {
                hashTable.Close();
            }
        }
示例#41
0
        public void RemoveCollisionTest()
        {
            PersistentHashTable hashTable = InitTable("HashTableRemove", 20, 4, 4, 6);

            try
            {
                //fill the table up with collisions
                byte[] lastCollisionKey = FillTableWithCollisions(5, hashTable);

                hashTable.Remove(lastCollisionKey);

                hashTable.Put(lastCollisionKey, new byte[hashTable.GetValueSize()]);
            }
            finally
            {
                hashTable.Close();
            }
        }
        public void After_resolving_conflict_will_remove_old_version_of_data()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    var version1 = actions.Put("test", new int[0], new byte[] { 1 });
                    actions.Put("test", new int[0], new byte[] { 2 });
                    var value = actions.Get("test", version1);
                    Assert.Equal(new byte[] { 1 }, value.Data);

                    actions.Put("test", new[] {1, 2}, new byte[] {3});

                    actions.Commit();

                    Assert.Null(actions.Get("test", version1));
                });
            }
        }
        public void After_item_expires_it_cannot_be_retrieved()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    actions.Put(new PutRequest
                    {
                        Key = "abc1",
                        ParentVersions = new ValueVersion[0],
                        Bytes = new byte[] { 1 },
                        ExpiresAt = DateTime.Now.AddYears(-1),
                        OptimisticConcurrency = false
                    });
                    Value[] values = actions.Get(new GetRequest { Key = "abc1" });

                    Assert.Equal(0, values.Length);
                });
            }
        }
示例#44
0
        public void Can_list_items()
        {
            int item2 = 0;
            int item1 = 0;
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    item1 = actions.AddItem(new AddItemRequest
                    {
                        Data = new byte[] { 1, 2, 4 },
                        Key = "test"
                    });
                    item2 = actions.AddItem(new AddItemRequest
                    {
                        Data = new byte[] { 5, 3, 1 },
                        Key = "test"
                    });

                    actions.Commit();
                });

                table.Batch(actions =>
                {
                    var items = actions.GetItems(new GetItemsRequest
                    {
                        Key = "test"
                    });
                    Assert.Equal(item1, items[0].Key);
                    Assert.Equal(item2, items[1].Key);

                    Assert.Equal(new byte[] { 1, 2, 4 }, items[0].Value);
                    Assert.Equal(new byte[] { 5, 3, 1 }, items[1].Value);
                });
            }
        }
示例#45
0
        public void Can_remove_items()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    int item1 = actions.AddItem(new AddItemRequest
                    {
                        Data = new byte[] { 1, 2, 4 },
                        Key = "test"
                    });
                    actions.AddItem(new AddItemRequest
                    {
                        Data = new byte[] { 3, 6, 4 },
                        Key = "test"
                    });
                     actions.RemoveItem(new RemoveItemRequest
                    {
                        Id = item1,
                        Key = "test"
                    });

                    actions.Commit();
                });

                table.Batch(actions =>
                {
                    var items = actions.GetItems(new GetItemsRequest
                    {
                        Key = "test"
                    });
                    Assert.Equal(1, items.Length);
                });
            }
        }
        public void ExportToCsv_exports_database_table_contents_to_csv_file_to_specified_path()
        {
            var dbRootPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, testDatabase);
            var dbFilePath = Path.Combine(dbRootPath, Path.GetFileName(testDatabase));
            var exportPath = Path.Combine(dbRootPath, "export");

            //Clear previous test results.
            if (Directory.Exists(exportPath))
                Directory.Delete(exportPath);
            Directory.CreateDirectory(exportPath);

            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();
                table.Batch(actions =>
                {
                    actions.Put(new PutRequest
                    {
                        Key = "abc1",
                        Bytes = new byte[] { 1 }
                    });
                    actions.Put(new PutRequest
                    {
                        Key = "abc2",
                        Bytes = new byte[] { 2 }
                    });
                    actions.Commit();
                });
            }

            var export = new ExportToCsv(dbFilePath, exportPath);
            export.Execute();

            Assert.True(Directory.Exists(exportPath));
            Assert.True(File.Exists(Path.Combine(exportPath, "keys.csv")));
            Assert.True(File.Exists(Path.Combine(exportPath, "data.csv")));
            Assert.True(File.Exists(Path.Combine(exportPath, "details.csv")));
            Assert.True(File.Exists(Path.Combine(exportPath, "lists.csv")));

            var linesInKeys = File.ReadAllLines(Path.Combine(exportPath, "keys.csv"));
            var linesInData = File.ReadAllLines(Path.Combine(exportPath, "data.csv"));
            Assert.True(linesInKeys.Length > 0);
            Assert.True(linesInData.Length > 0);
        }
        public void Id_of_table_is_persistent()
        {
            Guid id;
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                id = table.Id;
                Assert.NotEqual(Guid.Empty, id);
            }

            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                Assert.NotEqual(Guid.Empty, table.Id);
                Assert.Equal(id, table.Id);
            }
        }
        public void Can_remove_item_from_table_when_there_is_more_than_single_item()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                int versionOfA = 0, versionOfC = 0;

                table.Batch(actions =>
                {
                    versionOfA = actions.Put("a", new int[0], new byte[] {1});
                    actions.Put("b", new int[0], new byte[] { 1 });
                    versionOfC = actions.Put("c", new int[0], new byte[] {1});
                    actions.Put("d", new int[0], new byte[] { 1 });

                    actions.Commit();
                });

                table.Batch(actions =>
                {
                    var removed = actions.Remove("a", new []{ versionOfA });
                    Assert.True(removed);
                    removed = actions.Remove("c", new[] { versionOfC });
                    Assert.True(removed);
                    actions.Commit();
                });

            }
        }
        public void Interleaved()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    actions.Put(new PutRequest
                    {
                        Key = "abc1",
                        ParentVersions = new ValueVersion[0],
                        Bytes = new byte[] { 1 }
                    });

                    table.Batch(actions2 =>
                    {
                        actions2.Put(new PutRequest
                        {
                            Key = "dve",
                            ParentVersions = new ValueVersion[0],
                            Bytes = new byte[] { 3 }
                        });
                        actions2.Commit();
                    });

                    actions.Commit();
                });

                table.Batch(actions =>
                {
                    Assert.NotEmpty(actions.Get(new GetRequest { Key = "abc1" }));
                    Assert.NotEmpty(actions.Get(new GetRequest { Key = "dve" }));
                });
            }
        }
        public void Save_several_items_for_same_version_will_save_all_of_them()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    actions.Put(new PutRequest
                    {
                        Key = "test",
                        ParentVersions = new ValueVersion[0],
                        Bytes = new byte[] { 1 }
                    });
                    actions.Put(new PutRequest
                    {
                        Key = "test",
                        ParentVersions = new ValueVersion[0],
                        Bytes = new byte[] { 2 }
                    });
                    Value[] values = actions.Get(new GetRequest
                    {
                        Key = "test"
                    });
                    Assert.Equal(2, values.Length);

                    Assert.Equal(1, values[0].Version.Number);
                    Assert.Equal(new byte[] { 1 }, values[0].Data);

                    Assert.Equal(2, values[1].Version.Number);
                    Assert.Equal(new byte[] { 2 }, values[1].Data);
                });
            }
        }
        public void When_setting_the_same_value_on_the_key_twice_will_not_create_new_version()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    PutResult version1 = actions.Put(new PutRequest
                    {
                        Key = "abc1",
                        ParentVersions = new ValueVersion[0],
                        Bytes = new byte[] { 6 }
                    });

                    var version2 = actions.Put(new PutRequest
                    {
                        Key = "abc1",
                        ParentVersions = new[] { version1.Version },
                        Bytes = new byte[] { 6 }
                    });

                    Assert.False(version2.ConflictExists);
                    Assert.Equal(version1.Version.InstanceId, version2.Version.InstanceId);
                    Assert.Equal(version1.Version.Number, version2.Version.Number);
                });
            }
        }
        public void Can_save_and_load_item_from_cache()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    actions.Put("test", new int[0], new byte[] { 1 });
                    var values = actions.Get("test");
                    Assert.Equal(1, values[0].Version);
                    Assert.Equal(new byte[] { 1 }, values[0].Data);
                });
            }
        }
        public void Can_resolve_conflict()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    actions.Put("test", new int[0], new byte[] { 1 });
                    actions.Put("test", new int[0], new byte[] { 2 });
                    var values = actions.Get("test");
                    Assert.Equal(2, values.Length);
                    actions.Put("test", new[] {1, 2}, new byte[] {3});

                    values = actions.Get("test");
                    Assert.Equal(1, values.Length);
                    Assert.Equal(new byte[] {3}, values[0].Data);
                    Assert.Equal(3, values[0].Version);
                });
            }
        }
        public void will_generate_sha256_for_new_values()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    actions.Put(new PutRequest
                    {
                        Bytes = new byte[] { 1, 2, },
                        Key = "test",
                        ParentVersions = new ValueVersion[0]
                    });

                    actions.Commit();
                });

                table.Batch(actions =>
                {
                    Value[] values = actions.Get(new GetRequest { Key = "test" });

                    Assert.NotNull(values[0].Sha256Hash);
                    Assert.NotEmpty(values[0].Sha256Hash);
                    actions.Commit();
                });
            }
        }
        public void Writing_identical_data_to_existing_vlaue_will_not_create_conflict()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    PutResult put1 = actions.Put(new PutRequest
                    {
                        Key = "test",
                        ParentVersions = new ValueVersion[0],
                        Bytes = new byte[] { 1 }
                    });
                    PutResult put2 = actions.Put(new PutRequest
                    {
                        Key = "test",
                        ParentVersions = new ValueVersion[0],
                        Bytes = new byte[] { 1 }
                    });
                    Value[] values = actions.Get(new GetRequest { Key = "test" });
                    Assert.Equal(1, values.Length);
                    Assert.Equal(put1.Version.Number, put2.Version.Number);
                    Assert.Equal(put1.Version.InstanceId, put2.Version.InstanceId);
                });
            }
        }
示例#56
0
 public void Start()
 {
     table = new PersistentHashTable("test.esent");
     table.Initialize();
 }
示例#57
0
        public void will_get_items_from_current_list_only()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    actions.AddItem(new AddItemRequest
                    {
                        Data = new byte[] { 1, 2, 4 },
                        Key = "test"
                    });
                    actions.AddItem(new AddItemRequest
                    {
                        Data = new byte[] { 5, 3, 1 },
                        Key = "test"
                    });
                    actions.AddItem(new AddItemRequest
                    {
                        Data = new byte[] { 5, 3, 1 },
                        Key = "test2"
                    });
                    actions.Commit();
                });

                table.Batch(actions =>
                {
                    var items = actions.GetItems(new GetItemsRequest
                    {
                        Key = "test"
                    });
                    Assert.Equal(2, items.Length);
                });
            }
        }
        public void ExpireDataFromPhtByTimestamp_removes_data_whose_expiration_is_lesser_or_equal_than_specified()
        {
            var dbRootPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, testDatabase);
            var dbFilePath = Path.Combine(dbRootPath, Path.GetFileName(testDatabase));

            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();
                table.Batch(actions =>
                {
                    actions.Put(new PutRequest
                    {
                        Key = "expire1",
                        Bytes = new byte[] { 1 },
                        ReplicationTimeStamp = DateTime.Now.AddHours(-1)
                    });
                    var parent = actions.Put(new PutRequest
                    {
                        Key = "expire2",
                        Bytes = new byte[] { 2 },
                        ReplicationTimeStamp = DateTime.Now.AddMinutes(-20)
                    });

                    actions.Put(new PutRequest
                    {
                        Key = "expire2",
                        Bytes = new byte[] { 2, 1 },
                        ParentVersions = new[] { parent.Version },
                        ReplicationTimeStamp = DateTime.Now.AddMinutes(-10)
                    });
                    actions.Put(new PutRequest
                    {
                        Key = "shouldnotexpire",
                        Bytes = new byte[] { 3 },
                        ReplicationTimeStamp = DateTime.Now.AddMinutes(30)
                    });
                    actions.Commit();
                });
            }

            var expiryUtil = new ExpireDataFromPhtByTimestamp(dbFilePath, DateTime.Now);
            expiryUtil.Excute();

            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    var results1 = actions.Get(new GetRequest
                    {
                        Key = "expire1",
                        SpecifiedVersion = null
                    });
                    Assert.True(results1.Length == 0);

                    var results2 = actions.Get(new GetRequest
                    {
                        Key = "expire2",
                        SpecifiedVersion = null
                    });
                    Assert.True(results2.Length == 0);

                    var results3 = actions.Get(new GetRequest
                    {
                        Key = "shouldnotexpire",
                        SpecifiedVersion = null
                    });
                    Assert.True(results3.Length > 0);
                });
            }
        }
示例#59
0
        public void Can_put_with_tag()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    actions.Put(new PutRequest
                    {
                        Bytes = new byte[]{1,2,3,4},
                        Key = "test",
                        Tag = 1
                    });

                    actions.Commit();
                });

                table.Batch(actions =>
                {
                    var values = actions.Get(new GetRequest()
                    {
                        Key = "test",
                    });

                    Assert.Equal(1, values[0].Tag);

                    actions.Commit();
                });
            }
        }
        public void Save_several_items_for_same_version_will_save_all_of_them()
        {
            using (var table = new PersistentHashTable(testDatabase))
            {
                table.Initialize();

                table.Batch(actions =>
                {
                    actions.Put("test", new int[0], new byte[] { 1 });
                    actions.Put("test", new int[0], new byte[] { 2 });
                    var values = actions.Get("test");
                    Assert.Equal(2, values.Length);

                    Assert.Equal(1, values[0].Version);
                    Assert.Equal(new byte[] { 1 }, values[0].Data);

                    Assert.Equal(2, values[1].Version);
                    Assert.Equal(new byte[] { 2 }, values[1].Data);
                });
            }
        }