-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
finish metadata support and JsonSerializer use metadata
- Loading branch information
1 parent
647a01e
commit 45d8acb
Showing
16 changed files
with
297 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...c/main/java/com/timeyang/jkes/core/elasticsearch/exception/IllegalJkesStateException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.timeyang.jkes.core.elasticsearch.exception; | ||
|
||
/** | ||
* @author chaokunyang | ||
*/ | ||
public class IllegalJkesStateException extends IllegalStateException { | ||
|
||
public IllegalJkesStateException() { | ||
} | ||
|
||
public IllegalJkesStateException(String s) { | ||
super(s); | ||
} | ||
|
||
public IllegalJkesStateException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
18 changes: 0 additions & 18 deletions
18
...ain/java/com/timeyang/jkes/core/elasticsearch/exception/IlllegalSearchStateException.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
.../main/java/com/timeyang/jkes/core/elasticsearch/serialize/MetaableJkesJsonSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package com.timeyang.jkes.core.elasticsearch.serialize; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.core.JsonStreamContext; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import com.fasterxml.jackson.databind.ser.std.StdSerializer; | ||
import com.timeyang.jkes.core.elasticsearch.exception.IllegalJkesStateException; | ||
import com.timeyang.jkes.core.exception.IllegalMemberAccessException; | ||
import com.timeyang.jkes.core.exception.ReflectiveInvocationTargetException; | ||
import com.timeyang.jkes.core.metadata.DocumentMetadata; | ||
import com.timeyang.jkes.core.metadata.FieldMetadata; | ||
import com.timeyang.jkes.core.metadata.Metadata; | ||
import com.timeyang.jkes.core.metadata.MultiFieldsMetadata; | ||
import com.timeyang.jkes.core.util.ReflectionUtils; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.util.Set; | ||
|
||
/** | ||
* Entity capable of search, json serializer | ||
* | ||
* @author chaokunyang | ||
*/ | ||
public class MetaableJkesJsonSerializer<T> extends StdSerializer<T> { | ||
|
||
public MetaableJkesJsonSerializer() { | ||
this(null); | ||
} | ||
|
||
private MetaableJkesJsonSerializer(Class<T> t) { | ||
super(t); | ||
} | ||
|
||
@Override | ||
public Class<T> handledType() { | ||
return _handledType; | ||
} | ||
|
||
@Override | ||
public void serialize(T value, JsonGenerator gen, SerializerProvider provider) throws IOException { | ||
|
||
JsonStreamContext context = gen.getOutputContext(); | ||
gen.setCurrentValue(value); // must set manually, or else the result of context.getCurrentValue() will be null。this method will call context.setCurrentValue(value); | ||
|
||
gen.writeStartObject(); // writeStartObject(value) will call setCurrentValue(value) well | ||
Class<?> entityClass = value.getClass(); | ||
|
||
if(Metadata.getMetadata() == null) | ||
throw new IllegalJkesStateException("Metadata doesn't be inited correctly, the init method doesn't get invoked"); | ||
DocumentMetadata documentMetadata = Metadata.getMetadata().getMetadataMap().get(entityClass); | ||
Set<FieldMetadata> fieldMetadataSet = documentMetadata.getFieldMetadataSet(); | ||
Set<MultiFieldsMetadata> multiFieldsMetadataSet = documentMetadata.getMultiFieldsMetadataSet(); | ||
|
||
for(FieldMetadata fieldMetadata : fieldMetadataSet) { | ||
Method method = fieldMetadata.getMethod(); | ||
// check whether serialization will be recursive | ||
if (willRecursive(context, method)) { | ||
gen.writeEndObject(); | ||
return; | ||
} | ||
|
||
serializeField(value, gen, method, fieldMetadata.getFieldName()); | ||
} | ||
|
||
for(MultiFieldsMetadata multiFieldsMetadata : multiFieldsMetadataSet) { | ||
Method method = multiFieldsMetadata.getMethod(); | ||
// check whether serialization will be recursive | ||
if (willRecursive(context, method)) { | ||
gen.writeEndObject(); | ||
return; | ||
} | ||
|
||
serializeField(value, gen, method, multiFieldsMetadata.getFieldName()); | ||
} | ||
|
||
// gen.writeObjectField("extra_field", "whatever_value"); | ||
gen.writeEndObject(); | ||
} | ||
|
||
private void serializeField(T value, JsonGenerator gen, Method method, String fieldName) throws IOException { | ||
try { | ||
Object v = method.invoke(value); | ||
|
||
gen.writeObjectField(fieldName, v); // serialize data | ||
} catch (IllegalAccessException e) { | ||
throw new IllegalMemberAccessException(e); | ||
} catch (InvocationTargetException e) { | ||
throw new ReflectiveInvocationTargetException(e); | ||
} | ||
} | ||
|
||
private boolean willRecursive(JsonStreamContext ctxt, Method method) throws IOException { | ||
if (ctxt != null) { | ||
JsonStreamContext ptxt; | ||
ptxt = ctxt.getParent(); | ||
while(ptxt != null) { | ||
if(ptxt.getCurrentValue() == null | ||
|| !ReflectionUtils.getInnermostType(method).equals(ptxt.getCurrentValue().getClass().getCanonicalName())) { | ||
ptxt = ptxt.getParent(); | ||
}else { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.