-
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.
Refactor loaders to use inputstream allowing for potential use
for set binary stream when available to JDBC driver. Fixes #705 where Files.readAllBytes throws out of memory error. Conflicts: data-migration/src/test/java/com/quorum/tessera/data/migration/DirectoryStoreFileTest.java
- Loading branch information
Showing
17 changed files
with
148 additions
and
47 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
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
Oops, something went wrong.