Skip to content

Commit

Permalink
support atomic writes
Browse files Browse the repository at this point in the history
  • Loading branch information
sandeepvinayak committed Dec 7, 2024
1 parent 7705be9 commit 04a45a7
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions docstore/docstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,20 +135,20 @@ func (c *Collection) Actions() *ActionList {
// document; a Get after the write will see the new value if the service is strongly
// consistent, but may see the old value if the service is eventually consistent.
type ActionList struct {
coll *Collection
actions []*Action
atomicWrites bool
beforeDo func(asFunc func(interface{}) bool) error
coll *Collection
actions []*Action
enableAtomicWrites bool
beforeDo func(asFunc func(interface{}) bool) error
}

// An Action is a read or write on a single document.
// Use the methods of ActionList to create and execute Actions.
type Action struct {
kind driver.ActionKind
doc Document
fieldpaths []FieldPath // paths to retrieve, for Get
mods Mods // modifications to make, for Update
transaction bool // if this action is a part of transaction
kind driver.ActionKind
doc Document
fieldpaths []FieldPath // paths to retrieve, for Get
mods Mods // modifications to make, for Update
inAtomicWrite bool // if this action is a part of atomic writes
}

func (l *ActionList) add(a *Action) *ActionList {
Expand All @@ -172,7 +172,7 @@ func (l *ActionList) add(a *Action) *ActionList {
// Except for setting the revision field and possibly setting the key fields, the doc
// argument is not modified.
func (l *ActionList) Create(doc Document) *ActionList {
return l.add(&Action{kind: driver.Create, doc: doc, transaction: l.atomicWrites})
return l.add(&Action{kind: driver.Create, doc: doc, inAtomicWrite: l.enableAtomicWrites})
}

// Replace adds an action that replaces a document to the given ActionList, and
Expand All @@ -184,7 +184,7 @@ func (l *ActionList) Create(doc Document) *ActionList {
// See the Revisions section of the package documentation for how revisions are
// handled.
func (l *ActionList) Replace(doc Document) *ActionList {
return l.add(&Action{kind: driver.Replace, doc: doc, transaction: l.atomicWrites})
return l.add(&Action{kind: driver.Replace, doc: doc, inAtomicWrite: l.enableAtomicWrites})
}

// Put adds an action that adds or replaces a document to the given ActionList, and returns the ActionList.
Expand All @@ -197,7 +197,7 @@ func (l *ActionList) Replace(doc Document) *ActionList {
// See the Revisions section of the package documentation for how revisions are
// handled.
func (l *ActionList) Put(doc Document) *ActionList {
return l.add(&Action{kind: driver.Put, doc: doc, transaction: l.atomicWrites})
return l.add(&Action{kind: driver.Put, doc: doc, inAtomicWrite: l.enableAtomicWrites})
}

// Delete adds an action that deletes a document to the given ActionList, and returns
Expand All @@ -212,7 +212,7 @@ func (l *ActionList) Delete(doc Document) *ActionList {
// semantics of an action list are to stop at first error, then we might abort a
// list of Deletes just because one of the docs was not present, and that seems
// wrong, or at least something you'd want to turn off.
return l.add(&Action{kind: driver.Delete, doc: doc, transaction: l.atomicWrites})
return l.add(&Action{kind: driver.Delete, doc: doc, inAtomicWrite: l.enableAtomicWrites})
}

// Get adds an action that retrieves a document to the given ActionList, and
Expand Down Expand Up @@ -254,10 +254,10 @@ func (l *ActionList) Get(doc Document, fps ...FieldPath) *ActionList {
// the updated document, call Get after calling Update.
func (l *ActionList) Update(doc Document, mods Mods) *ActionList {
return l.add(&Action{
kind: driver.Update,
doc: doc,
mods: mods,
transaction: l.atomicWrites,
kind: driver.Update,
doc: doc,
mods: mods,
inAtomicWrite: l.enableAtomicWrites,
})
}

Expand Down Expand Up @@ -433,7 +433,7 @@ func (c *Collection) toDriverAction(a *Action) (*driver.Action, error) {
// A Put with a revision field is equivalent to a Replace.
kind = driver.Replace
}
d := &driver.Action{Kind: kind, Doc: ddoc, Key: key, InAtomicWrite: a.transaction}
d := &driver.Action{Kind: kind, Doc: ddoc, Key: key, InAtomicWrite: a.inAtomicWrite}
if a.fieldpaths != nil {
d.FieldPaths, err = parseFieldPaths(a.fieldpaths)
if err != nil {
Expand Down Expand Up @@ -539,7 +539,7 @@ func (l *ActionList) String() string {

// AtomicWrites causes all following writes in the list to execute atomically.
func (l *ActionList) AtomicWrites() *ActionList {
l.atomicWrites = true
l.enableAtomicWrites = true
return l
}

Expand Down

0 comments on commit 04a45a7

Please sign in to comment.