From 1d0460e4b4e8d6a7df712c5b7e5a90ef3f2aa05b Mon Sep 17 00:00:00 2001 From: jmd Date: Tue, 12 Sep 2023 21:00:01 +0200 Subject: [PATCH] contexts and sharding in progress. --- .../DotJEM.Json.Index2.Contexts.Test.csproj | 24 ++++++++++ .../LuceneIndexContextTest.cs | 48 +++++++++++++++++++ .../LuceneIndexContext.cs | 28 +++++++++++ .../Sharding/ShardingJsonIndex.cs | 37 ++++++++++++++ src/DotJEM.Json.Index2.sln | 8 +++- src/DotJEM.Json.Index2/IJsonIndex.cs | 4 +- 6 files changed, 146 insertions(+), 3 deletions(-) create mode 100644 src/DotJEM.Json.Index2.Contexts.Test/DotJEM.Json.Index2.Contexts.Test.csproj create mode 100644 src/DotJEM.Json.Index2.Contexts.Test/LuceneIndexContextTest.cs create mode 100644 src/DotJEM.Json.Index2.Contexts/Sharding/ShardingJsonIndex.cs diff --git a/src/DotJEM.Json.Index2.Contexts.Test/DotJEM.Json.Index2.Contexts.Test.csproj b/src/DotJEM.Json.Index2.Contexts.Test/DotJEM.Json.Index2.Contexts.Test.csproj new file mode 100644 index 0000000..7fab50d --- /dev/null +++ b/src/DotJEM.Json.Index2.Contexts.Test/DotJEM.Json.Index2.Contexts.Test.csproj @@ -0,0 +1,24 @@ + + + + + + net6.0 + enable + enable + False + False + False + + + + + + + + + + + + + diff --git a/src/DotJEM.Json.Index2.Contexts.Test/LuceneIndexContextTest.cs b/src/DotJEM.Json.Index2.Contexts.Test/LuceneIndexContextTest.cs new file mode 100644 index 0000000..1995492 --- /dev/null +++ b/src/DotJEM.Json.Index2.Contexts.Test/LuceneIndexContextTest.cs @@ -0,0 +1,48 @@ +using DotJEM.Json.Index2.Documents.Fields; +using DotJEM.Json.Index2.IO; +using DotJEM.Json.Index2.Searching; +using DotJEM.Json.Index2.Storage; +using Lucene.Net.Analysis.Standard; +using Lucene.Net.Index; +using Lucene.Net.Search; +using Newtonsoft.Json.Linq; +using NUnit.Framework; + +namespace DotJEM.Json.Index2.Contexts.Test; + +public class LuceneIndexContextTest +{ + [Test] + public async Task SayHello_ReturnsHello() + { + IJsonIndexContextBuilder builder = new JsonIndexContextBuilder(); + builder + .ByDefault(x => x.UsingMemmoryStorage().Build()); + builder + .For("IndexName", b => b.UsingStorage(new RamJsonIndexStorage()).Build()); + + + + IJsonIndexContext context = builder.Build(); + context.Open("IndexName"); + + IJsonIndex index = new JsonIndexBuilder("myIndex") + .UsingMemmoryStorage() + .WithAnalyzer(cfg => new StandardAnalyzer(cfg.Version)) + .WithFieldResolver(new FieldResolver("uuid", "type")) + .Build(); + + IJsonIndexWriter writer = index.CreateWriter(); + writer.Create(JObject.FromObject(new { uuid = Guid.NewGuid(), type = "CAR" })); + writer.Create(JObject.FromObject(new { uuid = Guid.NewGuid(), type = "CAR" })); + writer.Create(JObject.FromObject(new { uuid = Guid.NewGuid(), type = "CAR" })); + writer.Create(JObject.FromObject(new { uuid = Guid.NewGuid(), type = "CAR" })); + writer.Create(JObject.FromObject(new { uuid = Guid.NewGuid(), type = "CAR" })); + writer.Commit(); + + IJsonIndexSearcher? searcher = index.CreateSearcher(); + int count = searcher.Search(new TermQuery(new Term("type", "car"))).Count(); + //int count = searcher.Search(new MatchAllDocsQuery()).Count(); + Assert.AreEqual(5, count); + } +} \ No newline at end of file diff --git a/src/DotJEM.Json.Index2.Contexts/LuceneIndexContext.cs b/src/DotJEM.Json.Index2.Contexts/LuceneIndexContext.cs index 3232842..bc231e0 100644 --- a/src/DotJEM.Json.Index2.Contexts/LuceneIndexContext.cs +++ b/src/DotJEM.Json.Index2.Contexts/LuceneIndexContext.cs @@ -45,3 +45,31 @@ public interface ILuceneJsonIndexFactory { IJsonIndex Create(string name); } + +public interface IJsonIndexContextBuilder +{ + IJsonIndexContextBuilder ByDefault(Func defaultConfig); + IJsonIndexContextBuilder For(string name, Func defaultConfig); + IJsonIndexContext Build(); +} + +public class JsonIndexContextBuilder : IJsonIndexContextBuilder +{ + private readonly ConcurrentDictionary> configurators = new(); + public IJsonIndexContextBuilder ByDefault(Func defaultConfig) + { + configurators.AddOrUpdate("*", s => defaultConfig, (s, func) => defaultConfig); + return this; + } + + public IJsonIndexContextBuilder For(string name, Func defaultConfig) + { + configurators.AddOrUpdate(name, s => defaultConfig, (s, func) => defaultConfig); + return this; + } + + public IJsonIndexContext Build() + { + return new JsonIndexContext(null); + } +} \ No newline at end of file diff --git a/src/DotJEM.Json.Index2.Contexts/Sharding/ShardingJsonIndex.cs b/src/DotJEM.Json.Index2.Contexts/Sharding/ShardingJsonIndex.cs new file mode 100644 index 0000000..304376a --- /dev/null +++ b/src/DotJEM.Json.Index2.Contexts/Sharding/ShardingJsonIndex.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using DotJEM.Json.Index2.Configuration; +using DotJEM.Json.Index2.IO; +using DotJEM.Json.Index2.Searching; +using DotJEM.Json.Index2.Storage; +using DotJEM.ObservableExtensions.InfoStreams; + +namespace DotJEM.Json.Index2.Contexts.Sharding +{ + internal class ShardingJsonIndex : IJsonIndex + { + public IJsonIndexSearcher CreateSearcher() + { + throw new NotImplementedException(); + } + + public IInfoStream InfoStream { get; } + public IJsonIndexStorageManager Storage { get; } + public IJsonIndexConfiguration Configuration { get; } + public IIndexWriterManager WriterManager { get; } + public IIndexSearcherManager SearcherManager { get; } + + public IJsonIndexWriter CreateWriter() + { + throw new NotImplementedException(); + } + + public void Close() + { + throw new NotImplementedException(); + } + } +} diff --git a/src/DotJEM.Json.Index2.sln b/src/DotJEM.Json.Index2.sln index 0935a4d..e01eaaa 100644 --- a/src/DotJEM.Json.Index2.sln +++ b/src/DotJEM.Json.Index2.sln @@ -21,7 +21,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotJEM.Json.Index2.Contexts EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotJEM.Json.Index2.QueryParsers", "DotJEM.Json.Index2.QueryParsers\DotJEM.Json.Index2.QueryParsers.csproj", "{B07B4E9F-7202-4E9D-ACCC-717B43469A7A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotJEM.Json.Index2.Snapshots", "DotJEM.Json.Index2.Snapshots\DotJEM.Json.Index2.Snapshots.csproj", "{98FF81EC-F9B6-4B6C-9B02-2A5830756B05}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotJEM.Json.Index2.Snapshots", "DotJEM.Json.Index2.Snapshots\DotJEM.Json.Index2.Snapshots.csproj", "{98FF81EC-F9B6-4B6C-9B02-2A5830756B05}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotJEM.Json.Index2.Contexts.Test", "DotJEM.Json.Index2.Contexts.Test\DotJEM.Json.Index2.Contexts.Test.csproj", "{8F30DDCD-334C-4C47-AE47-9DCEB0CB4C73}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -49,6 +51,10 @@ Global {98FF81EC-F9B6-4B6C-9B02-2A5830756B05}.Debug|Any CPU.Build.0 = Debug|Any CPU {98FF81EC-F9B6-4B6C-9B02-2A5830756B05}.Release|Any CPU.ActiveCfg = Release|Any CPU {98FF81EC-F9B6-4B6C-9B02-2A5830756B05}.Release|Any CPU.Build.0 = Release|Any CPU + {8F30DDCD-334C-4C47-AE47-9DCEB0CB4C73}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8F30DDCD-334C-4C47-AE47-9DCEB0CB4C73}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8F30DDCD-334C-4C47-AE47-9DCEB0CB4C73}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8F30DDCD-334C-4C47-AE47-9DCEB0CB4C73}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/DotJEM.Json.Index2/IJsonIndex.cs b/src/DotJEM.Json.Index2/IJsonIndex.cs index 3ee3996..ecf4b8f 100644 --- a/src/DotJEM.Json.Index2/IJsonIndex.cs +++ b/src/DotJEM.Json.Index2/IJsonIndex.cs @@ -75,13 +75,13 @@ public interface IJsonIndexBuilder public class JsonIndexBuilder : IJsonIndexBuilder { - private readonly string name = Guid.NewGuid().ToString("D"); + public string Name { get; } = Guid.NewGuid().ToString("D"); private IJsonIndexStorage storage = new RamJsonIndexStorage(); private readonly Dictionary> factories = new(); public JsonIndexBuilder(string name) { - this.name = name; + this.Name = name; } public IJsonIndexBuilder UsingStorage(IJsonIndexStorage storage)