Compare commits

..

2 Commits

Author SHA1 Message Date
0e7a40a639 Merge pull request 'dev_0.2' (#4) from dev_0.2 into master
Reviewed-on: #4
2024-08-12 12:53:20 +09:00
883236e581 Merge pull request 'fix: readme' (#1) from dev_0.2 into master
Reviewed-on: #1
2024-08-12 12:09:54 +09:00
3 changed files with 5 additions and 60 deletions

View File

@ -1,8 +0,0 @@
namespace DevJsonDB.IO;
public enum FastMergeType
{
None = 0,
Add = 2,
Remove = 4,
}

View File

@ -8,13 +8,11 @@ public class LocalStore<T> : IStore<T>
private static Encoding DefaultEncoding = Encoding.UTF8;
private string filePath;
private List<T> items;
private bool autoCommit;
public LocalStore(string filePath, bool autoCommit)
public LocalStore(string filePath)
{
this.filePath = filePath;
this.items = new List<T>();
this.autoCommit = autoCommit;
this.InitDatabase();
}
@ -34,15 +32,10 @@ public class LocalStore<T> : IStore<T>
fs.Seek(0, SeekOrigin.Begin);
fs.SetLength(0);
for (int i = 0 ; i < items.Count; i++)
foreach (T item in this.items)
{
T item = items[i];
string json = JsonSerializer.Serialize<T>(item);
writer.Write(json);
if (i != items.Count - 1)
{
writer.Write("\n");
}
writer.WriteLine(json);
}
}
}
@ -60,7 +53,6 @@ public class LocalStore<T> : IStore<T>
}
this.items.Add(item);
this.AutoCommit(FastMergeType.Add);
return true;
}
@ -71,9 +63,7 @@ public class LocalStore<T> : IStore<T>
public bool Remove(T item)
{
bool ret = this.items.Remove(item);
this.AutoCommit(FastMergeType.Remove);
return ret;
return this.items.Remove(item);
}
public IEnumerable<T> GetValues()
@ -81,43 +71,6 @@ public class LocalStore<T> : IStore<T>
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<T>(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

View File

@ -6,7 +6,7 @@ public static class JsonDBFactory
{
public static JsonDB<T> CreateDB<T>(string filePath)
{
IStore<T> store = new LocalStore<T>(filePath, true);
IStore<T> store = new LocalStore<T>(filePath);
return new JsonDB<T>(store);
}
}