-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
75a3648
commit a4103ad
Showing
13 changed files
with
202 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.