Пример #1
0
        public void TestCreateAndAppendFile_Bigger()
        {
            // this method test FileLog's behaviour when file size is exactly max size per FBT and new data is appended
            // there was a problem before with new FBT not being created in this case
            var file1 = log.CreateFile();

            log.Close();
            // a little hack to avoid writing gigabyte of data
            FileStream stream      = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite);
            var        newFileSize = FileLogTestsHelper.CreateBigLogFile(file1.Hint, stream, true);

            log = new FileLog(filename, FileMode.Open);
            // Don't use hint from previously open log file
            log.AppendDataToFile(file1, new ByteArray(new byte[] { 233, 234, 235, 236 }));
            log.AppendDataToFile(file1, new ByteArray(new byte[] { 237, 238, 239 }));
            log.Close();
            stream = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite);
            using (BinaryReader binReader = new BinaryReader(stream))
            {
                stream.Position = 0;
                // move to file table pointer
                stream.Position = binReader.ReadInt64() + 4 * sizeof(long);
                // move to pointer to file 1
                stream.Position = binReader.ReadInt64() + sizeof(long);
                // move to file 1 - file size
                stream.Position = binReader.ReadInt64();
                Assert.Equal(newFileSize + 7, binReader.ReadInt64());
                long nextFBT = binReader.ReadInt64();
                Assert.InRange(nextFBT, 1, stream.Length);
                // move to last FBT entry (next pointer was read already)
                stream.Position += (FileLog.FilesPerBlock - 1) * sizeof(long);
                // move to last FBT2 entry
                stream.Position = binReader.ReadInt64() + FileLog.FilesPerBlock * sizeof(long);
                // move to data block
                stream.Position = binReader.ReadInt64();
                for (int i = 0; i < FileLog.BlockSize; ++i)
                {
                    // check that previous data were not altered
                    Assert.Equal(i % 128, binReader.ReadByte());
                }
                // move to second FBT
                stream.Position = nextFBT;
                // no third FBT
                Assert.Equal(0, binReader.ReadInt64());
                long fbt2Ptr = binReader.ReadInt64();
                Assert.InRange(fbt2Ptr, 1, stream.Length);
                // no other entry in the second FBT
                Assert.Equal(0, binReader.ReadInt64());
                stream.Position = fbt2Ptr;
                long dataPtr = binReader.ReadInt64();
                Assert.InRange(dataPtr, 1, stream.Length);
                stream.Position = dataPtr;
                for (int i = 0; i < 7; ++i)
                {
                    var b = binReader.ReadByte();
                    Assert.Equal(233 + i, b);
                }
            }
        }
Пример #2
0
        public void TestGetFileLength()
        {
            var hint = log.CreateFile();

            Assert.Equal(0, logReader.GetFileLength(hint));
            log.AppendDataToFile(hint, new ByteArray(new byte[] { 1, 2, 3, 4 }));
            Assert.Equal(4, logReader.GetFileLength(hint));
            log.AppendDataToFile(hint, new ByteArray(new byte[] { 1, 2, 3, 4 }));
            Assert.Equal(8, logReader.GetFileLength(hint));
        }
Пример #3
0
        public void TestCreateAndAppendFile_OneBlock()
        {
            var file1 = log.CreateFile();

            log.AppendDataToFile(file1, new ByteArray(new byte[] { 1, 2, 3, 4 }));
            log.AppendDataToFile(file1, new ByteArray(new byte[] { 5, 6, 7, 8 }));
            log.Close();
            FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read);

            using (BinaryReader binReader = new BinaryReader(stream))
            {
                // a pointer to format info
                stream.Position = 0;
                // a pointer to file table
                stream.Position = binReader.ReadInt64() + 4 * sizeof(long);
                var fileTable = binReader.ReadInt64();
                Assert.InRange(fileTable, 1, stream.Length - 1);
                // file count
                Assert.Equal(1, binReader.ReadInt64());
                // skip file table next pointer
                stream.Position = fileTable + sizeof(long);
                long filePtr = binReader.ReadInt64();
                Assert.InRange(filePtr, 1, stream.Length - 1);
                stream.Position = filePtr;
                // file size
                Assert.Equal(8, binReader.ReadInt64());
                // FBT - next pointer
                Assert.Equal(0, binReader.ReadInt64());
                // FBT - first FBT2 entry
                long fbt2Ptr = binReader.ReadInt64();
                Assert.InRange(fbt2Ptr, 1, stream.Length - 1);
                stream.Position = fbt2Ptr;
                long dataPtr = binReader.ReadInt64();
                Assert.InRange(dataPtr, 1, stream.Length - 1);
                stream.Position = dataPtr;
                for (int i = 1; i <= 8; i++)
                {
                    Assert.Equal(i, binReader.ReadByte());
                }
                binReader.Close();
            }
        }
Пример #4
0
        public void TestReadFileDataToBuffer_Big()
        {
            var hint = log.CreateFile();

            logReader.Close();
            log.Close();
            var stream      = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
            var newFileSize = FileLogTestsHelper.CreateBigLogFile(hint.Hint, stream);

            stream.Close();
            log = new FileLog(filename, FileMode.Open);
            log.AppendDataToFile(hint, new ByteArray(new byte[] { 1, 2, 3, 4 }));
            byte[] buffer = new byte[128];
            buffer[0] = 15;
            logReader = log.CreateReader();
            logReader.ReadFileDataToBuffer(hint, buffer, newFileSize, 4, 1);
            Assert.Equal(15, buffer[0]);
            Assert.Equal(1, buffer[1]);
            Assert.Equal(2, buffer[2]);
        }