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

Persitence Plugin for exporting storage changes #21

Merged
merged 15 commits into from
Dec 4, 2018
Merged
71 changes: 37 additions & 34 deletions Persistence/Persistence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,47 +31,50 @@ public void OnPersist(Snapshot snapshot)
private static void OnPersistStorage(Snapshot snapshot)
{
uint blockIndex = snapshot.Height;
string dirPath = "./Storage";
Directory.CreateDirectory(dirPath);
string path = $"{HandlePaths(dirPath, blockIndex)}/dump-block-{blockIndex.ToString()}.json";

JArray array = new JArray();

foreach (DataCache<StorageKey, StorageItem>.Trackable trackable in snapshot.Storages.GetChangeSet())
if (blockIndex >= Settings.Default.HeightToBegin)
{
JObject state = new JObject();
string dirPath = "./Storage";
Directory.CreateDirectory(dirPath);
string path = $"{HandlePaths(dirPath, blockIndex)}/dump-block-{blockIndex.ToString()}.json";

JArray array = new JArray();

switch (trackable.State)
foreach (DataCache<StorageKey, StorageItem>.Trackable trackable in snapshot.Storages.GetChangeSet())
{
JObject state = new JObject();

case TrackState.Added:
state["state"] = "Added";
state["key"] = trackable.Key.ToArray().ToHexString();
state["value"] = trackable.Item.ToArray().ToHexString();
// Here we have a new trackable.Key and trackable.Item
break;
case TrackState.Changed:
state["state"] = "Changed";
state["key"] = trackable.Key.ToArray().ToHexString();
state["value"] = trackable.Item.ToArray().ToHexString();
break;
case TrackState.Deleted:
state["state"] = "Deleted";
state["key"] = trackable.Key.ToArray().ToHexString();
break;
switch (trackable.State)
{

case TrackState.Added:
state["state"] = "Added";
state["key"] = trackable.Key.ToArray().ToHexString();
state["value"] = trackable.Item.ToArray().ToHexString();
// Here we have a new trackable.Key and trackable.Item
break;
case TrackState.Changed:
state["state"] = "Changed";
state["key"] = trackable.Key.ToArray().ToHexString();
state["value"] = trackable.Item.ToArray().ToHexString();
break;
case TrackState.Deleted:
state["state"] = "Deleted";
state["key"] = trackable.Key.ToArray().ToHexString();
break;
}
array.Add(state);
}
array.Add(state);
}

Settings.Default.BlockStorageCache = Settings.Default.BlockStorageCache + "{\"block\":" + blockIndex.ToString() + ",\"size\":" + array.Count.ToString() + ",\"storage\":\n";
Settings.Default.BlockStorageCache = Settings.Default.BlockStorageCache + array.ToString() + "},\n";
Settings.Default.BlockStorageCache = Settings.Default.BlockStorageCache + "{\"block\":" + blockIndex.ToString() + ",\"size\":" + array.Count.ToString() + ",\"storage\":\n";
Settings.Default.BlockStorageCache = Settings.Default.BlockStorageCache + array.ToString() + "},\n";

if ((blockIndex % Settings.Default.BlockCacheSize == 0) || (blockIndex > Settings.Default.HeightToStartRealTimeSyncing))
{
Settings.Default.BlockStorageCache += "]";
File.WriteAllText(path, Settings.Default.BlockStorageCache);
Settings.Default.BlockStorageCache = "[";
}
if ((blockIndex % Settings.Default.BlockCacheSize == 0) || (blockIndex > Settings.Default.HeightToStartRealTimeSyncing))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea brother... a config variable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neo's design affords us these beauty configurations. 💃

Copy link
Member Author

@vncoelho vncoelho Nov 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@igormcoelho, maybe put all these parameters in an array is a better idea, yep? What do you think?

Because there is a switch case at OnPersist in the way that other information can be extracted if someone thing about something.

{
Settings.Default.BlockStorageCache += "]";
File.WriteAllText(path, Settings.Default.BlockStorageCache);
Settings.Default.BlockStorageCache = "[";
}
}
}

private static string HandlePaths(string dirPath, uint blockIndex)
Expand Down
3 changes: 2 additions & 1 deletion Persistence/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ internal class Settings
public uint HeightToBegin { get; }
//Height to begin real-time syncing and dumping on single files
public uint HeightToStartRealTimeSyncing { get; }
public string BlockStorageCache;
public uint PersistAction { get; }
public string BlockStorageCache;


public static Settings Default { get; }

Expand Down