package foundation.data.entity;
|
|
import java.math.BigDecimal;
|
import java.util.HashMap;
|
import java.util.Map;
|
|
import foundation.data.meta.field.Field;
|
import foundation.data.meta.field.FieldsRuntime;
|
import foundation.translator.ValueType;
|
|
public class Indexes {
|
|
private EntitySet entitySet;
|
private Map<String, IIndex> indexMap;
|
private boolean dirty;
|
private MaxValue maxValue;
|
|
|
public Indexes(EntitySet entitySet) {
|
indexMap = new HashMap<String, IIndex>();
|
this.entitySet = entitySet;
|
this.maxValue = new MaxValue();
|
}
|
|
public Entity getEntity(String fieldName, Object value) throws Exception {
|
if (fieldName == null) {
|
return null;
|
}
|
|
fieldName = fieldName.toLowerCase();
|
IIndex index = indexMap.get(fieldName);
|
|
if (dirty || index == null) {
|
index = createIndex(fieldName);
|
indexMap.put(fieldName, index);
|
}
|
|
Entity entity = index.get(value);
|
return entity;
|
}
|
|
public boolean contains(String field, Object value) throws Exception {
|
Entity entity = getEntity(field, value);
|
return entity != null;
|
}
|
|
public Object getMax(String fieldName) {
|
if (!dirty && maxValue.isSame(fieldName)) {
|
return maxValue.getValue();
|
}
|
|
FieldsRuntime fieldMetas = entitySet.getEntityMeta();
|
Field field = fieldMetas.get(fieldName);
|
|
if (field == null) {
|
return null;
|
}
|
|
ValueType valueType = field.getType();
|
|
if (ValueType.Decimal == valueType) {
|
getMaxDecimal(field);
|
}
|
else if (ValueType.Int == valueType) {
|
getMaxInt(field);
|
}
|
else if (ValueType.Long == valueType) {
|
getMaxLong(field);
|
}
|
else if (ValueType.Double == valueType) {
|
getMaxDouble(field);
|
}
|
else if (ValueType.Float == valueType) {
|
getMaxFloat(field);
|
}
|
else {
|
return null;
|
}
|
|
return maxValue.getValue();
|
}
|
|
private IIndex createIndex(String fieldName) throws Exception {
|
FieldsRuntime meta = entitySet.getEntityMeta();
|
Field field = meta.get(fieldName);
|
|
if (field == null) {
|
throw new Exception("field not exists: " + fieldName);
|
}
|
|
ValueType type = field.getType();
|
|
if (ValueType.String != type && ValueType.Long != type && ValueType.Int != type) {
|
throw new Exception("filed can not create index: " + fieldName);
|
}
|
|
Index index = new Index(fieldName);
|
index.load(entitySet);
|
|
return index;
|
}
|
|
public void setDirty() {
|
dirty = true;
|
}
|
|
private void getMaxDecimal(Field field) {
|
BigDecimal max = null;
|
int idx = field.getIndexNo();
|
|
for (Entity entity: entitySet) {
|
BigDecimal current = entity.getBigDecimal(idx, null);
|
|
if (current == null) {
|
continue;
|
}
|
|
if (max == null) {
|
max = current;
|
continue;
|
}
|
|
max = max.max(current);
|
}
|
|
maxValue.setField(field.getName());
|
maxValue.setValue(max);
|
}
|
|
private void getMaxInt(Field field) {
|
Integer max = null;
|
int idx = field.getIndexNo();
|
|
for (Entity entity: entitySet) {
|
Integer current = entity.getInt(idx, null);
|
|
if (current == null) {
|
continue;
|
}
|
|
if (max == null) {
|
max = current;
|
continue;
|
}
|
|
max = Math.max(max, current);
|
}
|
|
maxValue.setField(field.getName());
|
maxValue.setValue(max);
|
}
|
|
private void getMaxLong(Field field) {
|
|
}
|
|
private void getMaxDouble(Field field) {
|
|
}
|
|
private void getMaxFloat(Field field) {
|
|
}
|
|
}
|