-
Notifications
You must be signed in to change notification settings - Fork 6
#60 Adding a new API which returns the previous entities before bulk upsert #61
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -127,6 +127,41 @@ public void upsertEntities(Entities request, StreamObserver<Empty> responseObser | |
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void getAndUpsertEntities(Entities request, StreamObserver<Entity> responseObserver) { | ||
| String tenantId = RequestContext.CURRENT.get().getTenantId().orElse(null); | ||
| if (tenantId == null) { | ||
| responseObserver.onError(new ServiceException("Tenant id is missing in the request.")); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| Map<String, Entity> entityMap = | ||
| request.getEntityList().stream() | ||
| .map(entity -> this.upsertNormalizer.normalize(tenantId, entity)) | ||
| .collect(Collectors.toUnmodifiableMap(Entity::getEntityId, Function.identity())); | ||
|
|
||
| Map<Key, Document> documentMap = new HashMap<>(); | ||
| for (Map.Entry<String, Entity> entry : entityMap.entrySet()) { | ||
| Document doc = convertEntityToDocument(entry.getValue()); | ||
| SingleValueKey key = new SingleValueKey(tenantId, entry.getKey()); | ||
| documentMap.put(key, doc); | ||
| } | ||
|
|
||
| Streams.stream(entitiesCollection.bulkUpsertAndReturnOlderDocuments(documentMap)) | ||
| .flatMap(document -> PARSER.<Entity>parseOrLog(document, Entity.newBuilder()).stream()) | ||
| .map(Entity::toBuilder) | ||
| .map(builder -> builder.setTenantId(tenantId)) | ||
| .map(Entity.Builder::build) | ||
| .forEach(responseObserver::onNext); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will send one document at a time, right? Should we send them in batch?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Streaming one at a time will allow the client to process it and discard instead of keeping it in memory. i think that's the pattern we should always follow. |
||
|
|
||
| responseObserver.onCompleted(); | ||
| } catch (IOException e) { | ||
| LOG.error("Failed to bulk upsert entities", e); | ||
| responseObserver.onError(e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get an Entity by the EntityId and EntityType | ||
| * | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As rest of the methods are following that pattern.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is very minor. Will avoid one more commit and full build cycle for this :)