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