1
1
package org .enso .interpreter .arrow .runtime ;
2
2
3
3
import com .oracle .truffle .api .CompilerDirectives ;
4
- import com .oracle .truffle .api .dsl .Cached ;
5
4
import com .oracle .truffle .api .dsl .ImportStatic ;
6
5
import com .oracle .truffle .api .dsl .Specialization ;
7
6
import com .oracle .truffle .api .interop .InteropLibrary ;
8
7
import com .oracle .truffle .api .interop .TruffleObject ;
9
8
import com .oracle .truffle .api .interop .UnsupportedMessageException ;
10
- import com .oracle .truffle .api .library .CachedLibrary ;
11
9
import com .oracle .truffle .api .library .ExportLibrary ;
12
10
import com .oracle .truffle .api .library .ExportMessage ;
13
11
import java .time .Instant ;
14
12
import java .time .LocalDate ;
15
13
import java .time .LocalTime ;
16
14
import java .time .ZoneId ;
17
- import java .time .ZoneOffset ;
18
15
import java .time .ZonedDateTime ;
16
+ import org .enso .interpreter .arrow .LogicalLayout ;
19
17
20
18
@ ExportLibrary (InteropLibrary .class )
21
19
public final class ArrowFixedArrayDate implements TruffleObject {
22
20
private final int size ;
23
21
private final ByteBufferDirect buffer ;
22
+ private final LogicalLayout unit ;
24
23
25
- private final DateUnit unit ;
26
-
27
- public ArrowFixedArrayDate (int size , DateUnit unit ) {
24
+ public ArrowFixedArrayDate (int size , LogicalLayout unit ) {
28
25
this .size = size ;
29
26
this .unit = unit ;
30
- this .buffer = allocateBuffer ( size * unit . sizeInBytes (), size );
27
+ this .buffer = ByteBufferDirect . forSize ( size , unit );
31
28
}
32
29
33
- public ArrowFixedArrayDate (ByteBufferDirect buffer , DateUnit unit )
30
+ public ArrowFixedArrayDate (ByteBufferDirect buffer , LogicalLayout unit )
34
31
throws UnsupportedMessageException {
35
32
this .size = buffer .capacity () / unit .sizeInBytes ();
36
33
this .unit = unit ;
37
34
this .buffer = buffer ;
38
35
}
39
36
40
- public DateUnit getUnit () {
37
+ public LogicalLayout getUnit () {
41
38
return unit ;
42
39
}
43
40
@@ -47,86 +44,42 @@ public boolean hasArrayElements() {
47
44
}
48
45
49
46
@ ExportMessage
50
- @ ImportStatic (ArrowFixedArrayDate . DateUnit .class )
47
+ @ ImportStatic (LogicalLayout .class )
51
48
static class ReadArrayElement {
52
- @ Specialization (guards = "receiver.getUnit() == Day " )
49
+ @ Specialization (guards = "receiver.getUnit() == Date32 " )
53
50
static Object doDay (ArrowFixedArrayDate receiver , long index )
54
51
throws UnsupportedMessageException {
55
- if (receiver .buffer .isNull ((int ) index )) {
56
- return NullValue .get ();
57
- }
58
- var at = typeAdjustedIndex (index , receiver .unit );
59
- var daysSinceEpoch = receiver .buffer .getInt (at );
60
- var localDate = localDateFromDays (daysSinceEpoch );
61
- return new ArrowDate (localDate );
52
+ return readDay (receiver .buffer , index );
62
53
}
63
54
64
- @ Specialization (guards = "receiver.getUnit() == Millisecond " )
55
+ @ Specialization (guards = "receiver.getUnit() == Date64 " )
65
56
static Object doMilliseconds (ArrowFixedArrayDate receiver , long index )
66
57
throws UnsupportedMessageException {
67
- if (receiver .buffer .isNull ((int ) index )) {
68
- return NullValue .get ();
69
- }
70
- var at = typeAdjustedIndex (index , receiver .unit );
71
- var secondsPlusNanoSinceEpoch = receiver .buffer .getLong (at );
72
- var seconds = Math .floorDiv (secondsPlusNanoSinceEpoch , nanoDiv );
73
- var nano = Math .floorMod (secondsPlusNanoSinceEpoch , nanoDiv );
74
- var zonedDateTime = zonedDateTimeFromSeconds (seconds , nano , utc );
75
- return new ArrowZonedDateTime (zonedDateTime );
58
+ return readMilliseconds (receiver .buffer , index );
76
59
}
77
60
}
78
61
79
- @ ExportMessage
80
- @ ImportStatic (ArrowFixedArrayDate .DateUnit .class )
81
- static class WriteArrayElement {
82
- @ Specialization (guards = "receiver.getUnit() == Day" )
83
- static void doDay (
84
- ArrowFixedArrayDate receiver ,
85
- long index ,
86
- Object value ,
87
- @ Cached .Shared ("interop" ) @ CachedLibrary (limit = "1" ) InteropLibrary iop )
88
- throws UnsupportedMessageException {
89
- if (!iop .isDate (value )) {
90
- throw UnsupportedMessageException .create ();
91
- }
92
- var at = typeAdjustedIndex (index , receiver .unit );
93
- var time = iop .asDate (value ).toEpochDay ();
94
- receiver .buffer .putInt (at , Math .toIntExact (time ));
95
- }
96
-
97
- @ Specialization (guards = {"receiver.getUnit() == Millisecond" , "!iop.isNull(value)" })
98
- static void doMilliseconds (
99
- ArrowFixedArrayDate receiver ,
100
- long index ,
101
- Object value ,
102
- @ Cached .Shared ("interop" ) @ CachedLibrary (limit = "1" ) InteropLibrary iop )
103
- throws UnsupportedMessageException {
104
- if (!iop .isDate (value ) || !iop .isTime (value )) {
105
- throw UnsupportedMessageException .create ();
106
- }
107
-
108
- var at = typeAdjustedIndex (index , receiver .unit );
109
- if (iop .isTimeZone (value )) {
110
- var zoneDateTimeInstant =
111
- instantForZone (iop .asDate (value ), iop .asTime (value ), iop .asTimeZone (value ), utc );
112
- var secondsPlusNano =
113
- zoneDateTimeInstant .getEpochSecond () * nanoDiv + zoneDateTimeInstant .getNano ();
114
- receiver .buffer .putLong (at , secondsPlusNano );
115
- } else {
116
- var dateTime = instantForOffset (iop .asDate (value ), iop .asTime (value ), ZoneOffset .UTC );
117
- var secondsPlusNano = dateTime .getEpochSecond () * nanoDiv + dateTime .getNano ();
118
- receiver .buffer .putLong (at , secondsPlusNano );
119
- }
62
+ static Object readDay (ByteBufferDirect buffer , long index ) throws UnsupportedMessageException {
63
+ if (buffer .isNull ((int ) index )) {
64
+ return NullValue .get ();
120
65
}
66
+ var at = typeAdjustedIndex (index , 4 );
67
+ var daysSinceEpoch = buffer .getInt (at );
68
+ var localDate = localDateFromDays (daysSinceEpoch );
69
+ return new ArrowDate (localDate );
70
+ }
121
71
122
- @ Specialization (guards = "iop.isNull(value)" )
123
- static void doNull (
124
- ArrowFixedArrayDate receiver ,
125
- long index ,
126
- Object value ,
127
- @ Cached .Shared ("interop" ) @ CachedLibrary (limit = "1" ) InteropLibrary iop ) {
128
- receiver .buffer .setNull ((int ) index );
72
+ static Object readMilliseconds (ByteBufferDirect buffer , long index )
73
+ throws UnsupportedMessageException {
74
+ if (buffer .isNull ((int ) index )) {
75
+ return NullValue .get ();
129
76
}
77
+ var at = typeAdjustedIndex (index , 8 );
78
+ var secondsPlusNanoSinceEpoch = buffer .getLong (at );
79
+ var seconds = Math .floorDiv (secondsPlusNanoSinceEpoch , NANO_DIV );
80
+ var nano = Math .floorMod (secondsPlusNanoSinceEpoch , NANO_DIV );
81
+ var zonedDateTime = zonedDateTimeFromSeconds (seconds , nano , UTC );
82
+ return new ArrowZonedDateTime (zonedDateTime );
130
83
}
131
84
132
85
@ ExportMessage
@@ -139,16 +92,6 @@ boolean isArrayElementReadable(long index) {
139
92
return index >= 0 && index < size && !buffer .isNull ((int ) index );
140
93
}
141
94
142
- @ ExportMessage
143
- boolean isArrayElementModifiable (long index ) {
144
- return index >= 0 && index < size ;
145
- }
146
-
147
- @ ExportMessage
148
- boolean isArrayElementInsertable (long index ) {
149
- return index >= 0 && index < size ;
150
- }
151
-
152
95
@ ExportLibrary (InteropLibrary .class )
153
96
static class ArrowDate implements TruffleObject {
154
97
private LocalDate date ;
@@ -207,11 +150,6 @@ public ZoneId asTimeZone() {
207
150
}
208
151
}
209
152
210
- @ CompilerDirectives .TruffleBoundary
211
- private static ByteBufferDirect allocateBuffer (int sizeInBytes , int size ) {
212
- return new ByteBufferDirect (sizeInBytes , size );
213
- }
214
-
215
153
@ CompilerDirectives .TruffleBoundary
216
154
private static LocalDate localDateFromDays (int daysSinceEpoch ) {
217
155
return LocalDate .ofEpochDay (daysSinceEpoch );
@@ -222,37 +160,11 @@ private static ZonedDateTime zonedDateTimeFromSeconds(long seconds, long nano, Z
222
160
return Instant .ofEpochSecond (seconds , nano ).atZone (zone );
223
161
}
224
162
225
- @ CompilerDirectives .TruffleBoundary
226
- private static Instant instantForZone (
227
- LocalDate date , LocalTime time , ZoneId zone , ZoneId target ) {
228
- return date .atTime (time ).atZone (zone ).withZoneSameLocal (target ).toInstant ();
229
- }
230
-
231
- @ CompilerDirectives .TruffleBoundary
232
- private static Instant instantForOffset (LocalDate date , LocalTime time , ZoneOffset offset ) {
233
- return date .atTime (time ).toInstant (offset );
234
- }
235
-
236
- public enum DateUnit implements SizeInBytes {
237
- Day (4 ),
238
- Millisecond (8 );
239
-
240
- private final int bytes ;
241
-
242
- DateUnit (int bytes ) {
243
- this .bytes = bytes ;
244
- }
245
-
246
- public int sizeInBytes () {
247
- return bytes ;
248
- }
249
- }
250
-
251
- private static final long nanoDiv = 1000000000L ;
163
+ static final long NANO_DIV = 1000000000L ;
252
164
253
- private static final ZoneId utc = ZoneId .of ("UTC" );
165
+ static final ZoneId UTC = ZoneId .of ("UTC" );
254
166
255
- private static int typeAdjustedIndex (long index , SizeInBytes unit ) {
256
- return Math .toIntExact (index * unit . sizeInBytes () );
167
+ static int typeAdjustedIndex (long index , int daySizeInBytes ) {
168
+ return Math .toIntExact (index * daySizeInBytes );
257
169
}
258
170
}
0 commit comments