commit d8c6f4044209e1a594eec7c1b33f0fe91a3f51e8 Author: Sakurai Date: Mon Aug 12 12:02:12 2024 +0900 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1746e32 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +bin +obj diff --git a/DevJsonDB.csproj b/DevJsonDB.csproj new file mode 100644 index 0000000..412d83d --- /dev/null +++ b/DevJsonDB.csproj @@ -0,0 +1,10 @@ + + + + Library + net8.0 + enable + enable + + + diff --git a/DevJsonDB.sln b/DevJsonDB.sln new file mode 100644 index 0000000..0db6094 --- /dev/null +++ b/DevJsonDB.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DevJsonDB", "DevJsonDB.csproj", "{23D26ECE-BE59-48D5-9548-9E7C42BD85B4}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {23D26ECE-BE59-48D5-9548-9E7C42BD85B4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {23D26ECE-BE59-48D5-9548-9E7C42BD85B4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {23D26ECE-BE59-48D5-9548-9E7C42BD85B4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {23D26ECE-BE59-48D5-9548-9E7C42BD85B4}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/IO/IStore.cs b/IO/IStore.cs new file mode 100644 index 0000000..a39ca26 --- /dev/null +++ b/IO/IStore.cs @@ -0,0 +1,10 @@ +namespace DevJsonDB.IO; + +public interface IStore : IDisposable +{ + public void Commit(); + + public bool Add(T item); + + public IEnumerable GetValues(); +} diff --git a/IO/LocalStore.cs b/IO/LocalStore.cs new file mode 100644 index 0000000..1f60230 --- /dev/null +++ b/IO/LocalStore.cs @@ -0,0 +1,84 @@ +using System.Text; +using System.Text.Json; + +namespace DevJsonDB.IO; + +public class LocalStore : IStore +{ + private static Encoding DefaultEncoding = Encoding.UTF8; + private string filePath; + private List items; + + public LocalStore(string filePath) + { + this.filePath = filePath; + this.items = new List(); + + this.InitDatabase(); + } + + public void Dispose() + { + this.Commit(); + } + + public void Commit() + { + try + { + using (FileStream fs = new FileStream(this.filePath, FileMode.OpenOrCreate)) + using (StreamWriter writer = new StreamWriter(fs, DefaultEncoding)) + { + foreach (T item in this.items) + { + string json = JsonSerializer.Serialize(item); + writer.WriteLine(json); + } + } + } + catch (Exception ex) + { + throw new JsonDBException(ex.Message, ex); + } + } + + public bool Add(T item) + { + if (this.items.Contains(item)) + { + return false; + } + + this.items.Add(item); + return true; + } + + public IEnumerable GetValues() + { + return this.items.ToList(); + } + + private void InitDatabase() + { + try + { + using (FileStream fs = new FileStream(this.filePath, FileMode.OpenOrCreate)) + using (StreamReader reader = new StreamReader(fs, DefaultEncoding)) + { + string? line = null; + while ((line = reader.ReadLine()) != null) + { + T? value = JsonSerializer.Deserialize(line); + if (value is object) + { + this.items.Add(value); + } + } + } + } + catch (Exception ex) + { + throw new JsonDBException(ex.Message, ex); + } + } +} diff --git a/JsonDB.cs b/JsonDB.cs new file mode 100644 index 0000000..b9eb24a --- /dev/null +++ b/JsonDB.cs @@ -0,0 +1,38 @@ +using DevJsonDB.IO; + +namespace DevJsonDB; + +public class JsonDB : IDisposable +{ + private IStore store; + + public JsonDB(IStore store) + { + this.store = store; + } + + public void Dispose() + { + this.store.Dispose(); + } + + public void Commit() + { + this.store.Commit(); + } + + public bool Add(T item) + { + return this.store.Add(item); + } + + public IEnumerable GetValues() + { + return this.store.GetValues(); + } + + public IEnumerable GetValues(Func filter) + { + return this.store.GetValues().Where(filter).ToList(); + } +} diff --git a/JsonDBException.cs b/JsonDBException.cs new file mode 100644 index 0000000..5502170 --- /dev/null +++ b/JsonDBException.cs @@ -0,0 +1,8 @@ +namespace DevJsonDB; + +public class JsonDBException : Exception +{ + public JsonDBException(string message, Exception innerException) : base(message, innerException) + { + } +} diff --git a/JsonDBFactory.cs b/JsonDBFactory.cs new file mode 100644 index 0000000..43d4efd --- /dev/null +++ b/JsonDBFactory.cs @@ -0,0 +1,12 @@ +using DevJsonDB.IO; + +namespace DevJsonDB; + +public static class JsonDBFactory +{ + public static JsonDB CreateDB(string filePath) + { + IStore store = new LocalStore(filePath); + return new JsonDB(store); + } +} diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..594930c --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +The MIT License (MIT) + +Copyright 2024 Sakurai Ryota + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..1994bfd --- /dev/null +++ b/README.md @@ -0,0 +1,31 @@ +# JsonDB + +## 概要 + +シンプルなJSONL入出力をサポートするC#ライブラリです。 + +## 使い方 + +`JsonDBFactory`を使用し、DBを初期化します。 + +```csharp +// DBを初期化します。 +string filePath = "fileName.jsonl"; +JsonDB jsonDB = JsonDBFactory.CreateDB(filePath); + +// DBにアイテムを追加します。 +bool result = jsonDB.Add(new T()); + +// DBから全アイテムを取得します。 +IEnumerable allItems = jsonDB.GetValues(); + +// DBから特定のアイテムを取得します。 +IEnumerable filteredItems = jsonDB.GetValues((T) => true): + +// ファイルに書き込む +jsonDB.Commit(); + +// リソースを破棄する +jsonDB.Dispose(); + +```