protected bool TryApply(MutationPacket mutationPacket, out Mutation mutation, out MutationResult result) { FragmentKey key = fragmentKeyFactory.FromBytes(mutationPacket.GetKey()); mutation = mutationFactory.FromBytes(mutationPacket.GetPayload()); result = mutation.Mutate(state, key, mutationPacket.GetRequester()); if (result.IsFailure()) { return(false); } if (result.IsSuccess()) { bool isUpdate = state.ContainsKey(key); state = state.CloneWith(key, result.GetFragment()); if (isUpdate) { SafeInvoker.Invoke(log, OnFragmentUpdatedCallback, result.GetFragment(), state, key, mutation); } else { SafeInvoker.Invoke(log, OnFragmentInsertedCallback, result.GetFragment(), state, key, mutation); } } else if (result.IsDelete()) { if (state.ContainsKey(key)) { state = state.CloneWithout(key); SafeInvoker.Invoke(log, OnFragmentDeletedCallback, state, key, mutation); } } SafeInvoker.Invoke(log, OnStateChangedCallback, state, key, mutation); return(true); }
public Guid Request(FragmentKey key, Mutation mutation) { if (!isAttached) { throw log.ExitError(new Exception("Cannot Request the mutation of a fragment if the Karmax container is no longer attached to the Server.")); } Guid mutationId = Guid.NewGuid(); byte[] keyBytes = fragmentKeyFactory.GetBytes(key); byte[] payload = mutationFactory.GetBytes(mutation); MutationPacket mutationPacket = new MutationPacket(mutationId, containerId, keyBytes, payload); Request(mutationPacket, mutation); return(mutationId); }
private void HandleMutationBy(MutationPacket mutationPacket, Guid requester) { if (!TryApply(mutationPacket, out Mutation mutation, out MutationResult result)) { if (requester.Equals(Guid.Empty)) { log.Warning($"Mutation[{mutationPacket.GetId()}] failed. Reason: {result.GetFailureReason()}. Details: {mutation.GetType().Name} on fragment[{fragmentKeyFactory.FromBytes(mutationPacket.GetKey()).AsString()}].\nStackTrace: {Environment.StackTrace}"); SafeInvoker.Invoke(log, OnMutationFailedCallback, mutationPacket.GetId(), result.GetFailureReason()); } else { server.Send(requester, new MutationFailedPacket(mutationPacket.GetId(), result.GetFailureReason())); } return; } server.Broadcast(mutationPacket); }
protected abstract void Request(MutationPacket mutationPacket, Mutation mutation);
protected override void Request(MutationPacket mutationPacket, Mutation mutation) { HandleMutationBy(mutationPacket, Guid.Empty); }
protected override void Request(MutationPacket mutationPacket, Mutation mutation) { client.Send(mutationPacket); pending.Add(mutationPacket.GetId(), $"{mutation.GetType().Name} on fragment[{fragmentKeyFactory.FromBytes(mutationPacket.GetKey()).AsString()}].\nStackTrace: {Environment.StackTrace}"); }