/// <summary>
        /// Opens specified data file.
        /// </summary>
        /// <param name="fileName">File name.</param>
        /// <param name="waitTime">If data base file is exclusively locked, then how many seconds to wait file to unlock before raising a error.</param>
        public void Open(string fileName, int waitTime)
        {
            DateTime lockExpireTime = DateTime.Now.AddSeconds(waitTime);

            while (true)
            {
                try{
                    m_pDbFile = File.Open(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);

                    break;
                }
                catch (IOException x) {
                    if (!File.Exists(fileName))
                    {
                        throw new Exception("Specified database file '" + fileName + "' does not exists !");
                    }

                    // Make this because to get rid of "The variable 'x' is declared but never used"
                    string dummy = x.Message;

                    System.Threading.Thread.Sleep(15);

                    // Lock wait time timed out
                    if (DateTime.Now > lockExpireTime)
                    {
                        throw new Exception("Database file is locked and lock wait time expired !");
                    }
                }
            }

            /* Table structure:
             *      50    bytes         - version
             *      2     bytes         - CRLF
             * 4     bytes         - Free rows count
             * 2     bytes         - CRLF
             *      100 x 500 bytes     - 100 columns info store
             *      2     bytes         - CRLF
             *      ... data pages
             */

            m_DbFileName = fileName;

            // TODO: check if LDB file

            // Read version line (50 bytes + CRLF)
            byte[] version = new byte[52];
            ReadFromFile(0, version, 0, version.Length);

            // Read free rows count
            byte[] freeRows = new byte[6];
            ReadFromFile(0, freeRows, 0, freeRows.Length);

            long currentColumnOffset = 58;

            // Read 100 column lines (500 + CRLF bytes each)
            for (int i = 0; i < 100; i++)
            {
                byte[] columnInfo = new byte[102];
                if (ReadFromFile(currentColumnOffset, columnInfo, 0, columnInfo.Length) != columnInfo.Length)
                {
                    throw new Exception("Invalid columns data area length !");
                }

                if (columnInfo[0] != '\0')
                {
                    m_pColumns.Parse(columnInfo);
                }

                currentColumnOffset += 102;
            }

            // Header terminator \0
            m_pDbFile.Position++;

            // No we have rows start offset
            m_RowsStartOffset = m_pDbFile.Position;

            // Store file length and position
            m_FileLength   = m_pDbFile.Length;
            m_FilePosition = m_pDbFile.Position;

            // Calculate row length
            m_RowLength = 1 + 2;
            for (int i = 0; i < m_pColumns.Count; i++)
            {
                m_RowLength += m_pColumns[i].ColumnSize;
            }

            m_RowDataBuffer = new byte[m_RowLength];
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Opens specified data file.
        /// </summary>
        /// <param name="fileName">File name.</param>
        /// <param name="waitTime">If data base file is exclusively locked, then how many seconds to wait file to unlock before raising a error.</param>
        public void Open(string fileName, int waitTime)
        {
            DateTime lockExpireTime = DateTime.Now.AddSeconds(waitTime);

            while (true)
            {
                try{
                    m_pDbFile = File.Open(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);

                    break;
                }
                catch (IOException x) {
                    // Make this because to get rid of "The variable 'x' is declared but never used"
                    string dummy = x.Message;

                    System.Threading.Thread.Sleep(15);

                    // Lock wait time timed out
                    if (DateTime.Now > lockExpireTime)
                    {
                        throw new Exception("Database file is locked and lock wait time expired !");
                    }
                }
            }

            /* Table structure:
             *      50    bytes         - version
             *      2     bytes         - CRLF
             *      8     bytes         - free datapages count
             *      2     bytes         - CRLF
             *      4     bytes         - datapage data area size
             *      2     bytes         - CRLF
             *      100 x 500 bytes     - 100 columns info store
             *      2     bytes         - CRLF
             *      ... data pages
             */

            m_DbFileName = fileName;
            StreamLineReader r = new StreamLineReader(m_pDbFile);

            // TODO: check if LDB file

            // Read version line (50 bytes + CRLF)
            byte[] version = r.ReadLine();

            // Skip free data pages count
            byte[] freeDataPagesCount = new byte[10];
            m_pDbFile.Read(freeDataPagesCount, 0, freeDataPagesCount.Length);

            // 4 bytes datapage data area size + CRLF
            byte[] dataPageDataAreaSize = new byte[6];
            m_pDbFile.Read(dataPageDataAreaSize, 0, dataPageDataAreaSize.Length);
            m_DataPageDataAreaSize = ldb_Utils.ByteToInt(dataPageDataAreaSize, 0);

            // Read 100 column lines (500 + CRLF bytes each)
            for (int i = 0; i < 100; i++)
            {
                byte[] columnInfo = r.ReadLine();
                if (columnInfo == null)
                {
                    throw new Exception("Invalid columns data area length !");
                }

                if (columnInfo[0] != '\0')
                {
                    m_pColumns.Parse(columnInfo);
                }
            }

            // Header terminator \0
            m_pDbFile.Position++;

            // No we have rows start offset
            m_DatapagesStartOffset = m_pDbFile.Position;

            // Store file length and position
            m_FileLength   = m_pDbFile.Length;
            m_FilePosition = m_pDbFile.Position;
        }