10225501448 李度 10225101546 陈胤遒 10215501422 高宇菲
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

535 lines
16 KiB

Add support for Zstd-based compression in LevelDB. This change implements support for Zstd-based compression in LevelDB. Building up from the Snappy compression (which has been supported since inception), this change adds Zstd as an alternate compression algorithm. We are implementing this to provide alternative options for users who might have different performance and efficiency requirements. For instance, the Zstandard website (https://facebook.github.io/zstd/) claims that the Zstd algorithm can achieve around 30% higher compression ratios than Snappy, with relatively smaller (~10%) slowdowns in de/compression speeds. Benchmarking results: $ blaze-bin/third_party/leveldb/db_bench LevelDB: version 1.23 Date: Thu Feb 2 18:50:06 2023 CPU: 56 * Intel(R) Xeon(R) CPU E5-2690 v4 @ 2.60GHz CPUCache: 35840 KB Keys: 16 bytes each Values: 100 bytes each (50 bytes after compression) Entries: 1000000 RawSize: 110.6 MB (estimated) FileSize: 62.9 MB (estimated) ------------------------------------------------ fillseq : 2.613 micros/op; 42.3 MB/s fillsync : 3924.432 micros/op; 0.0 MB/s (1000 ops) fillrandom : 3.609 micros/op; 30.7 MB/s overwrite : 4.508 micros/op; 24.5 MB/s readrandom : 6.136 micros/op; (864322 of 1000000 found) readrandom : 5.446 micros/op; (864083 of 1000000 found) readseq : 0.180 micros/op; 613.3 MB/s readreverse : 0.321 micros/op; 344.7 MB/s compact : 827043.000 micros/op; readrandom : 4.603 micros/op; (864105 of 1000000 found) readseq : 0.169 micros/op; 656.3 MB/s readreverse : 0.315 micros/op; 350.8 MB/s fill100K : 854.009 micros/op; 111.7 MB/s (1000 ops) crc32c : 1.227 micros/op; 3184.0 MB/s (4K per op) snappycomp : 3.610 micros/op; 1081.9 MB/s (output: 55.2%) snappyuncomp : 0.691 micros/op; 5656.3 MB/s zstdcomp : 15.731 micros/op; 248.3 MB/s (output: 44.1%) zstduncomp : 4.218 micros/op; 926.2 MB/s PiperOrigin-RevId: 509957778
1 year ago
leveldb: Fix PosixWritableFile::Sync() on Apple systems. Apple doesn't follow POSIX specifications for fsync(). Instead, fsync() guarantees to flush the buffer cache to the device, which means the data will survive kernel panics, but may not survive power outages. Applications that need stronger guarantees (like databases) need to use fcntl(F_FULLFSYNC). This CL switches PosixWritableFile::Sync() to get the stronger guarantees on Apple systems. The improved implementation follows the same principles as SQLite [1] and node.js [2]. Research for the fcntl() to fsync() fallback strategy: Apple's released source code at https://opensource.apple.com/ shows at least three different error codes being returned when a filesystem does not support F_FULLFSYNC. fcntl() is implemented in xnu-4903.221.2 in bsd/kern/kern_descrip.c, where it delegates to fcntl_nocancel(). The documentation for fcntl_nocancel() mentions error codes for some operations, but does not include F_FULLFSYNC. The F_FULLSYNC branch in fcntl_nocancel() calls VNOP_IOCTL(_, F_FULLSYNC, NULL, 0, _), whose return value sets the error code. VNOP_IOCTL() is implemented in bsd/vfs/kpi_vfs.c and calls the ioctl function in the vnode's operation vector. The per-filesystem function names follow the pattern _vnop_ioctl() for all the instances in opensource code: {hfs,msdosfs,nfs,ntfs,smbfs,webdav,zfs}_vnop_ioctl(). hfs-407.30.1, msdosfs-229.200.3, and nfs in xnu-4903.221.2 handle F_FULLFSYNC. ntfs-94.200.1 and smb-759.40.1 do not handle F_FULLFSYNC, and the default branch returns ENOSUP. webdav-380.200.1 also does not handle F_FULLFSYNC, but the default branch returns EINVAL. zfs-59 also does not handle F_FULLSYNC, and its default branch returns ENOTTY. From a different angle, Apple's ntfs-94.200.1 includes utility code that uses fcntl(F_FULLFSYNC) and falls back to fsync() just like we do, supporting the hypothesis that there is no good way to detect lack of F_FULLFSYNC support. Also, Apple's fcntl() man page [3] does not mention a way to detect lack of F_FULLFSYNC support. [1] https://www.sqlite.org/src/doc/trunk/src/os_unix.c [2] https://github.com/libuv/libuv/blob/master/src/unix/fs.c [3] https://developer.apple.com/library/archive/documentatiVon/System/Conceptual/ManPages_iPhoneOS/man2/fcntl.2.html Tested: https://travis-ci.org/pwnall/leveldb/builds/477318498 TAP global presubmit ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=228593729
5 years ago
3 weeks ago
Add support for Zstd-based compression in LevelDB. This change implements support for Zstd-based compression in LevelDB. Building up from the Snappy compression (which has been supported since inception), this change adds Zstd as an alternate compression algorithm. We are implementing this to provide alternative options for users who might have different performance and efficiency requirements. For instance, the Zstandard website (https://facebook.github.io/zstd/) claims that the Zstd algorithm can achieve around 30% higher compression ratios than Snappy, with relatively smaller (~10%) slowdowns in de/compression speeds. Benchmarking results: $ blaze-bin/third_party/leveldb/db_bench LevelDB: version 1.23 Date: Thu Feb 2 18:50:06 2023 CPU: 56 * Intel(R) Xeon(R) CPU E5-2690 v4 @ 2.60GHz CPUCache: 35840 KB Keys: 16 bytes each Values: 100 bytes each (50 bytes after compression) Entries: 1000000 RawSize: 110.6 MB (estimated) FileSize: 62.9 MB (estimated) ------------------------------------------------ fillseq : 2.613 micros/op; 42.3 MB/s fillsync : 3924.432 micros/op; 0.0 MB/s (1000 ops) fillrandom : 3.609 micros/op; 30.7 MB/s overwrite : 4.508 micros/op; 24.5 MB/s readrandom : 6.136 micros/op; (864322 of 1000000 found) readrandom : 5.446 micros/op; (864083 of 1000000 found) readseq : 0.180 micros/op; 613.3 MB/s readreverse : 0.321 micros/op; 344.7 MB/s compact : 827043.000 micros/op; readrandom : 4.603 micros/op; (864105 of 1000000 found) readseq : 0.169 micros/op; 656.3 MB/s readreverse : 0.315 micros/op; 350.8 MB/s fill100K : 854.009 micros/op; 111.7 MB/s (1000 ops) crc32c : 1.227 micros/op; 3184.0 MB/s (4K per op) snappycomp : 3.610 micros/op; 1081.9 MB/s (output: 55.2%) snappyuncomp : 0.691 micros/op; 5656.3 MB/s zstdcomp : 15.731 micros/op; 248.3 MB/s (output: 44.1%) zstduncomp : 4.218 micros/op; 926.2 MB/s PiperOrigin-RevId: 509957778
1 year ago
  1. # Copyright 2017 The LevelDB Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file. See the AUTHORS file for names of contributors.
  4. cmake_minimum_required(VERSION 3.9)
  5. # Keep the version below in sync with the one in db.h
  6. project(leveldb VERSION 1.23.0 LANGUAGES C CXX)
  7. # C standard can be overridden when this is used as a sub-project.
  8. if(NOT CMAKE_C_STANDARD)
  9. # This project can use C11, but will gracefully decay down to C89.
  10. set(CMAKE_C_STANDARD 11)
  11. set(CMAKE_C_STANDARD_REQUIRED OFF)
  12. set(CMAKE_C_EXTENSIONS OFF)
  13. endif(NOT CMAKE_C_STANDARD)
  14. # C++ standard can be overridden when this is used as a sub-project.
  15. if(NOT CMAKE_CXX_STANDARD)
  16. # This project requires C++11.
  17. set(CMAKE_CXX_STANDARD 11)
  18. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  19. set(CMAKE_CXX_EXTENSIONS OFF)
  20. endif(NOT CMAKE_CXX_STANDARD)
  21. if (WIN32)
  22. set(LEVELDB_PLATFORM_NAME LEVELDB_PLATFORM_WINDOWS)
  23. # TODO(cmumford): Make UNICODE configurable for Windows.
  24. add_definitions(-D_UNICODE -DUNICODE)
  25. else (WIN32)
  26. set(LEVELDB_PLATFORM_NAME LEVELDB_PLATFORM_POSIX)
  27. endif (WIN32)
  28. option(LEVELDB_BUILD_TESTS "Build LevelDB's unit tests" ON)
  29. option(LEVELDB_BUILD_BENCHMARKS "Build LevelDB's benchmarks" ON)
  30. option(LEVELDB_INSTALL "Install LevelDB's header and library" ON)
  31. include(CheckIncludeFile)
  32. check_include_file("unistd.h" HAVE_UNISTD_H)
  33. include(CheckLibraryExists)
  34. check_library_exists(crc32c crc32c_value "" HAVE_CRC32C)
  35. check_library_exists(snappy snappy_compress "" HAVE_SNAPPY)
  36. check_library_exists(zstd zstd_compress "" HAVE_ZSTD)
  37. check_library_exists(tcmalloc malloc "" HAVE_TCMALLOC)
  38. include(CheckCXXSymbolExists)
  39. # Using check_cxx_symbol_exists() instead of check_c_symbol_exists() because
  40. # we're including the header from C++, and feature detection should use the same
  41. # compiler language that the project will use later. Principles aside, some
  42. # versions of do not expose fdatasync() in <unistd.h> in standard C mode
  43. # (-std=c11), but do expose the function in standard C++ mode (-std=c++11).
  44. check_cxx_symbol_exists(fdatasync "unistd.h" HAVE_FDATASYNC)
  45. check_cxx_symbol_exists(F_FULLFSYNC "fcntl.h" HAVE_FULLFSYNC)
  46. check_cxx_symbol_exists(O_CLOEXEC "fcntl.h" HAVE_O_CLOEXEC)
  47. if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
  48. # Disable C++ exceptions.
  49. string(REGEX REPLACE "/EH[a-z]+" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
  50. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHs-c-")
  51. add_definitions(-D_HAS_EXCEPTIONS=0)
  52. # Disable RTTI.
  53. string(REGEX REPLACE "/GR" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
  54. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /GR-")
  55. else(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
  56. # Enable strict prototype warnings for C code in clang and gcc.
  57. if(NOT CMAKE_C_FLAGS MATCHES "-Wstrict-prototypes")
  58. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wstrict-prototypes")
  59. endif(NOT CMAKE_C_FLAGS MATCHES "-Wstrict-prototypes")
  60. # Disable C++ exceptions.
  61. string(REGEX REPLACE "-fexceptions" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
  62. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions")
  63. # Disable RTTI.
  64. string(REGEX REPLACE "-frtti" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
  65. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti")
  66. endif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
  67. # Test whether -Wthread-safety is available. See
  68. # https://clang.llvm.org/docs/ThreadSafetyAnalysis.html
  69. include(CheckCXXCompilerFlag)
  70. check_cxx_compiler_flag(-Wthread-safety HAVE_CLANG_THREAD_SAFETY)
  71. # Used by googletest.
  72. check_cxx_compiler_flag(-Wno-missing-field-initializers
  73. LEVELDB_HAVE_NO_MISSING_FIELD_INITIALIZERS)
  74. include(CheckCXXSourceCompiles)
  75. # Test whether C++17 __has_include is available.
  76. check_cxx_source_compiles("
  77. #if defined(__has_include) && __has_include(<string>)
  78. #include <string>
  79. #endif
  80. int main() { std::string str; return 0; }
  81. " HAVE_CXX17_HAS_INCLUDE)
  82. set(LEVELDB_PUBLIC_INCLUDE_DIR "include/leveldb")
  83. set(LEVELDB_PORT_CONFIG_DIR "include/port")
  84. configure_file(
  85. "port/port_config.h.in"
  86. "${PROJECT_BINARY_DIR}/${LEVELDB_PORT_CONFIG_DIR}/port_config.h"
  87. )
  88. include_directories(
  89. "${PROJECT_BINARY_DIR}/include"
  90. "."
  91. "./third_party/googletest/googletest/include/"
  92. )
  93. if(BUILD_SHARED_LIBS)
  94. # Only export LEVELDB_EXPORT symbols from the shared library.
  95. add_compile_options(-fvisibility=hidden)
  96. endif(BUILD_SHARED_LIBS)
  97. # Must be included before CMAKE_INSTALL_INCLUDEDIR is used.
  98. include(GNUInstallDirs)
  99. add_library(leveldb "")
  100. target_sources(leveldb
  101. PRIVATE
  102. "${PROJECT_BINARY_DIR}/${LEVELDB_PORT_CONFIG_DIR}/port_config.h"
  103. "db/builder.cc"
  104. "db/builder.h"
  105. "db/c.cc"
  106. "db/db_impl.cc"
  107. "db/db_impl.h"
  108. "db/db_iter.cc"
  109. "db/db_iter.h"
  110. "db/dbformat.cc"
  111. "db/dbformat.h"
  112. "db/dumpfile.cc"
  113. "db/filename.cc"
  114. "db/filename.h"
  115. "db/log_format.h"
  116. "db/log_reader.cc"
  117. "db/log_reader.h"
  118. "db/log_writer.cc"
  119. "db/log_writer.h"
  120. "db/memtable.cc"
  121. "db/memtable.h"
  122. "db/repair.cc"
  123. "db/skiplist.h"
  124. "db/snapshot.h"
  125. "db/table_cache.cc"
  126. "db/table_cache.h"
  127. "db/version_edit.cc"
  128. "db/version_edit.h"
  129. "db/version_set.cc"
  130. "db/version_set.h"
  131. "db/write_batch_internal.h"
  132. "db/write_batch.cc"
  133. "port/port_stdcxx.h"
  134. "port/port.h"
  135. "port/thread_annotations.h"
  136. "table/block_builder.cc"
  137. "table/block_builder.h"
  138. "table/block.cc"
  139. "table/block.h"
  140. "table/filter_block.cc"
  141. "table/filter_block.h"
  142. "table/format.cc"
  143. "table/format.h"
  144. "table/iterator_wrapper.h"
  145. "table/iterator.cc"
  146. "table/merger.cc"
  147. "table/merger.h"
  148. "table/table_builder.cc"
  149. "table/table.cc"
  150. "table/two_level_iterator.cc"
  151. "table/two_level_iterator.h"
  152. "util/arena.cc"
  153. "util/arena.h"
  154. "util/bloom.cc"
  155. "util/cache.cc"
  156. "util/coding.cc"
  157. "util/coding.h"
  158. "util/comparator.cc"
  159. "util/crc32c.cc"
  160. "util/crc32c.h"
  161. "util/env.cc"
  162. "util/filter_policy.cc"
  163. "util/hash.cc"
  164. "util/hash.h"
  165. "util/logging.cc"
  166. "util/logging.h"
  167. "util/mutexlock.h"
  168. "util/no_destructor.h"
  169. "util/options.cc"
  170. "util/random.h"
  171. "util/status.cc"
  172. # Only CMake 3.3+ supports PUBLIC sources in targets exported by "install".
  173. $<$<VERSION_GREATER:CMAKE_VERSION,3.2>:PUBLIC>
  174. "${LEVELDB_PUBLIC_INCLUDE_DIR}/c.h"
  175. "${LEVELDB_PUBLIC_INCLUDE_DIR}/cache.h"
  176. "${LEVELDB_PUBLIC_INCLUDE_DIR}/comparator.h"
  177. "${LEVELDB_PUBLIC_INCLUDE_DIR}/db.h"
  178. "${LEVELDB_PUBLIC_INCLUDE_DIR}/dumpfile.h"
  179. "${LEVELDB_PUBLIC_INCLUDE_DIR}/env.h"
  180. "${LEVELDB_PUBLIC_INCLUDE_DIR}/export.h"
  181. "${LEVELDB_PUBLIC_INCLUDE_DIR}/filter_policy.h"
  182. "${LEVELDB_PUBLIC_INCLUDE_DIR}/iterator.h"
  183. "${LEVELDB_PUBLIC_INCLUDE_DIR}/options.h"
  184. "${LEVELDB_PUBLIC_INCLUDE_DIR}/slice.h"
  185. "${LEVELDB_PUBLIC_INCLUDE_DIR}/status.h"
  186. "${LEVELDB_PUBLIC_INCLUDE_DIR}/table_builder.h"
  187. "${LEVELDB_PUBLIC_INCLUDE_DIR}/table.h"
  188. "${LEVELDB_PUBLIC_INCLUDE_DIR}/write_batch.h"
  189. )
  190. if (WIN32)
  191. target_sources(leveldb
  192. PRIVATE
  193. "util/env_windows.cc"
  194. "util/windows_logger.h"
  195. )
  196. else (WIN32)
  197. target_sources(leveldb
  198. PRIVATE
  199. "util/env_posix.cc"
  200. "util/posix_logger.h"
  201. )
  202. endif (WIN32)
  203. # MemEnv is not part of the interface and could be pulled to a separate library.
  204. target_sources(leveldb
  205. PRIVATE
  206. "helpers/memenv/memenv.cc"
  207. "helpers/memenv/memenv.h"
  208. )
  209. target_include_directories(leveldb
  210. PUBLIC
  211. $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
  212. $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
  213. )
  214. set_target_properties(leveldb
  215. PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR})
  216. target_compile_definitions(leveldb
  217. PRIVATE
  218. # Used by include/export.h when building shared libraries.
  219. LEVELDB_COMPILE_LIBRARY
  220. # Used by port/port.h.
  221. ${LEVELDB_PLATFORM_NAME}=1
  222. )
  223. if (NOT HAVE_CXX17_HAS_INCLUDE)
  224. target_compile_definitions(leveldb
  225. PRIVATE
  226. LEVELDB_HAS_PORT_CONFIG_H=1
  227. )
  228. endif(NOT HAVE_CXX17_HAS_INCLUDE)
  229. if(BUILD_SHARED_LIBS)
  230. target_compile_definitions(leveldb
  231. PUBLIC
  232. # Used by include/export.h.
  233. LEVELDB_SHARED_LIBRARY
  234. )
  235. endif(BUILD_SHARED_LIBS)
  236. if(HAVE_CLANG_THREAD_SAFETY)
  237. target_compile_options(leveldb
  238. PUBLIC
  239. -Werror -Wthread-safety)
  240. endif(HAVE_CLANG_THREAD_SAFETY)
  241. if(HAVE_CRC32C)
  242. target_link_libraries(leveldb crc32c)
  243. endif(HAVE_CRC32C)
  244. if(HAVE_SNAPPY)
  245. target_link_libraries(leveldb snappy)
  246. endif(HAVE_SNAPPY)
  247. if(HAVE_ZSTD)
  248. target_link_libraries(leveldb zstd)
  249. endif(HAVE_ZSTD)
  250. if(HAVE_TCMALLOC)
  251. target_link_libraries(leveldb tcmalloc)
  252. endif(HAVE_TCMALLOC)
  253. # Needed by port_stdcxx.h
  254. find_package(Threads REQUIRED)
  255. target_link_libraries(leveldb Threads::Threads)
  256. add_executable(leveldbutil
  257. "db/leveldbutil.cc"
  258. )
  259. target_link_libraries(leveldbutil leveldb)
  260. if(LEVELDB_BUILD_TESTS)
  261. enable_testing()
  262. # Prevent overriding the parent project's compiler/linker settings on Windows.
  263. set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
  264. set(install_gtest OFF)
  265. set(install_gmock OFF)
  266. set(build_gmock ON)
  267. # This project is tested using GoogleTest.
  268. add_subdirectory("third_party/googletest")
  269. # GoogleTest triggers a missing field initializers warning.
  270. if(LEVELDB_HAVE_NO_MISSING_FIELD_INITIALIZERS)
  271. set_property(TARGET gtest
  272. APPEND PROPERTY COMPILE_OPTIONS -Wno-missing-field-initializers)
  273. set_property(TARGET gmock
  274. APPEND PROPERTY COMPILE_OPTIONS -Wno-missing-field-initializers)
  275. endif(LEVELDB_HAVE_NO_MISSING_FIELD_INITIALIZERS)
  276. add_executable(leveldb_tests "")
  277. target_sources(leveldb_tests
  278. PRIVATE
  279. # "db/fault_injection_test.cc"
  280. # "issues/issue178_test.cc"
  281. # "issues/issue200_test.cc"
  282. # "issues/issue320_test.cc"
  283. "${PROJECT_BINARY_DIR}/${LEVELDB_PORT_CONFIG_DIR}/port_config.h"
  284. # "util/env_test.cc"
  285. "util/status_test.cc"
  286. "util/no_destructor_test.cc"
  287. "util/testutil.cc"
  288. "util/testutil.h"
  289. )
  290. if(NOT BUILD_SHARED_LIBS)
  291. target_sources(leveldb_tests
  292. PRIVATE
  293. "db/autocompact_test.cc"
  294. "db/corruption_test.cc"
  295. "db/db_test.cc"
  296. "db/dbformat_test.cc"
  297. "db/filename_test.cc"
  298. "db/log_test.cc"
  299. "db/recovery_test.cc"
  300. "db/skiplist_test.cc"
  301. "db/version_edit_test.cc"
  302. "db/version_set_test.cc"
  303. "db/write_batch_test.cc"
  304. "helpers/memenv/memenv_test.cc"
  305. "table/filter_block_test.cc"
  306. "table/table_test.cc"
  307. "util/arena_test.cc"
  308. "util/bloom_test.cc"
  309. "util/cache_test.cc"
  310. "util/coding_test.cc"
  311. "util/crc32c_test.cc"
  312. "util/hash_test.cc"
  313. "util/logging_test.cc"
  314. )
  315. endif(NOT BUILD_SHARED_LIBS)
  316. target_link_libraries(leveldb_tests leveldb gmock gtest gtest_main)
  317. target_compile_definitions(leveldb_tests
  318. PRIVATE
  319. ${LEVELDB_PLATFORM_NAME}=1
  320. )
  321. if (NOT HAVE_CXX17_HAS_INCLUDE)
  322. target_compile_definitions(leveldb_tests
  323. PRIVATE
  324. LEVELDB_HAS_PORT_CONFIG_H=1
  325. )
  326. endif(NOT HAVE_CXX17_HAS_INCLUDE)
  327. add_test(NAME "leveldb_tests" COMMAND "leveldb_tests")
  328. function(leveldb_test test_file)
  329. get_filename_component(test_target_name "${test_file}" NAME_WE)
  330. add_executable("${test_target_name}" "")
  331. target_sources("${test_target_name}"
  332. PRIVATE
  333. "${PROJECT_BINARY_DIR}/${LEVELDB_PORT_CONFIG_DIR}/port_config.h"
  334. "util/testutil.cc"
  335. "util/testutil.h"
  336. "${test_file}"
  337. )
  338. target_link_libraries("${test_target_name}" leveldb gmock gtest)
  339. target_compile_definitions("${test_target_name}"
  340. PRIVATE
  341. ${LEVELDB_PLATFORM_NAME}=1
  342. )
  343. if (NOT HAVE_CXX17_HAS_INCLUDE)
  344. target_compile_definitions("${test_target_name}"
  345. PRIVATE
  346. LEVELDB_HAS_PORT_CONFIG_H=1
  347. )
  348. endif(NOT HAVE_CXX17_HAS_INCLUDE)
  349. add_test(NAME "${test_target_name}" COMMAND "${test_target_name}")
  350. endfunction(leveldb_test)
  351. leveldb_test("db/c_test.c")
  352. if(NOT BUILD_SHARED_LIBS)
  353. # TODO(costan): This test also uses
  354. # "util/env_{posix|windows}_test_helper.h"
  355. if (WIN32)
  356. leveldb_test("util/env_windows_test.cc")
  357. else (WIN32)
  358. leveldb_test("util/env_posix_test.cc")
  359. endif (WIN32)
  360. endif(NOT BUILD_SHARED_LIBS)
  361. endif(LEVELDB_BUILD_TESTS)
  362. if(LEVELDB_BUILD_BENCHMARKS)
  363. # This project uses Google benchmark for benchmarking.
  364. set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "" FORCE)
  365. set(BENCHMARK_ENABLE_EXCEPTIONS OFF CACHE BOOL "" FORCE)
  366. add_subdirectory("third_party/benchmark")
  367. function(leveldb_benchmark bench_file)
  368. get_filename_component(bench_target_name "${bench_file}" NAME_WE)
  369. add_executable("${bench_target_name}" "")
  370. target_sources("${bench_target_name}"
  371. PRIVATE
  372. "${PROJECT_BINARY_DIR}/${LEVELDB_PORT_CONFIG_DIR}/port_config.h"
  373. "util/histogram.cc"
  374. "util/histogram.h"
  375. "util/testutil.cc"
  376. "util/testutil.h"
  377. "${bench_file}"
  378. )
  379. target_link_libraries("${bench_target_name}" leveldb gmock gtest benchmark)
  380. target_compile_definitions("${bench_target_name}"
  381. PRIVATE
  382. ${LEVELDB_PLATFORM_NAME}=1
  383. )
  384. if (NOT HAVE_CXX17_HAS_INCLUDE)
  385. target_compile_definitions("${bench_target_name}"
  386. PRIVATE
  387. LEVELDB_HAS_PORT_CONFIG_H=1
  388. )
  389. endif(NOT HAVE_CXX17_HAS_INCLUDE)
  390. endfunction(leveldb_benchmark)
  391. if(NOT BUILD_SHARED_LIBS)
  392. leveldb_benchmark("benchmarks/db_bench.cc")
  393. endif(NOT BUILD_SHARED_LIBS)
  394. check_library_exists(sqlite3 sqlite3_open "" HAVE_SQLITE3)
  395. if(HAVE_SQLITE3)
  396. leveldb_benchmark("benchmarks/db_bench_sqlite3.cc")
  397. target_link_libraries(db_bench_sqlite3 sqlite3)
  398. endif(HAVE_SQLITE3)
  399. # check_library_exists is insufficient here because the library names have
  400. # different manglings when compiled with clang or gcc, at least when installed
  401. # with Homebrew on Mac.
  402. set(OLD_CMAKE_REQURED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  403. list(APPEND CMAKE_REQUIRED_LIBRARIES kyotocabinet)
  404. check_cxx_source_compiles("
  405. #include <kcpolydb.h>
  406. int main() {
  407. kyotocabinet::TreeDB* db = new kyotocabinet::TreeDB();
  408. delete db;
  409. return 0;
  410. }
  411. " HAVE_KYOTOCABINET)
  412. set(CMAKE_REQUIRED_LIBRARIES ${OLD_CMAKE_REQURED_LIBRARIES})
  413. if(HAVE_KYOTOCABINET)
  414. leveldb_benchmark("benchmarks/db_bench_tree_db.cc")
  415. target_link_libraries(db_bench_tree_db kyotocabinet)
  416. endif(HAVE_KYOTOCABINET)
  417. endif(LEVELDB_BUILD_BENCHMARKS)
  418. if(LEVELDB_INSTALL)
  419. install(TARGETS leveldb
  420. EXPORT leveldbTargets
  421. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  422. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  423. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  424. )
  425. install(
  426. FILES
  427. "${LEVELDB_PUBLIC_INCLUDE_DIR}/c.h"
  428. "${LEVELDB_PUBLIC_INCLUDE_DIR}/cache.h"
  429. "${LEVELDB_PUBLIC_INCLUDE_DIR}/comparator.h"
  430. "${LEVELDB_PUBLIC_INCLUDE_DIR}/db.h"
  431. "${LEVELDB_PUBLIC_INCLUDE_DIR}/dumpfile.h"
  432. "${LEVELDB_PUBLIC_INCLUDE_DIR}/env.h"
  433. "${LEVELDB_PUBLIC_INCLUDE_DIR}/export.h"
  434. "${LEVELDB_PUBLIC_INCLUDE_DIR}/filter_policy.h"
  435. "${LEVELDB_PUBLIC_INCLUDE_DIR}/iterator.h"
  436. "${LEVELDB_PUBLIC_INCLUDE_DIR}/options.h"
  437. "${LEVELDB_PUBLIC_INCLUDE_DIR}/slice.h"
  438. "${LEVELDB_PUBLIC_INCLUDE_DIR}/status.h"
  439. "${LEVELDB_PUBLIC_INCLUDE_DIR}/table_builder.h"
  440. "${LEVELDB_PUBLIC_INCLUDE_DIR}/table.h"
  441. "${LEVELDB_PUBLIC_INCLUDE_DIR}/write_batch.h"
  442. DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/leveldb"
  443. )
  444. include(CMakePackageConfigHelpers)
  445. configure_package_config_file(
  446. "cmake/${PROJECT_NAME}Config.cmake.in"
  447. "${PROJECT_BINARY_DIR}/cmake/${PROJECT_NAME}Config.cmake"
  448. INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
  449. )
  450. write_basic_package_version_file(
  451. "${PROJECT_BINARY_DIR}/cmake/${PROJECT_NAME}ConfigVersion.cmake"
  452. COMPATIBILITY SameMajorVersion
  453. )
  454. install(
  455. EXPORT leveldbTargets
  456. NAMESPACE leveldb::
  457. DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
  458. )
  459. install(
  460. FILES
  461. "${PROJECT_BINARY_DIR}/cmake/${PROJECT_NAME}Config.cmake"
  462. "${PROJECT_BINARY_DIR}/cmake/${PROJECT_NAME}ConfigVersion.cmake"
  463. DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
  464. )
  465. endif(LEVELDB_INSTALL)
  466. add_executable(db_test2
  467. "${PROJECT_SOURCE_DIR}/test/db_test2.cc"
  468. )
  469. target_link_libraries(db_test2 PRIVATE leveldb)
  470. add_executable(ttl_mmtable_test
  471. "${PROJECT_SOURCE_DIR}/test/ttl_mmtable_test.cc"
  472. )
  473. target_link_libraries(ttl_mmtable_test PRIVATE leveldb)
  474. add_executable(ttl_test
  475. "${PROJECT_SOURCE_DIR}/test/ttl_test.cc"
  476. )
  477. target_link_libraries(ttl_test PRIVATE leveldb gtest)