Beispiel #1
0
        /// <summary>
        /// Deletes current record.
        /// </summary>
        public void DeleteCurrentRecord()
        {
            if (!IsDatabaseOpen)
            {
                throw new Exception("Database isn't open, please open database first !");
            }
            if (m_pCurrentRecord == null)
            {
                throw new Exception("There is no current record !");
            }

            bool unlock = true;

            // Table is already locked, don't lock it
            if (TableLocked)
            {
                unlock = false;
            }
            else
            {
                LockTable(15);
            }

            // Release all data pages hold by this row
            DataPage[] dataPages = m_pCurrentRecord.DataPages;
            for (int i = 0; i < dataPages.Length; i++)
            {
                DataPage p = dataPages[i];

                byte[] dataPage = DataPage.CreateDataPage(m_DataPageDataAreaSize,
                                                          false,
                                                          0,
                                                          0,
                                                          0,
                                                          new byte[m_DataPageDataAreaSize]);
                SetFilePosition(p.Pointer);
                WriteToFile(dataPage, 0, dataPage.Length);
            }

            // Increase free data pages count info in table header
            byte[] freeDataPagesCount = new byte[8];
            SetFilePosition(52);
            ReadFromFile(freeDataPagesCount, 0, freeDataPagesCount.Length);
            freeDataPagesCount =
                ldb_Utils.LongToByte(ldb_Utils.ByteToLong(freeDataPagesCount, 0) + dataPages.Length);
            SetFilePosition(52);
            WriteToFile(freeDataPagesCount, 0, freeDataPagesCount.Length);

            if (unlock)
            {
                UnlockTable();
            }

            // Activate next record **** Change it ???
            NextRecord();
        }
Beispiel #2
0
        /// <summary>
        /// Gets specified number of free data pages. If free data pages won't exist, creates new ones.
        /// Data pages are marked as used and OwnerDataPagePointer and NextDataPagePointer is set as needed.
        /// </summary>
        /// <param name="ownerDataPagePointer">Owner data page pointer that own first requested data page. If no owner then this value is 0.</param>
        /// <param name="count">Number of data pages wanted.</param>
        internal DataPage[] GetDataPages(long ownerDataPagePointer, int count)
        {
            if (!TableLocked)
            {
                throw new Exception("Table must be locked to acess GetDataPages() method !");
            }

            ArrayList freeDataPages = new ArrayList();

            // Get free data pages count from table header
            byte[] freeDataPagesCount = new byte[8];
            SetFilePosition(52);
            ReadFromFile(freeDataPagesCount, 0, freeDataPagesCount.Length);
            long nFreeDataPages = ldb_Utils.ByteToLong(freeDataPagesCount, 0);

            // We have plenty free data pages and enough for count requested, find requested count free data pages
            if (nFreeDataPages > 1000 && nFreeDataPages > count)
            {
                long nextDataPagePointer = m_DatapagesStartOffset + 1;
                while (freeDataPages.Count < count)
                {
                    DataPage dataPage = new DataPage(m_DataPageDataAreaSize, this, nextDataPagePointer);
                    if (!dataPage.Used)
                    {
                        dataPage.Used = true;
                        freeDataPages.Add(dataPage);
                    }

                    nextDataPagePointer += m_DataPageDataAreaSize + 33;
                }

                // Decrease free data pages count in table header
                SetFilePosition(52);
                ReadFromFile(ldb_Utils.LongToByte(nFreeDataPages - count), 0, 8);
            }
            // Just create new data pages
            else
            {
                for (int i = 0; i < count; i++)
                {
                    byte[] dataPage = DataPage.CreateDataPage(m_DataPageDataAreaSize,
                                                              true,
                                                              0,
                                                              0,
                                                              0,
                                                              new byte[m_DataPageDataAreaSize]);
                    GoToFileEnd();
                    long dataPageStartPointer = GetFilePosition();
                    WriteToFile(dataPage, 0, dataPage.Length);

                    freeDataPages.Add(new DataPage(m_DataPageDataAreaSize, this, dataPageStartPointer));
                }
            }

            // Relate data pages (chain)
            for (int i = 0; i < freeDataPages.Count; i++)
            {
                // First data page
                if (i == 0)
                {
                    // Owner data page poitner specified for first data page
                    if (ownerDataPagePointer > 0)
                    {
                        ((DataPage)freeDataPages[i]).OwnerDataPagePointer = ownerDataPagePointer;
                    }

                    // There is continuing data page
                    if (freeDataPages.Count > 1)
                    {
                        ((DataPage)freeDataPages[i]).NextDataPagePointer =
                            ((DataPage)freeDataPages[i + 1]).Pointer;
                    }
                }
                // Last data page
                else if (i == (freeDataPages.Count - 1))
                {
                    ((DataPage)freeDataPages[i]).OwnerDataPagePointer =
                        ((DataPage)freeDataPages[i - 1]).Pointer;
                }
                // Middle range data page
                else
                {
                    ((DataPage)freeDataPages[i]).OwnerDataPagePointer =
                        ((DataPage)freeDataPages[i - 1]).Pointer;
                    ((DataPage)freeDataPages[i]).NextDataPagePointer =
                        ((DataPage)freeDataPages[i + 1]).Pointer;
                }
            }

            DataPage[] retVal = new DataPage[freeDataPages.Count];
            freeDataPages.CopyTo(retVal);

            return(retVal);
        }