virtual_db.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Tencent is pleased to support the open source community by making wwsearch
  3. * available.
  4. *
  5. * Copyright (C) 2018-present Tencent. All Rights Reserved.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  8. * use this file except in compliance with the License. You may obtain a copy of
  9. * the License at
  10. *
  11. * https://opensource.org/licenses/Apache-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OF ANY KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations under the License.
  17. */
  18. #pragma once
  19. #include "codec.h"
  20. #include "logger.h"
  21. #include "rocksdb/options.h"
  22. #include "search_iterator.h"
  23. #include "storage_type.h"
  24. #include "virtual_db_rocks_compaction_filter.h"
  25. #include "write_buffer.h"
  26. namespace wwsearch {
  27. class IndexConfig;
  28. typedef struct VDBParams {
  29. std::string path;
  30. // we must have two file in mmseg_dict_dir_
  31. // * uni.lib
  32. // * mmseg.ini
  33. std::string mmseg_dict_dir_ = "./";
  34. uint64_t mmseg_instance_num_ = 20;
  35. // paxos log expire time (seconds)
  36. // <=0 indicate infinite
  37. int32_t plog_ttl_seconds_ = 86400;
  38. // rocksdb
  39. uint32_t rocks_max_background_jobs_{8};
  40. uint32_t rocks_max_subcompactions{1};
  41. uint64_t rocks_max_bytes_for_level_base = 256 << 20; /* 256MB */
  42. bool rocks_allow_concurrent_memtable_write{true};
  43. int32_t rocks_num_levels{7};
  44. int32_t rocks_level0_file_num_compaction_trigger{4};
  45. int32_t rocks_level0_slowdown_writes_trigger{20};
  46. int32_t rocks_level0_stop_writes_trigger{36};
  47. // reference to index config
  48. // IndexConfig* config_;
  49. // rocksdb
  50. uint64_t rocks_max_log_file_size = 50 << 20; // 50MB
  51. uint64_t rocks_log_file_time_to_roll = 3600; // one hours
  52. uint64_t rocks_keep_log_file_num = 240; // 10 days
  53. // manifest
  54. uint64_t rocks_max_manifest_file_size = 50 << 20; // 50MB
  55. // write
  56. uint64_t rocks_write_buffer_size = 64 << 20; // 64 MB
  57. uint64_t rocks_max_write_buffer_number = 6; // 6 write buffer
  58. uint64_t rocks_max_total_wal_size = 100 << 20; // 200 MB
  59. uint64_t rocks_db_write_buffer_size = 100 << 20; // 200 MB
  60. // uint64_t rocks_wal_bytes_per_sync = 20 << 20; // 2MB sync once ?
  61. // uint64_t rocks_bytes_per_sync = 20 << 20;
  62. // table cache block cache
  63. uint64_t rocks_db_block_cache_bytes = 0; // in bytes. if 0,not set
  64. bool rocks_db_cache_index_and_filter_blocks = false;
  65. uint64_t rocks_db_max_open_files = 0; // if 0 ,not set
  66. // compression
  67. rocksdb::CompressionType rocks_compression = rocksdb::kSnappyCompression;
  68. std::map<StorageColumnType, VirtualDBRocksCompactionFilter*>
  69. columns_compactionfilter;
  70. Codec* codec_;
  71. uint32_t max_doc_list_num_ = 1000000;
  72. } VDBParams;
  73. class VirtualDBSnapshot {
  74. private:
  75. public:
  76. virtual ~VirtualDBSnapshot() {}
  77. private:
  78. };
  79. class VirtualDBWriteOption {
  80. private:
  81. public:
  82. virtual ~VirtualDBWriteOption() {}
  83. private:
  84. };
  85. class VirtualDBReadOption {
  86. public:
  87. VirtualDBSnapshot* snapshot_;
  88. public:
  89. VirtualDBReadOption() : snapshot_(nullptr) {}
  90. virtual ~VirtualDBReadOption() {}
  91. private:
  92. };
  93. class VirtualDB {
  94. private:
  95. std::string msg_if_error;
  96. public:
  97. virtual ~VirtualDB(){};
  98. virtual bool Open() = 0;
  99. // snapshot
  100. virtual VirtualDBSnapshot* NewSnapshot() = 0;
  101. virtual void ReleaseSnapshot(VirtualDBSnapshot*) = 0;
  102. // for write
  103. virtual SearchStatus FlushBuffer(WriteBuffer* write_buffer) = 0;
  104. // virtual SearchStatus FlushBuffer(const std::string* write_buffer) = 0;
  105. virtual WriteBuffer* NewWriteBuffer(const std::string* write_buffer) = 0;
  106. virtual void ReleaseWriteBuffer(WriteBuffer* buffer) = 0;
  107. // for read
  108. virtual SearchStatus Get(StorageColumnType column, const std::string& key,
  109. std::string& value, VirtualDBSnapshot* snapshot) = 0;
  110. // columns's size and keys's size should be equal.
  111. // status and values 's size will alway resize to keys's size.
  112. virtual void MultiGet(std::vector<StorageColumnType> columns,
  113. std::vector<std::string>& keys,
  114. std::vector<std::string>& values,
  115. std::vector<SearchStatus>& status,
  116. VirtualDBSnapshot* snapshot) = 0;
  117. // for iterator
  118. virtual Iterator* NewIterator(StorageColumnType column,
  119. VirtualDBReadOption* options) = 0;
  120. // Compact the underlying storage for the key range [*begin,*end]
  121. virtual SearchStatus CompactRange(StorageColumnType column,
  122. const std::string& begin,
  123. const std::string& end) = 0;
  124. inline void SetState(const char* msg) { this->msg_if_error.assign(msg); }
  125. inline std::string& GetState() { return this->msg_if_error; }
  126. // for drop
  127. virtual SearchStatus DropDB() = 0;
  128. private:
  129. };
  130. } // namespace wwsearch