Esempio n. 1
0
        /*  Needed operations:
         *	  1. Given an offset and length, set that range to be free space.
         *    2. Given an length, return the pair (offset, length) can be used.
         *		 that range will be removed from free space tree before this
         *       operation	returns.
         */

        /// <summary>
        /// Set the segment in the databse file to be free.
        /// </summary>
        /// <param name="offset"></param>
        /// <param name="length"></param>
        public void SetSegmentFree(uint offset, int length)
        {
            if (offset + length != m_dbFile.Length)
            {
                uint    nextSeg = (uint)(offset + length);
                KOffset target  = new KOffset(nextSeg);
                KOffset finding = (KOffset)m_tree.Search(target);
                if (finding == null)
                {
                    //combine to next segment is not possible, just go ahead to insert this one
                    target.Offset = offset;
                    target.Length = new DLength(length);
                    if (m_tree.Insert(target) == false)
                    {
                        throw new OOD.Exception.ProgramLogicError(
                                  this,
                                  "Insert into free space b-tree failed.");
                    }
                }
                else
                {
                    //combine these two free segment into a big one
                    length       += finding.Length.Num;
                    target.Length = new DLength(length);
                    target.Offset = offset;
                    if (m_tree.Delete(finding) == false || m_tree.Insert(target) == false)
                    {
                        throw new OOD.Exception.ProgramLogicError(
                                  this,
                                  "Delete free space failed.");
                    }
                }
            }
            else
            {
                //this is the last segment in the database file, just remove it from the file
                m_dbFile.SetLength(offset);
            }
        }