Skip to content

Commit

Permalink
New insertion.
Browse files Browse the repository at this point in the history
TODO Remove operation
  • Loading branch information
rodoufu committed Nov 10, 2018
1 parent 41dd1d9 commit 01e29b3
Showing 1 changed file with 25 additions and 54 deletions.
79 changes: 25 additions & 54 deletions NeoDataStructure/MerklePatricia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace com.github.neoresearch.NeoDataStructure
public class MerklePatricia
{
private const int LEAF_SIZE = 3;
private const int NODE_SIZE = 18;
private readonly Dictionary<string, string[]> db = new Dictionary<string, string[]>();
private string RootHash = string.Empty;

Expand All @@ -21,7 +22,7 @@ public string this[string key]
get => RootHash.IsEmpty() ? null : Get(db[RootHash], key.CompactEncodeString());
set
{
var node = RootHash.IsEmpty() ? new string[0] : db[RootHash];
var node = RootHash.IsEmpty() ? null : db[RootHash];
if (db.ContainsKey(RootHash))
{
db.Remove(RootHash);
Expand Down Expand Up @@ -63,74 +64,44 @@ private string Get(string[] node, string path)

private string Append(string[] node, string path, string key, string value)
{
if (path.IsEmpty())
if (node == null)
{
if (node.Length == 0)
{
node = new[] {string.Empty, null, null};
}

// Already found the node
node[node.Length - 2] = key;
node[node.Length - 1] = value;
node = new[] {2 + path.Length % 2 + path, key, value};
}
else if (node.Length == LEAF_SIZE)
{
var innerHash = Append(new string[NODE_SIZE], node[0], node[1], node[2]);
node = db[innerHash];
db.Remove(innerHash);
innerHash = Append(node, path, key, value);
node = db[innerHash];
}
else
{
if (node.Length == LEAF_SIZE)
if (path.IsEmpty())
{
// When only has an optimization node
if (path.Length + 1 == node[0].Length && path == node[0].Substring(1))
{
// return Append(node, path, node[1], value);
node[node.Length - 2] = key;
node[node.Length - 1] = value;
}
else
{
var innerNodeHash = Append(new string[18], node[0], node[node.Length - 2],
node[node.Length - 1]);
node = db[innerNodeHash];
db.Remove(innerNodeHash);
}
node[node.Length - 2] = key;
node[node.Length - 1] = value;
}

if (node.Length == 0)
{
// Creates a leaf
node = new[] {(2 + path.Length % 2) + path, key, value};
// Says to add a zero when it is even
// node = new[] {(2 + path.Length % 2) + (path.Length % 2 == 0 ? "0" : string.Empty) + path, key, value};
}

if (node.Length > LEAF_SIZE)
else
{
int kint = int.Parse(path.Substring(0, 1), System.Globalization.NumberStyles.HexNumber);
var nodeHash = node[kint];
var curnode = nodeHash.IsEmpty() ? new string[0] : db[nodeHash];
if (!nodeHash.IsEmpty() && db.ContainsKey(nodeHash))
var innerHash = node[kint];
var innerNode = innerHash != null ? db[innerHash] : null;
if (innerHash != null && db.ContainsKey(innerHash))
{
db.Remove(nodeHash);
db.Remove(innerHash);
}

node[kint] = Append(curnode, path.Substring(1), key, value);
/*
try
{
}
catch (ArgumentOutOfRangeException e)
{
System.Console.WriteLine(e.ToString());
node[kint] = Append(curnode, path.Substring(1), key, value);
}
*/
node[kint] = Append(innerNode, path.Substring(1), key, value);
}
}

var tempHash = node.Hash();
db[tempHash] = node;
return tempHash;
}

public bool Remove(string key)
{
if (RootHash.IsEmpty())
Expand Down Expand Up @@ -194,7 +165,7 @@ public int Height() =>
private int Height(string nodeHash) =>
nodeHash.IsEmpty() ? 0 : (db[nodeHash].Length == LEAF_SIZE ? 1 : 1 + db[nodeHash].Select(Height).Max());


public bool Validade() => RootHash.IsEmpty() || Validade(RootHash, db[RootHash]);

private bool Validade(string nodeHash, string[] node)
Expand Down Expand Up @@ -277,9 +248,9 @@ private bool nodeEquals(MerklePatricia mpB, string nodeHash)

public static bool operator ==(MerklePatricia b1, MerklePatricia b2)
{
if ((object)b1 == null)
if ((object) b1 == null)
{
return (object)b2 == null;
return (object) b2 == null;
}

return b1.Equals(b2);
Expand Down

0 comments on commit 01e29b3

Please sign in to comment.