Skip to content

Commit

Permalink
Fix issue #21
Browse files Browse the repository at this point in the history
  • Loading branch information
KotlinFactory committed Feb 20, 2020
1 parent 0482238 commit 6aeaa00
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
50 changes: 36 additions & 14 deletions src/main/java/de/leonhard/storage/internal/FileData.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import de.leonhard.storage.internal.settings.DataType;
import de.leonhard.storage.util.JsonUtils;
import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

import java.util.HashMap;
Expand Down Expand Up @@ -78,17 +79,17 @@ private Object get(final Map<String, Object> map, final String[] key, final int
public synchronized void insert(final String key, final Object value) {
final String[] parts = key.split("\\.");
localMap.put(parts[0],
localMap.containsKey(parts[0]) && localMap.get(parts[0]) instanceof Map
? insert((Map<String, Object>) localMap.get(parts[0]), parts, value, 1)
: insert(new HashMap<>(), parts, value, 1));
localMap.containsKey(parts[0]) && localMap.get(parts[0]) instanceof Map
? insert((Map<String, Object>) localMap.get(parts[0]), parts, value, 1)
: insert(new HashMap<>(), parts, value, 1));
}

private Object insert(final Map<String, Object> map, final String[] key, final Object value, final int id) {
if (id < key.length) {
final Map<String, Object> tempMap = new HashMap<>(map);
final Map<String, Object> childMap = map.containsKey(key[id]) && map.get(key[id]) instanceof Map
? (Map<String, Object>) map.get(key[id])
: new HashMap<>();
? (Map<String, Object>) map.get(key[id])
: new HashMap<>();
tempMap.put(key[id], insert(childMap, key, value, id + 1));
return tempMap;
} else {
Expand Down Expand Up @@ -128,23 +129,44 @@ private boolean containsKey(final Map<String, Object> map, final String[] key, f
public synchronized void remove(final String key) {
if (containsKey(key)) {
final String[] parts = key.split("\\.");
remove(localMap, parts, 0);
remove(parts);
}
}

private synchronized void remove(final Map<String, Object> map, final String[] key, final int id) {
Map tempMap = map;
for (int i = 0; i < key.length - (1 + id); i++) {
if (tempMap.containsKey(key[i]) && tempMap.get(key[i]) instanceof Map) {
tempMap = (Map) tempMap.get(key[i]);
private void remove(final @NotNull String[] key) {
if (key.length == 1) {
this.localMap.remove(key[0]);
} else {
final Object tempValue = this.localMap.get(key[0]);
if (tempValue instanceof Map) {
//noinspection unchecked
this.localMap.put(key[0], this.remove((Map) tempValue, key, 1));
if (((Map) this.localMap.get(key[0])).isEmpty()) {
this.localMap.remove(key[0]);
}
}
}
if (tempMap.keySet().size() <= 1) {
map.remove(key[key.length - (1 + id)]);
remove(map, key, id + 1);
}

private Map<String, Object> remove(final Map<String, Object> map,
final String[] key,
final int keyIndex) {
if (keyIndex < key.length - 1) {
final Object tempValue = map.get(key[keyIndex]);
if (tempValue instanceof Map) {
//noinspection unchecked
map.put(key[keyIndex], this.remove((Map) tempValue, key, keyIndex + 1));
if (((Map) map.get(key[keyIndex])).isEmpty()) {
map.remove(key[keyIndex]);
}
}
} else {
map.remove(key[keyIndex]);
}
return map;
}


/**
* get the keySet of a single layer of the map.
*
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/de/leonhard/storage/internal/FlatFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ public Set<String> keySet(final String key) {
@Override
@Synchronized
public void remove(final String key) {
reloadIfNeeded();
fileData.remove(key);
write();
}

// ----------------------------------------------------------------------------------------------------
Expand Down

0 comments on commit 6aeaa00

Please sign in to comment.