Exemplo n.º 1
0
        private IEnumerable <string> EnumerateIndexedWords(IndexNode node)
        {
            if (node.IntraNodeText.Length > 0)
            {
                this.navigatedWith.Append(node.IntraNodeText);
            }

            if (node.HasMatches)
            {
                yield return(this.navigatedWith.ToString());
            }

            if (node.HasChildNodes)
            {
                foreach (var childNode in node.ChildNodes)
                {
                    this.navigatedWith.Append(childNode.Key);
                    foreach (var result in this.EnumerateIndexedWords(childNode.Value))
                    {
                        yield return(result);
                    }

                    this.navigatedWith.Length -= 1;
                }
            }

            if (node.IntraNodeText.Length > 0)
            {
                this.navigatedWith.Length -= node.IntraNodeText.Length;
            }
        }
    public IEnumerable <ITreeNode> GetChildren(bool refresh)
    {
        var commandText = "select index_name from sys.all_indexes where owner = '{0}' and table_name = '{1}' order by index_name";

        commandText = string.Format(commandText, _table.Schema.Name, _table.Name);
        var command = new OracleCommand(commandText, _table.Schema.SchemasNode.Connection);

        command.FetchSize = 256 * 1024;
        var dataTable = command.ExecuteDataTable(CancellationToken.None);
        var count     = dataTable.Rows.Count;
        var indexes   = new string[count];

        for (var i = 0; i < count; i++)
        {
            var name = (string)dataTable.Rows[i][0];
            indexes[i] = name;
        }

        var treeNodes = new ITreeNode[indexes.Length];

        for (var i = 0; i < indexes.Length; i++)
        {
            treeNodes[i] = new IndexNode(_table, indexes[i]);
        }

        return(treeNodes);
    }
Exemplo n.º 3
0
        public void ImportPath(string source, string dest)
        {
            var sourceInfo = new DirectoryInfo(source);

            if (!sourceInfo.Exists)
            {
                throw new ArgumentException("Source folder does not exist", nameof(source));
            }
            if (!dest.StartsWith('/'))
            {
                throw new ArgumentException("Destination path must start with /", nameof(dest));
            }
            var       destPath = dest.TrimEnd('/').Split("/");
            IndexNode destRoot = null;

            for (var i = 0; i < destPath.Length; i++)
            {
                destRoot = GetOrCreateDir(destPath[i], destRoot?.ParentID ?? 0);
            }
            CopyIn(sourceInfo, destRoot);

            void CopyIn(DirectoryInfo dirInfo, IndexNode parent)
            {
                foreach (var childDirInfo in dirInfo.EnumerateDirectories())
                {
                    var childDir = GetOrCreateDir(childDirInfo.Name, parent.ID);
                    CopyIn(childDirInfo, childDir);
                }

                foreach (var fileInfo in dirInfo.EnumerateFiles())
                {
                    AddFile(fileInfo, parent);
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        ///     Executes an index opeation.
        /// </summary>
        public SkryptObject ExecuteIndex(IndexNode node, ScopeContext scopeContext)
        {
            var arguments = new List <SkryptObject>();

            foreach (var subNode in node.Arguments)
            {
                var result = ExecuteExpression(subNode, scopeContext);

                arguments.Add(result);
            }

            var Object = ExecuteExpression(node.Getter, scopeContext);

            // Dynamically change type so we can get it's actual operations.
            dynamic left = Convert.ChangeType(Object, Object.GetType());

            Operation opLeft = SkryptObject.GetOperation(Operators.Index, Object.GetType(), arguments[0].GetType(), left.Operations);

            OperationDelegate operation = null;

            if (opLeft != null)
            {
                operation = opLeft.OperationDelegate;
            }
            else
            {
                _engine.ThrowError("No such operation as index " + left.Name + "!", node.Getter.Token);
            }

            var inputArray = new List <SkryptObject>(arguments);

            inputArray.Insert(0, Object);

            return(operation(inputArray.ToArray(), _engine));
        }
Exemplo n.º 5
0
 internal void Initialize(IndexNode node, IIndexNavigatorPool pool)
 {
     this.pool                  = pool;
     this.currentNode           = node;
     this.intraNodeTextPosition = 0;
     this.navigatedWith.Length  = 0;
 }
Exemplo n.º 6
0
        public override void ReadContent(BinaryReader reader)
        {
            this.Nodes = new Dictionary<ushort, IndexNode>(this.ItemCount);

            for (var i = 0; i < this.ItemCount; i++)
            {
                var index = reader.ReadUInt16();
                var levels = reader.ReadByte();

                var node = new IndexNode(levels);

                node.Page = this;
                node.Position = new PageAddress(this.PageID, index);
                node.KeyLength = reader.ReadUInt16();
                node.Key = reader.ReadBsonValue(node.KeyLength);
                node.DataBlock = reader.ReadPageAddress();

                for (var j = 0; j < node.Prev.Length; j++)
                {
                    node.Prev[j] = reader.ReadPageAddress();
                    node.Next[j] = reader.ReadPageAddress();
                }

                this.Nodes.Add(node.Position.Index, node);
            }            
        }
Exemplo n.º 7
0
 public AdvancingFrontIndex(double min, double max, int depth)
 {
     if (depth > 5)
     {
         depth = 5;
     }
     _root = createIndex(depth);
 }
Exemplo n.º 8
0
        /// <summary>
        /// 添加索引
        /// </summary>
        public static void AddIndex(IndexNode node, List <IndexBase> indexs)
        {
            var doc = CreateDoucment(node);

            foreach (var index in indexs)
            {
                index.AddIndex(doc);
            }
        }
Exemplo n.º 9
0
        static IndexNode[] CreateIndexTable(int length)
        {
            var table = new IndexNode[length];

            for (int i = 0; i < length; i++)
            {
                table[i] = new IndexNode();
            }
            return(table);
        }
Exemplo n.º 10
0
        public IIndexNavigator Create(IndexNode node)
        {
            if (!pool.TryTake(out var navigator))
            {
                navigator = new IndexNavigator();
            }

            navigator.Initialize(node, this);
            return(navigator);
        }
Exemplo n.º 11
0
        /// <summary>
        /// 建立以节目预告名称为索引的字典
        /// </summary>
        private static List <IndexNode> CreateNewNode(LiveVideoNode video, string builder)
        {
            var flag = video.Flags.FormatListToStr(SplitArray.Line, 16);
            var list = new List <IndexNode>(video.Items.Count + 1);

            list.Add(AddNameVideo(video, builder, flag));
            foreach (var parade in video.Items)
            {
                IndexNode node         = new IndexNode();
                var       indexnames   = new List <KeyValuePair <string, string> >(5);
                var       paradename   = new KeyValuePair <string, string>(LiveEpgNode.ParadeName, parade.Language[CustomArray.LanguageArray[0]].Title);
                var       paradecnname = new KeyValuePair <string, string>(LiveEpgNode.ParadeCnName, string.Format("{0}{1}", parade.Language[CustomArray.LanguageArray[0]].Title, SplitArray.IndexCnName));
                var       flags        = new KeyValuePair <string, string>(LiveEpgNode.Flags, flag);
                var       pindex       = new KeyValuePair <string, string>(LiveEpgNode.PlatformIndex, string.Format("{0}{1}", builder, SplitArray.IndexFlag));
                var       fbcode       = new KeyValuePair <string, string>(LiveEpgNode.ForbiddenCode, string.Format("{0},{1}", string.Join(",", ForbiddenAreaCache.Instance.ForbiddenNames.Except(video.ForbiddenAreas).ToArray()), SplitArray.IndexFlag));
                indexnames.Add(paradename);
                indexnames.Add(paradecnname);
                indexnames.Add(flags);
                indexnames.Add(pindex);
                indexnames.Add(fbcode);

                List <KeyValuePair <string, string> > indexvalues = new List <KeyValuePair <string, string> >(5);
                KeyValuePair <string, string>         vid         = new KeyValuePair <string, string>(LiveEpgNode.IndexValue, parade.ID.ToString());
                KeyValuePair <string, string>         paradeid    = new KeyValuePair <string, string>(LiveEpgNode.ParadeId, parade.ID.ToString());
                KeyValuePair <string, string>         vipflag     = new KeyValuePair <string, string>(LiveEpgNode.VipFlag, video.Main.VipFlag.ToString());
                var tabletype = new KeyValuePair <string, string>(LiveEpgNode.TableType, video.Main.TableType.ToString());
                var group     = new KeyValuePair <string, string>(LiveEpgNode.IsGroup, video.IsGroup ? "1" : "0");
                indexvalues.Add(vid);
                indexvalues.Add(paradeid);
                indexvalues.Add(vipflag);
                indexvalues.Add(tabletype);
                indexvalues.Add(group);

                List <KeyValuePair <string, int> > indexnumerices  = new List <KeyValuePair <string, int> >(3);
                KeyValuePair <string, int>         bitrate         = new KeyValuePair <string, int>(LiveEpgNode.BitrateRange, video.Main.BitRate);
                KeyValuePair <string, int>         hight           = new KeyValuePair <string, int>(LiveEpgNode.HightRange, video.Main.VideoHeight);
                KeyValuePair <string, int>         width           = new KeyValuePair <string, int>(LiveEpgNode.WidthRange, video.Main.VideoWidth);
                KeyValuePair <string, int>         paradebegintime = new KeyValuePair <string, int>(LiveEpgNode.ParadeBeginTime, video.Main.VideoWidth);
                indexnumerices.Add(bitrate);
                indexnumerices.Add(hight);
                indexnumerices.Add(width);

                var indexlongs = new List <KeyValuePair <string, long> >(1);
                KeyValuePair <string, long> begintime = new KeyValuePair <string, long>(LiveEpgNode.ParadeBeginTime, parade.StartTime.Ticks);
                indexlongs.Add(begintime);

                node.IndexNames     = indexnames;
                node.IndexValues    = indexvalues;
                node.IndexNumerices = indexnumerices;
                node.IndexLongs     = indexlongs;
                list.Add(node);
            }
            return(list);
        }
Exemplo n.º 12
0
        public IndexPage(uint pageID)
        {
            PageID     = pageID;
            NextPageID = uint.MaxValue;
            Nodes      = new IndexNode[IndexPage.NODES_PER_PAGE];
            IsDirty    = false;

            for (int i = 0; i < IndexPage.NODES_PER_PAGE; i++)
            {
                Nodes[i] = new IndexNode(this);
            }
        }
Exemplo n.º 13
0
        public override Node VisitIndexLValue([NotNull] TigerParser.IndexLValueContext context)
        {
            var node = new IndexNode(context);

            var lvalue = (LValueNode)Visit(context.lvalue());

            lvalue.ByValue = true;
            node.Children.Add(lvalue);                // LVALUE

            node.Children.Add(Visit(context.expr())); // INDEX EXPRESSION
            return(node);
        }
Exemplo n.º 14
0
        private async Task WriteNodeAsync(IndexNode node)
        {
            var matchCount          = node.Matches.Count;
            var childNodeCount      = node.ChildNodes.Count;
            var intraNodeTextLength = node.IntraNodeText.Length;

            this.writer.Write(intraNodeTextLength);
            this.writer.Write(matchCount);
            this.writer.Write(childNodeCount);

            if (intraNodeTextLength > 0)
            {
Exemplo n.º 15
0
        private IndexNode <A> createIndex(int n)
        {
            IndexNode <A> node = null;

            if (n > 0)
            {
                node         = new IndexNode <A>();
                node.bigger  = createIndex(n - 1);
                node.smaller = createIndex(n - 1);
            }
            return(node);
        }
Exemplo n.º 16
0
        public IndexPage(uint pageID)
        {
            PageID = pageID;
            NextPageID = uint.MaxValue; 
            Nodes = new IndexNode[IndexPage.NODES_PER_PAGE];
            IsDirty = false;

            for (int i = 0; i < IndexPage.NODES_PER_PAGE; i++)
            {
                Nodes[i] = new IndexNode(this);
            }
        }
Exemplo n.º 17
0
        public static void AddTag(TagNode tag, string dimension, IndexBase index)
        {
            var builder = new StringBuilder();

            //Dictionary<int, string[]> fbcodes = new Dictionary<int, string[]>(tag.Channels.Keys.Count);
            foreach (var plat in tag.Channels.Keys)
            {
                var indexx = VideoNodeKeyArray.Instance.Items[VideoNodeKeyArray.Instance.FindKey(plat.PlatForm, 0, plat.Auth)];
                builder.Append(indexx);
                builder.Append(SplitArray.DH);
                //IEnumerable<string> fbcode = null;
                //var query = tag.Channels[plat].Select(v => ListCache.Instance.Dictionary[v].ForbiddenAreas);
                //foreach (var q in query)
                //{
                //    if (fbcode == null)
                //        fbcode = q;
                //    else
                //        fbcode = fbcode.Intersect(q);
                //}
                //fbcodes[indexx] = fbcode.ToArray();
            }
            if (builder.Length < 2)
            {
                return;
            }
            var node       = new IndexNode();
            var indexnames = new List <KeyValuePair <string, string> >(3);
            var name       = new KeyValuePair <string, string>(EpgIndexNode.Name, tag.Language[CustomArray.LanguageArray[0]].Title);
            var cnname     = new KeyValuePair <string, string>(EpgIndexNode.CnName, string.Format("{0}{1}", tag.Language[CustomArray.LanguageArray[0]].Title, SplitArray.IndexCnName));
            var pindex     = new KeyValuePair <string, string>(EpgIndexNode.PlatformIndex, string.Format("{0}{1}", builder.Remove(builder.Length - 1, 1), SplitArray.IndexFlag));

            //foreach (var fbcode in fbcodes)
            //{
            //    var fb = new KeyValuePair<string, string>(string.Format("{0}{1}", EpgIndexNode.ForbiddenCode, fbcode.Key), string.Format("{0},{1}", string.Join(",", ForbiddenAreaCache.Instance.ForbiddenNames.Except(fbcode.Value).ToArray()), SplitArray.IndexFlag));
            //    indexnames.Add(fb);
            //}
            indexnames.Add(name);
            indexnames.Add(cnname);
            indexnames.Add(pindex);

            var indexvalues = new List <KeyValuePair <string, string> >(2);
            var vid         = new KeyValuePair <string, string>(EpgIndexNode.IndexValue, tag.Language[CustomArray.LanguageArray[0]].Title);
            var dimen       = new KeyValuePair <string, string>(EpgIndexNode.Flags, dimension);

            indexvalues.Add(vid);
            indexvalues.Add(dimen);

            node.IndexNames  = indexnames;
            node.IndexValues = indexvalues;

            LuceneNetUtils.AddIndex(node, index);
        }
Exemplo n.º 18
0
 public AstNode VisitIndex(IndexNode n)
 {
     Visit(n.Instance);
     Append("[");
     Visit(n.Arguments[0]);
     for (var i = 1; i < n.Arguments.Count; i++)
     {
         Append(", ");
         Visit(n.Arguments[i]);
     }
     Append("]");
     return(n);
 }
Exemplo n.º 19
0
    public void Parse_SafeAccessor_Index()
    {
        ASTNode root = ExpressionParser.Parse("instance?[1]");
        MemberAccessExpressionNode node = AssertInstanceOfAndReturn <MemberAccessExpressionNode>(root);

        Assert.AreEqual(1, node.parts.Count);
        Assert.AreEqual("instance", node.identifier);
        Assert.IsInstanceOf <IndexNode>(node.parts[0]);
        IndexNode indexNode = (IndexNode)node.parts[0];

        Assert.IsTrue(indexNode.isNullableAccess);
        Assert.AreEqual(1, indexNode.arguments.Count);
        Assert.IsInstanceOf <LiteralNode>(indexNode.arguments[0]);
    }
Exemplo n.º 20
0
        public void WithCustomIndexNodeFactory_ShouldPassCustomImplementationToIndex()
        {
            var factory      = new Mock <IIndexNodeFactory>();
            var expectedRoot = new IndexNode(null, null, null);

            factory.Setup(f => f.CreateRootNode()).Returns(expectedRoot);

            this.sut.WithIndexNodeFactory(factory.Object);

            var index = this.sut.Build();

            index.Root.Should().Be(expectedRoot);
            factory.Verify(f => f.CreateRootNode(), Times.Once);
        }
Exemplo n.º 21
0
        //private TypeInfo Visit(PlusNode node)
        //{
        //    return BinaryOperator(node, "Plus");
        //}
        //private TypeInfo Visit(MinusNode node)
        //{
        //    return BinaryOperator(node, "Minus");
        //}
        //private TypeInfo Visit(MultNode node)
        //{
        //    return BinaryOperator(node, "Mul");
        //}
        //private TypeInfo Visit(DivNode node)
        //{
        //    return BinaryOperator(node, "Div");
        //}
        //private TypeInfo Visit(ModNode node)
        //{
        //    return BinaryOperator(node, "Mod");
        //}
        //private TypeInfo Visit(EqualNode node)
        //{
        //    return BinaryOperator(node, "Equals");
        //}
        //private TypeInfo Visit(NotEqualNode node)
        //{
        //    return BinaryOperator(node, "NotEquals");
        //}
        private void Visit(IndexNode node)
        {
            var where = node.Where;
            var args = new List <BasicNode>()
            {
                node.Index
            };

            var method = new MethodCallNode("Get", where, args);

            ReplaceNode(node.Parent, node.ChildIndex, method);

            Visit(where as dynamic);
            Visit(node.Index as dynamic);
        }
Exemplo n.º 22
0
        private void WriteMatchLocations(IndexNode node)
        {
            foreach (var match in node.Matches)
            {
                this.writer.Write(match.Key);
                this.writer.Write(match.Value.Count);

                foreach (var fieldMatch in match.Value)
                {
                    this.writer.Write(fieldMatch.FieldId);
                    this.writer.Write(fieldMatch.Locations.Count);
                    this.WriteWordLocations(fieldMatch);
                }
            }
        }
            internal override Node SetItem(int index, T value)
            {
                int  pageIndex = FindLowerBound(_offsets, _nodeCount, index);
                Node node      = _nodes[pageIndex].SetItem(index - _offsets[pageIndex], value);

                if (node == _nodes[pageIndex])
                {
                    return(this);
                }

                IndexNode mutableNode = AsMutable();

                mutableNode._nodes[pageIndex] = node;
                return(mutableNode);
            }
Exemplo n.º 24
0
        /// <summary>
        /// 创建Document
        /// </summary>
        public static Document CreateDoucment(IndexNode node)
        {
            Document doc = new Document();

            foreach (KeyValuePair <string, string> name in node.IndexNames)
            {
                var field = new Field(name.Key, name.Value, Field.Store.YES, Field.Index.ANALYZED);
                var boost = SettingCache.Boost;
                switch (name.Key)
                {
                case "name":
                    break;

                case "catalog":
                    boost = boost - 4; break;

                case "act":
                    boost = boost - 8; break;

                case "area":
                    boost = boost - 16; break;
                }
                field.SetBoost(boost);
                doc.Add(field);
            }
            foreach (KeyValuePair <string, string> value in node.IndexValues)
            {
                doc.Add(new Field(value.Key, value.Value, Field.Store.YES, Field.Index.NOT_ANALYZED));
            }
            if (node.IndexNumerices != null)
            {
                foreach (KeyValuePair <string, int> numeric in node.IndexNumerices)
                {
                    doc.Add(new NumericField(numeric.Key, Field.Store.YES, true).SetIntValue(numeric.Value));
                }
            }
            if (node.IndexLongs != null)
            {
                foreach (var numeric in node.IndexLongs)
                {
                    doc.Add(new NumericField(numeric.Key, Field.Store.YES, true).SetLongValue(numeric.Value));
                }
            }
            return(doc);
        }
Exemplo n.º 25
0
        public void WriteFS(FileSystem fs)
        {
            fs.UpdateBitmap();
            WriteHeader(fs.Header);
            var emptyIndex = new IndexNode();

            for (long i = 0; i < fs.IndexNodes.LongLength; i++)
            {
                WriteIndex(fs.IndexNodes[i] ?? emptyIndex);
            }
            WriteBitmap(fs.Bitmap);
            var emptyData = new DataSector();

            for (long i = 0; i < fs.DataSectors.LongLength; i++)
            {
                WriteData(fs.DataSectors[i]);
            }
        }
Exemplo n.º 26
0
 public void AddNode(int lIdx, int vIdx)
 {
     try
     {
         if (this.CanBeAdded(lIdx, vIdx))
         {
             IndexNode item = new IndexNode {
                 ListIndex = lIdx,
                 ViewIndex = vIdx
             };
             this.idxList.Add(item);
         }
     }
     catch (Exception exception)
     {
         MessageBox.Show("Error at DeviceIndexTable.AddNode!\n" + exception.Message);
     }
 }
            internal static Node InsertRange(Node root, int index, IEnumerable <T> collection)
            {
                if (collection == null)
                {
                    throw new ArgumentNullException(nameof(collection));
                }
                if (index < 0)
                {
                    throw new ArgumentOutOfRangeException(nameof(index));
                }
                if (index > root.Count)
                {
                    throw new ArgumentOutOfRangeException();
                }

                // We can't insert a range into the empty node
                if (root == Empty)
                {
                    root = new LeafNode();
                }

                ImmutableTreeList <Node> .Node?splitNode = root.InsertRange(index == root.Count, index, collection);
                while (splitNode != null)
                {
                    if (splitNode.Count == 1)
                    {
                        root = splitNode[0];
                        break;
                    }
                    else
                    {
                        // Make a new level, walking nodes on the previous root level from 'node' to 'splitNode'
                        IndexNode newRoot = new IndexNode(splitNode, out splitNode);
                        root = newRoot;
                    }
                }

                if (root.Count == 0)
                {
                    return(Empty);
                }

                return(root);
            }
Exemplo n.º 28
0
        /// <summary>
        /// 建立以频道名称为索引的字典
        /// </summary>
        private static IndexNode AddNameVideo(LiveVideoNode video, string builder, string flag)
        {
            IndexNode node       = new IndexNode();
            var       indexnames = new List <KeyValuePair <string, string> >(5);
            var       name       = new KeyValuePair <string, string>(LiveEpgNode.Name, video.ChannelName);
            var       cnname     = new KeyValuePair <string, string>(LiveEpgNode.CnName, string.Format("{0}{1}", video.ChannelName, SplitArray.IndexCnName));
            var       flags      = new KeyValuePair <string, string>(LiveEpgNode.Flags, flag);
            var       pindex     = new KeyValuePair <string, string>(LiveEpgNode.PlatformIndex, string.Format("{0}{1}", builder, SplitArray.IndexFlag));
            var       fbcode     = new KeyValuePair <string, string>(LiveEpgNode.ForbiddenCode, string.Format("{0},{1}", string.Join(",", ForbiddenAreaCache.Instance.ForbiddenNames.Except(video.ForbiddenAreas).ToArray()), SplitArray.IndexFlag));

            indexnames.Add(name);
            indexnames.Add(cnname);
            indexnames.Add(flags);
            indexnames.Add(pindex);
            indexnames.Add(fbcode);

            List <KeyValuePair <string, string> > indexvalues = new List <KeyValuePair <string, string> >(5);
            KeyValuePair <string, string>         vid         = new KeyValuePair <string, string>(LiveEpgNode.IndexValue, video.Main.ChannelID.ToString());
            KeyValuePair <string, string>         channelid   = new KeyValuePair <string, string>(LiveEpgNode.ChannelId, video.Main.ChannelID.ToString());
            KeyValuePair <string, string>         vipflag     = new KeyValuePair <string, string>(LiveEpgNode.VipFlag, video.Main.VipFlag.ToString());
            var tabletype = new KeyValuePair <string, string>(LiveEpgNode.TableType, video.Main.TableType.ToString());
            var group     = new KeyValuePair <string, string>(LiveEpgNode.IsGroup, video.IsGroup ? "1" : "0");

            indexvalues.Add(vid);
            indexvalues.Add(channelid);
            indexvalues.Add(vipflag);
            indexvalues.Add(tabletype);
            indexvalues.Add(group);

            List <KeyValuePair <string, int> > indexnumerices = new List <KeyValuePair <string, int> >(3);
            KeyValuePair <string, int>         bitrate        = new KeyValuePair <string, int>(LiveEpgNode.BitrateRange, video.Main.BitRate);
            KeyValuePair <string, int>         hight          = new KeyValuePair <string, int>(LiveEpgNode.HightRange, video.Main.VideoHeight);
            KeyValuePair <string, int>         width          = new KeyValuePair <string, int>(LiveEpgNode.WidthRange, video.Main.VideoWidth);

            indexnumerices.Add(bitrate);
            indexnumerices.Add(hight);
            indexnumerices.Add(width);

            node.IndexNames     = indexnames;
            node.IndexValues    = indexvalues;
            node.IndexNumerices = indexnumerices;
            return(node);
        }
Exemplo n.º 29
0
        private async Task WriteNodeAsync(IndexNode node)
        {
            var matchCount          = node.Matches.Count;
            var childNodeCount      = node.ChildNodes.Count;
            var intraNodeTextLength = node.IntraNodeText.Length;

            this.writer.Write(intraNodeTextLength);
            this.writer.Write(matchCount);
            this.writer.Write(childNodeCount);

            if (intraNodeTextLength > 0)
            {
                void WriteIntraNodeText(BinaryWriter writer, ReadOnlySpan <char> span)
                {
                    for (var i = 0; i < span.Length; i++)
                    {
                        writer.Write(span[i]);
                    }
                }

                WriteIntraNodeText(this.writer, node.IntraNodeText.Span);
            }

            if (childNodeCount > 0)
            {
                foreach (var childNode in node.ChildNodes)
                {
                    this.writer.Write(childNode.Key);
                    await this.WriteNodeAsync(childNode.Value).ConfigureAwait(false);
                }
            }

            if (matchCount > 0)
            {
                this.WriteMatchLocations(node);
            }

            if (childNodeCount > 0)
            {
                await this.FlushAsync().ConfigureAwait(false);
            }
        }
Exemplo n.º 30
0
            internal IndexNode(int branchingFactor, Node firstChild, Node lastChild, out IndexNode lastNode)
                : this(branchingFactor)
            {
                lastNode = null;
                for (Node current = firstChild; current != null; current = current == lastChild ? null : current.NextNode)
                {
                    if (_nodeCount == _nodes.Length)
                    {
                        // TODO: Avoid recursion here
                        _next = new IndexNode(branchingFactor, current, lastChild, out lastNode);
                        break;
                    }

                    _nodes[_nodeCount]   = current;
                    _offsets[_nodeCount] = _count;
                    _count += current.Count;
                    _nodeCount++;
                }

                lastNode = lastNode ?? this;
            }
Exemplo n.º 31
0
        /// <summary>
        /// Create a new index and returns head page address (skip list)
        /// </summary>
        public CollectionIndex CreateIndex(CollectionPage col)
        {
            // get index slot
            var index = col.GetFreeIndex();

            // get a new index page for first index page
            var page = _pager.NewPage<IndexPage>();

            // create a empty node with full max level
            var head = new IndexNode(IndexNode.MAX_LEVEL_LENGTH)
            {
                Key = BsonValue.MinValue,
                KeyLength = (ushort)BsonValue.MinValue.GetBytesCount(false),
                Slot = (byte)index.Slot,
                Page = page,
                Position = new PageAddress(page.PageID, 0)
            };

            // add as first node
            page.Nodes.Add(head.Position.Index, head);

            // update freebytes + item count (for head)
            page.UpdateItemCount();

            // set index page as dirty
            _pager.SetDirty(index.Page);

            // add indexPage on freelist if has space
            _pager.AddOrRemoveToFreeList(true, page, index.Page, ref index.FreeIndexPageID);

            // point the head/tail node to this new node position
            index.HeadNode = head.Position;

            // insert tail node
            var tail = this.AddNode(index, BsonValue.MaxValue, IndexNode.MAX_LEVEL_LENGTH, null);

            index.TailNode = tail.Position;

            return index;
        }
Exemplo n.º 32
0
        public Node(T value, long data, Node <T> parent, ObjectStorage <IndexNode> indexStorage, ObjectStorage <T> dataStorage, bool duplicable = false)
        {
            _parent       = parent;
            _indexStorage = indexStorage;
            _dataStorage  = dataStorage;
            _duplicable   = duplicable;
            _duplicates   = duplicable ? new List <long>() : null;
            _data         = data > 0 ? data : dataStorage.WriteObject(value);
            _left         = 0;
            _right        = 0;

            var indexNode = new IndexNode
            {
                Data           = _data,
                Left           = _left,
                Right          = _right,
                DuplicateCount = 0,
                Duplicates     = new long[0]
            };

            _offset = indexStorage.WriteObject(indexNode);
        }
Exemplo n.º 33
0
        private IndexNode GetOrCreateDir(string name, uint parentId)
        {
            var node = FS.GetChildren(parentId).FirstOrDefault(x => x.Name == name);

            if (node == null)
            {
                node = new IndexNode
                {
                    ID       = FS.GenerateIndexId(),
                    Flags    = IndexFlags.Directory | IndexFlags.Valid,
                    ParentID = parentId,
                    Name     = name
                };
                FS.InsertIndexNode(node);
                return(node);
            }
            if (!node.Flags.HasFlag(IndexFlags.Directory))
            {
                throw new InvalidOperationException("existing index record is not a directory");
            }
            return(node);
        }
Exemplo n.º 34
0
        private long Save()
        {
            var duplicateCount = _duplicable ? _duplicates.Count : 0;
            var duplicates     = _duplicable ? _duplicates.ToArray() : new long[0];

            var indexNode = new IndexNode
            {
                Data           = _data,
                Left           = _left,
                Right          = _right,
                DuplicateCount = duplicateCount,
                Duplicates     = duplicates
            };

            var offset = _indexStorage.UpdateObject(_offset, indexNode);

            if (offset != _offset)
            {
                _parent?.UpdateChild(_offset, offset);
                _offset = offset;
            }

            return(offset);
        }
Exemplo n.º 35
0
        /// <summary>
        /// Get all node list from any index node (go fordward and backward)
        /// </summary>
        public IEnumerable<IndexNode> GetNodeList(IndexNode node, bool includeInitial)
        {
            var next = node.NextNode;
            var prev = node.PrevNode;

            // returns some inital node
            if (includeInitial) yield return node;

            // go fordward
            while (next.IsEmpty == false)
            {
                var n = this.GetNode(next);
                next = n.NextNode;
                yield return n;
            }

            // go backward
            while (prev.IsEmpty == false)
            {
                var p = this.GetNode(prev);
                prev = p.PrevNode;
                yield return p;
            }
        }
Exemplo n.º 36
0
 /// <summary>
 /// Insert a new node index inside an collection index. Flip coin to know level
 /// </summary>
 public IndexNode AddNode(CollectionIndex index, BsonValue key, IndexNode last)
 {
     // call AddNode with key value
     return this.AddNode(index, key, this.FlipCoin(), last);
 }
Exemplo n.º 37
0
        /// <summary>
        /// Go first/last occurence of this index value
        /// </summary>
        private IndexNode FindBoundary(CollectionIndex index, IndexNode cur, BsonValue value, int order, int level)
        {
            var last = cur;

            while (cur.Key.CompareTo(value) == 0)
            {
                last = cur;
                cur = this.GetNode(cur.NextPrev(0, order));
                if (cur.IsHeadTail(index)) break;
            }

            return last;
        }
Exemplo n.º 38
0
 static IndexNode[] CreateIndexTable(int length)
 {
     var table = new IndexNode[length];
     for (int i = 0; i < length; i++)
     {
         table[i] = new IndexNode();
     }
     return table;
 }
Exemplo n.º 39
0
        /// <summary>
        /// Insert a new node index inside an collection index.
        /// </summary>
        private IndexNode AddNode(CollectionIndex index, BsonValue key, byte level)
        {
            // creating a new index node 
            var node = new IndexNode(level)
            { 
                Key = key, 
                KeyLength = key.GetBytesCount()
            };

            if (node.KeyLength > MAX_INDEX_LENGTH)
            {
                throw LiteException.IndexKeyTooLong();
            }

            // get a free page to insert my index node
            var page = _pager.GetFreePage<IndexPage>(index.FreeIndexPageID, node.Length);

            node.Position = new PageAddress(page.PageID, page.Nodes.NextIndex());
            node.Page = page;

            // add index node to page
            page.Nodes.Add(node.Position.Index, node);

            // update freebytes + items count
            page.UpdateItemCount();

            // now, let's link my index node on right place
            var cur = this.GetNode(index.HeadNode);

            // scan from top left
            for (var i = IndexNode.MAX_LEVEL_LENGTH - 1; i >= 0; i--)
            {
                // for(; <while_not_this>; <do_this>) { ... }
                for (; cur.Next[i].IsEmpty == false; cur = this.GetNode(cur.Next[i]))
                {
                    // read next node to compare
                    var diff = this.GetNode(cur.Next[i]).Key.CompareTo(key);

                    // if unique and diff = 0, throw index exception (must rollback transaction - others nodes can be dirty)
                    if (diff == 0 && index.Options.Unique) throw LiteException.IndexDuplicateKey(index.Field, key);

                    if (diff == 1) break;
                }

                if (i <= (level - 1)) // level == length
                {
                    // cur = current (imediatte before - prev)
                    // node = new inserted node 
                    // next = next node (where cur is poiting)

                    node.Next[i] = cur.Next[i];
                    node.Prev[i] = cur.Position;
                    cur.Next[i] = node.Position;

                    var next = this.GetNode(node.Next[i]);

                    if (next != null)
                    {
                        next.Prev[i] = node.Position;

                        next.Page.IsDirty = true;
                    }

                    cur.Page.IsDirty = true;
                }
            }

            // add/remove indexPage on freelist if has space
            _pager.AddOrRemoveToFreeList(page.FreeBytes > IndexPage.INDEX_RESERVED_BYTES, page, index.Page, ref index.FreeIndexPageID);

            page.IsDirty = true;

            return node;
        }
Exemplo n.º 40
0
        /// <summary>
        /// Insert a new node index inside an collection index.
        /// </summary>
        private IndexNode AddNode(CollectionIndex index, BsonValue key, byte level, IndexNode last)
        {
            // calc key size
            var keyLength = key.GetBytesCount(false);

            if (keyLength > MAX_INDEX_LENGTH)
            {
                throw LiteException.IndexKeyTooLong();
            }

            // creating a new index node
            var node = new IndexNode(level)
            {
                Key = key,
                KeyLength = (ushort)keyLength,
                Slot = (byte)index.Slot
            };

            // get a free page to insert my index node
            var page = _pager.GetFreePage<IndexPage>(index.FreeIndexPageID, node.Length);

            node.Position = new PageAddress(page.PageID, page.Nodes.NextIndex());
            node.Page = page;

            // add index node to page
            page.Nodes.Add(node.Position.Index, node);

            // update freebytes + items count
            page.UpdateItemCount();

            // now, let's link my index node on right place
            var cur = this.GetNode(index.HeadNode);

            // using as cache last
            IndexNode cache = null;

            // scan from top left
            for (var i = IndexNode.MAX_LEVEL_LENGTH - 1; i >= 0; i--)
            {
                // get cache for last node
                cache = cache != null && cache.Position.Equals(cur.Next[i]) ? cache : this.GetNode(cur.Next[i]);

                // for(; <while_not_this>; <do_this>) { ... }
                for (; cur.Next[i].IsEmpty == false; cur = cache)
                {
                    // get cache for last node
                    cache = cache != null && cache.Position.Equals(cur.Next[i]) ? cache : this.GetNode(cur.Next[i]);

                    // read next node to compare
                    var diff = cache.Key.CompareTo(key);

                    // if unique and diff = 0, throw index exception (must rollback transaction - others nodes can be dirty)
                    if (diff == 0 && index.Unique) throw LiteException.IndexDuplicateKey(index.Field, key);

                    if (diff == 1) break;
                }

                if (i <= (level - 1)) // level == length
                {
                    // cur = current (imediatte before - prev)
                    // node = new inserted node
                    // next = next node (where cur is poiting)
                    _pager.SetDirty(cur.Page);

                    node.Next[i] = cur.Next[i];
                    node.Prev[i] = cur.Position;
                    cur.Next[i] = node.Position;

                    var next = this.GetNode(node.Next[i]);

                    if (next != null)
                    {
                        next.Prev[i] = node.Position;
                        _pager.SetDirty(next.Page);
                    }
                }
            }

            // add/remove indexPage on freelist if has space
            _pager.AddOrRemoveToFreeList(page.FreeBytes > IndexPage.INDEX_RESERVED_BYTES, page, index.Page, ref index.FreeIndexPageID);

            // if last node exists, create a double link list
            if (last != null)
            {
                // link new node with last node
                if (last.NextNode.IsEmpty == false)
                {
                    // fix link pointer with has more nodes in list
                    var next = this.GetNode(last.NextNode);
                    next.PrevNode = node.Position;
                    last.NextNode = node.Position;
                    node.PrevNode = last.Position;
                    node.NextNode = next.Position;

                    _pager.SetDirty(next.Page);
                }
                else
                {
                    last.NextNode = node.Position;
                    node.PrevNode = last.Position;
                }

                // set last node page as dirty
                _pager.SetDirty(last.Page);
            }

            return node;
        }