Search in sources :

Example 1 with BasicCache

use of org.infinispan.commons.api.BasicCache 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 2 with BasicCache

use of org.infinispan.commons.api.BasicCache 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)

Example 3 with BasicCache

use of org.infinispan.commons.api.BasicCache in project teiid by teiid.

the class TestSchemaToProtobufProcessor method testConverstionUsingCacheAnnotation.

@SuppressWarnings("rawtypes")
@Test
public void testConverstionUsingCacheAnnotation() throws Exception {
    SchemaToProtobufProcessor tool = new SchemaToProtobufProcessor();
    tool.setIndexMessages(true);
    MetadataFactory mf = TestProtobufMetadataProcessor.protoMatadata("tables_bad.proto");
    InfinispanConnection conn = Mockito.mock(InfinispanConnection.class);
    BasicCache cache = Mockito.mock(BasicCache.class);
    Mockito.stub(cache.getName()).toReturn("foo");
    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";
    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 BasicCache

use of org.infinispan.commons.api.BasicCache in project teiid by teiid.

the class TestSchemaToProtobufProcessor method testSimpleNoMetadataConversion.

@Test
public void testSimpleNoMetadataConversion() throws Exception {
    Properties props = new Properties();
    MetadataFactory mf = new MetadataFactory("proto", 1, "model", SystemMetadata.getInstance().getRuntimeTypeMap(), props, null);
    mf.setParser(new QueryParser());
    mf.parse(new FileReader(UnitTestUtil.getTestDataFile("tables_no_metadata.ddl")));
    SchemaToProtobufProcessor tool = new SchemaToProtobufProcessor();
    tool.setIndexMessages(true);
    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 */\n" + "message G1 {\n" + "    /* @Id */\n" + "    required int32 e1 = 1;\n" + "    required string e2 = 2;\n" + "    optional float e3 = 3;\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" + "    optional string e2 = 2;\n" + "    optional bytes e5 = 3;\n" + "    optional int64 e6 = 4;\n" + "    repeated G4 g4 = 5;\n" + "}\n" + "\n" + "/* @Indexed */\n" + "message G4 {\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";
    assertEquals(expected, resource.getContents());
}
Also used : QueryParser(org.teiid.query.parser.QueryParser) MetadataFactory(org.teiid.metadata.MetadataFactory) BasicCache(org.infinispan.commons.api.BasicCache) InfinispanConnection(org.teiid.infinispan.api.InfinispanConnection) FileReader(java.io.FileReader) ProtobufResource(org.teiid.infinispan.api.ProtobufResource) Properties(java.util.Properties) Test(org.junit.Test)

Aggregations

BasicCache (org.infinispan.commons.api.BasicCache)4 ProtobufResource (org.teiid.infinispan.api.ProtobufResource)4 Test (org.junit.Test)3 InfinispanConnection (org.teiid.infinispan.api.InfinispanConnection)3 MetadataFactory (org.teiid.metadata.MetadataFactory)3 ProtoFile (com.squareup.protoparser.ProtoFile)1 File (java.io.File)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 Properties (java.util.Properties)1 QueryParser (org.teiid.query.parser.QueryParser)1 TranslatorException (org.teiid.translator.TranslatorException)1