This repository has been archived by the owner on Feb 2, 2019. It is now read-only.
forked from kikinteractive/go-bqstreamer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
error_insert.go
59 lines (55 loc) · 1.91 KB
/
error_insert.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
package bqstreamer
// InsertErrors is returned from an insert attempt.
// It provides functions to iterate over errors relating to an insert
// operation.
//
// BigQuery has a complex error hierarchy:
//
// Insert --> Tables --> Table Insert Attempt --> Rows --> Row Error
//
// During an insert operation, multiple tables are inserted in bulk
// into BigQuery using a separate request for each table.
// Each table-specific insert operation is comprised from multiple
// insert attempts (requests).
// Every table insert can be retried until an attempt is successful
// or too many attempts have failed. Failures can occur for various
// reasons e.g. server errors, malformed payload, etc.
//
// This interface allows to iterate over all tables, attempts, rows,
// and row errors associated with an insert operation.
type InsertErrors struct {
Tables []*TableInsertErrors
}
// Next iterates over all inserted tables once,
// returning a single TableInsertErrors every call.
// Calling Next() multiple times will consequently return more tables,
// until all have been returned.
//
// The function returns true if a non-nil value was fetched.
// Once the iterator has been exhausted, (nil, false) will be returned
// on every subsequent call.
func (insert *InsertErrors) Next() (*TableInsertErrors, bool) {
if len(insert.Tables) == 0 {
return nil, false
}
// Return elements in reverse order for memory efficiency.
var table *TableInsertErrors
table, insert.Tables = insert.Tables[len(insert.Tables)-1], insert.Tables[:len(insert.Tables)-1]
return table, true
}
// All returns all remaining tables
// (those that have not been iterated over using Next()).
//
// Calling Next() or All() again afterwards will yield a failed (empty)
// result.
func (insert *InsertErrors) All() []*TableInsertErrors {
var tables []*TableInsertErrors
for {
table, ok := insert.Next()
if !ok {
break
}
tables = append(tables, table)
}
return tables
}