components-contrib/state
ItalyPaleAle 55b6a04805 Merge branch 'master' of https://github.com/dapr/components-contrib into newdeps
Signed-off-by: ItalyPaleAle <43508+ItalyPaleAle@users.noreply.github.com>
2023-02-17 21:20:45 +00:00
..
aerospike Propagate context from caller to appropriate places in the code (#2474) 2023-02-16 14:18:35 -08:00
alicloud/tablestore Propagate context from caller to appropriate places in the code (#2474) 2023-02-16 14:18:35 -08:00
aws/dynamodb Propagate context from caller to appropriate places in the code (#2474) 2023-02-16 14:18:35 -08:00
azure Merge branch 'master' of https://github.com/dapr/components-contrib into newdeps 2023-02-16 23:35:28 +00:00
cassandra Propagate context from caller to appropriate places in the code (#2474) 2023-02-16 14:18:35 -08:00
cloudflare/workerskv 💄 2023-02-17 00:59:25 +00:00
cockroachdb Propagate context from caller to appropriate places in the code (#2474) 2023-02-16 14:18:35 -08:00
couchbase Propagate context from caller to appropriate places in the code (#2474) 2023-02-16 14:18:35 -08:00
gcp/firestore Propagate context from caller to appropriate places in the code (#2474) 2023-02-16 14:18:35 -08:00
hashicorp/consul Merge branch 'master' of https://github.com/dapr/components-contrib into newdeps 2023-02-16 23:35:28 +00:00
hazelcast Propagate context from caller to appropriate places in the code (#2474) 2023-02-16 14:18:35 -08:00
in-memory Propagate context from caller to appropriate places in the code (#2474) 2023-02-16 14:18:35 -08:00
jetstream Propagate context from caller to appropriate places in the code (#2474) 2023-02-16 14:18:35 -08:00
memcached Merge branch 'master' of https://github.com/dapr/components-contrib into newdeps 2023-02-16 23:35:28 +00:00
mongodb Propagate context from caller to appropriate places in the code (#2474) 2023-02-16 14:18:35 -08:00
mysql Propagate context from caller to appropriate places in the code (#2474) 2023-02-16 14:18:35 -08:00
oci/objectstorage Propagate context from caller to appropriate places in the code (#2474) 2023-02-16 14:18:35 -08:00
oracledatabase Propagate context from caller to appropriate places in the code (#2474) 2023-02-16 14:18:35 -08:00
postgresql Propagate context from caller to appropriate places in the code (#2474) 2023-02-16 14:18:35 -08:00
query Expose parsefilter function 2022-12-27 14:56:11 -03:00
redis Propagate context from caller to appropriate places in the code (#2474) 2023-02-16 14:18:35 -08:00
rethinkdb Merge branch 'master' of https://github.com/dapr/components-contrib into newdeps 2023-02-16 23:35:28 +00:00
sqlite Merge branch 'master' of https://github.com/dapr/components-contrib into release-1.10 2023-02-16 23:23:23 +00:00
sqlserver Propagate context from caller to appropriate places in the code (#2474) 2023-02-16 14:18:35 -08:00
utils 💄 2022-11-22 04:53:24 +00:00
zookeeper Propagate context from caller to appropriate places in the code (#2474) 2023-02-16 14:18:35 -08:00
README.md middleware: changes wasm basic to use waPC (#1833) 2022-09-13 17:12:59 -07:00
errors.go Change BulkDelete error handling in transaction 2022-02-21 15:40:58 +05:30
errors_test.go update license to Apache v2.0 (#1406) 2022-01-04 19:53:31 -08:00
feature.go adding query api to the feature list (#1775) 2022-06-07 08:38:02 -07:00
metadata.go Define common metadata across components (#1994) 2022-08-25 10:10:53 -07:00
request_options.go Misc refactorings 2022-11-17 16:29:29 +00:00
request_options_test.go Misc refactorings 2022-11-17 16:29:29 +00:00
requests.go Added ContentType to pubsub/binding/state request-response (#1376) 2022-01-28 10:17:04 -08:00
responses.go Added ContentType to pubsub/binding/state request-response (#1376) 2022-01-28 10:17:04 -08:00
store.go Propagate context from caller to appropriate places in the code (#2474) 2023-02-16 14:18:35 -08:00
store_test.go Propagate context from caller to appropriate places in the code (#2474) 2023-02-16 14:18:35 -08:00
transactional_store.go Propagate context from caller to appropriate places in the code (#2474) 2023-02-16 14:18:35 -08:00

README.md

State Stores

State Stores provide a common way to interact with different data store implementations, and allow users to opt-in to advanced capabilities using defined metadata.

Implementing a new State Store

A compliant state store needs to implement one or more interfaces: Store and TransactionalStore, defined in the store.go file.

See the documentation site for examples.

Implementing State Query API

State Store has an optional API for querying the state.

Please refer to the documentation site for API description and definition.

// Querier is an interface to execute queries.
type Querier interface {
        Query(req *QueryRequest) (*QueryResponse, error)
}

Below are the definitions of structures (including nested) for QueryRequest and QueryResponse.

// QueryResponse is the request object for querying the state.
type QueryRequest struct {
        Query    query.Query       `json:"query"`
        Metadata map[string]string `json:"metadata,omitempty"`
}

type Query struct {
        Filters map[string]interface{} `json:"filter"`
        Sort    []Sorting              `json:"sort"`
        Page    Pagination             `json:"page"`

        // derived from Filters
        Filter Filter
}

type Sorting struct {
        Key   string `json:"key"`
        Order string `json:"order,omitempty"`
}

type Pagination struct {
        Limit int    `json:"limit"`
        Token string `json:"token,omitempty"`
}

// QueryResponse is the response object on querying state.
type QueryResponse struct {
        Results  []QueryItem       `json:"results"`
        Token    string            `json:"token,omitempty"`
        Metadata map[string]string `json:"metadata,omitempty"`
}

// QueryItem is an object representing a single entry in query results.
type QueryItem struct {
        Key   string  `json:"key"`
        Data  []byte  `json:"data"`
        ETag  *string `json:"etag,omitempty"`
        Error string  `json:"error,omitempty"`
}

Upon receiving the query request, Dapr validates it and transforms into object Query, which, in turn, is passed on to the state store component.

The Query object has a member Filter that implements parsing interface per component as described below.

type Filter interface {
	Parse(interface{}) error
}

type FilterEQ struct {
	Key string
	Val interface{}
}

type FilterIN struct {
	Key  string
	Vals []interface{}
}

type FilterAND struct {
	Filters []Filter
}

type FilterOR struct {
	Filters []Filter
}

To simplify the process of query translation, we leveraged visitor design pattern. A state store component developer would need to implement the visit method, and the runtime will use it to construct the native query statement.

type Visitor interface {
	// returns "equal" expression
	VisitEQ(*FilterEQ) (string, error)
	// returns "in" expression
	VisitIN(*FilterIN) (string, error)
	// returns "and" expression
	VisitAND(*FilterAND) (string, error)
	// returns "or" expression
	VisitOR(*FilterOR) (string, error)
	// receives concatenated filters and finalizes the native query
	Finalize(string, *MidQuery) error
}

The Dapr runtime implements QueryBuilder object that takes in Visitor interface and constructs the native query.

type QueryBuilder struct {
	visitor Visitor
}

func (h *QueryBuilder) BuildQuery(mq *MidQuery) error {...}

The last part is to implement Querier interface in the component:

type Querier interface {
	Query(req *QueryRequest) (*QueryResponse, error)
}

A sample implementation might look like that:

func (m *MyComponent) Query(req *state.QueryRequest) (*state.QueryResponse, error) {
	ctx, cancel := context.WithTimeout(context.Background(), timeout)
	defer cancel()

	query := &Query{} // Query implements Visitor interface
	qbuilder := state.NewQueryBuilder(query)
	if err := qbuilder.BuildQuery(&req.Query); err != nil {
		return &state.QueryResponse{}, err
	}
	data, token, err := query.execute(ctx)
	if err != nil {
		return &state.QueryResponse{}, err
	}
	return &state.QueryResponse{
		Results:  data,
		Token:    token,
	}, nil
}

Some of the examples of State Query API implementation are MongoDB and CosmosDB state store components.