mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-08-18 08:25:32 +00:00
ref[storage]: rename primary key
This commit is contained in:
@@ -52,8 +52,8 @@ public class StorageContext implements ApplicationListener<ApplicationContextEve
|
||||
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) {
|
||||
|
||||
@@ -18,7 +18,6 @@ import io.netty.util.collection.IntObjectHashMap;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
@@ -42,18 +41,18 @@ public class StorageInt<K, V> extends StorageObject<K, V> {
|
||||
}
|
||||
|
||||
@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
|
||||
|
||||
@@ -18,7 +18,6 @@ import io.netty.util.collection.LongObjectHashMap;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
@@ -42,18 +41,18 @@ public class StorageLong<K, V> extends StorageObject<K, V> {
|
||||
}
|
||||
|
||||
@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
|
||||
|
||||
@@ -204,9 +204,9 @@ public class StorageManager implements IStorageManager {
|
||||
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
|
||||
|
||||
@@ -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
|
||||
@@ -181,31 +181,31 @@ public class StorageObject<K, V> implements IStorage<K, V> {
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,11 +24,11 @@ import java.util.Map;
|
||||
*/
|
||||
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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user