-
Notifications
You must be signed in to change notification settings - Fork 78
/
inputtable_stub.go
183 lines (159 loc) · 7.88 KB
/
inputtable_stub.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
package client
import (
"context"
"github.com/apache/arrow/go/v8/arrow"
"github.com/apache/arrow/go/v8/arrow/flight"
"github.com/apache/arrow/go/v8/arrow/memory"
inputtablepb2 "github.com/deephaven/deephaven-core/go/internal/proto/inputtable"
tablepb2 "github.com/deephaven/deephaven-core/go/internal/proto/table"
ticketpb2 "github.com/deephaven/deephaven-core/go/internal/proto/ticket"
)
// AppendOnlyInputTable is a handle to an append-only input table on the server.
// The only difference between this handle and a normal table handle is the ability
// to add data to it using AddTable.
type AppendOnlyInputTable struct {
*TableHandle
}
// KeyBackedInputTable is a handle to a key-backed input table on the server.
// The only difference between this handle and a normal table handle is the ability
// to add and remove data to and from it using AddTable and DeleteTable.
type KeyBackedInputTable struct {
*TableHandle
}
// inputTableStub wraps gRPC calls for inputtable.proto.
type inputTableStub struct {
client *Client
stub inputtablepb2.InputTableServiceClient // The stub for performing inputtable gRPC requests.
}
func newInputTableStub(client *Client) *inputTableStub {
return &inputTableStub{client: client, stub: inputtablepb2.NewInputTableServiceClient(client.grpcChannel)}
}
type inputTableKind = tablepb2.CreateInputTableRequest_InputTableKind
// makeInputTableRequestFromSchema is just a shorthand method to construct a CreateInputTableRequest,
// since it's a very verbose process.
func makeInputTableRequestFromSchema(kind *inputTableKind, resultId *ticketpb2.Ticket, schema *arrow.Schema) *tablepb2.CreateInputTableRequest {
schemaBytes := flight.SerializeSchema(schema, memory.DefaultAllocator)
def := &tablepb2.CreateInputTableRequest_Schema{Schema: schemaBytes}
return &tablepb2.CreateInputTableRequest{ResultId: resultId, Definition: def, Kind: kind}
}
// makeInputTableRequestFromSchema is just a shorthand method to construct a CreateInputTableRequest,
// since it's a very verbose process.
func makeInputTableRequestFromTable(kind *inputTableKind, resultId *ticketpb2.Ticket, table *TableHandle) *tablepb2.CreateInputTableRequest {
def := &tablepb2.CreateInputTableRequest_SourceTableId{SourceTableId: &tablepb2.TableReference{Ref: &tablepb2.TableReference_Ticket{Ticket: table.ticket}}}
return &tablepb2.CreateInputTableRequest{ResultId: resultId, Definition: def, Kind: kind}
}
// NewAppendOnlyInputTableFromSchema creates a new append-only input table with columns according to the provided schema.
func (its *inputTableStub) NewAppendOnlyInputTableFromSchema(ctx context.Context, schema *arrow.Schema) (*AppendOnlyInputTable, error) {
kind := inputTableKind{Kind: &tablepb2.CreateInputTableRequest_InputTableKind_InMemoryAppendOnly_{
InMemoryAppendOnly: &tablepb2.CreateInputTableRequest_InputTableKind_InMemoryAppendOnly{},
}}
resultId := its.client.ticketFact.newTicket()
req := makeInputTableRequestFromSchema(&kind, &resultId, schema)
newTable, err := its.client.tableStub.createInputTable(ctx, req)
if err != nil {
return nil, err
}
return &AppendOnlyInputTable{TableHandle: newTable}, nil
}
// NewAppendOnlyInputTableFromTable creates a new append-only input table with the same columns as the provided table.
func (its *inputTableStub) NewAppendOnlyInputTableFromTable(ctx context.Context, table *TableHandle) (*AppendOnlyInputTable, error) {
if !table.IsValid() {
return nil, ErrInvalidTableHandle
}
kind := inputTableKind{Kind: &tablepb2.CreateInputTableRequest_InputTableKind_InMemoryAppendOnly_{
InMemoryAppendOnly: &tablepb2.CreateInputTableRequest_InputTableKind_InMemoryAppendOnly{},
}}
resultId := its.client.ticketFact.newTicket()
req := makeInputTableRequestFromTable(&kind, &resultId, table)
newTable, err := its.client.tableStub.createInputTable(ctx, req)
if err != nil {
return nil, err
}
return &AppendOnlyInputTable{TableHandle: newTable}, nil
}
// NewKeyBackedInputTableFromSchema creates a new key-backed input table with columns according to the provided schema.
// The columns to use as the keys are specified by keyColumns.
func (its *inputTableStub) NewKeyBackedInputTableFromSchema(ctx context.Context, schema *arrow.Schema, keyColumns ...string) (*KeyBackedInputTable, error) {
kind := inputTableKind{Kind: &tablepb2.CreateInputTableRequest_InputTableKind_InMemoryKeyBacked_{
InMemoryKeyBacked: &tablepb2.CreateInputTableRequest_InputTableKind_InMemoryKeyBacked{KeyColumns: keyColumns},
}}
resultId := its.client.ticketFact.newTicket()
req := makeInputTableRequestFromSchema(&kind, &resultId, schema)
newTable, err := its.client.tableStub.createInputTable(ctx, req)
if err != nil {
return nil, err
}
return &KeyBackedInputTable{TableHandle: newTable}, nil
}
// NewKeyBackedInputTableFromTable creates a new key-backed input table with the same columns as the provided table.
// The columns to use as the keys are specified by keyColumns.
func (its *inputTableStub) NewKeyBackedInputTableFromTable(ctx context.Context, table *TableHandle, keyColumns ...string) (*KeyBackedInputTable, error) {
if !table.IsValid() {
return nil, ErrInvalidTableHandle
}
kind := inputTableKind{Kind: &tablepb2.CreateInputTableRequest_InputTableKind_InMemoryKeyBacked_{
InMemoryKeyBacked: &tablepb2.CreateInputTableRequest_InputTableKind_InMemoryKeyBacked{KeyColumns: keyColumns},
}}
resultId := its.client.ticketFact.newTicket()
req := makeInputTableRequestFromTable(&kind, &resultId, table)
newTable, err := its.client.tableStub.createInputTable(ctx, req)
if err != nil {
return nil, err
}
return &KeyBackedInputTable{TableHandle: newTable}, nil
}
// AddTable appends data from the given table to the end of this table.
// This will automatically update all tables derived from this one.
func (th *AppendOnlyInputTable) AddTable(ctx context.Context, toAdd *TableHandle) error {
if !th.IsValid() || !toAdd.IsValid() {
return ErrInvalidTableHandle
}
return th.client.inputTableStub.addTable(ctx, th.TableHandle, toAdd)
}
// AddTable merges the keys from the given table into this table.
// If a key does not exist in the current table, the entire row is added,
// otherwise the new data row replaces the existing key.
// This will automatically update all tables derived from this one.
func (th *KeyBackedInputTable) AddTable(ctx context.Context, toAdd *TableHandle) error {
if !th.IsValid() || !toAdd.IsValid() {
return ErrInvalidTableHandle
}
return th.client.inputTableStub.addTable(ctx, th.TableHandle, toAdd)
}
// addTable makes the AddTableToInputTable gRPC request.
// See the docs for AddTable on each kind of table for details.
func (its *inputTableStub) addTable(ctx context.Context, inputTable *TableHandle, toAdd *TableHandle) error {
ctx, err := its.client.tokenMgr.withToken(ctx)
if err != nil {
return err
}
req := inputtablepb2.AddTableRequest{InputTable: inputTable.ticket, TableToAdd: toAdd.ticket}
_, err = its.stub.AddTableToInputTable(ctx, &req)
if err != nil {
return nil
}
return nil
}
// DeleteTable deletes the rows with the given keys from this table.
// The provided table must consist only of columns that were specified as key columns in the input table.
// This will automatically update all tables derived from this one.
func (th *KeyBackedInputTable) DeleteTable(ctx context.Context, toDelete *TableHandle) error {
if !th.IsValid() || !toDelete.IsValid() {
return ErrInvalidTableHandle
}
return th.client.inputTableStub.deleteTable(ctx, th.TableHandle, toDelete)
}
// deleteTable makes the DeleteTableFromInputTable gRPC request.
// See the docs for DeleteTable for details.
func (its *inputTableStub) deleteTable(ctx context.Context, inputTable *TableHandle, toRemove *TableHandle) error {
ctx, err := its.client.tokenMgr.withToken(ctx)
if err != nil {
return err
}
req := inputtablepb2.DeleteTableRequest{InputTable: inputTable.ticket, TableToRemove: toRemove.ticket}
_, err = its.stub.DeleteTableFromInputTable(ctx, &req)
if err != nil {
return nil
}
return nil
}