Skip to content

Commit

Permalink
Fix for #31. Additional cleanup to serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
mikesname committed Apr 4, 2014
1 parent 90f17c8 commit 4791fe4
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,10 @@ public void write(OutputStream os) throws IOException {
*/
protected <T extends Frame> StreamingOutput streamingList(
final Iterable<T> list) {
final Serializer serializer = getSerializer();
return MediaType.TEXT_XML_TYPE.equals(checkMediaType())
? getStreamingXmlOutput(list, getSerializer())
: getStreamingJsonOutput(list, getSerializer());
? getStreamingXmlOutput(list, serializer)
: getStreamingJsonOutput(list, serializer);
}

/**
Expand Down
119 changes: 59 additions & 60 deletions ehri-frames/src/main/java/eu/ehri/project/persistence/Serializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ protected boolean removeEldestEntry(final Map.Entry<A, B> eldest) {


public Serializer withCache() {
return new Serializer(new Builder(graph).withCache());
return new Builder(graph)
.withIncludedProperties(includeProps)
.withLiteMode(liteMode)
.dependentOnly(dependentOnly)
.withDepth(maxTraversals)
.withCache().build();
}

/**
Expand All @@ -62,6 +67,16 @@ public Serializer(FramedGraph<?> graph) {
this(new Builder(graph));
}

/**
* Fetch the included properties.
*
* @return A list of property names that will always be serialized.
*/
public List<String> getIncludeProperties() {
return includeProps;
}


/**
* Builder for serializers with non-default options.
*/
Expand All @@ -87,6 +102,11 @@ public Builder dependentOnly() {
return this;
}

public Builder dependentOnly(boolean dependentOnly) {
this.dependentOnly = dependentOnly;
return this;
}

public Builder withLiteMode(boolean lite) {
this.liteMode = lite;
return this;
Expand Down Expand Up @@ -136,6 +156,7 @@ private Serializer(FramedGraph<?> graph, boolean dependentOnly, int depth, boole

/**
* Create a new serializer from this one, with extra included properties.
*
* @param includeProps A set of properties to include.
* @return A new serializer.
*/
Expand All @@ -147,8 +168,8 @@ public Serializer withIncludedProperties(List<String> includeProps) {
/**
* Convert a vertex frame to a raw bundle of data.
*
* @param item
* @return
* @param item The framed item
* @return A map of data
* @throws SerializationError
*/
public <T extends Frame> Map<String, Object> vertexFrameToData(T item)
Expand All @@ -159,8 +180,8 @@ public <T extends Frame> Map<String, Object> vertexFrameToData(T item)
/**
* Convert a vertex to a raw bundle of data.
*
* @param item
* @return
* @param item The item vertex
* @return A map of data
* @throws SerializationError
*/
public Map<String, Object> vertexToData(Vertex item)
Expand All @@ -172,8 +193,8 @@ public Map<String, Object> vertexToData(Vertex item)
* Convert a Frame into an EntityBundle that includes its @Fetch'd
* relations.
*
* @param item
* @return
* @param item The framed item
* @return A data bundle
* @throws SerializationError
*/
public <T extends Frame> Bundle vertexFrameToBundle(T item)
Expand All @@ -185,8 +206,8 @@ public <T extends Frame> Bundle vertexFrameToBundle(T item)
* Convert a Vertex into an EntityBundle that includes its @Fetch'd
* relations.
*
* @param item
* @return
* @param item The item vertex
* @return A data bundle
* @throws SerializationError
*/
public Bundle vertexFrameToBundle(Vertex item)
Expand All @@ -197,8 +218,8 @@ public Bundle vertexFrameToBundle(Vertex item)
/**
* Serialise a vertex frame to JSON.
*
* @param item
* @return
* @param item The framed item
* @return A JSON string
* @throws SerializationError
*/
public <T extends Frame> String vertexFrameToJson(T item)
Expand All @@ -209,8 +230,8 @@ public <T extends Frame> String vertexFrameToJson(T item)
/**
* Serialise a vertex to JSON.
*
* @param item
* @return
* @param item The item vertex
* @return A JSON string
* @throws SerializationError
*/
public String vertexToJson(Vertex item)
Expand All @@ -221,8 +242,8 @@ public String vertexToJson(Vertex item)
/**
* Serialise a vertex frame to XML.
*
* @param item
* @return document
* @param item The item vertex
* @return An XML document
* @throws SerializationError
*/
public Document vertexFrameToXml(Vertex item)
Expand All @@ -233,20 +254,20 @@ public Document vertexFrameToXml(Vertex item)
/**
* Serialise a vertex frame to XML.
*
* @param item
* @return document
* @param item A framed item
* @return An XML document
* @throws SerializationError
*/
public <T extends Frame> Document vertexFrameToXml(T item)
throws SerializationError {
return DataConverter.bundleToXml(vertexFrameToBundle(item));
return vertexFrameToXml(item.asVertex());
}

/**
* Serialise a vertex frame to XML string.
*
* @param item
* @return document string
* @param item The item vertex
* @return An XML document string
* @throws SerializationError
*/
public String vertexToXmlString(Vertex item)
Expand All @@ -257,8 +278,8 @@ public String vertexToXmlString(Vertex item)
/**
* Serialise a vertex frame to XML string.
*
* @param item
* @return document string
* @param item The framed item
* @return An XML document string
* @throws SerializationError
*/
public <T extends Frame> String vertexFrameToXmlString(T item)
Expand All @@ -282,9 +303,9 @@ public <T extends Frame> void traverseSubtree(T item,
* Convert a Frame into an EntityBundle that includes its @Fetch'd
* relations.
*
* @param item
* @param depth
* @return
* @param item The item vertex
* @param depth The maximum serialization depth
* @return A data bundle
* @throws SerializationError
*/
private Bundle vertexToBundle(Vertex item, int depth, boolean lite)
Expand All @@ -297,13 +318,13 @@ private Bundle vertexToBundle(Vertex item, int depth, boolean lite)

Bundle.Builder builder = new Bundle.Builder(type);
builder.setId(id)
.addRelations(getRelationData(item,
depth, lite, type.getEntityClass()))
.addData(getVertexData(item, type, depth, lite))
.addMetaData(getVertexMeta(item, type, depth, lite));
.addRelations(getRelationData(item,
depth, lite, type.getEntityClass()))
.addData(getVertexData(item, type, lite))
.addMetaData(getVertexMeta(item));
if (!lite) {
builder.addMetaData(getVertexMeta(item, type, depth, lite))
.addMetaDataValue("gid", item.getId());
builder.addMetaData(getVertexMeta(item))
.addMetaDataValue("gid", item.getId());
}
return builder.build();
} catch (IllegalArgumentException e) {
Expand Down Expand Up @@ -340,7 +361,7 @@ private ListMultimap<String, Bundle> getRelationData(
Method method = entry.getValue();

boolean isLite = liteMode || lite
|| shouldSerializeLite(relationName, method);
|| shouldSerializeLite(method);

if (shouldTraverse(relationName, method, depth, isLite)) {
logger.trace("Fetching relation: {}, depth {}, {}",
Expand Down Expand Up @@ -375,30 +396,11 @@ private ListMultimap<String, Bundle> getRelationData(
return relations;
}

/**
* Determine if a relation should be serialized without its non-mandatory
* data. This will be the case if:
* <p/>
* - depth is > 0
* - item is a non-dependent relationship
*
* @param relationName
* @param method
* @return
*/
private boolean shouldSerializeLite(String relationName, Method method) {
private boolean shouldSerializeLite(Method method) {
Dependent dep = method.getAnnotation(Dependent.class);
return dep == null;
}

/**
* Determine if traversal should proceed on a Frames relation.
*
* @param relationName
* @param method
* @param depth
* @return
*/
private boolean shouldTraverse(String relationName, Method method, int depth, boolean lite) {
// In order to avoid @Fetching the whole graph we track the
// depth parameter and increase it for every traversal.
Expand Down Expand Up @@ -445,7 +447,7 @@ private boolean shouldTraverse(String relationName, Method method, int depth, bo
/**
* Fetch a map of data from a vertex.
*/
private Map<String, Object> getVertexData(Vertex item, EntityClass type, int depth, boolean lite) {
private Map<String, Object> getVertexData(Vertex item, EntityClass type, boolean lite) {
Map<String, Object> data = Maps.newHashMap();
Iterable<String> keys = lite
? getMandatoryOrSpecificProps(type)
Expand All @@ -463,6 +465,7 @@ private Map<String, Object> getVertexData(Vertex item, EntityClass type, int dep
* Get a list of properties with are either given specifically
* in this serializer's includeProps attr, or are mandatory for
* the type.
*
* @param type An EntityClass
* @return A list of mandatory or included properties.
*/
Expand All @@ -477,7 +480,7 @@ private List<String> getMandatoryOrSpecificProps(EntityClass type) {
* This is anything that begins with an underscore (but now
* two underscores)
*/
private Map<String, Object> getVertexMeta(Vertex item, EntityClass type, int depth, boolean lite) {
private Map<String, Object> getVertexMeta(Vertex item) {
Map<String, Object> data = Maps.newHashMap();
for (String key : item.getPropertyKeys()) {
if (!key.startsWith("__") && key.startsWith("_")) {
Expand All @@ -487,8 +490,8 @@ private Map<String, Object> getVertexMeta(Vertex item, EntityClass type, int dep
return data;
}

private Map<String,Object> getVertexData(Vertex item) {
Map<String,Object> data = Maps.newHashMap();
private Map<String, Object> getVertexData(Vertex item) {
Map<String, Object> data = Maps.newHashMap();
for (String key : item.getPropertyKeys()) {
data.put(key, item.getProperty(key));
}
Expand All @@ -498,10 +501,6 @@ private Map<String,Object> getVertexData(Vertex item) {
/**
* Run a callback every time a node in a subtree is encountered, excepting
* the top-level node.
*
* @param item
* @param depth
* @param cb
*/
private <T extends Frame> void traverseSubtree(T item, int depth,
final TraversalCallback cb) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,17 @@ public void testIncludedProperties() throws Exception {
// Not mandatory properties should be null...
assertNull(BundleUtils.get(serialized, "describes[0]/scopeAndContent"));

Bundle serialized2 = serializer.withIncludedProperties(Lists.newArrayList("scopeAndContent"))
Serializer withProps = serializer.withIncludedProperties(Lists.newArrayList("scopeAndContent"));
Bundle serialized2 = withProps
.vertexFrameToBundle(doc);
assertNotNull(BundleUtils.get(serialized2, "describes[0]/scopeAndContent"));

// Ensure `withCache` preserves includedProperties (#31)
Serializer withPropsAndCache = withProps.withCache();
assertEquals(Lists.newArrayList("scopeAndContent"),
withPropsAndCache.getIncludeProperties());
Bundle serialized3 = withPropsAndCache
.vertexFrameToBundle(doc);
assertNotNull(BundleUtils.get(serialized3, "describes[0]/scopeAndContent"));
}
}

0 comments on commit 4791fe4

Please sign in to comment.