Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add batch patch endpoint to web project #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Hexastore.Web/Controllers/StoreController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
Expand Down Expand Up @@ -169,6 +171,27 @@ public async Task<IActionResult> PatchJson(string storeId, [FromBody]JToken data
}
}

[HttpPatch("{storeId}/batch/json")]
public async Task<IActionResult> BatchPatchJson(string storeId, [FromBody]IEnumerable<JToken> dataPatches)
{
_logger.LogInformation(LoggingEvents.ControllerPatchJson, "BATCH PATCH JSON: store {storeId}", storeId);
try
{
var payloads = dataPatches.Select(patch => new StoreEvent
{
Operation = EventType.PATCH_JSON,
Data = patch.ToString(Formatting.None)
});

await SendEvents(storeId, payloads);
return Accepted();
}
catch (Exception e)
{
return HandleException(e);
}
}

[HttpPatch("{storeId}/triple")]
public async Task<IActionResult> PatchTriple(string storeId, [FromBody]JObject data)
{
Expand Down
17 changes: 17 additions & 0 deletions Hexastore.Web/Controllers/StoreControllerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,23 @@ protected Task SendEvent(string storeId, StoreEvent payload)
return tc.Task;
}

protected Task SendEvents(string storeId, IEnumerable<StoreEvent> payloads)
{
var tc = new TaskCompletionSource<bool>();
var storeEvents = payloads.ToList();
foreach (var payload in storeEvents)
{
var guid = Guid.NewGuid().ToString();
payload.OperationId = guid;
payload.StoreId = storeId;
_receiver.SetCompletion(guid, tc);
}

_ = _eventProcessor.SendMessages(storeEvents);
return tc.Task;
}


protected IActionResult HandleException(Exception e)
{
_logger.LogError(LoggingEvents.ControllerError, e, e.Message);
Expand Down
33 changes: 33 additions & 0 deletions Hexastore.Web/EventHubs/EventSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,39 @@ public async Task SendMessage(StoreEvent storeEvent)
}
}

public async Task SendMessages(IEnumerable<StoreEvent> storeEvents)
{
if (!_active)
{
// pass through
foreach(var storeEvent in storeEvents)
{
await _storeReceiver.ProcessEventsAsync(storeEvent);
}
return;
}

try
{
var partitionKey = string.Empty;
var eventDatas = new List<EventData>();
foreach (var storeEvent in storeEvents)
{
storeEvent.PartitionId = storeEvent.StoreId.GetHashCode() % _storeConfig.EventHubPartitionCount;
if(partitionKey == string.Empty) partitionKey = storeEvent.PartitionId.ToString();
var content = JsonConvert.SerializeObject(storeEvent, Formatting.None);
var bytes = Encoding.UTF8.GetBytes(content);
eventDatas.Add(new EventData(bytes));
}

await _eventHubClient.SendAsync(eventDatas, partitionKey);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}

public void Dispose()
{
if (_eventHubClient != null) {
Expand Down