Example #1
0
        public static void DoFreeContent(IReaderCtx ctx, ulong id, int cfgId)
        {
            var readerCtx = (DBReaderCtx)ctx;
            var tr        = readerCtx.GetTransaction();
            var dict      = new ODBDictionary <TKey, TValue>(tr, ODBDictionaryConfiguration.Get(cfgId), id);

            dict.FreeContent(ctx, cfgId);
        }
Example #2
0
        void CreateConfiguration()
        {
            HandledType();
            var keyAndValueTypes = _type !.GetGenericArguments();
            var cfg = new ODBDictionaryConfiguration(_odb, _keysHandler, null)
            {
                KeyReader = CreateReader(_keysHandler, keyAndValueTypes[0]),
                KeyWriter = CreateWriter(_keysHandler, keyAndValueTypes[0]),
            };

            _configurationId = ((IInstanceRegistry)_odb).RegisterInstance(cfg);
        }
 void CreateConfiguration()
 {
     HandledType();
     var cfg = new ODBDictionaryConfiguration(_odb, _keysHandler, _valuesHandler)
         {
             KeyReader = CreateReader(_keysHandler, _type.GetGenericArguments()[0]),
             KeyWriter = CreateWriter(_keysHandler, _type.GetGenericArguments()[0]),
             ValueReader = CreateReader(_valuesHandler, _type.GetGenericArguments()[1]),
             ValueWriter = CreateWriter(_valuesHandler, _type.GetGenericArguments()[1])
         };
     _configurationId = ((IInstanceRegistry)_odb).RegisterInstance(cfg);
 }
Example #4
0
        void CreateConfiguration()
        {
            HandledType();
            var cfg = new ODBDictionaryConfiguration(_odb, _keysHandler, _valuesHandler)
            {
                KeyReader   = CreateReader(_keysHandler, _type.GetGenericArguments()[0]),
                KeyWriter   = CreateWriter(_keysHandler, _type.GetGenericArguments()[0]),
                ValueReader = CreateReader(_valuesHandler, _type.GetGenericArguments()[1]),
                ValueWriter = CreateWriter(_valuesHandler, _type.GetGenericArguments()[1])
            };

            _configurationId = ((IInstanceRegistry)_odb).RegisterInstance(cfg);
        }
Example #5
0
        void CreateConfiguration()
        {
            HandledType();
            var keyAndValueTypes = _type !.GetGenericArguments();

            _configurationId = ODBDictionaryConfiguration.Register(_keysHandler, keyAndValueTypes[0], null, null);
            var cfg = ODBDictionaryConfiguration.Get(_configurationId);

            lock (cfg)
            {
                cfg.KeyReader ??= CreateReader(_keysHandler, keyAndValueTypes[0]);
                cfg.KeyWriter ??= CreateWriter(_keysHandler, keyAndValueTypes[0]);
            }
        }
Example #6
0
 public ODBDictionary(IInternalObjectDBTransaction tr, ODBDictionaryConfiguration config, ulong id)
 {
     _tr           = tr;
     _keyHandler   = config.KeyHandler;
     _valueHandler = config.ValueHandler;
     _id           = id;
     GeneratePrefix();
     _keyReader           = (Func <AbstractBufferedReader, IReaderCtx, TKey>)config.KeyReader;
     _keyWriter           = (Action <TKey, AbstractBufferedWriter, IWriterCtx>)config.KeyWriter;
     _valueReader         = (Func <AbstractBufferedReader, IReaderCtx, TValue>)config.ValueReader;
     _valueWriter         = (Action <TValue, AbstractBufferedWriter, IWriterCtx>)config.ValueWriter;
     _keyValueTr          = _tr.KeyValueDBTransaction;
     _keyValueTrProtector = _tr.TransactionProtector;
     _count = -1;
 }
Example #7
0
        // ReSharper disable once MemberCanBePrivate.Global used by FieldHandler.Load
        public ODBSet(IInternalObjectDBTransaction tr, ODBDictionaryConfiguration config, ulong id)
        {
            _tr         = tr;
            _keyHandler = config.KeyHandler;
            _id         = id;
            var len    = PackUnpack.LengthVUInt(id);
            var prefix = new byte[ObjectDB.AllDictionariesPrefixLen + len];

            MemoryMarshal.GetReference(prefix.AsSpan()) = ObjectDB.AllDictionariesPrefixByte;
            PackUnpack.UnsafePackVUInt(ref Unsafe.AddByteOffset(ref MemoryMarshal.GetReference(prefix.AsSpan()), (IntPtr)ObjectDB.AllDictionariesPrefixLen), id, len);
            _prefix     = prefix;
            _keyReader  = ((ReaderFun <TKey>)config.KeyReader) !;
            _keyWriter  = ((WriterFun <TKey>)config.KeyWriter) !;
            _keyValueTr = _tr.KeyValueDBTransaction;
            _count      = -1;
        }
Example #8
0
        int GetConfigurationId(Type type)
        {
            var keyAndValueTypes = type.GetGenericArguments();
            var configurationId  = ODBDictionaryConfiguration.Register(_keysHandler, keyAndValueTypes[0], _valuesHandler, keyAndValueTypes[1]);
            var cfg = ODBDictionaryConfiguration.Get(configurationId);

            lock (cfg)
            {
                cfg.KeyReader ??= CreateReader(_keysHandler, keyAndValueTypes[0]);
                cfg.KeyWriter ??= CreateWriter(_keysHandler, keyAndValueTypes[0]);
                cfg.ValueReader ??= CreateReader(_valuesHandler, keyAndValueTypes[1]);
                cfg.ValueWriter ??= CreateWriter(_valuesHandler, keyAndValueTypes[1]);
            }

            return(configurationId);
        }
Example #9
0
        public void Init(IILGen ilGenerator, Action <IILGen> pushReaderCtx)
        {
            var genericArguments = _type !.GetGenericArguments();
            var instanceType     = typeof(ODBDictionary <,>).MakeGenericType(genericArguments);
            var constructorInfo  = instanceType.GetConstructor(
                new[] { typeof(IInternalObjectDBTransaction), typeof(ODBDictionaryConfiguration) });

            ilGenerator
            .Do(pushReaderCtx)
            .Castclass(typeof(IDBReaderCtx))
            .Callvirt(() => default(IDBReaderCtx).GetTransaction())
            .LdcI4(_configurationId)
            .Call(() => ODBDictionaryConfiguration.Get(0))
            .Newobj(constructorInfo !)
            .Castclass(_type);
        }
Example #10
0
        public ODBSet(IInternalObjectDBTransaction tr, ODBDictionaryConfiguration config, ulong id)
        {
            _tr         = tr;
            _keyHandler = config.KeyHandler;
            _id         = id;
            var o = ObjectDB.AllDictionariesPrefix.Length;

            _prefix = new byte[o + PackUnpack.LengthVUInt(_id)];
            Array.Copy(ObjectDB.AllDictionariesPrefix, _prefix, o);
            PackUnpack.PackVUInt(_prefix, ref o, _id);
            _keyReader           = ((Func <AbstractBufferedReader, IReaderCtx, TKey>)config.KeyReader) !;
            _keyWriter           = ((Action <TKey, AbstractBufferedWriter, IWriterCtx>)config.KeyWriter) !;
            _keyValueTr          = _tr.KeyValueDBTransaction;
            _keyValueTrProtector = _tr.TransactionProtector;
            _count = -1;
        }
        public void Load(IILGen ilGenerator, Action <IILGen> pushReaderOrCtx)
        {
            var genericArguments = _type !.GetGenericArguments();
            var instanceType     = typeof(ODBSet <>).MakeGenericType(genericArguments);
            var constructorInfo  = instanceType.GetConstructor(
                new[] { typeof(IInternalObjectDBTransaction), typeof(ODBDictionaryConfiguration), typeof(ulong) });

            ilGenerator
            .Do(pushReaderOrCtx)
            .Castclass(typeof(IDBReaderCtx))
            .Callvirt(() => default(IDBReaderCtx).GetTransaction())
            .LdcI4(_configurationId)
            .Call(() => ODBDictionaryConfiguration.Get(0))
            .Do(Extensions.PushReaderFromCtx(pushReaderOrCtx))
            .Callvirt(() => default(AbstractBufferedReader).ReadVUInt64())
            .Newobj(constructorInfo !)
            .Castclass(_type);
        }
Example #12
0
        public static void DoSave(ref SpanWriter writer, IWriterCtx ctx, IOrderedSet <TKey>?dictionary, int cfgId)
        {
            var writerCtx = (IDBWriterCtx)ctx;

            if (!(dictionary is ODBSet <TKey> goodDict))
            {
                var tr = writerCtx.GetTransaction();
                var id = tr.AllocateDictionaryId();
                goodDict = new ODBSet <TKey>(tr, ODBDictionaryConfiguration.Get(cfgId), id);
                if (dictionary != null)
                {
                    foreach (var pair in dictionary)
                    {
                        goodDict.Add(pair);
                    }
                }
            }

            writer.WriteVUInt64(goodDict._id);
        }
Example #13
0
        public static void DoSave(IWriterCtx ctx, IDictionary <TKey, TValue>?dictionary, int cfgId)
        {
            var writerCtx = (IDBWriterCtx)ctx;

            if (!(dictionary is ODBDictionary <TKey, TValue> goodDict))
            {
                var tr = writerCtx.GetTransaction();
                var id = tr.AllocateDictionaryId();
                goodDict = new ODBDictionary <TKey, TValue>(tr, ODBDictionaryConfiguration.Get(cfgId), id);
                if (dictionary != null)
                {
                    foreach (var pair in dictionary)
                    {
                        goodDict.Add(pair.Key, pair.Value);
                    }
                }
            }

            ctx.Writer().WriteVUInt64(goodDict._id);
        }
Example #14
0
        internal static int Register(IFieldHandler keyHandler, Type keyType, IFieldHandler?valueHandler, Type?valueType)
        {
            while (true)
            {
                var i = 0;
                var currentInstances = _instances;
                for (; i < currentInstances !.Length; i++)
                {
                    if (currentInstances[i].Compare(keyHandler, keyType, valueHandler, valueType))
                    {
                        return(i);
                    }
                }

                i = currentInstances.Length;
                var newInstances = new ODBDictionaryConfiguration[i + 1];
                currentInstances.CopyTo(newInstances.AsSpan());
                newInstances[i] = new ODBDictionaryConfiguration(keyHandler, keyType, valueHandler, valueType);
                if (Interlocked.CompareExchange(ref _instances, newInstances, currentInstances) == currentInstances)
                {
                    return(i);
                }
            }
        }
Example #15
0
 /// Use only for tests of upgrade compatibility in same process, never use in production code
 public static void ResetAllMetadataCaches()
 {
     RelationBuilder.Reset();
     ODBDictionaryConfiguration.Reset();
 }
Example #16
0
 // ReSharper disable once UnusedMember.Global
 public ODBSet(IInternalObjectDBTransaction tr, ODBDictionaryConfiguration config) : this(tr, config,
                                                                                          tr.AllocateDictionaryId())
 {
 }
Example #17
0
        void FreeContent(IReaderCtx readerCtx, int cfgId)
        {
            var config = ODBDictionaryConfiguration.Get(cfgId);
            var ctx    = (DBReaderWithFreeInfoCtx)readerCtx;

            if (config.FreeContent == null)
            {
                var method      = ILBuilder.Instance.NewMethod <FreeContentFun>($"IDictFinder_Cfg_{cfgId}");
                var ilGenerator = method.Generator;

                var readerLoc = ilGenerator.DeclareLocal(typeof(IReaderCtx));
                ilGenerator
                .Ldarg(0)
                .Ldarg(2)
                // ReSharper disable once ObjectCreationAsStatement
                .Newobj(() => new DBReaderWithFreeInfoCtx(null, null))
                .Stloc(readerLoc);

                var readerOrCtx = _valueHandler.NeedsCtx() ? (Action <IILGen>?)(il => il.Ldloc(readerLoc)) : null;
                _valueHandler.FreeContent(ilGenerator, il => il.Ldarg(1), readerOrCtx);
                ilGenerator.Ret();
                config.FreeContent = method.Create();
            }

            var findIDictAction = (FreeContentFun)config.FreeContent;

            long prevProtectionCounter = 0;
            long pos = 0;

            while (true)
            {
                if (pos == 0)
                {
                    if (!_keyValueTr.FindFirstKey(_prefix))
                    {
                        break;
                    }
                }
                else
                {
                    if (_keyValueTr.CursorMovedCounter != prevProtectionCounter)
                    {
                        if (!_keyValueTr.SetKeyIndex(_prefix, pos))
                        {
                            break;
                        }
                    }
                    else
                    {
                        if (!_keyValueTr.FindNextKey(_prefix))
                        {
                            break;
                        }
                    }
                }

                prevProtectionCounter = _keyValueTr.CursorMovedCounter;
                var valueBytes  = _keyValueTr.GetValue();
                var valueReader = new SpanReader(valueBytes);
                findIDictAction(ctx.GetTransaction(), ref valueReader, ctx.DictIds);

                pos++;
            }
        }
Example #18
0
        void FreeContent(IReaderCtx readerCtx, int cfgId)
        {
            var config = ODBDictionaryConfiguration.Get(cfgId);
            var ctx    = (DBReaderWithFreeInfoCtx)readerCtx;

            if (config.FreeContent == null)
            {
                var method = ILBuilder.Instance
                             .NewMethod <Action <IInternalObjectDBTransaction, AbstractBufferedReader, IList <ulong> > >(
                    $"IDictFinder_Cfg_{cfgId}");
                var ilGenerator = method.Generator;

                var readerLoc = ilGenerator.DeclareLocal(typeof(IReaderCtx));
                ilGenerator
                .Ldarg(0)
                .Ldarg(1)
                .Ldarg(2)
                // ReSharper disable once ObjectCreationAsStatement
                .Newobj(() => new DBReaderWithFreeInfoCtx(null, null, null))
                .Stloc(readerLoc);

                Action <IILGen> readerOrCtx;
                if (_valueHandler.NeedsCtx())
                {
                    readerOrCtx = il => il.Ldloc(readerLoc);
                }
                else
                {
                    readerOrCtx = il => il.Ldarg(1);
                }
                _valueHandler.FreeContent(ilGenerator, readerOrCtx);
                ilGenerator.Ret();
                config.FreeContent = method.Create();
            }

            var findIDictAction =
                (Action <IInternalObjectDBTransaction, AbstractBufferedReader, IList <ulong> >)config.FreeContent;

            long prevProtectionCounter = 0;
            long pos = 0;

            while (true)
            {
                _keyValueTrProtector.Start();
                if (pos == 0)
                {
                    _keyValueTr.SetKeyPrefix(_prefix);
                    if (!_keyValueTr.FindFirstKey())
                    {
                        break;
                    }
                }
                else
                {
                    if (_keyValueTrProtector.WasInterupted(prevProtectionCounter))
                    {
                        _keyValueTr.SetKeyPrefix(_prefix);
                        if (!_keyValueTr.SetKeyIndex(pos))
                        {
                            break;
                        }
                    }
                    else
                    {
                        if (!_keyValueTr.FindNextKey())
                        {
                            break;
                        }
                    }
                }

                prevProtectionCounter = _keyValueTrProtector.ProtectionCounter;
                var valueBytes  = _keyValueTr.GetValueAsByteArray();
                var valueReader = new ByteArrayReader(valueBytes);
                findIDictAction(ctx.GetTransaction(), valueReader, ctx.DictIds);

                pos++;
            }
        }