mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-19 23:02:24 +00:00
#304 - ElasticSearch integration part 1 / doc store integration
This commit is contained in:
1 parent
cb1fe297d6
commit
8d3b4a0407
209 files changed
+6736
-573
No files matched your search
@@ -0,0 +1,51 @@
|
||||
package com.avaje.ebeaninternal.server.text.json;
|
||||
|
||||
import com.avaje.ebean.PersistenceIOException;
|
||||
import com.avaje.ebean.bean.PersistenceContext;
|
||||
import com.avaje.ebean.text.json.JsonBeanReader;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* A 'context' for reading entity beans from JSON.
|
||||
* <p>
|
||||
* This is used such that a load context and persistence context can be used to span multiple marshalling requests.
|
||||
* </p>
|
||||
*/
|
||||
public class DJsonBeanReader<T> implements JsonBeanReader<T> {
|
||||
|
||||
private final BeanDescriptor<T> desc;
|
||||
|
||||
private final ReadJson readJson;
|
||||
|
||||
public DJsonBeanReader(BeanDescriptor<T> desc, ReadJson readJson) {
|
||||
this.desc = desc;
|
||||
this.readJson = readJson;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void persistenceContextPut(Object beanId, T currentBean) {
|
||||
readJson.persistenceContextPut(beanId, currentBean);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PersistenceContext getPersistenceContext() {
|
||||
return readJson.getPersistenceContext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T read() {
|
||||
try {
|
||||
return desc.jsonRead(readJson, null);
|
||||
} catch (IOException e) {
|
||||
throw new PersistenceIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonBeanReader<T> forJson(JsonParser moreJson) {
|
||||
return new DJsonBeanReader(desc, readJson.forJson(moreJson));
|
||||
}
|
||||
}
|
||||
@@ -106,15 +106,23 @@ public class DJsonContext implements JsonContext {
|
||||
|
||||
public <T> T toBean(Class<T> cls, JsonParser parser, JsonReadOptions options) throws JsonIOException {
|
||||
|
||||
ReadJson readJson = new ReadJson(parser, options, determineObjectMapper(options));
|
||||
BeanDescriptor<T> desc = getDescriptor(cls);
|
||||
ReadJson readJson = new ReadJson(desc, parser, options, determineObjectMapper(options));
|
||||
try {
|
||||
BeanDescriptor<T> d = getDescriptor(cls);
|
||||
return d.jsonRead(readJson, null);
|
||||
return desc.jsonRead(readJson, null);
|
||||
} catch (IOException e) {
|
||||
throw new JsonIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> DJsonBeanReader createBeanReader(Class<T> cls, JsonParser parser, JsonReadOptions options) throws JsonIOException {
|
||||
|
||||
BeanDescriptor<T> desc = getDescriptor(cls);
|
||||
ReadJson readJson = new ReadJson(desc, parser, options, determineObjectMapper(options));
|
||||
return new DJsonBeanReader<T>(desc, readJson);
|
||||
}
|
||||
|
||||
public <T> List<T> toList(Class<T> cls, String json) throws JsonIOException {
|
||||
return toList(cls, new StringReader(json));
|
||||
}
|
||||
@@ -138,9 +146,9 @@ public class DJsonContext implements JsonContext {
|
||||
|
||||
public <T> List<T> toList(Class<T> cls, JsonParser src, JsonReadOptions options) throws JsonIOException {
|
||||
|
||||
ReadJson readJson = new ReadJson(src, options, determineObjectMapper(options));
|
||||
BeanDescriptor<T> desc = getDescriptor(cls);
|
||||
ReadJson readJson = new ReadJson(desc, src, options, determineObjectMapper(options));
|
||||
try {
|
||||
BeanDescriptor<T> d = getDescriptor(cls);
|
||||
|
||||
List<T> list = new ArrayList<T>();
|
||||
|
||||
@@ -153,7 +161,7 @@ public class DJsonContext implements JsonContext {
|
||||
}
|
||||
|
||||
do {
|
||||
T bean = d.jsonRead(readJson, null);
|
||||
T bean = desc.jsonRead(readJson, null);
|
||||
if (bean == null) {
|
||||
break;
|
||||
} else {
|
||||
|
||||
@@ -1,7 +1,14 @@
|
||||
package com.avaje.ebeaninternal.server.text.json;
|
||||
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
import com.avaje.ebean.bean.EntityBeanIntercept;
|
||||
import com.avaje.ebean.bean.PersistenceContext;
|
||||
import com.avaje.ebean.text.json.JsonReadBeanVisitor;
|
||||
import com.avaje.ebean.text.json.JsonReadOptions;
|
||||
import com.avaje.ebeaninternal.api.LoadContext;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import com.avaje.ebeaninternal.server.loadcontext.DLoadContext;
|
||||
import com.avaje.ebeaninternal.server.transaction.DefaultPersistenceContext;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonToken;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
@@ -14,34 +21,120 @@ import java.util.Map;
|
||||
*/
|
||||
public class ReadJson {
|
||||
|
||||
private final BeanDescriptor<?> rootDesc;
|
||||
|
||||
/**
|
||||
* Jackson parser.
|
||||
*/
|
||||
final JsonParser parser;
|
||||
private final JsonParser parser;
|
||||
|
||||
/**
|
||||
* Stack of the path - used to find the appropriate JsonReadBeanVisitor.
|
||||
*/
|
||||
final PathStack pathStack;
|
||||
private final PathStack pathStack;
|
||||
|
||||
/**
|
||||
* Map of the JsonReadBeanVisitor keyed by path.
|
||||
*/
|
||||
final Map<String, JsonReadBeanVisitor<?>> visitorMap;
|
||||
private final Map<String, JsonReadBeanVisitor<?>> visitorMap;
|
||||
|
||||
final Object objectMapper;
|
||||
private final Object objectMapper;
|
||||
|
||||
private final PersistenceContext persistenceContext;
|
||||
|
||||
private final LoadContext loadContext;
|
||||
|
||||
/**
|
||||
* Construct with parser and readOptions.
|
||||
*/
|
||||
public ReadJson(JsonParser parser, JsonReadOptions readOptions, Object objectMapper) {
|
||||
public ReadJson(BeanDescriptor<?> desc, JsonParser parser, JsonReadOptions readOptions, Object objectMapper) {
|
||||
|
||||
this.rootDesc = desc;
|
||||
this.parser = parser;
|
||||
this.objectMapper = objectMapper;
|
||||
this.persistenceContext = initPersistenceContext(readOptions);
|
||||
this.loadContext = initLoadContext(desc, readOptions);
|
||||
|
||||
// only create visitorMap, pathStack if needed ...
|
||||
this.visitorMap = (readOptions == null) ? null : readOptions.getVisitorMap();
|
||||
this.pathStack = (visitorMap == null) ? null : new PathStack();
|
||||
this.pathStack = (visitorMap == null && loadContext == null) ? null : new PathStack();
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct when transferring load context, persistence context, object mapper etc to a new ReadJson instance.
|
||||
*/
|
||||
private ReadJson(JsonParser moreJson, ReadJson source) {
|
||||
this.parser = moreJson;
|
||||
this.rootDesc = source.rootDesc;
|
||||
this.pathStack = source.pathStack;
|
||||
this.visitorMap = source.visitorMap;
|
||||
this.objectMapper = source.objectMapper;
|
||||
this.persistenceContext = source.persistenceContext;
|
||||
this.loadContext = source.loadContext;
|
||||
}
|
||||
|
||||
private LoadContext initLoadContext(BeanDescriptor<?> desc, JsonReadOptions readOptions) {
|
||||
if (readOptions != null && readOptions.isEnableLazyLoading()) {
|
||||
return new DLoadContext(desc, persistenceContext);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private PersistenceContext initPersistenceContext(JsonReadOptions readOptions) {
|
||||
if (readOptions != null && readOptions.getPersistenceContext() != null) {
|
||||
return readOptions.getPersistenceContext();
|
||||
}
|
||||
return new DefaultPersistenceContext();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the persistence context being used if any.
|
||||
*/
|
||||
public PersistenceContext getPersistenceContext() {
|
||||
return persistenceContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a new instance of ReadJson using the existing context but with a new JsonParser.
|
||||
*/
|
||||
public ReadJson forJson(JsonParser moreJson) {
|
||||
return new ReadJson(moreJson, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the bean to the persistence context.
|
||||
*/
|
||||
public <T> void persistenceContextPut(Object beanId, T currentBean) {
|
||||
|
||||
persistenceContextPutIfAbsent(beanId, (EntityBean)currentBean, rootDesc);
|
||||
}
|
||||
|
||||
/**
|
||||
* Put the bean into the persistence context. If there is already a matching bean in the
|
||||
* persistence context then return that instance else return null.
|
||||
*/
|
||||
public Object persistenceContextPutIfAbsent(Object id, EntityBean bean, BeanDescriptor<?> beanDesc) {
|
||||
|
||||
if (persistenceContext == null) {
|
||||
// no persistenceContext means no lazy loading either
|
||||
return null;
|
||||
}
|
||||
|
||||
Object contextBean = persistenceContext.putIfAbsent(id, bean);
|
||||
if (contextBean == null) {
|
||||
if (loadContext != null) {
|
||||
EntityBeanIntercept ebi = bean._ebean_getIntercept();
|
||||
if (ebi.isPartial()) {
|
||||
// register for further lazy loading
|
||||
String path = pathStack.peekWithNull();
|
||||
loadContext.register(path, ebi);
|
||||
beanDesc.lazyLoadRegister(path, ebi, bean, loadContext);
|
||||
}
|
||||
ebi.setLoaded();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
return contextBean;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -110,4 +203,5 @@ public class ReadJson {
|
||||
public Object readValueUsingObjectMapper(Class<?> propertyType) throws IOException {
|
||||
return getObjectMapper().readValue(parser, propertyType);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user