Skip to content

Commit 45e5c04

Browse files
utku-caglayanyuce
authored andcommitted
Write/Read positions of the array elements in Portable Serialization [API-1619] (#895)
* write/read positions of the array elements of type Date, Time, Timestamp, TimestampWithZone, Decimal in portable serialization * remove redundant comments * address review comments
1 parent 372ab9a commit 45e5c04

File tree

3 files changed

+73
-62
lines changed

3 files changed

+73
-62
lines changed

internal/serialization/default_portable_reader.go

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ func (pr *DefaultPortableReader) ReadTimestampWithTimezone(fieldName string) (t
382382
func (pr *DefaultPortableReader) ReadDateArray(fieldName string) (t []types.LocalDate) {
383383
pos := pr.positionByField(fieldName, serialization.TypeDateArray)
384384
pr.runAtPosition(pos, func() {
385-
v := readArrayOfTime(pr.input, ReadPortableDate)
385+
v := pr.readArrayOfTime(ReadPortableDate)
386386
t = *(*[]types.LocalDate)(unsafe.Pointer(&v))
387387
})
388388
return
@@ -391,7 +391,7 @@ func (pr *DefaultPortableReader) ReadDateArray(fieldName string) (t []types.Loca
391391
func (pr *DefaultPortableReader) ReadTimeArray(fieldName string) (t []types.LocalTime) {
392392
pos := pr.positionByField(fieldName, serialization.TypeTimeArray)
393393
pr.runAtPosition(pos, func() {
394-
v := readArrayOfTime(pr.input, ReadPortableTime)
394+
v := pr.readArrayOfTime(ReadPortableTime)
395395
t = *(*[]types.LocalTime)(unsafe.Pointer(&v))
396396
})
397397
return
@@ -400,7 +400,7 @@ func (pr *DefaultPortableReader) ReadTimeArray(fieldName string) (t []types.Loca
400400
func (pr *DefaultPortableReader) ReadTimestampArray(fieldName string) (t []types.LocalDateTime) {
401401
pos := pr.positionByField(fieldName, serialization.TypeTimestampArray)
402402
pr.runAtPosition(pos, func() {
403-
v := readArrayOfTime(pr.input, ReadPortableTimestamp)
403+
v := pr.readArrayOfTime(ReadPortableTimestamp)
404404
t = *(*[]types.LocalDateTime)(unsafe.Pointer(&v))
405405
})
406406
return
@@ -409,7 +409,7 @@ func (pr *DefaultPortableReader) ReadTimestampArray(fieldName string) (t []types
409409
func (pr *DefaultPortableReader) ReadTimestampWithTimezoneArray(fieldName string) (t []types.OffsetDateTime) {
410410
pos := pr.positionByField(fieldName, serialization.TypeTimestampWithTimezoneArray)
411411
pr.runAtPosition(pos, func() {
412-
v := readArrayOfTime(pr.input, ReadPortableTimestampWithTimezone)
412+
v := pr.readArrayOfTime(ReadPortableTimestampWithTimezone)
413413
t = *(*[]types.OffsetDateTime)(unsafe.Pointer(&v))
414414
})
415415
return
@@ -426,7 +426,18 @@ func (pr *DefaultPortableReader) ReadDecimal(fieldName string) (d *types.Decimal
426426
func (pr *DefaultPortableReader) ReadDecimalArray(fieldName string) (ds []types.Decimal) {
427427
pos := pr.positionByField(fieldName, serialization.TypeDecimalArray)
428428
pr.runAtPosition(pos, func() {
429-
ds = ReadDecimalArray(pr.input)
429+
l := pr.input.ReadInt32()
430+
if l == nilArrayLength {
431+
return
432+
}
433+
ds = make([]types.Decimal, l)
434+
offset := pr.input.Position()
435+
for i := int32(0); i < l; i++ {
436+
pr.input.SetPosition(offset + i*Int32SizeInBytes)
437+
pos := pr.input.ReadInt32()
438+
pr.input.SetPosition(pos)
439+
ds[i] = ReadDecimal(pr.input)
440+
}
430441
})
431442
return
432443
}
@@ -454,6 +465,22 @@ func (pr *DefaultPortableReader) runAtPosition(pos int32, f func()) {
454465
pr.input.SetPosition(backup)
455466
}
456467

468+
func (pr *DefaultPortableReader) readArrayOfTime(f func(input serialization.DataInput) time.Time) (ts []time.Time) {
469+
l := pr.input.ReadInt32()
470+
if l == nilArrayLength {
471+
return
472+
}
473+
ts = make([]time.Time, l)
474+
offset := pr.input.Position()
475+
objInput := pr.input.(*ObjectDataInput)
476+
for i := int32(0); i < l; i++ {
477+
pos := objInput.ReadInt32AtPosition(offset + i*Int32SizeInBytes)
478+
pr.input.SetPosition(pos)
479+
ts[i] = f(pr.input)
480+
}
481+
return
482+
}
483+
457484
func ReadPortableDate(i serialization.DataInput) time.Time {
458485
y, m, d := readPortableDate(i)
459486
return time.Date(y, m, d, 0, 0, 0, 0, time.Local)

internal/serialization/default_portable_writer.go

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -215,26 +215,26 @@ func (pw *DefaultPortableWriter) WriteTimestampWithTimezone(fieldName string, t
215215

216216
func (pw *DefaultPortableWriter) WriteDateArray(fieldName string, a []types.LocalDate) {
217217
pw.setPosition(fieldName, int32(serialization.TypeDateArray))
218-
ts := (*([]time.Time))(unsafe.Pointer(&a))
219-
writeArrayOfTime(pw.output.ObjectDataOutput, *ts, WritePortableDate)
218+
ts := (*[]time.Time)(unsafe.Pointer(&a))
219+
pw.writeArrayOfTime(*ts, WritePortableDate)
220220
}
221221

222222
func (pw *DefaultPortableWriter) WriteTimeArray(fieldName string, a []types.LocalTime) {
223223
pw.setPosition(fieldName, int32(serialization.TypeTimeArray))
224-
ts := (*([]time.Time))(unsafe.Pointer(&a))
225-
writeArrayOfTime(pw.output.ObjectDataOutput, *ts, WritePortableTime)
224+
ts := (*[]time.Time)(unsafe.Pointer(&a))
225+
pw.writeArrayOfTime(*ts, WritePortableTime)
226226
}
227227

228228
func (pw *DefaultPortableWriter) WriteTimestampArray(fieldName string, a []types.LocalDateTime) {
229229
pw.setPosition(fieldName, int32(serialization.TypeTimestampArray))
230-
ts := (*([]time.Time))(unsafe.Pointer(&a))
231-
writeArrayOfTime(pw.output.ObjectDataOutput, *ts, WritePortableTimestamp)
230+
ts := (*[]time.Time)(unsafe.Pointer(&a))
231+
pw.writeArrayOfTime(*ts, WritePortableTimestamp)
232232
}
233233

234234
func (pw *DefaultPortableWriter) WriteTimestampWithTimezoneArray(fieldName string, a []types.OffsetDateTime) {
235235
pw.setPosition(fieldName, int32(serialization.TypeTimestampWithTimezoneArray))
236-
ts := (*([]time.Time))(unsafe.Pointer(&a))
237-
writeArrayOfTime(pw.output.ObjectDataOutput, *ts, WritePortableTimestampWithTimezone)
236+
ts := (*[]time.Time)(unsafe.Pointer(&a))
237+
pw.writeArrayOfTime(*ts, WritePortableTimestampWithTimezone)
238238
}
239239

240240
func (pw *DefaultPortableWriter) WriteDecimal(fieldName string, d *types.Decimal) {
@@ -245,7 +245,21 @@ func (pw *DefaultPortableWriter) WriteDecimal(fieldName string, d *types.Decimal
245245

246246
func (pw *DefaultPortableWriter) WriteDecimalArray(fieldName string, ds []types.Decimal) {
247247
pw.setPosition(fieldName, int32(serialization.TypeDecimalArray))
248-
WriteDecimalArray(pw.output.ObjectDataOutput, ds)
248+
arrLen := len(ds)
249+
if ds == nil {
250+
arrLen = nilArrayLength
251+
}
252+
pw.output.WriteInt32(int32(arrLen))
253+
if arrLen < 1 {
254+
return
255+
}
256+
offset := pw.output.Position()
257+
pw.output.WriteZeroBytes(arrLen * Int32SizeInBytes)
258+
for i, v := range ds {
259+
pos := pw.output.Position()
260+
pw.output.PWriteInt32(offset+int32(Int32SizeInBytes*i), pos)
261+
WriteDecimal(pw.output.ObjectDataOutput, v)
262+
}
249263
}
250264

251265
func (pw *DefaultPortableWriter) GetRawDataOutput() serialization.DataOutput {
@@ -287,6 +301,24 @@ func (pw *DefaultPortableWriter) writeNullableField(fieldName string, fieldType
287301
}
288302
}
289303

304+
func (pw *DefaultPortableWriter) writeArrayOfTime(ts []time.Time, f func(o serialization.DataOutput, t time.Time)) {
305+
arrLen := len(ts)
306+
if ts == nil {
307+
arrLen = nilArrayLength
308+
}
309+
pw.output.WriteInt32(int32(arrLen))
310+
if arrLen < 1 {
311+
return
312+
}
313+
offset := pw.output.Position()
314+
pw.output.WriteZeroBytes(arrLen * Int32SizeInBytes)
315+
for i, t := range ts {
316+
pos := pw.output.Position()
317+
pw.output.PWriteInt32(offset+int32(Int32SizeInBytes*i), pos)
318+
f(pw.output.ObjectDataOutput, t)
319+
}
320+
}
321+
290322
func WritePortableDate(o serialization.DataOutput, t time.Time) {
291323
y, m, d := t.Date()
292324
o.WriteInt16(int16(y))

internal/serialization/object_data.go

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -793,28 +793,6 @@ func WriteDecimal(o serialization.DataOutput, d types.Decimal) {
793793
o.WriteInt32(int32(d.Scale()))
794794
}
795795

796-
func WriteDecimalArray(o serialization.DataOutput, ds []types.Decimal) {
797-
if len(ds) == 0 {
798-
o.WriteInt32(nilArrayLength)
799-
return
800-
}
801-
o.WriteInt32(int32(len(ds)))
802-
for _, d := range ds {
803-
WriteDecimal(o, d)
804-
}
805-
}
806-
807-
func writeArrayOfTime(o serialization.DataOutput, ts []time.Time, f func(o serialization.DataOutput, t time.Time)) {
808-
if len(ts) == 0 {
809-
o.WriteInt32(nilArrayLength)
810-
return
811-
}
812-
o.WriteInt32(int32(len(ts)))
813-
for _, t := range ts {
814-
f(o, t)
815-
}
816-
}
817-
818796
func ReadDate(i serialization.DataInput) time.Time {
819797
y, m, d := readDate(i)
820798
return time.Date(y, m, d, 0, 0, 0, 0, time.Local)
@@ -852,19 +830,6 @@ func ReadDecimal(i serialization.DataInput) types.Decimal {
852830
return types.NewDecimal(v, int(scale))
853831
}
854832

855-
func ReadDecimalArray(i serialization.DataInput) []types.Decimal {
856-
var ds []types.Decimal
857-
l := i.ReadInt32()
858-
if l == nilArrayLength {
859-
return ds
860-
}
861-
ds = make([]types.Decimal, l)
862-
for j := 0; j < int(l); j++ {
863-
ds[j] = ReadDecimal(i)
864-
}
865-
return ds
866-
}
867-
868833
func readDate(i serialization.DataInput) (y int, m time.Month, d int) {
869834
y = int(i.ReadInt32())
870835
m = time.Month(i.ReadByte())
@@ -879,16 +844,3 @@ func readTime(i serialization.DataInput) (h, m, s, nanos int) {
879844
nanos = int(i.ReadInt32())
880845
return
881846
}
882-
883-
func readArrayOfTime(i serialization.DataInput, f func(i serialization.DataInput) time.Time) []time.Time {
884-
var ts []time.Time
885-
l := i.ReadInt32()
886-
if l == nilArrayLength {
887-
return ts
888-
}
889-
ts = make([]time.Time, l)
890-
for j := 0; j < int(l); j++ {
891-
ts[j] = f(i)
892-
}
893-
return ts
894-
}

0 commit comments

Comments
 (0)