Skip to content

Commit

Permalink
minor updates to index client
Browse files Browse the repository at this point in the history
- implement commit method
- update tests
  • Loading branch information
stevenferrer committed Jun 13, 2020
1 parent e819760 commit 24d6da6
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ test:

.PHONY: solr
solr:
docker rm -f solr-solr-test || true
docker rm -f solr-test || true
docker run -d -p 8983:8983 --name solr-test solr:latest solr-precreate gettingstarted

.PHONY: stop-solr
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ go 1.14
require (
github.com/dnaeon/go-vcr v1.0.1
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.5.1
github.com/stretchr/testify v1.6.1
gopkg.in/yaml.v2 v2.3.0 // indirect
)
11 changes: 7 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
14 changes: 10 additions & 4 deletions index/examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ func main() {
},
}

err := indexClient.AddDocs(context.Background(), collection, docs)
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
err := indexClient.AddDocs(ctx, collection, docs)
checkErr(err)
err = indexClient.Commit(ctx, collection)
checkErr(err)

// Sending multiple update commands
err = indexClient.UpdateCommands(context.Background(), collection,
Expand Down Expand Up @@ -71,6 +72,11 @@ func main() {
Query: "*:*",
},
)
checkErr(err)
err = indexClient.Commit(ctx, collection)
}

func checkErr(err error) {
if err != nil {
log.Fatal(err)
}
Expand Down
44 changes: 43 additions & 1 deletion index/fixtures/add-docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interactions:
{
"responseHeader":{
"status":0,
"QTime":282}}
"QTime":137}}
headers:
Content-Length:
- "57"
Expand All @@ -34,3 +34,45 @@ interactions:
status: 200 OK
code: 200
duration: ""
- request:
body: ""
form: {}
headers:
Content-Type:
- application/json
url: http://localhost:8983/solr/gettingstarted/update?commit=true
method: GET
response:
body: |
{
"responseHeader":{
"status":0,
"QTime":1214}}
headers:
Cache-Control:
- no-cache, no-store
Content-Length:
- "58"
Content-Security-Policy:
- default-src 'none'; base-uri 'none'; connect-src 'self'; form-action 'self';
font-src 'self'; frame-ancestors 'none'; img-src 'self'; media-src 'self';
style-src 'self' 'unsafe-inline'; script-src 'self'; worker-src 'self';
Content-Type:
- text/plain;charset=utf-8
Etag:
- '"172ad393043"'
Expires:
- Sat, 01 Jan 2000 01:00:00 GMT
Last-Modified:
- Sat, 13 Jun 2020 10:28:25 GMT
Pragma:
- no-cache
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- SAMEORIGIN
X-Xss-Protection:
- 1; mode=block
status: 200 OK
code: 200
duration: ""
6 changes: 3 additions & 3 deletions index/fixtures/init-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ interactions:
{
"responseHeader":{
"status":0,
"QTime":880}}
"QTime":1337}}
headers:
Content-Length:
- "57"
- "58"
Content-Security-Policy:
- default-src 'none'; base-uri 'none'; connect-src 'self'; form-action 'self';
font-src 'self'; frame-ancestors 'none'; img-src 'self'; media-src 'self';
Expand Down Expand Up @@ -46,7 +46,7 @@ interactions:
{
"responseHeader":{
"status":0,
"QTime":946}}
"QTime":683}}
headers:
Content-Length:
- "57"
Expand Down
44 changes: 43 additions & 1 deletion index/fixtures/update-commands.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interactions:
{
"responseHeader":{
"status":0,
"QTime":642}}
"QTime":200}}
headers:
Content-Length:
- "57"
Expand All @@ -34,3 +34,45 @@ interactions:
status: 200 OK
code: 200
duration: ""
- request:
body: ""
form: {}
headers:
Content-Type:
- application/json
url: http://localhost:8983/solr/gettingstarted/update?commit=true
method: GET
response:
body: |
{
"responseHeader":{
"status":0,
"QTime":572}}
headers:
Cache-Control:
- no-cache, no-store
Content-Length:
- "57"
Content-Security-Policy:
- default-src 'none'; base-uri 'none'; connect-src 'self'; form-action 'self';
font-src 'self'; frame-ancestors 'none'; img-src 'self'; media-src 'self';
style-src 'self' 'unsafe-inline'; script-src 'self'; worker-src 'self';
Content-Type:
- text/plain;charset=utf-8
Etag:
- '"172ad393350"'
Expires:
- Sat, 01 Jan 2000 01:00:00 GMT
Last-Modified:
- Sat, 13 Jun 2020 10:28:26 GMT
Pragma:
- no-cache
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- SAMEORIGIN
X-Xss-Protection:
- 1; mode=block
status: 200 OK
code: 200
duration: ""
57 changes: 43 additions & 14 deletions index/json_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type JSONClient interface {
AddDocs(ctx context.Context, collection string, docs interface{}) error
// UpdateCmds send multiple update commands
UpdateCommands(ctx context.Context, collection string, commands ...Commander) error

Commit(ctx context.Context, collection string) error
}

type jsonClient struct {
Expand Down Expand Up @@ -54,35 +56,40 @@ func NewJSONClientWithHTTPClient(host string, port int, httpClient *http.Client)
}
}

func (c jsonClient) AddDocs(ctx context.Context, collection string, docs interface{}) error {
theURL, err := url.Parse(fmt.Sprintf("%s://%s:%d/solr/%s/update",
func (c jsonClient) buildURL(collection string) (*url.URL, error) {
u, err := url.Parse(fmt.Sprintf("%s://%s:%d/solr/%s/update",
c.proto, c.host, c.port, collection))
if err != nil {
return errors.Wrap(err, "parse url")
return nil, errors.Wrap(err, "parse url")
}

theURL.Query().Add("commitWithin", "3000")
return u, nil
}

func (c jsonClient) AddDocs(ctx context.Context, collection string, docs interface{}) error {
theURL, err := c.buildURL(collection)
if err != nil {
return errors.Wrap(err, "build url")
}

var b []byte
b, err = json.Marshal(docs)
if err != nil {
return errors.Wrap(err, "marshal docs")
}

return c.doUpdt(ctx, theURL.String(), b)
return c.update(ctx, theURL.String(), b)
}

func (c jsonClient) UpdateCommands(ctx context.Context, collection string, commands ...Commander) error {
if len(commands) == 0 {
return nil
}

theURL, err := url.Parse(fmt.Sprintf("%s://%s:%d/solr/%s/update",
c.proto, c.host, c.port, collection))
theURL, err := c.buildURL(collection)
if err != nil {
return errors.Wrap(err, "parse url")
return errors.Wrap(err, "build url")
}
theURL.Query().Add("commitWithin", "3000")

cmdStrs := []string{}
for _, cmd := range commands {
Expand All @@ -95,19 +102,22 @@ func (c jsonClient) UpdateCommands(ctx context.Context, collection string, comma
}

cmdBody := "{" + strings.Join(cmdStrs, ",") + "}"
return c.doUpdt(ctx, theURL.String(), []byte(cmdBody))
return c.update(ctx, theURL.String(), []byte(cmdBody))
}

func (c jsonClient) doUpdt(ctx context.Context, urlStr string, body []byte) error {
func (c jsonClient) update(ctx context.Context, urlStr string, body []byte) error {
httpReq, err := http.NewRequestWithContext(ctx,
http.MethodPost, urlStr, bytes.NewReader(body))
if err != nil {
return errors.Wrap(err, "new http request")
}
httpReq.Header.Add("content-type", "application/json")

var httpResp *http.Response
httpResp, err = c.httpClient.Do(httpReq)
return c.doReq(ctx, httpReq)
}

func (c jsonClient) doReq(ctx context.Context, httpReq *http.Request) error {
httpReq.Header.Add("content-type", "application/json")
httpResp, err := c.httpClient.Do(httpReq)
if err != nil {
return errors.Wrap(err, "http do request")
}
Expand All @@ -124,3 +134,22 @@ func (c jsonClient) doUpdt(ctx context.Context, urlStr string, body []byte) erro

return nil
}

func (c jsonClient) Commit(ctx context.Context, collection string) error {
theURL, err := c.buildURL(collection)
if err != nil {
return errors.Wrap(err, "build url")
}

q := theURL.Query()
q.Add("commit", "true")
theURL.RawQuery = q.Encode()

httpReq, err := http.NewRequestWithContext(ctx,
http.MethodGet, theURL.String(), nil)
if err != nil {
return errors.Wrap(err, "new http request")
}

return c.doReq(ctx, httpReq)
}
9 changes: 8 additions & 1 deletion index/json_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestJSONClient(t *testing.T) {
assert.NoError(t, err)

// only for covering
_ = schema.NewClient(host, port)
_ = index.NewJSONClient(host, port)

schemaClient := schema.NewClientWithHTTPClient(host, port, &http.Client{
Timeout: timeout,
Expand Down Expand Up @@ -62,6 +62,9 @@ func TestJSONClient(t *testing.T) {
Transport: rec,
})

err = client.UpdateCommands(ctx, collection)
a.NoError(err)

var docs = []struct {
ID string `json:"id"`
Name string `json:"name"`
Expand All @@ -82,6 +85,8 @@ func TestJSONClient(t *testing.T) {

err = client.AddDocs(ctx, collection, docs)
a.NoError(err)
err = client.Commit(ctx, collection)
a.NoError(err)
})

t.Run("error", func(t *testing.T) {
Expand Down Expand Up @@ -136,6 +141,8 @@ func TestJSONClient(t *testing.T) {
},
)
a.NoError(err)
err = client.Commit(ctx, collection)
a.NoError(err)
})

t.Run("error", func(t *testing.T) {
Expand Down

0 comments on commit 24d6da6

Please sign in to comment.