Esempio n. 1
0
func (docstore *DocStore) Collections() ([]string, error) {
	resp, err := docstore.client.DoReq("GET", "/api/ext/docstore/v1/", nil, nil)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	switch {
	case resp.StatusCode == 200:
		respBody := clientutil.NewSnappyResponseReader(resp)
		defer respBody.Close()
		colResp := &collectionResp{}
		if err := json.NewDecoder(respBody).Decode(colResp); err != nil {
			return nil, err
		}
		return colResp.Collections, nil
	default:
		var body bytes.Buffer
		body.ReadFrom(resp.Body)
		return nil, fmt.Errorf("failed to insert doc: %v", body.String())
	}
}
Esempio n. 2
0
// Get retrieve the document, `doc` must a map[string]interface{} or a struct pointer.
func (col *Collection) GetID(id string, doc interface{}) error {
	resp, err := col.docstore.client.DoReq("GET", fmt.Sprintf("/api/ext/docstore/v1/%s/%s", col.col, id), nil, nil)
	if err != nil {
		return err
	}

	defer resp.Body.Close()
	switch resp.StatusCode {
	case 200:
		respBody := clientutil.NewSnappyResponseReader(resp)
		defer respBody.Close()
		if err := json.NewDecoder(respBody).Decode(doc); err != nil {
			return err
		}
		return nil
	case 404:
		return ErrIDNotFound
	default:
		var body bytes.Buffer
		body.ReadFrom(resp.Body)
		return fmt.Errorf("failed to insert doc: %v", body.String())
	}
}
Esempio n. 3
0
// Next unmarshall the request into the given slice,
// returns false when there's no more data
func (iter *Iter) Next(res interface{}) bool {
	if iter.closed {
		return false
	}
	resp, err := iter.col.docstore.client.DoReq("GET", fmt.Sprintf("/api/ext/docstore/v1/%s?query=%s", iter.col.col, iter.query), nil, nil)
	if err != nil {
		iter.err = err
		return false
	}
	defer resp.Body.Close()
	switch {
	case resp.StatusCode == 200:
		respBody := clientutil.NewSnappyResponseReader(resp)
		defer respBody.Close()
		if err := json.NewDecoder(respBody).Decode(res); err != nil {
			iter.err = err
			return false
		}
		cnt, err := strconv.Atoi(resp.Header.Get("BlobStash-DocStore-Iter-Count"))
		if err != nil {
			iter.err = err
			return false
		}
		if cnt == iter.Opts.Limit {
			return true
		}
		iter.closed = true // Next call will return false
		// FIXME(tsileo): add the number of results (along with the latest ID / it will act as a cursor)
		// in a header and return true only if cnt == iter.Opts.Limit
		return true
	default:
		var body bytes.Buffer
		body.ReadFrom(resp.Body)
		iter.err = fmt.Errorf("failed to insert doc: %v", body.String())
		return false
	}
}