コード例 #1
0
ファイル: AppendLog.cs プロジェクト: zhouweiaccp/sones
        /// <summary>
        /// reads an OnDiscAdress from the index.
        /// </summary>
        /// <param name="NumberOfAdress">is the number of the OnDiscAdress to be read</param>
        /// <returns></returns>
        public OnDiscAdress ReadOnDiscAdress(long NumberOfAdress)
        {
            //byte[] Readin;
            lock (DatabaseIndexFile)
            {
                // check if this is even possible...
                if (DatabaseIndexFile.Length <= NumberOfAdress * 33)
                {
                    return(null);
                }

                // seek to the multiple of NumberOfAdress
                DatabaseIndexFile.Seek(NumberOfAdress * 33, SeekOrigin.Begin);

                byte[]       _SerializedData;
                OnDiscAdress _deserializedAdress;

                try
                {
                    _SerializedData = new byte[33];
                    // todo: maybe a read cache might be great, to read more bytes sequential
                    DatabaseIndexFile.Read(_SerializedData, 0, 33);

                    _deserializedAdress = new OnDiscAdress();
                    _reader.Data        = _SerializedData;
                    _deserializedAdress.Deserialize(ref _reader);

                    return(_deserializedAdress);
                }
                catch (Exception)
                {
                    return(null);
                }
            }
        }
コード例 #2
0
ファイル: AppendLog.cs プロジェクト: zhouweiaccp/sones
        /// <summary>
        /// writes a new chunk of data to the disk (and index)
        /// </summary>
        /// <param name="Data">the byte array with the data</param>
        public void Write(byte[] Data)
        {
            // write the data to the database file
            OnDiscAdress adress = WriteToDatabase(Data);

            // and the adress to the index
            WriteToIndex(adress);
        }
コード例 #3
0
ファイル: AppendLog.cs プロジェクト: loubo/sones
        public byte[] ReadData(OnDiscAdress Adress)
        {
            byte[] Readin;

            lock (DatabaseFile)
            {
                // seek to the position
                DatabaseFile.Seek(Adress.Start, SeekOrigin.Begin);
                Readin = new byte[Adress.End - Adress.Start];
                // read it in
                DatabaseFile.Read(Readin, 0, Readin.Length);
            }
            return Readin;
        }
コード例 #4
0
ファイル: AppendLog.cs プロジェクト: zhouweiaccp/sones
        public byte[] ReadData(OnDiscAdress Adress)
        {
            byte[] Readin;

            lock (DatabaseFile)
            {
                // seek to the position
                DatabaseFile.Seek(Adress.Start, SeekOrigin.Begin);
                Readin = new byte[Adress.End - Adress.Start];
                // read it in
                DatabaseFile.Read(Readin, 0, Readin.Length);
            }
            return(Readin);
        }
コード例 #5
0
ファイル: AppendLog.cs プロジェクト: zhouweiaccp/sones
        private void WriteToIndex(OnDiscAdress Adresspattern)
        {
            lock (DatabaseIndexFile)
            {
                // seek to the end...
                DatabaseIndexFile.Seek(DatabaseIndexFile.Length, SeekOrigin.Begin);
                // re-use the writer
                _writer.ResetBuffer();
                // serialize the index...
                byte[] ToWrite = Adresspattern.SerializeAligned(ref _writer);

                DatabaseIndexFile.Write(ToWrite, 0, ToWrite.Length);
                if (FlushOnWrite)
                {
                    DatabaseIndexFile.Flush();
                }
            }
        }
コード例 #6
0
ファイル: AppendLog.cs プロジェクト: zhouweiaccp/sones
        private OnDiscAdress WriteToDatabase(byte[] ToWrite)
        {
            OnDiscAdress OutputAdress = new OnDiscAdress();

            lock (DatabaseFile)
            {
                // seek to the end...
                DatabaseFile.Seek(DatabaseFile.Length, SeekOrigin.Begin);
                OutputAdress.Start = DatabaseFile.Position;
                DatabaseFile.Write(ToWrite, 0, ToWrite.Length);
                OutputAdress.End = DatabaseFile.Position;

                if (FlushOnWrite)
                {
                    DatabaseFile.Flush();
                }
            }
            return(OutputAdress);
        }
コード例 #7
0
ファイル: AppendLog.cs プロジェクト: loubo/sones
        private void WriteToIndex(OnDiscAdress Adresspattern)
        {
            lock (DatabaseIndexFile)
            {
                // seek to the end...
                DatabaseIndexFile.Seek(DatabaseIndexFile.Length, SeekOrigin.Begin);
                // re-use the writer
                _writer.ResetBuffer();
                // serialize the index...
                byte[] ToWrite = Adresspattern.SerializeAligned(ref _writer);

                DatabaseIndexFile.Write(ToWrite, 0, ToWrite.Length);
                if (FlushOnWrite)
                    DatabaseIndexFile.Flush();
            }
        }
コード例 #8
0
ファイル: AppendLog.cs プロジェクト: loubo/sones
        private OnDiscAdress WriteToDatabase(byte[] ToWrite)
        {
            OnDiscAdress OutputAdress = new OnDiscAdress();

            lock (DatabaseFile)
            {
                // seek to the end...
                DatabaseFile.Seek(DatabaseFile.Length, SeekOrigin.Begin);
                OutputAdress.Start = DatabaseFile.Position;
                DatabaseFile.Write(ToWrite, 0, ToWrite.Length);
                OutputAdress.End = DatabaseFile.Position;

                if (FlushOnWrite)
                    DatabaseFile.Flush();
            }
            return OutputAdress;
        }
コード例 #9
0
ファイル: AppendLog.cs プロジェクト: loubo/sones
        /// <summary>
        /// reads an OnDiscAdress from the index. 
        /// </summary>
        /// <param name="NumberOfAdress">is the number of the OnDiscAdress to be read</param>
        /// <returns></returns>
        public OnDiscAdress ReadOnDiscAdress(long NumberOfAdress)
        {
            byte[] Readin;
            lock (DatabaseIndexFile)
            {
                // check if this is even possible...
                if (DatabaseIndexFile.Length <= NumberOfAdress * 33)
                    return null;

                // seek to the multiple of NumberOfAdress
                DatabaseIndexFile.Seek(NumberOfAdress * 33, SeekOrigin.Begin);

                byte[] _SerializedData;
                OnDiscAdress _deserializedAdress;

                try
                {
                    _SerializedData = new byte[33];
                    // todo: maybe a read cache might be great, to read more bytes sequential
                    DatabaseIndexFile.Read(_SerializedData, 0, 33);

                    _deserializedAdress = new OnDiscAdress();
                    _reader.Data = _SerializedData;
                    _deserializedAdress.Deserialize(ref _reader);

                    return _deserializedAdress;
                }
                catch (Exception)
                {
                    return null;
                }
            }
        }