@@ -22,7 +22,6 @@ package client
2222import (
2323 "bytes"
2424 "encoding/binary"
25- "errors"
2625 "fmt"
2726 "reflect"
2827 "sort"
@@ -38,6 +37,7 @@ type Tablet struct {
3837 measurementSchemas []* MeasurementSchema
3938 timestamps []int64
4039 values []interface {}
40+ bitMaps []* BitMap
4141 maxRowNumber int
4242 RowSize int
4343}
@@ -69,6 +69,24 @@ func (t *Tablet) Swap(i, j int) {
6969 sortedSlice [i ], sortedSlice [j ] = sortedSlice [j ], sortedSlice [i ]
7070 }
7171 }
72+ if t .bitMaps != nil {
73+ for _ , bitMap := range t .bitMaps {
74+ if bitMap != nil {
75+ isNilI := bitMap .IsMarked (i )
76+ isNilJ := bitMap .IsMarked (j )
77+ if isNilI {
78+ bitMap .Mark (j )
79+ } else {
80+ bitMap .UnMark (j )
81+ }
82+ if isNilJ {
83+ bitMap .Mark (i )
84+ } else {
85+ bitMap .UnMark (i )
86+ }
87+ }
88+ }
89+ }
7290 t .timestamps [i ], t .timestamps [j ] = t .timestamps [j ], t .timestamps [i ]
7391}
7492
@@ -81,18 +99,27 @@ func (t *Tablet) SetTimestamp(timestamp int64, rowIndex int) {
8199}
82100
83101func (t * Tablet ) SetValueAt (value interface {}, columnIndex , rowIndex int ) error {
84- if value == nil {
85- return errors .New ("illegal argument value can't be nil" )
86- }
87102
88103 if columnIndex < 0 || columnIndex > len (t .measurementSchemas ) {
89104 return fmt .Errorf ("illegal argument columnIndex %d" , columnIndex )
90105 }
91106
92- if rowIndex < 0 || rowIndex > int ( t .maxRowNumber ) {
107+ if rowIndex < 0 || rowIndex > t .maxRowNumber {
93108 return fmt .Errorf ("illegal argument rowIndex %d" , rowIndex )
94109 }
95110
111+ if value == nil {
112+ // Init the bitMap to mark nil value
113+ if t .bitMaps == nil {
114+ t .bitMaps = make ([]* BitMap , len (t .values ))
115+ }
116+ if t .bitMaps [columnIndex ] == nil {
117+ t .bitMaps [columnIndex ] = NewBitMap (t .maxRowNumber )
118+ }
119+ // Mark the nil value position
120+ t .bitMaps [columnIndex ].Mark (rowIndex )
121+ }
122+
96123 switch t .measurementSchemas [columnIndex ].DataType {
97124 case BOOLEAN :
98125 values := t .values [columnIndex ].([]bool )
@@ -167,11 +194,15 @@ func (t *Tablet) GetValueAt(columnIndex, rowIndex int) (interface{}, error) {
167194 return nil , fmt .Errorf ("illegal argument columnIndex %d" , columnIndex )
168195 }
169196
170- if rowIndex < 0 || rowIndex > int ( t .maxRowNumber ) {
197+ if rowIndex < 0 || rowIndex > t .maxRowNumber {
171198 return nil , fmt .Errorf ("illegal argument rowIndex %d" , rowIndex )
172199 }
173200
174201 schema := t .measurementSchemas [columnIndex ]
202+
203+ if t .bitMaps != nil && t .bitMaps [columnIndex ] != nil && t .bitMaps [columnIndex ].IsMarked (rowIndex ) {
204+ return nil , nil
205+ }
175206 switch schema .DataType {
176207 case BOOLEAN :
177208 return t .values [columnIndex ].([]bool )[rowIndex ], nil
@@ -235,6 +266,15 @@ func (t *Tablet) getValuesBytes() ([]byte, error) {
235266 return nil , fmt .Errorf ("illegal datatype %v" , schema .DataType )
236267 }
237268 }
269+ if t .bitMaps != nil {
270+ for _ , bitMap := range t .bitMaps {
271+ columnHasNil := bitMap != nil && ! bitMap .IsAllUnmarked ()
272+ binary .Write (buff , binary .BigEndian , columnHasNil )
273+ if columnHasNil {
274+ binary .Write (buff , binary .BigEndian , bitMap .GetBits ()[0 :t .RowSize / 8 + 1 ])
275+ }
276+ }
277+ }
238278 return buff .Bytes (), nil
239279}
240280
@@ -245,6 +285,7 @@ func (t *Tablet) Sort() error {
245285
246286func (t * Tablet ) Reset () {
247287 t .RowSize = 0
288+ t .bitMaps = nil
248289}
249290
250291func NewTablet (deviceId string , measurementSchemas []* MeasurementSchema , maxRowNumber int ) (* Tablet , error ) {
0 commit comments