Created
April 18, 2018 12:55
-
-
Save rbs392/e2f768b1a09bbd180490131dad0b5958 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <sstream> | |
| #include "rocksdb/db.h" | |
| #include "rocksdb/utilities/backupable_db.h" | |
| class Rocks{ | |
| rocksdb::DB *db; | |
| rocksdb::Options opts; | |
| rocksdb::WriteOptions wOpts; | |
| rocksdb::ReadOptions rOpts; | |
| bool isBackupEngineUp = false; | |
| rocksdb::BackupEngine* backup_engine; | |
| public: | |
| int open( std::string path ) { | |
| opts.create_if_missing = true; | |
| rocksdb::Status status = rocksdb::DB::Open(opts, path, &db); | |
| return status.ok(); | |
| } | |
| int get(std::string key, std::string &value) { | |
| rocksdb::Status st = db->Get(rOpts, key, &value); | |
| return st.ok(); | |
| } | |
| int put(std::string key, std::string value) { | |
| rocksdb::Status st = db->Put(wOpts, key, value); | |
| return st.ok(); | |
| } | |
| int backup(std::string path) { | |
| rocksdb::Status st = rocksdb::BackupEngine::Open(rocksdb::Env::Default(), rocksdb::BackupableDBOptions(path), &backup_engine); | |
| assert(st.ok()); | |
| st = backup_engine->CreateNewBackup(db); | |
| st.ok(); | |
| } | |
| std::vector<rocksdb::BackupInfo> backupInfo() { | |
| std::vector<rocksdb::BackupInfo> bkupsInfo; | |
| backup_engine->GetBackupInfo(&bkupsInfo); | |
| return bkupsInfo; | |
| } | |
| int restore(std::string path, std::string dbPath, std::string walPath) { | |
| rocksdb::BackupEngineReadOnly* backup_engine_ro; | |
| rocksdb::Status st = rocksdb::BackupEngineReadOnly::Open(rocksdb::Env::Default(), rocksdb::BackupableDBOptions(path), &backup_engine_ro); | |
| assert(st.ok()); | |
| st = backup_engine_ro->RestoreDBFromLatestBackup(dbPath, walPath); | |
| delete backup_engine_ro; | |
| return st.ok(); | |
| } | |
| int del() { | |
| delete db; | |
| delete backup_engine; | |
| return 0; | |
| } | |
| }; | |
| int main() | |
| { | |
| Rocks r; | |
| std::string choice; | |
| std::string key, value; | |
| std::string dbPath = "/tmp/db1"; | |
| r.open(dbPath); | |
| while(true) { | |
| std::cout << "=> "; | |
| std::string line; | |
| std::getline(std::cin, line); | |
| std::istringstream stream(line); | |
| stream >> choice >> key >> value; | |
| if(choice == "get") { | |
| std::string val; | |
| r.get(key, val); | |
| std::cout << "-> " << val << std::endl; | |
| } else if(choice == "put") { | |
| std::cout << "-> " << r.put(key, value) << std::endl; | |
| } else if(choice=="backup") { | |
| r.backup(key); | |
| for( rocksdb::BackupInfo bkup : r.backupInfo()) { | |
| std::cout << "backup_id -> " << bkup.backup_id << std::endl; | |
| } | |
| } else if(choice=="restore") { | |
| std::cout << " -> " << r.restore(key, value, value) << std::endl; | |
| }else if(choice == "del" || choice == "exit") { | |
| r.del(); | |
| break; | |
| } else if( choice == "update") { | |
| r.open(key); | |
| }else { | |
| std::cout << "invalid choice" << std::endl; | |
| } | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment