Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load storage from a json #3128

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/Neo/Persistence/SnapshotExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// SnapshotExtensions.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.Json;
using System;
using System.Linq;

namespace Neo.Persistence
{
public static class SnapshotExtensions
{
/// <summary>
/// Load data from json
///
/// Expected data (in base64):
///
/// - "key":"value"
/// - "prefix": { "key":"value" }
/// </summary>
/// <param name="snapshot">Snapshot to be used</param>
/// <param name="json">Json Object</param>
public static void LoadFromJson(this ISnapshot snapshot, JObject json)
{
foreach (var entry in json.Properties)
{
if (entry.Value is JString str)
{
// "key":"value" in base64

snapshot.Put(Convert.FromBase64String(entry.Key), Convert.FromBase64String(str.Value));
}
else if (entry.Value is JObject obj)
{
// "prefix": { "key":"value" } in base64

foreach (var subEntry in obj.Properties)
{
if (subEntry.Value is JString subStr)
{
snapshot.Put(Convert.FromBase64String(entry.Key).Concat(Convert.FromBase64String(subEntry.Key)).ToArray(),
Convert.FromBase64String(subStr.Value));
}
}
}
}
}
}
}
65 changes: 65 additions & 0 deletions tests/Neo.UnitTests/Persistence/UT_SnapshotExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// UT_SnapshotExtensions.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Json;
using Neo.Persistence;
using System;
using System.Linq;
using System.Text;

namespace Neo.UnitTests.Persistence
{
[TestClass]
public class UT_SnapshotExtensions
{
[TestMethod]
public void LoadFromJsonTest()
{
using MemoryStore store = new();
using var snapshot = store.GetSnapshot();

// empty

var entries = store.Seek(Array.Empty<byte>(), SeekDirection.Forward).ToArray();
Assert.AreEqual(entries.Length, 0);

// simple object

var json = @"{""a2V5"":""dmFsdWU=""}";

snapshot.LoadFromJson((JObject)JToken.Parse(json));
snapshot.Commit();

entries = store.Seek(Array.Empty<byte>(), SeekDirection.Forward).ToArray();
Assert.AreEqual(entries.Length, 1);

Assert.AreEqual("key", Encoding.ASCII.GetString(entries[0].Key));
Assert.AreEqual("value", Encoding.ASCII.GetString(entries[0].Value));

// prefix object

json = @"{""bXkt"":{""a2V5"":""bXktdmFsdWU=""}}";

snapshot.LoadFromJson((JObject)JToken.Parse(json));
snapshot.Commit();

entries = store.Seek(Array.Empty<byte>(), SeekDirection.Forward).ToArray();
Assert.AreEqual(entries.Length, 2);

Assert.AreEqual("key", Encoding.ASCII.GetString(entries[0].Key));
Assert.AreEqual("value", Encoding.ASCII.GetString(entries[0].Value));

Assert.AreEqual("my-key", Encoding.ASCII.GetString(entries[1].Key));
Assert.AreEqual("my-value", Encoding.ASCII.GetString(entries[1].Value));
}
}
}