fork of https://github.com/oxigraph/rocksdb and https://github.com/facebook/rocksdb for nextgraph and oxigraph
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.
134 lines
4.5 KiB
134 lines
4.5 KiB
8 years ago
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
||
8 years ago
|
// This source code is licensed under both the GPLv2 (found in the
|
||
|
// COPYING file in the root directory) and Apache 2.0 License
|
||
|
// (found in the LICENSE.Apache file in the root directory).
|
||
8 years ago
|
//
|
||
|
// Log format information shared by reader and writer.
|
||
|
|
||
|
#pragma once
|
||
|
|
||
|
#ifndef ROCKSDB_LITE
|
||
|
|
||
8 years ago
|
#include <limits>
|
||
6 years ago
|
#include <memory>
|
||
8 years ago
|
#include <utility>
|
||
6 years ago
|
|
||
8 years ago
|
#include "rocksdb/options.h"
|
||
7 years ago
|
#include "rocksdb/slice.h"
|
||
8 years ago
|
#include "rocksdb/status.h"
|
||
|
#include "rocksdb/types.h"
|
||
|
|
||
5 years ago
|
namespace ROCKSDB_NAMESPACE {
|
||
8 years ago
|
namespace blob_db {
|
||
|
|
||
7 years ago
|
constexpr uint32_t kMagicNumber = 2395959; // 0x00248f37
|
||
|
constexpr uint32_t kVersion1 = 1;
|
||
7 years ago
|
constexpr uint64_t kNoExpiration = std::numeric_limits<uint64_t>::max();
|
||
|
|
||
7 years ago
|
using ExpirationRange = std::pair<uint64_t, uint64_t>;
|
||
8 years ago
|
|
||
7 years ago
|
// Format of blob log file header (30 bytes):
|
||
|
//
|
||
|
// +--------------+---------+---------+-------+-------------+-------------------+
|
||
|
// | magic number | version | cf id | flags | compression | expiration range |
|
||
|
// +--------------+---------+---------+-------+-------------+-------------------+
|
||
|
// | Fixed32 | Fixed32 | Fixed32 | char | char | Fixed64 Fixed64 |
|
||
|
// +--------------+---------+---------+-------+-------------+-------------------+
|
||
|
//
|
||
|
// List of flags:
|
||
|
// has_ttl: Whether the file contain TTL data.
|
||
|
//
|
||
|
// Expiration range in the header is a rough range based on
|
||
|
// blob_db_options.ttl_range_secs.
|
||
|
struct BlobLogHeader {
|
||
|
static constexpr size_t kSize = 30;
|
||
8 years ago
|
|
||
5 years ago
|
BlobLogHeader() = default;
|
||
|
BlobLogHeader(uint32_t _column_family_id, CompressionType _compression,
|
||
|
bool _has_ttl, const ExpirationRange& _expiration_range)
|
||
|
: column_family_id(_column_family_id),
|
||
|
compression(_compression),
|
||
|
has_ttl(_has_ttl),
|
||
|
expiration_range(_expiration_range) {}
|
||
|
|
||
7 years ago
|
uint32_t version = kVersion1;
|
||
7 years ago
|
uint32_t column_family_id = 0;
|
||
|
CompressionType compression = kNoCompression;
|
||
|
bool has_ttl = false;
|
||
5 years ago
|
ExpirationRange expiration_range;
|
||
8 years ago
|
|
||
7 years ago
|
void EncodeTo(std::string* dst);
|
||
8 years ago
|
|
||
7 years ago
|
Status DecodeFrom(Slice slice);
|
||
8 years ago
|
};
|
||
|
|
||
7 years ago
|
// Format of blob log file footer (32 bytes):
|
||
7 years ago
|
//
|
||
7 years ago
|
// +--------------+------------+-------------------+------------+
|
||
|
// | magic number | blob count | expiration range | footer CRC |
|
||
|
// +--------------+------------+-------------------+------------+
|
||
|
// | Fixed32 | Fixed64 | Fixed64 + Fixed64 | Fixed32 |
|
||
|
// +--------------+------------+-------------------+------------+
|
||
7 years ago
|
//
|
||
|
// The footer will be presented only when the blob file is properly closed.
|
||
|
//
|
||
|
// Unlike the same field in file header, expiration range in the footer is the
|
||
|
// range of smallest and largest expiration of the data in this file.
|
||
|
struct BlobLogFooter {
|
||
7 years ago
|
static constexpr size_t kSize = 32;
|
||
8 years ago
|
|
||
7 years ago
|
uint64_t blob_count = 0;
|
||
|
ExpirationRange expiration_range = std::make_pair(0, 0);
|
||
|
uint32_t crc = 0;
|
||
8 years ago
|
|
||
7 years ago
|
void EncodeTo(std::string* dst);
|
||
8 years ago
|
|
||
7 years ago
|
Status DecodeFrom(Slice slice);
|
||
8 years ago
|
};
|
||
|
|
||
7 years ago
|
// Blob record format (32 bytes header + key + value):
|
||
|
//
|
||
|
// +------------+--------------+------------+------------+----------+---------+-----------+
|
||
|
// | key length | value length | expiration | header CRC | blob CRC | key | value |
|
||
|
// +------------+--------------+------------+------------+----------+---------+-----------+
|
||
|
// | Fixed64 | Fixed64 | Fixed64 | Fixed32 | Fixed32 | key len | value len |
|
||
|
// +------------+--------------+------------+------------+----------+---------+-----------+
|
||
|
//
|
||
|
// If file has has_ttl = false, expiration field is always 0, and the blob
|
||
|
// doesn't has expiration.
|
||
|
//
|
||
|
// Also note that if compression is used, value is compressed value and value
|
||
|
// length is compressed value length.
|
||
|
//
|
||
|
// Header CRC is the checksum of (key_len + val_len + expiration), while
|
||
|
// blob CRC is the checksum of (key + value).
|
||
|
//
|
||
|
// We could use variable length encoding (Varint64) to save more space, but it
|
||
|
// make reader more complicated.
|
||
|
struct BlobLogRecord {
|
||
|
// header include fields up to blob CRC
|
||
|
static constexpr size_t kHeaderSize = 32;
|
||
|
|
||
7 years ago
|
uint64_t key_size = 0;
|
||
|
uint64_t value_size = 0;
|
||
|
uint64_t expiration = 0;
|
||
|
uint32_t header_crc = 0;
|
||
|
uint32_t blob_crc = 0;
|
||
7 years ago
|
Slice key;
|
||
|
Slice value;
|
||
6 years ago
|
std::unique_ptr<char[]> key_buf;
|
||
|
std::unique_ptr<char[]> value_buf;
|
||
7 years ago
|
|
||
7 years ago
|
uint64_t record_size() const { return kHeaderSize + key_size + value_size; }
|
||
|
|
||
7 years ago
|
void EncodeHeaderTo(std::string* dst);
|
||
|
|
||
|
Status DecodeHeaderFrom(Slice src);
|
||
|
|
||
|
Status CheckBlobCRC() const;
|
||
8 years ago
|
};
|
||
|
|
||
|
} // namespace blob_db
|
||
5 years ago
|
} // namespace ROCKSDB_NAMESPACE
|
||
8 years ago
|
#endif // ROCKSDB_LITE
|