Skip to content

Commit

Permalink
ref[storage]: use the JDK new API
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Sep 21, 2023
1 parent ba7beb6 commit 93303cb
Showing 1 changed file with 45 additions and 43 deletions.
88 changes: 45 additions & 43 deletions storage/src/main/java/com/zfoo/storage/manager/StorageObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,29 @@ public class StorageObject<K, V> implements IStorage<K, V> {


public static StorageObject<?, ?> parse(InputStream inputStream, Class<?> resourceClazz, String suffix) {
var storage = new StorageObject<>();
storage.clazz = resourceClazz;
var idDef = IdDef.valueOf(resourceClazz);
storage.idDef = idDef;
storage.indexDefMap = Collections.unmodifiableMap(IndexDef.createResourceIndexes(resourceClazz));

try {
var storage = new StorageObject<>();
storage.clazz = resourceClazz;
var idDef = IdDef.valueOf(resourceClazz);
storage.idDef = idDef;
storage.indexDefMap = Collections.unmodifiableMap(IndexDef.createResourceIndexes(resourceClazz));
var list = ResourceInterpreter.read(inputStream, resourceClazz, suffix);
for (var object : list) {
storage.put(object);
}
var idType = idDef.getField().getType();
if (idType == int.class || idType == Integer.class) {
return new StorageInt<>(storage);
} else if (idType == long.class || idType == Long.class) {
return new StorageLong<>(storage);
} else {
return storage;
}
storage.putAll(list);
} catch (Throwable e) {
throw new RuntimeException(e.getMessage(), e);
} finally {
IOUtils.closeIO(inputStream);
}

var idType = idDef.getField().getType();
if (idType == int.class || idType == Integer.class) {
return new StorageInt<>(storage);
} else if (idType == long.class || idType == Long.class) {
return new StorageLong<>(storage);
} else {
return storage;
}
}

@Override
Expand Down Expand Up @@ -179,36 +179,38 @@ public int size() {
return dataMap.size();
}

public V put(Object value) {
@SuppressWarnings("unchecked")
var id = (K) ReflectionUtils.getField(idDef.getField(), value);
public void putAll(List<?> values) {
for (var value: values) {
@SuppressWarnings("unchecked")
var id = (K) ReflectionUtils.getField(idDef.getField(), value);

if (id == null) {
throw new RuntimeException("There is an item with an unconfigured id in the static resource");
}
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(id, v);
// 添加索引
for (var def : indexDefMap.values()) {
// 使用field的名称作为索引的名称
var indexKey = def.getField().getName();
var indexValue = ReflectionUtils.getField(def.getField(), v);
if (def.isUnique()) {
var uniqueIndex = uniqueIndexMap.computeIfAbsent(indexKey, it -> new HashMap<>(32));
if (uniqueIndex.put(indexValue, v) != null) {
throw new RuntimeException(StringUtils.format("Duplicate unique index [index:{}][value:{}] of static resource [class:{}]", indexKey, indexValue, clazz.getName()));
if (id == null) {
throw new RuntimeException("There is an item with an unconfigured id in the static resource");
}
if (dataMap.containsKey(id)) {
throw new RuntimeException(StringUtils.format("Duplicate [id:{}] of static resource [resource:{}]", id, clazz.getSimpleName()));
}
// 添加资源
@SuppressWarnings("unchecked")
var v = (V) value;
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 uniqueIndex = uniqueIndexMap.computeIfAbsent(indexKey, it -> HashMap.newHashMap(values.size()));
if (uniqueIndex.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, it -> HashMap.newHashMap(values.size()));
var list = index.computeIfAbsent(indexValue, it -> new ArrayList<V>());
list.add(v);
}
} else {
var index = indexMap.computeIfAbsent(indexKey, it -> new HashMap<>(32));
var list = index.computeIfAbsent(indexValue, it -> new ArrayList<V>());
list.add(v);
}
}
return result;
}

}

0 comments on commit 93303cb

Please sign in to comment.