github.com/pachyderm/pachyderm@v1.13.4/src/server/pkg/collection/types.go (about) 1 package collection 2 3 import ( 4 "context" 5 6 "github.com/pachyderm/pachyderm/src/server/pkg/watch" 7 8 "github.com/gogo/protobuf/proto" 9 ) 10 11 // Collection implements helper functions that makes common operations 12 // on top of etcd more pleasant to work with. It's called collection 13 // because most of our data is modelled as collections, such as repos, 14 // commits, branches, etc. 15 type Collection interface { 16 // Path returns the full etcd path of the given key in the collection 17 Path(string) string 18 // ReadWrite enables reads and writes on a collection in a 19 // transactional manner. Specifically, all writes are applied 20 // atomically, and writes are only applied if reads have not been 21 // invalidated at the end of the transaction. Basically, it's 22 // software transactional memory. See this blog post for details: 23 // https://coreos.com/blog/transactional-memory-with-etcd3.html 24 ReadWrite(stm STM) ReadWriteCollection 25 // ReadWriteInt is the same as ReadWrite except that it operates on 26 // integral items, as opposed to protobuf items 27 ReadWriteInt(stm STM) ReadWriteIntCollection 28 // For read-only operatons, use the ReadOnly for better performance 29 ReadOnly(ctx context.Context) ReadonlyCollection 30 // Claim attempts to claim a key and run the passed in callback with 31 // the context for the claim. 32 Claim(ctx context.Context, key string, val proto.Message, f func(context.Context) error) error 33 } 34 35 // Index specifies a secondary index on a collection. 36 // 37 // Indexes are created in a transactional manner thanks to etcd's 38 // transactional support. 39 // 40 // A secondary index for collection "foo" on field "bar" will reside under 41 // the path `/foo__index_bar`. Each item under the path is in turn a 42 // directory whose name is the value of the field `bar`. For instance, 43 // if you have a object in collection `foo` whose `bar` field is `test`, 44 // then you will see a directory at path `/foo__index_bar/test`. 45 // 46 // Under that directory, you have keys that point to items in the collection. 47 // For instance, if the aforementioned object has the key "buzz", then you 48 // will see an item at `/foo__index_bar/test/buzz`. The value of this item 49 // is empty. Thus, to get all items in collection `foo` whose values of 50 // field `bar` is `test`, we issue a query for all items under 51 // `foo__index_bar/test`. 52 // 53 // Multi specifies whether this is a multi-index. A multi-index is an index 54 // on a field that's a slice. The item is then indexed on each element of 55 // the slice. 56 type Index struct { 57 Field string 58 Multi bool 59 limit int64 60 } 61 62 // ReadWriteCollection is a collection interface that supports read,write and delete 63 // operations. 64 type ReadWriteCollection interface { 65 Get(key string, val proto.Message) error 66 Put(key string, val proto.Message) error 67 // TTL returns the amount of time that 'key' will continue to exist in the 68 // collection, or '0' if 'key' will remain in the collection indefinitely 69 TTL(key string) (int64, error) 70 // PutTTL is the same as Put except that the object is removed after 71 // TTL seconds. 72 // WARNING: using PutTTL with a collection that has secondary indices 73 // can result in inconsistency, as the indices are removed at roughly 74 // but not exactly the same time as the documents. 75 PutTTL(key string, val proto.Message, ttl int64) error 76 // Update reads the current value associated with 'key', calls 'f' to update 77 // the value, and writes the new value back to the collection. 'key' must be 78 // present in the collection, or a 'Not Found' error is returned 79 Update(key string, val proto.Message, f func() error) error 80 // Upsert is like Update but 'key' is not required to be present 81 Upsert(key string, val proto.Message, f func() error) error 82 Create(key string, val proto.Message) error 83 Delete(key string) error 84 DeleteAll() 85 DeleteAllPrefix(prefix string) 86 } 87 88 // ReadWriteIntCollection is a ReadonlyCollection interface specifically for ints. 89 type ReadWriteIntCollection interface { 90 Create(key string, val int) error 91 Get(key string) (int, error) 92 Increment(key string) error 93 IncrementBy(key string, n int) error 94 Decrement(key string) error 95 DecrementBy(key string, n int) error 96 Delete(key string) error 97 } 98 99 // ReadonlyCollection is a collection interface that only supports read ops. 100 type ReadonlyCollection interface { 101 Get(key string, val proto.Message) error 102 GetByIndex(index *Index, indexVal interface{}, val proto.Message, opts *Options, f func(key string) error) error 103 // GetBlock is like Get but waits for the key to exist if it doesn't already. 104 GetBlock(key string, val proto.Message) error 105 // TTL returns the number of seconds that 'key' will continue to exist in the 106 // collection, or '0' if 'key' will remain in the collection indefinitely 107 TTL(key string) (int64, error) 108 List(val proto.Message, opts *Options, f func(key string) error) error 109 ListRev(val proto.Message, opts *Options, f func(key string, createRev int64) error) error 110 ListPrefix(prefix string, val proto.Message, opts *Options, f func(string) error) error 111 Count() (int64, error) 112 CountRev(int64) (int64, int64, error) 113 Watch(opts ...watch.OpOption) (watch.Watcher, error) 114 WatchF(f func(*watch.Event) error, opts ...watch.OpOption) error 115 WatchOne(key string, opts ...watch.OpOption) (watch.Watcher, error) 116 WatchOneF(key string, f func(*watch.Event) error, opts ...watch.OpOption) error 117 WatchByIndex(index *Index, val interface{}) (watch.Watcher, error) 118 }