Skip to content

Commit 3fdfb15

Browse files
committed
Initial import
0 parents  commit 3fdfb15

17 files changed

+788
-0
lines changed

.hgignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
syntax: glob
2+
.idea/*
3+
target/*
4+
*.class
5+
*.iml

pom.xml

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.blazer</groupId>
8+
<artifactId>Testing</artifactId>
9+
<version>1.0</version>
10+
<packaging>jar</packaging>
11+
12+
<properties>
13+
<java.version>1.6</java.version>
14+
</properties>
15+
16+
<build>
17+
<plugins>
18+
<plugin>
19+
<groupId>org.apache.maven.plugins</groupId>
20+
<artifactId>maven-compiler-plugin</artifactId>
21+
<configuration>
22+
<source>${java.version}</source>
23+
<target>${java.version}</target>
24+
</configuration>
25+
</plugin>
26+
<plugin>
27+
<groupId>org.apache.maven.plugins</groupId>
28+
<artifactId>maven-jar-plugin</artifactId>
29+
<configuration>
30+
<archive>
31+
<manifest>
32+
<mainClass>com.blazer.homework.Application</mainClass>
33+
<packageName>com.blazer.homework</packageName>
34+
</manifest>
35+
</archive>
36+
</configuration>
37+
</plugin>
38+
<plugin>
39+
<artifactId>maven-assembly-plugin</artifactId>
40+
<configuration>
41+
<archive>
42+
<manifest>
43+
<mainClass>com.blazer.homework.Application</mainClass>
44+
</manifest>
45+
</archive>
46+
<descriptorRefs>
47+
<descriptorRef>jar-with-dependencies</descriptorRef>
48+
</descriptorRefs>
49+
</configuration>
50+
</plugin>
51+
</plugins>
52+
</build>
53+
54+
<reporting>
55+
<plugins>
56+
<plugin>
57+
<groupId>org.apache.maven.plugins</groupId>
58+
<artifactId>maven-project-info-reports-plugin</artifactId>
59+
<version>2.4</version>
60+
<reportSets>
61+
<reportSet>
62+
<reports>
63+
<report>index</report>
64+
</reports>
65+
</reportSet>
66+
</reportSets>
67+
</plugin>
68+
<plugin>
69+
<groupId>org.apache.maven.plugins</groupId>
70+
<artifactId>maven-checkstyle-plugin</artifactId>
71+
<version>2.9.1</version>
72+
</plugin>
73+
<plugin>
74+
<groupId>org.apache.maven.plugins</groupId>
75+
<artifactId>maven-pmd-plugin</artifactId>
76+
<version>2.7.1</version>
77+
</plugin>
78+
<plugin>
79+
<groupId>org.codehaus.mojo</groupId>
80+
<artifactId>cobertura-maven-plugin</artifactId>
81+
<version>2.5.1</version>
82+
</plugin>
83+
</plugins>
84+
</reporting>
85+
86+
<dependencies>
87+
<dependency>
88+
<groupId>org.projectlombok</groupId>
89+
<artifactId>lombok</artifactId>
90+
<version>0.11.0</version>
91+
</dependency>
92+
<dependency>
93+
<groupId>com.h2database</groupId>
94+
<artifactId>h2</artifactId>
95+
<version>1.3.167</version>
96+
</dependency>
97+
<dependency>
98+
<groupId>junit</groupId>
99+
<artifactId>junit</artifactId>
100+
<version>4.10</version>
101+
<scope>test</scope>
102+
</dependency>
103+
<dependency>
104+
<groupId>org.mockito</groupId>
105+
<artifactId>mockito-core</artifactId>
106+
<version>1.9.0</version>
107+
<scope>test</scope>
108+
</dependency>
109+
</dependencies>
110+
111+
<repositories>
112+
<repository>
113+
<id>sberan-github</id>
114+
<name>sberan-github</name>
115+
<url>https://raw.github.com/sberan/mvn-repo/master/releases</url>
116+
</repository>
117+
</repositories>
118+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.blazer.homework;
2+
3+
import java.io.*;
4+
import java.sql.Connection;
5+
import java.sql.DriverManager;
6+
import java.sql.SQLException;
7+
8+
/**
9+
* @author Constantine Linnick <theaspect@gmail.com>
10+
*/
11+
public class Application {
12+
private static final String CONNECTION_STRING = "jdbc:h2:file:dbfile;AUTOCOMMIT=OFF;MVCC=TRUE";
13+
private static final String INIT_TABLE = "CREATE TABLE IF NOT EXISTS data (key VARCHAR(64) PRIMARY KEY, value LONG)";
14+
15+
private static Connection getConnection() throws SQLException {
16+
Connection connection = DriverManager.getConnection(CONNECTION_STRING, "SA", "");
17+
connection.prepareStatement(INIT_TABLE).execute();
18+
return connection;
19+
}
20+
21+
public static void main(String argv[]) {
22+
if (argv.length < 1) {
23+
System.out.println("Specify file path with data");
24+
return;
25+
}
26+
27+
Connection connection = null;
28+
BufferedReader reader = null;
29+
30+
try {
31+
connection = getConnection();
32+
Dao dao = new Dao(connection);
33+
34+
reader = new BufferedReader(new FileReader(new FileSelector(new File(argv[0])).getMaxFile()));
35+
dao.putData(new DataReader(new Parser(" ")).parse(reader));
36+
37+
connection.commit();
38+
39+
System.out.print(dao.getData());
40+
} catch (SQLException e) {
41+
e.printStackTrace();
42+
} catch (FileNotFoundException e) {
43+
e.printStackTrace();
44+
} catch (IOException e) {
45+
e.printStackTrace();
46+
} finally {
47+
Closer.close(connection);
48+
Closer.close(reader);
49+
}
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.blazer.homework;
2+
3+
import java.io.Closeable;
4+
import java.io.IOException;
5+
import java.sql.Connection;
6+
import java.sql.SQLException;
7+
import java.sql.Statement;
8+
9+
public class Closer {
10+
public static void closeAll(Object... closeables) {
11+
for (Object c : closeables) {
12+
if (c == null) {
13+
continue;
14+
}
15+
16+
if (c instanceof Connection) {
17+
close((Connection) c);
18+
} else if (c instanceof Statement) {
19+
close((Statement) c);
20+
} else if (c instanceof Closeable) {
21+
close((Closeable) c);
22+
} else {
23+
throw new RuntimeException("Unknown class :" + c);
24+
}
25+
}
26+
}
27+
28+
public static void close(Connection connection) {
29+
try {
30+
if (connection != null) {
31+
connection.close();
32+
}
33+
} catch (SQLException e) {
34+
// do nothing
35+
}
36+
}
37+
38+
public static void close(Statement statement) {
39+
try {
40+
if (statement != null) {
41+
statement.close();
42+
}
43+
} catch (SQLException e) {
44+
// do nothing
45+
}
46+
}
47+
48+
public static void close(Closeable resource) {
49+
try {
50+
if (resource != null) {
51+
resource.close();
52+
}
53+
} catch (IOException e) {
54+
// do nothing
55+
}
56+
}
57+
}
+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.blazer.homework;
2+
3+
import java.sql.Connection;
4+
import java.sql.PreparedStatement;
5+
import java.sql.ResultSet;
6+
import java.sql.SQLException;
7+
import java.util.HashSet;
8+
import java.util.Set;
9+
10+
/**
11+
* @author Constantine Linnick <theaspect@gmail.com>
12+
*/
13+
public class Dao {
14+
private static final String SELECT_ALL = "SELECT * FROM data";
15+
private static final String SELECT = "SELECT * FROM data WHERE key = ?";
16+
private static final String DELETE = "DELETE FROM data WHERE key = ?";
17+
private static final String INSERT = "INSERT INTO data (key, value) VALUES (?, ?)";
18+
19+
private Connection connection;
20+
21+
private PreparedStatement selectStatement;
22+
private PreparedStatement deleteStatement;
23+
private PreparedStatement insertStatement;
24+
25+
26+
public Dao(Connection connection) {
27+
this.connection = connection;
28+
}
29+
30+
private void initStatements() throws SQLException {
31+
selectStatement = connection.prepareStatement(SELECT);
32+
deleteStatement = connection.prepareStatement(DELETE);
33+
insertStatement = connection.prepareStatement(INSERT);
34+
}
35+
36+
public void putData(Set<Domain> data) throws SQLException {
37+
try {
38+
initStatements();
39+
for (Domain d : data) {
40+
upsertRow(d.key, d.value);
41+
}
42+
deleteStatement.executeBatch();
43+
insertStatement.executeBatch();
44+
} finally {
45+
Closer.closeAll(selectStatement, deleteStatement, insertStatement);
46+
}
47+
}
48+
49+
public Set<Domain> getData() throws SQLException {
50+
Set<Domain> data = new HashSet<Domain>();
51+
52+
PreparedStatement preparedStatement = null;
53+
try {
54+
preparedStatement = connection.prepareStatement(SELECT_ALL);
55+
ResultSet resultSet = preparedStatement.executeQuery();
56+
while (resultSet.next()) {
57+
data.add(new Domain(resultSet.getString(1), resultSet.getLong(2)));
58+
}
59+
} finally {
60+
Closer.close(preparedStatement);
61+
}
62+
return data;
63+
}
64+
65+
private void upsertRow(String key, Long value) throws SQLException {
66+
selectStatement.setString(1, key);
67+
ResultSet select = selectStatement.executeQuery();
68+
if (select.next()) {
69+
deleteStatement.setString(1, key);
70+
deleteStatement.addBatch();
71+
} else {
72+
insertStatement.setString(1, key);
73+
insertStatement.setLong(2, value);
74+
insertStatement.addBatch();
75+
}
76+
}
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.blazer.homework;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.util.HashSet;
6+
import java.util.Set;
7+
8+
public class DataReader {
9+
private Parser parser;
10+
11+
public DataReader(Parser parser) {
12+
this.parser = parser;
13+
}
14+
15+
public Set<Domain> parse(BufferedReader reader) throws IOException {
16+
Set<Domain> data = new HashSet<Domain>();
17+
18+
String line;
19+
while ((line = reader.readLine()) != null) {
20+
Domain item = parser.parse(line);
21+
if (item != null) {
22+
data.add(item);
23+
}
24+
}
25+
26+
return data;
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.blazer.homework;
2+
3+
import lombok.EqualsAndHashCode;
4+
import lombok.ToString;
5+
6+
/**
7+
* @author Constantine Linnick <theaspect@gmail.com>
8+
*/
9+
@EqualsAndHashCode
10+
@ToString
11+
public class Domain {
12+
public String key;
13+
public Long value;
14+
15+
public Domain(String key, Long value) {
16+
this.key = key;
17+
this.value = value;
18+
}
19+
}

0 commit comments

Comments
 (0)