use of org.teiid.translator.TranslatorException in project teiid by teiid.
the class CoherenceVisitor method visit.
public void visit(Comparison obj) {
// $NON-NLS-1$
LogManager.logTrace(LogConstants.CTX_CONNECTOR, "Parsing Comparison criteria.");
try {
Comparison.Operator op = ((Comparison) obj).getOperator();
Expression lhs = ((Comparison) obj).getLeftExpression();
Expression rhs = ((Comparison) obj).getRightExpression();
String lhsString = getExpressionString(lhs);
String rhsString = getExpressionString(rhs);
if (lhsString == null || rhsString == null) {
// $NON-NLS-1$
final String msg = CoherencePlugin.Util.getString("CoherenceVisitor.missingComparisonExpression");
exception = new TranslatorException(msg);
}
if (rhs instanceof Literal || lhs instanceof Literal) {
if (rhs instanceof Literal) {
Literal literal = (Literal) rhs;
addCompareCriteria(lhsString, literal.getValue(), op, literal.getType());
// filter = CoherenceFilterUtil.createCompareFilter(lhsString, literal.getValue(), op, literal.getType() );
} else {
Literal literal = (Literal) lhs;
addCompareCriteria(rhsString, literal.getValue(), op, literal.getType());
// filter = CoherenceFilterUtil.createCompareFilter(rhsString, literal.getValue(), op, literal.getType() );
}
}
} catch (TranslatorException t) {
exception = t;
}
}
use of org.teiid.translator.TranslatorException in project teiid by teiid.
the class SolrUpdateExecution method performInsert.
private void performInsert(Insert insert) throws TranslatorException {
// build insert
List<ColumnReference> columns = insert.getColumns();
if (insert.getParameterValues() == null) {
final UpdateRequest request = new UpdateRequest();
SolrInputDocument doc = new SolrInputDocument();
List<Expression> values = ((ExpressionValueSource) insert.getValueSource()).getValues();
for (int i = 0; i < columns.size(); i++) {
String columnName = SolrSQLHierarchyVistor.getColumnName(columns.get(i));
Object value = values.get(i);
if (value instanceof Literal) {
doc.addField(columnName, ((Literal) value).getValue());
} else {
throw new TranslatorException(SolrPlugin.Event.TEIID20002, SolrPlugin.Util.gs(SolrPlugin.Event.TEIID20002));
}
}
this.updateCount++;
request.add(doc);
// check if the row already exists
Select q = buildSelectQuery(insert);
SolrQueryExecution query = new SolrQueryExecution(ef, q, this.executionContext, this.metadata, this.connection);
query.execute();
query.walkDocuments(new SolrDocumentCallback() {
@Override
public void walk(SolrDocument doc) {
request.clear();
}
});
if (request.getDocuments().isEmpty()) {
throw new TranslatorException(SolrPlugin.Event.TEIID20007, SolrPlugin.Util.gs(SolrPlugin.Event.TEIID20007));
}
// write the mutation
UpdateResponse response = this.connection.update(request);
if (response.getStatus() != 0) {
throw new TranslatorException(SolrPlugin.Event.TEIID20003, SolrPlugin.Util.gs(SolrPlugin.Event.TEIID20003, response.getStatus()));
}
} else {
UpdateRequest request = new UpdateRequest();
int batchSize = 1024;
// bulk insert; should help
Iterator<? extends List<?>> args = insert.getParameterValues();
while (args.hasNext()) {
List<?> arg = args.next();
SolrInputDocument doc = new SolrInputDocument();
for (int i = 0; i < columns.size(); i++) {
String columnName = SolrSQLHierarchyVistor.getColumnName(columns.get(i));
doc.addField(columnName, arg.get(i));
}
this.updateCount++;
request.add(doc);
if ((this.updateCount % batchSize) == 0) {
UpdateResponse response = this.connection.update(request);
if (response.getStatus() != 0) {
throw new TranslatorException(SolrPlugin.Event.TEIID20003, SolrPlugin.Util.gs(SolrPlugin.Event.TEIID20003, response.getStatus()));
}
request = new UpdateRequest();
}
}
if (request.getDocuments() != null && !request.getDocuments().isEmpty()) {
// write the mutation
UpdateResponse response = this.connection.update(request);
if (response.getStatus() != 0) {
throw new TranslatorException(SolrPlugin.Event.TEIID20003, SolrPlugin.Util.gs(SolrPlugin.Event.TEIID20003, response.getStatus()));
}
}
}
}
use of org.teiid.translator.TranslatorException in project teiid by teiid.
the class SolrUpdateExecution method performUpdate.
private void performUpdate(Delete obj) throws TranslatorException {
Table table = obj.getTable().getMetadataObject();
KeyRecord pk = table.getPrimaryKey();
final String id = getRecordName(pk.getColumns().get(0));
if (obj.getParameterValues() != null) {
throw new TranslatorException(SolrPlugin.Event.TEIID20008, SolrPlugin.Util.gs(SolrPlugin.Event.TEIID20008));
}
SolrQueryExecution query = new SolrQueryExecution(ef, obj, this.executionContext, this.metadata, this.connection);
query.execute();
final UpdateRequest request = new UpdateRequest();
query.walkDocuments(new SolrDocumentCallback() {
@Override
public void walk(SolrDocument doc) {
SolrUpdateExecution.this.updateCount++;
request.deleteById(doc.getFieldValue(id).toString());
}
});
UpdateResponse response = this.connection.update(request);
if (response.getStatus() != 0) {
throw new TranslatorException(SolrPlugin.Event.TEIID20005, SolrPlugin.Util.gs(SolrPlugin.Event.TEIID20005, response.getStatus()));
}
}
use of org.teiid.translator.TranslatorException in project teiid by teiid.
the class SolrUpdateExecution method performUpdate.
/**
* Did not find any other suitable way to pass the query through solrj otherthan walking the documents,
* all the examples were at the passing xml based query. so that would be a good update if the below does
* not performs or gets into OOM
* @param obj
* @throws TranslatorException
*/
private void performUpdate(final Update obj) throws TranslatorException {
if (obj.getParameterValues() != null) {
throw new TranslatorException(SolrPlugin.Event.TEIID20009, SolrPlugin.Util.gs(SolrPlugin.Event.TEIID20009));
}
SolrQueryExecution query = new SolrQueryExecution(ef, obj, this.executionContext, this.metadata, this.connection);
query.execute();
final UpdateRequest request = new UpdateRequest();
query.walkDocuments(new SolrDocumentCallback() {
@Override
public void walk(SolrDocument doc) {
SolrUpdateExecution.this.updateCount++;
Table table = obj.getTable().getMetadataObject();
SolrInputDocument updateDoc = new SolrInputDocument();
for (String name : doc.getFieldNames()) {
if (table.getColumnByName(name) != null) {
updateDoc.setField(name, doc.getFieldValue(name));
}
}
int elementCount = obj.getChanges().size();
for (int i = 0; i < elementCount; i++) {
String columnName = SolrSQLHierarchyVistor.getColumnName(obj.getChanges().get(i).getSymbol());
Literal value = (Literal) obj.getChanges().get(i).getValue();
updateDoc.setField(columnName, value.getValue());
}
request.add(updateDoc);
}
});
if (request.getDocuments() != null && !request.getDocuments().isEmpty()) {
UpdateResponse response = this.connection.update(request);
if (response.getStatus() != 0) {
throw new TranslatorException(SolrPlugin.Event.TEIID20004, SolrPlugin.Util.gs(SolrPlugin.Event.TEIID20004, response.getStatus()));
}
}
}
use of org.teiid.translator.TranslatorException in project teiid by teiid.
the class JsonSerializer method serialize.
@Override
public InputStream serialize(Document doc) throws TranslatorException {
ByteArrayOutputStream outputStream = null;
try {
outputStream = new ByteArrayOutputStream(1024 * 10);
JsonGenerator json = new JsonFactory().createGenerator(outputStream);
writeDocument(doc, null, json, false);
json.close();
return new ByteArrayInputStream(outputStream.toByteArray());
} catch (IOException | SQLException e) {
throw new TranslatorException(e);
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
// ignore.
}
}
}
}
Aggregations