Skip to content

Commit

Permalink
Merge pull request #90 from skyflowapi/release/23.7.2
Browse files Browse the repository at this point in the history
SK-897 Release/23.7.2
  • Loading branch information
yaswanth-pula-skyflow authored Aug 9, 2023
2 parents efc3323 + 378ce41 commit 64f1baa
Show file tree
Hide file tree
Showing 12 changed files with 644 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.skyflow</groupId>
<artifactId>skyflow-java</artifactId>
<version>1.9.0</version>
<version>1.10.0</version>
<packaging>jar</packaging>

<name>${project.groupId}:${project.artifactId}</name>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/skyflow/common/utils/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
public class Constants {

public static final String SDK_METRICS_HEADER_KEY = "sky-metadata";
public static final String SDK_VERSION = "1.9.0";
public static final String SDK_VERSION = "1.10.0";

}
13 changes: 13 additions & 0 deletions src/main/java/com/skyflow/common/utils/Validators.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,17 @@ public static void validateGetRequestRecord(GetRecordInput record) throws Skyflo
throw new SkyflowException(ErrorCode.MissingRecordColumnName);
}
}
public static void validateDeleteBySkyflowId(DeleteRecordInput deleteRecordInput) throws SkyflowException{
String table = deleteRecordInput.getTable();
String id = deleteRecordInput.getId();
if (table == null || table.trim().isEmpty()) {
LogUtil.printErrorLog(ErrorLogs.InvalidTable.getLog());
throw new SkyflowException(ErrorCode.InvalidTable);
}
if (id == null || id.trim().isEmpty()) {
LogUtil.printErrorLog(ErrorLogs.InvalidId.getLog());
throw new SkyflowException(ErrorCode.InvalidId);
}

}
}
13 changes: 13 additions & 0 deletions src/main/java/com/skyflow/entities/DeleteInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.skyflow.entities;

public class DeleteInput {
private DeleteRecordInput[] records;

public DeleteRecordInput[] getRecords() {
return records;
}

public void setRecords(DeleteRecordInput[] records) {
this.records = records;
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/skyflow/entities/DeleteOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.skyflow.entities;

public class DeleteOptions {
public DeleteOptions(){}
}
22 changes: 22 additions & 0 deletions src/main/java/com/skyflow/entities/DeleteRecordInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.skyflow.entities;

public class DeleteRecordInput {
private String id;
private String table;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getTable() {
return table;
}

public void setTable(String table) {
this.table = table;
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/skyflow/errors/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ public enum ErrorCode {
BearerThrownException(400, "getBearer() thrown exception"),
EmptyRecords(400, "Records cannot be empty"),
InvalidTable(400, "Table name is missing"),
InvalidId(400, "Skyflow id is missing"),
InvalidFields(400, "Fields are missing"),
InvalidSkyflowId(400, "Skyflow id are missing"),
InvalidToken(400, "Token is empty"),
InvalidDetokenizeInput(400, "Invalid Detokenize Input"),
InvalidInsertInput(400, "Invalid insert input"),
InvalidUpdateInput(400, "Invalid update input"),
InvalidDeleteInput(400, "Invalid delete input"),
InvalidGetByIdInput(400, "Invalid getById input"),
InvalidGetInput(400, "Invalid get input"),
MissingIdAndColumnName(400, "Provide either Ids or column name to get records."),
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/skyflow/logs/ErrorLogs.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public enum ErrorLogs {
InvalidTokenProvider("invalid TokenProvider. TokenProvider cannot be null"),
InvalidInsertInput("invalid insert input"),
InvalidUpdateInput("invalid update input"),
InvalidDeleteInput("invalid delete input"),
InvalidDetokenizeInput("invalid detokenize input"),
ResponseParsingError("Unable to parse response in %s1 method"),
ThreadInterruptedException("Thread was interrupted in %s1 method"),
Expand Down Expand Up @@ -38,6 +39,8 @@ public enum ErrorLogs {
BearerThrownException("getBearer() thrown exception "),
InvalidBearerToken("Invalid Bearer token"),
InvalidTable("Table name is missing"),
InvalidId("Skyflow id is missing"),

Server("Internal server error"),
ServerReturnedErrors("Server returned errors, check SkyflowException.getData() for more"),
InvalidUpsertOptionType("upsert options cannot be null, should be an non empty UpsertOption array."),
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/skyflow/logs/InfoLogs.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public enum InfoLogs {
ValidatingInvokeConnectionConfig("validating invoke connection configuration"),
InsertMethodCalled("insert method has triggered"),
UpdateMethodCalled("update method has triggered"),
deleteMethodCalled("delete method has triggered"),
ConstructInsertResponse("constructing insert response"),
ConstructUpdateResponse("constructing update response"),
DetokenizeMethodCalled("detokenize method has triggered"),
Expand Down
94 changes: 94 additions & 0 deletions src/main/java/com/skyflow/vault/DeleteBySkyflowId.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.skyflow.vault;

import com.skyflow.common.utils.Helpers;
import com.skyflow.common.utils.HttpUtility;
import com.skyflow.common.utils.LogUtil;
import com.skyflow.common.utils.Validators;
import com.skyflow.entities.DeleteOptions;
import com.skyflow.entities.DeleteRecordInput;
import com.skyflow.errors.ErrorCode;
import com.skyflow.errors.SkyflowException;
import com.skyflow.logs.ErrorLogs;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.io.IOException;
import java.net.URL;
import java.util.Map;
import java.util.concurrent.Callable;

public class DeleteBySkyflowId implements Callable<String> {
private final DeleteRecordInput recordInput;
private final String vaultID;
private final String vaultURL;
private final Map<String, String> headers;
private final DeleteOptions deleteOptions;

public DeleteBySkyflowId(DeleteRecordInput recordInput, String vaultID, String vaultURL, Map<String, String> headers, DeleteOptions deleteOptions) {
this.recordInput = recordInput;
this.vaultID = vaultID;
this.vaultURL = vaultURL;
this.headers = headers;
this.deleteOptions = deleteOptions;
}


@Override
public String call() throws SkyflowException {
String response = null;
try {
Validators.validateDeleteBySkyflowId(recordInput);
String url = vaultURL+ "/v1/vaults/"+ vaultID + "/" + recordInput.getTable() + "/" + recordInput.getId();
response = HttpUtility.sendRequest("DELETE", new URL(url), null,headers);
JSONObject formattedResponse = new JSONObject();
JSONArray formattedRecords = new JSONArray();

JSONObject responseRecords = ((JSONObject) (new JSONParser().parse(response)));
if (responseRecords != null && responseRecords.size() > 0) {
String id = (String) responseRecords.get("skyflow_id");
JSONObject formattedRecord = new JSONObject();
formattedRecord.put("skyflow_id", responseRecords.get("skyflow_id"));
formattedRecord.put("deleted", responseRecords.get("deleted"));
formattedRecords.add(formattedRecord);
}
formattedResponse.put("records", formattedRecords);
response = formattedResponse.toJSONString();

} catch (IOException e) {
LogUtil.printErrorLog(ErrorLogs.Server.getLog());
throw new SkyflowException(ErrorCode.Server, e);
} catch (ParseException e) {
LogUtil.printErrorLog(ErrorLogs.ResponseParsingError.getLog());
throw new SkyflowException(ErrorCode.ResponseParsingError, e);
} catch (SkyflowException e) {
response = constructDeleteByIdErrorObject(e, recordInput.getId());
}
return response;
}
private String constructDeleteByIdErrorObject(SkyflowException skyflowException, String id){
String deleteByIdResponse = null;
JSONObject finalResponseError = new JSONObject();
finalResponseError.put("id", id);
try{
JSONObject errorObject = (JSONObject) ((JSONObject) new JSONParser().parse(skyflowException.getMessage())).get("error");
if (errorObject != null) {
JSONObject responseError = new JSONObject();
responseError.put("code", errorObject.get("http_code"));
responseError.put("description", errorObject.get("message"));
finalResponseError.put("error", responseError);

deleteByIdResponse = finalResponseError.toString();
}

} catch (ParseException e){
JSONObject responseError = new JSONObject();
responseError.put("code", skyflowException.getCode());
responseError.put("description", skyflowException.getMessage());
finalResponseError.put("error", responseError);
deleteByIdResponse = finalResponseError.toString();
}
return deleteByIdResponse;
}
}
68 changes: 68 additions & 0 deletions src/main/java/com/skyflow/vault/Skyflow.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public JSONObject insert(JSONObject records) throws SkyflowException {
public JSONObject update(JSONObject records) throws SkyflowException {
return update(records, new UpdateOptions(true));
}
public JSONObject delete(JSONObject records) throws SkyflowException {
return delete(records, new DeleteOptions());
}

public JSONObject insert(JSONObject records, InsertOptions insertOptions) throws SkyflowException {
LogUtil.printInfoLog(InfoLogs.InsertMethodCalled.getLog());
Expand Down Expand Up @@ -344,6 +347,71 @@ public JSONObject update(JSONObject records, UpdateOptions updateOptions) throws

}

public JSONObject delete(JSONObject records, DeleteOptions deleteOptions) throws SkyflowException {
LogUtil.printInfoLog(InfoLogs.deleteMethodCalled.getLog());
Validators.validateConfiguration(configuration);
LogUtil.printInfoLog(Helpers.parameterizedString(InfoLogs.ValidatedSkyflowConfiguration.getLog(), "delete"));

JSONObject deleteResponse = new JSONObject();
JSONArray successRecordsArray = new JSONArray();
JSONArray errorRecordsArray = new JSONArray();

try {
DeleteInput deleteInput = new ObjectMapper().readValue(records.toString(), DeleteInput.class);
DeleteRecordInput[] recordInputs = deleteInput.getRecords();
if (recordInputs == null || recordInputs.length == 0) {
throw new SkyflowException(ErrorCode.EmptyRecords);
}
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", "Bearer " + TokenUtils.getBearerToken(configuration.getTokenProvider()));
headers.put(Constants.SDK_METRICS_HEADER_KEY, Helpers.getMetrics().toJSONString());
FutureTask[] futureTasks = new FutureTask[recordInputs.length];


for (int i = 0; i < recordInputs.length; i++) {
Callable<String> callable = new DeleteBySkyflowId(recordInputs[i], configuration.getVaultID(), configuration.getVaultURL(), headers, deleteOptions);
futureTasks[i] = new FutureTask(callable);
Thread t = new Thread(futureTasks[i]);
t.start();
}
for (FutureTask task : futureTasks) {
String taskData = (String) task.get();
JSONObject responseJson = (JSONObject) new JSONParser().parse(taskData);
if (responseJson.containsKey("error")) {
errorRecordsArray.add(responseJson);
} else if (responseJson.containsKey("records")) {
JSONArray resp = (JSONArray) new JSONParser().parse(responseJson.get("records").toString()) ;
successRecordsArray.add(resp.get(0));
}
}
if (errorRecordsArray.isEmpty()) {
deleteResponse.put("records", successRecordsArray);
} else if (successRecordsArray.isEmpty()) {
deleteResponse.put("errors", errorRecordsArray);
ErrorCode serverError = ErrorCode.ServerReturnedErrors;
throw new SkyflowException(serverError.getCode(), serverError.getDescription(), deleteResponse);
} else {
deleteResponse.put("records", successRecordsArray);
deleteResponse.put("errors", errorRecordsArray);
ErrorCode serverError = ErrorCode.ServerReturnedErrors;
throw new SkyflowException(serverError.getCode(), serverError.getDescription(), deleteResponse);
}

}catch (IOException e) {
LogUtil.printErrorLog(ErrorLogs.InvalidDeleteInput.getLog());
throw new SkyflowException(ErrorCode.InvalidDeleteInput, e);
} catch (InterruptedException e) {
LogUtil.printErrorLog(Helpers.parameterizedString(ErrorLogs.ThreadInterruptedException.getLog(), "deleteById"));
throw new SkyflowException(ErrorCode.ThreadInterruptedException, e);
} catch (ExecutionException e) {
LogUtil.printErrorLog(Helpers.parameterizedString(ErrorLogs.ThreadExecutionException.getLog(), "deleteById"));
throw new SkyflowException(ErrorCode.ThreadExecutionException, e);
} catch (ParseException e) {
LogUtil.printErrorLog(Helpers.parameterizedString(ErrorLogs.ResponseParsingError.getLog(), "deleteById"));
throw new SkyflowException(ErrorCode.ResponseParsingError, e);
}
return deleteResponse;
}
public JSONObject invokeConnection(JSONObject connectionConfig) throws SkyflowException {
LogUtil.printInfoLog(InfoLogs.InvokeConnectionCalled.getLog());
JSONObject connectionResponse;
Expand Down
Loading

0 comments on commit 64f1baa

Please sign in to comment.