本站源代码
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.

328 lines
5.5KB

  1. package store
  2. import (
  3. "bytes"
  4. "github.com/lunny/nodb/store/driver"
  5. )
  6. const (
  7. IteratorForward uint8 = 0
  8. IteratorBackward uint8 = 1
  9. )
  10. const (
  11. RangeClose uint8 = 0x00
  12. RangeLOpen uint8 = 0x01
  13. RangeROpen uint8 = 0x10
  14. RangeOpen uint8 = 0x11
  15. )
  16. // min must less or equal than max
  17. //
  18. // range type:
  19. //
  20. // close: [min, max]
  21. // open: (min, max)
  22. // lopen: (min, max]
  23. // ropen: [min, max)
  24. //
  25. type Range struct {
  26. Min []byte
  27. Max []byte
  28. Type uint8
  29. }
  30. type Limit struct {
  31. Offset int
  32. Count int
  33. }
  34. type Iterator struct {
  35. it driver.IIterator
  36. }
  37. // Returns a copy of key.
  38. func (it *Iterator) Key() []byte {
  39. k := it.it.Key()
  40. if k == nil {
  41. return nil
  42. }
  43. return append([]byte{}, k...)
  44. }
  45. // Returns a copy of value.
  46. func (it *Iterator) Value() []byte {
  47. v := it.it.Value()
  48. if v == nil {
  49. return nil
  50. }
  51. return append([]byte{}, v...)
  52. }
  53. // Returns a reference of key.
  54. // you must be careful that it will be changed after next iterate.
  55. func (it *Iterator) RawKey() []byte {
  56. return it.it.Key()
  57. }
  58. // Returns a reference of value.
  59. // you must be careful that it will be changed after next iterate.
  60. func (it *Iterator) RawValue() []byte {
  61. return it.it.Value()
  62. }
  63. // Copy key to b, if b len is small or nil, returns a new one.
  64. func (it *Iterator) BufKey(b []byte) []byte {
  65. k := it.RawKey()
  66. if k == nil {
  67. return nil
  68. }
  69. if b == nil {
  70. b = []byte{}
  71. }
  72. b = b[0:0]
  73. return append(b, k...)
  74. }
  75. // Copy value to b, if b len is small or nil, returns a new one.
  76. func (it *Iterator) BufValue(b []byte) []byte {
  77. v := it.RawValue()
  78. if v == nil {
  79. return nil
  80. }
  81. if b == nil {
  82. b = []byte{}
  83. }
  84. b = b[0:0]
  85. return append(b, v...)
  86. }
  87. func (it *Iterator) Close() {
  88. if it.it != nil {
  89. it.it.Close()
  90. it.it = nil
  91. }
  92. }
  93. func (it *Iterator) Valid() bool {
  94. return it.it.Valid()
  95. }
  96. func (it *Iterator) Next() {
  97. it.it.Next()
  98. }
  99. func (it *Iterator) Prev() {
  100. it.it.Prev()
  101. }
  102. func (it *Iterator) SeekToFirst() {
  103. it.it.First()
  104. }
  105. func (it *Iterator) SeekToLast() {
  106. it.it.Last()
  107. }
  108. func (it *Iterator) Seek(key []byte) {
  109. it.it.Seek(key)
  110. }
  111. // Finds by key, if not found, nil returns.
  112. func (it *Iterator) Find(key []byte) []byte {
  113. it.Seek(key)
  114. if it.Valid() {
  115. k := it.RawKey()
  116. if k == nil {
  117. return nil
  118. } else if bytes.Equal(k, key) {
  119. return it.Value()
  120. }
  121. }
  122. return nil
  123. }
  124. // Finds by key, if not found, nil returns, else a reference of value returns.
  125. // you must be careful that it will be changed after next iterate.
  126. func (it *Iterator) RawFind(key []byte) []byte {
  127. it.Seek(key)
  128. if it.Valid() {
  129. k := it.RawKey()
  130. if k == nil {
  131. return nil
  132. } else if bytes.Equal(k, key) {
  133. return it.RawValue()
  134. }
  135. }
  136. return nil
  137. }
  138. type RangeLimitIterator struct {
  139. it *Iterator
  140. r *Range
  141. l *Limit
  142. step int
  143. //0 for IteratorForward, 1 for IteratorBackward
  144. direction uint8
  145. }
  146. func (it *RangeLimitIterator) Key() []byte {
  147. return it.it.Key()
  148. }
  149. func (it *RangeLimitIterator) Value() []byte {
  150. return it.it.Value()
  151. }
  152. func (it *RangeLimitIterator) RawKey() []byte {
  153. return it.it.RawKey()
  154. }
  155. func (it *RangeLimitIterator) RawValue() []byte {
  156. return it.it.RawValue()
  157. }
  158. func (it *RangeLimitIterator) BufKey(b []byte) []byte {
  159. return it.it.BufKey(b)
  160. }
  161. func (it *RangeLimitIterator) BufValue(b []byte) []byte {
  162. return it.it.BufValue(b)
  163. }
  164. func (it *RangeLimitIterator) Valid() bool {
  165. if it.l.Offset < 0 {
  166. return false
  167. } else if !it.it.Valid() {
  168. return false
  169. } else if it.l.Count >= 0 && it.step >= it.l.Count {
  170. return false
  171. }
  172. if it.direction == IteratorForward {
  173. if it.r.Max != nil {
  174. r := bytes.Compare(it.it.RawKey(), it.r.Max)
  175. if it.r.Type&RangeROpen > 0 {
  176. return !(r >= 0)
  177. } else {
  178. return !(r > 0)
  179. }
  180. }
  181. } else {
  182. if it.r.Min != nil {
  183. r := bytes.Compare(it.it.RawKey(), it.r.Min)
  184. if it.r.Type&RangeLOpen > 0 {
  185. return !(r <= 0)
  186. } else {
  187. return !(r < 0)
  188. }
  189. }
  190. }
  191. return true
  192. }
  193. func (it *RangeLimitIterator) Next() {
  194. it.step++
  195. if it.direction == IteratorForward {
  196. it.it.Next()
  197. } else {
  198. it.it.Prev()
  199. }
  200. }
  201. func (it *RangeLimitIterator) Close() {
  202. it.it.Close()
  203. }
  204. func NewRangeLimitIterator(i *Iterator, r *Range, l *Limit) *RangeLimitIterator {
  205. return rangeLimitIterator(i, r, l, IteratorForward)
  206. }
  207. func NewRevRangeLimitIterator(i *Iterator, r *Range, l *Limit) *RangeLimitIterator {
  208. return rangeLimitIterator(i, r, l, IteratorBackward)
  209. }
  210. func NewRangeIterator(i *Iterator, r *Range) *RangeLimitIterator {
  211. return rangeLimitIterator(i, r, &Limit{0, -1}, IteratorForward)
  212. }
  213. func NewRevRangeIterator(i *Iterator, r *Range) *RangeLimitIterator {
  214. return rangeLimitIterator(i, r, &Limit{0, -1}, IteratorBackward)
  215. }
  216. func rangeLimitIterator(i *Iterator, r *Range, l *Limit, direction uint8) *RangeLimitIterator {
  217. it := new(RangeLimitIterator)
  218. it.it = i
  219. it.r = r
  220. it.l = l
  221. it.direction = direction
  222. it.step = 0
  223. if l.Offset < 0 {
  224. return it
  225. }
  226. if direction == IteratorForward {
  227. if r.Min == nil {
  228. it.it.SeekToFirst()
  229. } else {
  230. it.it.Seek(r.Min)
  231. if r.Type&RangeLOpen > 0 {
  232. if it.it.Valid() && bytes.Equal(it.it.RawKey(), r.Min) {
  233. it.it.Next()
  234. }
  235. }
  236. }
  237. } else {
  238. if r.Max == nil {
  239. it.it.SeekToLast()
  240. } else {
  241. it.it.Seek(r.Max)
  242. if !it.it.Valid() {
  243. it.it.SeekToLast()
  244. } else {
  245. if !bytes.Equal(it.it.RawKey(), r.Max) {
  246. it.it.Prev()
  247. }
  248. }
  249. if r.Type&RangeROpen > 0 {
  250. if it.it.Valid() && bytes.Equal(it.it.RawKey(), r.Max) {
  251. it.it.Prev()
  252. }
  253. }
  254. }
  255. }
  256. for i := 0; i < l.Offset; i++ {
  257. if it.it.Valid() {
  258. if it.direction == IteratorForward {
  259. it.it.Next()
  260. } else {
  261. it.it.Prev()
  262. }
  263. }
  264. }
  265. return it
  266. }
上海开阖软件有限公司 沪ICP备12045867号-1