示例#1
0
        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());
        }
示例#2
0
        private bool RemoveChildFromParent(SimpleCacheItem child)
        {
            if (child.Parent != null)
            {
                return(child.Parent.Children.Remove(child));
            }

            return(false);
        }
示例#3
0
        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;
        }
示例#4
0
 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);
     }
 }
示例#5
0
        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;
                }
            }
        }
示例#6
0
        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;
                }
            }
        }
示例#7
0
        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;
                }
            }
        }
示例#9
0
 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)));
         }
     }
 }
示例#10
0
 private void Replace(SimpleCacheItem root, JsonReader reader)
 {
     UpdateChildren(root, reader, true);
 }
示例#11
0
 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)));
         }
     }
 }
示例#12
0
 private void Replace(SimpleCacheItem root, JsonReader reader)
 {
     UpdateChildren(root, reader, true);
 }
示例#13
0
        private bool RemoveChildFromParent(SimpleCacheItem child)
        {
            if (child.Parent != null)
            {
                return child.Parent.Children.Remove(child);
            }

            return false;
        }
示例#14
0
        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();
        }