Skip to content

Commit

Permalink
Merge pull request #22 from aws-cloud-clubs/feature/#21
Browse files Browse the repository at this point in the history
[Feat] : 멘토님께서 말씀해주신 피드백 사항 반영 : 유저 시나리오 재정의에 따른 partitionKey 변경 및 유사 검색어 기능 부활
  • Loading branch information
Yeon-chae authored Aug 2, 2024
2 parents 0f49c25 + b27d2b6 commit bbbf702
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public class FileSearchController {
+ "파일 경로에 대한 입력을 하였을 경우 유사한 파일 경로에 대한 결과물을 제공합니다.")
@Parameter(name = "filePath", description = "파일 경로에 관한 정보를 입력. 입력하지 않으면 전체 파일 경로를 반환")
@GetMapping("/search")
public ResponseEntity<FileSearchResponseDto> searchFiles(@ModelAttribute FileSearchRequestDto fileSearchRequestDto) {
public ResponseEntity<FileSearchResponseDto> searchFiles(@RequestParam String userId, @RequestParam(required = false) String fileName) {
FileSearchRequestDto fileSearchRequestDto = new FileSearchRequestDto(userId, fileName);
List<String> searchedFiles = dbService.searchFiles(fileSearchRequestDto);
return ResponseEntity.ok(new FileSearchResponseDto("파일 검색 성공", searchedFiles));
}
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/com/mdt/backend/domain/FileInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBIndexRangeKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
import lombok.AllArgsConstructor;
Expand All @@ -18,12 +19,12 @@
@Builder
public class FileInfo {

@DynamoDBHashKey(attributeName = "file_name")
private String fileName;

@DynamoDBRangeKey(attributeName = "user_id")
@DynamoDBHashKey(attributeName = "user_id")
private String userId;

@DynamoDBRangeKey(attributeName = "file_name")
private String fileName;

@DynamoDBAttribute(attributeName = "file_size")
private int fileSize;

Expand All @@ -34,6 +35,7 @@ public class FileInfo {
private int fileContentLength;

@DynamoDBAttribute(attributeName = "created_at")
@DynamoDBIndexRangeKey(localSecondaryIndexName = "created_at-index")
private String createdAt;

}
5 changes: 5 additions & 0 deletions src/main/java/com/mdt/backend/dto/FileSearchRequestDto.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.mdt.backend.dto;

import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class FileSearchRequestDto {
private String userId;
private String fileName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,37 @@
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBScanExpression;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.mdt.backend.domain.FileInfo;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class DynamoDbExpressionProvider {

private static final String PARTITION_KEY=":file_name";
private static final String KEY_CONDITION = "file_name = "+ PARTITION_KEY;
private static final String PARTITION_KEY=":user_id";
private static final String KEY_CONDITION = "user_id = "+ PARTITION_KEY;


public static DynamoDBQueryExpression<FileInfo> provideQueryExpression(String value) {
Map<String, AttributeValue> attributeMap = Map.of(PARTITION_KEY,
new AttributeValue(value));
public static DynamoDBQueryExpression<FileInfo> provideQueryExpression(String userId, String fileName) {
Map<String, AttributeValue> attributeMap = new HashMap<>();

attributeMap.put(PARTITION_KEY, new AttributeValue(userId));
attributeMap.put(":file_name", new AttributeValue().withS(fileName));
return new DynamoDBQueryExpression<FileInfo>()
.withKeyConditionExpression(KEY_CONDITION)
.withIndexName("created_at-index")
.withFilterExpression("contains(file_name,:file_name)")
.withExpressionAttributeValues(attributeMap);
}

public static DynamoDBQueryExpression<FileInfo> provideEmptyQueryExpression(String userId) {
Map<String, AttributeValue> attributeMap = new HashMap<>();

public static DynamoDBScanExpression provideFilterScanExpression(String key,String value, String filterExpression) {
Map<String, AttributeValue> attributeMap = Map.of(key,
new AttributeValue(value));
attributeMap.put(PARTITION_KEY, new AttributeValue(userId));

return new DynamoDBScanExpression()
.withFilterExpression(filterExpression)
return new DynamoDBQueryExpression<FileInfo>()
.withKeyConditionExpression(KEY_CONDITION)
.withExpressionAttributeValues(attributeMap);
}

public static DynamoDBScanExpression provideEmptyScanExpression() {
return new DynamoDBScanExpression();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,15 @@ public void save(FileInfo fileInfo) {
dynamoDBMapper.save(fileInfo);
}

@Override
public List<FileInfo> findByScanFilePath(String fileName) {
try {
DynamoDBMapper mapper = new DynamoDBMapper(client);

DynamoDBScanExpression expression = DynamoDbExpressionProvider.
provideFilterScanExpression(
":file_name", fileName,
"file_name = :file_name");

PaginatedScanList<FileInfo> scan = mapper.scan(FileInfo.class,
expression);

return scan.stream().toList();
} catch (RuntimeException e) {
log.error("error : {}", e.getMessage());
throw new FileSearchException();
}
}

@Override
public List<FileInfo> findByQueryFilePath(String fileName) {
public List<FileInfo> findByQueryFilePath(String userId, String fileName) {
try {
DynamoDBMapper mapper = new DynamoDBMapper(client);

DynamoDBQueryExpression<FileInfo> expr = DynamoDbExpressionProvider.
provideQueryExpression(fileName);
provideQueryExpression(userId,fileName);

PaginatedQueryList<FileInfo> query = mapper.query(FileInfo.class, expr);

Expand All @@ -63,18 +45,14 @@ public List<FileInfo> findByQueryFilePath(String fileName) {
}
}

@Override
public void deleteByFilePath(String filePath) {

}

@Override
public List<FileInfo> findAll() {
public List<FileInfo> findAll(String userId) {
try {
DynamoDBMapper mapper = new DynamoDBMapper(client);
PaginatedScanList<FileInfo> scan = mapper.scan(FileInfo.class,
DynamoDbExpressionProvider.provideEmptyScanExpression());
return scan.stream().toList();
PaginatedQueryList<FileInfo> query = mapper.query(FileInfo.class,
DynamoDbExpressionProvider.provideEmptyQueryExpression(userId));
return query.stream().toList();
} catch (RuntimeException e) {
log.error("error : {}", e.getMessage());
throw new FileSearchException();
Expand Down
11 changes: 2 additions & 9 deletions src/main/java/com/mdt/backend/repository/FileInfoRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,10 @@

public interface FileInfoRepository {


void save(FileInfo fileInfo);

List<FileInfo> findByScanFilePath(String fileName);


List<FileInfo> findByQueryFilePath(String fileName);


void deleteByFilePath(String filePath);
List<FileInfo> findByQueryFilePath(String userName, String fileName);

List<FileInfo> findAll();
List<FileInfo> findAll(String userName);

}
15 changes: 11 additions & 4 deletions src/main/java/com/mdt/backend/service/AwsDynamoDbService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,23 @@ public class AwsDynamoDbService implements DbService {

private final FileInfoRepository fileInfoRepository;



// 테스트를 위해 한 번 12000개의 임시 데이터를 넣었습니다.
// 1번 케이스 -> 유저 이름 파일 이름 둘 다 다른 경우
// 2번 케이스 -> 유저 이름 다르지만 파일 이름이 동일한 경우


@Override
public List<String> searchFiles(FileSearchRequestDto dto) {
if (isRequestFilePathEmpty(dto))
return fileInfoRepository.findAll().stream().map(fileInfo -> fileInfo.getUserId() + "/" + fileInfo.getFileName()).toList();
if (isRequestFileNameEmpty(dto))
return fileInfoRepository.findAll(dto.getUserId()).stream().map(fileInfo -> fileInfo.getUserId() + "/" + fileInfo.getFileName()).toList();

return fileInfoRepository.findByQueryFilePath(dto.getFileName()).stream()
return fileInfoRepository.findByQueryFilePath(dto.getUserId(), dto.getFileName()).stream()
.map(fileInfo -> fileInfo.getUserId() + "/" + fileInfo.getFileName()).toList();
}

private boolean isRequestFilePathEmpty(FileSearchRequestDto dto) {
private boolean isRequestFileNameEmpty(FileSearchRequestDto dto) {
String fileName = dto.getFileName();
return (Objects.isNull(fileName) || fileName.isEmpty());
}
Expand Down

0 comments on commit bbbf702

Please sign in to comment.