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.

943 lines
19 KiB

  1. package nodb
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "errors"
  6. "time"
  7. "github.com/lunny/nodb/store"
  8. )
  9. const (
  10. MinScore int64 = -1<<63 + 1
  11. MaxScore int64 = 1<<63 - 1
  12. InvalidScore int64 = -1 << 63
  13. AggregateSum byte = 0
  14. AggregateMin byte = 1
  15. AggregateMax byte = 2
  16. )
  17. type ScorePair struct {
  18. Score int64
  19. Member []byte
  20. }
  21. var errZSizeKey = errors.New("invalid zsize key")
  22. var errZSetKey = errors.New("invalid zset key")
  23. var errZScoreKey = errors.New("invalid zscore key")
  24. var errScoreOverflow = errors.New("zset score overflow")
  25. var errInvalidAggregate = errors.New("invalid aggregate")
  26. var errInvalidWeightNum = errors.New("invalid weight number")
  27. var errInvalidSrcKeyNum = errors.New("invalid src key number")
  28. const (
  29. zsetNScoreSep byte = '<'
  30. zsetPScoreSep byte = zsetNScoreSep + 1
  31. zsetStopScoreSep byte = zsetPScoreSep + 1
  32. zsetStartMemSep byte = ':'
  33. zsetStopMemSep byte = zsetStartMemSep + 1
  34. )
  35. func checkZSetKMSize(key []byte, member []byte) error {
  36. if len(key) > MaxKeySize || len(key) == 0 {
  37. return errKeySize
  38. } else if len(member) > MaxZSetMemberSize || len(member) == 0 {
  39. return errZSetMemberSize
  40. }
  41. return nil
  42. }
  43. func (db *DB) zEncodeSizeKey(key []byte) []byte {
  44. buf := make([]byte, len(key)+2)
  45. buf[0] = db.index
  46. buf[1] = ZSizeType
  47. copy(buf[2:], key)
  48. return buf
  49. }
  50. func (db *DB) zDecodeSizeKey(ek []byte) ([]byte, error) {
  51. if len(ek) < 2 || ek[0] != db.index || ek[1] != ZSizeType {
  52. return nil, errZSizeKey
  53. }
  54. return ek[2:], nil
  55. }
  56. func (db *DB) zEncodeSetKey(key []byte, member []byte) []byte {
  57. buf := make([]byte, len(key)+len(member)+5)
  58. pos := 0
  59. buf[pos] = db.index
  60. pos++
  61. buf[pos] = ZSetType
  62. pos++
  63. binary.BigEndian.PutUint16(buf[pos:], uint16(len(key)))
  64. pos += 2
  65. copy(buf[pos:], key)
  66. pos += len(key)
  67. buf[pos] = zsetStartMemSep
  68. pos++
  69. copy(buf[pos:], member)
  70. return buf
  71. }
  72. func (db *DB) zDecodeSetKey(ek []byte) ([]byte, []byte, error) {
  73. if len(ek) < 5 || ek[0] != db.index || ek[1] != ZSetType {
  74. return nil, nil, errZSetKey
  75. }
  76. keyLen := int(binary.BigEndian.Uint16(ek[2:]))
  77. if keyLen+5 > len(ek) {
  78. return nil, nil, errZSetKey
  79. }
  80. key := ek[4 : 4+keyLen]
  81. if ek[4+keyLen] != zsetStartMemSep {
  82. return nil, nil, errZSetKey
  83. }
  84. member := ek[5+keyLen:]
  85. return key, member, nil
  86. }
  87. func (db *DB) zEncodeStartSetKey(key []byte) []byte {
  88. k := db.zEncodeSetKey(key, nil)
  89. return k
  90. }
  91. func (db *DB) zEncodeStopSetKey(key []byte) []byte {
  92. k := db.zEncodeSetKey(key, nil)
  93. k[len(k)-1] = zsetStartMemSep + 1
  94. return k
  95. }
  96. func (db *DB) zEncodeScoreKey(key []byte, member []byte, score int64) []byte {
  97. buf := make([]byte, len(key)+len(member)+14)
  98. pos := 0
  99. buf[pos] = db.index
  100. pos++
  101. buf[pos] = ZScoreType
  102. pos++
  103. binary.BigEndian.PutUint16(buf[pos:], uint16(len(key)))
  104. pos += 2
  105. copy(buf[pos:], key)
  106. pos += len(key)
  107. if score < 0 {
  108. buf[pos] = zsetNScoreSep
  109. } else {
  110. buf[pos] = zsetPScoreSep
  111. }
  112. pos++
  113. binary.BigEndian.PutUint64(buf[pos:], uint64(score))
  114. pos += 8
  115. buf[pos] = zsetStartMemSep
  116. pos++
  117. copy(buf[pos:], member)
  118. return buf
  119. }
  120. func (db *DB) zEncodeStartScoreKey(key []byte, score int64) []byte {
  121. return db.zEncodeScoreKey(key, nil, score)
  122. }
  123. func (db *DB) zEncodeStopScoreKey(key []byte, score int64) []byte {
  124. k := db.zEncodeScoreKey(key, nil, score)
  125. k[len(k)-1] = zsetStopMemSep
  126. return k
  127. }
  128. func (db *DB) zDecodeScoreKey(ek []byte) (key []byte, member []byte, score int64, err error) {
  129. if len(ek) < 14 || ek[0] != db.index || ek[1] != ZScoreType {
  130. err = errZScoreKey
  131. return
  132. }
  133. keyLen := int(binary.BigEndian.Uint16(ek[2:]))
  134. if keyLen+14 > len(ek) {
  135. err = errZScoreKey
  136. return
  137. }
  138. key = ek[4 : 4+keyLen]
  139. pos := 4 + keyLen
  140. if (ek[pos] != zsetNScoreSep) && (ek[pos] != zsetPScoreSep) {
  141. err = errZScoreKey
  142. return
  143. }
  144. pos++
  145. score = int64(binary.BigEndian.Uint64(ek[pos:]))
  146. pos += 8
  147. if ek[pos] != zsetStartMemSep {
  148. err = errZScoreKey
  149. return
  150. }
  151. pos++
  152. member = ek[pos:]
  153. return
  154. }
  155. func (db *DB) zSetItem(t *batch, key []byte, score int64, member []byte) (int64, error) {
  156. if score <= MinScore || score >= MaxScore {
  157. return 0, errScoreOverflow
  158. }
  159. var exists int64 = 0
  160. ek := db.zEncodeSetKey(key, member)
  161. if v, err := db.bucket.Get(ek); err != nil {
  162. return 0, err
  163. } else if v != nil {
  164. exists = 1
  165. if s, err := Int64(v, err); err != nil {
  166. return 0, err
  167. } else {
  168. sk := db.zEncodeScoreKey(key, member, s)
  169. t.Delete(sk)
  170. }
  171. }
  172. t.Put(ek, PutInt64(score))
  173. sk := db.zEncodeScoreKey(key, member, score)
  174. t.Put(sk, []byte{})
  175. return exists, nil
  176. }
  177. func (db *DB) zDelItem(t *batch, key []byte, member []byte, skipDelScore bool) (int64, error) {
  178. ek := db.zEncodeSetKey(key, member)
  179. if v, err := db.bucket.Get(ek); err != nil {
  180. return 0, err
  181. } else if v == nil {
  182. //not exists
  183. return 0, nil
  184. } else {
  185. //exists
  186. if !skipDelScore {
  187. //we must del score
  188. if s, err := Int64(v, err); err != nil {
  189. return 0, err
  190. } else {
  191. sk := db.zEncodeScoreKey(key, member, s)
  192. t.Delete(sk)
  193. }
  194. }
  195. }
  196. t.Delete(ek)
  197. return 1, nil
  198. }
  199. func (db *DB) zDelete(t *batch, key []byte) int64 {
  200. delMembCnt, _ := db.zRemRange(t, key, MinScore, MaxScore, 0, -1)
  201. // todo : log err
  202. return delMembCnt
  203. }
  204. func (db *DB) zExpireAt(key []byte, when int64) (int64, error) {
  205. t := db.zsetBatch
  206. t.Lock()
  207. defer t.Unlock()
  208. if zcnt, err := db.ZCard(key); err != nil || zcnt == 0 {
  209. return 0, err
  210. } else {
  211. db.expireAt(t, ZSetType, key, when)
  212. if err := t.Commit(); err != nil {
  213. return 0, err
  214. }
  215. }
  216. return 1, nil
  217. }
  218. func (db *DB) ZAdd(key []byte, args ...ScorePair) (int64, error) {
  219. if len(args) == 0 {
  220. return 0, nil
  221. }
  222. t := db.zsetBatch
  223. t.Lock()
  224. defer t.Unlock()
  225. var num int64 = 0
  226. for i := 0; i < len(args); i++ {
  227. score := args[i].Score
  228. member := args[i].Member
  229. if err := checkZSetKMSize(key, member); err != nil {
  230. return 0, err
  231. }
  232. if n, err := db.zSetItem(t, key, score, member); err != nil {
  233. return 0, err
  234. } else if n == 0 {
  235. //add new
  236. num++
  237. }
  238. }
  239. if _, err := db.zIncrSize(t, key, num); err != nil {
  240. return 0, err
  241. }
  242. //todo add binlog
  243. err := t.Commit()
  244. return num, err
  245. }
  246. func (db *DB) zIncrSize(t *batch, key []byte, delta int64) (int64, error) {
  247. sk := db.zEncodeSizeKey(key)
  248. size, err := Int64(db.bucket.Get(sk))
  249. if err != nil {
  250. return 0, err
  251. } else {
  252. size += delta
  253. if size <= 0 {
  254. size = 0
  255. t.Delete(sk)
  256. db.rmExpire(t, ZSetType, key)
  257. } else {
  258. t.Put(sk, PutInt64(size))
  259. }
  260. }
  261. return size, nil
  262. }
  263. func (db *DB) ZCard(key []byte) (int64, error) {
  264. if err := checkKeySize(key); err != nil {
  265. return 0, err
  266. }
  267. sk := db.zEncodeSizeKey(key)
  268. return Int64(db.bucket.Get(sk))
  269. }
  270. func (db *DB) ZScore(key []byte, member []byte) (int64, error) {
  271. if err := checkZSetKMSize(key, member); err != nil {
  272. return InvalidScore, err
  273. }
  274. var score int64 = InvalidScore
  275. k := db.zEncodeSetKey(key, member)
  276. if v, err := db.bucket.Get(k); err != nil {
  277. return InvalidScore, err
  278. } else if v == nil {
  279. return InvalidScore, ErrScoreMiss
  280. } else {
  281. if score, err = Int64(v, nil); err != nil {
  282. return InvalidScore, err
  283. }
  284. }
  285. return score, nil
  286. }
  287. func (db *DB) ZRem(key []byte, members ...[]byte) (int64, error) {
  288. if len(members) == 0 {
  289. return 0, nil
  290. }
  291. t := db.zsetBatch
  292. t.Lock()
  293. defer t.Unlock()
  294. var num int64 = 0
  295. for i := 0; i < len(members); i++ {
  296. if err := checkZSetKMSize(key, members[i]); err != nil {
  297. return 0, err
  298. }
  299. if n, err := db.zDelItem(t, key, members[i], false); err != nil {
  300. return 0, err
  301. } else if n == 1 {
  302. num++
  303. }
  304. }
  305. if _, err := db.zIncrSize(t, key, -num); err != nil {
  306. return 0, err
  307. }
  308. err := t.Commit()
  309. return num, err
  310. }
  311. func (db *DB) ZIncrBy(key []byte, delta int64, member []byte) (int64, error) {
  312. if err := checkZSetKMSize(key, member); err != nil {
  313. return InvalidScore, err
  314. }
  315. t := db.zsetBatch
  316. t.Lock()
  317. defer t.Unlock()
  318. ek := db.zEncodeSetKey(key, member)
  319. var oldScore int64 = 0
  320. v, err := db.bucket.Get(ek)
  321. if err != nil {
  322. return InvalidScore, err
  323. } else if v == nil {
  324. db.zIncrSize(t, key, 1)
  325. } else {
  326. if oldScore, err = Int64(v, err); err != nil {
  327. return InvalidScore, err
  328. }
  329. }
  330. newScore := oldScore + delta
  331. if newScore >= MaxScore || newScore <= MinScore {
  332. return InvalidScore, errScoreOverflow
  333. }
  334. sk := db.zEncodeScoreKey(key, member, newScore)
  335. t.Put(sk, []byte{})
  336. t.Put(ek, PutInt64(newScore))
  337. if v != nil {
  338. // so as to update score, we must delete the old one
  339. oldSk := db.zEncodeScoreKey(key, member, oldScore)
  340. t.Delete(oldSk)
  341. }
  342. err = t.Commit()
  343. return newScore, err
  344. }
  345. func (db *DB) ZCount(key []byte, min int64, max int64) (int64, error) {
  346. if err := checkKeySize(key); err != nil {
  347. return 0, err
  348. }
  349. minKey := db.zEncodeStartScoreKey(key, min)
  350. maxKey := db.zEncodeStopScoreKey(key, max)
  351. rangeType := store.RangeROpen
  352. it := db.bucket.RangeLimitIterator(minKey, maxKey, rangeType, 0, -1)
  353. var n int64 = 0
  354. for ; it.Valid(); it.Next() {
  355. n++
  356. }
  357. it.Close()
  358. return n, nil
  359. }
  360. func (db *DB) zrank(key []byte, member []byte, reverse bool) (int64, error) {
  361. if err := checkZSetKMSize(key, member); err != nil {
  362. return 0, err
  363. }
  364. k := db.zEncodeSetKey(key, member)
  365. it := db.bucket.NewIterator()
  366. defer it.Close()
  367. if v := it.Find(k); v == nil {
  368. return -1, nil
  369. } else {
  370. if s, err := Int64(v, nil); err != nil {
  371. return 0, err
  372. } else {
  373. var rit *store.RangeLimitIterator
  374. sk := db.zEncodeScoreKey(key, member, s)
  375. if !reverse {
  376. minKey := db.zEncodeStartScoreKey(key, MinScore)
  377. rit = store.NewRangeIterator(it, &store.Range{minKey, sk, store.RangeClose})
  378. } else {
  379. maxKey := db.zEncodeStopScoreKey(key, MaxScore)
  380. rit = store.NewRevRangeIterator(it, &store.Range{sk, maxKey, store.RangeClose})
  381. }
  382. var lastKey []byte = nil
  383. var n int64 = 0
  384. for ; rit.Valid(); rit.Next() {
  385. n++
  386. lastKey = rit.BufKey(lastKey)
  387. }
  388. if _, m, _, err := db.zDecodeScoreKey(lastKey); err == nil && bytes.Equal(m, member) {
  389. n--
  390. return n, nil
  391. }
  392. }
  393. }
  394. return -1, nil
  395. }
  396. func (db *DB) zIterator(key []byte, min int64, max int64, offset int, count int, reverse bool) *store.RangeLimitIterator {
  397. minKey := db.zEncodeStartScoreKey(key, min)
  398. maxKey := db.zEncodeStopScoreKey(key, max)
  399. if !reverse {
  400. return db.bucket.RangeLimitIterator(minKey, maxKey, store.RangeClose, offset, count)
  401. } else {
  402. return db.bucket.RevRangeLimitIterator(minKey, maxKey, store.RangeClose, offset, count)
  403. }
  404. }
  405. func (db *DB) zRemRange(t *batch, key []byte, min int64, max int64, offset int, count int) (int64, error) {
  406. if len(key) > MaxKeySize {
  407. return 0, errKeySize
  408. }
  409. it := db.zIterator(key, min, max, offset, count, false)
  410. var num int64 = 0
  411. for ; it.Valid(); it.Next() {
  412. sk := it.RawKey()
  413. _, m, _, err := db.zDecodeScoreKey(sk)
  414. if err != nil {
  415. continue
  416. }
  417. if n, err := db.zDelItem(t, key, m, true); err != nil {
  418. return 0, err
  419. } else if n == 1 {
  420. num++
  421. }
  422. t.Delete(sk)
  423. }
  424. it.Close()
  425. if _, err := db.zIncrSize(t, key, -num); err != nil {
  426. return 0, err
  427. }
  428. return num, nil
  429. }
  430. func (db *DB) zRange(key []byte, min int64, max int64, offset int, count int, reverse bool) ([]ScorePair, error) {
  431. if len(key) > MaxKeySize {
  432. return nil, errKeySize
  433. }
  434. if offset < 0 {
  435. return []ScorePair{}, nil
  436. }
  437. nv := 64
  438. if count > 0 {
  439. nv = count
  440. }
  441. v := make([]ScorePair, 0, nv)
  442. var it *store.RangeLimitIterator
  443. //if reverse and offset is 0, count < 0, we may use forward iterator then reverse
  444. //because store iterator prev is slower than next
  445. if !reverse || (offset == 0 && count < 0) {
  446. it = db.zIterator(key, min, max, offset, count, false)
  447. } else {
  448. it = db.zIterator(key, min, max, offset, count, true)
  449. }
  450. for ; it.Valid(); it.Next() {
  451. _, m, s, err := db.zDecodeScoreKey(it.Key())
  452. //may be we will check key equal?
  453. if err != nil {
  454. continue
  455. }
  456. v = append(v, ScorePair{Member: m, Score: s})
  457. }
  458. it.Close()
  459. if reverse && (offset == 0 && count < 0) {
  460. for i, j := 0, len(v)-1; i < j; i, j = i+1, j-1 {
  461. v[i], v[j] = v[j], v[i]
  462. }
  463. }
  464. return v, nil
  465. }
  466. func (db *DB) zParseLimit(key []byte, start int, stop int) (offset int, count int, err error) {
  467. if start < 0 || stop < 0 {
  468. //refer redis implementation
  469. var size int64
  470. size, err = db.ZCard(key)
  471. if err != nil {
  472. return
  473. }
  474. llen := int(size)
  475. if start < 0 {
  476. start = llen + start
  477. }
  478. if stop < 0 {
  479. stop = llen + stop
  480. }
  481. if start < 0 {
  482. start = 0
  483. }
  484. if start >= llen {
  485. offset = -1
  486. return
  487. }
  488. }
  489. if start > stop {
  490. offset = -1
  491. return
  492. }
  493. offset = start
  494. count = (stop - start) + 1
  495. return
  496. }
  497. func (db *DB) ZClear(key []byte) (int64, error) {
  498. t := db.zsetBatch
  499. t.Lock()
  500. defer t.Unlock()
  501. rmCnt, err := db.zRemRange(t, key, MinScore, MaxScore, 0, -1)
  502. if err == nil {
  503. err = t.Commit()
  504. }
  505. return rmCnt, err
  506. }
  507. func (db *DB) ZMclear(keys ...[]byte) (int64, error) {
  508. t := db.zsetBatch
  509. t.Lock()
  510. defer t.Unlock()
  511. for _, key := range keys {
  512. if _, err := db.zRemRange(t, key, MinScore, MaxScore, 0, -1); err != nil {
  513. return 0, err
  514. }
  515. }
  516. err := t.Commit()
  517. return int64(len(keys)), err
  518. }
  519. func (db *DB) ZRange(key []byte, start int, stop int) ([]ScorePair, error) {
  520. return db.ZRangeGeneric(key, start, stop, false)
  521. }
  522. //min and max must be inclusive
  523. //if no limit, set offset = 0 and count = -1
  524. func (db *DB) ZRangeByScore(key []byte, min int64, max int64,
  525. offset int, count int) ([]ScorePair, error) {
  526. return db.ZRangeByScoreGeneric(key, min, max, offset, count, false)
  527. }
  528. func (db *DB) ZRank(key []byte, member []byte) (int64, error) {
  529. return db.zrank(key, member, false)
  530. }
  531. func (db *DB) ZRemRangeByRank(key []byte, start int, stop int) (int64, error) {
  532. offset, count, err := db.zParseLimit(key, start, stop)
  533. if err != nil {
  534. return 0, err
  535. }
  536. var rmCnt int64
  537. t := db.zsetBatch
  538. t.Lock()
  539. defer t.Unlock()
  540. rmCnt, err = db.zRemRange(t, key, MinScore, MaxScore, offset, count)
  541. if err == nil {
  542. err = t.Commit()
  543. }
  544. return rmCnt, err
  545. }
  546. //min and max must be inclusive
  547. func (db *DB) ZRemRangeByScore(key []byte, min int64, max int64) (int64, error) {
  548. t := db.zsetBatch
  549. t.Lock()
  550. defer t.Unlock()
  551. rmCnt, err := db.zRemRange(t, key, min, max, 0, -1)
  552. if err == nil {
  553. err = t.Commit()
  554. }
  555. return rmCnt, err
  556. }
  557. func (db *DB) ZRevRange(key []byte, start int, stop int) ([]ScorePair, error) {
  558. return db.ZRangeGeneric(key, start, stop, true)
  559. }
  560. func (db *DB) ZRevRank(key []byte, member []byte) (int64, error) {
  561. return db.zrank(key, member, true)
  562. }
  563. //min and max must be inclusive
  564. //if no limit, set offset = 0 and count = -1
  565. func (db *DB) ZRevRangeByScore(key []byte, min int64, max int64, offset int, count int) ([]ScorePair, error) {
  566. return db.ZRangeByScoreGeneric(key, min, max, offset, count, true)
  567. }
  568. func (db *DB) ZRangeGeneric(key []byte, start int, stop int, reverse bool) ([]ScorePair, error) {
  569. offset, count, err := db.zParseLimit(key, start, stop)
  570. if err != nil {
  571. return nil, err
  572. }
  573. return db.zRange(key, MinScore, MaxScore, offset, count, reverse)
  574. }
  575. //min and max must be inclusive
  576. //if no limit, set offset = 0 and count = -1
  577. func (db *DB) ZRangeByScoreGeneric(key []byte, min int64, max int64,
  578. offset int, count int, reverse bool) ([]ScorePair, error) {
  579. return db.zRange(key, min, max, offset, count, reverse)
  580. }
  581. func (db *DB) zFlush() (drop int64, err error) {
  582. t := db.zsetBatch
  583. t.Lock()
  584. defer t.Unlock()
  585. return db.flushType(t, ZSetType)
  586. }
  587. func (db *DB) ZExpire(key []byte, duration int64) (int64, error) {
  588. if duration <= 0 {
  589. return 0, errExpireValue
  590. }
  591. return db.zExpireAt(key, time.Now().Unix()+duration)
  592. }
  593. func (db *DB) ZExpireAt(key []byte, when int64) (int64, error) {
  594. if when <= time.Now().Unix() {
  595. return 0, errExpireValue
  596. }
  597. return db.zExpireAt(key, when)
  598. }
  599. func (db *DB) ZTTL(key []byte) (int64, error) {
  600. if err := checkKeySize(key); err != nil {
  601. return -1, err
  602. }
  603. return db.ttl(ZSetType, key)
  604. }
  605. func (db *DB) ZPersist(key []byte) (int64, error) {
  606. if err := checkKeySize(key); err != nil {
  607. return 0, err
  608. }
  609. t := db.zsetBatch
  610. t.Lock()
  611. defer t.Unlock()
  612. n, err := db.rmExpire(t, ZSetType, key)
  613. if err != nil {
  614. return 0, err
  615. }
  616. err = t.Commit()
  617. return n, err
  618. }
  619. func getAggregateFunc(aggregate byte) func(int64, int64) int64 {
  620. switch aggregate {
  621. case AggregateSum:
  622. return func(a int64, b int64) int64 {
  623. return a + b
  624. }
  625. case AggregateMax:
  626. return func(a int64, b int64) int64 {
  627. if a > b {
  628. return a
  629. }
  630. return b
  631. }
  632. case AggregateMin:
  633. return func(a int64, b int64) int64 {
  634. if a > b {
  635. return b
  636. }
  637. return a
  638. }
  639. }
  640. return nil
  641. }
  642. func (db *DB) ZUnionStore(destKey []byte, srcKeys [][]byte, weights []int64, aggregate byte) (int64, error) {
  643. var destMap = map[string]int64{}
  644. aggregateFunc := getAggregateFunc(aggregate)
  645. if aggregateFunc == nil {
  646. return 0, errInvalidAggregate
  647. }
  648. if len(srcKeys) < 1 {
  649. return 0, errInvalidSrcKeyNum
  650. }
  651. if weights != nil {
  652. if len(srcKeys) != len(weights) {
  653. return 0, errInvalidWeightNum
  654. }
  655. } else {
  656. weights = make([]int64, len(srcKeys))
  657. for i := 0; i < len(weights); i++ {
  658. weights[i] = 1
  659. }
  660. }
  661. for i, key := range srcKeys {
  662. scorePairs, err := db.ZRange(key, 0, -1)
  663. if err != nil {
  664. return 0, err
  665. }
  666. for _, pair := range scorePairs {
  667. if score, ok := destMap[String(pair.Member)]; !ok {
  668. destMap[String(pair.Member)] = pair.Score * weights[i]
  669. } else {
  670. destMap[String(pair.Member)] = aggregateFunc(score, pair.Score*weights[i])
  671. }
  672. }
  673. }
  674. t := db.zsetBatch
  675. t.Lock()
  676. defer t.Unlock()
  677. db.zDelete(t, destKey)
  678. for member, score := range destMap {
  679. if err := checkZSetKMSize(destKey, []byte(member)); err != nil {
  680. return 0, err
  681. }
  682. if _, err := db.zSetItem(t, destKey, score, []byte(member)); err != nil {
  683. return 0, err
  684. }
  685. }
  686. var num = int64(len(destMap))
  687. sk := db.zEncodeSizeKey(destKey)
  688. t.Put(sk, PutInt64(num))
  689. //todo add binlog
  690. if err := t.Commit(); err != nil {
  691. return 0, err
  692. }
  693. return num, nil
  694. }
  695. func (db *DB) ZInterStore(destKey []byte, srcKeys [][]byte, weights []int64, aggregate byte) (int64, error) {
  696. aggregateFunc := getAggregateFunc(aggregate)
  697. if aggregateFunc == nil {
  698. return 0, errInvalidAggregate
  699. }
  700. if len(srcKeys) < 1 {
  701. return 0, errInvalidSrcKeyNum
  702. }
  703. if weights != nil {
  704. if len(srcKeys) != len(weights) {
  705. return 0, errInvalidWeightNum
  706. }
  707. } else {
  708. weights = make([]int64, len(srcKeys))
  709. for i := 0; i < len(weights); i++ {
  710. weights[i] = 1
  711. }
  712. }
  713. var destMap = map[string]int64{}
  714. scorePairs, err := db.ZRange(srcKeys[0], 0, -1)
  715. if err != nil {
  716. return 0, err
  717. }
  718. for _, pair := range scorePairs {
  719. destMap[String(pair.Member)] = pair.Score * weights[0]
  720. }
  721. for i, key := range srcKeys[1:] {
  722. scorePairs, err := db.ZRange(key, 0, -1)
  723. if err != nil {
  724. return 0, err
  725. }
  726. tmpMap := map[string]int64{}
  727. for _, pair := range scorePairs {
  728. if score, ok := destMap[String(pair.Member)]; ok {
  729. tmpMap[String(pair.Member)] = aggregateFunc(score, pair.Score*weights[i+1])
  730. }
  731. }
  732. destMap = tmpMap
  733. }
  734. t := db.zsetBatch
  735. t.Lock()
  736. defer t.Unlock()
  737. db.zDelete(t, destKey)
  738. for member, score := range destMap {
  739. if err := checkZSetKMSize(destKey, []byte(member)); err != nil {
  740. return 0, err
  741. }
  742. if _, err := db.zSetItem(t, destKey, score, []byte(member)); err != nil {
  743. return 0, err
  744. }
  745. }
  746. var num int64 = int64(len(destMap))
  747. sk := db.zEncodeSizeKey(destKey)
  748. t.Put(sk, PutInt64(num))
  749. //todo add binlog
  750. if err := t.Commit(); err != nil {
  751. return 0, err
  752. }
  753. return num, nil
  754. }
  755. func (db *DB) ZScan(key []byte, count int, inclusive bool, match string) ([][]byte, error) {
  756. return db.scan(ZSizeType, key, count, inclusive, match)
  757. }