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.
|
|
|
|
//
|
2011-03-18 23:37:00 +01:00
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
|
2013-10-05 07:32:05 +02:00
|
|
|
#pragma once
|
2013-08-23 17:38:13 +02:00
|
|
|
#include "rocksdb/types.h"
|
|
|
|
#include "rocksdb/write_batch.h"
|
|
|
|
#include "rocksdb/db.h"
|
|
|
|
#include "rocksdb/options.h"
|
2015-11-06 16:29:10 +01:00
|
|
|
#include "util/autovector.h"
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
namespace rocksdb {
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2011-07-15 02:20:57 +02:00
|
|
|
class MemTable;
|
|
|
|
|
2014-01-28 20:05:04 +01:00
|
|
|
class ColumnFamilyMemTables {
|
|
|
|
public:
|
2014-02-06 01:02:48 +01:00
|
|
|
virtual ~ColumnFamilyMemTables() {}
|
|
|
|
virtual bool Seek(uint32_t column_family_id) = 0;
|
|
|
|
// returns true if the update to memtable should be ignored
|
|
|
|
// (useful when recovering from log whose updates have already
|
|
|
|
// been processed)
|
|
|
|
virtual uint64_t GetLogNumber() const = 0;
|
|
|
|
virtual MemTable* GetMemTable() const = 0;
|
2014-02-11 02:04:44 +01:00
|
|
|
virtual ColumnFamilyHandle* GetColumnFamilyHandle() = 0;
|
2014-09-11 03:46:09 +02:00
|
|
|
virtual void CheckMemtableFull() = 0;
|
2014-02-06 01:02:48 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class ColumnFamilyMemTablesDefault : public ColumnFamilyMemTables {
|
|
|
|
public:
|
2014-11-18 19:20:10 +01:00
|
|
|
explicit ColumnFamilyMemTablesDefault(MemTable* mem)
|
|
|
|
: ok_(false), mem_(mem) {}
|
2014-02-06 01:02:48 +01:00
|
|
|
|
|
|
|
bool Seek(uint32_t column_family_id) override {
|
|
|
|
ok_ = (column_family_id == 0);
|
|
|
|
return ok_;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t GetLogNumber() const override { return 0; }
|
|
|
|
|
|
|
|
MemTable* GetMemTable() const override {
|
|
|
|
assert(ok_);
|
|
|
|
return mem_;
|
|
|
|
}
|
|
|
|
|
2014-02-11 02:04:44 +01:00
|
|
|
ColumnFamilyHandle* GetColumnFamilyHandle() override { return nullptr; }
|
2014-02-06 01:02:48 +01:00
|
|
|
|
2014-09-11 03:46:09 +02:00
|
|
|
void CheckMemtableFull() override {}
|
|
|
|
|
2014-02-06 01:02:48 +01:00
|
|
|
private:
|
|
|
|
bool ok_;
|
|
|
|
MemTable* mem_;
|
2014-01-28 20:05:04 +01:00
|
|
|
};
|
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
// WriteBatchInternal provides static methods for manipulating a
|
|
|
|
// WriteBatch that we don't want in the public WriteBatch interface.
|
|
|
|
class WriteBatchInternal {
|
|
|
|
public:
|
2014-04-22 20:27:33 +02:00
|
|
|
// WriteBatch methods with column_family_id instead of ColumnFamilyHandle*
|
|
|
|
static void Put(WriteBatch* batch, uint32_t column_family_id,
|
|
|
|
const Slice& key, const Slice& value);
|
|
|
|
|
|
|
|
static void Put(WriteBatch* batch, uint32_t column_family_id,
|
|
|
|
const SliceParts& key, const SliceParts& value);
|
|
|
|
|
2014-07-10 18:31:42 +02:00
|
|
|
static void Delete(WriteBatch* batch, uint32_t column_family_id,
|
|
|
|
const SliceParts& key);
|
|
|
|
|
2014-04-22 20:27:33 +02:00
|
|
|
static void Delete(WriteBatch* batch, uint32_t column_family_id,
|
|
|
|
const Slice& key);
|
|
|
|
|
Support for SingleDelete()
Summary:
This patch fixes #7460559. It introduces SingleDelete as a new database
operation. This operation can be used to delete keys that were never
overwritten (no put following another put of the same key). If an overwritten
key is single deleted the behavior is undefined. Single deletion of a
non-existent key has no effect but multiple consecutive single deletions are
not allowed (see limitations).
In contrast to the conventional Delete() operation, the deletion entry is
removed along with the value when the two are lined up in a compaction. Note:
The semantics are similar to @igor's prototype that allowed to have this
behavior on the granularity of a column family (
https://reviews.facebook.net/D42093 ). This new patch, however, is more
aggressive when it comes to removing tombstones: It removes the SingleDelete
together with the value whenever there is no snapshot between them while the
older patch only did this when the sequence number of the deletion was older
than the earliest snapshot.
Most of the complex additions are in the Compaction Iterator, all other changes
should be relatively straightforward. The patch also includes basic support for
single deletions in db_stress and db_bench.
Limitations:
- Not compatible with cuckoo hash tables
- Single deletions cannot be used in combination with merges and normal
deletions on the same key (other keys are not affected by this)
- Consecutive single deletions are currently not allowed (and older version of
this patch supported this so it could be resurrected if needed)
Test Plan: make all check
Reviewers: yhchiang, sdong, rven, anthony, yoshinorim, igor
Reviewed By: igor
Subscribers: maykov, dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D43179
2015-09-17 20:42:56 +02:00
|
|
|
static void SingleDelete(WriteBatch* batch, uint32_t column_family_id,
|
|
|
|
const SliceParts& key);
|
|
|
|
|
|
|
|
static void SingleDelete(WriteBatch* batch, uint32_t column_family_id,
|
|
|
|
const Slice& key);
|
|
|
|
|
2014-04-22 20:27:33 +02:00
|
|
|
static void Merge(WriteBatch* batch, uint32_t column_family_id,
|
|
|
|
const Slice& key, const Slice& value);
|
|
|
|
|
2015-05-28 01:59:22 +02:00
|
|
|
static void Merge(WriteBatch* batch, uint32_t column_family_id,
|
|
|
|
const SliceParts& key, const SliceParts& value);
|
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
// Return the number of entries in the batch.
|
|
|
|
static int Count(const WriteBatch* batch);
|
|
|
|
|
|
|
|
// Set the count for the number of entries in the batch.
|
|
|
|
static void SetCount(WriteBatch* batch, int n);
|
|
|
|
|
|
|
|
// Return the seqeunce number for the start of this batch.
|
|
|
|
static SequenceNumber Sequence(const WriteBatch* batch);
|
|
|
|
|
|
|
|
// Store the specified number as the seqeunce number for the start of
|
|
|
|
// this batch.
|
|
|
|
static void SetSequence(WriteBatch* batch, SequenceNumber seq);
|
|
|
|
|
2015-07-11 05:15:45 +02:00
|
|
|
// Returns the offset of the first entry in the batch.
|
|
|
|
// This offset is only valid if the batch is not empty.
|
|
|
|
static size_t GetFirstOffset(WriteBatch* batch);
|
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
static Slice Contents(const WriteBatch* batch) {
|
|
|
|
return Slice(batch->rep_);
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t ByteSize(const WriteBatch* batch) {
|
|
|
|
return batch->rep_.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SetContents(WriteBatch* batch, const Slice& contents);
|
|
|
|
|
2015-11-06 16:29:10 +01:00
|
|
|
// Inserts batches[i] into memtable, for i in 0..num_batches-1 inclusive.
|
|
|
|
//
|
|
|
|
// If dont_filter_deletes is false AND options.filter_deletes is true
|
|
|
|
// AND db->KeyMayExist is false, then a Delete won't modify the memtable.
|
|
|
|
//
|
|
|
|
// If ignore_missing_column_families == true. WriteBatch
|
|
|
|
// referencing non-existing column family will be ignored.
|
|
|
|
// If ignore_missing_column_families == false, processing of the
|
|
|
|
// batches will be stopped if a reference is found to a non-existing
|
|
|
|
// column family and InvalidArgument() will be returned. The writes
|
|
|
|
// in batches may be only partially applied at that point.
|
2014-09-02 22:29:05 +02:00
|
|
|
//
|
|
|
|
// If log_number is non-zero, the memtable will be updated only if
|
2015-11-06 16:29:10 +01:00
|
|
|
// memtables->GetLogNumber() >= log_number.
|
|
|
|
static Status InsertInto(const autovector<WriteBatch*>& batches,
|
|
|
|
SequenceNumber sequence,
|
|
|
|
ColumnFamilyMemTables* memtables,
|
|
|
|
bool ignore_missing_column_families = false,
|
|
|
|
uint64_t log_number = 0, DB* db = nullptr,
|
|
|
|
const bool dont_filter_deletes = true);
|
|
|
|
|
|
|
|
// Convenience form of InsertInto when you have only one batch
|
2014-01-28 20:05:04 +01:00
|
|
|
static Status InsertInto(const WriteBatch* batch,
|
|
|
|
ColumnFamilyMemTables* memtables,
|
2014-09-02 22:29:05 +02:00
|
|
|
bool ignore_missing_column_families = false,
|
|
|
|
uint64_t log_number = 0, DB* db = nullptr,
|
2014-02-06 01:02:48 +01:00
|
|
|
const bool dont_filter_deletes = true);
|
2014-01-28 20:05:04 +01:00
|
|
|
|
2012-03-09 01:23:21 +01:00
|
|
|
static void Append(WriteBatch* dst, const WriteBatch* src);
|
2015-11-06 16:29:10 +01:00
|
|
|
|
|
|
|
// Returns the byte size of appending a WriteBatch with ByteSize
|
|
|
|
// leftByteSize and a WriteBatch with ByteSize rightByteSize
|
|
|
|
static size_t AppendedByteSize(size_t leftByteSize, size_t rightByteSize);
|
2011-03-18 23:37:00 +01:00
|
|
|
};
|
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
} // namespace rocksdb
|