P15GEN2\59518
2024-05-29 d4210c7c4b04abde20037ea8aa0f54ef8a2649aa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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) {
        
    }
 
}