-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgob.go
326 lines (273 loc) · 9.22 KB
/
gob.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package gotables
import (
"bytes"
"encoding/gob"
"fmt"
)
// Prepare table for GOB encoding, by copying its contents to an exportable (public) table data structure.
func (table *Table) exportTable() (*TableExported, error) {
if table == nil {
return nil, fmt.Errorf("table.%s() table is <nil>", UtilFuncName())
}
var err error
var elementCount int
var tableExported *TableExported
tableExported, err = newTableExported(table.Name())
if err != nil {
return nil, err
}
var colCount int = table.ColCount()
tableExported.ColNames = make([]string, colCount)
if len(tableExported.ColNames) != colCount {
err = fmt.Errorf("exportTable() [%s] Could not make col names slice of size %d",
table.Name(), colCount)
return nil, err
}
elementCount = copy(tableExported.ColNames, table.colNames)
if elementCount != colCount {
err = fmt.Errorf("exportTable() [%s] expecting to export %d col names but exported: %d",
table.Name(), colCount, elementCount)
return nil, err
}
tableExported.ColTypes = make([]string, colCount)
if len(tableExported.ColTypes) != colCount {
err = fmt.Errorf("exportTable() [%s] Could not make col types slice of size %d",
table.Name(), colCount)
return nil, err
}
elementCount = copy(tableExported.ColTypes, table.colTypes)
if elementCount != colCount {
err = fmt.Errorf("exportTable() [%s] expecting to export %d col types but exported: %d",
table.Name(), colCount, elementCount)
return nil, err
}
tableExported.ColNamesMap = map[string]int{}
for key, val := range table.colNamesMap {
tableExported.ColNamesMap[key] = val
}
var rowCount int = table.RowCount()
tableExported.Rows = make([]tableRow, rowCount)
if len(tableExported.Rows) != rowCount {
err = fmt.Errorf("exportTable() [%s] Could not make rows slice of size %d",
table.Name(), rowCount)
return nil, err
}
elementCount = copy(tableExported.Rows, table.rows)
if elementCount != rowCount {
err = fmt.Errorf("exportTable() [%s] expecting to export %d rows but exported: %d",
table.Name(), rowCount, elementCount)
return nil, err
}
tableExported.SortKeys = []SortKeyExported{}
for keyIndex, _ := range table.sortKeys {
tableExported.SortKeys[keyIndex] = SortKeyExported{}
tableExported.SortKeys[keyIndex].ColName = table.sortKeys[keyIndex].colName
tableExported.SortKeys[keyIndex].ColType = table.sortKeys[keyIndex].colType
tableExported.SortKeys[keyIndex].Reverse = table.sortKeys[keyIndex].reverse
tableExported.SortKeys[keyIndex].SortFunc = table.sortKeys[keyIndex].sortFunc
}
tableExported.StructShape = table.isStructShape
return tableExported, nil
}
// Reconstitute table from GOB decoding.
func (tableExported *TableExported) importTable() (*Table, error) {
if tableExported == nil {
return nil, fmt.Errorf("table.%s() table is <nil>", UtilFuncName())
}
var err error
var elementCount int
var table *Table
var tableName string = tableExported.TableName
table, err = NewTable(tableName)
if err != nil {
return nil, err
}
var isValid bool
var colCount int = len(tableExported.ColNames)
table.colNames = make([]string, colCount)
elementCount = copy(table.colNames, tableExported.ColNames)
if elementCount != colCount {
err = fmt.Errorf("importTable() [%s] expecting to import %d col names but imported: %d",
tableName, colCount, elementCount)
return nil, err
}
table.colTypes = make([]string, colCount)
elementCount = copy(table.colTypes, tableExported.ColTypes)
if elementCount != colCount {
err = fmt.Errorf("importTable() [%s] expecting to import %d col types but imported: %d",
tableName, colCount, elementCount)
return nil, err
}
table.colNamesMap = map[string]int{}
for key, val := range tableExported.ColNamesMap {
table.colNamesMap[key] = val
}
var rowCount int = len(tableExported.Rows)
table.rows = make([]tableRow, rowCount)
elementCount = copy(table.rows, tableExported.Rows)
if elementCount != rowCount {
err = fmt.Errorf("importTable() [%s] expecting to import %d rows but imported: %d",
table.Name(), rowCount, elementCount)
return nil, err
}
table.sortKeys = []sortKey{}
for keyIndex, _ := range table.sortKeys {
table.sortKeys[keyIndex] = sortKey{}
table.sortKeys[keyIndex].colName = tableExported.SortKeys[keyIndex].ColName
table.sortKeys[keyIndex].colType = tableExported.SortKeys[keyIndex].ColType
table.sortKeys[keyIndex].reverse = tableExported.SortKeys[keyIndex].Reverse
table.sortKeys[keyIndex].sortFunc = tableExported.SortKeys[keyIndex].SortFunc
}
table.isStructShape = tableExported.StructShape
isValid, err = table.IsValidTable()
if !isValid {
return nil, err
}
return table, nil
}
/*
Note: GOB encoding and decoding does not (yet) support nested tables or type time.Time.
*/
func (table *Table) GobEncode() ([]byte, error) {
var emptyBuffer []byte
if table == nil {
return emptyBuffer, fmt.Errorf("table.%s() table is <nil>", UtilFuncName())
}
var err error
var buffer bytes.Buffer
var enc *gob.Encoder = gob.NewEncoder(&buffer)
var tableExported *TableExported
tableExported, err = table.exportTable()
if err != nil {
return emptyBuffer, err
}
err = enc.Encode(tableExported)
if err != nil {
return emptyBuffer, err
}
return buffer.Bytes(), nil
}
/*
Note: GOB encoding and decoding does not (yet) support nested tables or type time.Time.
*/
func (tableSet *TableSet) GobEncode() ([]bytes.Buffer, error) {
/*
go vet doesn't like this signature, but I see no other way. If it were an array of byte,
there would be no way of distinguishing individual tables (arrays of byte) within it.
$ go vet
$ method GobEncode() ([]bytes.Buffer, error) should have signature GobEncode() ([]byte, error)
*/
var emptyBuffer []bytes.Buffer
var encodedTableSet []bytes.Buffer
var err error
for tableIndex := 0; tableIndex < len(tableSet.tables); tableIndex++ {
var table *Table = tableSet.tables[tableIndex]
var encodedTable *bytes.Buffer
var encodedTableBytes []byte
encodedTableBytes, err = table.GobEncode()
if err != nil {
return emptyBuffer, err
}
encodedTable = bytes.NewBuffer(encodedTableBytes)
encodedTableSet = append(encodedTableSet, *encodedTable)
if len(encodedTableSet) != tableIndex+1 {
err = fmt.Errorf("GobEncode(): table [%s] Error appending table to table set",
table.Name())
return emptyBuffer, err
}
}
// Add header information to the tail end of the buffer array.
var tableSetHeader TableSetExported
tableSetHeader.TableSetName = tableSet.tableSetName
tableSetHeader.FileName = tableSet.fileName
var encodedHeader bytes.Buffer
var enc *gob.Encoder = gob.NewEncoder(&encodedHeader)
err = enc.Encode(tableSetHeader)
if err != nil {
return emptyBuffer, err
}
encodedTableSet = append(encodedTableSet, encodedHeader)
var headerIndex int = len(tableSet.tables)
if len(encodedTableSet) != headerIndex+1 {
err = fmt.Errorf("GobEncode(): error appending table set header to table set")
return emptyBuffer, err
}
return encodedTableSet, nil
}
/*
Reconstruct a TableSet from a slice of []bytes.Buffer
Each element in the slice is a Gob encoded table as a slice of []byte
Calls GobDecodeTableSet(buffer)
Note: GOB encoding and decoding does not (yet) support nested tables or type time.Time.
*/
func NewTableSetFromGob(buffer []bytes.Buffer) (*TableSet, error) {
return GobDecodeTableSet(buffer)
}
/*
Reconstruct a TableSet from a slice of []bytes.Buffer
Each element in the slice is a Gob encoded table as a slice of []byte
Equivalent to NewTableSetFromGob()
Note: GOB encoding and decoding does not (yet) support nested tables or type time.Time.
*/
func GobDecodeTableSet(buffer []bytes.Buffer) (*TableSet, error) {
var tableSet *TableSet
var err error
tableSet, err = NewTableSet("")
if err != nil {
return nil, err
}
var table *Table
var tableCount = len(buffer) - 1 // The tail end buffer element is the TableSet header.
for tableIndex := 0; tableIndex < tableCount; tableIndex++ {
table, err = GobDecodeTable(buffer[tableIndex].Bytes())
if err != nil {
return nil, err
}
err = tableSet.AppendTable(table)
if err != nil {
return nil, err
}
}
// Decode and restore the header.
var headerIndex int = len(buffer) - 1
var encodedHeader bytes.Buffer = buffer[headerIndex]
var dec *gob.Decoder = gob.NewDecoder(&encodedHeader)
var tableSetHeader TableSetExported
err = dec.Decode(&tableSetHeader)
if err != nil {
return nil, err
}
tableSet.tableSetName = tableSetHeader.TableSetName
tableSet.fileName = tableSetHeader.FileName
return tableSet, nil
}
/*
Reconstruct a Table from a slice of []byte
Calls GobDecodeTable([]byte)
Note: GOB encoding and decoding does not (yet) support nested tables or type time.Time.
*/
func NewTableFromGob(buf []byte) (*Table, error) {
return GobDecodeTable(buf)
}
/*
Reconstruct a Table from a slice of []byte
Equivalent to NewTableFromGob()
Note: GOB encoding and decoding does not (yet) support nested tables or type time.Time.
*/
func GobDecodeTable(buf []byte) (*Table, error) {
var err error
var tableDecoded *Table
var buffer *bytes.Buffer
buffer = bytes.NewBuffer(buf)
var dec *gob.Decoder = gob.NewDecoder(buffer)
var tableExported *TableExported
err = dec.Decode(&tableExported)
if err != nil {
return nil, err
}
tableDecoded, err = tableExported.importTable()
if err != nil {
return nil, err
}
return tableDecoded, nil
}