Esempio n. 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();
                });
            }
        }
        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);
            }
        }
Esempio n. 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();
                });
            }
        }
Esempio n. 4
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();
                });
            }
        }
Esempio n. 5
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();
                });
            }
        }
Esempio n. 6
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);
                });
            }
        }
        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;
            }
        }
Esempio n. 8
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);
                });
            }
        }
Esempio n. 9
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);
                });
            }
        }
Esempio n. 10
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);
                });
            }
        }
Esempio n. 11
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
                        );
                });
            }
        }
Esempio n. 12
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();
                });
            }
        }
Esempio n. 13
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);
                });
            }
        }
Esempio n. 14
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();
                });
            }
        }
Esempio n. 15
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();
                });
            }
        }
Esempio n. 16
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();
                });
            }
        }
Esempio n. 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);
            }
        }
Esempio n. 18
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();
                });
            }
        }
Esempio n. 19
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);
                });
            }
        }
Esempio n. 20
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);
            }
        }
Esempio n. 21
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);
            }
        }
Esempio n. 22
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());
                });
            }
        }
        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);
                });
            }
        }
Esempio n. 24
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
                    }));
                });
            }
        }
Esempio n. 25
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
                    }));
                });
            }
        }
Esempio n. 27
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"
                    });
                });
            }
        }
Esempio n. 28
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"
                    });
                });
            }
        }
Esempio n. 29
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();
                });
            }
        }
Esempio n. 30
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;
     }
 }
Esempio n. 33
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();
            }
        }
Esempio n. 34
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();
            }
        }
Esempio n. 35
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);
            }
        }
Esempio n. 36
0
        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 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));
                });
            }
        }
Esempio n. 38
0
        public void Can_resolve_conflict()
        {
            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[] values = actions.Get(new GetRequest {
                        Key = "test"
                    });
                    Assert.Equal(2, values.Length);
                    actions.Put(new PutRequest
                    {
                        Key            = "test",
                        ParentVersions = new[] { version1.Version, version2.Version },
                        Bytes          = new byte[] { 3 }
                    });

                    values = actions.Get(new GetRequest {
                        Key = "test"
                    });
                    Assert.Equal(1, values.Length);
                    Assert.Equal(new byte[] { 3 }, values[0].Data);
                    Assert.Equal(3, values[0].Version.Number);
                });
            }
        }
Esempio n. 39
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);
                });
            }
        }
        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);
                });
            }
        }
Esempio n. 41
0
        public void Can_save_and_load_item_from_cache()
        {
            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 }
                    });
                    Value[] values = actions.Get(new GetRequest {
                        Key = "test"
                    });
                    Assert.Equal(1, values[0].Version.Number);
                    Assert.Equal(new byte[] { 1 }, values[0].Data);
                });
            }
        }
Esempio n. 42
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);
                });
            }
        }
Esempio n. 43
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);
                });
            }
        }
Esempio n. 44
0
        public void Can_query_for_item_with_expiry()
        {
            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)
                    });

                    Value[] values = actions.Get(new GetRequest {
                        Key = "abc1"
                    });
                    Assert.NotEqual(0, values.Length);
                });
            }
        }
Esempio n. 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 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 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 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 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);
                });
            }
        }
        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 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);
                });
            }
        }
Esempio n. 54
0
 public void Start()
 {
     table = new PersistentHashTable("test.esent");
     table.Initialize();
 }
        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);
                });
            }
        }
        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);
                });
            }
        }
Esempio n. 58
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);
                });
            }
        }
Esempio n. 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 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);
                });
            }
        }