Search in sources :

Example 1 with ProtobufResource

use of org.teiid.infinispan.api.ProtobufResource in project teiid by teiid.

the class SchemaToProtobufProcessor method process.

public ProtobufResource process(MetadataFactory metadataFactory, InfinispanConnection connection) throws TranslatorException {
    String defaultCacheName = "default";
    if (connection != null) {
        defaultCacheName = connection.getCache().getName();
    }
    this.schema = metadataFactory.getSchema();
    buffer.append("package ").append(schema.getName()).append(";");
    buffer.append(NL);
    buffer.append(NL);
    for (Table table : schema.getTables().values()) {
        visit(table, defaultCacheName);
        buffer.append(NL);
        buffer.append(NL);
    }
    for (String name : this.processLater.keySet()) {
        visitTable(name, this.processLater.get(name));
    }
    return new ProtobufResource(schema.getName() + ".proto", buffer.toString());
}
Also used : Table(org.teiid.metadata.Table) ProtobufResource(org.teiid.infinispan.api.ProtobufResource)

Example 2 with ProtobufResource

use of org.teiid.infinispan.api.ProtobufResource in project teiid by teiid.

the class TestHotrodExecution method setup.

@BeforeClass
public static void setup() throws Exception {
    TimestampWithTimezone.resetCalendar(TimeZone.getTimeZone("GMT-5"));
    SERVER = new HotRodTestServer(PORT);
    MetadataFactory mf = TestProtobufMetadataProcessor.protoMatadata("tables.proto");
    EF = new InfinispanExecutionFactory();
    TransformationMetadata tm = TestProtobufMetadataProcessor.getTransformationMetadata(mf, EF);
    // String ddl = DDLStringVisitor.getDDLString(mf.getSchema(), null, null);
    // System.out.println(ddl);
    METADATA = new RuntimeMetadataImpl(tm);
    UTILITY = new TranslationUtility(tm);
    InfinispanConnection connection = SERVER.getConnection();
    // only use G2 & G4 as cache only support single id
    connection.registerProtobufFile(new ProtobufResource("tables.proto", ObjectConverterUtil.convertFileToString(UnitTestUtil.getTestDataFile("tables.proto"))));
    EC = Mockito.mock(ExecutionContext.class);
    Mockito.stub(EC.getBatchSize()).toReturn(512);
}
Also used : TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) ExecutionContext(org.teiid.translator.ExecutionContext) MetadataFactory(org.teiid.metadata.MetadataFactory) RuntimeMetadataImpl(org.teiid.dqp.internal.datamgr.RuntimeMetadataImpl) HotRodTestServer(org.teiid.infinispan.api.HotRodTestServer) TranslationUtility(org.teiid.cdk.api.TranslationUtility) InfinispanConnection(org.teiid.infinispan.api.InfinispanConnection) ProtobufResource(org.teiid.infinispan.api.ProtobufResource) BeforeClass(org.junit.BeforeClass)

Example 3 with ProtobufResource

use of org.teiid.infinispan.api.ProtobufResource in project teiid by teiid.

the class TestSchemaToProtobufProcessor method testConverstion.

@SuppressWarnings("rawtypes")
@Test
public void testConverstion() throws Exception {
    SchemaToProtobufProcessor tool = new SchemaToProtobufProcessor();
    // tool.setIndexMessages(true);
    MetadataFactory mf = TestProtobufMetadataProcessor.protoMatadata("tables.proto");
    InfinispanConnection conn = Mockito.mock(InfinispanConnection.class);
    BasicCache cache = Mockito.mock(BasicCache.class);
    Mockito.stub(cache.getName()).toReturn("default");
    Mockito.stub(conn.getCache()).toReturn(cache);
    ProtobufResource resource = tool.process(mf, conn);
    String expected = "package model;\n" + "\n" + "/* @Indexed @Cache(name=foo) */\n" + "message G1 {\n" + "    /* @Id @IndexedField(index=true, store=false) */\n" + "    required int32 e1 = 1;\n" + "    /* @IndexedField */\n" + "    required string e2 = 2;\n" + "    optional float e3 = 3;\n" + "    /* @IndexedField(index=true, store=false) */\n" + "    repeated string e4 = 4;\n" + "    repeated string e5 = 5;\n" + "}\n" + "\n" + "/* @Indexed */\n" + "message G2 {\n" + "    /* @Id */\n" + "    required int32 e1 = 1;\n" + "    required string e2 = 2;\n" + "    optional G3 g3 = 5;\n" + "    /* @IndexedField(index=false) */\n" + "    optional bytes e5 = 7;\n" + "    /* @Teiid(type=long) */\n" + "    optional fixed64 e6 = 8;\n" + "    repeated G4 g4 = 6;\n" + "}\n" + "\n" + "/* @Indexed */\n" + "message G4 {\n" + "    required int32 e1 = 1;\n" + "    required string e2 = 2;\n" + "    optional int32 e1 = 3;\n" + "}\n" + "\n" + "/* @Indexed */\n" + "message G5 {\n" + "    /* @Id */\n" + "    required int32 e1 = 1;\n" + "    required string e2 = 2;\n" + "    optional double e3 = 3;\n" + "    optional float e4 = 4;\n" + "    /* @Teiid(type=short) */\n" + "    optional int32 e5 = 5;\n" + "    /* @Teiid(type=byte) */\n" + "    optional int32 e6 = 6;\n" + "    /* @Teiid(type=char, length=1) */\n" + "    optional string e7 = 7;\n" + "    optional int64 e8 = 8;\n" + "    /* @Teiid(type=bigdecimal) */\n" + "    optional string e9 = 9;\n" + "    /* @Teiid(type=biginteger) */\n" + "    optional string e10 = 10;\n" + "    /* @Teiid(type=time) */\n" + "    optional int64 e11 = 11;\n" + "    /* @Teiid(type=timestamp) */\n" + "    optional int64 e12 = 12;\n" + "    /* @Teiid(type=date) */\n" + "    optional int64 e13 = 13;\n" + "    /* @Teiid(type=object) */\n" + "    optional bytes e14 = 14;\n" + "    /* @Teiid(type=blob) */\n" + "    optional bytes e15 = 15;\n" + "    /* @Teiid(type=clob) */\n" + "    optional bytes e16 = 16;\n" + "    /* @Teiid(type=xml) */\n" + "    optional bytes e17 = 17;\n" + "    /* @Teiid(type=geometry) */\n" + "    optional bytes e18 = 18;\n" + "}\n" + "\n" + "message pm1.G3 {\n" + "    required int32 e1 = 1;\n" + "    required string e2 = 2;\n" + "}";
    assertEquals(expected, resource.getContents());
}
Also used : MetadataFactory(org.teiid.metadata.MetadataFactory) BasicCache(org.infinispan.commons.api.BasicCache) InfinispanConnection(org.teiid.infinispan.api.InfinispanConnection) ProtobufResource(org.teiid.infinispan.api.ProtobufResource) Test(org.junit.Test)

Example 4 with ProtobufResource

use of org.teiid.infinispan.api.ProtobufResource in project teiid by teiid.

the class InfinispanExecutionFactory method getMetadata.

@Override
public void getMetadata(MetadataFactory metadataFactory, InfinispanConnection conn) throws TranslatorException {
    ProtobufMetadataProcessor metadataProcessor = (ProtobufMetadataProcessor) getMetadataProcessor();
    // $NON-NLS-1$
    PropertiesUtils.setBeanProperties(metadataProcessor, metadataFactory.getModelProperties(), "importer");
    // This block is only invoked when NATIVE metadata is defined, by the time code got here if we have
    // tables already in schema, then user defined through other metadata repositories. In this case,
    // a .proto file need to be generated based on schema, then use that to generate final metadata and
    // register the .proto with the Infinispan
    Schema schema = metadataFactory.getSchema();
    ProtobufResource resource = null;
    ArrayList<Table> removedTables = new ArrayList<>();
    if (schema.getTables() != null && !schema.getTables().isEmpty()) {
        SchemaToProtobufProcessor stpp = new SchemaToProtobufProcessor();
        stpp.setIndexMessages(true);
        resource = stpp.process(metadataFactory, conn);
        metadataProcessor.setProtobufResource(resource);
        ArrayList<Table> tables = new ArrayList<>(schema.getTables().values());
        for (Table t : tables) {
            // remove the previous tables, as we want to introduce them with necessary
            // extension metadata generated with generated .proto file. As some of the default
            // extension metadata can be added.
            removedTables.add(schema.removeTable(t.getName()));
        }
    }
    metadataProcessor.process(metadataFactory, conn);
    // may be not carried forward, we need to make sure we copy those back.
    for (Table oldT : removedTables) {
        Table newT = schema.getTable(oldT.getName());
        Map<String, String> properties = oldT.getProperties();
        for (Map.Entry<String, String> entry : properties.entrySet()) {
            newT.setProperty(entry.getKey(), entry.getValue());
        }
        newT.setSupportsUpdate(oldT.supportsUpdate());
        if (oldT.getAnnotation() != null) {
            newT.setAnnotation(oldT.getAnnotation());
        }
        List<Column> columns = oldT.getColumns();
        for (Column c : columns) {
            Column newCol = newT.getColumnByName(c.getName());
            if (newCol != null) {
                Map<String, String> colProperties = c.getProperties();
                for (Map.Entry<String, String> entry : colProperties.entrySet()) {
                    newCol.setProperty(entry.getKey(), entry.getValue());
                }
                newCol.setUpdatable(c.isUpdatable());
                if (c.getAnnotation() != null) {
                    newCol.setAnnotation(c.getAnnotation());
                }
            }
        }
    }
    resource = metadataProcessor.getProtobufResource();
    if (resource == null) {
        SchemaToProtobufProcessor stpp = new SchemaToProtobufProcessor();
        resource = stpp.process(metadataFactory, conn);
    }
    // register protobuf
    if (resource != null) {
        conn.registerProtobufFile(resource);
    }
}
Also used : Table(org.teiid.metadata.Table) Schema(org.teiid.metadata.Schema) ArrayList(java.util.ArrayList) Column(org.teiid.metadata.Column) ProtobufResource(org.teiid.infinispan.api.ProtobufResource) Map(java.util.Map)

Example 5 with ProtobufResource

use of org.teiid.infinispan.api.ProtobufResource in project teiid by teiid.

the class ProtobufMetadataProcessor method process.

@Override
public void process(MetadataFactory metadataFactory, InfinispanConnection connection) throws TranslatorException {
    String protobufFile = getProtoFilePath();
    String protoContents = null;
    String cacheName = "default";
    if (connection != null) {
        cacheName = connection.getCache().getName();
    }
    if (protobufFile != null && !protobufFile.isEmpty()) {
        File f = new File(protobufFile);
        if (f == null || !f.exists() || !f.isFile()) {
            throw new TranslatorException(InfinispanPlugin.Event.TEIID25000, InfinispanPlugin.Util.gs(InfinispanPlugin.Event.TEIID25000, protobufFile));
        }
        try {
            protoContents = ObjectConverterUtil.convertFileToString(f);
        } catch (IOException e) {
            throw new TranslatorException(e);
        }
        this.protoResource = new ProtobufResource(this.protobufName != null ? this.protobufName : protobufFile, protoContents);
        toTeiidSchema(protobufFile, protoContents, metadataFactory, cacheName);
    } else if (this.protobufName != null) {
        // Read from cache
        boolean added = false;
        BasicCache<Object, Object> metadataCache = connection.getCacheFactory().getCache(ProtobufMetadataManagerConstants.PROTOBUF_METADATA_CACHE_NAME);
        for (Object key : metadataCache.keySet()) {
            if (!this.protobufName.equalsIgnoreCase((String) key)) {
                continue;
            }
            protobufFile = (String) key;
            protoContents = (String) metadataCache.get(key);
            // read all the schemas
            toTeiidSchema(protobufFile, protoContents, metadataFactory, cacheName);
            this.protoResource = new ProtobufResource(protobufFile, protoContents);
            added = true;
            break;
        }
        if (!added) {
            throw new TranslatorException(InfinispanPlugin.Event.TEIID25012, InfinispanPlugin.Util.gs(InfinispanPlugin.Event.TEIID25012, this.protobufName));
        }
    } else if (this.protoResource != null) {
        toTeiidSchema(this.protoResource.getIdentifier(), this.protoResource.getContents(), metadataFactory, cacheName);
    } else {
        // expand the error message
        throw new TranslatorException(InfinispanPlugin.Event.TEIID25011, InfinispanPlugin.Util.gs(InfinispanPlugin.Event.TEIID25011));
    }
}
Also used : BasicCache(org.infinispan.commons.api.BasicCache) TranslatorException(org.teiid.translator.TranslatorException) IOException(java.io.IOException) ProtobufResource(org.teiid.infinispan.api.ProtobufResource) ProtoFile(com.squareup.protoparser.ProtoFile) File(java.io.File)

Aggregations

ProtobufResource (org.teiid.infinispan.api.ProtobufResource)7 BasicCache (org.infinispan.commons.api.BasicCache)4 InfinispanConnection (org.teiid.infinispan.api.InfinispanConnection)4 MetadataFactory (org.teiid.metadata.MetadataFactory)4 Test (org.junit.Test)3 Table (org.teiid.metadata.Table)2 ProtoFile (com.squareup.protoparser.ProtoFile)1 File (java.io.File)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Properties (java.util.Properties)1 BeforeClass (org.junit.BeforeClass)1 TranslationUtility (org.teiid.cdk.api.TranslationUtility)1 RuntimeMetadataImpl (org.teiid.dqp.internal.datamgr.RuntimeMetadataImpl)1 HotRodTestServer (org.teiid.infinispan.api.HotRodTestServer)1 Column (org.teiid.metadata.Column)1 Schema (org.teiid.metadata.Schema)1 TransformationMetadata (org.teiid.query.metadata.TransformationMetadata)1