Repository navigation
Expand file tree
/
Copy pathinfinity.go
More file actions
835 lines (718 loc) · 22.2 KB
/
Copy pathinfinity.go
File metadata and controls
835 lines (718 loc) · 22.2 KB
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
// Copyright(C) 2026 InfiniFlow, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package infinity
import (
"context"
"fmt"
"github.com/apache/thrift/lib/go/thrift"
thriftapi "github.com/infiniflow/infinity-go-sdk/internal/thrift"
)
// ClientVersion is the version of this Go SDK client
// This should match the protocol version expected by the server
const ClientVersion = 37 // version: 0.7.5
// Connect creates a new connection to Infinity
// This is a factory function that creates the appropriate connection type based on the URI
// For now, only NetworkAddress (remote thrift connection) is supported
//
// Example:
//
// conn, err := infinity.Connect(infinity.LocalHost)
// if err != nil {
// log.Fatal(err)
// }
// defer conn.Disconnect()
func Connect(uri URI) (*InfinityConnection, error) {
switch u := uri.(type) {
case NetworkAddress:
return NewInfinityConnectionWithConfig(u, &thrift.TConfiguration{})
default:
return nil, NewInfinityException(
int(ErrorCodeInvalidServerAddress),
fmt.Sprintf("Unknown uri type: %T", uri),
)
}
}
// InfinityConnection represents a connection to Infinity server
type InfinityConnection struct {
uri URI
transport thrift.TTransport
client *thriftapi.InfinityServiceClient
sessionID int64
isConnected bool
dbName string
}
// NewInfinityConnection creates a new connection to Infinity server
func NewInfinityConnection(address NetworkAddress) (*InfinityConnection, error) {
return NewInfinityConnectionWithConfig(address, &thrift.TConfiguration{})
}
// NewInfinityConnectionWithConfig creates a new connection to Infinity server with a custom thrift configuration.
func NewInfinityConnectionWithConfig(address NetworkAddress, conf *thrift.TConfiguration) (*InfinityConnection, error) {
if conf == nil {
conf = &thrift.TConfiguration{}
}
// Create thrift transport
// Use TBufferedTransport for sync communication (matching Python's TBufferedTransport)
transport := thrift.NewTSocketConf(fmt.Sprintf("%s:%d", address.IP, address.Port), conf)
// Use buffered transport
bufferedTransport := thrift.NewTBufferedTransport(transport, 8192)
// Create binary protocol (matching Python's TBinaryProtocol)
protocolFactory := thrift.NewTBinaryProtocolFactoryConf(conf)
protocol := protocolFactory.GetProtocol(bufferedTransport)
// Create client
client := thriftapi.NewInfinityServiceClientProtocol(bufferedTransport, protocol, protocol)
// Open transport
if err := bufferedTransport.Open(); err != nil {
return nil, NewInfinityException(
transportErrorCode(err),
fmt.Sprintf("Failed to connect to server at %s:%d: %v", address.IP, address.Port, err),
)
}
// Create connection object
conn := &InfinityConnection{
uri: address,
transport: bufferedTransport,
client: client,
isConnected: true,
dbName: "default_db",
}
// Perform connect handshake with server
ctx := context.Background()
connectReq := thriftapi.NewConnectRequest()
connectReq.ClientVersion = ClientVersion
resp, err := client.Connect(ctx, connectReq)
if err != nil {
bufferedTransport.Close()
return nil, NewInfinityException(
transportErrorCode(err),
fmt.Sprintf("Failed to connect: %v", err),
)
}
// Check response error code
if resp.ErrorCode != 0 {
bufferedTransport.Close()
return nil, NewInfinityException(
int(resp.ErrorCode),
fmt.Sprintf("Server rejected connection: %s", resp.ErrorMsg),
)
}
// Store session ID from server
conn.sessionID = resp.SessionID
return conn, nil
}
// Disconnect closes the connection
func (c *InfinityConnection) Disconnect() (interface{}, error) {
if !c.isConnected {
return nil, NewInfinityException(int(ErrorCodeClientClose), "Connection already closed")
}
// Send disconnect request to server
ctx := context.Background()
disconnectReq := thriftapi.NewCommonRequest()
disconnectReq.SessionID = c.sessionID
resp, err := c.client.Disconnect(ctx, disconnectReq)
if err != nil {
// Log error but still close transport
fmt.Printf("Warning: error during disconnect: %v\n", err)
} else if resp.ErrorCode != 0 {
// Log server error but still close transport
fmt.Printf("Warning: server error during disconnect: %s\n", resp.ErrorMsg)
}
// Close transport
if c.transport != nil {
c.transport.Close()
}
c.isConnected = false
c.sessionID = 0
return struct{}{}, nil
}
// IsConnected returns whether the connection is still active
func (c *InfinityConnection) IsConnected() bool {
if !c.isConnected {
return false
}
// Also check if the transport is still open
if c.transport != nil && !c.transport.IsOpen() {
c.isConnected = false
return false
}
return true
}
// GetSessionID returns the session ID
func (c *InfinityConnection) GetSessionID() int64 {
return c.sessionID
}
// SetSessionID adopts an existing session id on this connection.
//
// The reference keeps sessions in a static map keyed by id, so a client can
// reconnect and carry on in the session it opened on a previous socket; only
// `Disconnect` ends it. This is the setter that makes that possible.
func (c *InfinityConnection) SetSessionID(sessionID int64) {
c.sessionID = sessionID
}
// CreateDatabase creates a new database
func (c *InfinityConnection) CreateDatabase(dbName string, conflictType ConflictType, comment string) (*Database, error) {
if !c.isConnected {
return nil, NewInfinityException(int(ErrorCodeClientClose), "Connection is closed")
}
// Convert ConflictType to CreateConflict
var createConflict thriftapi.CreateConflict
switch conflictType {
case ConflictTypeIgnore:
createConflict = thriftapi.CreateConflict_Ignore
case ConflictTypeError:
createConflict = thriftapi.CreateConflict_Error
case ConflictTypeReplace:
createConflict = thriftapi.CreateConflict_Replace
default:
return nil, NewInfinityException(int(ErrorCodeInvalidConflictType), "Invalid conflict type")
}
// Create request
req := thriftapi.NewCreateDatabaseRequest()
req.DbName = dbName
req.SessionID = c.GetSessionID()
req.CreateOption = &thriftapi.CreateOption{
ConflictType: createConflict,
Properties: []*thriftapi.Property{}, // empty properties
}
req.DbComment = comment
// Call thrift
ctx := context.Background()
resp, err := c.client.CreateDatabase(ctx, req)
if err != nil {
return nil, NewInfinityException(
transportErrorCode(err),
fmt.Sprintf("Failed to create database: %v", err),
)
}
// Check response error code
if resp.ErrorCode != 0 {
return nil, NewInfinityException(
int(resp.ErrorCode),
fmt.Sprintf("Failed to create database: %s", resp.ErrorMsg),
)
}
return &Database{conn: c, dbName: dbName}, nil
}
// ListDatabases lists all databases
func (c *InfinityConnection) ListDatabases() (interface{}, error) {
if !c.isConnected {
return nil, NewInfinityException(int(ErrorCodeClientClose), "Connection is closed")
}
// Create request
req := thriftapi.NewListDatabaseRequest()
req.SessionID = c.GetSessionID()
// Call thrift
ctx := context.Background()
resp, err := c.client.ListDatabase(ctx, req)
if err != nil {
return nil, NewInfinityException(
transportErrorCode(err),
fmt.Sprintf("Failed to list databases: %v", err),
)
}
// Check response error code
if resp.ErrorCode != 0 {
return nil, NewInfinityException(
int(resp.ErrorCode),
fmt.Sprintf("Failed to list databases: %s", resp.ErrorMsg),
)
}
return resp, nil
}
// ShowDatabase shows details of a specific database
func (c *InfinityConnection) ShowDatabase(dbName string) (interface{}, error) {
if !c.isConnected {
return nil, NewInfinityException(int(ErrorCodeClientClose), "Connection is closed")
}
// Create request
req := thriftapi.NewShowDatabaseRequest()
req.DbName = dbName
req.SessionID = c.GetSessionID()
// Call thrift
ctx := context.Background()
resp, err := c.client.ShowDatabase(ctx, req)
if err != nil {
return nil, NewInfinityException(
transportErrorCode(err),
fmt.Sprintf("Failed to show database: %v", err),
)
}
// Check response error code
if resp.ErrorCode != 0 {
return nil, NewInfinityException(
int(resp.ErrorCode),
fmt.Sprintf("Failed to show database: %s", resp.ErrorMsg),
)
}
return resp, nil
}
// DropDatabase drops a database
func (c *InfinityConnection) DropDatabase(dbName string, conflictType ConflictType) (interface{}, error) {
if !c.isConnected {
return nil, NewInfinityException(int(ErrorCodeClientClose), "Connection is closed")
}
// Convert ConflictType to DropConflict
var dropConflict thriftapi.DropConflict
switch conflictType {
case ConflictTypeIgnore:
dropConflict = thriftapi.DropConflict_Ignore
case ConflictTypeError:
dropConflict = thriftapi.DropConflict_Error
default:
return nil, NewInfinityException(int(ErrorCodeInvalidConflictType), "Invalid conflict type for drop")
}
// Create request
req := thriftapi.NewDropDatabaseRequest()
req.DbName = dbName
req.SessionID = c.GetSessionID()
req.DropOption = &thriftapi.DropOption{
ConflictType: dropConflict,
}
// Call thrift
ctx := context.Background()
resp, err := c.client.DropDatabase(ctx, req)
if err != nil {
return nil, NewInfinityException(
transportErrorCode(err),
fmt.Sprintf("Failed to drop database: %v", err),
)
}
// Check response error code
if resp.ErrorCode != 0 {
return nil, NewInfinityException(
int(resp.ErrorCode),
fmt.Sprintf("Failed to drop database: %s", resp.ErrorMsg),
)
}
return resp, nil
}
// GetDatabase gets a database object
func (c *InfinityConnection) GetDatabase(dbName string) (*Database, error) {
if !c.isConnected {
return nil, NewInfinityException(int(ErrorCodeClientClose), "Connection is closed")
}
// Create get database request
req := thriftapi.NewGetDatabaseRequest()
req.DbName = dbName
req.SessionID = c.GetSessionID()
// Call thrift
ctx := context.Background()
resp, err := c.client.GetDatabase(ctx, req)
if err != nil {
return nil, NewInfinityException(
transportErrorCode(err),
fmt.Sprintf("Failed to get database: %v", err),
)
}
// Check response error code
if resp.ErrorCode != 0 {
return nil, NewInfinityException(
int(resp.ErrorCode),
fmt.Sprintf("Failed to get database: %s", resp.ErrorMsg),
)
}
return &Database{conn: c, dbName: dbName}, nil
}
// ShowCurrentNode shows current node information
func (c *InfinityConnection) ShowCurrentNode() (interface{}, error) {
if !c.isConnected {
return nil, NewInfinityException(int(ErrorCodeClientClose), "Connection is closed")
}
// Create request
req := thriftapi.NewShowCurrentNodeRequest()
req.SessionID = c.GetSessionID()
// Call thrift
ctx := context.Background()
resp, err := c.client.ShowCurrentNode(ctx, req)
if err != nil {
return nil, NewInfinityException(
transportErrorCode(err),
fmt.Sprintf("Failed to show current node: %v", err),
)
}
// Check response error code
if resp.ErrorCode != 0 {
return nil, NewInfinityException(
int(resp.ErrorCode),
fmt.Sprintf("Failed to show current node: %s", resp.ErrorMsg),
)
}
return resp, nil
}
// CreateDatabaseSnapshot creates a snapshot of a database
func (c *InfinityConnection) CreateDatabaseSnapshot(snapshotName string, dbName string) (interface{}, error) {
if !c.isConnected {
return nil, NewInfinityException(int(ErrorCodeClientClose), "Connection is closed")
}
// Create request
req := thriftapi.NewCreateDatabaseSnapshotRequest()
req.SessionID = c.GetSessionID()
req.DbName = dbName
req.SnapshotName = snapshotName
// Call thrift
ctx := context.Background()
resp, err := c.client.CreateDatabaseSnapshot(ctx, req)
if err != nil {
return nil, NewInfinityException(
transportErrorCode(err),
fmt.Sprintf("Failed to create database snapshot: %v", err),
)
}
// Check response error code
if resp.ErrorCode != 0 {
return nil, NewInfinityException(
int(resp.ErrorCode),
fmt.Sprintf("Failed to create database snapshot: %s", resp.ErrorMsg),
)
}
return resp, nil
}
// RestoreDatabaseSnapshot restores a database from a snapshot
func (c *InfinityConnection) RestoreDatabaseSnapshot(snapshotName string) (interface{}, error) {
if !c.isConnected {
return nil, NewInfinityException(int(ErrorCodeClientClose), "Connection is closed")
}
// Create request
req := thriftapi.NewRestoreSnapshotRequest()
req.SessionID = c.GetSessionID()
req.SnapshotName = snapshotName
req.Scope = "database"
// Call thrift
ctx := context.Background()
resp, err := c.client.RestoreSnapshot(ctx, req)
if err != nil {
return nil, NewInfinityException(
transportErrorCode(err),
fmt.Sprintf("Failed to restore database snapshot: %v", err),
)
}
// Check response error code
if resp.ErrorCode != 0 {
return nil, NewInfinityException(
int(resp.ErrorCode),
fmt.Sprintf("Failed to restore database snapshot: %s", resp.ErrorMsg),
)
}
return resp, nil
}
// CreateSystemSnapshot creates a system-wide snapshot
func (c *InfinityConnection) CreateSystemSnapshot(snapshotName string) (interface{}, error) {
if !c.isConnected {
return nil, NewInfinityException(int(ErrorCodeClientClose), "Connection is closed")
}
// Create request
req := thriftapi.NewCreateSystemSnapshotRequest()
req.SessionID = c.GetSessionID()
req.SnapshotName = snapshotName
// Call thrift
ctx := context.Background()
resp, err := c.client.CreateSystemSnapshot(ctx, req)
if err != nil {
return nil, NewInfinityException(
transportErrorCode(err),
fmt.Sprintf("Failed to create system snapshot: %v", err),
)
}
// Check response error code
if resp.ErrorCode != 0 {
return nil, NewInfinityException(
int(resp.ErrorCode),
fmt.Sprintf("Failed to create system snapshot: %s", resp.ErrorMsg),
)
}
return resp, nil
}
// RestoreSystemSnapshot restores the system from a snapshot
func (c *InfinityConnection) RestoreSystemSnapshot(snapshotName string) (interface{}, error) {
if !c.isConnected {
return nil, NewInfinityException(int(ErrorCodeClientClose), "Connection is closed")
}
// Create request
req := thriftapi.NewRestoreSnapshotRequest()
req.SessionID = c.GetSessionID()
req.SnapshotName = snapshotName
req.Scope = "system"
// Call thrift
ctx := context.Background()
resp, err := c.client.RestoreSnapshot(ctx, req)
if err != nil {
return nil, NewInfinityException(
transportErrorCode(err),
fmt.Sprintf("Failed to restore system snapshot: %v", err),
)
}
// Check response error code
if resp.ErrorCode != 0 {
return nil, NewInfinityException(
int(resp.ErrorCode),
fmt.Sprintf("Failed to restore system snapshot: %s", resp.ErrorMsg),
)
}
return resp, nil
}
// ListSnapshots lists all snapshots
func (c *InfinityConnection) ListSnapshots() (interface{}, error) {
if !c.isConnected {
return nil, NewInfinityException(int(ErrorCodeClientClose), "Connection is closed")
}
// Create request
req := thriftapi.NewListSnapshotsRequest()
req.SessionID = c.GetSessionID()
// Call thrift
ctx := context.Background()
resp, err := c.client.ListSnapshots(ctx, req)
if err != nil {
return nil, NewInfinityException(
transportErrorCode(err),
fmt.Sprintf("Failed to list snapshots: %v", err),
)
}
// Check response error code
if resp.ErrorCode != 0 {
return nil, NewInfinityException(
int(resp.ErrorCode),
fmt.Sprintf("Failed to list snapshots: %s", resp.ErrorMsg),
)
}
return resp, nil
}
// ShowSnapshot shows details of a specific snapshot
func (c *InfinityConnection) ShowSnapshot(snapshotName string) (interface{}, error) {
if !c.isConnected {
return nil, NewInfinityException(int(ErrorCodeClientClose), "Connection is closed")
}
// Create request
req := thriftapi.NewShowSnapshotRequest()
req.SessionID = c.GetSessionID()
req.SnapshotName = snapshotName
// Call thrift
ctx := context.Background()
resp, err := c.client.ShowSnapshot(ctx, req)
if err != nil {
return nil, NewInfinityException(
transportErrorCode(err),
fmt.Sprintf("Failed to show snapshot: %v", err),
)
}
// Check response error code
if resp.ErrorCode != 0 {
return nil, NewInfinityException(
int(resp.ErrorCode),
fmt.Sprintf("Failed to show snapshot: %s", resp.ErrorMsg),
)
}
return resp, nil
}
// DropSnapshot drops a snapshot
func (c *InfinityConnection) DropSnapshot(snapshotName string) (interface{}, error) {
if !c.isConnected {
return nil, NewInfinityException(int(ErrorCodeClientClose), "Connection is closed")
}
// Create request
req := thriftapi.NewDropSnapshotRequest()
req.SessionID = c.GetSessionID()
req.SnapshotName = snapshotName
// Call thrift
ctx := context.Background()
resp, err := c.client.DropSnapshot(ctx, req)
if err != nil {
return nil, NewInfinityException(
transportErrorCode(err),
fmt.Sprintf("Failed to drop snapshot: %v", err),
)
}
// Check response error code
if resp.ErrorCode != 0 {
return nil, NewInfinityException(
int(resp.ErrorCode),
fmt.Sprintf("Failed to drop snapshot: %s", resp.ErrorMsg),
)
}
return resp, nil
}
// Cleanup performs cleanup operations
func (c *InfinityConnection) Cleanup() (interface{}, error) {
if !c.isConnected {
return nil, NewInfinityException(int(ErrorCodeClientClose), "Connection is closed")
}
// TODO: Implement thrift call
return nil, nil
}
// Optimize optimizes a table
func (c *InfinityConnection) Optimize(dbName string, tableName string) (interface{}, error) {
if !c.isConnected {
return nil, NewInfinityException(int(ErrorCodeClientClose), "Connection is closed")
}
// TODO: Implement thrift call
return nil, nil
}
// TestCommand executes a test command
func (c *InfinityConnection) TestCommand(commandContent string) error {
if !c.isConnected {
return NewInfinityException(int(ErrorCodeClientClose), "Connection is closed")
}
// TODO: Implement thrift call
return nil
}
// FlushData flushes data to disk
func (c *InfinityConnection) FlushData() error {
if !c.isConnected {
return NewInfinityException(int(ErrorCodeClientClose), "Connection is closed")
}
// Create request
req := thriftapi.NewFlushRequest()
req.SessionID = c.GetSessionID()
req.FlushType = "data"
// Call thrift
ctx := context.Background()
resp, err := c.client.Flush(ctx, req)
if err != nil {
return NewInfinityException(
transportErrorCode(err),
fmt.Sprintf("Failed to flush data: %v", err),
)
}
// Check response error code
if resp.ErrorCode != 0 {
return NewInfinityException(
int(resp.ErrorCode),
fmt.Sprintf("Failed to flush data: %s", resp.ErrorMsg),
)
}
return nil
}
// FlushCatalog flushes catalog to disk
func (c *InfinityConnection) FlushCatalog() error {
if !c.isConnected {
return NewInfinityException(int(ErrorCodeClientClose), "Connection is closed")
}
// TODO: Implement thrift call
return nil
}
// SetConfig sets a configuration value
func (c *InfinityConnection) SetConfig(configName string, configValue interface{}) (interface{}, error) {
if !c.isConnected {
return nil, NewInfinityException(int(ErrorCodeClientClose), "Connection is closed")
}
// Convert configValue to thrift ConfigValue
thriftConfigValue, err := configValueFromInterface(configValue)
if err != nil {
return nil, NewInfinityException(
int(ErrorCodeInvalidParameterValue),
fmt.Sprintf("Invalid config value: %v", err),
)
}
// Create request
req := thriftapi.NewSetConfigRequest()
req.SessionID = c.GetSessionID()
req.ConfigName = configName
req.ConfigValue = thriftConfigValue
// Call thrift
ctx := context.Background()
resp, err := c.client.SetConfig(ctx, req)
if err != nil {
return nil, NewInfinityException(
transportErrorCode(err),
fmt.Sprintf("Failed to set config: %v", err),
)
}
// Check response error code
if resp.ErrorCode != 0 {
return nil, NewInfinityException(
int(resp.ErrorCode),
fmt.Sprintf("Failed to set config: %s", resp.ErrorMsg),
)
}
return resp, nil
}
// ShowConfig shows a configuration value
func (c *InfinityConnection) ShowConfig(configName string) (*ConfigResponse, error) {
if !c.isConnected {
return nil, NewInfinityException(int(ErrorCodeClientClose), "Connection is closed")
}
// Create request
req := thriftapi.NewShowConfigRequest()
req.SessionID = c.GetSessionID()
req.ConfigName = configName
// Call thrift
ctx := context.Background()
resp, err := c.client.ShowConfig(ctx, req)
if err != nil {
return nil, NewInfinityException(
transportErrorCode(err),
fmt.Sprintf("Failed to show config: %v", err),
)
}
// Check response error code
if resp.ErrorCode != 0 {
return nil, NewInfinityException(
int(resp.ErrorCode),
fmt.Sprintf("Failed to show config: %s", resp.ErrorMsg),
)
}
// Extract config value from thrift ConfigValue
var configValue interface{}
if resp.ConfigValue != nil {
if resp.ConfigValue.StringValue != nil {
configValue = *resp.ConfigValue.StringValue
} else if resp.ConfigValue.IntValue != nil {
configValue = *resp.ConfigValue.IntValue
} else if resp.ConfigValue.BoolValue != nil {
configValue = *resp.ConfigValue.BoolValue
} else if resp.ConfigValue.DoubleValue != nil {
configValue = *resp.ConfigValue.DoubleValue
} else {
configValue = nil
}
} else {
configValue = nil
}
return &ConfigResponse{
ErrorCode: ErrorCode(resp.ErrorCode),
ErrorMsg: resp.ErrorMsg,
ConfigName: resp.ConfigName,
ConfigValue: configValue,
}, nil
}
// ConfigResponse represents the response from show_config
type ConfigResponse struct {
ErrorCode ErrorCode
ErrorMsg string
ConfigName string
ConfigValue interface{}
}
// configValueFromInterface converts a Go interface{} to a thrift ConfigValue
func configValueFromInterface(v interface{}) (*thriftapi.ConfigValue, error) {
cv := thriftapi.NewConfigValue()
switch val := v.(type) {
case string:
cv.StringValue = &val
case int64:
cv.IntValue = &val
case int:
// Convert int to int64
i64 := int64(val)
cv.IntValue = &i64
case bool:
cv.BoolValue = &val
case float64:
cv.DoubleValue = &val
case float32:
// Convert float32 to float64
f64 := float64(val)
cv.DoubleValue = &f64
default:
return nil, fmt.Errorf("unsupported config value type %T", v)
}
return cv, nil
}