Inheritance: AbstractBufferedWriter
Ejemplo n.º 1
0
 internal RelationInfo CreateByName(IInternalObjectDBTransaction tr, string name, Type interfaceType)
 {
     name = string.Intern(name);
     uint id;
     if (!_name2Id.TryGetValue(name, out id))
     {
         id = _freeId++;
         _name2Id[name] = id;
         tr.KeyValueDBTransaction.SetKeyPrefixUnsafe(ObjectDB.RelationNamesPrefix);
         var nameWriter = new ByteBufferWriter();
         nameWriter.WriteString(name);
         var idWriter = new ByteBufferWriter();
         idWriter.WriteVUInt32(id);
         tr.KeyValueDBTransaction.CreateOrUpdateKeyValue(nameWriter.Data, idWriter.Data);
     }
     RelationInfo relation;
     if (_id2Relation.TryGetValue(id, out relation))
     {
         throw new BTDBException($"Relation with name '{name}' was already initialized");
     }
     var clientType = FindClientType(interfaceType);
     relation = new RelationInfo(id, name, _relationInfoResolver, interfaceType, clientType, tr);
     _id2Relation[id] = relation;
     return relation;
 }
Ejemplo n.º 2
0
 void SerializeIntoBuffer(object metadata, IReadOnlyList<object> events, out int startOffset,
                          out IDescriptorSerializerContext serializerContext, out BlockType blockType,
                          out int lenWithoutEndPadding, out ByteBuffer block)
 {
     startOffset = (int)EndBufferLen + HeaderSize;
     var writer = new ByteBufferWriter();
     writer.WriteBlock(_zeroes, 0, startOffset);
     serializerContext = Mapping;
     if (metadata != null)
         serializerContext = serializerContext.StoreNewDescriptors(writer, metadata);
     if (events != null)
     {
         foreach (var o in events)
         {
             serializerContext = serializerContext.StoreNewDescriptors(writer, o);
         }
         if (events.Count == 0) events = null;
     }
     serializerContext.FinishNewDescriptors(writer);
     blockType = BlockType.FirstBlock;
     if (serializerContext.SomeTypeStored)
         blockType |= BlockType.HasTypeDeclaration;
     if (metadata != null)
     {
         serializerContext.StoreObject(writer, metadata);
         blockType |= BlockType.HasMetadata;
     }
     if (events != null)
     {
         if (events.Count == 1)
         {
             serializerContext.StoreObject(writer, events[0]);
             blockType |= BlockType.HasOneEvent;
         }
         else
         {
             writer.WriteVUInt32((uint)events.Count);
             foreach (var o in events)
             {
                 serializerContext.StoreObject(writer, o);
             }
             blockType |= BlockType.HasMoreEvents;
         }
     }
     lenWithoutEndPadding = (int)writer.GetCurrentPosition();
     writer.WriteBlock(_zeroes, 0, (int)(SectorSize - 1));
     block = writer.Data;
     if (CompressionStrategy.ShouldTryToCompress(lenWithoutEndPadding - startOffset))
     {
         var compressedBlock = ByteBuffer.NewSync(block.Buffer, startOffset, lenWithoutEndPadding - startOffset);
         if (CompressionStrategy.Compress(ref compressedBlock))
         {
             blockType |= BlockType.Compressed;
             Array.Copy(compressedBlock.Buffer, compressedBlock.Offset, block.Buffer, startOffset, compressedBlock.Length);
             lenWithoutEndPadding = startOffset + compressedBlock.Length;
             Array.Copy(_zeroes, 0, block.Buffer, lenWithoutEndPadding, (int)SectorSize - 1);
         }
     }
 }
Ejemplo n.º 3
0
 public ServiceObjectFieldHandler(IServiceInternal service, Type type)
 {
     _service = service;
     _type = type;
     _typeName = _service.RegisterType(type);
     var writer = new ByteBufferWriter();
     writer.WriteString(_typeName);
     _configuration = writer.Data.ToByteArray();
 }
Ejemplo n.º 4
0
 public DBObjectFieldHandler(IObjectDB objectDB, Type type)
 {
     _objectDB = objectDB;
     _type = Unwrap(type);
     _indirect = _type != type;
     _typeName = _objectDB.RegisterType(_type);
     var writer = new ByteBufferWriter();
     writer.WriteString(_typeName);
     _configuration = writer.Data.ToByteArray();
 }
Ejemplo n.º 5
0
 public ListFieldHandler(IFieldHandlerFactory fieldHandlerFactory, ITypeConvertorGenerator typeConvertorGenerator, Type type)
 {
     _fieldHandlerFactory = fieldHandlerFactory;
     _typeConvertorGenerator = typeConvertorGenerator;
     _type = type;
     _itemsHandler = _fieldHandlerFactory.CreateFromType(type.GetGenericArguments()[0], FieldHandlerOptions.None);
     var writer = new ByteBufferWriter();
     writer.WriteFieldHandler(_itemsHandler);
     _configuration = writer.Data.ToByteArray();
 }
Ejemplo n.º 6
0
 internal void CommitLastDictId(ulong newLastDictId, IKeyValueDBTransaction tr)
 {
     if (_lastDictId != newLastDictId)
     {
         tr.SetKeyPrefix(null);
         var w = new ByteBufferWriter();
         w.WriteVUInt64(newLastDictId);
         tr.CreateOrUpdateKeyValue(LastDictIdKey, w.Data.ToByteArray());
         _lastDictId = newLastDictId;
     }
 }
Ejemplo n.º 7
0
 public ODBDictionaryFieldHandler(IObjectDB odb, Type type)
 {
     _odb = odb;
     _fieldHandlerFactory = odb.FieldHandlerFactory;
     _typeConvertorGenerator = odb.TypeConvertorGenerator;
     _type = type;
     _keysHandler = _fieldHandlerFactory.CreateFromType(type.GetGenericArguments()[0], FieldHandlerOptions.Orderable | FieldHandlerOptions.AtEndOfStream);
     _valuesHandler = _fieldHandlerFactory.CreateFromType(type.GetGenericArguments()[1], FieldHandlerOptions.None);
     var writer = new ByteBufferWriter();
     writer.WriteFieldHandler(_keysHandler);
     writer.WriteFieldHandler(_valuesHandler);
     _configuration = writer.Data.ToByteArray();
     CreateConfiguration();
 }
Ejemplo n.º 8
0
 public byte[] ToConfiguration()
 {
     var writer = new ByteBufferWriter();
     writer.WriteVUInt32((_signed ? 1u : 0) + (Flags ? 2u : 0) + 4u * (uint)Names.Length);
     foreach (var name in Names)
     {
         writer.WriteString(name);
     }
     foreach (var value in Values)
     {
         if (_signed) writer.WriteVInt64((long)value);
         else writer.WriteVUInt64(value);
     }
     return writer.Data.ToByteArray();
 }
Ejemplo n.º 9
0
 public void ReadToEnd(IEventStoreObserver observer)
 {
     var overflowWriter = default(ByteBufferWriter);
     var bufferBlock = new byte[FirstReadAhead + MaxBlockSize];
     var bufferStartPosition = NextReadPosition & SectorMask;
     var bufferFullLength = 0;
     var bufferReadOffset = (int)(NextReadPosition - bufferStartPosition);
     var currentReadAhead = FirstReadAhead;
     var buf = ByteBuffer.NewSync(bufferBlock, bufferFullLength, currentReadAhead);
     var bufReadLength = (int)File.Read(buf, bufferStartPosition);
     bufferFullLength += bufReadLength;
     while (true)
     {
         if (bufferStartPosition + (ulong)bufferReadOffset + HeaderSize > File.MaxFileSize)
         {
             KnownAsFinished = true;
             return;
         }
         if (bufferReadOffset == bufferFullLength)
         {
             break;
         }
         if (bufferReadOffset + HeaderSize > bufferFullLength)
         {
             for (var i = bufferReadOffset; i < bufferFullLength; i++)
             {
                 if (bufferBlock[i] != 0)
                 {
                     SetCorrupted();
                     return;
                 }
             }
             break;
         }
         var blockCheckSum = PackUnpack.UnpackUInt32LE(bufferBlock, bufferReadOffset);
         bufferReadOffset += 4;
         var blockLen = PackUnpack.UnpackUInt32LE(bufferBlock, bufferReadOffset);
         if (blockCheckSum == 0 && blockLen == 0)
         {
             bufferReadOffset -= 4;
             break;
         }
         var blockType = (BlockType)(blockLen & 0xff);
         blockLen >>= 8;
         if (blockType == BlockType.LastBlock && blockLen == 0)
         {
             if (Checksum.CalcFletcher32(bufferBlock, (uint)bufferReadOffset, 4) != blockCheckSum)
             {
                 SetCorrupted();
                 return;
             }
             KnownAsFinished = true;
             return;
         }
         if (blockLen == 0 && blockType != (BlockType.FirstBlock | BlockType.LastBlock))
         {
             SetCorrupted();
             return;
         }
         if (blockLen + HeaderSize > MaxBlockSize)
         {
             SetCorrupted();
             return;
         }
         bufferReadOffset += 4;
         var bufferLenToFill = (uint)(bufferReadOffset + (int)blockLen + FirstReadAhead) & SectorMaskUInt;
         if (bufferLenToFill > bufferBlock.Length) bufferLenToFill = (uint)bufferBlock.Length;
         buf = ByteBuffer.NewSync(bufferBlock, bufferFullLength, (int)(bufferLenToFill - bufferFullLength));
         if (buf.Length > 0)
         {
             bufferLenToFill = (uint)(bufferReadOffset + (int)blockLen + currentReadAhead) & SectorMaskUInt;
             if (bufferLenToFill > bufferBlock.Length) bufferLenToFill = (uint)bufferBlock.Length;
             if (bufferStartPosition + bufferLenToFill > File.MaxFileSize)
             {
                 bufferLenToFill = (uint)(File.MaxFileSize - bufferStartPosition);
             }
             buf = ByteBuffer.NewSync(bufferBlock, bufferFullLength, (int)(bufferLenToFill - bufferFullLength));
             if (buf.Length > 0) {
                 if (currentReadAhead * 4 < MaxBlockSize)
                 {
                     currentReadAhead = currentReadAhead * 2;
                 }
                 bufReadLength = (int)File.Read(buf, bufferStartPosition + (ulong)bufferFullLength);
                 bufferFullLength += bufReadLength;
             }
         }
         if (bufferReadOffset + (int)blockLen > bufferFullLength)
         {
             SetCorrupted();
             return;
         }
         if (Checksum.CalcFletcher32(bufferBlock, (uint)bufferReadOffset - 4, blockLen + 4) != blockCheckSum)
         {
             SetCorrupted();
             return;
         }
         var blockTypeBlock = blockType & (BlockType.FirstBlock | BlockType.MiddleBlock | BlockType.LastBlock);
         var stopReadingRequested = false;
         if (blockTypeBlock == (BlockType.FirstBlock | BlockType.LastBlock))
         {
             stopReadingRequested = Process(blockType, ByteBuffer.NewSync(bufferBlock, bufferReadOffset, (int)blockLen), observer);
         }
         else
         {
             if (blockTypeBlock == BlockType.FirstBlock)
             {
                 overflowWriter = new ByteBufferWriter();
             }
             else if (blockTypeBlock == BlockType.MiddleBlock || blockTypeBlock == BlockType.LastBlock)
             {
                 if (overflowWriter == null)
                 {
                     SetCorrupted();
                     return;
                 }
             }
             else
             {
                 SetCorrupted();
                 return;
             }
             overflowWriter.WriteBlock(ByteBuffer.NewSync(bufferBlock, bufferReadOffset, (int)blockLen));
             if (blockTypeBlock == BlockType.LastBlock)
             {
                 stopReadingRequested = Process(blockType, overflowWriter.Data, observer);
                 overflowWriter = null;
             }
         }
         bufferReadOffset += (int)blockLen;
         if (overflowWriter == null)
             NextReadPosition = bufferStartPosition + (ulong)bufferReadOffset;
         if (stopReadingRequested)
         {
             return;
         }
         var nextBufferStartPosition = (bufferStartPosition + (ulong)bufferReadOffset) & SectorMask;
         var bufferMoveDistance = (int)(nextBufferStartPosition - bufferStartPosition);
         if (bufferMoveDistance <= 0) continue;
         Array.Copy(bufferBlock, bufferMoveDistance, bufferBlock, 0, bufferFullLength - bufferMoveDistance);
         bufferStartPosition = nextBufferStartPosition;
         bufferFullLength -= bufferMoveDistance;
         bufferReadOffset -= bufferMoveDistance;
     }
     if (overflowWriter != null)
     {
         // It is not corrupted here just unfinished, but definitely not appendable
         EndBufferPosition = ulong.MaxValue;
         return;
     }
     EndBufferLen = (uint)(bufferReadOffset - (bufferReadOffset & SectorMaskUInt));
     EndBufferPosition = bufferStartPosition + (ulong)bufferReadOffset - EndBufferLen;
     Array.Copy(bufferBlock, bufferReadOffset - EndBufferLen, EndBuffer, 0, EndBufferLen);
 }