Skip to content

Commit ee6ce3b

Browse files
Make cassandra keyspace configurable (#51)
* Add cassandra keyspace name property in application yaml file Remove keyspace name from cql scripts geo_data.cql and truncate.cql Use keyspace name property in code Change in embedded cassandra factory config Signed-off-by: Franck LECUYER <[email protected]> * Modifications after code review Signed-off-by: Franck LECUYER <[email protected]>
1 parent e1dbb1d commit ee6ce3b

File tree

7 files changed

+28
-33
lines changed

7 files changed

+28
-33
lines changed

geo-data-server/src/main/java/org/gridsuite/geodata/server/CassandraConfig.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.powsybl.commons.PowsyblException;
1111
import org.gridsuite.geodata.extensions.Coordinate;
1212
import org.gridsuite.geodata.server.repositories.LineRepository;
13+
import org.springframework.beans.factory.annotation.Value;
1314
import org.springframework.context.annotation.Bean;
1415
import org.springframework.context.annotation.Configuration;
1516
import org.springframework.context.annotation.PropertySource;
@@ -31,9 +32,12 @@
3132
@EnableCassandraRepositories(basePackageClasses = LineRepository.class)
3233
public class CassandraConfig extends AbstractCassandraConfiguration {
3334

35+
@Value("${cassandra-keyspace:geo_data}")
36+
private String keyspaceName;
37+
3438
@Override
3539
protected String getKeyspaceName() {
36-
return CassandraConstants.KEYSPACE_GEO_DATA;
40+
return keyspaceName;
3741
}
3842

3943
@Bean
@@ -46,13 +50,13 @@ public CassandraClusterFactoryBean cluster(Environment env) {
4650
clusterFactory.setClusterBuilderConfigurer(builder -> {
4751
builder.withCodecRegistry(codecRegistry);
4852
Cluster cluster = builder.build();
49-
KeyspaceMetadata keyspace = cluster.getMetadata().getKeyspace(CassandraConstants.KEYSPACE_GEO_DATA);
53+
KeyspaceMetadata keyspace = cluster.getMetadata().getKeyspace(getKeyspaceName());
5054
if (keyspace == null) {
51-
throw new PowsyblException("Keyspace '" + CassandraConstants.KEYSPACE_GEO_DATA + "' not found");
55+
throw new PowsyblException("Keyspace '" + getKeyspaceName() + "' not found");
5256
}
53-
UserType coordinateType = keyspace.getUserType("coordinate");
57+
var coordinateType = keyspace.getUserType("coordinate");
5458
TypeCodec<UDTValue> coordinateTypeCodec = codecRegistry.codecFor(coordinateType);
55-
CoordinateCodec coordinateCodec = new CoordinateCodec(coordinateTypeCodec, Coordinate.class);
59+
var coordinateCodec = new CoordinateCodec(coordinateTypeCodec, Coordinate.class);
5660
codecRegistry.register(coordinateCodec);
5761
return builder;
5862
});
@@ -62,7 +66,7 @@ public CassandraClusterFactoryBean cluster(Environment env) {
6266
@Bean
6367
public CassandraMappingContext cassandraMapping(Cluster cluster, Environment env) {
6468
CassandraMappingContext mappingContext = new CassandraMappingContext();
65-
mappingContext.setUserTypeResolver(new SimpleUserTypeResolver(cluster, CassandraConstants.KEYSPACE_GEO_DATA));
69+
mappingContext.setUserTypeResolver(new SimpleUserTypeResolver(cluster, getKeyspaceName()));
6670
return mappingContext;
6771
}
6872

geo-data-server/src/main/java/org/gridsuite/geodata/server/CassandraConstants.java

Lines changed: 0 additions & 18 deletions
This file was deleted.

geo-data-server/src/main/resources/application.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ network-store-server:
77

88
network-geo-data:
99
iterations : 5
10+
11+
cassandra-keyspace: geo_data
12+
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
CREATE TYPE geo_data.coordinate (
1+
CREATE TYPE coordinate (
22
lat double,
33
lon double,
44
);
55

6-
CREATE TABLE geo_data.substations (
6+
CREATE TABLE substations (
77
country text,
88
id text,
9-
coordinate frozen<geo_data.coordinate>,
9+
coordinate frozen<coordinate>,
1010
PRIMARY KEY (country, id)
1111
);
1212

13-
CREATE TABLE geo_data.lines (
13+
CREATE TABLE lines (
1414
country text,
1515
id text,
1616
side1 boolean,
1717
otherCountry text,
1818
substationStart text,
1919
substationEnd text,
20-
coordinates frozen<list<geo_data.coordinate>>,
20+
coordinates frozen<list<coordinate>>,
2121
PRIMARY KEY (country, id)
2222
);

geo-data-server/src/test/java/org/gridsuite/geodata/test/EmbeddedCassandraFactoryConfig.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
import com.github.nosan.embedded.cassandra.api.Cassandra;
1111
import com.github.nosan.embedded.cassandra.api.CassandraFactory;
1212
import com.github.nosan.embedded.cassandra.api.Version;
13+
import com.github.nosan.embedded.cassandra.api.cql.CqlScript;
1314
import com.github.nosan.embedded.cassandra.artifact.RemoteArtifact;
15+
import org.springframework.beans.factory.annotation.Value;
1416
import org.springframework.context.annotation.Bean;
1517
import org.springframework.context.annotation.Configuration;
1618
import org.springframework.context.annotation.Scope;
@@ -34,6 +36,9 @@
3436
@Configuration
3537
public class EmbeddedCassandraFactoryConfig {
3638

39+
@Value("${cassandra-keyspace:geo_data}")
40+
private String keyspaceName;
41+
3742
@Bean(initMethod = "start", destroyMethod = "stop")
3843
Cassandra cassandra(CassandraFactory cassandraFactory) {
3944
return cassandraFactory.create();
@@ -42,7 +47,8 @@ Cassandra cassandra(CassandraFactory cassandraFactory) {
4247
@Bean
4348
CassandraConnection cassandraConnection(Cassandra cassandra) {
4449
CassandraConnection cassandraConnection = new DefaultCassandraConnectionFactory().create(cassandra);
45-
CqlDataSet.ofClasspaths("create_keyspace.cql", "geo_data.cql").forEachStatement(cassandraConnection::execute);
50+
String[] createKeyspace = CqlScript.ofClasspath("create_keyspace.cql").getStatements().stream().map(s -> String.format(s, keyspaceName)).toArray(l -> new String[l]);
51+
CqlDataSet.ofStrings(createKeyspace).add(CqlDataSet.ofStrings(String.format("USE %s;", keyspaceName))).add(CqlDataSet.ofClasspaths("geo_data.cql")).forEachStatement(cassandraConnection::execute);
4652
return cassandraConnection;
4753
}
4854

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
CREATE KEYSPACE "geo_data" WITH REPLICATION = {'class':'SimpleStrategy', 'replication_factor':1};
1+
CREATE KEYSPACE "%s" WITH REPLICATION = {'class':'SimpleStrategy', 'replication_factor':1};
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
TRUNCATE geo_data.substations;
1+
TRUNCATE substations;
22

3-
TRUNCATE geo_data.lines;
3+
TRUNCATE lines;

0 commit comments

Comments
 (0)