Example #1
0
        internal void InitializeCreate(EncodingDefinition treeNodeType, int blockSize)
        {
            if (m_isInitialized)
            {
                throw new Exception("Duplicate calls to Initialize");
            }
            if ((object)treeNodeType == null)
            {
                throw new ArgumentNullException("treeNodeType");
            }


            m_isInitialized = true;

            m_header.TreeNodeType = treeNodeType;
            m_header.BlockSize    = blockSize;

            m_header.RootNodeLevel        = 0;
            m_header.RootNodeIndexAddress = 1;
            m_header.LastAllocatedBlock   = 1;

            Initialize();

            LeafStorage.CreateEmptyNode(m_header.RootNodeIndexAddress);
            m_header.IsDirty = true;
            m_header.SaveHeader(Stream);
        }
Example #2
0
        /// <summary>
        /// Creates a new root node by combining the current root node with the provided node data.
        /// </summary>
        /// <param name="leafKey"></param>
        /// <param name="leafNodeIndex"></param>
        private void CreateNewRootNode(TKey leafKey, uint leafNodeIndex)
        {
            if (RootNodeLevel + 1 > 250)
            {
                throw new Exception("Tree is full. Tree cannot exceede 250 levels in depth.");
            }
            int nodeLevel = RootNodeLevel + 1;

            if (nodeLevel > m_nodes.Length)
            {
                SetCapacity(nodeLevel);
            }

            //Get the ID for the new root node.
            uint oldRootNode = RootNodeIndexAddress;

            RootNodeIndexAddress = m_getNextNewNodeIndex();
            RootNodeLevel       += 1;

            //Create the empty node
            SortedTreeNodeBase <TKey, SnapUInt32> rootNode = GetNode(RootNodeLevel);

            rootNode.CreateEmptyNode(RootNodeIndexAddress);

            //Insert the first entry in the root node.
            m_tmpKey.SetMin();
            m_tmpValue.Value = oldRootNode;
            rootNode.TryInsert(m_tmpKey, m_tmpValue);

            //Insert the second entry in the root node.
            m_tmpValue.Value = leafNodeIndex;
            rootNode.TryInsert(leafKey, m_tmpValue);

            OnRootHasChanged();
            //foreach (var node in m_nodes)
            //    node.Clear();
        }
Example #3
0
        public static void TestNode <TKey, TValue>(SortedTreeNodeBase <TKey, TValue> node, TreeNodeRandomizerBase <TKey, TValue> randomizer, int count)
            where TKey : SnapTypeBase <TKey>, new()
            where TValue : SnapTypeBase <TValue>, new()
        {
            int  Max       = count;
            uint rootKey   = 0;
            byte rootLevel = 0;

            uint        nextKeyIndex = 2;
            bool        hasChanged   = false;
            Func <uint> getNextKey   = () =>
            {
                nextKeyIndex++;
                return(nextKeyIndex - 1);
            };

            StringBuilder sb = new StringBuilder();

            using (BinaryStream bs = new BinaryStream())
            {
                const int          pageSize = 512;
                SparseIndex <TKey> sparse   = new SparseIndex <TKey>();
                sparse.Initialize(bs, pageSize, getNextKey, 0, 1);
                node.Initialize(bs, pageSize, getNextKey, sparse);
                node.CreateEmptyNode(1);

                TKey   key    = new TKey();
                TKey   key2   = new TKey();
                TValue value  = new TValue();
                TValue value2 = new TValue();

                randomizer.Reset(Max);
                for (int x = 0; x < Max; x++)
                {
                    randomizer.Next();
                    //Add the next point
                    randomizer.GetRandom(x, key, value);

                    //node.WriteNodeData(sb);

                    if (!node.TryInsert(key, value))
                    {
                        throw new Exception();
                    }

                    //node.WriteNodeData(sb);
                    //File.WriteAllText("c:\\temp\\temp.log", sb.ToString());


                    //Check if all points exist
                    for (int y = 0; y <= x; y++)
                    {
                        randomizer.GetRandom(y, key, value);
                        if (!node.TryGet(key, value2))
                        {
                            throw new Exception();
                        }
                        if (!value.IsEqualTo(value2))
                        {
                            throw new Exception();
                        }
                    }

                    //Check if scanner works.
                    SortedTreeScannerBase <TKey, TValue> scanner = node.CreateTreeScanner();
                    scanner.SeekToStart();
                    for (int y = 0; y <= x; y++)
                    {
                        randomizer.GetInSequence(y, key, value);
                        if (!scanner.Read(key2, value2))
                        {
                            throw new Exception();
                        }
                        if (!key.IsEqualTo(key2))
                        {
                            throw new Exception();
                        }
                        if (!value.IsEqualTo(value2))
                        {
                            throw new Exception();
                        }
                    }
                    if (scanner.Read(key2, value2))
                    {
                        throw new Exception();
                    }
                }

                node = node;
            }
        }
Example #4
0
        internal static void TestSpeed <TKey, TValue>(SortedTreeNodeBase <TKey, TValue> nodeInitializer, TreeNodeRandomizerBase <TKey, TValue> randomizer, int count, int pageSize)
            where TKey : SnapTypeBase <TKey>, new()
            where TValue : SnapTypeBase <TValue>, new()
        {
            int Max = count;

            uint        nextKeyIndex = 2;
            Func <uint> getNextKey   = () =>
            {
                nextKeyIndex++;
                return(nextKeyIndex - 1);
            };


            using (BinaryStream bs = new BinaryStream())
            {
                randomizer.Reset(Max);
                for (int x = 0; x < Max; x++)
                {
                    randomizer.Next();
                }

                TKey   key   = new TKey();
                TValue value = new TValue();
                SortedTreeNodeBase <TKey, TValue> node = null;

                System.Console.WriteLine(StepTimer.Time(count, (sw) =>
                {
                    nextKeyIndex = 2;
                    node         = nodeInitializer.Clone(0);
                    SparseIndex <TKey> sparse = new SparseIndex <TKey>();
                    sparse.Initialize(bs, pageSize, getNextKey, 0, 1);
                    node.Initialize(bs, pageSize, getNextKey, sparse);
                    node.CreateEmptyNode(1);
                    sw.Start();
                    for (int x = 0; x < Max; x++)
                    {
                        //Add the next point
                        randomizer.GetRandom(x, key, value);

                        if (!node.TryInsert(key, value))
                        {
                            throw new Exception();
                        }
                    }
                    sw.Stop();
                }));


                System.Console.WriteLine(StepTimer.Time(count, () =>
                {
                    for (int x = 0; x < Max; x++)
                    {
                        //Add the next point
                        randomizer.GetRandom(x, key, value);

                        if (!node.TryGet(key, value))
                        {
                            throw new Exception();
                        }
                    }
                }));



                System.Console.WriteLine(StepTimer.Time(count, () =>
                {
                    SortedTreeScannerBase <TKey, TValue> scanner = node.CreateTreeScanner();
                    scanner.SeekToStart();
                    while (scanner.Read(key, value))
                    {
                        ;
                    }
                }));

                node = node;
            }
        }