Skip to content

Commit 5fcc64b

Browse files
author
lucaijun
committed
AJ-543: duration support exchange calendar
1 parent c7020bc commit 5fcc64b

File tree

3 files changed

+91
-10
lines changed

3 files changed

+91
-10
lines changed

src/com/xxdb/data/BasicDuration.java

Lines changed: 87 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,41 @@ public class BasicDuration extends AbstractScalar implements Comparable<BasicDur
1010
private static final String[] unitSyms = {"ns", "us", "ms", "s", "m", "H", "d", "w", "M", "y", "B"};
1111
private int value;
1212
private DURATION unit;
13+
private int exchange_;
1314

1415
public BasicDuration(DURATION unit, int value){
1516
this.value = value;
1617
this.unit = unit;
18+
this.exchange_ = unit.ordinal();
19+
}
20+
21+
public BasicDuration(String unit, int value) {
22+
this.value = value;
23+
int codeNumber = getCodeNumber(unit);
24+
if (codeNumber >= 0 && codeNumber <= 10)
25+
this.unit = DURATION.values()[codeNumber];
26+
else
27+
this.unit = DURATION.TDAY;
28+
this.exchange_ = codeNumber;
29+
}
30+
31+
protected BasicDuration(int exchange_, int value) {
32+
this.value = value;
33+
if (exchange_ >= 0 && exchange_ <= 10)
34+
this.unit = DURATION.values()[exchange_];
35+
else
36+
this.unit = DURATION.TDAY;
37+
this.exchange_ = exchange_;
1738
}
1839

1940
public BasicDuration(ExtendedDataInput in) throws IOException{
2041
value = in.readInt();
21-
unit = DURATION.values()[in.readInt()];
42+
int codeNumber = in.readInt();
43+
if (codeNumber >= 0 && codeNumber <= 10)
44+
unit = DURATION.values()[codeNumber];
45+
else
46+
unit = DURATION.TDAY;
47+
exchange_ = codeNumber;
2248
}
2349

2450
public int getDuration() {
@@ -29,6 +55,22 @@ public DURATION getUnit() {
2955
return unit;
3056
}
3157

58+
public int getExchangeInt() {
59+
return exchange_;
60+
}
61+
62+
public String getExchangeName() {
63+
if (exchange_ >= 0 && exchange_ <= 10) {
64+
return unitSyms[exchange_];
65+
}
66+
char[] codes = new char[4];
67+
codes[0] = (char) ((exchange_ >> 24) & 255);
68+
codes[1] = (char) ((exchange_ >> 16) & 255);
69+
codes[2] = (char) ((exchange_ >> 8) & 255);
70+
codes[3] = (char) (exchange_ & 255);
71+
return String.valueOf(codes);
72+
}
73+
3274
@Override
3375
public boolean isNull() {
3476
return value == Integer.MIN_VALUE;
@@ -75,19 +117,19 @@ public String getString() {
75117
if(value == Integer.MIN_VALUE)
76118
return "";
77119
else
78-
return String.valueOf(value) + unitSyms[unit.ordinal()];
120+
return value + getExchangeName();
79121
}
80122

81123
@Override
82124
protected void writeScalarToOutputStream(ExtendedDataOutput out) throws IOException {
83125
out.writeInt(value);
84-
out.writeInt(unit.ordinal());
126+
out.writeInt(exchange_);
85127
}
86128

87129
@Override
88130
public int compareTo(BasicDuration o) {
89-
if(unit==o.unit)
90-
return Integer.compare(((BasicDuration)o).value,value);
131+
if (unit == o.unit && exchange_ == o.exchange_)
132+
return Integer.compare(o.value, value);
91133
else
92134
return -1;
93135
}
@@ -97,12 +139,51 @@ public boolean equals(Object o){
97139
if(! (o instanceof BasicDuration) || o == null)
98140
return false;
99141
else
100-
return value == ((BasicDuration)o).value && unit == ((BasicDuration)o).unit;
142+
return value == ((BasicDuration) o).value && unit == ((BasicDuration) o).unit && exchange_ == ((BasicDuration) o).exchange_;
101143
}
102144

103145
@JsonIgnore
104146
@Override
105147
public int getScale(){
106148
return super.getScale();
107149
}
150+
151+
private int getCodeNumber(String unit) {
152+
if (unit.length() == 1 || unit.length() == 2) {
153+
switch (unit) {
154+
case "ns":
155+
return 0;
156+
case "us":
157+
return 1;
158+
case "ms":
159+
return 2;
160+
case "s":
161+
return 3;
162+
case "m":
163+
return 4;
164+
case "H":
165+
return 5;
166+
case "d":
167+
return 6;
168+
case "w":
169+
return 7;
170+
case "M":
171+
return 8;
172+
case "y":
173+
return 9;
174+
case "B":
175+
return 10;
176+
default:
177+
throw new RuntimeException("error unit: " + unit);
178+
}
179+
} else if (unit.length() == 4) {
180+
int[] codes = new int[4];
181+
for (int i = 0; i < 4; i++) {
182+
codes[i] = unit.charAt(i);
183+
}
184+
return (codes[0] << 24) + (codes[1] << 16) + (codes[2] << 8) + codes[3];
185+
} else {
186+
throw new RuntimeException("error length of unit, " + unit.length());
187+
}
188+
}
108189
}

src/com/xxdb/data/BasicDurationVector.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ public Entity get(int index){
5353
if(unitIndex == Integer.MIN_VALUE)
5454
return new BasicDuration(DURATION.NS, Integer.MIN_VALUE);
5555
else
56-
return new BasicDuration(DURATION.values()[values[2*index + 1]], values[2*index]);
56+
return new BasicDuration(values[2*index + 1], values[2*index]);
5757
}
5858

5959
public void set(int index, Entity value) throws Exception {
6060
BasicDuration duration = (BasicDuration)value;
6161
values[2*index] = duration.getDuration();
62-
values[2*index + 1] = duration.getUnit().ordinal();
62+
values[2*index + 1] = duration.getExchangeInt();
6363
}
6464

6565
@Override
@@ -95,7 +95,7 @@ public void addRange(int[] valueList) {
9595

9696
@Override
9797
public void Append(Scalar value) throws Exception{
98-
add(((BasicDuration)value).getDuration(), ((BasicDuration)value).getUnit().ordinal());
98+
add(((BasicDuration)value).getDuration(), ((BasicDuration)value).getExchangeInt());
9999
}
100100

101101
@Override

src/com/xxdb/data/Entity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static DATA_TYPE valueOfTypeName(String name){
6666
enum DATA_CATEGORY {NOTHING,LOGICAL,INTEGRAL,FLOATING,TEMPORAL,LITERAL,SYSTEM,MIXED,BINARY,ARRAY,DENARY};
6767
enum DATA_FORM {DF_SCALAR,DF_VECTOR,DF_PAIR,DF_MATRIX,DF_SET,DF_DICTIONARY,DF_TABLE,DF_CHART,DF_CHUNK,DF_SYSOBJ};
6868
enum PARTITION_TYPE {SEQ, VALUE, RANGE, LIST, COMPO, HASH}
69-
enum DURATION {NS, US, MS, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, YEAR, BDAY};
69+
enum DURATION {NS, US, MS, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, YEAR, BDAY, TDAY};
7070
DATA_FORM getDataForm();
7171
DATA_CATEGORY getDataCategory();
7272
DATA_TYPE getDataType();

0 commit comments

Comments
 (0)