Skip to content

Commit

Permalink
ref[storage]: rename primary key
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Sep 21, 2023
1 parent 82d299e commit 1337857
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 37 deletions.
4 changes: 2 additions & 2 deletions storage/src/main/java/com/zfoo/storage/StorageContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public static IStorageManager getStorageManager() {
return instance.storageManager;
}

public static <V, K> V get(Class<V> clazz, K keyId) {
return instance.storageManager.getStorage(clazz).get(keyId);
public static <V, K> V get(Class<V> clazz, K id) {
return instance.storageManager.getStorage(clazz).get(id);
}

public static <V> List<V> getList(Class<V> clazz) {
Expand Down
13 changes: 6 additions & 7 deletions storage/src/main/java/com/zfoo/storage/manager/StorageInt.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author godotg
Expand All @@ -42,18 +41,18 @@ public StorageInt(StorageObject<K, V> storage) {
}

@Override
public boolean contain(K key) {
return contain((int) key);
public boolean contain(K id) {
return contain((int) id);
}

@Override
public boolean contain(int key) {
return dataMap.containsKey(key);
public boolean contain(int id) {
return dataMap.containsKey(id);
}

@Override
public boolean contain(long key) {
return contain((int) key);
public boolean contain(long id) {
return contain((int) id);
}

@Override
Expand Down
13 changes: 6 additions & 7 deletions storage/src/main/java/com/zfoo/storage/manager/StorageLong.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author godotg
Expand All @@ -42,18 +41,18 @@ public StorageLong(StorageObject<K, V> storage) {
}

@Override
public boolean contain(K key) {
return contain((long) key);
public boolean contain(K id) {
return contain((long) id);
}

@Override
public boolean contain(int key) {
return contain((long) key);
public boolean contain(int id) {
return contain((long) id);
}

@Override
public boolean contain(long key) {
return dataMap.containsKey(key);
public boolean contain(long id) {
return dataMap.containsKey(id);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,9 @@ public <T, INDEX> List<T> getIndexes(Class<T> clazz, Func1<T, INDEX> func, INDEX
return storage.getIndexes(func, index);
}

public <T, K> T get(Class<T> clazz, K key) {
public <T, K> T get(Class<T> clazz, K id) {
var storage = getStorage(clazz);
return storage.get(key);
return storage.get(id);
}

@Override
Expand Down
32 changes: 16 additions & 16 deletions storage/src/main/java/com/zfoo/storage/manager/StorageObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,22 @@ public class StorageObject<K, V> implements IStorage<K, V> {
}

@Override
public boolean contain(K key) {
return dataMap.containsKey(key);
public boolean contain(K id) {
return dataMap.containsKey(id);
}

@Override
public boolean contain(int key) {
public boolean contain(int id) {
@SuppressWarnings("unchecked")
var k = (K) Integer.valueOf(key);
return contain(k);
var key = (K) Integer.valueOf(id);
return contain(key);
}

@Override
public boolean contain(long key) {
public boolean contain(long id) {
@SuppressWarnings("unchecked")
var k = (K) Long.valueOf(key);
return contain(k);
var key = (K) Long.valueOf(id);
return contain(key);
}

@Override
Expand Down Expand Up @@ -181,31 +181,31 @@ public int size() {

public V put(Object value) {
@SuppressWarnings("unchecked")
var key = (K) ReflectionUtils.getField(idDef.getField(), value);
var id = (K) ReflectionUtils.getField(idDef.getField(), value);

if (key == null) {
if (id == null) {
throw new RuntimeException("There is an item with an unconfigured id in the static resource");
}
if (dataMap.containsKey(key)) {
throw new RuntimeException(StringUtils.format("Duplicate [id:{}] of static resource [resource:{}]", key, clazz.getSimpleName()));
if (dataMap.containsKey(id)) {
throw new RuntimeException(StringUtils.format("Duplicate [id:{}] of static resource [resource:{}]", id, clazz.getSimpleName()));
}
// 添加资源
@SuppressWarnings("unchecked")
var v = (V) value;
var result = dataMap.put(key, v);
var result = dataMap.put(id, v);
// 添加索引
for (var def : indexDefMap.values()) {
// 使用field的名称作为索引的名称
var indexKey = def.getField().getName();
var indexValue = ReflectionUtils.getField(def.getField(), v);
if (def.isUnique()) {// 唯一索引
var index = uniqueIndexMap.computeIfAbsent(indexKey, k -> new HashMap<>(12));
var index = uniqueIndexMap.computeIfAbsent(indexKey, it -> HashMap.newHashMap(12));
if (index.put(indexValue, v) != null) {
throw new RuntimeException(StringUtils.format("Duplicate unique index [index:{}][value:{}] of static resource [class:{}]", indexKey, indexValue, clazz.getName()));
}
} else {// 不是唯一索引
var index = indexMap.computeIfAbsent(indexKey, k -> new HashMap<>(32));
var list = index.computeIfAbsent(indexValue, k -> new ArrayList<V>());
var index = indexMap.computeIfAbsent(indexKey, it -> HashMap.newHashMap(32));
var list = index.computeIfAbsent(indexValue, it -> new ArrayList<V>());
list.add(v);
}
}
Expand Down
6 changes: 3 additions & 3 deletions storage/src/main/java/com/zfoo/storage/model/IStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
*/
public interface IStorage<K, V> {

boolean contain(K key);
boolean contain(K id);

boolean contain(int key);
boolean contain(int id);

boolean contain(long key);
boolean contain(long id);

V get(K id);

Expand Down

0 comments on commit 1337857

Please sign in to comment.