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

     1  package ravendb
     2  
     3  // BatchOperation represents a batch operation
     4  type BatchOperation struct {
     5  	session              *InMemoryDocumentSessionOperations
     6  	entities             []interface{}
     7  	sessionCommandsCount int
     8  }
     9  
    10  func newBatchOperation(session *InMemoryDocumentSessionOperations) *BatchOperation {
    11  	return &BatchOperation{
    12  		session: session,
    13  	}
    14  }
    15  
    16  func (b *BatchOperation) createRequest() (*BatchCommand, error) {
    17  	result, err := b.session.prepareForSaveChanges()
    18  	if err != nil {
    19  		return nil, err
    20  	}
    21  
    22  	b.sessionCommandsCount = len(result.sessionCommands)
    23  	result.sessionCommands = append(result.sessionCommands, result.deferredCommands...)
    24  	if len(result.sessionCommands) == 0 {
    25  		return nil, nil
    26  	}
    27  
    28  	if err = b.session.incrementRequestCount(); err != nil {
    29  		return nil, err
    30  	}
    31  
    32  	b.entities = result.entities
    33  
    34  	return newBatchCommand(b.session.GetConventions(), result.sessionCommands, result.options)
    35  }
    36  
    37  func (b *BatchOperation) setResult(result []map[string]interface{}) error {
    38  	if len(result) == 0 {
    39  		return throwOnNullResult()
    40  	}
    41  	for i := 0; i < b.sessionCommandsCount; i++ {
    42  		batchResult := result[i]
    43  		if batchResult == nil {
    44  			return newIllegalArgumentError("batchResult cannot be nil")
    45  		}
    46  		typ, _ := jsonGetAsText(batchResult, "Type")
    47  		if typ != "PUT" {
    48  			continue
    49  		}
    50  		entity := b.entities[i]
    51  		documentInfo := getDocumentInfoByEntity(b.session.documentsByEntity, entity)
    52  		if documentInfo == nil {
    53  			continue
    54  		}
    55  		changeVector := jsonGetAsTextPointer(batchResult, MetadataChangeVector)
    56  		if changeVector == nil {
    57  			return newIllegalStateError("PUT response is invalid. @change-vector is missing on " + documentInfo.id)
    58  		}
    59  		id, _ := jsonGetAsText(batchResult, MetadataID)
    60  		if id == "" {
    61  			return newIllegalStateError("PUT response is invalid. @id is missing on " + documentInfo.id)
    62  		}
    63  
    64  		for propertyName, v := range batchResult {
    65  			if propertyName == "Type" {
    66  				continue
    67  			}
    68  
    69  			meta := documentInfo.metadata
    70  			meta[propertyName] = v
    71  		}
    72  
    73  		documentInfo.id = id
    74  		documentInfo.changeVector = changeVector
    75  		doc := documentInfo.document
    76  		doc[MetadataKey] = documentInfo.metadata
    77  		documentInfo.metadataInstance = nil
    78  
    79  		b.session.documentsByID.add(documentInfo)
    80  		b.session.generateEntityIDOnTheClient.trySetIdentity(entity, id)
    81  
    82  		afterSaveChangesEventArgs := newAfterSaveChangesEventArgs(b.session, documentInfo.id, documentInfo.entity)
    83  		b.session.onAfterSaveChangesInvoke(afterSaveChangesEventArgs)
    84  	}
    85  	return nil
    86  }
    87  
    88  func throwOnNullResult() error {
    89  	return newIllegalStateError("Received empty response from the server. This is not supposed to happen and is likely a bug.")
    90  }