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

77 lines
2.1KB

  1. // Copyright (c) 2014 Couchbase, 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. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package upsidedown
  15. import (
  16. "encoding/binary"
  17. )
  18. var mergeOperator upsideDownMerge
  19. var dictionaryTermIncr []byte
  20. var dictionaryTermDecr []byte
  21. func init() {
  22. dictionaryTermIncr = make([]byte, 8)
  23. binary.LittleEndian.PutUint64(dictionaryTermIncr, uint64(1))
  24. dictionaryTermDecr = make([]byte, 8)
  25. var negOne = int64(-1)
  26. binary.LittleEndian.PutUint64(dictionaryTermDecr, uint64(negOne))
  27. }
  28. type upsideDownMerge struct{}
  29. func (m *upsideDownMerge) FullMerge(key, existingValue []byte, operands [][]byte) ([]byte, bool) {
  30. // set up record based on key
  31. dr, err := NewDictionaryRowK(key)
  32. if err != nil {
  33. return nil, false
  34. }
  35. if len(existingValue) > 0 {
  36. // if existing value, parse it
  37. err = dr.parseDictionaryV(existingValue)
  38. if err != nil {
  39. return nil, false
  40. }
  41. }
  42. // now process operands
  43. for _, operand := range operands {
  44. next := int64(binary.LittleEndian.Uint64(operand))
  45. if next < 0 && uint64(-next) > dr.count {
  46. // subtracting next from existing would overflow
  47. dr.count = 0
  48. } else if next < 0 {
  49. dr.count -= uint64(-next)
  50. } else {
  51. dr.count += uint64(next)
  52. }
  53. }
  54. return dr.Value(), true
  55. }
  56. func (m *upsideDownMerge) PartialMerge(key, leftOperand, rightOperand []byte) ([]byte, bool) {
  57. left := int64(binary.LittleEndian.Uint64(leftOperand))
  58. right := int64(binary.LittleEndian.Uint64(rightOperand))
  59. rv := make([]byte, 8)
  60. binary.LittleEndian.PutUint64(rv, uint64(left+right))
  61. return rv, true
  62. }
  63. func (m *upsideDownMerge) Name() string {
  64. return "upsideDownMerge"
  65. }
上海开阖软件有限公司 沪ICP备12045867号-1