-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BE] Feat/#548 공간 인덱스 구현 #584
Changes from all commits
2e08b0a
f5b4dcf
ee9c956
d70a522
0c20267
2e5e15b
9ae0bd9
25988ac
dd92c8f
ffa37a0
75c1d08
aef78bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,7 +44,7 @@ public List<TopicResponse> findNearbyTopicsSortedByPinCount( | |
) { | ||
Coordinate coordinate = Coordinate.of(latitude, longitude); | ||
List<Location> nearLocation = locationRepository.findAllByCoordinateAndDistanceInMeters( | ||
coordinate, | ||
coordinate.getCoordinate(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 간지나네용 |
||
NEAR_DISTANCE_METERS | ||
); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,16 @@ | ||
package com.mapbefine.mapbefine.location.domain; | ||
|
||
import static com.mapbefine.mapbefine.location.exception.LocationErrorCode.ILLEGAL_COORDINATE_RANGE; | ||
import static java.lang.Math.acos; | ||
import static java.lang.Math.cos; | ||
import static java.lang.Math.sin; | ||
import static java.lang.Math.toRadians; | ||
import static lombok.AccessLevel.PROTECTED; | ||
|
||
import com.mapbefine.mapbefine.location.exception.LocationException.LocationBadRequestException; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Embeddable; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import org.locationtech.jts.geom.GeometryFactory; | ||
import org.locationtech.jts.geom.Point; | ||
import org.locationtech.jts.geom.PrecisionModel; | ||
|
||
@Embeddable | ||
@NoArgsConstructor(access = PROTECTED) | ||
|
@@ -23,21 +22,25 @@ public class Coordinate { | |
private static final double LONGITUDE_LOWER_BOUND = 124; | ||
private static final double LONGITUDE_UPPER_BOUND = 132; | ||
|
||
@Column(columnDefinition = "Decimal(18,15)") | ||
private double latitude; | ||
/* | ||
* 4326은 데이터베이스에서 사용하는 여러 SRID 값 중, 일반적인 GPS기반의 위/경도 좌표를 저장할 때 쓰이는 값입니다. | ||
* */ | ||
Comment on lines
+25
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오호 그래서 SRID 값이 4326 이었던 거군용 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 일종의 식별코드 같은 것이로군요 |
||
private static final GeometryFactory geometryFactory = new GeometryFactory(new PrecisionModel(), 4326); | ||
|
||
@Column(columnDefinition = "Decimal(18,15)") | ||
private double longitude; | ||
@Column(columnDefinition = "geometry SRID 4326", nullable = false) | ||
private Point coordinate; | ||
|
||
private Coordinate(double latitude, double longitude) { | ||
this.latitude = latitude; | ||
this.longitude = longitude; | ||
private Coordinate(Point point) { | ||
this.coordinate = point; | ||
} | ||
|
||
|
||
public static Coordinate of(double latitude, double longitude) { | ||
validateRange(latitude, longitude); | ||
|
||
return new Coordinate(latitude, longitude); | ||
Point point = geometryFactory.createPoint(new org.locationtech.jts.geom.Coordinate(longitude, latitude)); | ||
|
||
return new Coordinate(point); | ||
} | ||
|
||
private static void validateRange(double latitude, double longitude) { | ||
|
@@ -51,13 +54,12 @@ private static boolean isNotInRange(double latitude, double longitude) { | |
|| (longitude < LONGITUDE_LOWER_BOUND || LONGITUDE_UPPER_BOUND < longitude); | ||
} | ||
|
||
public double calculateDistanceInMeters(Coordinate otherCoordinate) { | ||
double earthRadius = 6_371_000; | ||
public double getLatitude() { | ||
return coordinate.getY(); | ||
} | ||
|
||
return acos(sin(toRadians(otherCoordinate.latitude)) * sin(toRadians(this.latitude)) + ( | ||
cos(toRadians(otherCoordinate.latitude)) * cos(toRadians(this.latitude)) | ||
* cos(toRadians(otherCoordinate.longitude - this.longitude)) | ||
)) * earthRadius; | ||
Comment on lines
-54
to
-60
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 실제로 쓰이지 않고 있는 메서드를 삭제하고 테스트도 함께 지워주셨군요! |
||
public double getLongitude() { | ||
return coordinate.getX(); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package com.mapbefine.mapbefine.location.domain; | ||
|
||
import java.util.List; | ||
import org.locationtech.jts.geom.Point; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
|
@@ -11,15 +12,12 @@ public interface LocationRepository extends JpaRepository<Location, Long> { | |
|
||
@Query( | ||
"SELECT l FROM Location l " | ||
+ "WHERE ( 6371000 * acos( cos( radians(:#{#current_coordinate.latitude}) ) " | ||
+ " * cos( radians( l.coordinate.latitude ) ) " | ||
+ " * cos( radians( l.coordinate.longitude ) - radians(:#{#current_coordinate.longitude}) ) " | ||
+ " + sin( radians(:#{#current_coordinate.latitude}) ) " | ||
+ " * sin( radians( l.coordinate.latitude ) ) ) ) <= :distance" | ||
+ "WHERE ST_Contains(ST_Buffer(:coordinate, :distance), l.coordinate.coordinate)" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
ST_Contains(A, B) 함수는 A라는 MBR(Minimum Bounding Rectangle)안에 B가 존재하는지 여부를 판단합니다. 두 함수 모두 MySQL에서 지원하는 함수입니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 네 맞아요. 그래서 명확한 계산을 하기 위해서는 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 멋집니다~! |
||
) | ||
List<Location> findAllByCoordinateAndDistanceInMeters( | ||
@Param("current_coordinate") Coordinate coordinate, | ||
@Param("coordinate") Point coordinate, | ||
@Param("distance") double distance | ||
); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1. 개행!! |
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ | |
public class DatabaseCleanup implements InitializingBean { | ||
|
||
private static final String TRUNCATE_SQL_MESSAGE = "TRUNCATE TABLE %s"; | ||
private static final String SET_REFERENTIAL_INTEGRITY_SQL_MESSAGE = "SET REFERENTIAL_INTEGRITY %s"; | ||
private static final String SET_REFERENTIAL_INTEGRITY_SQL_MESSAGE = "SET FOREIGN_KEY_CHECKS = %s"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이건 MYSQL 로 변경되어서 그런건가요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 넹 H2랑 문법이 다르니까여 |
||
private static final String DISABLE_REFERENTIAL_QUERY = String.format(SET_REFERENTIAL_INTEGRITY_SQL_MESSAGE, false); | ||
private static final String ENABLE_REFERENTIAL_QUERY = String.format(SET_REFERENTIAL_INTEGRITY_SQL_MESSAGE, true); | ||
|
||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.mapbefine.mapbefine; | ||
|
||
import org.springframework.test.context.DynamicPropertyRegistry; | ||
import org.springframework.test.context.DynamicPropertySource; | ||
import org.testcontainers.containers.MySQLContainer; | ||
|
||
|
||
public abstract class TestDatabaseContainer { | ||
|
||
private static final MySQLContainer mySQLContainer = new MySQLContainer("mysql:8.0.32"); | ||
|
||
static { | ||
mySQLContainer.start(); | ||
} | ||
|
||
@DynamicPropertySource | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 동적으로 설정파일을 수정해줍니다 ~ 컨테이너가 뜰때마다 포트번호가 다르다보니, url이 달라지잖아요 ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 지렸네용 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
public static void overrideProps(DynamicPropertyRegistry registry) { | ||
registry.add("spring.datasource.url", mySQLContainer::getJdbcUrl); | ||
registry.add("spring.datasource.username", mySQLContainer::getUsername); | ||
registry.add("spring.datasource.password", mySQLContainer::getPassword); | ||
registry.add("spring.datasource.driver-class-name", mySQLContainer::getDriverClassName); | ||
} | ||
|
||
} | ||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오홍 testContainer 전용으로 junit 의존성도 추가로 필요한가보군요
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
예쓰 !! junit에서 제공해주는 것 같더라구요