2013-10-16 23:59:46 +02:00
|
|
|
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under the BSD-style license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
//
|
2013-03-21 23:59:47 +01:00
|
|
|
#ifndef MERGE_HELPER_H
|
|
|
|
#define MERGE_HELPER_H
|
|
|
|
|
[RocksDB] [MergeOperator] The new Merge Interface! Uses merge sequences.
Summary:
Here are the major changes to the Merge Interface. It has been expanded
to handle cases where the MergeOperator is not associative. It does so by stacking
up merge operations while scanning through the key history (i.e.: during Get() or
Compaction), until a valid Put/Delete/end-of-history is encountered; it then
applies all of the merge operations in the correct sequence starting with the
base/sentinel value.
I have also introduced an "AssociativeMerge" function which allows the user to
take advantage of associative merge operations (such as in the case of counters).
The implementation will always attempt to merge the operations/operands themselves
together when they are encountered, and will resort to the "stacking" method if
and only if the "associative-merge" fails.
This implementation is conjectured to allow MergeOperator to handle the general
case, while still providing the user with the ability to take advantage of certain
efficiencies in their own merge-operator / data-structure.
NOTE: This is a preliminary diff. This must still go through a lot of review,
revision, and testing. Feedback welcome!
Test Plan:
-This is a preliminary diff. I have only just begun testing/debugging it.
-I will be testing this with the existing MergeOperator use-cases and unit-tests
(counters, string-append, and redis-lists)
-I will be "desk-checking" and walking through the code with the help gdb.
-I will find a way of stress-testing the new interface / implementation using
db_bench, db_test, merge_test, and/or db_stress.
-I will ensure that my tests cover all cases: Get-Memtable,
Get-Immutable-Memtable, Get-from-Disk, Iterator-Range-Scan, Flush-Memtable-to-L0,
Compaction-L0-L1, Compaction-Ln-L(n+1), Put/Delete found, Put/Delete not-found,
end-of-history, end-of-file, etc.
-A lot of feedback from the reviewers.
Reviewers: haobo, dhruba, zshao, emayanke
Reviewed By: haobo
CC: leveldb
Differential Revision: https://reviews.facebook.net/D11499
2013-08-06 05:14:32 +02:00
|
|
|
#include <deque>
|
2015-07-29 04:21:55 +02:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "db/dbformat.h"
|
2015-03-03 19:59:36 +01:00
|
|
|
#include "rocksdb/env.h"
|
2015-07-29 04:21:55 +02:00
|
|
|
#include "rocksdb/slice.h"
|
2013-03-21 23:59:47 +01:00
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
namespace rocksdb {
|
2013-03-21 23:59:47 +01:00
|
|
|
|
|
|
|
class Comparator;
|
|
|
|
class Iterator;
|
|
|
|
class Logger;
|
|
|
|
class MergeOperator;
|
2013-11-22 23:14:05 +01:00
|
|
|
class Statistics;
|
2013-03-21 23:59:47 +01:00
|
|
|
|
|
|
|
class MergeHelper {
|
|
|
|
public:
|
|
|
|
MergeHelper(const Comparator* user_comparator,
|
2014-03-25 01:57:13 +01:00
|
|
|
const MergeOperator* user_merge_operator, Logger* logger,
|
|
|
|
unsigned min_partial_merge_operands,
|
2013-03-21 23:59:47 +01:00
|
|
|
bool assert_valid_internal_key)
|
|
|
|
: user_comparator_(user_comparator),
|
|
|
|
user_merge_operator_(user_merge_operator),
|
|
|
|
logger_(logger),
|
2014-03-25 01:57:13 +01:00
|
|
|
min_partial_merge_operands_(min_partial_merge_operands),
|
[RocksDB] [MergeOperator] The new Merge Interface! Uses merge sequences.
Summary:
Here are the major changes to the Merge Interface. It has been expanded
to handle cases where the MergeOperator is not associative. It does so by stacking
up merge operations while scanning through the key history (i.e.: during Get() or
Compaction), until a valid Put/Delete/end-of-history is encountered; it then
applies all of the merge operations in the correct sequence starting with the
base/sentinel value.
I have also introduced an "AssociativeMerge" function which allows the user to
take advantage of associative merge operations (such as in the case of counters).
The implementation will always attempt to merge the operations/operands themselves
together when they are encountered, and will resort to the "stacking" method if
and only if the "associative-merge" fails.
This implementation is conjectured to allow MergeOperator to handle the general
case, while still providing the user with the ability to take advantage of certain
efficiencies in their own merge-operator / data-structure.
NOTE: This is a preliminary diff. This must still go through a lot of review,
revision, and testing. Feedback welcome!
Test Plan:
-This is a preliminary diff. I have only just begun testing/debugging it.
-I will be testing this with the existing MergeOperator use-cases and unit-tests
(counters, string-append, and redis-lists)
-I will be "desk-checking" and walking through the code with the help gdb.
-I will find a way of stress-testing the new interface / implementation using
db_bench, db_test, merge_test, and/or db_stress.
-I will ensure that my tests cover all cases: Get-Memtable,
Get-Immutable-Memtable, Get-from-Disk, Iterator-Range-Scan, Flush-Memtable-to-L0,
Compaction-L0-L1, Compaction-Ln-L(n+1), Put/Delete found, Put/Delete not-found,
end-of-history, end-of-file, etc.
-A lot of feedback from the reviewers.
Reviewers: haobo, dhruba, zshao, emayanke
Reviewed By: haobo
CC: leveldb
Differential Revision: https://reviews.facebook.net/D11499
2013-08-06 05:14:32 +02:00
|
|
|
assert_valid_internal_key_(assert_valid_internal_key),
|
|
|
|
keys_(),
|
Simplify querying of merge results
Summary:
While working on supporting mixing merge operators with
single deletes ( https://reviews.facebook.net/D43179 ),
I realized that returning and dealing with merge results
can be made simpler. Submitting this as a separate diff
because it is not directly related to single deletes.
Before, callers of merge helper had to retrieve the merge
result in one of two ways depending on whether the merge
was successful or not (success = result of merge was single
kTypeValue). For successful merges, the caller could query
the resulting key/value pair and for unsuccessful merges,
the result could be retrieved in the form of two deques of
keys and values. However, with single deletes, a successful merge
does not return a single key/value pair (if merge
operands are merged with a single delete, we have to generate
a value and keep the original single delete around to make
sure that we are not accidentially producing a key overwrite).
In addition, the two existing call sites of the merge
helper were taking the same actions independently from whether
the merge was successful or not, so this patch simplifies that.
Test Plan: make clean all check
Reviewers: rven, sdong, yhchiang, anthony, igor
Reviewed By: igor
Subscribers: dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D43353
2015-08-18 02:34:38 +02:00
|
|
|
operands_() {
|
|
|
|
assert(user_comparator_ != nullptr);
|
|
|
|
}
|
2013-03-21 23:59:47 +01:00
|
|
|
|
2015-04-28 05:23:50 +02:00
|
|
|
// Wrapper around MergeOperator::FullMerge() that records perf statistics.
|
|
|
|
// Result of merge will be written to result if status returned is OK.
|
|
|
|
// If operands is empty, the value will simply be copied to result.
|
Simplify querying of merge results
Summary:
While working on supporting mixing merge operators with
single deletes ( https://reviews.facebook.net/D43179 ),
I realized that returning and dealing with merge results
can be made simpler. Submitting this as a separate diff
because it is not directly related to single deletes.
Before, callers of merge helper had to retrieve the merge
result in one of two ways depending on whether the merge
was successful or not (success = result of merge was single
kTypeValue). For successful merges, the caller could query
the resulting key/value pair and for unsuccessful merges,
the result could be retrieved in the form of two deques of
keys and values. However, with single deletes, a successful merge
does not return a single key/value pair (if merge
operands are merged with a single delete, we have to generate
a value and keep the original single delete around to make
sure that we are not accidentially producing a key overwrite).
In addition, the two existing call sites of the merge
helper were taking the same actions independently from whether
the merge was successful or not, so this patch simplifies that.
Test Plan: make clean all check
Reviewers: rven, sdong, yhchiang, anthony, igor
Reviewed By: igor
Subscribers: dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D43353
2015-08-18 02:34:38 +02:00
|
|
|
// Returns one of the following statuses:
|
|
|
|
// - OK: Entries were successfully merged.
|
|
|
|
// - Corruption: Merge operator reported unsuccessful merge.
|
|
|
|
// - NotSupported: Merge operator is missing.
|
2015-04-28 05:23:50 +02:00
|
|
|
static Status TimedFullMerge(const Slice& key, const Slice* value,
|
|
|
|
const std::deque<std::string>& operands,
|
|
|
|
const MergeOperator* merge_operator,
|
|
|
|
Statistics* statistics, Env* env, Logger* logger,
|
|
|
|
std::string* result);
|
|
|
|
|
2013-03-21 23:59:47 +01:00
|
|
|
// Merge entries until we hit
|
|
|
|
// - a corrupted key
|
|
|
|
// - a Put/Delete,
|
|
|
|
// - a different user key,
|
|
|
|
// - a specific sequence number (snapshot boundary),
|
|
|
|
// or - the end of iteration
|
|
|
|
// iter: (IN) points to the first merge type entry
|
|
|
|
// (OUT) points to the first entry not included in the merge process
|
|
|
|
// stop_before: (IN) a sequence number that merge should not cross.
|
|
|
|
// 0 means no restriction
|
|
|
|
// at_bottom: (IN) true if the iterator covers the bottem level, which means
|
|
|
|
// we could reach the start of the history of this user key.
|
Simplify querying of merge results
Summary:
While working on supporting mixing merge operators with
single deletes ( https://reviews.facebook.net/D43179 ),
I realized that returning and dealing with merge results
can be made simpler. Submitting this as a separate diff
because it is not directly related to single deletes.
Before, callers of merge helper had to retrieve the merge
result in one of two ways depending on whether the merge
was successful or not (success = result of merge was single
kTypeValue). For successful merges, the caller could query
the resulting key/value pair and for unsuccessful merges,
the result could be retrieved in the form of two deques of
keys and values. However, with single deletes, a successful merge
does not return a single key/value pair (if merge
operands are merged with a single delete, we have to generate
a value and keep the original single delete around to make
sure that we are not accidentially producing a key overwrite).
In addition, the two existing call sites of the merge
helper were taking the same actions independently from whether
the merge was successful or not, so this patch simplifies that.
Test Plan: make clean all check
Reviewers: rven, sdong, yhchiang, anthony, igor
Reviewed By: igor
Subscribers: dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D43353
2015-08-18 02:34:38 +02:00
|
|
|
// Returns one of the following statuses:
|
|
|
|
// - OK: Entries were successfully merged.
|
|
|
|
// - MergeInProgress: Put/Delete not encountered and unable to merge operands.
|
|
|
|
// - Corruption: Merge operator reported unsuccessful merge.
|
|
|
|
//
|
|
|
|
// REQUIRED: The first key in the input is not corrupted.
|
|
|
|
Status MergeUntil(Iterator* iter, const SequenceNumber stop_before = 0,
|
|
|
|
const bool at_bottom = false, Statistics* stats = nullptr,
|
|
|
|
Env* env_ = nullptr);
|
2013-03-21 23:59:47 +01:00
|
|
|
|
|
|
|
// Query the merge result
|
[RocksDB] [MergeOperator] The new Merge Interface! Uses merge sequences.
Summary:
Here are the major changes to the Merge Interface. It has been expanded
to handle cases where the MergeOperator is not associative. It does so by stacking
up merge operations while scanning through the key history (i.e.: during Get() or
Compaction), until a valid Put/Delete/end-of-history is encountered; it then
applies all of the merge operations in the correct sequence starting with the
base/sentinel value.
I have also introduced an "AssociativeMerge" function which allows the user to
take advantage of associative merge operations (such as in the case of counters).
The implementation will always attempt to merge the operations/operands themselves
together when they are encountered, and will resort to the "stacking" method if
and only if the "associative-merge" fails.
This implementation is conjectured to allow MergeOperator to handle the general
case, while still providing the user with the ability to take advantage of certain
efficiencies in their own merge-operator / data-structure.
NOTE: This is a preliminary diff. This must still go through a lot of review,
revision, and testing. Feedback welcome!
Test Plan:
-This is a preliminary diff. I have only just begun testing/debugging it.
-I will be testing this with the existing MergeOperator use-cases and unit-tests
(counters, string-append, and redis-lists)
-I will be "desk-checking" and walking through the code with the help gdb.
-I will find a way of stress-testing the new interface / implementation using
db_bench, db_test, merge_test, and/or db_stress.
-I will ensure that my tests cover all cases: Get-Memtable,
Get-Immutable-Memtable, Get-from-Disk, Iterator-Range-Scan, Flush-Memtable-to-L0,
Compaction-L0-L1, Compaction-Ln-L(n+1), Put/Delete found, Put/Delete not-found,
end-of-history, end-of-file, etc.
-A lot of feedback from the reviewers.
Reviewers: haobo, dhruba, zshao, emayanke
Reviewed By: haobo
CC: leveldb
Differential Revision: https://reviews.facebook.net/D11499
2013-08-06 05:14:32 +02:00
|
|
|
// These are valid until the next MergeUntil call
|
|
|
|
// If the merging was successful:
|
Simplify querying of merge results
Summary:
While working on supporting mixing merge operators with
single deletes ( https://reviews.facebook.net/D43179 ),
I realized that returning and dealing with merge results
can be made simpler. Submitting this as a separate diff
because it is not directly related to single deletes.
Before, callers of merge helper had to retrieve the merge
result in one of two ways depending on whether the merge
was successful or not (success = result of merge was single
kTypeValue). For successful merges, the caller could query
the resulting key/value pair and for unsuccessful merges,
the result could be retrieved in the form of two deques of
keys and values. However, with single deletes, a successful merge
does not return a single key/value pair (if merge
operands are merged with a single delete, we have to generate
a value and keep the original single delete around to make
sure that we are not accidentially producing a key overwrite).
In addition, the two existing call sites of the merge
helper were taking the same actions independently from whether
the merge was successful or not, so this patch simplifies that.
Test Plan: make clean all check
Reviewers: rven, sdong, yhchiang, anthony, igor
Reviewed By: igor
Subscribers: dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D43353
2015-08-18 02:34:38 +02:00
|
|
|
// - keys() contains a single element with the latest sequence number of
|
|
|
|
// the merges. The type will be Put or Merge. See IMPORTANT 1 note, below.
|
|
|
|
// - values() contains a single element with the result of merging all the
|
|
|
|
// operands together
|
[RocksDB] [MergeOperator] The new Merge Interface! Uses merge sequences.
Summary:
Here are the major changes to the Merge Interface. It has been expanded
to handle cases where the MergeOperator is not associative. It does so by stacking
up merge operations while scanning through the key history (i.e.: during Get() or
Compaction), until a valid Put/Delete/end-of-history is encountered; it then
applies all of the merge operations in the correct sequence starting with the
base/sentinel value.
I have also introduced an "AssociativeMerge" function which allows the user to
take advantage of associative merge operations (such as in the case of counters).
The implementation will always attempt to merge the operations/operands themselves
together when they are encountered, and will resort to the "stacking" method if
and only if the "associative-merge" fails.
This implementation is conjectured to allow MergeOperator to handle the general
case, while still providing the user with the ability to take advantage of certain
efficiencies in their own merge-operator / data-structure.
NOTE: This is a preliminary diff. This must still go through a lot of review,
revision, and testing. Feedback welcome!
Test Plan:
-This is a preliminary diff. I have only just begun testing/debugging it.
-I will be testing this with the existing MergeOperator use-cases and unit-tests
(counters, string-append, and redis-lists)
-I will be "desk-checking" and walking through the code with the help gdb.
-I will find a way of stress-testing the new interface / implementation using
db_bench, db_test, merge_test, and/or db_stress.
-I will ensure that my tests cover all cases: Get-Memtable,
Get-Immutable-Memtable, Get-from-Disk, Iterator-Range-Scan, Flush-Memtable-to-L0,
Compaction-L0-L1, Compaction-Ln-L(n+1), Put/Delete found, Put/Delete not-found,
end-of-history, end-of-file, etc.
-A lot of feedback from the reviewers.
Reviewers: haobo, dhruba, zshao, emayanke
Reviewed By: haobo
CC: leveldb
Differential Revision: https://reviews.facebook.net/D11499
2013-08-06 05:14:32 +02:00
|
|
|
//
|
|
|
|
// IMPORTANT 1: the key type could change after the MergeUntil call.
|
|
|
|
// Put/Delete + Merge + ... + Merge => Put
|
|
|
|
// Merge + ... + Merge => Merge
|
|
|
|
//
|
|
|
|
// If the merge operator is not associative, and if a Put/Delete is not found
|
|
|
|
// then the merging will be unsuccessful. In this case:
|
|
|
|
// - keys() contains the list of internal keys seen in order of iteration.
|
|
|
|
// - values() contains the list of values (merges) seen in the same order.
|
|
|
|
// values() is parallel to keys() so that the first entry in
|
|
|
|
// keys() is the key associated with the first entry in values()
|
|
|
|
// and so on. These lists will be the same length.
|
|
|
|
// All of these pairs will be merges over the same user key.
|
|
|
|
// See IMPORTANT 2 note below.
|
|
|
|
//
|
|
|
|
// IMPORTANT 2: The entries were traversed in order from BACK to FRONT.
|
|
|
|
// So keys().back() was the first key seen by iterator.
|
|
|
|
// TODO: Re-style this comment to be like the first one
|
Simplify querying of merge results
Summary:
While working on supporting mixing merge operators with
single deletes ( https://reviews.facebook.net/D43179 ),
I realized that returning and dealing with merge results
can be made simpler. Submitting this as a separate diff
because it is not directly related to single deletes.
Before, callers of merge helper had to retrieve the merge
result in one of two ways depending on whether the merge
was successful or not (success = result of merge was single
kTypeValue). For successful merges, the caller could query
the resulting key/value pair and for unsuccessful merges,
the result could be retrieved in the form of two deques of
keys and values. However, with single deletes, a successful merge
does not return a single key/value pair (if merge
operands are merged with a single delete, we have to generate
a value and keep the original single delete around to make
sure that we are not accidentially producing a key overwrite).
In addition, the two existing call sites of the merge
helper were taking the same actions independently from whether
the merge was successful or not, so this patch simplifies that.
Test Plan: make clean all check
Reviewers: rven, sdong, yhchiang, anthony, igor
Reviewed By: igor
Subscribers: dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D43353
2015-08-18 02:34:38 +02:00
|
|
|
const std::deque<std::string>& keys() const { return keys_; }
|
|
|
|
const std::deque<std::string>& values() const { return operands_; }
|
2014-07-31 02:24:36 +02:00
|
|
|
bool HasOperator() const { return user_merge_operator_ != nullptr; }
|
2013-03-21 23:59:47 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
const Comparator* user_comparator_;
|
|
|
|
const MergeOperator* user_merge_operator_;
|
|
|
|
Logger* logger_;
|
2014-03-25 01:57:13 +01:00
|
|
|
unsigned min_partial_merge_operands_;
|
2013-03-21 23:59:47 +01:00
|
|
|
bool assert_valid_internal_key_; // enforce no internal key corruption?
|
|
|
|
|
|
|
|
// the scratch area that holds the result of MergeUntil
|
|
|
|
// valid up to the next MergeUntil call
|
[RocksDB] [MergeOperator] The new Merge Interface! Uses merge sequences.
Summary:
Here are the major changes to the Merge Interface. It has been expanded
to handle cases where the MergeOperator is not associative. It does so by stacking
up merge operations while scanning through the key history (i.e.: during Get() or
Compaction), until a valid Put/Delete/end-of-history is encountered; it then
applies all of the merge operations in the correct sequence starting with the
base/sentinel value.
I have also introduced an "AssociativeMerge" function which allows the user to
take advantage of associative merge operations (such as in the case of counters).
The implementation will always attempt to merge the operations/operands themselves
together when they are encountered, and will resort to the "stacking" method if
and only if the "associative-merge" fails.
This implementation is conjectured to allow MergeOperator to handle the general
case, while still providing the user with the ability to take advantage of certain
efficiencies in their own merge-operator / data-structure.
NOTE: This is a preliminary diff. This must still go through a lot of review,
revision, and testing. Feedback welcome!
Test Plan:
-This is a preliminary diff. I have only just begun testing/debugging it.
-I will be testing this with the existing MergeOperator use-cases and unit-tests
(counters, string-append, and redis-lists)
-I will be "desk-checking" and walking through the code with the help gdb.
-I will find a way of stress-testing the new interface / implementation using
db_bench, db_test, merge_test, and/or db_stress.
-I will ensure that my tests cover all cases: Get-Memtable,
Get-Immutable-Memtable, Get-from-Disk, Iterator-Range-Scan, Flush-Memtable-to-L0,
Compaction-L0-L1, Compaction-Ln-L(n+1), Put/Delete found, Put/Delete not-found,
end-of-history, end-of-file, etc.
-A lot of feedback from the reviewers.
Reviewers: haobo, dhruba, zshao, emayanke
Reviewed By: haobo
CC: leveldb
Differential Revision: https://reviews.facebook.net/D11499
2013-08-06 05:14:32 +02:00
|
|
|
std::deque<std::string> keys_; // Keeps track of the sequence of keys seen
|
|
|
|
std::deque<std::string> operands_; // Parallel with keys_; stores the values
|
2013-03-21 23:59:47 +01:00
|
|
|
};
|
|
|
|
|
2015-09-10 23:35:25 +02:00
|
|
|
// MergeOutputIterator can be used to iterate over the result of a merge.
|
|
|
|
class MergeOutputIterator {
|
|
|
|
public:
|
|
|
|
// The MergeOutputIterator is bound to a MergeHelper instance.
|
|
|
|
explicit MergeOutputIterator(const MergeHelper* merge_helper);
|
|
|
|
|
|
|
|
// Seeks to the first record in the output.
|
|
|
|
void SeekToFirst();
|
|
|
|
// Advances to the next record in the output.
|
|
|
|
void Next();
|
|
|
|
|
|
|
|
Slice key() { return Slice(*it_keys_); }
|
|
|
|
Slice value() { return Slice(*it_values_); }
|
|
|
|
bool Valid() { return it_keys_ != merge_helper_->keys().rend(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
const MergeHelper* merge_helper_;
|
|
|
|
std::deque<std::string>::const_reverse_iterator it_keys_;
|
|
|
|
std::deque<std::string>::const_reverse_iterator it_values_;
|
|
|
|
};
|
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
} // namespace rocksdb
|
2013-03-21 23:59:47 +01:00
|
|
|
|
|
|
|
#endif
|