use of org.teiid.infinispan.api.InfinispanDocument in project teiid by teiid.
the class InfinispanUpdateVisitor method visit.
@Override
public void visit(Update obj) {
this.operationType = OperationType.UPDATE;
append(obj.getTable());
if (obj.getWhere() != null) {
buffer.append(Tokens.SPACE).append(SQLConstants.Reserved.WHERE).append(Tokens.SPACE);
append(obj.getWhere());
// Can't use the original where string because it is designed for the document model querying
this.whereClause = obj.getWhere();
}
// table that update issued for
Table table = obj.getTable().getMetadataObject();
if (!table.equals(getParentTable())) {
this.nested = true;
}
// read the properties
try {
InfinispanDocument targetDocument = buildTargetDocument(table, false);
int elementCount = obj.getChanges().size();
for (int i = 0; i < elementCount; i++) {
Column column = obj.getChanges().get(i).getSymbol().getMetadataObject();
if (isPartOfPrimaryKey(column.getName())) {
throw new TranslatorException(InfinispanPlugin.Event.TEIID25019, InfinispanPlugin.Util.gs(InfinispanPlugin.Event.TEIID25019, column.getName()));
}
Expression expr = obj.getChanges().get(i).getValue();
Object value = resolveExpressionValue(expr);
// this.updatePayload.put(getName(column), value);
updateDocument(targetDocument, column, value);
}
this.insertPayload = targetDocument;
} catch (TranslatorException e) {
this.exceptions.add(e);
}
}
use of org.teiid.infinispan.api.InfinispanDocument in project teiid by teiid.
the class TestTeiidTableMarsheller method testWriteComplex.
@Test
public void testWriteComplex() throws Exception {
IckleConversionVisitor visitor = helpExecute("select * from G2");
TeiidTableMarsheller writeMarshaller = new TeiidTableMarsheller(ProtobufMetadataProcessor.getMessageName(visitor.getParentTable()), MarshallerBuilder.getWireMap(visitor.getParentTable(), visitor.getMetadata()));
SerializationContext ctx = ProtobufUtil.newSerializationContext(Configuration.builder().build());
ctx.registerProtoFiles(FileDescriptorSource.fromString("tables.proto", ObjectConverterUtil.convertFileToString(UnitTestUtil.getTestDataFile("tables.proto"))));
ctx.registerMarshaller(new G3Marshaller());
ctx.registerMarshaller(new G4Marshaller());
G2Marshaller readMarshaller = new G2Marshaller() {
@Override
public void writeTo(ProtoStreamWriter writer, G2 g2) throws IOException {
throw new RuntimeException("Use Teiid marshaller for writing for this test..");
}
};
InfinispanDocument g2 = buildG2(visitor);
ctx.registerMarshaller(writeMarshaller);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
RawProtoStreamWriter out = RawProtoStreamWriterImpl.newInstance(baos);
WrappedMessage.writeMessage(ctx, out, g2);
out.flush();
baos.flush();
ctx.unregisterMarshaller(writeMarshaller);
// this is used for writing, if reading is being attempted then fail
ctx.registerMarshaller(readMarshaller);
RawProtoStreamReader in = RawProtoStreamReaderImpl.newInstance(baos.toByteArray());
G2 result = WrappedMessage.readMessage(ctx, in);
ctx.unregisterMarshaller(readMarshaller);
assertEquals(buildG2(), result);
}
use of org.teiid.infinispan.api.InfinispanDocument in project teiid by teiid.
the class InfinispanUpdateExecution method performInsert.
@SuppressWarnings("unchecked")
private void performInsert(final InfinispanUpdateVisitor visitor, Table table, final RemoteCache<Object, Object> cache, boolean upsert, TeiidTableMarsheller marshaller) throws TranslatorException {
Insert insert = (Insert) this.command;
if (visitor.isNestedOperation()) {
InfinispanDocument previous = null;
if (visitor.getIdentity() != null) {
previous = (InfinispanDocument) cache.get(visitor.getIdentity());
}
if (insert.getParameterValues() != null) {
throw new TranslatorException(InfinispanPlugin.Util.gs(InfinispanPlugin.Event.TEIID25017, table.getName(), visitor.getParentTable().getName()));
}
if (previous == null) {
throw new TranslatorException(InfinispanPlugin.Util.gs(InfinispanPlugin.Event.TEIID25009, table.getName(), visitor.getIdentity()));
}
String childName = ProtobufMetadataProcessor.getMessageName(visitor.getQueryTable());
previous.addChildDocument(childName, visitor.getInsertPayload().getChildDocuments(childName).get(0));
if (upsert) {
previous = (InfinispanDocument) cache.replace(visitor.getIdentity(), previous);
} else {
previous = (InfinispanDocument) cache.put(visitor.getIdentity(), previous);
}
this.updateCount++;
} else {
if (insert.getParameterValues() == null) {
insertRow(cache, visitor.getIdentity(), visitor.getInsertPayload(), upsert);
this.updateCount++;
} else {
boolean putAll = false;
if (this.executionContext.getSourceHint() != null) {
putAll = this.executionContext.getSourceHint().indexOf("use-putall") != -1;
}
// bulk insert
int batchSize = this.executionContext.getBatchSize();
Iterator<? extends List<Expression>> args = (Iterator<? extends List<Expression>>) insert.getParameterValues();
while (true) {
Map<Object, InfinispanDocument> rows = visitor.getBulkInsertPayload(insert, batchSize, args);
if (rows.isEmpty()) {
break;
}
if (putAll) {
BulkInsert bi = new UsePutAll(cache);
bi.run(rows, false);
} else {
BulkInsert bi = new OneAtATime(cache);
bi.run(rows, upsert);
}
this.updateCount += rows.size();
}
}
}
}
Aggregations