Commit fc336303 authored by Daniel Bluhm's avatar Daniel Bluhm
Browse files

Custom filter methods to handle UUID special case


Signed-off-by: Daniel Bluhm's avatarDaniel Bluhm <bluhmdj@ornl.gov>
parent d561f753
......@@ -87,6 +87,11 @@
<version>5.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.12.2</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package org.eclipse.ice.dev.annotations;
import java.util.UUID;
import org.bson.conversions.Bson;
import com.mongodb.client.model.Filters;
/**
* Customized filtering behavior for MongoDB persistence.
*
* @author Daniel Bluhm
*/
public class PersistenceFilters {
/**
* Create a filter for a UUID.
*
* MongoDB stores UUIDs in binary representation. We store UUIDs as Strings.
* This method solves this special case.
* @param key UUID key to match
* @param val UUID value to be converted to string before filtering
* @return MongoDB Filter
*/
public static Bson eq(String key, UUID val) {
return Filters.eq(key, val.toString());
}
/**
* Treat all non-UUID values as normal.
* @param key to match
* @param val to match
* @return MongoDB Filter
*/
public static Bson eq(String key, Object val) {
return Filters.eq(key, val);
}
}
......@@ -28,6 +28,7 @@ public class DefaultFields {
.name("UUID")
.unique(true)
.getter(true)
.setter(false)
.defaultField(true)
.build()
)
......
......@@ -11,12 +11,12 @@ import java.util.UUID;
import org.bson.Document;
import org.eclipse.ice.dev.annotations.IDataElement;
import org.eclipse.ice.dev.annotations.IPersistenceHandler;
import org.eclipse.ice.dev.annotations.PersistenceFilters;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
public class $class implements $interface {
......@@ -88,7 +88,7 @@ public class $class implements $interface {
#end
public <T extends IDataElement> T findBy${var.NameForMethod}($type ${var.Name}) throws Exception {
Document doc = this.collection
.find(Filters.eq("${docName}", ${var.Name}))
.find(PersistenceFilters.eq("${docName}", ${var.Name}))
.first();
if (doc == null) {
return null;
......@@ -108,7 +108,7 @@ public class $class implements $interface {
@Override
#end
public <T extends IDataElement> Iterable<T> findBy${var.NameForMethod}(${var.Type} ${var.Name}) throws Exception {
return this.collection.find(Filters.eq("${var.Name}", ${var.Name}))
return this.collection.find(PersistenceFilters.eq("${var.Name}", ${var.Name}))
.map(doc -> new ${implementation}().fromJSON(doc));
}
#end
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment