Skip to content

Commit

Permalink
Fix major issue where object graphs would use dangling db refs
Browse files Browse the repository at this point in the history
  • Loading branch information
ericvergnaud committed Mar 10, 2020
1 parent 0e6f605 commit 2c55f54
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions Server/src/main/java/prompto/server/StoreServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,12 @@ private boolean isRecordUpdate(JsonNode record) {


private IStorable populateExistingStorable(JsonNode record, Map<String, byte[]> parts, Map<Long, Object> updatedDbIds) {
List<String> categories = readJsonCategories(record);
IStorable storable = DataStore.getInstance().newStorable(categories, null);
// use dbId received from client
Object rawDbId = readJsonValue(record.get(IStore.dbIdName), parts, updatedDbIds);
Object dbId = DataStore.getInstance().convertToDbId(rawDbId);
// populate storable
List<String> categories = readJsonCategories(record);
IStorable storable = DataStore.getInstance().newStorable(categories, null);
Iterator<String> fieldNames = record.fieldNames();
while(fieldNames.hasNext()) {
String fieldName = fieldNames.next();
Expand All @@ -142,17 +144,20 @@ private IStorable populateExistingStorable(JsonNode record, Map<String, byte[]>
}

private IStorable populateNewStorable(JsonNode record, Map<String, byte[]> parts, Map<Long, Object> updatedDbIds) {
// use potentially existing dbId allocated by a dbRef in another storable
Long tempDbId = record.get(IStore.dbIdName).get("tempDbId").asLong();
Object dbId = updatedDbIds.getOrDefault(tempDbId, null);
List<String> categories = readJsonCategories(record);
IStorable storable = DataStore.getInstance().newStorable(categories, dbId -> updatedDbIds.put(tempDbId, dbId));
IStorable storable = DataStore.getInstance().newStorable(categories, newDbId -> updatedDbIds.put(tempDbId, newDbId));
Iterator<String> fieldNames = record.fieldNames();
while(fieldNames.hasNext()) {
String fieldName = fieldNames.next();
Object value = readJsonValue(record.get(fieldName), parts, updatedDbIds);
if(IStore.dbIdName.equals(fieldName))
continue;
else
storable.setData(fieldName, value);
else {
Object value = readJsonValue(record.get(fieldName), parts, updatedDbIds);
storable.setData(fieldName, value, ()->dbId);
}
}
return storable;
}
Expand Down

0 comments on commit 2c55f54

Please sign in to comment.