Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added custom mapping documentation and corrected handling of default …

…keyspaces to allow separate one for each cluster as well and a single overall.
  • Loading branch information...
commit a5179fb125d4b48b8c50ea6d768a385a851696aa 1 parent f7b8cf4
@bflorian authored
View
8 grails-app/conf/Config.groovy
@@ -15,7 +15,15 @@ astyanax {
}
}
}
+ dummy1 {
+ seeds = ["localhost:9160"]
+ defaultKeyspace = "Dummy1Default"
+ }
+ dummy2 {
+ seeds = ["localhost:9160"]
+ }
}
+ defaultKeyspace = "OverallDefault"
defaultCluster = 'standard'
}
View
19 grails-app/services/com/reachlocal/grails/plugins/cassandra/astyanax/AstyanaxService.groovy
@@ -58,7 +58,7 @@ class AstyanaxService implements InitializingBean
def config = grailsApplication.config
clusters = config.astyanax.clusters
defaultCluster = config.astyanax.defaultCluster
- defaultKeyspace = clusters[defaultCluster].defaultKeyspace
+ defaultKeyspace = clusters[defaultCluster].defaultKeyspace ?: config.astyanax.defaultKeyspace
clusters.each {key, props ->
def port = props.port ?: 9160
@@ -70,11 +70,17 @@ class AstyanaxService implements InitializingBean
.setMaxConnsPerHost(maxConsPerHost)
.setSeeds(props.seeds),
- contexts: [:]
+ contexts: [:],
+ defaultKeyspace: props.defaultKeyspace ?: config.astyanax.defaultKeyspace
]
}
}
+ def defaultKeyspaceName(cluster)
+ {
+ clusterMap[cluster].defaultKeyspace
+ }
+
/**
* Returns a keyspace entity
*
@@ -82,7 +88,7 @@ class AstyanaxService implements InitializingBean
* @param cluster Optional, name of the Cassandra cluster, defaults to configured defaultCluster
*
*/
- def keyspace(String name=defaultKeyspace, String cluster=defaultCluster)
+ def keyspace(String name=null, String cluster=defaultCluster)
{
context(name, cluster).entity
}
@@ -94,7 +100,7 @@ class AstyanaxService implements InitializingBean
* @param cluster Optional, name of the Cassandra cluster, defaults to configured defaultCluster
*
*/
- def withKeyspace(String keyspace=defaultKeyspace, String cluster=defaultCluster, Closure block) throws Exception
+ def withKeyspace(String keyspace=null, String cluster=defaultCluster, Closure block) throws Exception
{
block(context(keyspace, cluster).entity)
}
@@ -142,9 +148,10 @@ class AstyanaxService implements InitializingBean
*/
def context(keyspace, cluster)
{
- def context = clusterMap[cluster].contexts[keyspace]
+ def ks = keyspace ?: clusterMap[cluster].defaultKeyspace
+ def context = clusterMap[cluster].contexts[ks]
if (!context) {
- context = newContext(keyspace, cluster)
+ context = newContext(ks, cluster)
context.start()
}
return context
View
30 test/integration/com/reachlocal/grails/plugins/cassandra/test/AstyanaxServiceTests.groovy
@@ -35,6 +35,36 @@ class AstyanaxServiceTests
}
@Test
+ void testDefaultClusterDefaultKeyspace()
+ {
+ assertEquals "AstyanaxTest", astyanaxService.keyspace().keyspaceName
+ }
+
+ @Test
+ void testClusterDefaultKeyspace1()
+ {
+ assertEquals "Dummy1Default", astyanaxService.keyspace(null, 'dummy1').keyspaceName
+ }
+
+ @Test
+ void testClusterDefaultKeyspace2()
+ {
+ assertEquals "OverallDefault", astyanaxService.keyspace(null, 'dummy2').keyspaceName
+ }
+
+ @Test
+ void testKeyspaceDefaultCluster()
+ {
+ assertEquals "MyKeyspace", astyanaxService.keyspace('MyKeyspace').keyspaceName
+ }
+
+ @Test
+ void testKeyspaceDefaultCluster2()
+ {
+ assertEquals "MyOtherKeyspace", astyanaxService.keyspace('MyOtherKeyspace', 'dummy2').keyspaceName
+ }
+
+ @Test
void testRoundTrip()
{
astyanaxService.withKeyspace() {keyspace ->

0 comments on commit a5179fb

Please sign in to comment.
Something went wrong with that request. Please try again.