Skip to content

Commit 983bb16

Browse files
authored
Merge pull request #757 from bserdar/doc-metadata
Doc metadata
2 parents 4a34839 + 08de29c commit 983bb16

File tree

12 files changed

+256
-29
lines changed

12 files changed

+256
-29
lines changed

core-api/src/main/java/com/redhat/lightblue/JsonNodeBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public <T> JsonNodeBuilder addJsonObjectsList(String key, List<? extends JsonObj
104104
ArrayNode arr = JsonObject.getFactory().arrayNode();
105105
root.set(key, arr);
106106
for (JsonObject err : values) {
107-
arr.add(err.toJson());
107+
arr.add(err==null?JsonObject.getFactory().nullNode():err.toJson());
108108
}
109109
}
110110
return this;

core-api/src/main/java/com/redhat/lightblue/Response.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,16 @@ public class Response extends JsonObject {
4949
private static final String PROPERTY_DATA_ERRORS = "dataErrors";
5050
private static final String PROPERTY_ERRORS = "errors";
5151
private static final String PROPERTY_HOSTNAME = "hostname";
52+
private static final String PROPERTY_RESULT_METADATA = "resultMetadata";
5253

5354
private EntityVersion entity;
5455
private OperationStatus status;
5556
private long modifiedCount;
5657
private long matchCount;
5758
private String taskHandle;
5859
private SessionInfo session;
59-
private transient JsonNode entityData;
60+
private JsonNode entityData;
61+
private List<ResultMetadata> resultMetadata;
6062
private String hostname;
6163
private final List<DataError> dataErrors = new ArrayList<>();
6264
private final List<Error> errors = new ArrayList<>();
@@ -208,6 +210,18 @@ public void setEntityData(JsonNode node) {
208210
}
209211
}
210212

213+
/**
214+
* Metadata list for documents in entityData. If there are more
215+
* than one documents, the entitydata and metadata indexes match.
216+
*/
217+
public List<ResultMetadata> getResultMetadata() {
218+
return resultMetadata;
219+
}
220+
221+
public void setResultMetadata(List<ResultMetadata> l) {
222+
resultMetadata=l;
223+
}
224+
211225
/**
212226
* Errors related to each document
213227
*/
@@ -241,9 +255,13 @@ public JsonNode toJson() {
241255
builder.add(PROPERTY_HOSTNAME, HOSTNAME);
242256
builder.addJsonObjectsList(PROPERTY_DATA_ERRORS, dataErrors);
243257
builder.addErrorsList(PROPERTY_ERRORS, errors);
258+
if(resultMetadata!=null)
259+
builder.addJsonObjectsList(PROPERTY_RESULT_METADATA,resultMetadata);
244260
return builder.build();
245261
}
246262

263+
// This class is not used
264+
@Deprecated
247265
public static class ResponseBuilder {
248266

249267
private OperationStatus status;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Copyright 2013 Red Hat, Inc. and/or its affiliates.
3+
4+
This file is part of lightblue.
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
package com.redhat.lightblue;
20+
21+
import com.fasterxml.jackson.databind.JsonNode;
22+
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
23+
import com.fasterxml.jackson.databind.node.ObjectNode;
24+
25+
import com.redhat.lightblue.util.JsonObject;
26+
27+
/**
28+
* Contains result metadata returned for each document in the resultset
29+
*/
30+
public class ResultMetadata extends JsonObject {
31+
32+
/**
33+
* Metadata property denoting the field should receive its contents from result metadata.
34+
* The field must be an object or any, and it is not persisted.
35+
*/
36+
public static final String MD_PROPERTY_RESULT_METADATA="resultMetadata";
37+
38+
/**
39+
* Metadata property denoting the field should receive its contents from document version
40+
* The field must be a string, and it is not persisted.
41+
*/
42+
public static final String MD_PROPERTY_DOCVER="documentVersion";
43+
44+
private String documentVersion;
45+
46+
public String getDocumentVersion() {
47+
return documentVersion;
48+
}
49+
50+
public void setDocumentVersion(String s) {
51+
documentVersion=s;
52+
}
53+
54+
@Override
55+
public JsonNode toJson() {
56+
ObjectNode node=JsonNodeFactory.instance.objectNode();
57+
node.set("documentVersion",documentVersion==null?JsonNodeFactory.instance.nullNode():
58+
JsonNodeFactory.instance.textNode(documentVersion));
59+
return node;
60+
}
61+
}
62+

crud/src/main/java/com/redhat/lightblue/crud/CRUDOperationContext.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import com.redhat.lightblue.DataError;
3131
import com.redhat.lightblue.ExecutionOptions;
32+
import com.redhat.lightblue.ResultMetadata;
3233
import com.redhat.lightblue.hooks.HookManager;
3334
import com.redhat.lightblue.util.Error;
3435
import com.redhat.lightblue.util.JsonDoc;
@@ -168,11 +169,21 @@ public void setDocuments(List<DocCtx> docs) {
168169
*
169170
* @return Returns the new document
170171
*/
172+
@Deprecated
171173
public DocCtx addDocument(JsonDoc doc) {
174+
return addDocument(doc,null);
175+
}
176+
177+
/**
178+
* Adds a new document to the context with metadata
179+
*
180+
* @return Returns the new document
181+
*/
182+
public DocCtx addDocument(JsonDoc doc,ResultMetadata rmd) {
172183
if (documents == null) {
173184
documents = new ArrayList<>();
174185
}
175-
DocCtx x = new DocCtx(doc);
186+
DocCtx x = new DocCtx(doc,rmd);
176187
documents.add(x);
177188
return x;
178189
}
@@ -236,6 +247,23 @@ public List<JsonDoc> getOutputDocumentsWithoutErrors() {
236247
}
237248
}
238249

250+
/**
251+
* Returns a list of output document metadata with no errors
252+
*/
253+
public List<ResultMetadata> getOutputDocumentMetadataWithoutErrors() {
254+
if (documents != null) {
255+
List<ResultMetadata> list = new ArrayList<>(documents.size());
256+
for (DocCtx doc : documents) {
257+
if (!doc.hasErrors() && doc.getOutputDocument() != null) {
258+
list.add(doc.getResultMetadata());
259+
}
260+
}
261+
return list;
262+
} else {
263+
return null;
264+
}
265+
}
266+
239267
/**
240268
* Adds an error to the context
241269
*/

crud/src/main/java/com/redhat/lightblue/crud/DocCtx.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.redhat.lightblue.util.JsonDoc;
2929

3030
import com.redhat.lightblue.DataError;
31+
import com.redhat.lightblue.ResultMetadata;
3132

3233
/**
3334
* This class represents a document and its related copies, errors, and the
@@ -44,6 +45,7 @@
4445
* will not appear in the output.</li>
4546
* <li>updatedDoc: This is the copy of the unprojected updated document . This
4647
* has to be explicitly set (expected on the update & save operations). </li>
48+
* <li>resultMetadata: Result specific metadata</li>
4749
* </ul>
4850
*/
4951
public class DocCtx extends JsonDoc {
@@ -54,11 +56,17 @@ public class DocCtx extends JsonDoc {
5456
private JsonDoc updatedDoc = null;
5557
private CRUDOperation CRUDOperationPerformed;
5658
private final Map<String, Object> propertyMap = new HashMap<>();
59+
private ResultMetadata resultMetadata;
5760

5861
public DocCtx(JsonDoc doc) {
5962
super(doc.getRoot());
6063
}
6164

65+
public DocCtx(JsonDoc doc,ResultMetadata rmd) {
66+
super(doc.getRoot());
67+
this.resultMetadata=rmd;
68+
}
69+
6270
/**
6371
* Adds an error to this document
6472
*/
@@ -202,4 +210,11 @@ public void setUpdatedDocument(JsonDoc doc) {
202210
updatedDoc = doc;
203211
}
204212

213+
public ResultMetadata getResultMetadata() {
214+
return resultMetadata;
215+
}
216+
217+
public void setResultMetadata(ResultMetadata d) {
218+
resultMetadata=d;
219+
}
205220
}

crud/src/main/java/com/redhat/lightblue/mediator/Mediator.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.redhat.lightblue.OperationStatus;
3535
import com.redhat.lightblue.Request;
3636
import com.redhat.lightblue.Response;
37+
import com.redhat.lightblue.ResultMetadata;
3738
import com.redhat.lightblue.crud.BulkRequest;
3839
import com.redhat.lightblue.crud.BulkResponse;
3940
import com.redhat.lightblue.crud.CRUDController;
@@ -132,6 +133,7 @@ public Response insert(InsertionRequest req) {
132133
List<JsonDoc> insertedDocuments = ctx.getOutputDocumentsWithoutErrors();
133134
if (insertedDocuments != null && !insertedDocuments.isEmpty()) {
134135
response.setEntityData(JsonDoc.listToDoc(applyRange(req, insertedDocuments), factory.getNodeFactory()));
136+
response.setResultMetadata(ctx.getOutputDocumentMetadataWithoutErrors());
135137
response.setModifiedCount(insertedDocuments.size());
136138
}
137139
if (!ctx.hasErrors() && !ctx.hasDocumentErrors()
@@ -201,7 +203,8 @@ public Response save(SaveRequest req) {
201203
List<JsonDoc> updatedDocuments = ctx.getOutputDocumentsWithoutErrors();
202204
if (updatedDocuments != null && !updatedDocuments.isEmpty()) {
203205
response.setEntityData(JsonDoc.listToDoc(applyRange(req, updatedDocuments), factory.getNodeFactory()));
204-
response.setModifiedCount(updatedDocuments.size());
206+
response.setResultMetadata(ctx.getOutputDocumentMetadataWithoutErrors());
207+
response.setModifiedCount(updatedDocuments.size());
205208
}
206209
if (!ctx.hasErrors() && !ctx.hasDocumentErrors()
207210
&& updatedDocuments != null && updatedDocuments.size() == ctx.getDocuments().size()) {
@@ -286,6 +289,7 @@ public Response update(UpdateRequest req) {
286289
List<JsonDoc> updatedDocuments = ctx.getOutputDocumentsWithoutErrors();
287290
if (updatedDocuments != null && !updatedDocuments.isEmpty()) {
288291
response.setEntityData(JsonDoc.listToDoc(applyRange(req, updatedDocuments), factory.getNodeFactory()));
292+
response.setResultMetadata(ctx.getOutputDocumentMetadataWithoutErrors());
289293
}
290294
if (ctx.hasErrors()) {
291295
ctx.setStatus(OperationStatus.ERROR);
@@ -476,18 +480,20 @@ public Response find(FindRequest req) {
476480
response.setMatchCount(result == null ? 0 : result.getSize());
477481
List<DocCtx> documents = ctx.getDocuments();
478482
if (documents != null) {
483+
List<ResultMetadata> resultMetadata=new ArrayList<>(documents.size());
479484
List<JsonDoc> resultList = new ArrayList<>(documents.size());
480485
for (DocCtx doc : documents) {
481486
resultList.add(doc.getOutputDocument());
487+
resultMetadata.add(doc.getResultMetadata());
482488
}
483489
response.setEntityData(JsonDoc.listToDoc(resultList, factory.getNodeFactory()));
490+
response.setResultMetadata(resultMetadata);
484491
}
485492

486493
factory.getInterceptors().callInterceptors(InterceptPoint.POST_MEDIATOR_FIND, ctx);
487494
}
488495
// call any queued up hooks (regardless of status)
489496
ctx.getHookManager().queueMediatorHooks(ctx);
490-
491497
response.setStatus(ctx.getStatus());
492498
response.getErrors().addAll(ctx.getErrors());
493499
response.getDataErrors().addAll(ctx.getDataErrors());
@@ -591,6 +597,7 @@ protected Callable<Response> getFutureRequest(final Request req) {
591597
return new Callable<Response>() {
592598
@Override
593599
public Response call() {
600+
LOGGER.debug("Starting a future {} request",req.getOperation());
594601
switch (req.getOperation()) {
595602
case FIND:
596603
return find((FindRequest) req);
@@ -643,6 +650,7 @@ public BulkResponse bulkRequest(BulkRequest requests) {
643650
}
644651
}
645652
} else {
653+
LOGGER.debug("Scheduling a future operation");
646654
// unordered - do them all in parallel
647655
ctx.futures[i] = executor.submit(getFutureRequest(req));
648656
}

crud/src/test/java/com/redhat/lightblue/mediator/AbstractMediatorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public void initMediator() throws Exception {
128128
Factory factory = new Factory();
129129
factory.addFieldConstraintValidators(new DefaultFieldConstraintValidators());
130130
factory.addEntityConstraintValidators(new EmptyEntityConstraintValidators());
131-
factory.setBulkParallelExecutions(100);
131+
factory.setBulkParallelExecutions(10);
132132
new UIDInterceptor().register(factory.getInterceptors());
133133
new GeneratedFieldInterceptor().register(factory.getInterceptors());
134134
factory.addCRUDController("mongo", mockCrudController);

0 commit comments

Comments
 (0)