Skip to content

Commit

Permalink
Ab2d-6039/Fix performance defect (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
smirnovaae authored Apr 3, 2024
1 parent 79da7be commit 720593a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 47 deletions.
5 changes: 2 additions & 3 deletions optout/src/main/java/gov/cms/ab2d/optout/OptOutConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ public class OptOutConstants {
public static final String LINE_SEPARATOR = System.getProperty("line.separator");
public static final String CONF_FILE_NAME = "#EFT.ON.AB2D.NGD.CONF.";
public static final String CONF_FILE_NAME_PATTERN = "'D'yyMMdd.'T'HHmmsss";
public static final String UPDATE_STATEMENT = "UPDATE public.coverage\n" +
public static final String UPDATE_STATEMENT = "UPDATE public.current_mbi\n" +
"SET opt_out_flag = ?, effective_date = current_timestamp\n" +
"WHERE current_mbi = ? OR historic_mbis iLike CONCAT( '%',?,'%')";

"WHERE mbi = ?";

private OptOutConstants() {
}
Expand Down
53 changes: 22 additions & 31 deletions optout/src/main/java/gov/cms/ab2d/optout/OptOutProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,33 @@
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.List;

import static gov.cms.ab2d.optout.OptOutConstants.*;

public class OptOutProcessor {
private final LambdaLogger logger;
public SortedMap<Long, OptOutInformation> optOutInformationMap;
public List<OptOutInformation> optOutInformationList;
public boolean isRejected;

OptOutParameterStore parameterStore;

public OptOutProcessor(LambdaLogger logger) {
this.logger = logger;
this.optOutInformationMap = new TreeMap<>();
this.optOutInformationList = new ArrayList<>();
isRejected = false;
parameterStore = OptOutParameterStore.getParameterStore();
}

public void process(String fileName, String bfdBucket, String endpoint) throws URISyntaxException {
var optOutS3 = new OptOutS3(getS3Client(endpoint), fileName, bfdBucket, logger);

processFileFromS3(optOutS3.openFileS3());
updateOptOut();
var name = optOutS3.createResponseOptOutFile(createResponseContent());
logger.log("File with name " + name + " was uploaded to bucket: " + bfdBucket);
if (!isRejected)
Expand Down Expand Up @@ -76,17 +74,14 @@ public S3Client getS3Client(String endpoint) throws URISyntaxException {

public void processFileFromS3(BufferedReader reader) {
String line;
var lineNumber = 0L;
try (var dbConnection = DriverManager.getConnection(parameterStore.getDbHost(), parameterStore.getDbUser(), parameterStore.getDbPassword())){
try {
while ((line = reader.readLine()) != null) {
if (!line.startsWith(HEADER_RESP) && !line.startsWith(TRAILER_RESP)) {
var optOutInformation = createOptOutInformation(line);
optOutInformationMap.put(lineNumber, optOutInformation);
updateOptOut(lineNumber, optOutInformation, dbConnection);
optOutInformationList.add(optOutInformation);
}
lineNumber++;
}
} catch (IOException | SQLException ex) {
} catch (IOException ex) {
logger.log("An error occurred during file processing. " + ex.getMessage());
}
}
Expand All @@ -97,12 +92,17 @@ public OptOutInformation createOptOutInformation(String information) {
return new OptOutInformation(mbi, optOutFlag);
}

public void updateOptOut(long lineNumber, OptOutInformation optOutInformation, Connection dbConnection) {
try (var statement = dbConnection.prepareStatement(UPDATE_STATEMENT)) {
prepareInsert(optOutInformation, statement);
statement.execute();
public void updateOptOut() {
try (var dbConnection = DriverManager.getConnection(parameterStore.getDbHost(), parameterStore.getDbUser(), parameterStore.getDbPassword())){
var statement = dbConnection.prepareStatement(UPDATE_STATEMENT);
for (var optOutInformation : optOutInformationList) {
statement.setBoolean(1, optOutInformation.getOptOutFlag());
statement.setString(2, optOutInformation.getMbi());
statement.addBatch();
}
statement.executeBatch();
} catch (SQLException ex) {
logger.log("There is an insertion error on the line " + lineNumber);
logger.log("There is an insertion error " + ex.getMessage());
isRejected = true;
}
}
Expand All @@ -114,16 +114,14 @@ public String createResponseContent() {
var recordStatus = getRecordStatus();
var effectiveDate = getEffectiveDate(date);

for (var optOutResult : optOutInformationMap.entrySet()) {
var info = optOutResult.getValue();

responseContent.append(info.getMbi())
for (var optOutResult : optOutInformationList) {
responseContent.append(optOutResult.getMbi())
.append(effectiveDate)
.append((info.getOptOutFlag()) ? 'Y' : 'N')
.append((optOutResult.getOptOutFlag()) ? 'Y' : 'N')
.append(recordStatus)
.append(LINE_SEPARATOR);
}
responseContent.append(AB2D_TRAILER_CONF).append(date).append(String.format("%010d", optOutInformationMap.size()));
responseContent.append(AB2D_TRAILER_CONF).append(date).append(String.format("%010d", optOutInformationList.size()));

return responseContent.toString();
}
Expand All @@ -136,11 +134,4 @@ public String getEffectiveDate(String date) {
return (isRejected) ? " ".repeat(EFFECTIVE_DATE_LENGTH) : date;
}

private static void prepareInsert(OptOutInformation optOut, PreparedStatement statement) throws SQLException {
statement.setBoolean(1, optOut.getOptOutFlag());
statement.setString(2, optOut.getMbi());
statement.setString(3, optOut.getMbi());
statement.addBatch();
}

}
2 changes: 2 additions & 0 deletions optout/src/main/java/gov/cms/ab2d/optout/OptOutS3.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public String createResponseOptOutFile(String responseContent) {
.build();

s3Client.putObject(objectRequest, RequestBody.fromString(responseContent));
logger.log("Response file done");
return key;
} catch (AmazonS3Exception ex) {
var errorMessage = "Response OptOut file cannot be created. ";
Expand All @@ -85,6 +86,7 @@ public void deleteFileFromS3() {
.build();

s3Client.deleteObject(request);
logger.log("delete file done");
} catch (SdkClientException ex) {
logger.log(ex.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ void afterEach() {
void processTest() throws URISyntaxException {
optOutProcessing.isRejected = false;
optOutProcessing.process(TEST_FILE_NAME, TEST_BFD_BUCKET_NAME, TEST_ENDPOINT);
assertEquals(7, optOutProcessing.optOutInformationMap.size());
assertEquals(7, optOutProcessing.optOutInformationList.size());
}

@Test
void processEmptyFileTest() throws IOException, URISyntaxException {
var emptyFileName = "emptyDummy.txt";
S3MockAPIExtension.createFile(Files.readString(Paths.get("src/test/resources/" + emptyFileName), StandardCharsets.UTF_8), emptyFileName);
optOutProcessing.process(emptyFileName, TEST_BFD_BUCKET_NAME, TEST_ENDPOINT);
assertEquals(0, optOutProcessing.optOutInformationMap.size());
assertEquals(0, optOutProcessing.optOutInformationList.size());
S3MockAPIExtension.deleteFile(emptyFileName);
}

Expand All @@ -92,7 +92,7 @@ void createFalseOptOutInformationTest() {

@Test
void createAcceptedResponseTest() {
optOutProcessing.optOutInformationMap.put(1L, new OptOutInformation(MBI, true));
optOutProcessing.optOutInformationList.add(new OptOutInformation(MBI, true));
var expectedLine = MBI + DATE + "Y" + RecordStatus.ACCEPTED;
var expectedText = AB2D_HEADER_CONF + DATE + LINE_SEPARATOR
+ expectedLine + LINE_SEPARATOR
Expand All @@ -103,7 +103,7 @@ void createAcceptedResponseTest() {
@Test
void createRejectedResponseTest() {
optOutProcessing.isRejected = true;
optOutProcessing.optOutInformationMap.put(1L, new OptOutInformation(MBI, false));
optOutProcessing.optOutInformationList.add(new OptOutInformation(MBI, false));
var expectedLine = MBI + " " + "N" + RecordStatus.REJECTED;
var expectedText = AB2D_HEADER_CONF + DATE + LINE_SEPARATOR
+ expectedLine + LINE_SEPARATOR
Expand All @@ -113,16 +113,14 @@ void createRejectedResponseTest() {

@Test
void updateOptOutTest() {
var optOutInformation = optOutProcessing.createOptOutInformation(validLine('Y'));
optOutProcessing.updateOptOut(1L, optOutInformation, dbConnection);
optOutProcessing.updateOptOut();
assertFalse(optOutProcessing.isRejected);
}

@Test
void updateOptOutExceptionTest() throws SQLException {
var optOutInformation = optOutProcessing.createOptOutInformation(validLine('Y'));
when(dbConnection.prepareStatement(anyString())).thenThrow(SQLException.class);
optOutProcessing.updateOptOut(1L, optOutInformation, dbConnection);
optOutProcessing.updateOptOut();
// Insertion error exists
assertTrue(optOutProcessing.isRejected);
assertTrue(S3MockAPIExtension.isObjectExists(TEST_FILE_NAME));
Expand Down
10 changes: 5 additions & 5 deletions optout/src/test/resources/optOutDummy.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
HDR_BENEDATARSP20240123
DUMMY000001N
DUMMY000002N
DUMMY000003Y
DUMMY000004N
DUMMY000005N
1S00E00JG37N
7SP1D00AA00N
2SY1D00AA00Y
7SF9C00AA00N
6SF9C00AA00N
DUMMY000006Y
DUMMY000007N
TRL_BENEDATARSP202401230000000007

0 comments on commit 720593a

Please sign in to comment.