github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/kv/kv.go (about) 1 // Copyright 2015 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package kv 15 16 import ( 17 "bytes" 18 "io" 19 ) 20 21 const ( 22 // PresumeKeyNotExists directives that when dealing with a Get operation but failing to read data from cache, 23 // we presume that the key does not exist in Store. The actual existence will be checked before the 24 // transaction's commit. 25 // This option is an optimization for frequent checks during a transaction, e.g. batch inserts. 26 PresumeKeyNotExists Option = iota + 1 27 // PresumeKeyNotExistsError is the option key for error. 28 // When PresumeKeyNotExists is set and condition is not match, should throw the error. 29 PresumeKeyNotExistsError 30 ) 31 32 // Retriever is the interface wraps the basic Get and Seek methods. 33 type Retriever interface { 34 // Get gets the value for key k from kv store. 35 // If corresponding kv pair does not exist, it returns nil and ErrNotExist. 36 Get(k Key) ([]byte, error) 37 // Seek creates an Iterator positioned on the first entry that k <= entry's key. 38 // If such entry is not found, it returns an invalid Iterator with no error. 39 // The Iterator must be Closed after use. 40 Seek(k Key) (Iterator, error) 41 42 // SeekReverse creates a reversed Iterator positioned on the first entry which key is less than k. 43 // The returned iterator will iterate from greater key to smaller key. 44 // If k is nil, the returned iterator will be positioned at the last key. 45 SeekReverse(k Key) (Iterator, error) 46 } 47 48 // Mutator is the interface wraps the basic Set and Delete methods. 49 type Mutator interface { 50 // Set sets the value for key k as v into kv store. 51 // v must NOT be nil or empty, otherwise it returns ErrCannotSetNilValue. 52 Set(k Key, v []byte) error 53 // Delete removes the entry for key k from kv store. 54 Delete(k Key) error 55 } 56 57 // RetrieverMutator is the interface that groups Retriever and Mutator interfaces. 58 type RetrieverMutator interface { 59 Retriever 60 Mutator 61 } 62 63 // MemBuffer is an in-memory kv collection. It should be released after use. 64 type MemBuffer interface { 65 RetrieverMutator 66 // Release releases the buffer. 67 Release() 68 } 69 70 // Transaction defines the interface for operations inside a Transaction. 71 // This is not thread safe. 72 type Transaction interface { 73 RetrieverMutator 74 // Commit commits the transaction operations to KV store. 75 Commit() error 76 // Rollback undoes the transaction operations to KV store. 77 Rollback() error 78 // String implements fmt.Stringer interface. 79 String() string 80 // LockKeys tries to lock the entries with the keys in KV store. 81 LockKeys(keys ...Key) error 82 // SetOption sets an option with a value, when val is nil, uses the default 83 // value of this option. 84 SetOption(opt Option, val interface{}) 85 // DelOption deletes an option. 86 DelOption(opt Option) 87 // IsReadOnly checks if the transaction has only performed read operations. 88 IsReadOnly() bool 89 // GetClient gets a client instance. 90 GetClient() Client 91 // StartTS returns the transaction start timestamp. 92 StartTS() uint64 93 } 94 95 // Client is used to send request to KV layer. 96 type Client interface { 97 // Send sends request to KV layer, returns a Response. 98 Send(req *Request) Response 99 100 // SupportRequestType checks if reqType and subType is supported. 101 SupportRequestType(reqType, subType int64) bool 102 } 103 104 // ReqTypes. 105 const ( 106 ReqTypeSelect = 101 107 ReqTypeIndex = 102 108 109 ReqSubTypeBasic = 0 110 ReqSubTypeDesc = 10000 111 ) 112 113 // KeyRange represents a range where StartKey <= key < EndKey. 114 type KeyRange struct { 115 StartKey Key 116 EndKey Key 117 } 118 119 // IsPoint checks if the key range represents a point. 120 func (r *KeyRange) IsPoint() bool { 121 return bytes.Equal(r.StartKey.PrefixNext(), r.EndKey) 122 } 123 124 // Request represents a kv request. 125 type Request struct { 126 // The request type. 127 Tp int64 128 Data []byte 129 // Key Ranges 130 KeyRanges []KeyRange 131 // If KeepOrder is true, the response should be returned in order. 132 KeepOrder bool 133 // If desc is true, the request is sent in descending order. 134 Desc bool 135 // If concurrency is 1, it only sends the request to a single storage unit when 136 // ResponseIterator.Next is called. If concurrency is greater than 1, the request will be 137 // sent to multiple storage units concurrently. 138 Concurrency int 139 } 140 141 // Response represents the response returned from KV layer. 142 type Response interface { 143 // Next returns a resultSubset from a single storage unit. 144 // When full result set is returned, nil is returned. 145 Next() (resultSubset io.ReadCloser, err error) 146 // Close response. 147 Close() error 148 } 149 150 // Snapshot defines the interface for the snapshot fetched from KV store. 151 type Snapshot interface { 152 Retriever 153 // BatchGet gets a batch of values from snapshot. 154 BatchGet(keys []Key) (map[string][]byte, error) 155 // Release releases the snapshot to store. 156 Release() 157 } 158 159 // Driver is the interface that must be implemented by a KV storage. 160 type Driver interface { 161 // Open returns a new Storage. 162 // The path is the string for storage specific format. 163 Open(path string) (Storage, error) 164 } 165 166 // Storage defines the interface for storage. 167 // Isolation should be at least SI(SNAPSHOT ISOLATION) 168 type Storage interface { 169 // Begin transaction 170 Begin() (Transaction, error) 171 // GetSnapshot gets a snapshot that is able to read any data which data is <= ver. 172 // if ver is MaxVersion or > current max committed version, we will use current version for this snapshot. 173 GetSnapshot(ver Version) (Snapshot, error) 174 // Close store 175 Close() error 176 // Storage's unique ID 177 UUID() string 178 // CurrentVersion returns current max committed version. 179 CurrentVersion() (Version, error) 180 } 181 182 // FnKeyCmp is the function for iterator the keys 183 type FnKeyCmp func(key Key) bool 184 185 // Iterator is the interface for a iterator on KV store. 186 type Iterator interface { 187 Valid() bool 188 Key() Key 189 Value() []byte 190 Next() error 191 Close() 192 }