Support rocksdb 11
rocksdb 11 needs C++20 for its headers and hands the database back from
DB::Open() and DB::OpenForReadOnly() as a unique_ptr instead of a raw pointer.
The newest standard the compiler knows is used now, and the two open calls go
through a macro that adapts to the rocksdb version, so 7, 10 and 11 all build.
Verified with rocksdb 7.8.3 (debian), 10.2.1 (fedora) and 11.0.4 (alpine,
musl): no warnings, ctest passes, and reading a database written by rocksdb
5.11 still gives identical results for 251 word ids.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Change-Id: If16e6aa2fbe8478f3260e358dce53b2dc5144d9d
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6fccb26..84a14c4 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -3,8 +3,14 @@
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fno-rtti")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g")
-# RocksDB requires C++17 since version 7
-set(CMAKE_CXX_STANDARD 17)
+# RocksDB requires C++17 since version 7 and C++20 since version 11, so use the
+# newest standard the compiler knows. Nothing here depends on it, this only has
+# to satisfy the rocksdb headers.
+if ("cxx_std_20" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
+ set(CMAKE_CXX_STANDARD 20)
+else()
+ set(CMAKE_CXX_STANDARD 17)
+endif()
set(CMAKE_CXX_STANDARD_REQUIRED ON)
include(GNUInstallDirs)
diff --git a/src/collocatordb.cc b/src/collocatordb.cc
index 30656fc..1e0a8ec 100644
--- a/src/collocatordb.cc
+++ b/src/collocatordb.cc
@@ -16,11 +16,23 @@
#include <memory>
#include <rocksdb/merge_operator.h>
#include <rocksdb/slice_transform.h>
+#include <rocksdb/version.h>
#include <sstream> // for ostringstream
#include <string>
#include <thread>
+#include <utility>
#include <vector>
+/* Since rocksdb 11 DB::Open() and DB::OpenForReadOnly() hand the database back
+ as a unique_ptr instead of a raw pointer. */
+#if ROCKSDB_MAJOR >= 11
+#define ROCKSDB_DB_HANDLE std::unique_ptr<rocksdb::DB>
+#define ROCKSDB_DB_RELEASE(handle) (handle).release()
+#else
+#define ROCKSDB_DB_HANDLE rocksdb::DB *
+#define ROCKSDB_DB_RELEASE(handle) (handle)
+#endif
+
#define WINDOW_SIZE 5
#define FREQUENCY_THRESHOLD 5
#define IS_BIG_ENDIAN (*(uint16_t *)"\0\xff" < 0x100)
@@ -498,7 +510,7 @@
}
std::shared_ptr<DB> CollocatorDB::OpenDbForRead(const char *name) {
- DB *db;
+ ROCKSDB_DB_HANDLE db;
Options options;
options.env->SetBackgroundThreads(4);
options.create_if_missing = true;
@@ -517,11 +529,11 @@
}
vocabname << name << ".vocab";
readVocab(vocabname.str());
- return std::shared_ptr<DB>(db);
+ return std::shared_ptr<DB>(ROCKSDB_DB_RELEASE(db));
}
std::shared_ptr<DB> CollocatorDB::OpenDb(const char *dbname) {
- DB *db;
+ ROCKSDB_DB_HANDLE db;
Options options;
int max_cores = static_cast<int>(std::thread::hardware_concurrency());
@@ -569,7 +581,7 @@
assert(false);
}
total = 1000;
- return std::shared_ptr<DB>(db);
+ return std::shared_ptr<DB>(ROCKSDB_DB_RELEASE(db));
}
CollocatorIterator *