Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

row_merge.go 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. }