Пример #1
0
        public int RemoveByPrimaryKeyPrefix(ByteBuffer keyBytesPrefix)
        {
            var keysToDelete = new List <ByteBuffer>();
            var enumerator   = new RelationPrimaryKeyEnumerator <T>(_transaction, _relationInfo, keyBytesPrefix, _modificationCounter);

            while (enumerator.MoveNext())
            {
                keysToDelete.Add(enumerator.GetKeyBytes());
            }

            foreach (var key in keysToDelete)
            {
                StartWorkingWithPK();
                if (_transaction.KeyValueDBTransaction.Find(key) != FindResult.Exact)
                {
                    throw new BTDBException("Not found record to delete.");
                }

                var valueBytes = _transaction.KeyValueDBTransaction.GetValue();

                if (_hasSecondaryIndexes)
                {
                    RemoveSecondaryIndexes(key, valueBytes);
                }

                if (_relationInfo.NeedImplementFreeContent())
                {
                    _relationInfo.FreeContent(_transaction, valueBytes);
                }
            }

            return(RemovePrimaryKeysByPrefix(keyBytesPrefix));
        }
Пример #2
0
        void BuildRemoveByMethod(MethodInfo method, IILMethod reqMethod, Type relationDBManipulatorType)
        {
            var writerLoc = reqMethod.Generator.DeclareLocal(typeof(ByteBufferWriter));

            reqMethod.Generator.Newobj(() => new ByteBufferWriter());
            reqMethod.Generator.Stloc(writerLoc);
            Action <IILGen> pushWriter = il => il.Ldloc(writerLoc);

            var isPrefixBased = method.ReturnType == typeof(int); //returns number of removed items

            if (isPrefixBased)
            {
                WriteShortPrefixIl(reqMethod.Generator, pushWriter, _relationInfo.Prefix);
            }
            else
            {
                //ByteBufferWriter.WriteVUInt32(RelationInfo.Id);
                WriteIdIl(reqMethod.Generator, pushWriter, (int)_relationInfo.Id);
            }
            var primaryKeyFields = _relationInfo.ClientRelationVersionInfo.GetPrimaryKeyFields();

            var count = SaveMethodParameters(reqMethod.Generator, method.Name, method.GetParameters(), method.GetParameters().Length,
                                             _relationInfo.ApartFields, primaryKeyFields, writerLoc);

            if (!isPrefixBased && count != primaryKeyFields.Count)
            {
                throw new BTDBException($"Number of parameters in {method.Name} does not match primary key count {primaryKeyFields.Count}.");
            }

            //call manipulator.RemoveBy_
            reqMethod.Generator
            .Ldarg(0);     //manipulator
            //call byteBuffer.data
            var dataGetter = typeof(ByteBufferWriter).GetProperty("Data").GetGetMethod(true);

            reqMethod.Generator.Ldloc(writerLoc).Callvirt(dataGetter);
            if (isPrefixBased)
            {
                if (AllKeyPrefixesAreSame(_relationInfo.ClientRelationVersionInfo, count) && !_relationInfo.NeedImplementFreeContent())
                {
                    reqMethod.Generator.Callvirt(relationDBManipulatorType.GetMethod("RemoveByKeyPrefixWithoutIterate"));
                }
                else
                {
                    reqMethod.Generator.Callvirt(relationDBManipulatorType.GetMethod("RemoveByPrimaryKeyPrefix"));
                }
            }
            else
            {
                reqMethod.Generator.LdcI4(ShouldThrowWhenKeyNotFound(method.Name, method.ReturnType) ? 1 : 0);
                reqMethod.Generator.Callvirt(relationDBManipulatorType.GetMethod("RemoveById"));
                if (method.ReturnType == typeof(void))
                {
                    reqMethod.Generator.Pop();
                }
            }
        }