From b7d9c4ea705a427691d58b6dffa52ec01fadcdd6 Mon Sep 17 00:00:00 2001 From: Sakurai Date: Mon, 12 Aug 2024 16:35:58 +0900 Subject: [PATCH] =?UTF-8?q?Add:=20=E3=83=95=E3=82=A1=E3=82=B9=E3=83=88?= =?UTF-8?q?=E3=83=9E=E3=83=BC=E3=82=B8=E6=A9=9F=E8=83=BD=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IO/FastMergeType.cs | 8 +++++++ IO/LocalStore.cs | 55 +++++++++++++++++++++++++++++++++++++++++---- JsonDBFactory.cs | 2 +- 3 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 IO/FastMergeType.cs diff --git a/IO/FastMergeType.cs b/IO/FastMergeType.cs new file mode 100644 index 0000000..625906b --- /dev/null +++ b/IO/FastMergeType.cs @@ -0,0 +1,8 @@ +namespace DevJsonDB.IO; + +public enum FastMergeType +{ + None = 0, + Add = 2, + Remove = 4, +} diff --git a/IO/LocalStore.cs b/IO/LocalStore.cs index 7395d5b..1ec2306 100644 --- a/IO/LocalStore.cs +++ b/IO/LocalStore.cs @@ -8,11 +8,13 @@ public class LocalStore : IStore private static Encoding DefaultEncoding = Encoding.UTF8; private string filePath; private List items; + private bool autoCommit; - public LocalStore(string filePath) + public LocalStore(string filePath, bool autoCommit) { this.filePath = filePath; this.items = new List(); + this.autoCommit = autoCommit; this.InitDatabase(); } @@ -32,10 +34,15 @@ public class LocalStore : IStore fs.Seek(0, SeekOrigin.Begin); fs.SetLength(0); - foreach (T item in this.items) + for (int i = 0 ; i < items.Count; i++) { + T item = items[i]; string json = JsonSerializer.Serialize(item); - writer.WriteLine(json); + writer.Write(json); + if (i != items.Count - 1) + { + writer.Write("\n"); + } } } } @@ -53,6 +60,7 @@ public class LocalStore : IStore } this.items.Add(item); + this.AutoCommit(FastMergeType.Add); return true; } @@ -63,7 +71,9 @@ public class LocalStore : IStore public bool Remove(T item) { - return this.items.Remove(item); + bool ret = this.items.Remove(item); + this.AutoCommit(FastMergeType.Remove); + return ret; } public IEnumerable GetValues() @@ -71,6 +81,43 @@ public class LocalStore : IStore return this.items.ToList(); } + private void AutoCommit(FastMergeType mergeType) + { + if (!this.autoCommit) return; + + try + { + // Fast-Merge + switch (mergeType) + { + case FastMergeType.Add: + { + using (FileStream fs = new FileStream(this.filePath, FileMode.Append)) + using (StreamWriter writer = new StreamWriter(fs, DefaultEncoding)) + { + T lastItem = this.items.ElementAt(this.items.Count() - 1); + string json = JsonSerializer.Serialize(lastItem); + if (fs.Length > 0) + { + writer.Write("\n"); + } + writer.Write(json); + } + break; + } + case FastMergeType.Remove: + { + this.Commit(); + break; + } + } + } + catch (Exception ex) + { + throw new JsonDBException(ex.Message, ex); + } + } + private void InitDatabase() { try diff --git a/JsonDBFactory.cs b/JsonDBFactory.cs index 43d4efd..9d723a1 100644 --- a/JsonDBFactory.cs +++ b/JsonDBFactory.cs @@ -6,7 +6,7 @@ public static class JsonDBFactory { public static JsonDB CreateDB(string filePath) { - IStore store = new LocalStore(filePath); + IStore store = new LocalStore(filePath, true); return new JsonDB(store); } }