github.com/petermattis/pebble@v0.0.0-20190905164901-ab51a2166067/internal/base/merger.go (about)

     1  // Copyright 2018 The LevelDB-Go and Pebble Authors. All rights reserved. Use
     2  // of this source code is governed by a BSD-style license that can be found in
     3  // the LICENSE file.
     4  
     5  package base
     6  
     7  // Merge merges oldValue and newValue, and returns the merged value. The buf
     8  // parameter can be used to store the newly merged value in order to avoid
     9  // memory allocations. The merge operation must be associative. That is, for
    10  // the values A, B, C:
    11  //
    12  //   Merge(A, Merge(B, C)) == Merge(Merge(A, B), C)
    13  //
    14  // Examples of merge operators are integer addition, list append, and string
    15  // concatenation.
    16  //
    17  // Note that during forward scans merges are processed from newest to oldest
    18  // value, and in reverse scans merges are processed from oldest to newest
    19  // value. DO NOT rely on this ordering: compactions will create partial merge
    20  // results that may not show up in simple tests.
    21  type Merge func(key, oldValue, newValue, buf []byte) []byte
    22  
    23  // Merger defines an associative merge operation. The merge operation merges
    24  // two or more values for a single key. A merge operation is requested by
    25  // writing a value using {Batch,DB}.Merge(). The value at that key is merged
    26  // with any existing value. It is valid to Set a value at a key and then Merge
    27  // a new value. Similar to non-merged values, a merged value can be deleted by
    28  // either Delete or DeleteRange.
    29  //
    30  // The merge operation is invoked when a merge value is encountered during a
    31  // read, either during a compaction or during iteration.
    32  type Merger struct {
    33  	Merge Merge
    34  
    35  	// Name is the name of the merger.
    36  	//
    37  	// Pebble stores the merger name on disk, and opening a database with a
    38  	// different merger from the one it was created with will result in an error.
    39  	Name string
    40  }
    41  
    42  // DefaultMerger is the default implementation of the Merger interface. It
    43  // concatenates the two values to merge.
    44  var DefaultMerger = &Merger{
    45  	Merge: func(key, oldValue, newValue, buf []byte) []byte {
    46  		return append(append(buf, oldValue...), newValue...)
    47  	},
    48  
    49  	Name: "pebble.concatenate",
    50  }