Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,7 @@ UI_JS := $(UI_ROOT)/src/js/protos.js
UI_TS := $(UI_ROOT)/src/js/protos.d.ts
UI_PROTOS := $(UI_JS) $(UI_TS)

CPP_PROTOS := $(filter %/roachpb/metadata.proto %/roachpb/data.proto %/roachpb/internal.proto %/engine/enginepb/mvcc.proto %/engine/enginepb/mvcc3.proto %/engine/enginepb/rocksdb.proto %/hlc/legacy_timestamp.proto %/hlc/timestamp.proto %/unresolved_addr.proto,$(GO_PROTOS))
CPP_PROTOS := $(filter %/roachpb/metadata.proto %/roachpb/data.proto %/roachpb/internal.proto %/engine/enginepb/mvcc.proto %/engine/enginepb/mvcc3.proto %/engine/enginepb/registry.proto %/engine/enginepb/rocksdb.proto %/hlc/legacy_timestamp.proto %/hlc/timestamp.proto %/unresolved_addr.proto,$(GO_PROTOS))
CPP_HEADERS := $(subst $(PKG_ROOT),$(CPP_PROTO_ROOT),$(CPP_PROTOS:%.proto=%.pb.h))
CPP_SOURCES := $(subst $(PKG_ROOT),$(CPP_PROTO_ROOT),$(CPP_PROTOS:%.proto=%.pb.cc))

Expand Down
2 changes: 2 additions & 0 deletions c-deps/libroach/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ project(roachlib)
add_library(roach
db.cc
encoding.cc
env_switching.cc
eventlistener.cc
protos/roachpb/data.pb.cc
protos/roachpb/internal.pb.cc
protos/roachpb/metadata.pb.cc
protos/storage/engine/enginepb/mvcc.pb.cc
protos/storage/engine/enginepb/mvcc3.pb.cc
protos/storage/engine/enginepb/registry.pb.cc
protos/storage/engine/enginepb/rocksdb.pb.cc
protos/util/hlc/legacy_timestamp.pb.cc
protos/util/hlc/timestamp.pb.cc
Expand Down
22 changes: 17 additions & 5 deletions c-deps/libroach/db.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "protos/storage/engine/enginepb/mvcc.pb.h"
#include "db.h"
#include "encoding.h"
#include "env_switching.h"
#include "eventlistener.h"
#include "keys.h"

Expand Down Expand Up @@ -89,17 +90,19 @@ struct DBEngine {
};

struct DBImpl : public DBEngine {
std::unique_ptr<rocksdb::Env> switching_env;
std::unique_ptr<rocksdb::Env> memenv;
std::unique_ptr<rocksdb::DB> rep_deleter;
std::shared_ptr<rocksdb::Cache> block_cache;
std::shared_ptr<DBEventListener> event_listener;

// Construct a new DBImpl from the specified DB and Env. Both the DB
// and Env will be deleted when the DBImpl is deleted. It is ok to
// pass NULL for the Env.
// Construct a new DBImpl from the specified DB.
// The DB and passed Envs will be deleted when the DBImpl is deleted.
// Either env can be NULL.
DBImpl(rocksdb::DB* r, rocksdb::Env* m, std::shared_ptr<rocksdb::Cache> bc,
std::shared_ptr<DBEventListener> event_listener)
std::shared_ptr<DBEventListener> event_listener, rocksdb::Env* s_env)
: DBEngine(r),
switching_env(s_env),
memenv(m),
rep_deleter(r),
block_cache(bc),
Expand Down Expand Up @@ -1691,20 +1694,29 @@ DBStatus DBOpen(DBEngine **db, DBSlice dir, DBOptions db_opts) {
std::shared_ptr<DBEventListener> event_listener(new DBEventListener);
options.listeners.emplace_back(event_listener);

// TODO(mberhault): we shouldn't need two separate env objects,
// options.env should be sufficient with SwitchingEnv owning any
// underlying Env.
std::unique_ptr<rocksdb::Env> memenv;
if (dir.len == 0) {
memenv.reset(rocksdb::NewMemEnv(rocksdb::Env::Default()));
options.env = memenv.get();
}

std::unique_ptr<rocksdb::Env> switching_env;
if (db_opts.use_switching_env) {
switching_env.reset(NewSwitchingEnv(options.env, options.info_log));
options.env = switching_env.get();
}

rocksdb::DB *db_ptr;
rocksdb::Status status = rocksdb::DB::Open(options, ToString(dir), &db_ptr);
if (!status.ok()) {
return ToDBStatus(status);
}
*db = new DBImpl(db_ptr, memenv.release(),
db_opts.cache != nullptr ? db_opts.cache->rep : nullptr,
event_listener);
event_listener, switching_env.release());
return kSuccess;
}

Expand Down
50 changes: 50 additions & 0 deletions c-deps/libroach/env_switching.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2017 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.

#include "env_switching.h"

/*
*
* SwitchingEnv switches between a base_env (usually a Env::Default or MemEnv) and an encrypted env.
*
* -----------------------------
* | SwitchingEnv (EnvWrapper) |
* --- PLAIN - ENCRYPTED -------
* | \
* | \
* | ------------------------------
* | | encrypted_env (EnvWrapper) |
* | ------------------------------
* | /
* | /
* ------------------
* | base_env (Env) |
* ------------------
*
* Any unimplemented methods are called on the base_env.
*/
class SwitchingEnv : public rocksdb::EnvWrapper {
public:
SwitchingEnv(rocksdb::Env* base_env, std::shared_ptr<rocksdb::Logger> logger)
: rocksdb::EnvWrapper(base_env),
logger(logger) {
rocksdb::Info(logger, "initialized switching env");
}
private:
std::shared_ptr<rocksdb::Logger> logger;
};

rocksdb::Env* NewSwitchingEnv(rocksdb::Env* base_env, std::shared_ptr<rocksdb::Logger> logger) {
return new SwitchingEnv(base_env ? base_env : rocksdb::Env::Default(), logger);
}
20 changes: 20 additions & 0 deletions c-deps/libroach/env_switching.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2017 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.

#pragma once

#include <rocksdb/env.h>

// Returns a new SwitchingEnv using the passed-in env as the base.
rocksdb::Env* NewSwitchingEnv(rocksdb::Env* base_env, std::shared_ptr<rocksdb::Logger> logger);
1 change: 1 addition & 0 deletions c-deps/libroach/include/libroach.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ typedef struct {
bool logging_enabled;
int num_cpu;
int max_open_files;
bool use_switching_env;
} DBOptions;

// Create a new cache with the specified size.
Expand Down
Loading