Browse files

fix issue#240 support single writer and multiple readers

  • Loading branch information...
1 parent 5bb9a8f commit c3d382ad4baa8e811a48ac437a920416378be0bf @suwatch suwatch committed Dec 11, 2012
Showing with 8 additions and 3 deletions.
  1. +1 −1 Kudu.Core.Test/JsonSettingsFacts.cs
  2. +7 −2 Kudu.Core/Settings/JsonSettings.cs
View
2 Kudu.Core.Test/JsonSettingsFacts.cs
@@ -172,7 +172,7 @@ private IFileSystem GetMockFileSystem(string path, params KeyValuePair<string, s
var directory = new Mock<DirectoryBase>();
// Arrange
- file.Setup(f => f.Open(path, FileMode.Create, FileAccess.Write, FileShare.ReadWrite)).Returns(() =>
+ file.Setup(f => f.Open(path, FileMode.Create, FileAccess.Write, FileShare.Read)).Returns(() =>
{
mem = new MemoryStream();
return mem;
View
9 Kudu.Core/Settings/JsonSettings.cs
@@ -91,6 +91,8 @@ private JObject Read()
return new JObject();
}
+ // opens file for FileAccess.Read but does allow other read/write (FileShare.ReadWrite).
+ // it is the most optimal where write is infrequent and dirty read is acceptable.
using (var reader = new JsonTextReader(new StreamReader(_fileSystem.File.Open(_path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))))
{
return JObject.Load(reader);
@@ -104,10 +106,13 @@ private void Save(JObject json)
FileSystemHelpers.EnsureDirectory(_fileSystem, Path.GetDirectoryName(_path));
}
- using (var writer = new StreamWriter(_fileSystem.File.Open(_path, FileMode.Create, FileAccess.Write, FileShare.ReadWrite)))
+ // opens file for FileAccess.Write but does allow other dirty read (FileShare.Read).
+ // it is the most optimal where write is infrequent and dirty read is acceptable.
+ using (var writer = new JsonTextWriter(new StreamWriter(_fileSystem.File.Open(_path, FileMode.Create, FileAccess.Write, FileShare.Read))))
{
// prefer indented-readable format
- writer.Write(json.ToString());
+ writer.Formatting = Formatting.Indented;
+ json.WriteTo(writer);
}
}
}

0 comments on commit c3d382a

Please sign in to comment.