use of com.hedera.mirror.importer.exception.InvalidDatasetException in project hedera-mirror-node by hashgraph.
the class CsvBalanceFileReader method read.
@Override
public AccountBalanceFile read(StreamFileData streamFileData) {
MessageDigest messageDigest = DigestUtils.getSha384Digest();
int bufferSize = balanceParserProperties.getFileBufferSize();
try (InputStream inputStream = new DigestInputStream(streamFileData.getInputStream(), messageDigest);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, CHARSET), bufferSize)) {
long consensusTimestamp = parseConsensusTimestamp(reader);
AtomicLong count = new AtomicLong(0L);
List<AccountBalance> items = new ArrayList<>();
AccountBalanceFile accountBalanceFile = new AccountBalanceFile();
accountBalanceFile.setBytes(streamFileData.getBytes());
accountBalanceFile.setConsensusTimestamp(consensusTimestamp);
accountBalanceFile.setLoadStart(Instant.now().getEpochSecond());
accountBalanceFile.setName(streamFileData.getFilename());
reader.lines().map(line -> {
try {
AccountBalance accountBalance = parser.parse(line, consensusTimestamp);
count.incrementAndGet();
return accountBalance;
} catch (InvalidDatasetException ex) {
log.error(ex);
return null;
}
}).filter(Objects::nonNull).forEachOrdered(items::add);
accountBalanceFile.setCount(count.get());
accountBalanceFile.setFileHash(DomainUtils.bytesToHex(messageDigest.digest()));
accountBalanceFile.setItems(Flux.fromIterable(items));
return accountBalanceFile;
} catch (IOException ex) {
throw new InvalidDatasetException("Error reading account balance file", ex);
}
}
use of com.hedera.mirror.importer.exception.InvalidDatasetException in project hedera-mirror-node by hashgraph.
the class AccountBalanceLineParserV1 method parse.
/**
* Parses an account balance line to extract shard, realm, account, and balance. If the shard matches
* systemShardNum, creates and returns an {@code AccountBalance} entity object. The account balance line should be
* in the format of "shard,realm,account,balance"
*
* @param line The account balance line
* @param consensusTimestamp The consensus timestamp of the account balance line
* @return {@code AccountBalance} entity object
* @throws InvalidDatasetException if the line is malformed or the shard does not match {@code systemShardNum}
*/
@Override
public AccountBalance parse(String line, long consensusTimestamp) {
try {
if (line == null) {
throw new InvalidDatasetException("Null line cannot be parsed");
}
List<String> parts = SPLITTER.splitToList(line);
if (parts.size() != 4) {
throw new InvalidDatasetException("Invalid account balance line: " + line);
}
long shardNum = Long.parseLong(parts.get(0));
int realmNum = Integer.parseInt(parts.get(1));
int accountNum = Integer.parseInt(parts.get(2));
long balance = Long.parseLong(parts.get(3));
if (shardNum < 0 || realmNum < 0 || accountNum < 0 || balance < 0) {
throw new InvalidDatasetException("Invalid account balance line: " + line);
}
if (shardNum != mirrorProperties.getShard()) {
throw new InvalidDatasetException(String.format("Invalid account balance line: %s. Expect " + "shard (%d), got shard (%d)", line, mirrorProperties.getShard(), shardNum));
}
return new AccountBalance(balance, Collections.emptyList(), new AccountBalance.Id(consensusTimestamp, EntityId.of(shardNum, realmNum, accountNum, EntityType.ACCOUNT)));
} catch (NumberFormatException ex) {
throw new InvalidDatasetException("Invalid account balance line: " + line, ex);
}
}
use of com.hedera.mirror.importer.exception.InvalidDatasetException in project hedera-mirror-node by hashgraph.
the class AccountBalanceLineParserV2 method parse.
/**
* Parses an account balance line to extract shard, realm, account, balance, and token balances. If the shard
* matches systemShardNum, creates and returns an {@code AccountBalance} entity object. The account balance line
* should be in the format of "shard,realm,account,balance"
*
* @param line The account balance line
* @param consensusTimestamp The consensus timestamp of the account balance line
* @return {@code AccountBalance} entity object
* @throws InvalidDatasetException if the line is malformed or the shard does not match {@code systemShardNum}
*/
@Override
public AccountBalance parse(String line, long consensusTimestamp) {
try {
if (line == null) {
throw new InvalidDatasetException("Null line cannot be parsed");
}
List<String> parts = SPLITTER.splitToList(line);
boolean hasTokenBalance;
if (parts.size() == 5) {
hasTokenBalance = true;
} else if (parts.size() == 4) {
hasTokenBalance = false;
} else {
throw new InvalidDatasetException("Invalid account balance line: " + line);
}
long shardNum = Long.parseLong(parts.get(0));
int realmNum = Integer.parseInt(parts.get(1));
int accountNum = Integer.parseInt(parts.get(2));
long balance = Long.parseLong(parts.get(3));
if (shardNum < 0 || realmNum < 0 || accountNum < 0 || balance < 0) {
throw new InvalidDatasetException("Invalid account balance line: " + line);
}
if (shardNum != mirrorProperties.getShard()) {
throw new InvalidDatasetException(String.format("Invalid account balance line: %s. Expect " + "shard (%d), got shard (%d)", line, mirrorProperties.getShard(), shardNum));
}
EntityId accountId = EntityId.of(shardNum, realmNum, accountNum, EntityType.ACCOUNT);
List<TokenBalance> tokenBalances = hasTokenBalance ? parseTokenBalanceList(parts.get(4), consensusTimestamp, accountId) : Collections.emptyList();
return new AccountBalance(balance, tokenBalances, new AccountBalance.Id(consensusTimestamp, accountId));
} catch (NumberFormatException | InvalidProtocolBufferException ex) {
throw new InvalidDatasetException("Invalid account balance line: " + line, ex);
}
}
Aggregations