querycoordinator

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 6, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client interface {
	// Name returns unique DSName of the client
	Name() string
	// Exec runs a SQL query against ObjectServer
	Exec(ctx context.Context, query models.Query, user models.Credentials) models.QueryResult
}

type QueryCoordinator

type QueryCoordinator struct {
	// contains filtered or unexported fields
}

QueryCoordinator provides methods to run SQL query against a few ObjectServers concurrently and collect all results

func NewQueryCoordinator

func NewQueryCoordinator(client Client, clients ...Client) *QueryCoordinator

NewQueryCoordinator creates a ready to use instance of coordinator. At least one ObjectServer client must be provided

func (*QueryCoordinator) ClientNames

func (q *QueryCoordinator) ClientNames() (names []string)

ClientNames returns names of all configured clients

Example
client1 := &DemoClient{"ds1", RowSet{}}
client2 := &DemoClient{"ds2", RowSet{}}

coordinator := NewQueryCoordinator(client1, client2)

names := coordinator.ClientNames()

sort.Strings(names)
fmt.Println(names)
Output:

[ds1 ds2]

func (*QueryCoordinator) Exec

func (q *QueryCoordinator) Exec(
	ctx context.Context,
	query models.Query,
	user models.Credentials,
	clientNames ...string,
) map[string]models.QueryResult

Exec runs the given query on the specified NCOS clients and returns the query results for each of them.

If no client names are provided, the query will be executed on all available clients. All unknown client names will be ignored

Example
client1 := &DemoClient{
	DSName: "ds1",
	DBTable: RowSet{
		Columns: []string{"id", "name"},
		Rows:    [][]any{{1, "John"}, {2, "Jane"}},
	},
}

client2 := &DemoClient{
	DSName: "ds2",
	DBTable: RowSet{
		Columns: []string{"id", "name"},
		Rows:    [][]any{{1, "Bob"}},
	},
}

coordinator := NewQueryCoordinator(client1, client2)

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

result := coordinator.Exec(
	ctx,
	Query{SQL: "select * from dbtable"},
	Credentials{UserName: "alice", Password: "secret"},
)

resultJson, _ := json.Marshal(result)
fmt.Println(string(resultJson))
Output:

{"ds1":{"row_set":{"Columns":["id","name"],"Rows":[[1,"John"],[2,"Jane"]]},"affected_rows":0,"error":null},"ds2":{"row_set":{"Columns":["id","name"],"Rows":[[1,"Bob"]]},"affected_rows":0,"error":null}}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL