提交 af80d630 authored 作者: noelgrandin's avatar noelgrandin

reduce unnecessary object creation in Timestamp routines

上级 a9905aa0
......@@ -885,7 +885,7 @@ public class Function extends Expression implements FunctionCall {
case NOW:
case CURRENT_TIMESTAMP: {
long now = session.getTransactionStart();
ValueTimestamp vt = ValueTimestamp.get(new Timestamp(now));
ValueTimestamp vt = ValueTimestamp.fromMillis(now);
if (v0 != null) {
Mode mode = database.getMode();
vt = (ValueTimestamp) vt.convertScale(
......@@ -1226,8 +1226,7 @@ public class Function extends Expression implements FunctionCall {
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
result = ValueTimestamp.get(
new java.sql.Timestamp(c.getTimeInMillis()));
result = ValueTimestamp.fromMillis(c.getTimeInMillis());
} else {
double d = v0.getDouble();
int p = v1 == null ? 0 : v1.getInt();
......@@ -1453,7 +1452,7 @@ public class Function extends Expression implements FunctionCall {
null : v3 == ValueNull.INSTANCE ? null : v3.getString();
java.util.Date d = DateTimeUtils.parseDateTime(
v0.getString(), v1.getString(), locale, tz);
result = ValueTimestamp.get(new Timestamp(d.getTime()));
result = ValueTimestamp.fromMillis(d.getTime());
}
break;
}
......
......@@ -763,10 +763,9 @@ public class Data {
return ValueTimestamp.fromDateValueAndNanos(dateValue, nanos);
}
case Value.TIMESTAMP: {
Timestamp ts = new Timestamp(
DateTimeUtils.getTimeUTCWithoutDst(readVarLong()));
ts.setNanos(readVarInt());
return ValueTimestamp.get(ts);
return ValueTimestamp.fromMillisNanos(
DateTimeUtils.getTimeUTCWithoutDst(readVarLong()),
readVarInt());
}
case Value.BYTES: {
int len = readVarInt();
......
......@@ -294,8 +294,7 @@ public class Column {
if (dt.decimal) {
value = ValueInt.get(0).convertTo(type);
} else if (dt.type == Value.TIMESTAMP) {
value = ValueTimestamp.get(new Timestamp(
session.getTransactionStart()));
value = ValueTimestamp.fromMillis(session.getTransactionStart());
} else if (dt.type == Value.TIME) {
value = ValueTime.fromNanos(0);
} else if (dt.type == Value.DATE) {
......
......@@ -990,8 +990,7 @@ public class DataType {
} else if (x instanceof Timestamp) {
return ValueTimestamp.get((Timestamp) x);
} else if (x instanceof java.util.Date) {
return ValueTimestamp.get(
new Timestamp(((java.util.Date) x).getTime()));
return ValueTimestamp.fromMillis(((java.util.Date) x).getTime());
} else if (x instanceof java.io.Reader) {
Reader r = new BufferedReader((java.io.Reader) x);
return session.getDataHandler().getLobStorage().
......
......@@ -565,13 +565,11 @@ public class Transfer {
if (version >= Constants.TCP_PROTOCOL_VERSION_9) {
return ValueTimestamp.fromDateValueAndNanos(readLong(), readLong());
} else if (version >= Constants.TCP_PROTOCOL_VERSION_7) {
Timestamp ts = new Timestamp(DateTimeUtils.getTimeUTCWithoutDst(readLong()));
ts.setNanos(readInt());
return ValueTimestamp.get(ts);
return ValueTimestamp.fromMillisNanos(
DateTimeUtils.getTimeUTCWithoutDst(readLong()),
readInt());
}
Timestamp ts = new Timestamp(readLong());
ts.setNanos(readInt());
return ValueTimestamp.get(ts);
return ValueTimestamp.fromMillisNanos(readLong(), readInt());
}
case Value.DECIMAL:
return ValueDecimal.get(new BigDecimal(readString()));
......
......@@ -13,7 +13,6 @@ import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.TimeZone;
import org.h2.api.ErrorCode;
import org.h2.message.DbException;
import org.h2.util.DateTimeUtils;
......@@ -79,6 +78,28 @@ public class ValueTimestamp extends Value {
return fromDateValueAndNanos(dateValue, nanos);
}
/**
* Get or create a timestamp value for the given date/time in millis.
*
* @return the value
*/
public static ValueTimestamp fromMillisNanos(long ms, int nanos) {
long dateValue = DateTimeUtils.dateValueFromDate(ms);
long timeNanos = nanos + DateTimeUtils.nanosFromDate(ms);
return fromDateValueAndNanos(dateValue, timeNanos);
}
/**
* Get or create a timestamp value for the given date/time in millis.
*
* @return the value
*/
public static ValueTimestamp fromMillis(long ms) {
long dateValue = DateTimeUtils.dateValueFromDate(ms);
long nanos = DateTimeUtils.nanosFromDate(ms);
return fromDateValueAndNanos(dateValue, nanos);
}
/**
* Parse a string to a ValueTimestamp. This method supports the format
* +/-year-month-day hour:minute:seconds.fractional and an optional timezone
......
......@@ -166,8 +166,8 @@ public class TestDataPage extends TestBase implements DataHandler {
testValue(ValueDate.get(new Date(0)));
testValue(ValueTime.get(new Time(System.currentTimeMillis())));
testValue(ValueTime.get(new Time(0)));
testValue(ValueTimestamp.get(new Timestamp(System.currentTimeMillis())));
testValue(ValueTimestamp.get(new Timestamp(0)));
testValue(ValueTimestamp.fromMillis(System.currentTimeMillis()));
testValue(ValueTimestamp.fromMillis(0));
testValue(ValueJavaObject.getNoCopy(null, new byte[0], this));
testValue(ValueJavaObject.getNoCopy(null, new byte[100], this));
for (int i = 0; i < 300; i++) {
......
......@@ -162,7 +162,7 @@ public class TestValueMemory extends TestBase implements DataHandler {
case Value.DATE:
return ValueDate.get(new java.sql.Date(random.nextLong()));
case Value.TIMESTAMP:
return ValueTimestamp.get(new java.sql.Timestamp(random.nextLong()));
return ValueTimestamp.fromMillis(random.nextLong());
case Value.BYTES:
return ValueBytes.get(randomBytes(random.nextInt(1000)));
case Value.STRING:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论