private string PathFromRoot(SimpleCacheItem root) { var size = 1; while (root.Name != null) { size += root.Name.Length + 1; _pathFromRootList.AddFirst(root); root = root.Parent; } if (_pathFromRootList.Count == 0) { return("/"); } var sb = new StringBuilder(size); foreach (var d in _pathFromRootList) { sb.AppendFormat("/{0}", d.Name); } _pathFromRootList.Clear(); return(sb.ToString()); }
private bool RemoveChildFromParent(SimpleCacheItem child) { if (child.Parent != null) { return(child.Parent.Children.Remove(child)); } return(false); }
private static SimpleCacheItem GetNamedChild(SimpleCacheItem root, string segment) { var newRoot = root.Children.FirstOrDefault(c => c.Name == segment); if (newRoot == null) { newRoot = new SimpleCacheItem {Name = segment, Parent = root, Created = true}; root.Children.Add(newRoot); } return newRoot; }
private void DeleteRootOrChildren(SimpleCacheItem root) { if (root.Parent == null && root.Children.Count == 0) { // We don't have other choices than removing root root = null; OnRemoved(new ValueRemovedEventArgs("/")); } else { DeleteChild(root); } }
private void UpdateChildren(SimpleCacheItem root, JsonReader reader, bool replace = false) { if (replace) { DeleteChild(root); if (root.Parent != null) { root.Parent.Children.Add(root); } } while (reader.Read()) { switch (reader.TokenType) { case JsonToken.PropertyName: var segment = reader.Value.ToString(); var child = GetNamedChild(root, segment); UpdateChildren(child, reader); // Re-entrant, child become new cached root. break; case JsonToken.Boolean: case JsonToken.Bytes: case JsonToken.Date: case JsonToken.Float: case JsonToken.Integer: case JsonToken.String: if (root.Created) { root.Value = reader.Value.ToString(); var rootPath = PathFromRoot(root); OnAdded(new ValueAddedEventArgs(rootPath, root.Value)); root.Created = false; } else { var oldData = root.Value; root.Value = reader.Value.ToString(); var rootPath = PathFromRoot(root); OnChanged(new ValueChangedEventArgs(rootPath, root.Value, oldData)); } return; case JsonToken.Null: DeleteRootOrChildren(root); return; } } }
private void UpdateChildren(SimpleCacheItem root, JsonReader reader, bool replace = false) { if (replace) { DeleteChild(root); if (root.Parent != null) { root.Parent.Children.Add(root); } } while (reader.Read()) { switch (reader.TokenType) { case JsonToken.PropertyName: UpdateChildren(GetNamedChild(root, reader.Value.ToString()), reader); break; case JsonToken.Boolean: case JsonToken.Bytes: case JsonToken.Date: case JsonToken.Float: case JsonToken.Integer: case JsonToken.String: if (root.Created) { root.Value = reader.Value.ToString(); OnAdded(new ValueAddedEventArgs(PathFromRoot(root), reader.Value.ToString())); root.Created = false; } else { var oldData = root.Value; root.Value = reader.Value.ToString(); OnUpdated(new ValueChangedEventArgs(PathFromRoot(root), root.Value, oldData)); } return; case JsonToken.Null: DeleteChild(root); return; case JsonToken.EndObject: root = root.Parent; return; } } }
private static SimpleCacheItem GetNamedChild(SimpleCacheItem root, string segment) { var newRoot = root.Children.FirstOrDefault(c => c.Name == segment); if (newRoot == null) { newRoot = new SimpleCacheItem { Name = segment, Parent = root, Created = true }; root.Children.Add(newRoot); } return(newRoot); }
private void UpdateChildren(SimpleCacheItem root, JsonReader reader, bool replace = false) { if (replace) { DeleteChild(root); if (root.Parent != null) { root.Parent.Children.Add(root); } } while (reader.Read()) { switch (reader.TokenType) { case JsonToken.PropertyName: UpdateChildren(GetNamedChild(root, reader.Value.ToString()), reader); break; case JsonToken.Boolean: case JsonToken.Bytes: case JsonToken.Date: case JsonToken.Float: case JsonToken.Integer: case JsonToken.String: if (root.Created) { root.Value = reader.Value.ToString(); OnAdded(new ValueAddedEventArgs(PathFromRoot(root), reader.Value.ToString())); root.Created = false; } else { var oldData = root.Value; root.Value = reader.Value.ToString(); OnUpdated(new ValueChangedEventArgs(PathFromRoot(root), root.Value, oldData)); } return; case JsonToken.Null: DeleteChild(root); return; } } }
private void DeleteChild(SimpleCacheItem root) { if (root.Parent != null) { if (RemoveChildFromParent(root)) { OnRemoved(new ValueRemovedEventArgs(PathFromRoot(root))); } } else { foreach (var child in root.Children.ToArray()) { RemoveChildFromParent(child); OnRemoved(new ValueRemovedEventArgs(PathFromRoot(child))); } } }
private void Replace(SimpleCacheItem root, JsonReader reader) { UpdateChildren(root, reader, true); }
private bool RemoveChildFromParent(SimpleCacheItem child) { if (child.Parent != null) { return child.Parent.Children.Remove(child); } return false; }
private string PathFromRoot(SimpleCacheItem root) { var size = 1; while (root.Name != null) { size += root.Name.Length + 1; _pathFromRootList.AddFirst(root); root = root.Parent; } if (_pathFromRootList.Count == 0) { return "/"; } var sb = new StringBuilder(size); foreach (var d in _pathFromRootList) { sb.Append($"/{d.Name}"); } _pathFromRootList.Clear(); return sb.ToString(); }