From 2d890d625867ecda7d23c560db90f5fcac5ef923 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sun, 25 Nov 2018 22:28:41 -0600 Subject: [PATCH] fix LocalFileAlgoTemplateService --- .../LocalFileAlgoTemplateService.cs | 33 +++++++++++-------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/src/client/spark/Orders/AlgoTemplateService/LocalFileAlgoTemplateService.cs b/src/client/spark/Orders/AlgoTemplateService/LocalFileAlgoTemplateService.cs index 3c357bc07..72ee6b68c 100644 --- a/src/client/spark/Orders/AlgoTemplateService/LocalFileAlgoTemplateService.cs +++ b/src/client/spark/Orders/AlgoTemplateService/LocalFileAlgoTemplateService.cs @@ -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(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 ReadProtos(string path, bool check_existence_first = true) where T : Google.Protobuf.IMessage, 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(string path, T value) where T : Google.Protobuf.IMessage => + AppendProtos(path, new[] { value }); + + public static void AppendProtos(string path, IEnumerable source) where T : Google.Protobuf.IMessage { + 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(string path, IEnumerable source) where T : Google.Protobuf.IMessage { + 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 _);