github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/test/fs/gtest_rocksdb_storage.cpp (about) 1 // Copyright (C) 2018 go-nebulas authors 2 // 3 // This file is part of the go-nebulas library. 4 // 5 // the go-nebulas library is free software: you can redistribute it and/or 6 // modify 7 // it under the terms of the GNU General Public License as published by 8 // the Free Software Foundation, either version 3 of the License, or 9 // (at your option) any later version. 10 // 11 // the go-nebulas library is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 // 16 // You should have received a copy of the GNU General Public License 17 // along with the go-nebulas library. If not, see 18 // <http://www.gnu.org/licenses/>. 19 // 20 21 #include "common/configuration.h" 22 #include "core/command.h" 23 #include "fs/blockchain.h" 24 #include "fs/proto/block.pb.h" 25 #include "fs/rocksdb_storage.h" 26 #include "fs/util.h" 27 #include "gtest_common.h" 28 #include <gtest/gtest.h> 29 30 std::string get_db_path_for_read() { 31 std::string cur_path = neb::configuration::instance().nbre_root_dir(); 32 return neb::fs::join_path(cur_path, "test/data/read-data.db/"); 33 } 34 35 std::string get_db_path_for_write() { 36 std::string cur_path = neb::configuration::instance().nbre_root_dir(); 37 return neb::fs::join_path(cur_path, "test/data/write-data.db/"); 38 } 39 40 std::string get_blockchain_path_for_read() { 41 std::string cur_path = neb::configuration::instance().nbre_root_dir(); 42 return neb::fs::join_path(cur_path, "../data.db/"); 43 } 44 45 TEST(test_fs, positive_storage_read_bc) { 46 std::string db_path = get_db_path_for_read(); 47 48 neb::fs::rocksdb_storage rs; 49 EXPECT_THROW(rs.get(neb::fs::blockchain::Block_LIB), 50 neb::fs::storage_exception_no_init); 51 EXPECT_THROW( 52 rs.put(neb::fs::blockchain::Block_LIB, neb::string_to_byte("xxx")), 53 neb::fs::storage_exception_no_init); 54 EXPECT_THROW(rs.del(neb::fs::blockchain::Block_LIB), 55 neb::fs::storage_exception_no_init); 56 57 rs.open_database(db_path, neb::fs::storage_open_for_readonly); 58 neb::fs::rocksdb_storage rs2; 59 rs2.open_database(db_path, neb::fs::storage_open_for_readonly); 60 61 auto tail_block_hash = rs.get(neb::fs::blockchain::Block_LIB); 62 63 auto tail_bytes = rs.get_bytes(tail_block_hash); 64 65 corepb::Block block; 66 block.ParseFromArray(tail_bytes.value(), tail_bytes.size()); 67 rs.close_database(); 68 } 69 70 TEST(test_fs, storage_read_write) { 71 std::string db_path = get_db_path_for_read(); 72 73 neb::fs::rocksdb_storage rs; 74 rs.open_database(db_path, neb::fs::storage_open_for_readonly); 75 neb::fs::rocksdb_storage rs2; 76 rs2.open_database(db_path, neb::fs::storage_open_for_readwrite); 77 } 78 79 TEST(test_fs, storage_write_write) { 80 std::string db_path = get_db_path_for_read(); 81 82 neb::fs::rocksdb_storage rs; 83 rs.open_database(db_path, neb::fs::storage_open_for_readwrite); 84 neb::fs::rocksdb_storage rs2; 85 EXPECT_THROW(rs2.open_database(db_path, neb::fs::storage_open_for_readwrite), 86 neb::fs::storage_general_failure); 87 } 88 89 TEST(test_fs, storage_batch_op) { 90 std::string db_path = get_db_path_for_write(); 91 neb::fs::rocksdb_storage rs; 92 rs.open_database(db_path, neb::fs::storage_open_for_readwrite); 93 rs.put("123", neb::number_to_byte<neb::bytes>(static_cast<int64_t>(234))); 94 95 auto bytes = rs.get("123"); 96 int64_t value = neb::byte_to_number<int64_t>(bytes); 97 EXPECT_EQ(value, 234); 98 } 99