Exemple #1
0
 /// <summary>
 /// <pre>
 /// 1 take median element
 /// 2 insert the median in the parent  (shifting necessary elements)
 /// 3 create a new node with right part elements (moving keys and values and children)
 /// 4 set this new node as a child of parent
 /// </pre>
 /// </summary>
 public virtual void Split(NeoDatis.Btree.IBTreeNode parent, NeoDatis.Btree.IBTreeNode
                           node2Split, int childIndex)
 {
     // BTreeValidator.validateNode(parent, parent == root);
     // BTreeValidator.validateNode(node2Split, false);
     // 1
     NeoDatis.Btree.IKeyAndValue median = node2Split.GetMedian();
     // 2
     parent.SetKeyAndValueAt(median, childIndex, true, true);
     // 3
     NeoDatis.Btree.IBTreeNode rightPart = node2Split.ExtractRightPart();
     // 4
     parent.SetChildAt(rightPart, childIndex + 1);
     parent.SetChildAt(node2Split, childIndex);
     parent.IncrementNbChildren();
     persister.SaveNode(parent);
     persister.SaveNode(rightPart);
     persister.SaveNode(node2Split);
     if (NeoDatis.Btree.Tool.BTreeValidator.IsOn())
     {
         NeoDatis.Btree.Tool.BTreeValidator.ValidateNode(parent, parent == root);
         NeoDatis.Btree.Tool.BTreeValidator.ValidateNode(rightPart, false);
         NeoDatis.Btree.Tool.BTreeValidator.ValidateNode(node2Split, false);
     }
 }
Exemple #2
0
        public virtual NeoDatis.Btree.IKeyAndValue GetBiggest(NeoDatis.Btree.IBTreeNode node
                                                              , bool delete)
        {
            int lastKeyIndex   = node.GetNbKeys() - 1;
            int lastChildIndex = node.GetNbChildren() - 1;

            if (lastChildIndex > lastKeyIndex)
            {
                NeoDatis.Btree.IBTreeNode child = node.GetChildAt(lastChildIndex, true);
                if (child.GetNbKeys() == degree - 1)
                {
                    node = PrepareForDelete(node, child, lastChildIndex);
                }
                lastChildIndex = node.GetNbChildren() - 1;
                child          = node.GetChildAt(lastChildIndex, true);
                return(GetBiggest(child, delete));
            }
            NeoDatis.Btree.IKeyAndValue kav = node.GetKeyAndValueAt(lastKeyIndex);
            if (delete)
            {
                node.DeleteKeyAndValueAt(lastKeyIndex, false);
                persister.SaveNode(node);
            }
            return(kav);
        }
Exemple #3
0
        protected virtual T NextAsc()
        {
            // Try to go down till a leaf
            while (!currentNode.IsLeaf())
            {
                currentNode     = currentNode.GetChildAt(currentKeyIndex, true);
                currentKeyIndex = 0;
            }
            // If leaf has more keys
            if (currentKeyIndex < currentNode.GetNbKeys())
            {
                nbReturnedKeys++;
                nbReturnedElements++;
                object nodeValue = GetValueAt(currentNode, currentKeyIndex);
                currentKeyIndex++;
                return((T)nodeValue);
            }
            NeoDatis.Btree.IBTreeNode child = null;
            // else go up till a node with keys
            while (currentKeyIndex >= currentNode.GetNbKeys())
            {
                child           = currentNode;
                currentNode     = currentNode.GetParent();
                currentKeyIndex = IndexOfChild(currentNode, child);
            }
            nbReturnedElements++;
            nbReturnedKeys++;
            object value = GetValueAt(currentNode, currentKeyIndex);

            currentKeyIndex++;
            return((T)value);
        }
Exemple #4
0
        // Commented by Olivier 05/11/2007
        // persister.flush();
        private void InsertNonFull(NeoDatis.Btree.IBTreeNode node, System.IComparable key
                                   , object value)
        {
            if (node.IsLeaf())
            {
                node.InsertKeyAndValue(key, value);
                persister.SaveNode(node);
                return;
            }
            int position = node.GetPositionOfKey(key);
            // return an index starting
            // from 1 instead of 0
            int realPosition = -position - 1;

            // If position is positive, the key must be inserted in this node
            if (position >= 0)
            {
                realPosition = position - 1;
                node.InsertKeyAndValue(key, value);
                persister.SaveNode(node);
                return;
            }
            // descend
            NeoDatis.Btree.IBTreeNode nodeToDescend = node.GetChildAt(realPosition, true);
            if (nodeToDescend.IsFull())
            {
                Split(node, nodeToDescend, realPosition);
                if (node.GetKeyAt(realPosition).CompareTo(key) < 0)
                {
                    nodeToDescend = node.GetChildAt(realPosition + 1, true);
                }
            }
            InsertNonFull(nodeToDescend, key, value);
        }
 public virtual NeoDatis.Btree.IBTree LoadBTree(object id)
 {
     nbLoadTree++;
     NeoDatis.Odb.OID oid = (NeoDatis.Odb.OID)id;
     try
     {
         if (NeoDatis.Odb.OdbConfiguration.IsDebugEnabled(LogId))
         {
             NeoDatis.Tool.DLogger.Debug("Loading btree with id " + oid);
         }
         if (oid == NeoDatis.Odb.Impl.Core.Layers.Layer3.Engine.StorageEngineConstant.NullObjectId)
         {
             throw new NeoDatis.Odb.ODBRuntimeException(NeoDatis.Btree.BTreeError.InvalidIdForBtree
                                                        .AddParameter(NeoDatis.Odb.Impl.Core.Layers.Layer3.Engine.StorageEngineConstant.
                                                                      NullObjectId));
         }
         tree = (NeoDatis.Btree.IBTree)engine.GetObjectFromOid(oid);
         tree.SetId(oid);
         tree.SetPersister(this);
         NeoDatis.Btree.IBTreeNode root = tree.GetRoot();
         root.SetBTree(tree);
         return(tree);
     }
     catch (System.Exception e)
     {
         throw new NeoDatis.Odb.ODBRuntimeException(NeoDatis.Btree.BTreeError.InternalError
                                                    , e);
     }
 }
Exemple #6
0
 public override object GetValueAt(NeoDatis.Btree.IBTreeNode node, int currentIndex
                                   )
 {
     NeoDatis.Btree.IBTreeNodeOneValuePerKey n = (NeoDatis.Btree.IBTreeNodeOneValuePerKey
                                                  )node;
     return(n.GetValueAt(currentIndex));
 }
Exemple #7
0
 public AbstractBTree()
 {
     this.degree    = 0;
     this.size      = 0;
     this.height    = 1;
     this.persister = null;
     root           = null;
 }
 public override void SetChildAt(NeoDatis.Btree.IBTreeNode child, int index)
 {
     children[index] = child;
     if (child != null)
     {
         child.SetParent(this);
     }
 }
Exemple #9
0
 public override void Clear()
 {
     base.Clear();
     parent       = null;
     parentOid    = null;
     childrenOids = null;
     oid          = null;
 }
		public override NeoDatis.Btree.IBTreeNode GetParent()
		{
			if (parent != null)
			{
				return parent;
			}
			parent = btree.GetPersister().LoadNodeById(parentOid);
			return parent;
		}
Exemple #11
0
 public override NeoDatis.Btree.IBTreeNode GetParent()
 {
     if (parent != null)
     {
         return(parent);
     }
     parent = btree.GetPersister().LoadNodeById(parentOid);
     return(parent);
 }
 public override void SetChildAt(NeoDatis.Btree.IBTreeNode node, int childIndex, int
                                 index, bool throwExceptionIfDoesNotExist)
 {
     NeoDatis.Btree.IBTreeNode child = node.GetChildAt(childIndex, throwExceptionIfDoesNotExist
                                                       );
     children[index] = child;
     if (child != null)
     {
         child.SetParent(this);
     }
 }
Exemple #13
0
 public virtual System.Text.StringBuilder Build(NeoDatis.Btree.IBTreeNode node, int
                                                height, bool withIds)
 {
     lines = new System.Text.StringBuilder[height];
     for (int i = 0; i < height; i++)
     {
         lines[i] = new System.Text.StringBuilder();
     }
     BuildDisplay(node, 0, 0, "0", withIds);
     BuildRepresentation();
     return(result);
 }
        /// <summary>Creates a new node with the right part of this node.</summary>
        /// <remarks>
        /// Creates a new node with the right part of this node. This should only be
        /// called on a full node
        /// </remarks>
        public virtual NeoDatis.Btree.IBTreeNode ExtractRightPart()
        {
            if (!IsFull())
            {
                throw new NeoDatis.Btree.Exception.BTreeException("extract right part called on non full node"
                                                                  );
            }
            // Creates an empty new node
            NeoDatis.Btree.IBTreeNode rightPart = btree.BuildNode();
            int j = 0;

            for (int i = degree; i < maxNbKeys; i++)
            {
                rightPart.SetKeyAndValueAt(keys[i], values[i], j, false, false);
                keys[i]   = null;
                values[i] = null;
                rightPart.SetChildAt(this, i, j, false);
                // TODO must we load all nodes to set new parent
                NeoDatis.Btree.IBTreeNode c = rightPart.GetChildAt(j, false);
                if (c != null)
                {
                    c.SetParent(rightPart);
                }
                // rightPart.setChildAt(getChildAt(i,false), j);
                SetNullChildAt(i);
                j++;
            }
            // rightPart.setChildAt(getLastPositionChild(), j);
            rightPart.SetChildAt(this, GetMaxNbChildren() - 1, j, false);
            // correct father id
            NeoDatis.Btree.IBTreeNode c1 = rightPart.GetChildAt(j, false);
            if (c1 != null)
            {
                c1.SetParent(rightPart);
            }
            SetNullChildAt(maxNbChildren - 1);
            // resets last child
            keys[degree - 1] = null;
            // resets median element
            values[degree - 1] = null;
            // set numbers
            nbKeys = degree - 1;
            int originalNbChildren = nbChildren;

            nbChildren = System.Math.Min(nbChildren, degree);
            rightPart.SetNbKeys(degree - 1);
            rightPart.SetNbChildren(originalNbChildren - nbChildren);
            NeoDatis.Btree.Tool.BTreeValidator.ValidateNode(this);
            NeoDatis.Btree.Tool.BTreeValidator.ValidateNode(rightPart);
            NeoDatis.Btree.Tool.BTreeValidator.CheckDuplicateChildren(this, rightPart);
            return(rightPart);
        }
Exemple #15
0
 private int IndexOfChild(NeoDatis.Btree.IBTreeNode parent, NeoDatis.Btree.IBTreeNode
                          child)
 {
     for (int i = 0; i < parent.GetNbChildren(); i++)
     {
         if (parent.GetChildAt(i, true).GetId() == child.GetId())
         {
             return(i);
         }
     }
     throw new System.Exception("parent " + parent + " does not have the specified child : "
                                + child);
 }
Exemple #16
0
 public override void SetChildAt(NeoDatis.Btree.IBTreeNode node, int childIndex, int
                                 indexDestination, bool throwExceptionIfDoesNotExist)
 {
     NeoDatis.Odb.OID childOid = (NeoDatis.Odb.OID)node.GetChildIdAt(childIndex, throwExceptionIfDoesNotExist
                                                                     );
     childrenOids[indexDestination] = childOid;
     if (childOid != null)
     {
         // The parent of the child has changed
         NeoDatis.Btree.IBTreeNode child = btree.GetPersister().LoadNodeById(childOid);
         child.SetParent(this);
         btree.GetPersister().SaveNode(child);
     }
 }
Exemple #17
0
 // TODO Auto-generated method stub
 public override void Reset()
 {
     this.currentNode = btree.GetRoot();
     if (orderByType.IsOrderByDesc())
     {
         this.currentKeyIndex = currentNode.GetNbKeys();
     }
     else
     {
         this.currentKeyIndex = 0;
     }
     nbReturnedElements = 0;
     nbReturnedKeys     = 0;
 }
Exemple #18
0
 public AbstractBTreeIterator(NeoDatis.Btree.IBTree tree, NeoDatis.Odb.Core.OrderByConstants
                              orderByType)
 {
     this.btree       = tree;
     this.currentNode = tree.GetRoot();
     this.orderByType = orderByType;
     if (orderByType.IsOrderByDesc())
     {
         this.currentKeyIndex = currentNode.GetNbKeys();
     }
     else
     {
         this.currentKeyIndex = 0;
     }
 }
        public virtual object DeleteNode(NeoDatis.Btree.IBTreeNode o)
        {
            NeoDatis.Odb.OID oid = engine.Delete(o);
            oids.Remove(oid);
            int position = modifiedObjectOids.Remove2(oid);

            //TODO
            if (position != null)
            {
                // Just replace the element by null, to not modify all the other
                // positions
                modifiedObjectOidList.Set(position, null);
            }
            return(o);
        }
Exemple #20
0
 public AbstractBTree(string name, int degree, NeoDatis.Btree.IBTreePersister persister
                      )
 {
     this.name      = name;
     this.degree    = degree;
     this.size      = 0;
     this.height    = 1;
     this.persister = persister;
     root           = BuildNode();
     // TODO check if it is needed to store the root before the btree ->
     // saving btree will try to update root!
     persister.SaveNode(root);
     persister.SaveBTree(this);
     persister.Flush();
 }
Exemple #21
0
 private void BuildDisplay(NeoDatis.Btree.IBTreeNode node, int currentHeight, int
                           childIndex, object parentId, bool withIds)
 {
     if (currentHeight > lines.Length - 1)
     {
         return;
     }
     // get string buffer of this line
     System.Text.StringBuilder line = lines[currentHeight];
     if (withIds)
     {
         line.Append(node.GetId()).Append(":[");
     }
     else
     {
         line.Append("[");
     }
     for (int i = 0; i < node.GetNbKeys(); i++)
     {
         if (i > 0)
         {
             line.Append(" , ");
         }
         NeoDatis.Btree.IKeyAndValue kav = node.GetKeyAndValueAt(i);
         line.Append(kav.GetKey());
     }
     if (withIds)
     {
         line.Append("]:").Append(node.GetParentId()).Append("/").Append(parentId).Append(
             "    ");
     }
     else
     {
         line.Append("]  ");
     }
     for (int i = 0; i < node.GetNbChildren(); i++)
     {
         NeoDatis.Btree.IBTreeNode child = node.GetChildAt(i, false);
         if (child != null)
         {
             BuildDisplay(child, currentHeight + 1, i, node.GetId(), withIds);
         }
         else
         {
             lines[currentHeight + 1].Append("[Child " + (i + 1) + " null!] ");
         }
     }
 }
Exemple #22
0
 public override void SetChildAt(NeoDatis.Btree.IBTreeNode child, int index)
 {
     if (child != null)
     {
         if (child.GetId() == null)
         {
             btree.GetPersister().SaveNode(child);
         }
         childrenOids[index] = (NeoDatis.Odb.OID)child.GetId();
         child.SetParent(this);
     }
     else
     {
         childrenOids[index] = null;
     }
 }
		public override void SetParent(NeoDatis.Btree.IBTreeNode node)
		{
			parent = node;
			if (parent != null)
			{
				if (parent.GetId() == null)
				{
					btree.GetPersister().SaveNode(parent);
				}
				parentOid = (NeoDatis.Odb.OID)parent.GetId();
			}
			else
			{
				parentOid = null;
			}
		}
Exemple #24
0
 public override void SetParent(NeoDatis.Btree.IBTreeNode node)
 {
     parent = node;
     if (parent != null)
     {
         if (parent.GetId() == null)
         {
             btree.GetPersister().SaveNode(parent);
         }
         parentOid = (NeoDatis.Odb.OID)parent.GetId();
     }
     else
     {
         parentOid = null;
     }
 }
Exemple #25
0
 public static void ValidateNode(NeoDatis.Btree.IBTreeNode node, bool isRoot)
 {
     if (!on)
     {
         return;
     }
     ValidateNode(node);
     if (isRoot && node.HasParent())
     {
         throw new NeoDatis.Btree.Exception.BTreeNodeValidationException("Root node with a parent: "
                                                                         + node.ToString());
     }
     if (!isRoot && !node.HasParent())
     {
         throw new NeoDatis.Btree.Exception.BTreeNodeValidationException("Internal node without parent: "
                                                                         + node.ToString());
     }
 }
Exemple #26
0
 private static void CheckValuesOfChild(NeoDatis.Btree.IKeyAndValue key, NeoDatis.Btree.IBTreeNode
                                        node)
 {
     if (!on)
     {
         return;
     }
     if (node == null)
     {
         return;
     }
     for (int i = 0; i < node.GetNbKeys(); i++)
     {
         if (node.GetKeyAndValueAt(i).GetKey().CompareTo(key.GetKey()) >= 0)
         {
             throw new NeoDatis.Btree.Exception.BTreeNodeValidationException("Left child with values bigger than pivot "
                                                                             + key + " : " + node.ToString());
         }
     }
 }
Exemple #27
0
 public static void CheckDuplicateChildren(NeoDatis.Btree.IBTreeNode node1, NeoDatis.Btree.IBTreeNode
                                           node2)
 {
     if (!on)
     {
         return;
     }
     for (int i = 0; i < node1.GetNbChildren(); i++)
     {
         NeoDatis.Btree.IBTreeNode child1 = node1.GetChildAt(i, true);
         for (int j = 0; j < node2.GetNbChildren(); j++)
         {
             if (child1 == node2.GetChildAt(j, true))
             {
                 throw new NeoDatis.Btree.Exception.BTreeNodeValidationException("Duplicated node : "
                                                                                 + child1);
             }
         }
     }
 }
Exemple #28
0
        public override object GetValueAt(NeoDatis.Btree.IBTreeNode node, int currentIndex
                                          )
        {
            if (currentValue == null)
            {
                currentValue = (System.Collections.IList)node.GetValueAsObjectAt(currentIndex);
            }
            int listSize = currentValue.Count;

            if (listSize > currenListIndex)
            {
                object value = currentValue[currenListIndex];
                currenListIndex++;
                return(value);
            }
            // We have reached the end of the list or the list is empty
            // We must continue iterate in the current node / btree
            currenListIndex = 0;
            currentValue    = null;
            return(null);
        }
Exemple #29
0
 public virtual NeoDatis.Btree.IKeyAndValue GetSmallest(NeoDatis.Btree.IBTreeNode
                                                        node, bool delete)
 {
     if (!node.IsLeaf())
     {
         NeoDatis.Btree.IBTreeNode child = node.GetChildAt(0, true);
         if (child.GetNbKeys() == degree - 1)
         {
             node = PrepareForDelete(node, child, 0);
         }
         child = node.GetChildAt(0, true);
         return(GetSmallest(child, delete));
     }
     NeoDatis.Btree.IKeyAndValue kav = node.GetKeyAndValueAt(0);
     if (delete)
     {
         node.DeleteKeyAndValueAt(0, true);
         persister.SaveNode(node);
     }
     return(kav);
 }
 /// <summary>Loads a node from its id.</summary>
 /// <remarks>
 /// Loads a node from its id. Tries to get if from memory, if not present
 /// then loads it from odb storage
 /// </remarks>
 /// <param name="id">The id of the nod</param>
 /// <returns>The node with the specific id</returns>
 public virtual NeoDatis.Btree.IBTreeNode LoadNodeById(object id)
 {
     NeoDatis.Odb.OID oid = (NeoDatis.Odb.OID)id;
     // Check if node is in memory
     NeoDatis.Btree.IBTreeNode node = (NeoDatis.Btree.IBTreeNode)oids[oid];
     if (node != null)
     {
         nbLoadNodesFromCache++;
         return(node);
     }
     nbLoadNodes++;
     // else load from odb
     try
     {
         if (NeoDatis.Odb.OdbConfiguration.IsDebugEnabled(LogId))
         {
             NeoDatis.Tool.DLogger.Debug("Loading node with id " + oid);
         }
         if (oid == null)
         {
             throw new NeoDatis.Odb.ODBRuntimeException(NeoDatis.Btree.BTreeError.InvalidIdForBtree
                                                        .AddParameter(oid));
         }
         NeoDatis.Btree.IBTreeNode pn = (NeoDatis.Btree.IBTreeNode)engine.GetObjectFromOid
                                            (oid);
         pn.SetId(oid);
         if (tree != null)
         {
             pn.SetBTree(tree);
         }
         // Keep the node in memory
         oids.Add(oid, pn);
         return(pn);
     }
     catch (System.Exception e)
     {
         throw new NeoDatis.Odb.ODBRuntimeException(NeoDatis.Btree.BTreeError.InternalError
                                                    , e);
     }
 }
 /// <summary>
 /// saves the bree node Only puts the current node in an 'modified Node' map
 /// to be saved on commit
 /// </summary>
 public virtual object SaveNode(NeoDatis.Btree.IBTreeNode node)
 {
     NeoDatis.Odb.OID oid = null;
     // Here we only save the node if it does not have id,
     // else we just save into the hashmap
     if (node.GetId() == NeoDatis.Odb.Impl.Core.Layers.Layer3.Engine.StorageEngineConstant
         .NullObjectId)
     {
         try
         {
             nbSaveNodes++;
             // first get the oid. : -2:it could be any value
             oid = engine.GetObjectWriter().GetIdManager().GetNextObjectId(-2);
             node.SetId(oid);
             oid = engine.Store(oid, node);
             if (NeoDatis.Odb.OdbConfiguration.IsDebugEnabled(LogId))
             {
                 NeoDatis.Tool.DLogger.Debug("Saved node id " + oid);
             }
             // + " : " +
             // node.toString());
             if (tree != null && node.GetBTree() == null)
             {
                 node.SetBTree(tree);
             }
             oids.Add(oid, node);
             return(oid);
         }
         catch (System.Exception e)
         {
             throw new NeoDatis.Odb.ODBRuntimeException(NeoDatis.Btree.BTreeError.InternalError
                                                        .AddParameter("While saving node"), e);
         }
     }
     nbSaveNodesInCache++;
     oid = (NeoDatis.Odb.OID)node.GetId();
     oids.Add(oid, node);
     AddModifiedOid(oid);
     return(oid);
 }
        /// <summary>
        /// Can only merge node without intersection =&gt; the greater key of this must
        /// be smaller than the smallest key of the node
        /// </summary>
        public virtual void MergeWith(NeoDatis.Btree.IBTreeNode node)
        {
            NeoDatis.Btree.Tool.BTreeValidator.ValidateNode(this);
            NeoDatis.Btree.Tool.BTreeValidator.ValidateNode(node);
            CheckIfCanMergeWith(node);
            int j = nbKeys;

            for (int i = 0; i < node.GetNbKeys(); i++)
            {
                SetKeyAndValueAt(node.GetKeyAt(i), node.GetValueAsObjectAt(i), j, false, false);
                SetChildAt(node, i, j, false);
                j++;
            }
            // in this, we have to take the last child
            if (node.GetNbChildren() > node.GetNbKeys())
            {
                SetChildAt(node, node.GetNbChildren() - 1, j, true);
            }
            nbKeys     += node.GetNbKeys();
            nbChildren += node.GetNbChildren();
            NeoDatis.Btree.Tool.BTreeValidator.ValidateNode(this);
        }
		public override void SetParent(NeoDatis.Btree.IBTreeNode node)
		{
			parent = node;
		}
		public override void Clear()
		{
			base.Clear();
			parent = null;
			parentOid = null;
			childrenOids = null;
			oid = null;
		}
		protected override void Init()
		{
			childrenOids = new NeoDatis.Odb.OID[maxNbChildren];
			parentOid = null;
			parent = null;
		}