public void RemoveOp()
        {
            var t = new Table();

            t.Add(new Record(1, 2, 3));
            t.Add(new Record(4, 5, 6));
            t.Add(new Record(7, 8, 9));

            var reader = new TestReader(1);
            var op     = new RemoveOperation(t, reader);

            op.Execute();
            Assert.AreEqual(2, t.Count);
            Assert.AreEqual(new Record(4, 5, 6), t[1]);
            Assert.AreEqual(new Record(7, 8, 9), t[2]);

            reader = new TestReader(2);
            op     = new RemoveOperation(t, reader);
            op.Execute();
            Assert.AreEqual(1, t.Count);
            Assert.AreEqual(new Record(4, 5, 6), t[1]);

            reader = new TestReader(1);
            op     = new RemoveOperation(t, reader);
            op.Execute();
            Assert.AreEqual(0, t.Count);
        }
Example #2
0
        DeleteResult <JSONDocument> IDocumentStore.DeleteDocument(RemoveOperation operation)
        {
            UsageStats stats = new UsageStats();

            stats.BeginSample();

            DeleteResult <JSONDocument> result = new DeleteResult <JSONDocument>();
            CacheItem cItem;

            if (!_cache.ContainsKey(operation.RowId))
            {
                //TODO: Read-through
                if (!_persistenceManager.MetadataIndex(_parent.Name).ContainsRowId(operation.RowId))
                {
                    if (LoggerManager.Instance.StorageLogger != null && LoggerManager.Instance.StorageLogger.IsWarnEnabled)
                    {
                        LoggerManager.Instance.StorageLogger.Warn("Delete : document not found.", "Document not found while deleting in metadataIndex. rowId = " + operation.RowId + " collection = " + Name);
                    }
                    AddFailedOperation(operation);
                    return(result);
                }
                cItem = LoadDocument(operation.RowId, operation.Context);
            }
            else
            {
                cItem = CacheGet(operation.RowId);
            }

            //Remove an item from eviction index, so that it's not marked for eviction.
            if (cItem != null)
            {
                _evictionPolicy.Remove(operation.RowId, cItem.EvictionHint);
            }

            result.Document = cItem.Document;
            result.RowId    = operation.RowId;
            //_cache.Remove(operation.RowId);
            //CacheRemove(operation.RowId);

            _indexManager.UpdateIndex(operation.RowId, result.Document, new JSONDocument(), operation.OperationId);

            cItem.Flag.SetBit(BitsetConstants.DocumentDirty);
            cItem.Flag.SetBit(BitsetConstants.MarkedForDeletion);

            PersistenceOperation persistenceOperation = new PersistenceDeleteOperation(operation.OperationId, _parent.Name, operation.RowId, cItem, operation.Context);

            AddToPersistenceDictionary(persistenceOperation);
            _persistenceManager.MetadataIndex(_parent.Name).Remove(new DocumentKey(cItem.Document.Key));

            if (_statsCollector != null)
            {
                stats.EndSample();
                _statsCollector.IncrementStatsValue(StatisticsType.AvgDeleteTime, stats.Current);
                _statsCollector.IncrementStatsValue(StatisticsType.DeletesPerSec);
            }

            return(result);
        }
        /// <summary>
        /// The set value.
        /// </summary>
        /// <param name="tx">
        /// The tx.
        /// </param>
        /// <param name="key">
        /// The key.
        /// </param>
        /// <param name="value">
        /// The value.
        /// </param>
        public void SetValue(ITransaction tx, TKey key, TValue value)
        {
            var transaction = tx.GetTransaction();

            var id = Interlocked.Increment(ref this.operationNumber);

            Operation undo, redo;
            var       dbTransaction = this.tables.CreateTransaction();

            try
            {
                TValue initialValue;
                if (dbTransaction.Table.TryGetValue(key, out initialValue))
                {
                    undo = new SetOperation {
                        Key = key, Value = initialValue, Id = id
                    };
                }
                else
                {
                    undo = new RemoveOperation {
                        Key = key, Id = id
                    };
                }
            }
            catch
            {
                dbTransaction.Rollback();
                dbTransaction.Dispose();
                throw;
            }

            if (value == null)
            {
                redo = new RemoveOperation {
                    Key = key, Id = id
                };
            }
            else
            {
                redo = new SetOperation {
                    Key = key, Value = value, Id = id
                };
            }

            this.PerformOperation <object>(
                id,
                new OperationContext {
                DatabaseTransaction = dbTransaction, ReplicatorTransaction = transaction
            },
                undo,
                redo);
        }
Example #4
0
        /// <summary>
        /// Removes the specified identity.
        /// </summary>
        /// <param name="identity">The identity.</param>
        public void Remove(Identity identity)
        {
            var op = new RemoveOperation(_domains, identity, _logger);
            var tx = Transaction.Current;

            if (tx == null)
            {
                op.OnCommit();
            }
            else
            {
                tx.EnlistVolatile(op, EnlistmentOptions.None);
            }
        }
        /// <summary>
        /// Removes selected shapes
        /// </summary>
        public static void RemoveShapes()
        {
            var sf = App.SelectedShapefile;

            if (sf == null || !sf.InteractiveEditing || sf.NumSelected == 0)
            {
                return;
            }
            if (MessageHelper.Ask("Remove selected shapes: " + sf.NumSelected + "?") == DialogResult.Yes)
            {
                int layerHandle = App.Legend.SelectedLayer;
                RemoveOperation.Remove(sf, layerHandle);
                App.Map.Redraw();
            }
        }
        public void executeOperationTest()
        {
            TCPTransport trans = new TCPTransport(System.Net.IPAddress.Loopback, 11222);
            Codec codec = new Codec();
            Serializer s = new DefaultSerializer();

            byte[] key = UTF8Encoding.UTF8.GetBytes("key10");

            RemoveOperation target = new RemoveOperation(codec, key, null, 0, null);
            Transport transport = trans;
            byte[] expected = null;
            byte[] actual;
            actual = target.executeOperation(transport);
            Assert.AreEqual(expected, actual);
        }
Example #7
0
        public void executeOperationTest()
        {
            TCPTransport trans = new TCPTransport(System.Net.IPAddress.Loopback, 11222);
            Codec        codec = new Codec();
            Serializer   s     = new DefaultSerializer();

            byte[] key = UTF8Encoding.UTF8.GetBytes("key10");

            RemoveOperation target    = new RemoveOperation(codec, key, null, 0, null);
            Transport       transport = trans;

            byte[] expected = null;
            byte[] actual;
            actual = target.executeOperation(transport);
            Assert.AreEqual(expected, actual);
        }
        public ConditionalResult <TValue> TryRemove(ITransaction tx, TKey key, long version = -1)
        {
            var transaction = tx.GetTransaction();

            var       id = Interlocked.Increment(ref this.operationNumber);
            Operation undo;
            var       dbTransaction = this.tables.CreateTransaction();
            ConditionalResult <TValue> result;

            try
            {
                TValue initialValue;
                if (dbTransaction.Table.TryGetValue(key, out initialValue))
                {
                    undo = new SetOperation {
                        Key = key, Value = initialValue, Id = id
                    };
                    result = new ConditionalResult <TValue>(true, initialValue);
                }
                else
                {
                    undo   = NopOperation.Instance;
                    result = new ConditionalResult <TValue>();
                }
            }
            catch
            {
                dbTransaction.Rollback();
                dbTransaction.Dispose();
                throw;
            }

            var redo = new RemoveOperation {
                Key = key, Id = id
            };

            this.PerformOperation <object>(
                id,
                new OperationContext {
                DatabaseTransaction = dbTransaction, ReplicatorTransaction = transaction
            },
                undo,
                redo);

            return(result);
        }
Example #9
0
        /// <summary>
        /// Removes a record with given key
        /// </summary>
        /// <typeparam name="K">Key Data Type</typeparam>
        /// <param name="key">Key</param>
        /// <returns>Removed value</returns>
        public V remove <V>(Object key)
        {
            RemoveOperation removeOperation = operationsFactory.newRemoveOperation(serializer.serialize(key));

            transport = transportFactory.getTransport();
            byte[] existingValue;
            try
            {
                existingValue = (byte[])removeOperation.executeOperation(transport);
            }
            finally
            {
                transportFactory.releaseTransport(transport);
            }

            return((V)serializer.deserialize(existingValue));
        }
Example #10
0
        /// <summary>
        /// Processes a remove operation.
        /// </summary>
        /// <param name="dataOperation">The document that represents the operation.</param>
        /// <param name="message">The original message.</param>
        private void ProcessRemoveOperation(Document dataOperation, Message message)
        {
            RemoveOperation removeOperation = new RemoveOperation(dataOperation["remove"].ValueAsDocument);

            if (!removeOperation.Valid)
            {
                SendMessage(new Message(message, removeOperation.ErrorMessage, false));
                return;
            }

            _chunkListLock.EnterReadLock();
            var chunk = _chunkList.SingleOrDefault(e => ChunkMarker.IsBetween(e.Start, e.End, removeOperation.DocumentId.ToString()));

            _chunkListLock.ExitReadLock();

            NodeDefinition node = chunk == null ? null : chunk.Node;

            if (node == null)
            {
                SendMessage(new Message(message, new DataOperationResult(ErrorCodes.FailedMessage, "No storage node up for the specified id range."), false));
                return;
            }

            Message operationMessage = new Message(node, message.Data, true);

            operationMessage.SetResponseCallback(delegate(Message originalOperationMessage)
            {
                if (originalOperationMessage.Success)
                {
                    DataOperationResult result = (DataOperationResult)originalOperationMessage.Response.Data;
                    Document resultDocument    = new Document(result.Result);
                    if (!resultDocument["success"].ValueAsBoolean && (ErrorCodes)Enum.Parse(typeof(ErrorCodes), resultDocument["errorcode"].ValueAsString) == ErrorCodes.ChunkMoved)
                    {
                        ProcessRemoveOperation(dataOperation, message);
                        return;
                    }

                    SendMessage(new Message(message, result, false));
                    return;
                }

                SendMessage(new Message(message, new DataOperationResult(ErrorCodes.FailedMessage, "Failed to send message to storage node."), false));
            });
            SendMessage(operationMessage);
        }
Example #11
0
            internal virtual IList <UpdateOperation> GenerateUpdatesForNextIteration()
            {
                IList <UpdateOperation> updateOperations = new List <UpdateOperation>();

                if (ToAdd.Count == 0 && ToRemove.Count == 0)
                {
                    EndSignalConflict.set(true);
                    return(updateOperations);
                }

                int operationsInIteration = ReadersShouldSee.Count < 1000 ? 100 : ReadersShouldSee.Count / 10;
                int count = 0;

                while (count < operationsInIteration && (ToAdd.Count > 0 || ToRemove.Count > 0))
                {
                    UpdateOperation operation;
                    if (ToAdd.Count == 0)
                    {
                        operation = new RemoveOperation(_outerInstance, ToRemove.RemoveFirst());
                    }
                    else if (ToRemove.Count == 0)
                    {
                        operation = new PutOperation(_outerInstance, ToAdd.RemoveFirst());
                    }
                    else
                    {
                        bool remove = Random.NextDouble() > WritePercentage;
                        if (remove)
                        {
                            operation = new RemoveOperation(_outerInstance, ToRemove.RemoveFirst());
                        }
                        else
                        {
                            operation = new PutOperation(_outerInstance, ToAdd.RemoveFirst());
                        }
                    }
                    updateOperations.Add(operation);
                    count++;
                }
                return(updateOperations);
            }
Example #12
0
        /// <summary>
        /// Processes an remove operation.
        /// </summary>
        /// <param name="doc">The document representing the operation.</param>
        /// <returns>The result of the operation.</returns>
        private DataOperationResult ProcessRemoveOperation(Document doc)
        {
            RemoveOperation op = new RemoveOperation(doc);

            if (!op.Valid)
            {
                return(op.ErrorMessage);
            }

            _lock.EnterReadLock();

            Document removed;
            bool     success = GetChunk(op.DocumentId).TryRemove(op.DocumentId, out removed);

            _lock.ExitReadLock();

            if (success)
            {
                _checkForMerge = true;
            }

            return(new DataOperationResult(removed));
        }
Example #13
0
 protected abstract void Remove(RemoveOperation operation);
 protected abstract void Remove(RemoveOperation operation, TDoc target);
Example #15
0
        private void ApplyPatch(int patchIndex, AssetLocation patchSourcefile, JsonPatch jsonPatch, ref int applied, ref int notFound, ref int errorCount)
        {
            EnumAppSide targetSide = jsonPatch.Side == null ? jsonPatch.File.Category.SideType : (EnumAppSide)jsonPatch.Side;

            if (targetSide != EnumAppSide.Universal && jsonPatch.Side != api.Side)
            {
                return;
            }

            var loc = jsonPatch.File.Clone();

            if (jsonPatch.File.Path.EndsWith("*"))
            {
                List <IAsset> assets = api.Assets.GetMany(jsonPatch.File.Path.TrimEnd('*'), jsonPatch.File.Domain, false);
                foreach (var val in assets)
                {
                    jsonPatch.File = val.Location;
                    ApplyPatch(patchIndex, patchSourcefile, jsonPatch, ref applied, ref notFound, ref errorCount);
                }

                jsonPatch.File = loc;

                return;
            }



            if (!loc.Path.EndsWith(".json"))
            {
                loc.Path += ".json";
            }

            var asset = api.Assets.TryGet(loc);

            if (asset == null)
            {
                if (jsonPatch.File.Category == null)
                {
                    api.World.Logger.VerboseDebug("Patch {0} in {1}: File {2} not found. Wrong asset category", patchIndex, patchSourcefile, loc);
                }
                else
                {
                    EnumAppSide catSide = jsonPatch.File.Category.SideType;
                    if (catSide != EnumAppSide.Universal && api.Side != catSide)
                    {
                        api.World.Logger.VerboseDebug("Patch {0} in {1}: File {2} not found. Hint: This asset is usually only loaded {3} side", patchIndex, patchSourcefile, loc, catSide);
                    }
                    else
                    {
                        api.World.Logger.VerboseDebug("Patch {0} in {1}: File {2} not found", patchIndex, patchSourcefile, loc);
                    }
                }


                notFound++;
                return;
            }

            Operation op = null;

            switch (jsonPatch.Op)
            {
            case EnumJsonPatchOp.Add:
                if (jsonPatch.Value == null)
                {
                    api.World.Logger.Error("Patch {0} in {1} failed probably because it is an add operation and the value property is not set or misspelled", patchIndex, patchSourcefile);
                    errorCount++;
                    return;
                }
                op = new AddOperation()
                {
                    Path = new Tavis.JsonPointer(jsonPatch.Path), Value = jsonPatch.Value.Token
                };
                break;

            case EnumJsonPatchOp.Remove:
                op = new RemoveOperation()
                {
                    Path = new Tavis.JsonPointer(jsonPatch.Path)
                };
                break;

            case EnumJsonPatchOp.Replace:
                if (jsonPatch.Value == null)
                {
                    api.World.Logger.Error("Patch {0} in {1} failed probably because it is a replace operation and the value property is not set or misspelled", patchIndex, patchSourcefile);
                    errorCount++;
                    return;
                }

                op = new ReplaceOperation()
                {
                    Path = new Tavis.JsonPointer(jsonPatch.Path), Value = jsonPatch.Value.Token
                };
                break;

            case EnumJsonPatchOp.Copy:
                op = new CopyOperation()
                {
                    Path = new Tavis.JsonPointer(jsonPatch.Path), FromPath = new JsonPointer(jsonPatch.FromPath)
                };
                break;

            case EnumJsonPatchOp.Move:
                op = new MoveOperation()
                {
                    Path = new Tavis.JsonPointer(jsonPatch.Path), FromPath = new JsonPointer(jsonPatch.FromPath)
                };
                break;
            }

            PatchDocument patchdoc = new PatchDocument(op);
            JToken        token    = null;

            try
            {
                token = JToken.Parse(asset.ToText());
            }
            catch (Exception e)
            {
                api.World.Logger.Error("Patch {0} (target: {3}) in {1} failed probably because the syntax of the value is broken: {2}", patchIndex, patchSourcefile, e, loc);
                errorCount++;
                return;
            }

            try
            {
                patchdoc.ApplyTo(token);
            }
            catch (Tavis.PathNotFoundException p)
            {
                api.World.Logger.Error("Patch {0} (target: {4}) in {1} failed because supplied path {2} is invalid: {3}", patchIndex, patchSourcefile, jsonPatch.Path, p.Message, loc);
                errorCount++;
                return;
            }
            catch (Exception e)
            {
                api.World.Logger.Error("Patch {0} (target: {3}) in {1} failed, following Exception was thrown: {2}", patchIndex, patchSourcefile, e.Message, loc);
                errorCount++;
                return;
            }

            string text = token.ToString();

            asset.Data = System.Text.Encoding.UTF8.GetBytes(text);

            applied++;
        }
Example #16
0
        private void ApplyPatch(int patchIndex, AssetLocation patchSourcefile, JsonPatch jsonPatch, ref int applied, ref int notFound, ref int errorCount)
        {
            if (jsonPatch.SideType != EnumAppSide.Universal && jsonPatch.SideType != api.Side)
            {
                return;
            }

            var path = jsonPatch.File.Path;

            if (!path.EndsWith(".json"))
            {
                path += ".json";
            }

            var asset = api.Assets.TryGet(path);

            if (asset == null)
            {
                api.World.Logger.VerboseDebug("Patch {0} in {1}: File {2} not found", patchIndex, patchSourcefile, path);
                notFound++;
                return;
            }

            Operation op = null;

            switch (jsonPatch.Op)
            {
            case EnumJsonPatchOp.Add:
                if (jsonPatch.Value == null)
                {
                    api.World.Logger.Error("Patch {0} in {1} failed probably because it is an add operation and the value property is not set or misspelled", patchIndex, patchSourcefile);
                    errorCount++;
                    return;
                }
                op = new AddOperation()
                {
                    Path = new Tavis.JsonPointer(jsonPatch.Path), Value = jsonPatch.Value.Token
                };
                break;

            case EnumJsonPatchOp.Remove:
                op = new RemoveOperation()
                {
                    Path = new Tavis.JsonPointer(jsonPatch.Path)
                };
                break;

            case EnumJsonPatchOp.Replace:
                if (jsonPatch.Value == null)
                {
                    api.World.Logger.Error("Patch {0} in {1} failed probably because it is a replace operation and the value property is not set or misspelled", patchIndex, patchSourcefile);
                    errorCount++;
                    return;
                }

                op = new ReplaceOperation()
                {
                    Path = new Tavis.JsonPointer(jsonPatch.Path), Value = jsonPatch.Value.Token
                };
                break;

            case EnumJsonPatchOp.Copy:
                op = new CopyOperation()
                {
                    Path = new Tavis.JsonPointer(jsonPatch.Path), FromPath = new JsonPointer(jsonPatch.FromPath)
                };
                break;

            case EnumJsonPatchOp.Move:
                op = new MoveOperation()
                {
                    Path = new Tavis.JsonPointer(jsonPatch.Path), FromPath = new JsonPointer(jsonPatch.FromPath)
                };
                break;
            }

            PatchDocument patchdoc = new PatchDocument(op);
            JToken        token    = null;

            try
            {
                token = JToken.Parse(asset.ToText());
            }
            catch (Exception e)
            {
                api.World.Logger.Error("Patch {0} in {1} failed probably because the syntax of the value is broken: {2}", patchIndex, patchSourcefile, e);
                errorCount++;
                return;
            }

            try
            {
                patchdoc.ApplyTo(token);
            }
            catch (Tavis.PathNotFoundException p)
            {
                api.World.Logger.Error("Patch {0} in {1} failed because supplied path {2} is invalid: {3}", patchIndex, patchSourcefile, jsonPatch.Path, p.Message);
                errorCount++;
                return;
            }
            catch (Exception e)
            {
                api.World.Logger.Error("Patch {0} in {1} failed, following Exception was thrown: {2}", patchIndex, patchSourcefile, e.Message);
                errorCount++;
                return;
            }

            string text = token.ToString();

            asset.Data = System.Text.Encoding.UTF8.GetBytes(text);

            applied++;
        }