Skip to content

Commit

Permalink
modify DatabaseUtilities.getConnection so that eventStream, recordStr…
Browse files Browse the repository at this point in the history
…eam, accountBalance can use the same connection object; #2
  • Loading branch information
QianSwirlds committed Jul 31, 2019
1 parent f2724d4 commit 3945d99
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static boolean processFile(File balanceFile) {

try {
// process the file
connect = DatabaseUtilities.openDatabase(connect);
connect = DatabaseUtilities.getConnection();

if (connect != null) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static void main(String[] args) {
File balanceFile = getLatestBalancefile(balanceFilesPath);
if (balanceFile != null) {
// process the file
connect = DatabaseUtilities.openDatabase(connect);
connect = DatabaseUtilities.getConnection();

if (connect != null) {
PreparedStatement insertBalance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
public class DatabaseUtilities {
private static final Logger log = LogManager.getLogger("recordStream-log");
static final Marker LOGM_EXCEPTION = MarkerManager.getMarker("EXCEPTION");
private static Connection connect = null;

public static Connection openDatabase(Connection connect) {
public static Connection getConnection() {
if (connect == null) {
try {
ConfigLoader configLoader = new ConfigLoader("./config/config.json");
Expand All @@ -34,8 +35,8 @@ public static Connection openDatabase(Connection connect) {
return connect;
}

public static Connection closeDatabase(Connection connect) throws SQLException {
public static void closeDatabase() throws SQLException {
connect.close();
return null;
connect = null;
}
}
35 changes: 35 additions & 0 deletions src/main/java/com/hedera/downloader/Downloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -447,4 +447,39 @@ protected static void setupCloudConnection() {
xfer_mgr = TransferManagerBuilder.standard()
.withS3Client(s3Client).build();
}

protected void listObjects() {
ListObjectsRequest listRequest = new ListObjectsRequest()
.withBucketName(bucketName)
.withPrefix("eventstreams/")
.withDelimiter("/")
.withMaxKeys(100);
ObjectListing objects = s3Client.listObjects(listRequest);
try {
while (true) {
List<S3ObjectSummary> summaries = objects.getObjectSummaries();
for(S3ObjectSummary summary : summaries) {

String s3ObjectKey = summary.getKey();
System.out.println(s3ObjectKey);
}
if(objects.isTruncated()) {
objects = s3Client.listNextBatchOfObjects(objects);
}
else {
break;
}
}

} catch(AmazonServiceException e) {
// The call was transmitted successfully, but Amazon S3 couldn't process
// it, so it returned an error response.
log.error(MARKER, "Signatures download failed, Exception: {}", e.getStackTrace());
} catch(SdkClientException e) {
// Amazon S3 couldn't be contacted for a response, or the client
// couldn't parse the response from Amazon S3.
log.error(MARKER, "Signatures download failed, Exception: {}", e.getStackTrace());
}

}
}
18 changes: 18 additions & 0 deletions src/main/java/com/hedera/downloader/EventStreamDownloader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.hedera.downloader;

import com.hedera.configLoader.ConfigLoader;
import com.hedera.parser.EventStreamFileParser;

public class EventStreamDownloader extends Downloader {
public EventStreamDownloader(ConfigLoader configLoader) {
super(configLoader);
}

public static void main(String[] args) {
configLoader = new ConfigLoader("./config/config.json");
EventStreamDownloader downloader = new EventStreamDownloader(configLoader);
setupCloudConnection();
downloader.listObjects();

}
}
8 changes: 4 additions & 4 deletions src/main/java/com/hedera/parser/EventStreamFileParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.time.Instant;
import java.util.Arrays;
import java.util.List;
Expand All @@ -33,6 +34,7 @@ public class EventStreamFileParser {
static final byte STREAM_EVENT_START_WITH_VERSION = 0x5a;
static final byte commEventLast = 0x46;

private static Connection connect = null;

private static ConfigLoader configLoader;

Expand Down Expand Up @@ -221,12 +223,10 @@ static public void loadRecordFiles(List<String> fileNames) {
}

public static void main(String[] args) {
String pathName;

configLoader = new ConfigLoader("./config/config.json");

pathName = configLoader.getDefaultParseDir_EventStream();
log.info(MARKER, "EventStream files folder got from configuration file: {}", configLoader.getDefaultParseDir_EventStream());
String pathName = configLoader.getDefaultParseDir_EventStream();
log.info(MARKER, "EventStream files folder got from configuration file: {}", pathName);

if (pathName != null) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.Instant;
import java.util.Date;
import java.util.HashMap;
Expand Down Expand Up @@ -109,15 +108,15 @@ enum F_ENTITIES {
}

public static boolean start() {
connect = DatabaseUtilities.openDatabase(connect);
connect = DatabaseUtilities.getConnection();
if (connect == null) {
return false;
}
return true;
}
public static boolean finish() {
try {
connect = DatabaseUtilities.closeDatabase(connect);
DatabaseUtilities.closeDatabase();
return false;
} catch (SQLException e) {
log.error(LOGM_EXCEPTION, "Exception {}", e);
Expand Down

0 comments on commit 3945d99

Please sign in to comment.