govespa

package module
v0.0.0-...-6e43124 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2022 License: MIT Imports: 15 Imported by: 2

README

govespa

Govespa is a client implementation for the vespa-engine. It uses the Document Api and can be used over http or http/2, since you can supply your own http.Client. It's goal is to support all functionalities exposed by the Document Api.

Features
  • Put: writes a Document by ID and allows binding a struct/map to the document fields.
  • Get: returns a Document by ID and allows mapping the Response to a struct.
  • Update: updates fields of a Document by ID.
  • Remove: removes a Document by ID.
  • Query: executes yql and allows binding the Response to a struct.
Getting Started
go get -u github.com/jonashiltl/govespa

Create an http.Client, for http/2 add your TLS Certificates.

key, err := ioutil.ReadFile("client.key")
...

crt, err := ioutil.ReadFile("client.pem")
...

ca, err := ioutil.ReadFile("ca-vespa.pem")
...

rootCAs := x509.NewCertPool()
rootCAs.AppendCertsFromPEM(ca)

cert, err := tls.X509KeyPair(crt, key)
... 

tls := &tls.Config{
  Certificates: []tls.Certificate{cert},
  RootCAs:      rootCAs,
  ServerName:   "localhost",
}

trns := &http2.Transport{
  TLSClientConfig: tls,
  AllowHTTP:       false,
}

client := &http.Client{Transport: trns}

Create the VespaClient with a baseUrl and the http.Client.

c := govespa.NewClient(govespa.NewClientParams{
  HttpClient: client,
  BaseUrl:    "https://localhost:8090",
})
Examples

Have a look at the integration folder to see examples.

TODO
  • Extend the DocumentId with the key/value pair section
  • Decide where and how to use concurrency
  • Improve the ParseDocId function, current benchmark can be seen in documentid_test.go
  • Implement an Iterator which can be used for "update where", "visit", "delete where". It can either be a single struct used by Update/Get/Delete/Query but that may be harder to implement than just defining the logic for every instance separately.
Disclaimer

This client implementation is work in progress and not an official client created by the Team @vespa.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoDoc = errors.New("no document found")

Functions

This section is empty.

Types

type DocumentId

type DocumentId struct {
	Namespace    string
	DocType      string
	UserSpecific string
}

func ParseDocId

func ParseDocId(s string) (DocumentId, error)

TODO: cleean this mess up

func (DocumentId) String

func (i DocumentId) String() string

type ErrorType

type ErrorType int64
const (
	ConditionNotMet ErrorType = iota
	VespaFailure
	TransportFailure
)

type Get

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

func (*Get) AddParameter

func (r *Get) AddParameter(p GetParameter) *Get

func (*Get) Exec

func (g *Get) Exec(dest any) (GetResponse, error)

func (*Get) WithContext

func (g *Get) WithContext(c context.Context) *Get

type GetParameter

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

type GetResponse

type GetResponse struct {
	PathId string         `json:"pathId"`
	Id     string         `json:"id"`
	Fields map[string]any `json:"fields"`
}

type NewClientParams

type NewClientParams struct {
	HttpClient *http.Client
	Headers    http.Header
	BaseUrl    string
}

type OperationalParams

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

type Put

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

func (*Put) AddParameter

func (p *Put) AddParameter(param OperationalParams) *Put

func (*Put) BindMap

func (p *Put) BindMap(s map[string]any) *Put

func (*Put) BindStruct

func (p *Put) BindStruct(s any) *Put

BindStruct adds all values of the struct with the tag `vespa:"field_name"` to the fields object of the Put Request. use `vespa:"-" to exclude the value in the fields object`. Empty fields are ignored.

func (*Put) Exec

func (p *Put) Exec() error

func (*Put) WithContext

func (p *Put) WithContext(c context.Context) *Put

type Query

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

func (*Query) AddParameter

func (q *Query) AddParameter(p QueryParameter) *Query

func (*Query) AddVariable

func (q *Query) AddVariable(key string, value string) *Query

AddVariables can be used to add any kind of key/value pair to the query. For example, AddVariable("ranking", "rank_albums") would select "rank_albums" as the Ranking Profile.

func (*Query) AddYQL

func (q *Query) AddYQL(yql string) *Query

func (*Query) Exec

func (q *Query) Exec() (QueryResponse, error)

Exec runs the Query but doesn't scan the fields of the result into a destination.

func (*Query) Get

func (q *Query) Get(dest any) (QueryResponse, error)

Get scans the fields of the first result/children into a destination. The destination needs to be a pointer to a struct which fields are annotated with the "vespa" Tag.

func (*Query) Select

func (q *Query) Select(dest any) (QueryResponse, error)

Select scans the fiels of all results into a destination. The destination needs to be a pointer to a slice of structs, annotated with the "vespa" Tag.

func (*Query) WithContext

func (q *Query) WithContext(c context.Context) *Query

type QueryParameter

type QueryParameter struct {
	Offset               uint64
	Hits                 uint32
	QueryProfile         string
	GroupingSessionCache *bool
	SearchChain          string
	Timeout              time.Duration
}

groupingSessionCache is a pointer so that we can distinguish between true/false/not defined

type QueryResponse

type QueryResponse struct {
	Root Root `json:"root"`
}

type Remove

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

func (*Remove) AddParameter

func (r *Remove) AddParameter(p RemoveParams) *Remove

func (*Remove) Exec

func (r *Remove) Exec() error

func (*Remove) WithContext

func (r *Remove) WithContext(c context.Context) *Remove

type RemoveParams

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

type Root

type Root struct {
	Id        string       `json:"id,omitempty"`
	Relevance float64      `json:"relevance"`
	Label     string       `json:"label,omitempty"`
	Source    string       `json:"source,omitempty"`
	Value     string       `json:"value,omitempty"`
	Types     []string     `json:"types,omitempty"`
	Children  []children   `json:"children"`
	Fields    fields       `json:"fields"`
	Coverage  coverage     `json:"coverage"`
	Limits    limits       `json:"limits"`
	Errors    []vespaError `json:"errors,omitempty"`
}

type Update

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

func (*Update) Add

func (u *Update) Add(field string, value []any) *Update

func (*Update) AddParameter

func (u *Update) AddParameter(p OperationalParams) *Update

func (*Update) AddWeightedSet

func (u *Update) AddWeightedSet(field string, ws map[any]int) *Update

Adds a Weighted Set to a field. The weight can either be an Interger or a String. See documentation https://docs.vespa.ai/en/reference/document-json-format.html#add

func (*Update) Assign

func (u *Update) Assign(field string, value any) *Update

func (*Update) Decrement

func (u *Update) Decrement(field string, value any) *Update

func (*Update) Divide

func (u *Update) Divide(field string, value any) *Update

func (*Update) Exec

func (q *Update) Exec() error

func (*Update) Increment

func (u *Update) Increment(field string, value any) *Update

func (*Update) Match

func (u *Update) Match(field string, element string, operation string, value any) *Update

Can be used to do an operation on a specific element in a weighted set or array. See https://docs.vespa.ai/en/reference/document-json-format.html#match

func (*Update) Multiply

func (u *Update) Multiply(field string, value any) *Update

func (*Update) Remove

func (u *Update) Remove(field string) *Update

Remove can be used to remove an element from a map. See https://docs.vespa.ai/en/reference/document-json-format.html#composite-remove

func (*Update) RemoveWeightedSet

func (u *Update) RemoveWeightedSet(field string, element string) *Update

func (*Update) WithContext

func (u *Update) WithContext(c context.Context) *Update

type VespaClient

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

func NewClient

func NewClient(params NewClientParams) *VespaClient

func (*VespaClient) Get

func (v *VespaClient) Get(docId DocumentId) *Get

func (*VespaClient) Put

func (v *VespaClient) Put(docId DocumentId) *Put

func (*VespaClient) Query

func (v *VespaClient) Query() *Query

func (*VespaClient) Remove

func (v *VespaClient) Remove(docId DocumentId) *Remove

func (*VespaClient) Update

func (v *VespaClient) Update(docId DocumentId) *Update

Jump to

Keyboard shortcuts

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