fix LocalFileAlgoTemplateService
This commit is contained in:
1 parent
de8c920a8c
commit
2d890d6258
1 file changed
+20
-13
@@ -81,7 +81,7 @@ namespace bts {
|
||||
while (_templates.ContainsKey(id)) id++;
|
||||
var template = new AlgoTemplate { Id = id, Name = name, Definition = def.CloneToMutable() };
|
||||
_templates.Add(id, new AlgoTemplateImmutable(template));
|
||||
FileStreamHelper.AppendBytes(_full_path, template.ToByteArray());
|
||||
FileStreamHelper.AppendProtos(_full_path, template);
|
||||
_subject.OnNext(_templates);
|
||||
return new Result<AlgoTemplateUpdateResult>(new AlgoTemplateUpdateResult(AlgoTemplateUpdateResult.Error.ERROR_OK, id));
|
||||
});
|
||||
@@ -113,19 +113,9 @@ namespace bts {
|
||||
});
|
||||
|
||||
void _save_all() =>
|
||||
FileStreamHelper.WriteBytes(_full_path, _templates.Values.SelectMany(ot => ot.ToByteArray()).ToArray());
|
||||
FileStreamHelper.WriteProtos(_full_path, _templates.Values.Select(t => t.CloneToMutable()));
|
||||
|
||||
static class FileStreamHelper {
|
||||
public static void AppendBytes(string path, byte[] bytes) {
|
||||
using (var fs = File.Open(path, FileMode.Append, FileAccess.Write, FileShare.Read))
|
||||
fs.Write(bytes, 0, bytes.Length);
|
||||
}
|
||||
|
||||
public static void WriteBytes(string path, byte[] bytes) {
|
||||
using (var fs = File.Open(path, FileMode.Create, FileAccess.Write, FileShare.Read))
|
||||
fs.Write(bytes, 0, bytes.Length);
|
||||
}
|
||||
|
||||
public static List<T> ReadProtos<T>(string path, bool check_existence_first = true) where T : Google.Protobuf.IMessage<T>, new() {
|
||||
if (!check_existence_first || File.Exists(path)) {
|
||||
var fs = SafeOpen(path, FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||
@@ -142,7 +132,7 @@ namespace bts {
|
||||
while (fs.Position < fs.Length) {
|
||||
T val;
|
||||
try {
|
||||
val = parser.ParseFrom(fs);
|
||||
val = parser.ParseDelimitedFrom(fs);
|
||||
} catch {
|
||||
break;
|
||||
}
|
||||
@@ -150,6 +140,23 @@ namespace bts {
|
||||
}
|
||||
}
|
||||
|
||||
public static void AppendProtos<T>(string path, T value) where T : Google.Protobuf.IMessage<T> =>
|
||||
AppendProtos<T>(path, new[] { value });
|
||||
|
||||
public static void AppendProtos<T>(string path, IEnumerable<T> source) where T : Google.Protobuf.IMessage<T> {
|
||||
using (var fs = File.Open(path, FileMode.Append, FileAccess.Write, FileShare.Read)) {
|
||||
foreach (var value in source)
|
||||
Google.Protobuf.MessageExtensions.WriteDelimitedTo(value, fs);
|
||||
}
|
||||
}
|
||||
|
||||
public static void WriteProtos<T>(string path, IEnumerable<T> source) where T : Google.Protobuf.IMessage<T> {
|
||||
using (var fs = File.Open(path, FileMode.Create, FileAccess.Write, FileShare.Read)) {
|
||||
foreach (var value in source)
|
||||
Google.Protobuf.MessageExtensions.WriteDelimitedTo(value, fs);
|
||||
}
|
||||
}
|
||||
|
||||
public static FileStream SafeOpen(string path, FileMode mode, FileAccess access, FileShare share) =>
|
||||
SafeOpen(path, mode, access, share, out string _);
|
||||
|
||||
|
||||
Reference in new issue
Block a user