Skip to content

Commit

Permalink
Refact: DSM 파일 하나 넣는 시간 1분 단축 (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
brorica authored and kimtaehyun98 committed May 4, 2022
1 parent 75a3648 commit a4103ad
Show file tree
Hide file tree
Showing 13 changed files with 202 additions and 148 deletions.
40 changes: 40 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
HELP.md
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/

### DSM ###
/src/Files/
9 changes: 9 additions & 0 deletions .idea/libraries/postgresql_42_3_1.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 0 additions & 10 deletions .idea/runConfigurations.xml

This file was deleted.

Binary file modified out/production/storeDSM/Main.class
Binary file not shown.
Binary file modified out/production/storeDSM/Model/Dsm.class
Binary file not shown.
Binary file modified out/production/storeDSM/Repository/DsmRepository.class
Binary file not shown.
Binary file modified out/production/storeDSM/Service/DsmService.class
Binary file not shown.
18 changes: 18 additions & 0 deletions src/Config/JdbcTemplate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package Config;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class JdbcTemplate {

private final String url = "jdbc:postgresql://localhost:5432/geor";
private final String user = "postgres";
private final String password = "1"; //password 입력

public Connection getConnection() throws SQLException {
Connection connect = DriverManager.getConnection(url, user, password);
connect.setAutoCommit(false);
return connect;
}
}
18 changes: 4 additions & 14 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,17 @@
import Service.DsmService;

import java.io.IOException;
import java.nio.file.*;
import java.sql.SQLException;

public class Main {

private static DsmService dsmService = new DsmService();
private static String rootPath = "src/Files";

public static void main(String[] args) throws SQLException {
public static void main(String[] args) {

long startTime = System.currentTimeMillis();

try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(rootPath))) {
for (Path file: stream) {
// System.out.println(file.getFileName());
dsmService.run(rootPath + "/" + file.getFileName().toString());
}
} catch (IOException | DirectoryIteratorException ex) {
System.err.println(ex);
}

DsmService dsmService = new DsmService();
dsmService.save();

long endTime = System.currentTimeMillis();
System.out.println(String.format("코드 실행 시간: %20dms", endTime - startTime));
}
Expand Down
41 changes: 32 additions & 9 deletions src/Model/Dsm.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,37 @@
package Model;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

@Getter @Setter @AllArgsConstructor
/**
* Dsm 파일에 대한 ReadStream 을 관리하는 도메인
*/
public class Dsm {

private double x;
private double y;
private double z;


private final File dsm;
private final BufferedReader reader;

public Dsm(File dsm) throws FileNotFoundException {
this.dsm = dsm;
reader = new BufferedReader(new FileReader(dsm));
}

public BufferedReader getReader() {
return reader;
}

public String getDsmName() {
return dsm.getName();
}

public void close() {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
98 changes: 60 additions & 38 deletions src/Repository/DsmRepository.java
Original file line number Diff line number Diff line change
@@ -1,51 +1,73 @@
package Repository;

import Model.Dsm;
import Config.JdbcTemplate;

import Model.Dsm;
import java.io.BufferedReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DsmRepository {

public void saveDsm(ArrayList<Dsm> dsm) throws SQLException {
String url = "jdbc:postgresql://localhost:5432/geor";
String user = "postgres";
String password = ""; //password 입력
try
{
Connection connect = null;
connect = DriverManager.getConnection(url, user, password);

if(connect != null) {
System.out.println("Connection successful!!");
}
else {
throw new SQLException("no connection...");

private JdbcTemplate jdbcTemplate = new JdbcTemplate();
private final int batchCountLimit = 4096;

/**
* DsmService 에서 수집한 모든 DSM read stream 배열 전달받음
*/
public void save(List<Dsm> dsmList) {
try(Connection connect = jdbcTemplate.getConnection()) {
setPsTmt(connect, dsmList);
connect.commit();
} catch (SQLException e) {
e.printStackTrace();
}
}

private void setPsTmt(Connection connect, List<Dsm> dsmList) throws SQLException {
String sql = "INSERT INTO DSM VALUES(?, ?, ?)";
long totalBatchCount = 0;
try (PreparedStatement ps = connect.prepareStatement(sql)) {
for (Dsm dsm : dsmList) {
long startTime = System.currentTimeMillis();
System.out.printf("save %s ... ", dsm.getDsmName());
totalBatchCount += readDsm(ps, dsm);
dsm.close();
long endTime = System.currentTimeMillis();
System.out.println(String.format("코드 실행 시간: %20dms", endTime - startTime));
}

connect.setAutoCommit(false);

// sql문
String sql = "INSERT INTO DSM VALUES(?, ?, ?)";
PreparedStatement ps = connect.prepareStatement(sql);

for(Dsm temp : dsm){
ps.setDouble(1, temp.getX());
ps.setDouble(2, temp.getY());
ps.setDouble(3, temp.getZ());
ps.addBatch();
ps.clearParameters();
} catch (SQLException | IOException e) {
// DSM 파일의 하나라도 오류가 나면 해당 파일 전체 작업 rollback.
System.err.printf("오류, 다음 %d 개의 작업 rollback\n", totalBatchCount);
connect.rollback();
e.printStackTrace();
}
System.out.printf("\ntotalBatchCount : %d\n", totalBatchCount);
}

private int readDsm(PreparedStatement ps, Dsm dsm) throws IOException, SQLException {
BufferedReader reader = dsm.getReader();
int batchCount = batchCountLimit, batchResult = 0;
String line;
while ((line = reader.readLine()) != null) {
String[] s = line.split(" ");
ps.setDouble(1, Double.parseDouble(s[0]));
ps.setDouble(2, Double.parseDouble(s[1]));
ps.setDouble(3, Double.parseDouble(s[2]));
ps.addBatch();
ps.clearParameters();
if(--batchCount == 0) {
batchResult += ps.executeBatch().length;
batchCount = batchCountLimit;
ps.clearBatch();
}

ps.executeBatch();
ps.clearParameters(); //Batch 초기화
connect.commit();

} catch (SQLException ex) {
throw ex;
}
batchResult += ps.executeBatch().length;
ps.clearBatch();
System.out.printf("%d records\n", batchResult);
return batchResult;
}
}
115 changes: 38 additions & 77 deletions src/Service/DsmService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,86 +2,47 @@

import Model.Dsm;
import Repository.DsmRepository;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DsmService {

private File file;
private ArrayList<Dsm> arr = new ArrayList<>();
private DsmRepository dsmRepository = new DsmRepository();

public void run(String pathName) throws SQLException{
// 파일 객체 생성
file = new File(pathName);
setArr();
}

private void setArr() throws SQLException {
try{
//입력 스트림 생성
FileReader file_reader = new FileReader(file);

String temp = "";
double x = 0.0, y = 0.0, z = 0.0;
int cur = 0, flag = 0, cnt = 0;

while((cur = file_reader.read()) != -1){
char c = (char)cur;
if(('0' <= c && c <= '9') || c == '.') {
temp += c;
}
else{
if(temp.equals("")) {
continue;
}
if(flag == 0) {
x = Double.parseDouble(temp);
}
else if(flag == 1){
y = Double.parseDouble(temp);
}
else {
z = Double.parseDouble(temp);
arr.add(new Dsm(x, y, z));
cnt++;
if(cnt >= 1000000) { // heap overFlow 발생하기 때문에 100만 단위로 끊어줌
dsmRepository.saveDsm(arr);
// printDsm(arr);
cnt = 0;
arr.clear();
}
}
flag++;
flag %= 3;
temp = "";
}
}
// 마지막 남은 좌표들 한번에 저장
dsmRepository.saveDsm(arr);
arr.clear(); // clear 해줘야지 추후 여러개의 dsm 파일 저장 시 오류 없이 가능
file_reader.close();

} catch (FileNotFoundException e) {
e.getStackTrace();
} catch(IOException e){
e.getStackTrace();
}
}

/**
* test용 x,y,z 찍어보는 함수
* @param dsm
*/
void printDsm(ArrayList<Dsm>dsm){
for(Dsm temp : dsm) {
System.out.println(temp.getX() + " " + temp.getY() + " " + temp.getZ());
}
}


private final DsmRepository dsmRepository;

public DsmService() {
dsmRepository = new DsmRepository();
}

public void save() {
dsmRepository.save(getDsmList());
}

/**
* DSM 파일들에 대한 read stream 생성
*/
private List<Dsm> getDsmList() {
File[] files = findDsm();
List<Dsm> dsmList = new ArrayList<>();
for (File file : files) {
try {
dsmList.add(new Dsm(file));
} catch (FileNotFoundException e) {
System.err.printf("error : %s\n", file.getName());
e.printStackTrace();
}
}
return dsmList;
}

/**
* 지정한 경로 내의 모든 DSM 파일 가져옴
*/
private File[] findDsm() {
String path = "src/Files";
String extension = "xyz";
File directory = new File(path);
return directory.listFiles((dir, name) -> name.endsWith(extension));
}
}
Loading

0 comments on commit a4103ad

Please sign in to comment.