Skip to content

Commit

Permalink
TEIID-4581 changed for hotrod and libmode to be consistent for requir…
Browse files Browse the repository at this point in the history
…ing pkField configured for enabling updates or supporting relationshipts, and that when not defined only can do reads and no relationships are supported
  • Loading branch information
vhalbert committed Dec 7, 2016
1 parent 0d11a3a commit e98f100
Show file tree
Hide file tree
Showing 19 changed files with 271 additions and 103 deletions.
Expand Up @@ -162,16 +162,6 @@ public boolean supportsNotCriteria() {

@Override
public MetadataProcessor<ObjectConnection> getMetadataProcessor(){
// commenting out as the config of protobuf annotations will trigger JDG
// to auto create the protobuf file and marshallers, and therefore,
// the descriptor info will be available for metadatda creation.
// this is important because the correct NIS is assigned to the
// child object/tables so that they are correctly searched via DSL.


// if (this.supportsSearchabilityUsingAnnotations()) {
// return new AnnotationMetadataProcessor(true);
// }
return new ProtobufMetadataProcessor();
}

Expand Down
Expand Up @@ -115,7 +115,8 @@ public void process(MetadataFactory metadataFactory, ObjectConnection connection
private void createRootTable(MetadataFactory mf, Class<?> entity, Descriptor descriptor, String cacheName, InfinispanHotRodConnection conn) throws TranslatorException {

String pkField = conn.getPkField();
boolean updatable = (materialized ? true : (pkField != null ? true : false));
// the table can only be updateable if it has the pkField defined, even if its materialized)
boolean updatable = (pkField != null ? true : false);

rootTable = addTable(mf, entity, updatable, false);
if (materialized) {
Expand All @@ -131,8 +132,7 @@ private void createRootTable(MetadataFactory mf, Class<?> entity, Descriptor des
if (updatable) {
pkMethod = findMethod(entity.getName(), pkField, conn);
if (pkMethod == null) {
throw new TranslatorException(InfinispanPlugin.Util.gs(InfinispanPlugin.Event.TEIID25008, new Object[] {pkField, cacheName, entity.getName()}));

throw new TranslatorException(InfinispanPlugin.Util.gs(InfinispanPlugin.Event.TEIID25008, new Object[] {pkField, cacheName, entity.getName()}));
}
}

Expand All @@ -148,8 +148,7 @@ private void createRootTable(MetadataFactory mf, Class<?> entity, Descriptor des
NullType nt = NullType.Nullable;
SearchType st = isSearchable(fd);
Class<?> returnType = getJavaType( fd,entity, conn);
// String j = fd.getJavaType().name();
if (fd.getName().equalsIgnoreCase(pkField)) {
if (updatable && fd.getName().equalsIgnoreCase(pkField)) {
addKey = true;
st = SearchType.Searchable;
nt = NullType.No_Nulls;
Expand All @@ -165,14 +164,15 @@ private void createRootTable(MetadataFactory mf, Class<?> entity, Descriptor des
}
}
// dont make primary key updatable, the object must be deleted and readded in order to change the key
addRootColumn(mf, returnType, getProtobufNativeType(fd), fd.getFullName(), fd.getName(), st, rootTable.getName(), rootTable, true, true, nt);
addRootColumn(mf, returnType, getProtobufNativeType(fd), fd.getFullName(), fd.getName(), st, rootTable.getName(), rootTable, true, updatable, nt);
if (materialized) {
addRootColumn(mf, returnType, getProtobufNativeType(fd), fd.getFullName(), fd.getName(), st, stagingTable.getName(), stagingTable, true, true, nt);

}
}

if (pkMethod != null) {


if (updatable) {
@SuppressWarnings("null")
String pkName = "PK_" + pkField.toUpperCase(); //$NON-NLS-1$
ArrayList<String> x = new ArrayList<String>(1) ;
Expand All @@ -181,18 +181,17 @@ private void createRootTable(MetadataFactory mf, Class<?> entity, Descriptor des
if (materialized) {
mf.addPrimaryKey(pkName, x , stagingTable);
}

}

if (!addKey) {
addRootColumn(mf, pkMethod.getReturnType(), pkMethod.getReturnType(), pkField, pkField, SearchType.Searchable, rootTable.getName(), rootTable, true, true, NullType.No_Nulls);
}

for (String key : descriptorMap.keySet()) {
FieldDescriptor fd = descriptorMap.get(key);
Descriptor d = fd.getMessageType();
createInnerTable(mf, d, key, rootTable, pkMethod, conn);


// if materialized, child objects (relationships) are not supported, so no need to create them
if (!materialized && updatable) {
for (String key : descriptorMap.keySet()) {
FieldDescriptor fd = descriptorMap.get(key);
Descriptor d = fd.getMessageType();
createInnerTable(mf, d, key, rootTable, pkMethod, conn);

}
}

}
Expand Down
Expand Up @@ -28,7 +28,7 @@ TEIID25004=Unable to perform {0}, no Primary or Foreign Key defined for table {1
TEIID25005=Table {0} has defined a container class of type Maps, which is not currently supported
TEIID25006=Updating primary key column {0} in table {1} is not supported, use delete and insert.
TEIID25007=Updates are disabled, no Primary key defined for cache {0}
TEIID25008=Primary Key {0} defined for cache {1} does not match a method on class {2}
TEIID25008=Primary key field {0} defined for cache {1} does not match a method on class {2}
TEIID25009=Unable to insert duplicate into table {0} with key value {1}
TEIID25010=The alias cache '{0}' doesnt exist in the Remote Cache container
TEIID25011=The StagingCacheName and AliasCacheName must both be configured to do materialization
Expand Down
Expand Up @@ -38,17 +38,14 @@
*/
public class PersonCacheConnection extends TestInfinispanHotRodConnection {

public static InfinispanHotRodConnection createConnection(RemoteCache map, boolean useKeyClassType, Version version) {
CacheNameProxy proxy = new CacheNameProxy(PersonCacheSource.PERSON_CACHE_NAME);

PersonCacheConnection conn = new PersonCacheConnection(map, PersonCacheSource.CLASS_REGISTRY, proxy, useKeyClassType);
conn.setVersion(version);
conn.setConfiguredUsingAnnotations(true);
return conn;
}

public static InfinispanHotRodConnection createConnection(RemoteCache map, final String keyField, boolean useKeyClassType, Version version) {
CacheNameProxy proxy = new CacheNameProxy(PersonCacheSource.PERSON_CACHE_NAME);
public static InfinispanHotRodConnection createConnection(RemoteCache map, final String keyField, boolean useKeyClassType, boolean staging, Version version) {
CacheNameProxy proxy = null;

if (staging) {
proxy = new CacheNameProxy(PersonCacheSource.PERSON_CACHE_NAME, "ST_" + PersonCacheSource.PERSON_CACHE_NAME, "aliasCacheName");
} else {
proxy = new CacheNameProxy(PersonCacheSource.PERSON_CACHE_NAME);
}

PersonCacheConnection conn = new PersonCacheConnection(map, PersonCacheSource.CLASS_REGISTRY, proxy, useKeyClassType) {

Expand All @@ -62,6 +59,32 @@ public void setPkField(String keyfield) {
return conn;
}

public static InfinispanHotRodConnection createConnection(RemoteCache map, boolean useKeyClassType, Version version) {
return createConnection(map, "id", useKeyClassType, version);
// CacheNameProxy proxy = new CacheNameProxy(PersonCacheSource.PERSON_CACHE_NAME);
//
// PersonCacheConnection conn = new PersonCacheConnection(map, PersonCacheSource.CLASS_REGISTRY, proxy, useKeyClassType);
// conn.setVersion(version);
// conn.setConfiguredUsingAnnotations(true);
// return conn;
}

public static InfinispanHotRodConnection createConnection(RemoteCache map, final String keyField, boolean useKeyClassType, Version version) {
return createConnection(map, keyField, useKeyClassType, false, version);
// CacheNameProxy proxy = new CacheNameProxy(PersonCacheSource.PERSON_CACHE_NAME);
// PersonCacheConnection conn = new PersonCacheConnection(map, PersonCacheSource.CLASS_REGISTRY, proxy, useKeyClassType) {
//
// @Override
// public void setPkField(String keyfield) {
// super.setPkField(keyField);
// }
// };
// conn.setVersion(version);
// conn.setConfiguredUsingAnnotations(true);
// return conn;

}

/**
* @param map
* @param registry
Expand Down
Expand Up @@ -49,6 +49,7 @@
import org.infinispan.query.dsl.Query;
import org.teiid.translator.infinispan.hotrod.InfinispanHotRodConnection;
import org.teiid.translator.object.ClassRegistry;
import org.teiid.translator.object.ObjectConnection;
import org.teiid.util.Version;

/**
Expand Down Expand Up @@ -96,17 +97,23 @@ public class PersonCacheSource<K, V> implements RemoteCache<K, V>{


public static InfinispanHotRodConnection createConnection(final boolean useKeyClass) {
final RemoteCache objects = PersonCacheSource.loadCache();

return PersonCacheConnection.createConnection(objects, useKeyClass, null);

return createConnection(useKeyClass, null);
}

public static InfinispanHotRodConnection createConnection(final boolean useKeyClass, Version version) {
return createConnection("id", useKeyClass, version);
}

public static InfinispanHotRodConnection createConnection(final String keyField, final boolean useKeyClass, Version version) {
final RemoteCache objects = PersonCacheSource.loadCache();

return PersonCacheConnection.createConnection(objects, useKeyClass, version);
return PersonCacheConnection.createConnection(objects, keyField, useKeyClass, version);

}
public static ObjectConnection createConnection(final boolean useKeyClass, boolean staging, Version version) {
final RemoteCache objects = PersonCacheSource.loadCache();

return PersonCacheConnection.createConnection(objects, "id", useKeyClass, staging, version);
}

public static void loadCache(Map<Object, Object> cache) {
Expand Down
Expand Up @@ -17,6 +17,8 @@
import org.teiid.query.metadata.SystemMetadata;
import org.teiid.translator.infinispan.hotrod.InfinispanHotRodConnection;
import org.teiid.translator.infinispan.hotrod.InfinispanHotRodExecutionFactory;
import org.teiid.translator.object.ObjectConnection;
import org.teiid.translator.object.testdata.trades.TradesCacheSource;

@SuppressWarnings("nls")
public class TestProtobufMetadataProcessor {
Expand Down Expand Up @@ -47,7 +49,7 @@ public void testPersonMetadata() throws Exception {
String metadataDDL = DDLStringVisitor.getDDLString(mf.getSchema(),
null, null);

assertEquals(ObjectConverterUtil.convertFileToString(UnitTestUtil.getTestDataFile("personMetadata.ddl")), metadataDDL);
assertEquals(ObjectConverterUtil.convertFileToString(UnitTestUtil.getTestDataFile("personMetadata.ddl")).trim(), metadataDDL.trim());

}

Expand All @@ -58,14 +60,14 @@ public void testPersonMetadataUpperCaseKeyField() throws Exception {
SystemMetadata.getInstance().getRuntimeTypeMap(),
new Properties(), null);

InfinispanHotRodConnection conn = PersonCacheConnection.createConnection(PersonCacheSource.loadCache(), "ID", false, null);

InfinispanHotRodConnection conn = PersonCacheSource.createConnection("ID", false, null);
TRANSLATOR.getMetadataProcessor().process(mf, conn);

String metadataDDL = DDLStringVisitor.getDDLString(mf.getSchema(),
null, null);

assertEquals(ObjectConverterUtil.convertFileToString(UnitTestUtil.getTestDataFile("personMetadata.ddl")), metadataDDL);
assertEquals(ObjectConverterUtil.convertFileToString(UnitTestUtil.getTestDataFile("personMetadata.ddl")).trim(), metadataDDL.trim());
}

@Test
Expand All @@ -75,14 +77,14 @@ public void testPersonMetadataMixedCaseKeyField() throws Exception {
SystemMetadata.getInstance().getRuntimeTypeMap(),
new Properties(), null);

InfinispanHotRodConnection conn = PersonCacheConnection.createConnection(PersonCacheSource.loadCache(), "Id", false, null);

InfinispanHotRodConnection conn = PersonCacheSource.createConnection("Id", false, null);
TRANSLATOR.getMetadataProcessor().process(mf, conn);

String metadataDDL = DDLStringVisitor.getDDLString(mf.getSchema(),
null, null);

assertEquals(ObjectConverterUtil.convertFileToString(UnitTestUtil.getTestDataFile("personMetadata.ddl")), metadataDDL);
assertEquals(ObjectConverterUtil.convertFileToString(UnitTestUtil.getTestDataFile("personMetadata.ddl")).trim(), metadataDDL.trim());
}

@Test
Expand All @@ -99,8 +101,44 @@ public void testAllTypesMetadata() throws Exception {
String metadataDDL = DDLStringVisitor.getDDLString(mf.getSchema(),
null, null);

assertEquals(ObjectConverterUtil.convertFileToString(UnitTestUtil.getTestDataFile("allTypesMetadata.ddl")), metadataDDL );
assertEquals(ObjectConverterUtil.convertFileToString(UnitTestUtil.getTestDataFile("allTypesMetadata.ddl")).trim(), metadataDDL.trim() );

}

@Test
public void testMatPersonMetadata() throws Exception {
ObjectConnection conn = PersonCacheSource.createConnection(true, true, null);

MetadataFactory mf = new MetadataFactory("vdb", 1, "objectvdb",
SystemMetadata.getInstance().getRuntimeTypeMap(),
new Properties(), null);

TRANSLATOR.getMetadataProcessor().process(mf, conn);

String metadataDDL = DDLStringVisitor.getDDLString(mf.getSchema(),
null, null);

assertEquals(ObjectConverterUtil.convertFileToString(UnitTestUtil.getTestDataFile("personMatMetadata.ddl")).trim(), metadataDDL.trim() );

}

@Test
public void testPersonMetadataNoPKKey() throws Exception {
ObjectConnection conn = PersonCacheSource.createConnection(null, false, null);

MetadataFactory mf = new MetadataFactory("vdb", 1, "objectvdb",
SystemMetadata.getInstance().getRuntimeTypeMap(),
new Properties(), null);


TRANSLATOR.getMetadataProcessor().process(mf, conn);

String metadataDDL = DDLStringVisitor.getDDLString(mf.getSchema(),
null, null);

assertEquals(ObjectConverterUtil.convertFileToString(UnitTestUtil.getTestDataFile("personNoKey.ddl")).trim(), metadataDDL.trim() );

}


}
@@ -0,0 +1,15 @@
SET NAMESPACE 'http://www.teiid.org/translator/object/2016' AS n0;

CREATE FOREIGN TABLE Person (
name string NOT NULL OPTIONS (NAMEINSOURCE 'name', SEARCHABLE 'Searchable', NATIVE_TYPE 'java.lang.String'),
id integer NOT NULL OPTIONS (NAMEINSOURCE 'id', SEARCHABLE 'Searchable', NATIVE_TYPE 'int'),
email string OPTIONS (NAMEINSOURCE 'email', SEARCHABLE 'Unsearchable', NATIVE_TYPE 'java.lang.String'),
CONSTRAINT PK_ID PRIMARY KEY(id)
) OPTIONS (UPDATABLE TRUE);

CREATE FOREIGN TABLE ST_Person (
name string NOT NULL OPTIONS (NAMEINSOURCE 'name', SEARCHABLE 'Searchable', NATIVE_TYPE 'java.lang.String'),
id integer NOT NULL OPTIONS (NAMEINSOURCE 'id', SEARCHABLE 'Searchable', NATIVE_TYPE 'int'),
email string OPTIONS (NAMEINSOURCE 'email', SEARCHABLE 'Unsearchable', NATIVE_TYPE 'java.lang.String'),
CONSTRAINT PK_ID PRIMARY KEY(id)
) OPTIONS (UPDATABLE TRUE, "n0:primary_table" 'objectvdb.Person');
Expand Up @@ -18,4 +18,4 @@ CREATE FOREIGN TABLE PhoneNumber (
type string OPTIONS (NAMEINSOURCE 'phone.type', SEARCHABLE 'Unsearchable', NATIVE_TYPE 'java.lang.Enum'),
id integer NOT NULL OPTIONS (NAMEINSOURCE 'id', SELECTABLE FALSE, SEARCHABLE 'Searchable', NATIVE_TYPE 'int'),
CONSTRAINT FK_PERSON FOREIGN KEY(id) REFERENCES Person (id) OPTIONS (NAMEINSOURCE 'phones')
) OPTIONS (UPDATABLE TRUE);
) OPTIONS (UPDATABLE TRUE);
@@ -0,0 +1,6 @@
CREATE FOREIGN TABLE Person (
name string NOT NULL OPTIONS (NAMEINSOURCE 'name', SEARCHABLE 'Searchable', NATIVE_TYPE 'java.lang.String'),
id integer NOT NULL OPTIONS (NAMEINSOURCE 'id', SEARCHABLE 'Searchable', NATIVE_TYPE 'int'),
email string OPTIONS (NAMEINSOURCE 'email', SEARCHABLE 'Unsearchable', NATIVE_TYPE 'java.lang.String')
);

Expand Up @@ -56,6 +56,7 @@ public static enum Event implements BundleUtil.Event{
TEIID21014,
TEIID21015,
TEIID21016,
TEIID21017,
TEIID21302,
}
}

0 comments on commit e98f100

Please sign in to comment.