Skip to content

Commit

Permalink
Add optimistic concurrency control
Browse files Browse the repository at this point in the history
  • Loading branch information
olivere committed Aug 5, 2019
1 parent 37f9df1 commit 6da3cc8
Show file tree
Hide file tree
Showing 20 changed files with 611 additions and 174 deletions.
62 changes: 41 additions & 21 deletions bulk_delete_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ import (
// for details.
type BulkDeleteRequest struct {
BulkableRequest
index string
typ string
id string
parent string
routing string
version int64 // default is MATCH_ANY
versionType string // default is "internal"
index string
typ string
id string
parent string
routing string
version int64 // default is MATCH_ANY
versionType string // default is "internal"
ifSeqNo *int64
ifPrimaryTerm *int64

source []string

Expand All @@ -38,13 +40,15 @@ type bulkDeleteRequestCommand map[string]bulkDeleteRequestCommandOp

//easyjson:json
type bulkDeleteRequestCommandOp struct {
Index string `json:"_index,omitempty"`
Type string `json:"_type,omitempty"`
Id string `json:"_id,omitempty"`
Parent string `json:"parent,omitempty"`
Routing string `json:"routing,omitempty"`
Version int64 `json:"version,omitempty"`
VersionType string `json:"version_type,omitempty"`
Index string `json:"_index,omitempty"`
Type string `json:"_type,omitempty"`
Id string `json:"_id,omitempty"`
Parent string `json:"parent,omitempty"`
Routing string `json:"routing,omitempty"`
Version int64 `json:"version,omitempty"`
VersionType string `json:"version_type,omitempty"`
IfSeqNo *int64 `json:"if_seq_no,omitempty"`
IfPrimaryTerm *int64 `json:"if_primary_term,omitempty"`
}

// NewBulkDeleteRequest returns a new BulkDeleteRequest.
Expand Down Expand Up @@ -116,6 +120,20 @@ func (r *BulkDeleteRequest) VersionType(versionType string) *BulkDeleteRequest {
return r
}

// IfSeqNo indicates to only perform the delete operation if the last
// operation that has changed the document has the specified sequence number.
func (r *BulkDeleteRequest) IfSeqNo(ifSeqNo int64) *BulkDeleteRequest {
r.ifSeqNo = &ifSeqNo
return r
}

// IfPrimaryTerm indicates to only perform the delete operation if the
// last operation that has changed the document has the specified primary term.
func (r *BulkDeleteRequest) IfPrimaryTerm(ifPrimaryTerm int64) *BulkDeleteRequest {
r.ifPrimaryTerm = &ifPrimaryTerm
return r
}

// String returns the on-wire representation of the delete request,
// concatenated as a single string.
func (r *BulkDeleteRequest) String() string {
Expand All @@ -136,13 +154,15 @@ func (r *BulkDeleteRequest) Source() ([]string, error) {
}
command := bulkDeleteRequestCommand{
"delete": bulkDeleteRequestCommandOp{
Index: r.index,
Type: r.typ,
Id: r.id,
Routing: r.routing,
Parent: r.parent,
Version: r.version,
VersionType: r.versionType,
Index: r.index,
Type: r.typ,
Id: r.id,
Routing: r.routing,
Parent: r.parent,
Version: r.version,
VersionType: r.versionType,
IfSeqNo: r.ifSeqNo,
IfPrimaryTerm: r.ifPrimaryTerm,
},
}

Expand Down
72 changes: 54 additions & 18 deletions bulk_delete_request_easyjson.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions bulk_index_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type BulkIndexRequest struct {
doc interface{}
pipeline string
retryOnConflict *int
ifSeqNo *int64
ifPrimaryTerm *int64

source []string

Expand All @@ -50,6 +52,8 @@ type bulkIndexRequestCommandOp struct {
Version *int64 `json:"version,omitempty"`
VersionType string `json:"version_type,omitempty"`
Pipeline string `json:"pipeline,omitempty"`
IfSeqNo *int64 `json:"if_seq_no,omitempty"`
IfPrimaryTerm *int64 `json:"if_primary_term,omitempty"`
}

// NewBulkIndexRequest returns a new BulkIndexRequest.
Expand Down Expand Up @@ -158,6 +162,20 @@ func (r *BulkIndexRequest) Pipeline(pipeline string) *BulkIndexRequest {
return r
}

// IfSeqNo indicates to only perform the index operation if the last
// operation that has changed the document has the specified sequence number.
func (r *BulkIndexRequest) IfSeqNo(ifSeqNo int64) *BulkIndexRequest {
r.ifSeqNo = &ifSeqNo
return r
}

// IfPrimaryTerm indicates to only perform the index operation if the
// last operation that has changed the document has the specified primary term.
func (r *BulkIndexRequest) IfPrimaryTerm(ifPrimaryTerm int64) *BulkIndexRequest {
r.ifPrimaryTerm = &ifPrimaryTerm
return r
}

// String returns the on-wire representation of the index request,
// concatenated as a single string.
func (r *BulkIndexRequest) String() string {
Expand Down Expand Up @@ -193,6 +211,8 @@ func (r *BulkIndexRequest) Source() ([]string, error) {
VersionType: r.versionType,
RetryOnConflict: r.retryOnConflict,
Pipeline: r.pipeline,
IfSeqNo: r.ifSeqNo,
IfPrimaryTerm: r.ifPrimaryTerm,
}
command := bulkIndexRequestCommand{
r.opType: indexCommand,
Expand Down
Loading

0 comments on commit 6da3cc8

Please sign in to comment.