use of com.bluenimble.platform.indexer.IndexerException in project serverless by bluenimble.
the class IndexerApiRequestTrack method finish.
@Override
public void finish(JsonObject feedback) {
final Date now = new Date();
Json.getObject(track, Fields.Time.class.getSimpleName().toLowerCase()).set(Fields.Time.Finished, Lang.toString(now, DateFormat));
int code = Json.getInteger(feedback, ApiResponse.Error.Code, ApiResponse.OK.getCode());
track.set(Fields.Status, code);
if (feedback.containsKey(Fields.FeedBack.Message)) {
feedback.set(Fields.FeedBack.Message, feedback.get(Fields.FeedBack.Message).toString());
}
track.set(Fields.Feedback, feedback);
JsonObject metrics = Json.getObject(track, Fields.Metrics.class.getSimpleName().toLowerCase());
if (MX.isCurrentThreadCpuTimeSupported()) {
metrics.set(Fields.Metrics.Cpu, MX.getCurrentThreadUserTime());
}
if (MX.isCurrentThreadCpuTimeSupported()) {
metrics.set(Fields.Metrics.Memory, MX.getThreadAllocatedBytes(Thread.currentThread().getId()));
}
try {
tracker.executor().execute(new Runnable() {
@Override
public void run() {
api.tracer().log(Tracer.Level.Info, "Executor Started Track Thread ...");
// shrink
track.shrink();
JsonObject oTracking = api.getTracking();
// put
Indexer indexer = null;
try {
indexer = api.space().feature(Indexer.class, Json.getString(oTracking, Spec.Feature, ApiSpace.Features.Default), tracker.context());
if (indexer == null) {
api.tracer().log(Tracer.Level.Error, "CantStore[{0}] due to indexer declared in the api is not defined at the space level", track.toString());
return;
}
String entity = Json.getString(oTracking, Spec.Entity, DefaultEntity);
if (!indexer.exists(entity)) {
api.tracer().log(Tracer.Level.Info, "Entity not found in Index, Create Mapping...");
indexer.create(entity, requestMapping());
}
indexer.put(entity, track);
} catch (IndexerException e) {
api.tracer().log(Tracer.Level.Error, "CantStore[{0}] due to {1}", track.toString(), e.getMessage());
api.tracer().log(Tracer.Level.Error, Lang.BLANK, e);
}
}
});
} catch (Exception ex) {
api.tracer().log(Tracer.Level.Error, Lang.BLANK, ex);
}
}
use of com.bluenimble.platform.indexer.IndexerException in project serverless by bluenimble.
the class ElasticSearchIndexer method search.
@Override
public JsonObject search(JsonObject query, String[] entities) throws IndexerException {
String types = Lang.BLANK;
if (entities == null || entities.length == 0) {
types = Lang.join(entities, Lang.COMMA);
}
tracer.log(Tracer.Level.Info, "search documents in [{0}] with query ", types.equals(Lang.BLANK) ? "All Entities" : Lang.join(entities), query);
JsonObject result = new JsonObject();
Error error = new Error();
remote.get((JsonObject) new JsonObject().set(Remote.Spec.Endpoint, url + types + (types.equals(Lang.BLANK) ? Lang.BLANK : Lang.SLASH) + Internal.Elk.Search).set(Remote.Spec.Headers, new JsonObject().set(HttpHeaders.AUTHORIZATION, authToken).set(HttpHeaders.CONTENT_TYPE, ContentTypes.Json)).set(Remote.Spec.Data, new JsonObject().set(Internal.Elk.Source, Lang.encode(query.toString()))).set(Remote.Spec.Serializer, Serializer.Name.json), new Remote.Callback() {
@Override
public void onSuccess(int code, Object data) {
if (data != null) {
result.putAll((JsonObject) data);
}
}
@Override
public void onError(int code, Object message) {
error.set(code, message);
}
});
if (error.happened()) {
throw new IndexerException("Error occured while calling Indexer: Code=" + error.code + ", Message: " + error.message);
}
return result;
}
use of com.bluenimble.platform.indexer.IndexerException in project serverless by bluenimble.
the class ElasticSearchIndexer method get.
@Override
public JsonObject get(String entity, String id) throws IndexerException {
if (Lang.isNullOrEmpty(id)) {
throw new IndexerException("Document Id cannot be null.");
}
tracer.log(Tracer.Level.Info, "Lookup document [{0}]", id);
JsonObject result = new JsonObject();
Error error = new Error();
remote.get((JsonObject) new JsonObject().set(Remote.Spec.Endpoint, url + entity + Lang.SLASH + id).set(Remote.Spec.Headers, new JsonObject().set(HttpHeaders.AUTHORIZATION, authToken)).set(Remote.Spec.Serializer, Serializer.Name.json), new Remote.Callback() {
@Override
public void onSuccess(int code, Object data) {
if (data != null) {
result.putAll((JsonObject) data);
}
}
@Override
public void onError(int code, Object message) {
error.set(code, message);
}
});
if (error.happened()) {
throw new IndexerException("Error occured while calling Indexer: Code=" + error.code + ", Message: " + error.message);
}
return result;
}
use of com.bluenimble.platform.indexer.IndexerException in project serverless by bluenimble.
the class ElasticSearchIndexer method update.
@Override
public JsonObject update(String entity, JsonObject doc, boolean partial) throws IndexerException {
if (doc == null || doc.isEmpty()) {
throw new IndexerException("Document cannot be null nor empty.");
}
String id = Json.getString(doc, Internal.Id);
if (Lang.isNullOrEmpty(id)) {
throw new IndexerException("Document Id cannot be null.");
}
if (!doc.containsKey(Internal.Timestamp)) {
doc.set(Internal.Timestamp, Lang.utc());
}
if (!partial) {
tracer.log(Tracer.Level.Info, "It's not partial update. Create document [{0}]", id);
return create(entity, doc);
}
tracer.log(Tracer.Level.Info, "Update partially document [{0}]", id);
doc.remove(Internal.Id);
JsonObject result = new JsonObject();
Error error = new Error();
remote.post((JsonObject) new JsonObject().set(Remote.Spec.Endpoint, url + entity + Lang.SLASH + id + Lang.SLASH + Internal.Elk.Update).set(Remote.Spec.Headers, new JsonObject().set(HttpHeaders.AUTHORIZATION, authToken).set(HttpHeaders.CONTENT_TYPE, ContentTypes.Json)).set(Remote.Spec.Data, new JsonObject().set(Internal.Elk.Doc, doc)).set(Remote.Spec.Serializer, Serializer.Name.json), new Remote.Callback() {
@Override
public void onSuccess(int code, Object data) {
if (data != null) {
result.putAll((JsonObject) data);
}
}
@Override
public void onError(int code, Object message) {
error.set(code, message);
}
});
if (error.happened()) {
throw new IndexerException("Error occured while calling Indexer: Code=" + error.code + ", Message: " + error.message);
}
return result;
}
use of com.bluenimble.platform.indexer.IndexerException in project serverless by bluenimble.
the class ElasticSearchIndexer method put.
@Override
public JsonObject put(String entity, JsonObject doc) throws IndexerException {
if (Lang.isNullOrEmpty(entity)) {
throw new IndexerException("Entity cannot be null nor empty.");
}
if (doc == null || doc.isEmpty()) {
throw new IndexerException("Document cannot be null nor empty.");
}
String id = Json.getString(doc, Internal.Id);
if (Lang.isNullOrEmpty(id)) {
doc.set(Internal.Id, Lang.rand());
}
if (!doc.containsKey(Internal.Timestamp)) {
doc.set(Internal.Timestamp, Lang.utc());
}
tracer.log(Tracer.Level.Info, "Indexing document [{0}]", id);
JsonObject result = new JsonObject();
Error error = new Error();
remote.put((JsonObject) new JsonObject().set(Remote.Spec.Endpoint, url + entity + Lang.SLASH + id).set(Remote.Spec.Headers, new JsonObject().set(HttpHeaders.AUTHORIZATION, authToken).set(HttpHeaders.CONTENT_TYPE, ContentTypes.Json)).set(Remote.Spec.Data, doc).set(Remote.Spec.Serializer, Serializer.Name.json), new Remote.Callback() {
@Override
public void onSuccess(int code, Object data) {
if (data != null) {
result.putAll((JsonObject) data);
}
}
@Override
public void onError(int code, Object message) {
error.set(code, message);
}
});
if (error.happened()) {
throw new IndexerException("Error occured while calling Indexer: Code=" + error.code + ", Message: " + error.message);
}
return result;
}
Aggregations