You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

439 lines
10 KiB

7 years ago
7 years ago
7 years ago
7 years ago
  1. package kv
  2. import (
  3. "bytes"
  4. "encoding/hex"
  5. "fmt"
  6. "sort"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/pkg/errors"
  11. wire "github.com/tendermint/go-wire"
  12. cmn "github.com/tendermint/tmlibs/common"
  13. dbm "github.com/tendermint/tmlibs/db"
  14. "github.com/tendermint/tmlibs/pubsub/query"
  15. "github.com/tendermint/tendermint/state/txindex"
  16. "github.com/tendermint/tendermint/types"
  17. )
  18. const (
  19. tagKeySeparator = "/"
  20. )
  21. var _ txindex.TxIndexer = (*TxIndex)(nil)
  22. // TxIndex is the simplest possible indexer, backed by key-value storage (levelDB).
  23. type TxIndex struct {
  24. store dbm.DB
  25. tagsToIndex []string
  26. indexAllTags bool
  27. }
  28. // NewTxIndex creates new KV indexer.
  29. func NewTxIndex(store dbm.DB, options ...func(*TxIndex)) *TxIndex {
  30. txi := &TxIndex{store: store, tagsToIndex: make([]string, 0), indexAllTags: false}
  31. for _, o := range options {
  32. o(txi)
  33. }
  34. return txi
  35. }
  36. // IndexTags is an option for setting which tags to index.
  37. func IndexTags(tags []string) func(*TxIndex) {
  38. return func(txi *TxIndex) {
  39. txi.tagsToIndex = tags
  40. }
  41. }
  42. // IndexAllTags is an option for indexing all tags.
  43. func IndexAllTags() func(*TxIndex) {
  44. return func(txi *TxIndex) {
  45. txi.indexAllTags = true
  46. }
  47. }
  48. // Get gets transaction from the TxIndex storage and returns it or nil if the
  49. // transaction is not found.
  50. func (txi *TxIndex) Get(hash []byte) (*types.TxResult, error) {
  51. if len(hash) == 0 {
  52. return nil, txindex.ErrorEmptyHash
  53. }
  54. rawBytes := txi.store.Get(hash)
  55. if rawBytes == nil {
  56. return nil, nil
  57. }
  58. txResult := new(types.TxResult)
  59. err := wire.UnmarshalBinary(rawBytes, &txResult)
  60. if err != nil {
  61. return nil, fmt.Errorf("Error reading TxResult: %v", err)
  62. }
  63. return txResult, nil
  64. }
  65. // AddBatch indexes a batch of transactions using the given list of tags.
  66. func (txi *TxIndex) AddBatch(b *txindex.Batch) error {
  67. storeBatch := txi.store.NewBatch()
  68. for _, result := range b.Ops {
  69. hash := result.Tx.Hash()
  70. // index tx by tags
  71. for _, tag := range result.Result.Tags {
  72. if txi.indexAllTags || cmn.StringInSlice(string(tag.Key), txi.tagsToIndex) {
  73. storeBatch.Set(keyForTag(tag, result), hash)
  74. }
  75. }
  76. // index tx by hash
  77. rawBytes, err := wire.MarshalBinary(result)
  78. if err != nil {
  79. return err
  80. }
  81. storeBatch.Set(hash, rawBytes)
  82. }
  83. storeBatch.Write()
  84. return nil
  85. }
  86. // Index indexes a single transaction using the given list of tags.
  87. func (txi *TxIndex) Index(result *types.TxResult) error {
  88. b := txi.store.NewBatch()
  89. hash := result.Tx.Hash()
  90. // index tx by tags
  91. for _, tag := range result.Result.Tags {
  92. if txi.indexAllTags || cmn.StringInSlice(string(tag.Key), txi.tagsToIndex) {
  93. b.Set(keyForTag(tag, result), hash)
  94. }
  95. }
  96. // index tx by hash
  97. rawBytes, err := wire.MarshalBinary(result)
  98. if err != nil {
  99. return err
  100. }
  101. b.Set(hash, rawBytes)
  102. b.Write()
  103. return nil
  104. }
  105. // Search performs a search using the given query. It breaks the query into
  106. // conditions (like "tx.height > 5"). For each condition, it queries the DB
  107. // index. One special use cases here: (1) if "tx.hash" is found, it returns tx
  108. // result for it (2) for range queries it is better for the client to provide
  109. // both lower and upper bounds, so we are not performing a full scan. Results
  110. // from querying indexes are then intersected and returned to the caller.
  111. func (txi *TxIndex) Search(q *query.Query) ([]*types.TxResult, error) {
  112. var hashes [][]byte
  113. var hashesInitialized bool
  114. // get a list of conditions (like "tx.height > 5")
  115. conditions := q.Conditions()
  116. // if there is a hash condition, return the result immediately
  117. hash, err, ok := lookForHash(conditions)
  118. if err != nil {
  119. return nil, errors.Wrap(err, "error during searching for a hash in the query")
  120. } else if ok {
  121. res, err := txi.Get(hash)
  122. if res == nil {
  123. return []*types.TxResult{}, nil
  124. }
  125. return []*types.TxResult{res}, errors.Wrap(err, "error while retrieving the result")
  126. }
  127. // conditions to skip because they're handled before "everything else"
  128. skipIndexes := make([]int, 0)
  129. // if there is a height condition ("tx.height=3"), extract it for faster lookups
  130. height, heightIndex := lookForHeight(conditions)
  131. if heightIndex >= 0 {
  132. skipIndexes = append(skipIndexes, heightIndex)
  133. }
  134. // extract ranges
  135. // if both upper and lower bounds exist, it's better to get them in order not
  136. // no iterate over kvs that are not within range.
  137. ranges, rangeIndexes := lookForRanges(conditions)
  138. if len(ranges) > 0 {
  139. skipIndexes = append(skipIndexes, rangeIndexes...)
  140. for _, r := range ranges {
  141. if !hashesInitialized {
  142. hashes = txi.matchRange(r, []byte(r.key))
  143. hashesInitialized = true
  144. } else {
  145. hashes = intersect(hashes, txi.matchRange(r, []byte(r.key)))
  146. }
  147. }
  148. }
  149. // for all other conditions
  150. for i, c := range conditions {
  151. if cmn.IntInSlice(i, skipIndexes) {
  152. continue
  153. }
  154. if !hashesInitialized {
  155. hashes = txi.match(c, startKey(c, height))
  156. hashesInitialized = true
  157. } else {
  158. hashes = intersect(hashes, txi.match(c, startKey(c, height)))
  159. }
  160. }
  161. results := make([]*types.TxResult, len(hashes))
  162. i := 0
  163. for _, h := range hashes {
  164. results[i], err = txi.Get(h)
  165. if err != nil {
  166. return nil, errors.Wrapf(err, "failed to get Tx{%X}", h)
  167. }
  168. i++
  169. }
  170. // sort by height by default
  171. sort.Slice(results, func(i, j int) bool {
  172. return results[i].Height < results[j].Height
  173. })
  174. return results, nil
  175. }
  176. func lookForHash(conditions []query.Condition) (hash []byte, err error, ok bool) {
  177. for _, c := range conditions {
  178. if c.Tag == types.TxHashKey {
  179. decoded, err := hex.DecodeString(c.Operand.(string))
  180. return decoded, err, true
  181. }
  182. }
  183. return
  184. }
  185. func lookForHeight(conditions []query.Condition) (height int64, index int) {
  186. for i, c := range conditions {
  187. if c.Tag == types.TxHeightKey {
  188. return c.Operand.(int64), i
  189. }
  190. }
  191. return 0, -1
  192. }
  193. // special map to hold range conditions
  194. // Example: account.number => queryRange{lowerBound: 1, upperBound: 5}
  195. type queryRanges map[string]queryRange
  196. type queryRange struct {
  197. key string
  198. lowerBound interface{} // int || time.Time
  199. includeLowerBound bool
  200. upperBound interface{} // int || time.Time
  201. includeUpperBound bool
  202. }
  203. func (r queryRange) lowerBoundValue() interface{} {
  204. if r.lowerBound == nil {
  205. return nil
  206. }
  207. if r.includeLowerBound {
  208. return r.lowerBound
  209. } else {
  210. switch t := r.lowerBound.(type) {
  211. case int64:
  212. return t + 1
  213. case time.Time:
  214. return t.Unix() + 1
  215. default:
  216. panic("not implemented")
  217. }
  218. }
  219. }
  220. func (r queryRange) AnyBound() interface{} {
  221. if r.lowerBound != nil {
  222. return r.lowerBound
  223. } else {
  224. return r.upperBound
  225. }
  226. }
  227. func (r queryRange) upperBoundValue() interface{} {
  228. if r.upperBound == nil {
  229. return nil
  230. }
  231. if r.includeUpperBound {
  232. return r.upperBound
  233. } else {
  234. switch t := r.upperBound.(type) {
  235. case int64:
  236. return t - 1
  237. case time.Time:
  238. return t.Unix() - 1
  239. default:
  240. panic("not implemented")
  241. }
  242. }
  243. }
  244. func lookForRanges(conditions []query.Condition) (ranges queryRanges, indexes []int) {
  245. ranges = make(queryRanges)
  246. for i, c := range conditions {
  247. if isRangeOperation(c.Op) {
  248. r, ok := ranges[c.Tag]
  249. if !ok {
  250. r = queryRange{key: c.Tag}
  251. }
  252. switch c.Op {
  253. case query.OpGreater:
  254. r.lowerBound = c.Operand
  255. case query.OpGreaterEqual:
  256. r.includeLowerBound = true
  257. r.lowerBound = c.Operand
  258. case query.OpLess:
  259. r.upperBound = c.Operand
  260. case query.OpLessEqual:
  261. r.includeUpperBound = true
  262. r.upperBound = c.Operand
  263. }
  264. ranges[c.Tag] = r
  265. indexes = append(indexes, i)
  266. }
  267. }
  268. return ranges, indexes
  269. }
  270. func isRangeOperation(op query.Operator) bool {
  271. switch op {
  272. case query.OpGreater, query.OpGreaterEqual, query.OpLess, query.OpLessEqual:
  273. return true
  274. default:
  275. return false
  276. }
  277. }
  278. func (txi *TxIndex) match(c query.Condition, startKey []byte) (hashes [][]byte) {
  279. if c.Op == query.OpEqual {
  280. it := dbm.IteratePrefix(txi.store, startKey)
  281. defer it.Close()
  282. for ; it.Valid(); it.Next() {
  283. hashes = append(hashes, it.Value())
  284. }
  285. } else if c.Op == query.OpContains {
  286. // XXX: doing full scan because startKey does not apply here
  287. // For example, if startKey = "account.owner=an" and search query = "accoutn.owner CONSISTS an"
  288. // we can't iterate with prefix "account.owner=an" because we might miss keys like "account.owner=Ulan"
  289. it := txi.store.Iterator(nil, nil)
  290. defer it.Close()
  291. for ; it.Valid(); it.Next() {
  292. if !isTagKey(it.Key()) {
  293. continue
  294. }
  295. if strings.Contains(extractValueFromKey(it.Key()), c.Operand.(string)) {
  296. hashes = append(hashes, it.Value())
  297. }
  298. }
  299. } else {
  300. panic("other operators should be handled already")
  301. }
  302. return
  303. }
  304. func (txi *TxIndex) matchRange(r queryRange, prefix []byte) (hashes [][]byte) {
  305. // create a map to prevent duplicates
  306. hashesMap := make(map[string][]byte)
  307. lowerBound := r.lowerBoundValue()
  308. upperBound := r.upperBoundValue()
  309. it := dbm.IteratePrefix(txi.store, prefix)
  310. defer it.Close()
  311. LOOP:
  312. for ; it.Valid(); it.Next() {
  313. if !isTagKey(it.Key()) {
  314. continue
  315. }
  316. switch r.AnyBound().(type) {
  317. case int64:
  318. v, err := strconv.ParseInt(extractValueFromKey(it.Key()), 10, 64)
  319. if err != nil {
  320. continue LOOP
  321. }
  322. include := true
  323. if lowerBound != nil && v < lowerBound.(int64) {
  324. include = false
  325. }
  326. if upperBound != nil && v > upperBound.(int64) {
  327. include = false
  328. }
  329. if include {
  330. hashesMap[fmt.Sprintf("%X", it.Value())] = it.Value()
  331. }
  332. // XXX: passing time in a ABCI Tags is not yet implemented
  333. // case time.Time:
  334. // v := strconv.ParseInt(extractValueFromKey(it.Key()), 10, 64)
  335. // if v == r.upperBound {
  336. // break
  337. // }
  338. }
  339. }
  340. hashes = make([][]byte, len(hashesMap))
  341. i := 0
  342. for _, h := range hashesMap {
  343. hashes[i] = h
  344. i++
  345. }
  346. return
  347. }
  348. ///////////////////////////////////////////////////////////////////////////////
  349. // Keys
  350. func startKey(c query.Condition, height int64) []byte {
  351. var key string
  352. if height > 0 {
  353. key = fmt.Sprintf("%s/%v/%d", c.Tag, c.Operand, height)
  354. } else {
  355. key = fmt.Sprintf("%s/%v", c.Tag, c.Operand)
  356. }
  357. return []byte(key)
  358. }
  359. func isTagKey(key []byte) bool {
  360. return strings.Count(string(key), tagKeySeparator) == 3
  361. }
  362. func extractValueFromKey(key []byte) string {
  363. parts := strings.SplitN(string(key), tagKeySeparator, 3)
  364. return parts[1]
  365. }
  366. func keyForTag(tag cmn.KVPair, result *types.TxResult) []byte {
  367. return []byte(fmt.Sprintf("%s/%s/%d/%d", tag.Key, tag.Value, result.Height, result.Index))
  368. }
  369. ///////////////////////////////////////////////////////////////////////////////
  370. // Utils
  371. func intersect(as, bs [][]byte) [][]byte {
  372. i := make([][]byte, 0, cmn.MinInt(len(as), len(bs)))
  373. for _, a := range as {
  374. for _, b := range bs {
  375. if bytes.Equal(a, b) {
  376. i = append(i, a)
  377. }
  378. }
  379. }
  380. return i
  381. }