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

65 lines
1.9KB

  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 store
  15. // At the moment this happens to be the same interface as described by
  16. // RocksDB, but this may not always be the case.
  17. type MergeOperator interface {
  18. // FullMerge the full sequence of operands on top of the existingValue
  19. // if no value currently exists, existingValue is nil
  20. // return the merged value, and success/failure
  21. FullMerge(key, existingValue []byte, operands [][]byte) ([]byte, bool)
  22. // Partially merge these two operands.
  23. // If partial merge cannot be done, return nil,false, which will defer
  24. // all processing until the FullMerge is done.
  25. PartialMerge(key, leftOperand, rightOperand []byte) ([]byte, bool)
  26. // Name returns an identifier for the operator
  27. Name() string
  28. }
  29. type EmulatedMerge struct {
  30. Merges map[string][][]byte
  31. mo MergeOperator
  32. }
  33. func NewEmulatedMerge(mo MergeOperator) *EmulatedMerge {
  34. return &EmulatedMerge{
  35. Merges: make(map[string][][]byte),
  36. mo: mo,
  37. }
  38. }
  39. func (m *EmulatedMerge) Merge(key, val []byte) {
  40. ops, ok := m.Merges[string(key)]
  41. if ok && len(ops) > 0 {
  42. last := ops[len(ops)-1]
  43. mergedVal, partialMergeOk := m.mo.PartialMerge(key, last, val)
  44. if partialMergeOk {
  45. // replace last entry with the result of the merge
  46. ops[len(ops)-1] = mergedVal
  47. } else {
  48. // could not partial merge, append this to the end
  49. ops = append(ops, val)
  50. }
  51. } else {
  52. ops = [][]byte{val}
  53. }
  54. m.Merges[string(key)] = ops
  55. }
上海开阖软件有限公司 沪ICP备12045867号-1