-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #710 from jpmorganchase/issue-705
Fix #705 out of memory error thrown by Files.readAllBytes
- Loading branch information
Showing
20 changed files
with
161 additions
and
51 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
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
3 changes: 2 additions & 1 deletion
3
data-migration/src/main/java/com/quorum/tessera/data/migration/DataExporter.java
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,11 +1,12 @@ | ||
package com.quorum.tessera.data.migration; | ||
|
||
import java.io.InputStream; | ||
import java.nio.file.Path; | ||
import java.sql.SQLException; | ||
import java.util.Map; | ||
|
||
public interface DataExporter { | ||
|
||
void export(Map<byte[], byte[]> data, Path output, String username, String password) throws SQLException; | ||
void export(Map<byte[], InputStream> data, Path output, String username, String password) throws SQLException; | ||
|
||
} |
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
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
38 changes: 33 additions & 5 deletions
38
data-migration/src/main/java/com/quorum/tessera/data/migration/SqliteDataExporter.java
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,25 +1,53 @@ | ||
package com.quorum.tessera.data.migration; | ||
|
||
import java.net.URL; | ||
import com.quorum.tessera.io.IOCallback; | ||
import com.quorum.tessera.io.UriCallback; | ||
import java.io.InputStream; | ||
import java.net.URI; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class SqliteDataExporter implements DataExporter { | ||
|
||
private static final String INSERT_ROW = "INSERT INTO ENCRYPTED_TRANSACTION (HASH,ENCODED_PAYLOAD) VALUES (?,?)"; | ||
|
||
@Override | ||
public void export(Map<byte[], byte[]> data, Path output, final String username, final String password) throws SQLException { | ||
public void export(Map<byte[], InputStream> data, Path output, String username, String password) throws SQLException { | ||
|
||
final String connectionString = "jdbc:sqlite:" + output.toString(); | ||
|
||
final URL sqlFile = getClass().getResource("/ddls/h2-ddl.sql"); | ||
final URI sqlFile = UriCallback.execute(() -> getClass().getResource("/ddls/sqlite-ddl.sql").toURI()); | ||
|
||
final JdbcDataExporter jdbcDataExporter = new JdbcDataExporter(connectionString, INSERT_ROW, sqlFile); | ||
List<String> createTables = IOCallback.execute(() -> Files.readAllLines(Paths.get(sqlFile))); | ||
|
||
jdbcDataExporter.export(data, output, username, password); | ||
try (Connection conn = DriverManager.getConnection(connectionString, username, password)) { | ||
|
||
try (Statement stmt = conn.createStatement()) { | ||
for (String createTable : createTables) { | ||
stmt.executeUpdate(createTable); | ||
} | ||
} | ||
|
||
try (PreparedStatement insertStatement = conn.prepareStatement(INSERT_ROW)) { | ||
for (Map.Entry<byte[], InputStream> values : data.entrySet()) { | ||
insertStatement.setBytes(1, values.getKey()); | ||
|
||
insertStatement.setBytes(2, Utils.toByteArray(values.getValue())); | ||
|
||
insertStatement.execute(); | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
||
|
||
} |
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
22 changes: 22 additions & 0 deletions
22
data-migration/src/main/java/com/quorum/tessera/data/migration/Utils.java
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,22 @@ | ||
package com.quorum.tessera.data.migration; | ||
|
||
import com.quorum.tessera.io.IOCallback; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.InputStream; | ||
|
||
public interface Utils { | ||
|
||
static byte[] toByteArray(InputStream in) { | ||
return IOCallback.execute(() -> { | ||
ByteArrayOutputStream os = new ByteArrayOutputStream(); | ||
|
||
byte[] buffer = new byte[1024]; | ||
int len; | ||
while ((len = in.read(buffer)) != -1) { | ||
os.write(buffer, 0, len); | ||
} | ||
|
||
return os.toByteArray(); | ||
}); | ||
} | ||
} |
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
43 changes: 36 additions & 7 deletions
43
data-migration/src/test/java/com/quorum/tessera/data/migration/DirectoryStoreFileTest.java
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,24 +1,53 @@ | ||
package com.quorum.tessera.data.migration; | ||
|
||
import java.io.InputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Map; | ||
import java.util.Random; | ||
import java.util.UUID; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.Test; | ||
|
||
public class DirectoryStoreFileTest { | ||
|
||
@Test | ||
public void load() throws Exception { | ||
|
||
Path directory = Paths.get(getClass().getResource("/dir/").toURI()); | ||
|
||
DirectoryStoreFile directoryStoreFile = new DirectoryStoreFile(); | ||
Map<byte[],byte[]> results = directoryStoreFile.load(directory); | ||
|
||
Map<byte[], InputStream> results = directoryStoreFile.load(directory); | ||
|
||
assertThat(results).hasSize(22); | ||
|
||
} | ||
|
||
@Test | ||
public void loadLarge() throws Exception { | ||
|
||
Path baseDir = Paths.get(getClass().getResource("/").toURI()); | ||
|
||
Path directory = baseDir.resolve(UUID.randomUUID().toString()); | ||
|
||
Files.createDirectories(directory); | ||
|
||
Path largeFile = Paths.get(directory.toAbsolutePath().toString(), "loadLarge"); | ||
|
||
Random random = new Random(); | ||
byte[] data = new byte[33554432]; | ||
random.nextBytes(data); | ||
Files.write(largeFile, data); | ||
|
||
DirectoryStoreFile directoryStoreFile = new DirectoryStoreFile(); | ||
|
||
Map results = directoryStoreFile.load(directory); | ||
|
||
assertThat(results).hasSize(1); | ||
} | ||
|
||
} |
Oops, something went wrong.