Skip to content

Commit 2027bf7

Browse files
authored
refactor: move file access methods to static class (#82)
1 parent 400ac42 commit 2027bf7

File tree

2 files changed

+75
-63
lines changed

2 files changed

+75
-63
lines changed

JsonFlatFileDataStore/DataStore.cs

Lines changed: 11 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
using System;
66
using System.Collections.Concurrent;
77
using System.Collections.Generic;
8-
using System.Diagnostics;
98
using System.Dynamic;
109
using System.Globalization;
11-
using System.IO;
1210
using System.Linq;
1311
using System.Threading;
1412
using System.Threading.Tasks;
@@ -71,7 +69,7 @@ public DataStore(string path, bool useLowerCamelCase = true, string keyProperty
7169
_decryptJson = (json => json);
7270
}
7371

74-
_jsonData = JObject.Parse(ReadJsonFromFile(path));
72+
_jsonData = GetJsonObjectFromFile();
7573

7674
// Run updates on a background thread and use BlockingCollection to prevent multiple updates to run simultaneously
7775
Task.Run(() =>
@@ -103,8 +101,8 @@ public DataStore(string path, bool useLowerCamelCase = true, string keyProperty
103101
// Ignore this and exit
104102
break;
105103
}
106-
107-
var jsonText = ReadJsonFromFile(_filePath);
104+
105+
var jsonText = GetJsonTextFromFile();
108106

109107
foreach (var action in batch)
110108
{
@@ -121,7 +119,7 @@ public DataStore(string path, bool useLowerCamelCase = true, string keyProperty
121119

122120
try
123121
{
124-
writeSuccess = WriteJsonToFile(_filePath, jsonText);
122+
writeSuccess = FileAccess.WriteJsonToFile(_filePath, _encryptJson, jsonText);
125123

126124
lock (_jsonData)
127125
{
@@ -165,14 +163,14 @@ public void UpdateAll(string jsonData)
165163
_jsonData = JObject.Parse(jsonData);
166164
}
167165

168-
WriteJsonToFile(_filePath, jsonData);
166+
FileAccess.WriteJsonToFile(_filePath, _encryptJson, jsonData);
169167
}
170168

171169
public void Reload()
172170
{
173171
lock (_jsonData)
174172
{
175-
_jsonData = JObject.Parse(ReadJsonFromFile(_filePath));
173+
_jsonData = GetJsonObjectFromFile();
176174
}
177175
}
178176

@@ -181,7 +179,7 @@ public T GetItem<T>(string key)
181179
if (_reloadBeforeGetCollection)
182180
{
183181
// This might be a bad idea especially if the file is in use, as this can take a long time
184-
_jsonData = JObject.Parse(ReadJsonFromFile(_filePath));
182+
_jsonData = GetJsonObjectFromFile();
185183
}
186184

187185
var token = _jsonData[key];
@@ -204,7 +202,7 @@ public dynamic GetItem(string key)
204202
if (_reloadBeforeGetCollection)
205203
{
206204
// This might be a bad idea especially if the file is in use, as this can take a long time
207-
_jsonData = JObject.Parse(ReadJsonFromFile(_filePath));
205+
_jsonData = GetJsonObjectFromFile();
208206
}
209207

210208
var token = _jsonData[key];
@@ -358,7 +356,7 @@ private IDocumentCollection<T> GetCollection<T>(string path, Func<JToken, T> rea
358356
if (_reloadBeforeGetCollection)
359357
{
360358
// This might be a bad idea especially if the file is in use, as this can take a long time
361-
_jsonData = JObject.Parse(ReadJsonFromFile(_filePath));
359+
_jsonData = GetJsonObjectFromFile();
362360
}
363361

364362
return _jsonData[path]?
@@ -469,59 +467,9 @@ private dynamic SingleDynamicItemReadConverter(JToken e)
469467
}
470468
}
471469

472-
private string ReadJsonFromFile(string path)
473-
{
474-
Stopwatch sw = null;
475-
string json = "{}";
476-
477-
while (true)
478-
{
479-
try
480-
{
481-
json = File.ReadAllText(path);
482-
break;
483-
}
484-
catch (FileNotFoundException)
485-
{
486-
json = _encryptJson(json);
487-
File.WriteAllText(path, json);
488-
break;
489-
}
490-
catch (IOException e) when (e.Message.Contains("because it is being used by another process"))
491-
{
492-
// If some other process is using this file, retry operation unless elapsed times is greater than 10sec
493-
sw = sw ?? Stopwatch.StartNew();
494-
if (sw.ElapsedMilliseconds > 10000)
495-
throw;
496-
}
497-
}
470+
private string GetJsonTextFromFile() => FileAccess.ReadJsonFromFile(_filePath, _encryptJson, _decryptJson);
498471

499-
return _decryptJson(json);
500-
}
501-
502-
private bool WriteJsonToFile(string path, string content)
503-
{
504-
Stopwatch sw = null;
505-
while (true)
506-
{
507-
try
508-
{
509-
File.WriteAllText(path, _encryptJson(content));
510-
return true;
511-
}
512-
catch (IOException e) when (e.Message.Contains("because it is being used by another process"))
513-
{
514-
// If some other process is using this file, retry operation unless elapsed times is greater than 10sec
515-
sw = sw ?? Stopwatch.StartNew();
516-
if (sw.ElapsedMilliseconds > 10000)
517-
return false;
518-
}
519-
catch (Exception)
520-
{
521-
return false;
522-
}
523-
}
524-
}
472+
private JObject GetJsonObjectFromFile() => JObject.Parse(GetJsonTextFromFile());
525473

526474
private class CommitAction
527475
{
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.IO;
4+
5+
namespace JsonFlatFileDataStore
6+
{
7+
public static class FileAccess
8+
{
9+
public static string ReadJsonFromFile(string path, Func<string, string> encryptJson, Func<string, string> decryptJson)
10+
{
11+
Stopwatch sw = null;
12+
var json = "{}";
13+
14+
while (true)
15+
{
16+
try
17+
{
18+
json = File.ReadAllText(path);
19+
break;
20+
}
21+
catch (FileNotFoundException)
22+
{
23+
json = encryptJson(json);
24+
File.WriteAllText(path, json);
25+
break;
26+
}
27+
catch (IOException e) when (e.Message.Contains("because it is being used by another process"))
28+
{
29+
// If some other process is using this file, retry operation unless elapsed times is greater than 10sec
30+
sw ??= Stopwatch.StartNew();
31+
if (sw.ElapsedMilliseconds > 10000)
32+
throw;
33+
}
34+
}
35+
36+
return decryptJson(json);
37+
}
38+
39+
public static bool WriteJsonToFile(string path, Func<string, string> encryptJson, string content)
40+
{
41+
Stopwatch sw = null;
42+
43+
while (true)
44+
{
45+
try
46+
{
47+
File.WriteAllText(path, encryptJson(content));
48+
return true;
49+
}
50+
catch (IOException e) when (e.Message.Contains("because it is being used by another process"))
51+
{
52+
// If some other process is using this file, retry operation unless elapsed times is greater than 10sec
53+
sw ??= Stopwatch.StartNew();
54+
if (sw.ElapsedMilliseconds > 10000)
55+
return false;
56+
}
57+
catch (Exception)
58+
{
59+
return false;
60+
}
61+
}
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)