MCPcopy
hub / github.com/hashicorp/go-memdb / FirstWatch

Method FirstWatch

txn.go:534–558  ·  view source on GitHub ↗

FirstWatch is used to return the first matching object for the given constraints on the index along with the watch channel. Note that all values read in the transaction form a consistent snapshot from the time when the transaction was created. The watch channel is closed when a subsequent write tr

(table, index string, args ...interface{})

Source from the content-addressed store, hash-verified

532// match instead of full match on the index. The registered indexer must implement
533// PrefixIndexer, otherwise an error is returned.
534func (txn *Txn) FirstWatch(table, index string, args ...interface{}) (<-chan struct{}, interface{}, error) {
535 // Get the index value
536 indexSchema, val, err := txn.getIndexValue(table, index, args...)
537 if err != nil {
538 return nil, nil, err
539 }
540
541 // Get the index itself
542 indexTxn := txn.readableIndex(table, indexSchema.Name)
543
544 // Do an exact lookup
545 if indexSchema.Unique && val != nil && indexSchema.Name == index {
546 watch, obj, ok := indexTxn.GetWatch(val)
547 if !ok {
548 return watch, nil, nil
549 }
550 return watch, obj, nil
551 }
552
553 // Handle non-unique index by using an iterator and getting the first value
554 iter := indexTxn.Root().Iterator()
555 watch := iter.SeekPrefixWatch(val)
556 _, value, _ := iter.Next()
557 return watch, value, nil
558}
559
560// LastWatch is used to return the last matching object for
561// the given constraints on the index along with the watch channel.

Callers 2

FirstMethod · 0.95
TestWatchUpdateFunction · 0.80

Calls 3

getIndexValueMethod · 0.95
readableIndexMethod · 0.95
NextMethod · 0.65

Tested by 1

TestWatchUpdateFunction · 0.64