private void TryReadingExistingFile()
		{
			using (var reader = OpenReader())
			using (var binaryReader = new BinaryReaderWith7BitEncoding(reader))
			{
				var headerSignature = binaryReader.ReadBytes(16);
				if (headerSignature .Length != 16 || new Guid(headerSignature) != HeaderSignatureGuid)
					throw new Exceptions.InvalidFileFormatException("File signature is invalid, probably not a valid Raven storage file, or a corrupted one");
				var version = binaryReader.Read7BitEncodedInt();
				if (version != Version)
					throw new Exceptions.InvalidFileFormatException("File signature is valid, but the version information is " +
						version + ", while " + Version + " was expected");
				var fileId = binaryReader.ReadBytes(16);
				if(fileId.Length!=16)
					throw new Exceptions.InvalidFileFormatException("File signature is valid, but the file ID has been truncated");
				Id = new Guid(fileId);

				var pos = reader.Length;
				while (pos > 16)
				{
					reader.Position = pos - 16;
					if (new Guid(binaryReader.ReadBytes(16)) == TransactionSignatureGuid)
					{
						reader.Position = pos - 24; // move to the position of the transaction itself
						var txPos = binaryReader.ReadInt64();
						reader.Position = txPos;
						transaction = new JsonSerializer().Deserialize<StorageTransaction>(new BsonReader(reader));
						return;
					}
					pos -= 1;
				}
				throw new Exceptions.InvalidFileFormatException("Could not find a valid transaction in the file");
			}
		}
Example #2
0
		public Tree(Stream reader, Stream writer, StartMode mode)
		{
			this.reader = reader;
			this.writer = writer;
			binaryReader = new BinaryReaderWith7BitEncoding(this.reader);
			binaryWriter = new BinaryWriterWith7BitEncoding(this.writer);

			root = mode == StartMode.Create
				? new TreeNode(null, null, null, null, ReadNode, WriteNodeLazy)
				: ReadNode(new StreamPosition(reader.Position, null));
		}
Example #3
0
		public void CanEncodeUsing7BitInt64()
		{
			var buffer = new byte[1024];

			for (long i = 0; i < 64*1024; i++)
			{
				var bw = new BinaryWriterWith7BitEncoding(new MemoryStream(buffer));
				bw.Write7BitEncodedInt64(i);
				var br = new BinaryReaderWith7BitEncoding(new MemoryStream(buffer));
				Assert.Equal(i, br.Read7BitEncodedInt64());
			}
		}
        public void CanEncodeUsing7BitInt64()
        {
            var buffer = new byte[1024];

            for (long i = 0; i < 64 * 1024; i++)
            {
                var bw = new BinaryWriterWith7BitEncoding(new MemoryStream(buffer));
                bw.Write7BitEncodedInt64(i);
                var br = new BinaryReaderWith7BitEncoding(new MemoryStream(buffer));
                Assert.Equal(i, br.Read7BitEncodedInt64());
            }
        }
Example #5
0
		public Queue(Stream reader, Stream writer, StartMode mode)
		{
			this.reader = reader;
			this.writer = writer;

			binaryReader = new BinaryReaderWith7BitEncoding(reader);
			binaryWriter = new BinaryWriterWith7BitEncoding(writer);

			if(mode == StartMode.Open)
			{
				currentId = binaryReader.Read7BitEncodedInt64();
				var treePos = binaryReader.Read7BitEncodedInt64();
				reader.Position = treePos;
			}
			tree = new Tree(reader, writer, mode);
		}
Example #6
0
        private void TryReadingExistingFile()
        {
            using (var reader = OpenReader())
                using (var binaryReader = new BinaryReaderWith7BitEncoding(reader))
                {
                    var headerSignature = binaryReader.ReadBytes(16);
                    if (headerSignature.Length != 16 || new Guid(headerSignature) != HeaderSignatureGuid)
                    {
                        throw new Exceptions.InvalidFileFormatException("File signature is invalid, probably not a valid Raven storage file, or a corrupted one");
                    }
                    var version = binaryReader.Read7BitEncodedInt();
                    if (version != Version)
                    {
                        throw new Exceptions.InvalidFileFormatException("File signature is valid, but the version information is " +
                                                                        version + ", while " + Version + " was expected");
                    }
                    var fileId = binaryReader.ReadBytes(16);
                    if (fileId.Length != 16)
                    {
                        throw new Exceptions.InvalidFileFormatException("File signature is valid, but the file ID has been truncated");
                    }
                    Id = new Guid(fileId);

                    var pos = reader.Length;
                    while (pos > 16)
                    {
                        reader.Position = pos - 16;
                        if (new Guid(binaryReader.ReadBytes(16)) == TransactionSignatureGuid)
                        {
                            reader.Position = pos - 24;                     // move to the position of the transaction itself
                            var txPos = binaryReader.ReadInt64();
                            reader.Position = txPos;
                            transaction     = new JsonSerializer().Deserialize <StorageTransaction>(new BsonReader(reader));
                            return;
                        }
                        pos -= 1;
                    }
                    throw new Exceptions.InvalidFileFormatException("Could not find a valid transaction in the file");
                }
        }