Skip to content

Commit ecd545f

Browse files
committed
Started with Storage upgrades
1 parent bc345ba commit ecd545f

File tree

15 files changed

+369
-459
lines changed

15 files changed

+369
-459
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
namespace GAIL.Storage.Hierarchy;
2+
3+
/// <summary>
4+
/// The default implementation of <see cref="IChildNode"/>.
5+
/// </summary>
6+
public abstract class ChildNode : IChildNode {
7+
/// <summary>
8+
/// Create a node with a key, value and a parent.
9+
/// </summary>
10+
/// <param name="key">The key</param>
11+
/// <param name="parent"></param>
12+
/// <returns></returns>
13+
public ChildNode(string key, IParentNode parent) {
14+
Key = key;
15+
SetParent(parent);
16+
}
17+
18+
/// <inheritdoc/>
19+
public string Key { get; private set; }
20+
21+
/// <inheritdoc/>
22+
public IParentNode? Parent { get; private set; }
23+
24+
/// <inheritdoc/>
25+
public virtual string ID { get {
26+
if (Parent == null) {
27+
return Key;
28+
}
29+
if (Parent is IChildNode parentChild) {
30+
string parentID = parentChild.ID;
31+
return parentID+'.'+Key;
32+
}
33+
return Key;
34+
} }
35+
36+
/// <inheritdoc/>
37+
public virtual void ClearParent() {
38+
IParentNode? previousParent = Parent;
39+
Parent = null;
40+
if (previousParent?.Children.ContainsKey(Key) ?? false) {
41+
previousParent.RemoveChild(this);
42+
}
43+
44+
}
45+
46+
/// <inheritdoc/>
47+
/// <exception cref="ArgumentException"><paramref name="parent"/> is not a valid member.</exception>
48+
public virtual void SetParent(IParentNode parent) {
49+
// if (parent is IChildNode member) {
50+
// if (!StorageRegister.IsMemberRegistered(member)) {
51+
// throw new ArgumentException("Parent is not a valid member", nameof(parent));
52+
// }
53+
// } else if (parent is not Storage) {
54+
// throw new ArgumentException("Parent is not a valid member", nameof(parent));
55+
// } TODO: ???
56+
if (Parent!=null) {
57+
ClearParent();
58+
}
59+
Parent = parent;
60+
if (!parent.Children.ContainsKey(Key)) {
61+
parent.AddChild(this);
62+
}
63+
}
64+
}
Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
namespace GAIL.Storage.Members;
1+
namespace GAIL.Storage.Hierarchy;
22

33
/// <summary>
4-
/// Represents any member of tree and storage file.
4+
/// Represents a child of a parent node.
55
/// </summary>
6-
public interface IMember {
6+
public interface IChildNode {
77
/// <summary>
8-
/// The type of this member.
9-
/// </summary>
10-
public MemberType Type { get; }
11-
/// <summary>
12-
/// The key of this node (cannot contain a dot).
8+
/// The key of this node.
139
/// </summary>
1410
public string Key { get; }
1511
/// <summary>
@@ -18,13 +14,12 @@ public interface IMember {
1814
public IParentNode? Parent { get; }
1915

2016
/// <summary>
21-
/// Creates the total ID from the root (root.childnode.leafnode).
17+
/// Gets the total ID from the root (root.childnode.leafnode).
2218
/// </summary>
23-
/// <returns>The ID from the root.</returns>
24-
public string GetID();
19+
public string ID { get; }
2520

2621
/// <summary>
27-
/// Removes the parent.
22+
/// Clears the parent of this node.
2823
/// </summary>
2924
public void ClearParent();
3025
/// <summary>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
namespace GAIL.Storage.Hierarchy;
2+
3+
/// <summary>
4+
/// Represents a node that has children.
5+
/// </summary>
6+
public interface IParentNode {
7+
/// <summary>
8+
/// The children of this node.
9+
/// </summary>
10+
public Dictionary<string, IChildNode> Children { get; }
11+
12+
/// <summary>
13+
/// Gets the node of the corresponding ID. This is relative to this node.
14+
/// </summary>
15+
/// <param name="ID">The ID of the node to return (can contain dots).</param>
16+
/// <returns>The node if that node exists.</returns>
17+
[Obsolete("Use Get(List<string>) instead.")]
18+
public IChildNode? Get(string ID);
19+
/// <summary>
20+
/// Gets the node of the corresponding keys. This is relative to this node.
21+
/// </summary>
22+
/// <param name="keys">The list of keys for what path to take.</param>
23+
/// <returns>The node if that node exists.</returns>
24+
public IChildNode? Get(List<string> keys);
25+
26+
/// <summary>
27+
/// Gets the node of the corresponding ID. This is relative to this node.
28+
/// </summary>
29+
/// <typeparam name="T">The type of the child node.</typeparam>
30+
/// <param name="ID">The ID of the node to return (can contain dots).</param>
31+
/// <returns>The node if that node exists.</returns>
32+
[Obsolete("Use Get<T>(List<string>) instead.")]
33+
public T? Get<T>(string ID) where T : IChildNode;
34+
35+
/// <summary>
36+
/// Gets the node of the corresponding ID. This is relative to this node.
37+
/// </summary>
38+
/// <typeparam name="T">The type of the child node.</typeparam>
39+
/// <param name="keys">The list of keys for what path to take.</param>
40+
/// <returns>The node if that node exists.</returns>
41+
public T? Get<T>(List<string> keys) where T : IChildNode;
42+
43+
/// <summary>
44+
/// Adds a child to this node.
45+
/// </summary>
46+
/// <param name="member">The node to add as a child.</param>
47+
public void AddChild(IChildNode member);
48+
/// <summary>
49+
/// Removes a child from this node.
50+
/// </summary>
51+
/// <param name="child">The child node to remove.</param>
52+
/// <returns>True it was successful. Else if it didn't find the node in the list, it is false.</returns>
53+
public bool RemoveChild(IChildNode child);
54+
}

GAIL.Storage/Hierarchy/Node.cs

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
namespace GAIL.Storage.Hierarchy;
2+
3+
/// <summary>
4+
/// A node is a parent node and a child node.
5+
/// </summary>
6+
public class Node : IParentNode, IChildNode {
7+
/// <summary>
8+
/// Creates a node with a key, without a parent.
9+
/// </summary>
10+
/// <param name="key">The key of this node.</param>
11+
/// <exception cref="InvalidOperationException">Key cannot contain a dot.</exception>
12+
public Node(string key) {
13+
if (key.Contains('.')) {
14+
throw new InvalidOperationException("Invalid key, cannot contain dots");
15+
}
16+
Key = key;
17+
}
18+
/// <summary>
19+
/// Create a node with a key, value and a parent.
20+
/// </summary>
21+
/// <param name="key">The key</param>
22+
/// <param name="parent"></param>
23+
/// <returns></returns>
24+
public Node(string key, IParentNode parent) : this(key) {
25+
SetParent(parent);
26+
}
27+
28+
/// <inheritdoc/>
29+
public virtual Dictionary<string, IChildNode> Children => children;
30+
/// <summary>
31+
/// The children of this node.
32+
/// </summary>
33+
protected Dictionary<string, IChildNode> children = [];
34+
35+
/// <inheritdoc/>
36+
public string Key { get; private set; }
37+
38+
/// <inheritdoc/>
39+
public IParentNode? Parent { get; private set; }
40+
41+
/// <inheritdoc/>
42+
public virtual string ID { get {
43+
if (Parent == null) {
44+
return Key;
45+
}
46+
if (Parent is IChildNode parentChild) {
47+
string parentID = parentChild.ID;
48+
return parentID+'.'+Key;
49+
}
50+
return Key;
51+
} }
52+
53+
/// <inheritdoc/>
54+
public virtual void ClearParent() {
55+
IParentNode? previousParent = Parent;
56+
Parent = null;
57+
if (previousParent?.Children.ContainsKey(Key) ?? false) {
58+
previousParent.RemoveChild(this);
59+
}
60+
61+
}
62+
63+
/// <inheritdoc/>
64+
/// <exception cref="ArgumentException"><paramref name="parent"/> is not a valid member.</exception>
65+
public virtual void SetParent(IParentNode parent) {
66+
// if (parent is IChildNode member) {
67+
// if (!StorageRegister.IsMemberRegistered(member)) {
68+
// throw new ArgumentException("Parent is not a valid member", nameof(parent));
69+
// }
70+
// } else if (parent is not Storage) {
71+
// throw new ArgumentException("Parent is not a valid member", nameof(parent));
72+
// } TODO: ???
73+
74+
if (Parent!=null) {
75+
ClearParent();
76+
}
77+
Parent = parent;
78+
if (!parent.Children.ContainsKey(Key)) {
79+
parent.AddChild(this);
80+
}
81+
}
82+
83+
/// <inheritdoc/>
84+
/// <exception cref="ArgumentException"><paramref name="member"/> is not registered.</exception>
85+
public virtual void AddChild(IChildNode member) {
86+
// if (!StorageRegister.IsMemberRegistered(member)) {
87+
// throw new ArgumentException("Member is not registered", nameof(member));
88+
// } TODO: ???
89+
if (children.ContainsKey(member.Key)) { return; }
90+
91+
children.Add(member.Key, member);
92+
93+
if (member.Parent != this) {
94+
member.SetParent(this);
95+
}
96+
}
97+
98+
/// <inheritdoc/>
99+
public virtual bool RemoveChild(IChildNode child) {
100+
if (!children.ContainsKey(child.Key)) { return false; }
101+
children.Remove(child.Key);
102+
if (child.Parent == this) {
103+
child.ClearParent();
104+
}
105+
return true;
106+
}
107+
108+
/// <inheritdoc/>
109+
[Obsolete("Use Get<T>(List<string>) instead.")]
110+
public virtual T? Get<T>(string ID) where T : IChildNode {
111+
return (T?)Get(ID);
112+
}
113+
/// <inheritdoc/>
114+
public virtual T? Get<T>(List<string> keys) where T : IChildNode {
115+
return (T?)Get(keys);
116+
}
117+
118+
/// <inheritdoc/>
119+
[Obsolete("Use Get(List<string>) instead.")]
120+
public virtual IChildNode? Get(string ID) {
121+
return Get([.. ID.Split('.')]);
122+
}
123+
124+
/// <inheritdoc/>
125+
public virtual IChildNode? Get(List<string> keys) {
126+
if (children.Count < 1) {
127+
return null;
128+
}
129+
IChildNode? child = children!.GetValueOrDefault(keys[0]);
130+
if (child == null) {
131+
return null;
132+
}
133+
keys.RemoveAt(0);
134+
if (keys.Count <= 0) {
135+
return child;
136+
}
137+
if (child is IParentNode childParent) {
138+
return childParent.Get(keys);
139+
}
140+
return null;
141+
}
142+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
namespace GAIL.Storage.Hierarchy;
2+
3+
/// <summary>
4+
/// The default implementation of <see cref="IParentNode"/>.
5+
/// </summary>
6+
public class ParentNode : IParentNode {
7+
/// <inheritdoc/>
8+
public virtual Dictionary<string, IChildNode> Children => children;
9+
/// <summary>
10+
/// The children of this node.
11+
/// </summary>
12+
protected Dictionary<string, IChildNode> children = [];
13+
14+
/// <inheritdoc/>
15+
/// <exception cref="ArgumentException"><paramref name="member"/> is not registered.</exception>
16+
public virtual void AddChild(IChildNode member) {
17+
// if (!StorageRegister.IsMemberRegistered(member)) {
18+
// throw new ArgumentException("Member is not registered", nameof(member));
19+
// } TODO: ???
20+
if (children.ContainsKey(member.Key)) { return; }
21+
22+
children.Add(member.Key, member);
23+
24+
if (member.Parent != this) {
25+
member.SetParent(this);
26+
}
27+
}
28+
29+
/// <inheritdoc/>
30+
public virtual bool RemoveChild(IChildNode child) {
31+
if (!children.ContainsKey(child.Key)) { return false; }
32+
children.Remove(child.Key);
33+
if (child.Parent == this) {
34+
child.ClearParent();
35+
}
36+
return true;
37+
}
38+
/// <inheritdoc/>
39+
[Obsolete("Use Get<T>(List<string>) instead.")]
40+
public virtual T? Get<T>(string ID) where T : IChildNode {
41+
return (T?)Get(ID);
42+
}
43+
/// <inheritdoc/>
44+
public virtual T? Get<T>(List<string> keys) where T : IChildNode {
45+
return (T?)Get(keys);
46+
}
47+
48+
/// <inheritdoc/>
49+
[Obsolete("Use Get(List<string>) instead.")]
50+
public virtual IChildNode? Get(string ID) {
51+
return Get([.. ID.Split('.')]);
52+
}
53+
54+
/// <inheritdoc/>
55+
public virtual IChildNode? Get(List<string> keys) {
56+
if (children.Count < 1) {
57+
return null;
58+
}
59+
IChildNode? child = children!.GetValueOrDefault(keys[0]);
60+
if (child == null) {
61+
return null;
62+
}
63+
keys.RemoveAt(0);
64+
if (keys.Count <= 0) {
65+
return child;
66+
}
67+
if (child is IParentNode childParent) {
68+
return childParent.Get(keys);
69+
}
70+
return null;
71+
}
72+
}

0 commit comments

Comments
 (0)