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

            return(false);
        }
Esempio n. 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;
        }
Esempio n. 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);
     }
 }
Esempio n. 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;
                }
            }
        }
Esempio n. 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;
                }
            }
        }
Esempio n. 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;
                }
            }
        }
Esempio n. 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)));
         }
     }
 }
Esempio n. 10
0
 private void Replace(SimpleCacheItem root, JsonReader reader)
 {
     UpdateChildren(root, reader, true);
 }
Esempio n. 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)));
         }
     }
 }
Esempio n. 12
0
 private void Replace(SimpleCacheItem root, JsonReader reader)
 {
     UpdateChildren(root, reader, true);
 }
Esempio n. 13
0
        private bool RemoveChildFromParent(SimpleCacheItem child)
        {
            if (child.Parent != null)
            {
                return child.Parent.Children.Remove(child);
            }

            return false;
        }
Esempio n. 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();
        }