Skip to content

Commit 37014e0

Browse files
migrate to postgres (#65)
1 parent 90a0169 commit 37014e0

29 files changed

+277
-1840
lines changed

README.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,11 @@
66

77
### Run integration tests
88

9-
To run the integration tests, a cassandra distribution is downloaded automatically once for all the projects at the first execution for your user. It is stored in $HOME/.embedded-cassandra . For this first execution, you need http internet access, and if you are a on a restricted network requiring a proxy, you need to set the proxy details. In addition to the standard java system properties -DproxyHost, -DproxyPort, we use -DproxyUser and -DproxyPassword for authenticated proxies. In IDEs, set them in the tests system properties (usually in the "Edit run configuration" menu). For maven CLI, either set them in MAVEN_OPTS or directly on the command line:
9+
Please read [liquibase usage](https://github.com/powsybl/powsybl-parent/#liquibase-usage) for instructions to automatically generate changesets.
10+
After you generated a changeset do not forget to add it to git and in src/resource/db/changelog/db.changelog-master.yml
1011

11-
```bash
12-
$ export MAVEN_OPTS="-DproxyHost=proxy.com -DproxyPort=8080 -DproxyUser=user -DproxyPassword=XXXX"
13-
$ mvn verify
14-
```
15-
16-
OR
1712

18-
```bash
19-
$ mvn verify -DproxyHost=proxy.com -DproxyPort=8080 -DproxyUser=user -DproxyPassword=XXXX
13+
The old way to automatically generate the sql schema file (directly using hibernate) can still be used for debugging. Use the following command:
14+
```
15+
mvn package -DskipTests && rm -f src/main/resources/geoData.sql && java -jar target/gridsuite-geo-data-server-1.0.0-SNAPSHOT-exec.jar --spring.jpa.properties.javax.persistence.schema-generation.scripts.action=create
2016
```

geo-data-server/pom.xml

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
<name>Geo data server</name>
2121
<description>Geographical data server</description>
2222

23+
<properties>
24+
<liquibase-hibernate-package>org.gridsuite.geodata.server</liquibase-hibernate-package>
25+
</properties>
2326
<build>
2427
<pluginManagement>
2528
<plugins>
@@ -52,14 +55,11 @@
5255

5356
<dependencies>
5457
<!-- compile scope -->
58+
5559
<dependency>
5660
<groupId>org.apache.commons</groupId>
5761
<artifactId>commons-lang3</artifactId>
5862
</dependency>
59-
<dependency>
60-
<groupId>org.springframework.boot</groupId>
61-
<artifactId>spring-boot-starter-data-cassandra</artifactId>
62-
</dependency>
6363
<dependency>
6464
<groupId>org.springframework.boot</groupId>
6565
<artifactId>spring-boot-starter-web</artifactId>
@@ -89,20 +89,36 @@
8989
<artifactId>gridsuite-geo-data-extensions</artifactId>
9090
<version>${project.version}</version>
9191
</dependency>
92+
<dependency>
93+
<groupId>org.springframework.boot</groupId>
94+
<artifactId>spring-boot-starter-data-jpa</artifactId>
95+
</dependency>
9296

9397
<!-- runtime scope -->
9498
<dependency>
9599
<groupId>com.powsybl</groupId>
96100
<artifactId>powsybl-config-classic</artifactId>
97101
<scope>runtime</scope>
98102
</dependency>
103+
<dependency>
104+
<groupId>org.liquibase</groupId>
105+
<artifactId>liquibase-core</artifactId>
106+
<scope>runtime</scope>
107+
</dependency>
99108

100-
<!-- test scope -->
109+
<!-- PostgreSQL -->
101110
<dependency>
102-
<groupId>com.github.nosan</groupId>
103-
<artifactId>embedded-cassandra-spring-test</artifactId>
104-
<scope>test</scope>
111+
<groupId>org.postgresql</groupId>
112+
<artifactId>postgresql</artifactId>
113+
<scope>runtime</scope>
114+
</dependency>
115+
<dependency>
116+
<groupId>org.springframework.boot</groupId>
117+
<artifactId>spring-boot-starter-actuator</artifactId>
118+
<scope>runtime</scope>
105119
</dependency>
120+
121+
<!-- test scope -->
106122
<dependency>
107123
<groupId>junit</groupId>
108124
<artifactId>junit</artifactId>
@@ -117,13 +133,6 @@
117133
<groupId>org.springframework.boot</groupId>
118134
<artifactId>spring-boot-starter-test</artifactId>
119135
<scope>test</scope>
120-
<!-- cassandra driver 4.0 already brings an org.json and vaadin json is only a subset of org.json -->
121-
<exclusions>
122-
<exclusion>
123-
<groupId>com.vaadin.external.google</groupId>
124-
<artifactId>android-json</artifactId>
125-
</exclusion>
126-
</exclusions>
127136
</dependency>
128137
<dependency>
129138
<groupId>org.mockito</groupId>
@@ -146,5 +155,11 @@
146155
<artifactId>powsybl-iidm-test</artifactId>
147156
<scope>test</scope>
148157
</dependency>
158+
<dependency>
159+
<groupId>com.h2database</groupId>
160+
<artifactId>h2</artifactId>
161+
<scope>test</scope>
162+
</dependency>
163+
149164
</dependencies>
150165
</project>

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

Lines changed: 0 additions & 56 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright (c) 2022, RTE (http://www.rte-france.com)
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
*/
7+
package org.gridsuite.geodata.server;
8+
9+
import org.springframework.context.annotation.Configuration;
10+
import org.springframework.context.annotation.PropertySource;
11+
12+
/**
13+
* @author Jacques Borsenberger <jacques.borsenebrger at rte-france.com>
14+
*/
15+
16+
@Configuration
17+
@PropertySource(value = {"classpath:database.properties"})
18+
@PropertySource(value = {"file:/config/database.properties"}, ignoreResourceNotFound = true)
19+
public class DataSourceConfig {
20+
}

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

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.springframework.beans.factory.annotation.Autowired;
1919
import org.springframework.beans.factory.annotation.Value;
2020
import org.springframework.stereotype.Service;
21+
import org.springframework.transaction.annotation.Transactional;
2122

2223
import java.util.*;
2324
import java.util.Map.Entry;
@@ -27,6 +28,7 @@
2728

2829
/**
2930
* @author Chamseddine Benhamed <chamseddine.benhamed at rte-france.com>
31+
* @author Jacques Borsenberger <jacques.borsenberger at rte-france.com>
3032
*/
3133
@Service
3234
public class GeoDataService {
@@ -42,15 +44,16 @@ public class GeoDataService {
4244
@Autowired
4345
private LineRepository lineRepository;
4446

45-
@Autowired
46-
private LineCustomRepository lineCustomRepository;
47+
private Set<String> toCountryIds(Collection<Country> countries) {
48+
return countries.stream().map(Country::name).collect(Collectors.toSet());
49+
}
4750

4851
private Map<String, SubstationGeoData> readSubstationGeoDataFromDb(Set<Country> countries) {
4952
// read substations from DB
50-
// TODO filter by country
5153
StopWatch stopWatch = StopWatch.createStarted();
5254

53-
List<SubstationEntity> substationEntities = substationRepository.findAll();
55+
List<SubstationEntity> substationEntities = countries.isEmpty() ? substationRepository.findAll() :
56+
substationRepository.findByCountryIn(toCountryIds(countries));
5457
Map<String, SubstationGeoData> substationsGeoDataDB = substationEntities.stream()
5558
.map(SubstationEntity::toGeoData)
5659
.collect(Collectors.toMap(SubstationGeoData::getId, Function.identity()));
@@ -300,26 +303,24 @@ private List<Coordinate> addCoordinates(Coordinate substationStart, List<Coordin
300303

301304
}
302305

303-
List<LineGeoData> getLines(Network network, Set<Country> countries) {
306+
@Transactional(readOnly = true)
307+
public List<LineGeoData> getLines(Network network, Set<Country> countries) {
304308
LOGGER.info("Loading lines geo data for countries {} of network '{}'", countries, network.getId());
305309

306310
Objects.requireNonNull(network);
307311
Objects.requireNonNull(countries);
308312

309313
StopWatch stopWatch = StopWatch.createStarted();
314+
Set<String> lineIds = new TreeSet<>();
310315

316+
network.getLines().forEach(l -> lineIds.add(l.getId()));
311317
// read lines from DB
312-
// TODO filter by country
313-
Map<String, LineGeoData> linesGeoDataDb = lineCustomRepository.getLines();
318+
Map<String, LineGeoData> linesGeoDataDb = lineRepository.findAllById(lineIds).stream().collect(Collectors.toMap(LineEntity::getId, this::toDto));
314319

315-
List<Line> lines = network.getLineStream()
316-
.filter(line -> countries.isEmpty()
317-
|| line.getTerminal1().getVoltageLevel().getSubstation().orElseThrow().getCountry().map(countries::contains).isPresent() // TODO
318-
|| line.getTerminal2().getVoltageLevel().getSubstation().orElseThrow().getCountry().map(countries::contains).isPresent()) // TODO
319-
.collect(Collectors.toList());
320+
List<Line> lines = network.getLineStream().collect(Collectors.toList());
320321
// we also want the destination substation (so we add the neighbouring country)
321322
Set<Country> countryAndNextTo =
322-
lines.stream().flatMap(line -> line.getTerminals().stream().map(term -> term.getVoltageLevel().getSubstation().orElseThrow().getNullableCountry()).filter(Objects::nonNull)) // TODO
323+
lines.stream().flatMap(line -> line.getTerminals().stream().map(term -> term.getVoltageLevel().getSubstation().orElseThrow().getNullableCountry()).filter(Objects::nonNull))
323324
.collect(Collectors.toSet());
324325
Map<String, SubstationGeoData> substationGeoDataDb = getSubstationMap(network, countryAndNextTo);
325326
List<LineGeoData> lineGeoData = lines.stream().map(line -> getLineGeoDataWithEndSubstations(linesGeoDataDb, substationGeoDataDb, line))
@@ -329,6 +330,21 @@ List<LineGeoData> getLines(Network network, Set<Country> countries) {
329330
return lineGeoData;
330331
}
331332

333+
private LineGeoData toDto(LineEntity lineEntity) {
334+
return new LineGeoData(lineEntity.getId(), toDtoCountry(lineEntity.getCountry()),
335+
toDtoCountry(lineEntity.getOtherCountry()), lineEntity.getSubstationStart(), lineEntity.getSubstationEnd(),
336+
toDto(lineEntity.getCoordinates())
337+
);
338+
}
339+
340+
private Country toDtoCountry(String country) {
341+
return Country.valueOf(country);
342+
}
343+
344+
private List<Coordinate> toDto(List<CoordinateEmbeddable> coordinates) {
345+
return coordinates.stream().map(e -> new Coordinate(e.getLat(), e.getLon())).collect(Collectors.toList());
346+
}
347+
332348
private Map<String, SubstationGeoData> getSubstationMap(Network network, Set<Country> countries) {
333349
return getSubstations(network, countries).stream().collect(Collectors.toMap(SubstationGeoData::getId, Function.identity()));
334350
}
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,32 @@
88

99
import lombok.*;
1010
import org.gridsuite.geodata.extensions.Coordinate;
11-
import org.springframework.data.cassandra.core.mapping.UserDefinedType;
1211

12+
import javax.persistence.Column;
13+
import javax.persistence.Embeddable;
1314
import java.util.List;
1415
import java.util.stream.Collectors;
1516

1617
/**
1718
* @author Chamseddine Benhamed <chamseddine.benhamed at rte-france.com>
1819
*/
19-
@UserDefinedType("coordinate")
2020
@AllArgsConstructor
2121
@NoArgsConstructor
2222
@Getter
2323
@Builder
2424
@ToString
25-
public class CoordinateEntity {
25+
@Embeddable
26+
public class CoordinateEmbeddable {
2627

28+
@Column(name = "latitude")
2729
private double lat;
2830

31+
@Column(name = "longitude")
2932
private double lon;
3033

31-
static List<CoordinateEntity> create(List<Coordinate> coordinates) {
34+
static List<CoordinateEmbeddable> create(List<Coordinate> coordinates) {
3235
return coordinates.stream()
33-
.map(p -> CoordinateEntity.builder()
36+
.map(p -> CoordinateEmbeddable.builder()
3437
.lat(p.getLat())
3538
.lon(p.getLon())
3639
.build())

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

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

0 commit comments

Comments
 (0)