Skip to content

Commit

Permalink
Alias type
Browse files Browse the repository at this point in the history
  • Loading branch information
gengliangwang committed Feb 26, 2020
1 parent c6c2c82 commit b0bb448
Showing 1 changed file with 9 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ public void clear() {
}
}

private static class NaturalKeys extends ConcurrentHashMap<Comparable<Object>, Boolean> {}

private static class InstanceList<T> {

/**
Expand Down Expand Up @@ -206,8 +208,7 @@ public void accept(Comparable<Object> key, T value) {
private final ConcurrentMap<Comparable<Object>, T> data;
private final String naturalParentIndexName;
// A mapping from parent to the natural keys of its children.
private final ConcurrentMap<Comparable<Object>, ConcurrentMap<Comparable<Object>, Boolean>>
parentToChildrenMap;
private final ConcurrentMap<Comparable<Object>, NaturalKeys> parentToChildrenMap;

private InstanceList(Class<?> klass) {
this.ti = new KVTypeInfo(klass);
Expand All @@ -225,8 +226,7 @@ int countingRemoveAllByIndexValues(String index, Collection<?> indexValues) {
if (!naturalParentIndexName.isEmpty() && naturalParentIndexName.equals(index)) {
for (Object indexValue : indexValues) {
Comparable<Object> parentKey = asKey(indexValue);
ConcurrentMap<Comparable<Object>, Boolean> children =
parentToChildrenMap.computeIfAbsent(parentKey, k -> new ConcurrentHashMap<>());
NaturalKeys children = parentToChildrenMap.computeIfAbsent(parentKey, k -> new NaturalKeys());
int count = 0;
for (Object key : children.keySet()) {
data.remove(asKey(key));
Expand All @@ -253,16 +253,15 @@ public void put(T value) throws Exception {
data.put(asKey(naturalKey.get(value)), value);
if (!naturalParentIndexName.isEmpty()) {
Comparable<Object> parentKey = asKey(getIndexAccessor(naturalParentIndexName).get(value));
ConcurrentMap<Comparable<Object>, Boolean> children =
parentToChildrenMap.computeIfAbsent(parentKey, k -> new ConcurrentHashMap<>());
NaturalKeys children = parentToChildrenMap.computeIfAbsent(parentKey, k -> new NaturalKeys());
children.put(asKey(naturalKey.get(value)), true);
}
}

public void delete(Object key) {
data.remove(asKey(key));
if (!naturalParentIndexName.isEmpty()) {
for (ConcurrentMap<Comparable<Object>, Boolean> v : parentToChildrenMap.values()) {
for (NaturalKeys v : parentToChildrenMap.values()) {
if (v.remove(asKey(key))) {
break;
}
Expand Down Expand Up @@ -310,16 +309,14 @@ private static class InMemoryView<T> extends KVStoreView<T> {
private final ConcurrentMap<Comparable<Object>, T> data;
private final KVTypeInfo ti;
private final KVTypeInfo.Accessor natural;
private final ConcurrentMap<Comparable<Object>, ConcurrentMap<Comparable<Object>, Boolean>>
parentToChildrenMap;
private final ConcurrentMap<Comparable<Object>, NaturalKeys> parentToChildrenMap;
private final String naturalParentIndexName;

InMemoryView(
ConcurrentMap<Comparable<Object>, T> data,
KVTypeInfo ti,
String naturalParentIndexName,
ConcurrentMap<Comparable<Object>, ConcurrentMap<Comparable<Object>, Boolean>>
parentToChildrenMap) {
ConcurrentMap<Comparable<Object>, NaturalKeys> parentToChildrenMap) {
this.data = data;
this.ti = ti;
this.natural = ti != null ? ti.getAccessor(KVIndex.NATURAL_INDEX_NAME) : null;
Expand Down Expand Up @@ -369,8 +366,7 @@ private List<T> copyElements() {
Comparable<Object> parentKey = asKey(parent);
if (!naturalParentIndexName.isEmpty() &&
naturalParentIndexName.equals(ti.getParentIndexName(index))) {
ConcurrentMap<Comparable<Object>, Boolean> children =
parentToChildrenMap.computeIfAbsent(parentKey, k -> new ConcurrentHashMap<>());
NaturalKeys children = parentToChildrenMap.computeIfAbsent(parentKey, k -> new NaturalKeys());
ArrayList<T> elements = new ArrayList<>();
for (Comparable<Object> naturalKey : children.keySet()) {
data.computeIfPresent(naturalKey, (k, v) -> {
Expand Down

0 comments on commit b0bb448

Please sign in to comment.