github.com/hernad/nomad@v1.6.112/nomad/state/indexer/indexer.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package indexer 5 6 import ( 7 "bytes" 8 "encoding/binary" 9 "errors" 10 "fmt" 11 "time" 12 13 "github.com/hashicorp/go-memdb" 14 ) 15 16 var ( 17 // Ensure the required memdb interfaces are met at compile time. 18 _ memdb.Indexer = SingleIndexer{} 19 _ memdb.SingleIndexer = SingleIndexer{} 20 ) 21 22 // SingleIndexer implements both memdb.Indexer and memdb.SingleIndexer. It may 23 // be used in a memdb.IndexSchema to specify functions that generate the index 24 // value for memdb.Txn operations. 25 type SingleIndexer struct { 26 27 // readIndex is used by memdb for Txn.Get, Txn.First, and other operations 28 // that read data. 29 ReadIndex 30 31 // writeIndex is used by memdb for Txn.Insert, Txn.Delete, and other 32 // operations that write data to the index. 33 WriteIndex 34 } 35 36 // ReadIndex implements memdb.Indexer. It exists so that a function can be used 37 // to provide the interface. 38 // 39 // Unlike memdb.Indexer, a readIndex function accepts only a single argument. To 40 // generate an index from multiple values, use a struct type with multiple fields. 41 type ReadIndex func(arg any) ([]byte, error) 42 43 func (f ReadIndex) FromArgs(args ...interface{}) ([]byte, error) { 44 if len(args) != 1 { 45 return nil, fmt.Errorf("index supports only a single arg") 46 } 47 return f(args[0]) 48 } 49 50 var ErrMissingValueForIndex = fmt.Errorf("object is missing a value for this index") 51 52 // WriteIndex implements memdb.SingleIndexer. It exists so that a function 53 // can be used to provide this interface. 54 // 55 // Instead of a bool return value, writeIndex expects errMissingValueForIndex to 56 // indicate that an index could not be build for the object. It will translate 57 // this error into a false value to satisfy the memdb.SingleIndexer interface. 58 type WriteIndex func(raw any) ([]byte, error) 59 60 func (f WriteIndex) FromObject(raw any) (bool, []byte, error) { 61 v, err := f(raw) 62 if errors.Is(err, ErrMissingValueForIndex) { 63 return false, nil, nil 64 } 65 return err == nil, v, err 66 } 67 68 // IndexBuilder is a buffer used to construct memdb index values. 69 type IndexBuilder bytes.Buffer 70 71 // Bytes returns the stored IndexBuilder value as a byte array. 72 func (b *IndexBuilder) Bytes() []byte { return (*bytes.Buffer)(b).Bytes() } 73 74 // Time is used to write the passed time into the IndexBuilder for use as a 75 // memdb index value. 76 func (b *IndexBuilder) Time(t time.Time) { 77 val := t.Unix() 78 buf := make([]byte, 8) 79 binary.BigEndian.PutUint64(buf, uint64(val)) 80 (*bytes.Buffer)(b).Write(buf) 81 }