...

Source file src/github.com/go-kivik/kivik/v4/couchdb/bulkget.go

Documentation: github.com/go-kivik/kivik/v4/couchdb

     1  // Licensed under the Apache License, Version 2.0 (the "License"); you may not
     2  // use this file except in compliance with the License. You may obtain a copy of
     3  // the License at
     4  //
     5  //  http://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     9  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    10  // License for the specific language governing permissions and limitations under
    11  // the License.
    12  
    13  package couchdb
    14  
    15  import (
    16  	"context"
    17  	"encoding/json"
    18  	"fmt"
    19  	"net/http"
    20  
    21  	"github.com/go-kivik/kivik/v4/couchdb/chttp"
    22  	"github.com/go-kivik/kivik/v4/driver"
    23  )
    24  
    25  func (d *db) BulkGet(ctx context.Context, docs []driver.BulkGetReference, options driver.Options) (driver.Rows, error) {
    26  	opts := map[string]interface{}{}
    27  	options.Apply(opts)
    28  	query, err := optionsToParams(opts)
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  	body := map[string]interface{}{
    33  		"docs": docs,
    34  	}
    35  	chttpOpts := &chttp.Options{
    36  		Query:   query,
    37  		GetBody: chttp.BodyEncoder(body),
    38  		Header: http.Header{
    39  			chttp.HeaderIdempotencyKey: []string{},
    40  		},
    41  	}
    42  	resp, err := d.Client.DoReq(ctx, http.MethodPost, d.path("_bulk_get"), chttpOpts)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  	if err = chttp.ResponseError(resp); err != nil {
    47  		return nil, err
    48  	}
    49  	return newBulkGetRows(ctx, resp.Body), nil
    50  }
    51  
    52  // bulkGetError represents an error for a single document returned by a
    53  // GetBulk call.
    54  type bulkGetError struct {
    55  	ID     string `json:"id"`
    56  	Rev    string `json:"rev"`
    57  	Err    string `json:"error"`
    58  	Reason string `json:"reason"`
    59  }
    60  
    61  var _ error = &bulkGetError{}
    62  
    63  func (e *bulkGetError) Error() string {
    64  	return fmt.Sprintf("%s: %s", e.Err, e.Reason)
    65  }
    66  
    67  type bulkResultDoc struct {
    68  	Doc   json.RawMessage `json:"ok,omitempty"`
    69  	Error *bulkGetError   `json:"error,omitempty"`
    70  }
    71  
    72  type bulkResult struct {
    73  	ID   string          `json:"id"`
    74  	Docs []bulkResultDoc `json:"docs"`
    75  }
    76  

View as plain text