Skip to content

Commit

Permalink
Fix migrating process states from older versions
Browse files Browse the repository at this point in the history
Add branch for compatibility with the older encoding of reduction records into files.
See ce01315#diff-d7e54a6500d82439620a96092749a70e
  • Loading branch information
Viir committed Apr 19, 2020
1 parent 2c605a9 commit ef5b9d0
Showing 1 changed file with 29 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,36 @@ public ReductionRecord GetReduction(byte[] reducedCompositionHash)
if (fileContent == null)
return null;

var reductionRecordFromFile =
JsonConvert.DeserializeObject<ReductionRecordInFile>(Encoding.UTF8.GetString(fileContent));

if (reducedCompositionHashBase16 != reductionRecordFromFile.ReducedCompositionHashBase16)
throw new Exception("Unexpected content in file " + string.Join("/", filePath) + ", composition hash does not match.");

return new ReductionRecord
try
{
ReducedCompositionHash = reducedCompositionHash,
ReducedValueLiteralString = reductionRecordFromFile.ReducedValue?.LiteralString,
};
var payloadStartIndex =
/*
Previous implementation used `File.WriteAllText`:
https://github.com/elm-fullstack/elm-fullstack/blob/1cd3f00bdf5a05e9bda479c534b0458b2496393c/implement/PersistentProcess/PersistentProcess.Common/ProcessStore.cs#L183
Looking at the files from stores in production, it seems like that caused addition of BOM.
*/
fileContent.Take(3).SequenceEqual(new byte[] { 0xEF, 0xBB, 0xBF })
?
3
:
0;

var reductionRecordFromFile =
JsonConvert.DeserializeObject<ReductionRecordInFile>(Encoding.UTF8.GetString(fileContent.AsSpan(payloadStartIndex)));

if (reducedCompositionHashBase16 != reductionRecordFromFile.ReducedCompositionHashBase16)
throw new Exception("Unexpected content in file " + string.Join("/", filePath) + ", composition hash does not match.");

return new ReductionRecord
{
ReducedCompositionHash = reducedCompositionHash,
ReducedValueLiteralString = reductionRecordFromFile.ReducedValue?.LiteralString,
};
}
catch (Exception e)
{
throw new Exception("Failed to read reduction from file '" + string.Join("/", filePath) + "'.", e);
}
}

public IEnumerable<string> ReductionsFilesNames() =>
Expand Down

0 comments on commit ef5b9d0

Please sign in to comment.