Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'trunk' of github.com:impetus-opensource/Kundera into trunk

  • Loading branch information...
commit 4bae789050d37ed60a5b3bdcec6e6f3ec923439c 2 parents 9d45a90 + a0421a7
@kkmishra kkmishra authored
View
135 kundera-cassandra/src/main/java/com/impetus/client/cassandra/pelops/PelopsClient.java
@@ -56,6 +56,7 @@
import org.scale7.cassandra.pelops.RowDeletor;
import org.scale7.cassandra.pelops.Selector;
import org.scale7.cassandra.pelops.pool.IThriftPool;
+import org.scale7.cassandra.pelops.pool.IThriftPool.IPooledConnection;
import com.impetus.client.cassandra.pelops.PelopsDataHandler.ThriftRow;
import com.impetus.client.cassandra.query.CassQuery;
@@ -287,7 +288,7 @@ public void persistJoinTable(JoinTableData joinTableData)
createIndexesOnColumns(joinTableName, poolName, columns);
String pk = (String) key;
- mutator.writeColumns(joinTableName, new Bytes(pk.getBytes()), Arrays.asList(columns.toArray(new Column[0])));
+ mutator.writeColumns(joinTableName, Bytes.fromUTF8(pk), Arrays.asList(columns.toArray(new Column[0])));
mutator.execute(ConsistencyLevel.ONE);
}
@@ -308,7 +309,7 @@ public void persistJoinTable(JoinTableData joinTableData)
String parentId)
{
Selector selector = Pelops.createSelector(PelopsUtils.generatePoolName(getPersistenceUnit()));
- List<Column> columns = selector.getColumnsFromRow(joinTableName, new Bytes(parentId.getBytes()),
+ List<Column> columns = selector.getColumnsFromRow(joinTableName, Bytes.fromUTF8(parentId),
Selector.newColumnsPredicateAll(true, 10), ConsistencyLevel.ONE);
// PelopsDataHandler handler = new PelopsDataHandler(this);
@@ -585,65 +586,75 @@ public List executeQuery(String cqlQuery, Class clazz, List<String> relationalFi
{
IThriftPool thrift = Pelops.getDbConnPool(PelopsUtils.generatePoolName(getPersistenceUnit()));
// thrift.get
- org.apache.cassandra.thrift.Cassandra.Client thriftClient = thrift.getConnection().getAPI();
-
- EntityMetadata entityMetadata = KunderaMetadataManager.getEntityMetadata(clazz);
- CqlResult result = null;
- List returnedEntities = null;
- try
- {
- result = thriftClient.execute_cql_query(ByteBufferUtil.bytes(cqlQuery),
- org.apache.cassandra.thrift.Compression.NONE);
- if (result != null && (result.getRows() != null || result.getRowsSize() > 0))
- {
- returnedEntities = new ArrayList<Object>(result.getRowsSize());
- Iterator<CqlRow> iter = result.getRowsIterator();
- while (iter.hasNext())
- {
- CqlRow row = iter.next();
- String rowKey = Bytes.toUTF8(row.getKey());
-
- ThriftRow thriftRow = handler.new ThriftRow(rowKey, entityMetadata.getTableName(),
- row.getColumns(), null);
-
- Object entity = handler.fromColumnThriftRow(clazz, entityMetadata, thriftRow, relationalField,
- relationalField != null && !relationalField.isEmpty());
- returnedEntities.add(entity);
- }
- }
- }
- catch (InvalidRequestException e)
- {
- log.error("Error while executing native CQL query Caused by:" + e.getLocalizedMessage());
- throw new PersistenceException(e);
- }
- catch (UnavailableException e)
- {
- log.error("Error while executing native CQL query Caused by:" + e.getLocalizedMessage());
- throw new PersistenceException(e);
- }
- catch (TimedOutException e)
- {
- log.error("Error while executing native CQL query Caused by:" + e.getLocalizedMessage());
- throw new PersistenceException(e);
- }
- catch (SchemaDisagreementException e)
- {
- log.error("Error while executing native CQL query Caused by:" + e.getLocalizedMessage());
- throw new PersistenceException(e);
- }
- catch (TException e)
- {
- log.error("Error while executing native CQL query Caused by:" + e.getLocalizedMessage());
- throw new PersistenceException(e);
- }
- catch (Exception e)
- {
- log.error("Error while executing native CQL query Caused by:" + e.getLocalizedMessage());
- throw new PersistenceException(e);
- }
-
- return returnedEntities;
+ IPooledConnection connection = thrift.getConnection();
+ try {
+ org.apache.cassandra.thrift.Cassandra.Client thriftClient = connection.getAPI();
+
+ EntityMetadata entityMetadata = KunderaMetadataManager.getEntityMetadata(clazz);
+ CqlResult result = null;
+ List returnedEntities = null;
+ try
+ {
+ result = thriftClient.execute_cql_query(ByteBufferUtil.bytes(cqlQuery),
+ org.apache.cassandra.thrift.Compression.NONE);
+ if (result != null && (result.getRows() != null || result.getRowsSize() > 0))
+ {
+ returnedEntities = new ArrayList<Object>(result.getRowsSize());
+ Iterator<CqlRow> iter = result.getRowsIterator();
+ while (iter.hasNext())
+ {
+ CqlRow row = iter.next();
+ String rowKey = Bytes.toUTF8(row.getKey());
+
+ ThriftRow thriftRow = handler.new ThriftRow(rowKey, entityMetadata.getTableName(),
+ row.getColumns(), null);
+
+ Object entity = handler.fromColumnThriftRow(clazz, entityMetadata, thriftRow, relationalField,
+ relationalField != null && !relationalField.isEmpty());
+ returnedEntities.add(entity);
+ }
+ }
+ }
+ catch (InvalidRequestException e)
+ {
+ log.error("Error while executing native CQL query Caused by:" + e.getLocalizedMessage());
+ throw new PersistenceException(e);
+ }
+ catch (UnavailableException e)
+ {
+ log.error("Error while executing native CQL query Caused by:" + e.getLocalizedMessage());
+ throw new PersistenceException(e);
+ }
+ catch (TimedOutException e)
+ {
+ log.error("Error while executing native CQL query Caused by:" + e.getLocalizedMessage());
+ throw new PersistenceException(e);
+ }
+ catch (SchemaDisagreementException e)
+ {
+ log.error("Error while executing native CQL query Caused by:" + e.getLocalizedMessage());
+ throw new PersistenceException(e);
+ }
+ catch (TException e)
+ {
+ log.error("Error while executing native CQL query Caused by:" + e.getLocalizedMessage());
+ throw new PersistenceException(e);
+ }
+ catch (Exception e)
+ {
+ log.error("Error while executing native CQL query Caused by:" + e.getLocalizedMessage());
+ throw new PersistenceException(e);
+ }
+ return returnedEntities;
+ } finally {
+ try {
+ if (connection != null) {
+ connection.release();
+ }
+ } catch (Exception e) {
+ log.warn("Releasing connection for native CQL query failed", e);
+ }
+ }
}
/*
@@ -681,7 +692,7 @@ protected void onPersist(EntityMetadata metadata, Object entity, Object id, List
if (thriftColumns != null && !thriftColumns.isEmpty())
{
// Bytes.fromL
- mutator.writeColumns(metadata.getTableName(), new Bytes(tf.getId().getBytes()),
+ mutator.writeColumns(metadata.getTableName(), Bytes.fromUTF8(tf.getId()),
Arrays.asList(tf.getColumns().toArray(new Column[0])));
}
View
4 kundera-cassandra/src/main/java/com/impetus/client/cassandra/query/CassandraEntityReader.java
@@ -68,9 +68,9 @@ public CassandraEntityReader()
}
@Override
- public EnhanceEntity findById(Object primaryKey, EntityMetadata m, List<String> relationNames, Client client)
+ public EnhanceEntity findById(Object primaryKey, EntityMetadata m, Client client)
{
- return super.findById(primaryKey, m, relationNames, client);
+ return super.findById(primaryKey, m, client);
}
View
65 kundera-cassandra/src/test/java/com/impetus/client/persistence/NativeQueryTest.java
@@ -53,8 +53,8 @@
public class NativeQueryTest
{
- /** The schema. */
- private final String schema = "test";
+// /** The schema. */
+ private final String schema = "KunderaExamples";
/**
* Sets the up.
@@ -66,7 +66,7 @@
public void setUp() throws Exception
{
CassandraCli.cassandraSetUp();
- CassandraCli.createKeySpace("KunderaExamples");
+ CassandraCli.createKeySpace(schema);
}
/**
@@ -91,38 +91,75 @@ public void testCreateNativeQuery()
public void testExecutNativeQuery()
{
EntityManagerFactoryImpl emf = getEntityManagerFactory();
- String nativeSql = "CREATE KEYSPACE " + schema
- + " with strategy_class = 'SimpleStrategy' and strategy_options:replication_factor=1";
- String useNativeSql = "USE test";
+// String nativeSql = "CREATE KEYSPACE " + schema
+// + " with strategy_class = 'SimpleStrategy' and strategy_options:replication_factor=1";
+ String useNativeSql = "USE " + schema;
EntityManager em = new EntityManagerImpl(emf, PersistenceUnitTransactionType.RESOURCE_LOCAL,
PersistenceContextType.EXTENDED);
- Query q = em.createNativeQuery(nativeSql, CassandraEntitySample.class);
- // q.getResultList();
- q.executeUpdate();
- q = em.createNativeQuery(useNativeSql, CassandraEntitySample.class);
+// Query q = em.createNativeQuery(nativeSql, CassandraEntitySample.class);
+// // q.getResultList();
+// q.executeUpdate();
+ Query q = em.createNativeQuery(useNativeSql, CassandraEntitySample.class);
// q.getResultList();
q.executeUpdate();
Assert.assertTrue(CassandraCli.keyspaceExist(schema));
Assert.assertFalse(CassandraCli.keyspaceExist("invalidSchema"));
}
+ /**
+ * Native queries should not leak connections. Pelops pool fails providing a
+ * connection if we don't call {@link IPooledConnection#release()}
+ */
+ @Test
+ public void testReleasesNativeQueryConnection() {
+ EntityManagerFactoryImpl emf = getEntityManagerFactory();
+// String nativeSql = "CREATE KEYSPACE "
+// + schema
+// + " with strategy_class = 'SimpleStrategy' and strategy_options:replication_factor=1";
+// String useNativeSql = "USE test";
+ String useNativeSql = "USE " + schema;
+
+ EntityManager em = new EntityManagerImpl(emf,
+ PersistenceUnitTransactionType.RESOURCE_LOCAL,
+ PersistenceContextType.EXTENDED);
+// Query q = em.createNativeQuery(nativeSql, CassandraEntitySample.class);
+// // q.getResultList();
+// q.executeUpdate();
+
+ // won't be able to loop if connections are leaked
+ for (int i = 0; i < 30; i++) {
+ Query q = em.createNativeQuery(useNativeSql, CassandraEntitySample.class);
+ // q.getResultList();
+ q.executeUpdate();
+ }
+ }
+
/**
* Test create insert column family query.
*/
@Test
public void testCreateInsertColumnFamilyQuery()
{
+// String nativeSql = "CREATE KEYSPACE " + schema
+// + " with strategy_class = 'SimpleStrategy' and strategy_options:replication_factor=1";
+// String useNativeSql = "USE test";
+ String useNativeSql = "USE " + schema;
EntityManagerFactoryImpl emf = getEntityManagerFactory();
EntityManager em = new EntityManagerImpl(emf, PersistenceUnitTransactionType.RESOURCE_LOCAL,
PersistenceContextType.EXTENDED);
-
+// Query q = em.createNativeQuery(nativeSql, CassandraEntitySample.class);
+// // q.getResultList();
+// q.executeUpdate();
+ Query q = em.createNativeQuery(useNativeSql, CassandraEntitySample.class);
+ // q.getResultList();
+ q.executeUpdate();
// create column family
String colFamilySql = "CREATE COLUMNFAMILY users (key varchar PRIMARY KEY,full_name varchar, birth_date int,state varchar)";
- Query q = em.createNativeQuery(colFamilySql, CassandraEntitySample.class);
+ q = em.createNativeQuery(colFamilySql, CassandraEntitySample.class);
// q.getResultList();
q.executeUpdate();
- Assert.assertTrue(CassandraCli.columnFamilyExist("users", "KunderaExamples"));
+ Assert.assertTrue(CassandraCli.columnFamilyExist("users", "test"));
// Add indexes
String idxSql = "CREATE INDEX ON users (birth_date)";
@@ -224,7 +261,7 @@ private EntityManagerFactoryImpl getEntityManagerFactory()
@After
public void tearDown() throws Exception
{
- CassandraCli.dropKeySpace("KunderaExamples");
+// CassandraCli.dropKeySpace("KunderaExamples");
CassandraCli.dropKeySpace(schema);
}
View
3  kundera-core/src/main/java/com/impetus/kundera/lifecycle/states/ManagedState.java
@@ -103,8 +103,7 @@ public void handleFind(NodeStateContext nodeStateContext)
Object nodeData = null; // Node data
EntityReader reader = client.getReader();
- EnhanceEntity enhanceEntity = reader.findById(entityId, entityMetadata, entityMetadata.getRelationNames(),
- client);
+ EnhanceEntity enhanceEntity = reader.findById(entityId, entityMetadata, client);
if (enhanceEntity != null && enhanceEntity.getEntity() != null)
{
View
2  kundera-core/src/main/java/com/impetus/kundera/persistence/AbstractEntityReader.java
@@ -269,7 +269,7 @@ else if (multiplicity.equals(Relation.ForeignKey.ONE_TO_MANY))
* com.impetus.kundera.client.Client)
*/
- protected EnhanceEntity findById(Object primaryKey, EntityMetadata m, List<String> relationNames, Client client)
+ protected EnhanceEntity findById(Object primaryKey, EntityMetadata m, Client client)
{
try
{
View
2  kundera-core/src/main/java/com/impetus/kundera/persistence/EntityReader.java
@@ -83,6 +83,6 @@
* the client
* @return the enhance entity
*/
- EnhanceEntity findById(Object primaryKey, EntityMetadata m, List<String> relationNames, Client client);
+ EnhanceEntity findById(Object primaryKey, EntityMetadata m, Client client);
}
View
10 kundera-core/src/main/java/com/impetus/kundera/property/accessor/StringAccessor.java
@@ -41,7 +41,7 @@ public final String fromBytes(Class targetClass, byte[] bytes)
try
{
- return new String(bytes, Constants.ENCODING);
+ return bytes != null? new String(bytes, Constants.ENCODING): null;
}
catch (UnsupportedEncodingException e)
{
@@ -62,7 +62,7 @@ public final String fromBytes(Class targetClass, byte[] bytes)
try
{
- return ((String) s).getBytes(Constants.ENCODING);
+ return s != null ? ((String) s).getBytes(Constants.ENCODING):null;
}
catch (UnsupportedEncodingException e)
{
@@ -80,7 +80,11 @@ public final String fromBytes(Class targetClass, byte[] bytes)
@Override
public final String toString(Object object)
{
- return (String) object;
+ if (object != null && object.getClass().isAssignableFrom(String.class))
+ {
+ return (String) object;
+ }
+ return null;
}
/*
View
4 kundera-hbase/src/main/java/com/impetus/client/hbase/HBaseEntityReader.java
@@ -55,9 +55,9 @@
* com.impetus.kundera.client.Client)
*/
@Override
- public EnhanceEntity findById(Object primaryKey, EntityMetadata m, List<String> relationNames, Client client)
+ public EnhanceEntity findById(Object primaryKey, EntityMetadata m, Client client)
{
- return super.findById(primaryKey, m, relationNames, client);
+ return super.findById(primaryKey, m, client);
}
}
View
4 kundera-mongo/src/main/java/com/impetus/client/mongodb/MongoEntityReader.java
@@ -55,9 +55,9 @@
* com.impetus.kundera.client.Client)
*/
@Override
- public EnhanceEntity findById(Object primaryKey, EntityMetadata m, List<String> relationNames, Client client)
+ public EnhanceEntity findById(Object primaryKey, EntityMetadata m, Client client)
{
- return super.findById(primaryKey, m, relationNames, client);
+ return super.findById(primaryKey, m, client);
}
}
View
3  kundera-rdbms/src/main/java/com/impetus/client/rdbms/query/RDBMSEntityReader.java
@@ -427,8 +427,9 @@ public void setFilter(String filter)
* com.impetus.kundera.client.Client)
*/
@Override
- public EnhanceEntity findById(Object primaryKey, EntityMetadata m, List<String> relationNames, Client client)
+ public EnhanceEntity findById(Object primaryKey, EntityMetadata m, Client client)
{
+ List<String> relationNames = m.getRelationNames();
if (relationNames != null && !relationNames.isEmpty())
{
Set<String> keys = new HashSet<String>(1);
View
11 pom.xml
@@ -105,17 +105,6 @@
<name>Clover maven repo</name>
<url>http://repository.atlassian.com/maven2/</url>
</repository>
- <repository>
- <id>kundera-missing</id>
- <name>Kundera Public Missing Resources Repository</name>
- <url>http://kundera.googlecode.com/svn/maven2/maven-missing-resources</url>
- <releases>
- <enabled>true</enabled>
- </releases>
- <snapshots>
- <enabled>true</enabled>
- </snapshots>
- </repository>
</repositories>
<!-- Build profiles -->
<profiles>
Please sign in to comment.
Something went wrong with that request. Please try again.