github.com/altipla-consulting/ravendb-go-client@v0.1.3/lazy_load_operation.go (about)

     1  package ravendb
     2  
     3  var _ ILazyOperation = &LazyLoadOperation{}
     4  
     5  // LazyLoadOperation represents lazy load operation
     6  type LazyLoadOperation struct {
     7  	_session       *InMemoryDocumentSessionOperations
     8  	_loadOperation *LoadOperation
     9  	_ids           []string
    10  	_includes      []string
    11  
    12  	hasItems    bool
    13  	queryResult *QueryResult
    14  	//result interface{}
    15  	requiresRetry bool
    16  }
    17  
    18  // NewLazyLoadOperation returns new LazyLoadOperation
    19  func newLazyLoadOperation(session *InMemoryDocumentSessionOperations, loadOperation *LoadOperation) *LazyLoadOperation {
    20  	return &LazyLoadOperation{
    21  		_session:       session,
    22  		_loadOperation: loadOperation,
    23  	}
    24  }
    25  
    26  // TODO: should return an error
    27  // needed for ILazyOperation
    28  func (o *LazyLoadOperation) createRequest() *getRequest {
    29  	var idsToCheckOnServer []string
    30  	for _, id := range o._ids {
    31  		if !o._session.IsLoadedOrDeleted(id) {
    32  			idsToCheckOnServer = append(idsToCheckOnServer, id)
    33  		}
    34  	}
    35  	queryBuilder := "?"
    36  
    37  	if o._includes != nil {
    38  		for _, include := range o._includes {
    39  			queryBuilder += "&include="
    40  			queryBuilder += include
    41  		}
    42  	}
    43  
    44  	for _, id := range idsToCheckOnServer {
    45  		queryBuilder += "&id="
    46  		queryBuilder += urlUtilsEscapeDataString(id)
    47  	}
    48  
    49  	o.hasItems = len(idsToCheckOnServer) == 0
    50  
    51  	if o.hasItems {
    52  		// no need to hit the server
    53  		return nil
    54  	}
    55  
    56  	getRequest := &getRequest{
    57  		url:   "/docs",
    58  		query: queryBuilder,
    59  	}
    60  	return getRequest
    61  }
    62  
    63  func (o *LazyLoadOperation) byID(id string) *LazyLoadOperation {
    64  	if id == "" {
    65  		return o
    66  	}
    67  
    68  	if o._ids == nil {
    69  		o._ids = []string{id}
    70  	}
    71  
    72  	return o
    73  }
    74  
    75  func (o *LazyLoadOperation) byIds(ids []string) *LazyLoadOperation {
    76  	o._ids = ids
    77  	return o
    78  }
    79  
    80  func (o *LazyLoadOperation) withIncludes(includes []string) *LazyLoadOperation {
    81  	o._includes = includes
    82  	return o
    83  }
    84  
    85  // needed for ILazyOperation
    86  func (o *LazyLoadOperation) getResult(result interface{}) error {
    87  	if o.hasItems {
    88  		err := o._loadOperation.getDocuments(result)
    89  		if err != nil && len(o._ids) == 1 {
    90  			err = o._loadOperation.getDocument(result)
    91  		}
    92  		return err
    93  	}
    94  
    95  	if o.requiresRetry {
    96  		return nil
    97  	}
    98  
    99  	err := o._loadOperation.getDocuments(result)
   100  	// TODO: a better way to distinguish between a Load() and LoadMulti() operation
   101  	if err != nil && len(o._ids) == 1 {
   102  		err = o._loadOperation.getDocument(result)
   103  	}
   104  	return err
   105  }
   106  
   107  // needed for ILazyOperation
   108  func (o *LazyLoadOperation) getQueryResult() *QueryResult {
   109  	return o.queryResult
   110  }
   111  
   112  // needed for ILazyOperation
   113  func (o *LazyLoadOperation) isRequiresRetry() bool {
   114  	return o.requiresRetry
   115  }
   116  
   117  // needed for ILazyOperation
   118  func (o *LazyLoadOperation) handleResponse(response *GetResponse) error {
   119  	if response.IsForceRetry {
   120  		o.requiresRetry = true
   121  		return nil
   122  	}
   123  
   124  	res := response.Result
   125  	if len(res) == 0 {
   126  		return o.handleResponse2(nil)
   127  	}
   128  	var multiLoadResult *GetDocumentsResult
   129  	err := jsonUnmarshal(res, &multiLoadResult)
   130  	if err != nil {
   131  		return err
   132  	}
   133  	return o.handleResponse2(multiLoadResult)
   134  }
   135  
   136  func (o *LazyLoadOperation) handleResponse2(loadResult *GetDocumentsResult) error {
   137  	o._loadOperation.setResult(loadResult)
   138  	// Note: rest delayed to getResult()
   139  	return nil
   140  }