Search in sources :

Example 96 with TranslatorException

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;
    }
}
Also used : Comparison(org.teiid.language.Comparison) Expression(org.teiid.language.Expression) Literal(org.teiid.language.Literal) TranslatorException(org.teiid.translator.TranslatorException) Operator(org.teiid.language.Comparison.Operator)

Example 97 with TranslatorException

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()));
            }
        }
    }
}
Also used : UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) UpdateResponse(org.apache.solr.client.solrj.response.UpdateResponse) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrDocument(org.apache.solr.common.SolrDocument) SolrDocumentCallback(org.teiid.translator.solr.SolrQueryExecution.SolrDocumentCallback) TranslatorException(org.teiid.translator.TranslatorException)

Example 98 with TranslatorException

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()));
    }
}
Also used : KeyRecord(org.teiid.metadata.KeyRecord) UpdateResponse(org.apache.solr.client.solrj.response.UpdateResponse) Table(org.teiid.metadata.Table) SolrDocument(org.apache.solr.common.SolrDocument) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) SolrDocumentCallback(org.teiid.translator.solr.SolrQueryExecution.SolrDocumentCallback) TranslatorException(org.teiid.translator.TranslatorException)

Example 99 with TranslatorException

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()));
        }
    }
}
Also used : UpdateResponse(org.apache.solr.client.solrj.response.UpdateResponse) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrDocument(org.apache.solr.common.SolrDocument) Table(org.teiid.metadata.Table) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) SolrDocumentCallback(org.teiid.translator.solr.SolrQueryExecution.SolrDocumentCallback) TranslatorException(org.teiid.translator.TranslatorException)

Example 100 with TranslatorException

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.
            }
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) SQLException(java.sql.SQLException) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) TranslatorException(org.teiid.translator.TranslatorException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

TranslatorException (org.teiid.translator.TranslatorException)227 ArrayList (java.util.ArrayList)51 Column (org.teiid.metadata.Column)47 List (java.util.List)32 Table (org.teiid.metadata.Table)30 IOException (java.io.IOException)26 SQLException (java.sql.SQLException)26 ResourceException (javax.resource.ResourceException)26 Test (org.junit.Test)16 Expression (org.teiid.language.Expression)16 Literal (org.teiid.language.Literal)16 DataNotAvailableException (org.teiid.translator.DataNotAvailableException)16 Blob (java.sql.Blob)15 Argument (org.teiid.language.Argument)13 DBObject (com.mongodb.DBObject)11 HashMap (java.util.HashMap)11 ColumnReference (org.teiid.language.ColumnReference)11 ExecutionContext (org.teiid.translator.ExecutionContext)11 BasicDBObject (com.mongodb.BasicDBObject)10 RuntimeMetadata (org.teiid.metadata.RuntimeMetadata)10