Skip to content
This repository has been archived by the owner on Jun 1, 2021. It is now read-only.

Commit

Permalink
#95: Expose document-version to the API
Browse files Browse the repository at this point in the history
  • Loading branch information
ja-fra committed May 3, 2019
1 parent 9a9f6ef commit bdc57d6
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
10 changes: 10 additions & 0 deletions api/src/main/java/com/rbmhtechnology/vind/api/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
*/
public interface Document {

long VERSION_MUST_EXIST = 1;
long VERSION_DONT_CARE = 0;
long VERSION_MUST_NOT_EXIST = -1;

/**
* Sets a value in a document field. If the field had already a value it is overwritten by the new value.
* @param field Name of the field to set.
Expand Down Expand Up @@ -301,6 +305,12 @@ public interface Document {
*/
String getType();

/**
* Gets the document version. This field is used for optimistic concurrency.
* @return document version
*/
long getVersion();

/**
* Gets the document distance.
* @return a distance for the document.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,33 @@ public Map<String, FieldDescriptor<?>> getFields() {
* @return A {@link Document} with the configured {@link FieldDescriptor}.
*/
public Document createDoc(String id) {
return new DocumentImpl(id, this.type);
return createDoc(id, Document.VERSION_DONT_CARE);
}

/**
* Creates an instance of a {@link Document} within the schema of the actual configuration of the factory.
* If a document with the same {@code id} already exists, indexing this document will fail.
* @param id Identification string of the new document.
* @return A {@link Document} with the configured {@link FieldDescriptor}.
*/
public Document createNewDoc(String id) {
return createDoc(id, Document.VERSION_MUST_NOT_EXIST);
}

/**
* Creates an instance of a {@link Document} within the schema of the actual configuration of the factory with
* the provided document-version.
*
* @param id Identification string of the new document.
* @param docVersion the document version, or one of the pre-defined constants.
* @return A {@link Document} with the configured {@link FieldDescriptor}.
*
* @see Document#VERSION_DONT_CARE
* @see Document#VERSION_MUST_NOT_EXIST
* @see Document#VERSION_MUST_EXIST
*/
public Document createDoc(String id, long docVersion) {
return new DocumentImpl(id, this.type, docVersion);
}

/**
Expand Down Expand Up @@ -128,14 +154,16 @@ class DocumentImpl implements Document {
private final Set<Document> children = new HashSet<>();
private final String id;
private final String type;
private final long version;
private float score;
private float distance;
private Integer childCount;

private DocumentImpl(String id, String type) {
private DocumentImpl(String id, String type, long version) {
//this.values.put(DocumentFactory.ID, id);
this.id = id;
this.type = type;
this.version = version;
}

/**
Expand Down Expand Up @@ -660,6 +688,14 @@ public String getType() {
return type;
}

/**
* {@inheritDoc}
*/
@Override
public long getVersion() {
return version;
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ private SolrInputDocument createInputDocument(Document doc) {

document.addField(ID, doc.getId());
document.addField(TYPE, doc.getType());
document.addField(VERSION, doc.getVersion());

return document;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,7 @@ public static Type getFromClass(Class clazz) {

public static final String ID = "_id_";
public static final String TYPE = "_type_";
public static final String VERSION = "_version_";
public static final String SCORE = "score";
public static final String DISTANCE = "_distance_";
public static final String TEXT = "text";
Expand Down Expand Up @@ -905,7 +906,9 @@ public static List<Document> buildResultList(SolrDocumentList results, Map<Strin

return results.stream().map(result -> {

Document document = factory.createDoc((String) result.getFieldValue(Fieldname.ID));
final Document document = factory.createDoc((
String) result.getFieldValue(Fieldname.ID),
(long) result.getFieldValue(Fieldname.VERSION));

if (childCounts != null) {
document.setChildCount(ObjectUtils.defaultIfNull(childCounts.get(document.getId()), 0));
Expand All @@ -922,6 +925,7 @@ public static List<Document> buildResultList(SolrDocumentList results, Map<Strin
result.getFieldNames().stream()
.filter(name -> !name.equals(Fieldname.ID))
.filter(name -> !name.equals(Fieldname.TYPE))
.filter(name -> !name.equals(Fieldname.VERSION))
.filter(name -> !name.equals(Fieldname.SCORE))
.filter(name -> !name.equals(Fieldname.DISTANCE))
.forEach(name -> {
Expand Down

0 comments on commit bdc57d6

Please sign in to comment.