Esempio n. 1
0
        uint ReadRelationVersions(uint relationIndex, string name, Dictionary <uint, RelationVersionInfo> relationVersions)
        {
            uint lastPersistedVersion = 0;
            var  relationInfoResolver = new RelationInfoResolver((ObjectDB)_tr.Owner);

            var writer = new SpanWriter();

            writer.WriteByteArrayRaw(ObjectDB.RelationVersionsPrefix);
            writer.WriteVUInt32(relationIndex);
            var prefix = writer.GetSpan().ToArray();

            if (!_trkv.FindFirstKey(prefix))
            {
                return(lastPersistedVersion);
            }
            do
            {
                var keyReader   = new SpanReader(_trkv.GetKey().Slice(prefix.Length));
                var valueReader = new SpanReader(_trkv.GetValue());
                lastPersistedVersion = keyReader.ReadVUInt32();
                var relationVersionInfo = RelationVersionInfo.LoadUnresolved(ref valueReader, name);
                relationVersionInfo.ResolveFieldHandlers(relationInfoResolver.FieldHandlerFactory);
                relationVersions[lastPersistedVersion] = relationVersionInfo;
            } while (_trkv.FindNextKey(prefix));

            return(lastPersistedVersion);
        }
Esempio n. 2
0
        void TestSerialization(object value)
        {
            var writer = new SpanWriter();
            var storedDescriptorCtx = _mapping.StoreNewDescriptors(value);

            storedDescriptorCtx.FinishNewDescriptors(ref writer);
            storedDescriptorCtx.StoreObject(ref writer, value);
            storedDescriptorCtx.CommitNewDescriptors();
            var reader = new SpanReader(writer.GetSpan());

            _mapping.LoadTypeDescriptors(ref reader);
            Assert.Equal(value, _mapping.LoadObject(ref reader));
            Assert.True(reader.Eof);
            _mapping = _ts.CreateMapping();
            reader   = new SpanReader(writer.GetSpan());
            _mapping.LoadTypeDescriptors(ref reader);
            Assert.Equal(value, _mapping.LoadObject(ref reader));
            Assert.True(reader.Eof);
        }
Esempio n. 3
0
        ReadOnlySpan <byte> ValueToByteArray(TValue value)
        {
            var        writer = new SpanWriter();
            IWriterCtx ctx    = null;

            if (_valueHandler.NeedsCtx())
            {
                ctx = new DBWriterCtx(_tr);
            }
            _valueWriter(value, ref writer, ctx);
            return(writer.GetSpan());
        }
Esempio n. 4
0
        public ListFieldHandler(IFieldHandlerFactory fieldHandlerFactory, ITypeConvertorGenerator typeConvertGenerator, Type type)
        {
            _fieldHandlerFactory  = fieldHandlerFactory;
            _typeConvertGenerator = typeConvertGenerator;
            _type         = type;
            _isSet        = type.InheritsOrImplements(typeof(ISet <>));
            _itemsHandler = _fieldHandlerFactory.CreateFromType(type.GetGenericArguments()[0], FieldHandlerOptions.None);
            var writer = new SpanWriter();

            writer.WriteFieldHandler(_itemsHandler);
            Configuration = writer.GetSpan().ToArray();
        }
Esempio n. 5
0
        public ODBSetFieldHandler(IObjectDB odb, Type type, IFieldHandlerFactory fieldHandlerFactory)
        {
            _odb = odb;
            _typeConvertGenerator = odb.TypeConvertorGenerator;
            _type        = type;
            _keysHandler = fieldHandlerFactory.CreateFromType(type.GetGenericArguments()[0],
                                                              FieldHandlerOptions.Orderable | FieldHandlerOptions.AtEndOfStream);
            var writer = new SpanWriter();

            writer.WriteFieldHandler(_keysHandler);
            _configuration = writer.GetSpan().ToArray();
            CreateConfiguration();
        }
Esempio n. 6
0
        public DictionaryFieldHandler(IFieldHandlerFactory fieldHandlerFactory, ITypeConvertorGenerator typeConvertGenerator, Type type)
        {
            _fieldHandlerFactory  = fieldHandlerFactory;
            _typeConvertGenerator = typeConvertGenerator;
            _type          = type;
            _keysHandler   = _fieldHandlerFactory.CreateFromType(type.GetGenericArguments()[0], FieldHandlerOptions.None);
            _valuesHandler = _fieldHandlerFactory.CreateFromType(type.GetGenericArguments()[1], FieldHandlerOptions.None);
            var writer = new SpanWriter();

            writer.WriteFieldHandler(_keysHandler);
            writer.WriteFieldHandler(_valuesHandler);
            Configuration = writer.GetSpan().ToArray();
        }
Esempio n. 7
0
        ReadOnlySpan <byte> KeyToByteArray(TKey key)
        {
            var        writer = new SpanWriter();
            IWriterCtx ctx    = null;

            if (_keyHandler.NeedsCtx())
            {
                ctx = new DBWriterCtx(_tr);
            }
            writer.WriteBlock(_prefix);
            _keyWriter(key, ref writer, ctx);
            return(writer.GetSpan());
        }
Esempio n. 8
0
        void CanSerializeSimpleValue(object value)
        {
            var writer = new SpanWriter();
            var storedDescriptorCtx = _mapping.StoreNewDescriptors(value);

            storedDescriptorCtx.FinishNewDescriptors(ref writer);
            storedDescriptorCtx.StoreObject(ref writer, value);
            storedDescriptorCtx.CommitNewDescriptors();
            var reader = new SpanReader(writer.GetSpan());
            var obj    = _mapping.LoadObject(ref reader);

            Assert.Equal(value, obj);
        }
Esempio n. 9
0
        public void CanSerializeInt()
        {
            var writer = new SpanWriter();
            var storedDescriptorCtx = _mapping.StoreNewDescriptors(12345);

            storedDescriptorCtx.FinishNewDescriptors(ref writer);
            storedDescriptorCtx.StoreObject(ref writer, 12345);
            storedDescriptorCtx.CommitNewDescriptors();
            var reader = new SpanReader(writer.GetSpan());
            var obj    = _mapping.LoadObject(ref reader);

            Assert.Equal(12345, obj);
        }
Esempio n. 10
0
        public void WriteOrderedEncryptedString(ref SpanWriter outerWriter, EncryptedString value)
        {
            var writer = new SpanWriter();

            writer.WriteString(value);
            var cipher  = _transaction.Owner.GetSymmetricCipher();
            var plain   = writer.GetSpan();
            var encSize = cipher.CalcOrderedEncryptedSizeFor(plain);
            var enc     = new byte[encSize];

            cipher.OrderedEncrypt(plain, enc);
            outerWriter.WriteByteArray(enc);
        }
Esempio n. 11
0
        public NullableFieldHandler(IFieldHandlerFactory fieldHandlerFactory, ITypeConvertorGenerator typeConvertorGenerator, Type type)
        {
            _fieldHandlerFactory    = fieldHandlerFactory;
            _typeConvertorGenerator = typeConvertorGenerator;
            _type        = type;
            _itemHandler = _fieldHandlerFactory.CreateFromType(type.GetGenericArguments()[0], FieldHandlerOptions.None);
            if (_itemHandler.NeedsCtx())
            {
                throw new NotSupportedException("Nullable complex types are not supported.");
            }
            var writer = new SpanWriter();

            writer.WriteFieldHandler(_itemHandler);
            Configuration = writer.GetSpan().ToArray();
        }
Esempio n. 12
0
 public byte[] ToConfiguration()
 {
     var writer = new SpanWriter();
     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.GetSpan().ToArray();
 }
Esempio n. 13
0
 public DBObjectFieldHandler(IObjectDB objectDb, Type type)
 {
     _objectDb = objectDb;
     _type     = Unwrap(type);
     _indirect = _type != type;
     if (_type.IsInterface || _type.IsAbstract)
     {
         _typeName      = null;
         _configuration = Array.Empty <byte>();
     }
     else
     {
         _typeName = (_objectDb as ObjectDB)?.RegisterType(_type, false);
         var writer = new SpanWriter();
         writer.WriteString(_typeName);
         _configuration = writer.GetSpan().ToArray();
     }
 }
Esempio n. 14
0
        public void CanSerializeSimpleDto()
        {
            var writer = new SpanWriter();
            var value  = new SimpleDto {
                IntField = 42, StringField = "Hello"
            };
            var storedDescriptorCtx = _mapping.StoreNewDescriptors(value);

            storedDescriptorCtx.FinishNewDescriptors(ref writer);
            storedDescriptorCtx.StoreObject(ref writer, value);
            storedDescriptorCtx.CommitNewDescriptors();
            var reader = new SpanReader(writer.GetSpan());

            _mapping.LoadTypeDescriptors(ref reader);
            var obj = (SimpleDto)_mapping.LoadObject(ref reader);

            Assert.Equal(value.IntField, obj.IntField);
            Assert.Equal(value.StringField, obj.StringField);
        }
Esempio n. 15
0
        dynamic ConvertToDynamicThroughSerialization(object value)
        {
            var writer = new SpanWriter();
            var storedDescriptorCtx = _mapping.StoreNewDescriptors(value);

            storedDescriptorCtx.FinishNewDescriptors(ref writer);
            storedDescriptorCtx.StoreObject(ref writer, value);
            storedDescriptorCtx.CommitNewDescriptors();
            var originalDescription = _ts.DescriptorOf(value).Describe();
            var reader = new SpanReader(writer.GetSpan());
            var ts     = new TypeSerializers();

            ts.SetTypeNameMapper(new ToDynamicMapper());
            var mapping = ts.CreateMapping();

            mapping.LoadTypeDescriptors(ref reader);
            var obj = (dynamic)mapping.LoadObject(ref reader);

            Assert.Equal(originalDescription, ts.DescriptorOf((object)obj) !.Describe());
            return(obj);
        }
Esempio n. 16
0
        static void TestWriteRead(SpanWriterAction writeAction, byte[] checkResult,
                                  SpanReaderAction readAction, SpanReaderAction?skipAction)
        {
            Span <byte> buf = stackalloc byte[1];
            var         sw  = new SpanWriter(buf);

            writeAction(ref sw);
            Assert.Equal(checkResult, sw.GetSpan().ToArray());
            SpanReader sr;

            if (checkResult.Length > 1)
            {
                sw = new SpanWriter(new Span <byte>());
                writeAction(ref sw);
                Assert.Equal(checkResult, sw.GetSpanAndReset().ToArray());
                writeAction(ref sw);
                Assert.Equal(checkResult, sw.GetSpan().ToArray());
            }
            sr = new SpanReader(checkResult);
            readAction(ref sr);
            Assert.True(sr.Eof);
            sw = new SpanWriter();
            writeAction(ref sw);
            writeAction(ref sw);
            Assert.Equal(checkResult.Concat(checkResult).ToArray(), sw.GetByteBufferAndReset().ToByteArray());
            sr = new SpanReader(checkResult.Concat(checkResult).ToArray());
            readAction(ref sr);
            readAction(ref sr);
            if (skipAction != null)
            {
                sr = new SpanReader(checkResult.Concat(checkResult).ToArray());
                skipAction(ref sr);
                readAction(ref sr);
                Assert.True(sr.Eof);
                sr = new SpanReader(checkResult.Concat(checkResult).ToArray());
                readAction(ref sr);
                skipAction(ref sr);
                Assert.True(sr.Eof);
            }
        }
Esempio n. 17
0
        internal RelationInfo CreateByName(IInternalObjectDBTransaction tr, string name, Type interfaceType,
                                           RelationBuilder builder)
        {
            name = string.Intern(name);
            if (!_name2Id.TryGetValue(name, out var id))
            {
                id             = _freeId++;
                _name2Id[name] = id;
                var nameWriter = new SpanWriter();
                nameWriter.WriteBlock(ObjectDB.RelationNamesPrefix);
                nameWriter.WriteString(name);
                var idWriter = new SpanWriter();
                idWriter.WriteVUInt32(id);
                tr.KeyValueDBTransaction.CreateOrUpdateKeyValue(nameWriter.GetSpan(), idWriter.GetSpan());
            }

            if (_id2Relation.TryGetValue(id, out var relation))
            {
                throw new BTDBException($"Relation with name '{name}' was already initialized");
            }
            relation         = new RelationInfo(id, name, builder, tr);
            _id2Relation[id] = relation;
            return(relation);
        }