小组成员:谢瑞阳、徐翔宇
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.

463 line
17 KiB

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
6 年之前
Fix snapshot compaction bug Closes google/leveldb#320 During compaction it was possible that records from a block b1=(l1,u1) would be pushed down from level i to level i+1. If there is a block b2=(l2,u2) at level i with k1 = user_key(u1) = user_key(l2) then a subsequent search for k1 will yield the record l2 which has a smaller sequence number than u1 because the sort order for records sorts increasing by user key but decreaing by sequence number. This change add a call to a new function AddBoundaryInputs to SetupOtherInputs. AddBoundaryInputs searches for a block b2 matching the criteria above and adds it to the set of files to be compacted. Whenever AddBoundaryInputs is called it is important that the compaction fileset in level i+1 (known as c->inputs_[1] in the code) be recomputed. Each call to AddBoundaryInputs is followed by a call to GetOverlappingInputs. SetupOtherInputs is called on both manual and automated compaction passes. It is called for both level zero and for levels greater than 0. The original change posted in https://github.com/google/leveldb/pull/339 has been modified to also include changed made by Chris Mumford<cmumford@google.com> in https://github.com/cmumford/leveldb/commit/4b72cb14f8da2aab12451c24b8e205aff686e9dc 1. Releasing snapshots during test cleanup to avoid memory leak warnings. 2. Refactored test to use testutil.h to be in line with other issue tests and to create the test database in the correct temporary location. 3. Added copyright banner. Otherwise, just minor formatting and limiting character width to 80 characters. Additionally the change was rebased on top of current master and changes previously made to the Makefile were ported to the CMakeLists.txt. Testing Done: A test program (issue320_test) was constructed that performs mutations while snapshots are active. issue320_test fails without this bug fix after 64k writes. It passes with this bug fix. It was run with 200M writes and passed. Unit tests were written for the new function that was added to the code. Make test was run and seen to pass. Signed-off-by: Richard Cole <richcole@amazon.com>
9 年之前
Add move constructor to Status. This will result in smaller code generation when Status instances are passed around. Benchmarks don't indicate a significant change either way. CPU: 48 * Intel(R) Xeon(R) CPU E5-2690 v3 @ 2.60GHz CPUCache: 30720 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) Baseline: fillseq : 3.589 micros/op; 30.8 MB/s fillsync : 4165.299 micros/op; 0.0 MB/s (1000 ops) fillrandom : 5.864 micros/op; 18.9 MB/s overwrite : 7.830 micros/op; 14.1 MB/s readrandom : 5.534 micros/op; (1000000 of 1000000 found) readrandom : 4.292 micros/op; (1000000 of 1000000 found) readseq : 0.312 micros/op; 354.1 MB/s readreverse : 0.501 micros/op; 220.8 MB/s compact : 886211.000 micros/op; readrandom : 3.518 micros/op; (1000000 of 1000000 found) readseq : 0.251 micros/op; 441.2 MB/s readreverse : 0.456 micros/op; 242.4 MB/s fill100K : 1329.723 micros/op; 71.7 MB/s (1000 ops) crc32c : 1.976 micros/op; 1976.7 MB/s (4K per op) snappycomp : 4.705 micros/op; 830.2 MB/s (output: 55.1%) snappyuncomp : 0.958 micros/op; 4079.1 MB/s acquireload : 0.727 micros/op; (each op is 1000 loads) New: fillseq : 3.129 micros/op; 35.4 MB/s fillsync : 2748.099 micros/op; 0.0 MB/s (1000 ops) fillrandom : 5.394 micros/op; 20.5 MB/s overwrite : 7.253 micros/op; 15.3 MB/s readrandom : 5.655 micros/op; (1000000 of 1000000 found) readrandom : 4.425 micros/op; (1000000 of 1000000 found) readseq : 0.298 micros/op; 371.3 MB/s readreverse : 0.508 micros/op; 217.9 MB/s compact : 885842.000 micros/op; readrandom : 3.545 micros/op; (1000000 of 1000000 found) readseq : 0.252 micros/op; 438.2 MB/s readreverse : 0.425 micros/op; 260.2 MB/s fill100K : 1418.347 micros/op; 67.2 MB/s (1000 ops) crc32c : 1.987 micros/op; 1966.0 MB/s (4K per op) snappycomp : 4.767 micros/op; 819.4 MB/s (output: 55.1%) snappyuncomp : 0.916 micros/op; 4264.9 MB/s acquireload : 0.665 micros/op; (each op is 1000 loads) ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=194002392
6 年之前
  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.22.0 LANGUAGES C CXX)
  7. # This project can use C11, but will gracefully decay down to C89.
  8. set(CMAKE_C_STANDARD 11)
  9. set(CMAKE_C_STANDARD_REQUIRED OFF)
  10. set(CMAKE_C_EXTENSIONS OFF)
  11. # This project requires C++11.
  12. set(CMAKE_CXX_STANDARD 11)
  13. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  14. set(CMAKE_CXX_EXTENSIONS OFF)
  15. if (WIN32)
  16. set(LEVELDB_PLATFORM_NAME LEVELDB_PLATFORM_WINDOWS)
  17. # TODO(cmumford): Make UNICODE configurable for Windows.
  18. add_definitions(-D_UNICODE -DUNICODE)
  19. else (WIN32)
  20. set(LEVELDB_PLATFORM_NAME LEVELDB_PLATFORM_POSIX)
  21. endif (WIN32)
  22. option(LEVELDB_BUILD_TESTS "Build LevelDB's unit tests" ON)
  23. option(LEVELDB_BUILD_BENCHMARKS "Build LevelDB's benchmarks" ON)
  24. option(LEVELDB_INSTALL "Install LevelDB's header and library" ON)
  25. include(TestBigEndian)
  26. test_big_endian(LEVELDB_IS_BIG_ENDIAN)
  27. include(CheckIncludeFile)
  28. check_include_file("unistd.h" HAVE_UNISTD_H)
  29. include(CheckLibraryExists)
  30. check_library_exists(crc32c crc32c_value "" HAVE_CRC32C)
  31. check_library_exists(snappy snappy_compress "" HAVE_SNAPPY)
  32. check_library_exists(tcmalloc malloc "" HAVE_TCMALLOC)
  33. include(CheckCXXSymbolExists)
  34. # Using check_cxx_symbol_exists() instead of check_c_symbol_exists() because
  35. # we're including the header from C++, and feature detection should use the same
  36. # compiler language that the project will use later. Principles aside, some
  37. # versions of do not expose fdatasync() in <unistd.h> in standard C mode
  38. # (-std=c11), but do expose the function in standard C++ mode (-std=c++11).
  39. check_cxx_symbol_exists(fdatasync "unistd.h" HAVE_FDATASYNC)
  40. check_cxx_symbol_exists(F_FULLFSYNC "fcntl.h" HAVE_FULLFSYNC)
  41. check_cxx_symbol_exists(O_CLOEXEC "fcntl.h" HAVE_O_CLOEXEC)
  42. if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
  43. # Disable C++ exceptions.
  44. string(REGEX REPLACE "/EH[a-z]+" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
  45. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHs-c-")
  46. add_definitions(-D_HAS_EXCEPTIONS=0)
  47. # Disable RTTI.
  48. string(REGEX REPLACE "/GR" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
  49. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /GR-")
  50. else(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
  51. # Enable strict prototype warnings for C code in clang and gcc.
  52. if(NOT CMAKE_C_FLAGS MATCHES "-Wstrict-prototypes")
  53. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wstrict-prototypes")
  54. endif(NOT CMAKE_C_FLAGS MATCHES "-Wstrict-prototypes")
  55. # Disable C++ exceptions.
  56. string(REGEX REPLACE "-fexceptions" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
  57. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions")
  58. # Disable RTTI.
  59. string(REGEX REPLACE "-frtti" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
  60. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti")
  61. endif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
  62. # Test whether -Wthread-safety is available. See
  63. # https://clang.llvm.org/docs/ThreadSafetyAnalysis.html
  64. include(CheckCXXCompilerFlag)
  65. check_cxx_compiler_flag(-Wthread-safety HAVE_CLANG_THREAD_SAFETY)
  66. include(CheckCXXSourceCompiles)
  67. # Test whether C++17 __has_include is available.
  68. check_cxx_source_compiles("
  69. #if defined(__has_include) && __has_include(<string>)
  70. #include <string>
  71. #endif
  72. int main() { std::string str; return 0; }
  73. " HAVE_CXX17_HAS_INCLUDE)
  74. set(LEVELDB_PUBLIC_INCLUDE_DIR "include/leveldb")
  75. set(LEVELDB_PORT_CONFIG_DIR "include/port")
  76. configure_file(
  77. "${PROJECT_SOURCE_DIR}/port/port_config.h.in"
  78. "${PROJECT_BINARY_DIR}/${LEVELDB_PORT_CONFIG_DIR}/port_config.h"
  79. )
  80. include_directories(
  81. "${PROJECT_BINARY_DIR}/include"
  82. "${PROJECT_SOURCE_DIR}"
  83. )
  84. if(BUILD_SHARED_LIBS)
  85. # Only export LEVELDB_EXPORT symbols from the shared library.
  86. add_compile_options(-fvisibility=hidden)
  87. endif(BUILD_SHARED_LIBS)
  88. add_library(leveldb "")
  89. target_sources(leveldb
  90. PRIVATE
  91. "${PROJECT_BINARY_DIR}/${LEVELDB_PORT_CONFIG_DIR}/port_config.h"
  92. "${PROJECT_SOURCE_DIR}/db/builder.cc"
  93. "${PROJECT_SOURCE_DIR}/db/builder.h"
  94. "${PROJECT_SOURCE_DIR}/db/c.cc"
  95. "${PROJECT_SOURCE_DIR}/db/db_impl.cc"
  96. "${PROJECT_SOURCE_DIR}/db/db_impl.h"
  97. "${PROJECT_SOURCE_DIR}/db/db_iter.cc"
  98. "${PROJECT_SOURCE_DIR}/db/db_iter.h"
  99. "${PROJECT_SOURCE_DIR}/db/dbformat.cc"
  100. "${PROJECT_SOURCE_DIR}/db/dbformat.h"
  101. "${PROJECT_SOURCE_DIR}/db/dumpfile.cc"
  102. "${PROJECT_SOURCE_DIR}/db/filename.cc"
  103. "${PROJECT_SOURCE_DIR}/db/filename.h"
  104. "${PROJECT_SOURCE_DIR}/db/log_format.h"
  105. "${PROJECT_SOURCE_DIR}/db/log_reader.cc"
  106. "${PROJECT_SOURCE_DIR}/db/log_reader.h"
  107. "${PROJECT_SOURCE_DIR}/db/log_writer.cc"
  108. "${PROJECT_SOURCE_DIR}/db/log_writer.h"
  109. "${PROJECT_SOURCE_DIR}/db/memtable.cc"
  110. "${PROJECT_SOURCE_DIR}/db/memtable.h"
  111. "${PROJECT_SOURCE_DIR}/db/repair.cc"
  112. "${PROJECT_SOURCE_DIR}/db/skiplist.h"
  113. "${PROJECT_SOURCE_DIR}/db/snapshot.h"
  114. "${PROJECT_SOURCE_DIR}/db/table_cache.cc"
  115. "${PROJECT_SOURCE_DIR}/db/table_cache.h"
  116. "${PROJECT_SOURCE_DIR}/db/version_edit.cc"
  117. "${PROJECT_SOURCE_DIR}/db/version_edit.h"
  118. "${PROJECT_SOURCE_DIR}/db/version_set.cc"
  119. "${PROJECT_SOURCE_DIR}/db/version_set.h"
  120. "${PROJECT_SOURCE_DIR}/db/write_batch_internal.h"
  121. "${PROJECT_SOURCE_DIR}/db/write_batch.cc"
  122. "${PROJECT_SOURCE_DIR}/port/port_stdcxx.h"
  123. "${PROJECT_SOURCE_DIR}/port/port.h"
  124. "${PROJECT_SOURCE_DIR}/port/thread_annotations.h"
  125. "${PROJECT_SOURCE_DIR}/table/block_builder.cc"
  126. "${PROJECT_SOURCE_DIR}/table/block_builder.h"
  127. "${PROJECT_SOURCE_DIR}/table/block.cc"
  128. "${PROJECT_SOURCE_DIR}/table/block.h"
  129. "${PROJECT_SOURCE_DIR}/table/filter_block.cc"
  130. "${PROJECT_SOURCE_DIR}/table/filter_block.h"
  131. "${PROJECT_SOURCE_DIR}/table/format.cc"
  132. "${PROJECT_SOURCE_DIR}/table/format.h"
  133. "${PROJECT_SOURCE_DIR}/table/iterator_wrapper.h"
  134. "${PROJECT_SOURCE_DIR}/table/iterator.cc"
  135. "${PROJECT_SOURCE_DIR}/table/merger.cc"
  136. "${PROJECT_SOURCE_DIR}/table/merger.h"
  137. "${PROJECT_SOURCE_DIR}/table/table_builder.cc"
  138. "${PROJECT_SOURCE_DIR}/table/table.cc"
  139. "${PROJECT_SOURCE_DIR}/table/two_level_iterator.cc"
  140. "${PROJECT_SOURCE_DIR}/table/two_level_iterator.h"
  141. "${PROJECT_SOURCE_DIR}/util/arena.cc"
  142. "${PROJECT_SOURCE_DIR}/util/arena.h"
  143. "${PROJECT_SOURCE_DIR}/util/bloom.cc"
  144. "${PROJECT_SOURCE_DIR}/util/cache.cc"
  145. "${PROJECT_SOURCE_DIR}/util/coding.cc"
  146. "${PROJECT_SOURCE_DIR}/util/coding.h"
  147. "${PROJECT_SOURCE_DIR}/util/comparator.cc"
  148. "${PROJECT_SOURCE_DIR}/util/crc32c.cc"
  149. "${PROJECT_SOURCE_DIR}/util/crc32c.h"
  150. "${PROJECT_SOURCE_DIR}/util/env.cc"
  151. "${PROJECT_SOURCE_DIR}/util/filter_policy.cc"
  152. "${PROJECT_SOURCE_DIR}/util/hash.cc"
  153. "${PROJECT_SOURCE_DIR}/util/hash.h"
  154. "${PROJECT_SOURCE_DIR}/util/logging.cc"
  155. "${PROJECT_SOURCE_DIR}/util/logging.h"
  156. "${PROJECT_SOURCE_DIR}/util/mutexlock.h"
  157. "${PROJECT_SOURCE_DIR}/util/no_destructor.h"
  158. "${PROJECT_SOURCE_DIR}/util/options.cc"
  159. "${PROJECT_SOURCE_DIR}/util/random.h"
  160. "${PROJECT_SOURCE_DIR}/util/status.cc"
  161. # Only CMake 3.3+ supports PUBLIC sources in targets exported by "install".
  162. $<$<VERSION_GREATER:CMAKE_VERSION,3.2>:PUBLIC>
  163. "${LEVELDB_PUBLIC_INCLUDE_DIR}/c.h"
  164. "${LEVELDB_PUBLIC_INCLUDE_DIR}/cache.h"
  165. "${LEVELDB_PUBLIC_INCLUDE_DIR}/comparator.h"
  166. "${LEVELDB_PUBLIC_INCLUDE_DIR}/db.h"
  167. "${LEVELDB_PUBLIC_INCLUDE_DIR}/dumpfile.h"
  168. "${LEVELDB_PUBLIC_INCLUDE_DIR}/env.h"
  169. "${LEVELDB_PUBLIC_INCLUDE_DIR}/export.h"
  170. "${LEVELDB_PUBLIC_INCLUDE_DIR}/filter_policy.h"
  171. "${LEVELDB_PUBLIC_INCLUDE_DIR}/iterator.h"
  172. "${LEVELDB_PUBLIC_INCLUDE_DIR}/options.h"
  173. "${LEVELDB_PUBLIC_INCLUDE_DIR}/slice.h"
  174. "${LEVELDB_PUBLIC_INCLUDE_DIR}/status.h"
  175. "${LEVELDB_PUBLIC_INCLUDE_DIR}/table_builder.h"
  176. "${LEVELDB_PUBLIC_INCLUDE_DIR}/table.h"
  177. "${LEVELDB_PUBLIC_INCLUDE_DIR}/write_batch.h"
  178. )
  179. if (WIN32)
  180. target_sources(leveldb
  181. PRIVATE
  182. "${PROJECT_SOURCE_DIR}/util/env_windows.cc"
  183. "${PROJECT_SOURCE_DIR}/util/windows_logger.h"
  184. )
  185. else (WIN32)
  186. target_sources(leveldb
  187. PRIVATE
  188. "${PROJECT_SOURCE_DIR}/util/env_posix.cc"
  189. "${PROJECT_SOURCE_DIR}/util/posix_logger.h"
  190. )
  191. endif (WIN32)
  192. # MemEnv is not part of the interface and could be pulled to a separate library.
  193. target_sources(leveldb
  194. PRIVATE
  195. "${PROJECT_SOURCE_DIR}/helpers/memenv/memenv.cc"
  196. "${PROJECT_SOURCE_DIR}/helpers/memenv/memenv.h"
  197. )
  198. target_include_directories(leveldb
  199. PUBLIC
  200. $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
  201. $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
  202. )
  203. set_target_properties(leveldb
  204. PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR})
  205. target_compile_definitions(leveldb
  206. PRIVATE
  207. # Used by include/export.h when building shared libraries.
  208. LEVELDB_COMPILE_LIBRARY
  209. # Used by port/port.h.
  210. ${LEVELDB_PLATFORM_NAME}=1
  211. )
  212. if (NOT HAVE_CXX17_HAS_INCLUDE)
  213. target_compile_definitions(leveldb
  214. PRIVATE
  215. LEVELDB_HAS_PORT_CONFIG_H=1
  216. )
  217. endif(NOT HAVE_CXX17_HAS_INCLUDE)
  218. if(BUILD_SHARED_LIBS)
  219. target_compile_definitions(leveldb
  220. PUBLIC
  221. # Used by include/export.h.
  222. LEVELDB_SHARED_LIBRARY
  223. )
  224. endif(BUILD_SHARED_LIBS)
  225. if(HAVE_CLANG_THREAD_SAFETY)
  226. target_compile_options(leveldb
  227. PUBLIC
  228. -Werror -Wthread-safety)
  229. endif(HAVE_CLANG_THREAD_SAFETY)
  230. if(HAVE_CRC32C)
  231. target_link_libraries(leveldb crc32c)
  232. endif(HAVE_CRC32C)
  233. if(HAVE_SNAPPY)
  234. target_link_libraries(leveldb snappy)
  235. endif(HAVE_SNAPPY)
  236. if(HAVE_TCMALLOC)
  237. target_link_libraries(leveldb tcmalloc)
  238. endif(HAVE_TCMALLOC)
  239. # Needed by port_stdcxx.h
  240. find_package(Threads REQUIRED)
  241. target_link_libraries(leveldb Threads::Threads)
  242. add_executable(leveldbutil
  243. "${PROJECT_SOURCE_DIR}/db/leveldbutil.cc"
  244. )
  245. target_link_libraries(leveldbutil leveldb)
  246. if(LEVELDB_BUILD_TESTS)
  247. enable_testing()
  248. function(leveldb_test test_file)
  249. get_filename_component(test_target_name "${test_file}" NAME_WE)
  250. add_executable("${test_target_name}" "")
  251. target_sources("${test_target_name}"
  252. PRIVATE
  253. "${PROJECT_BINARY_DIR}/${LEVELDB_PORT_CONFIG_DIR}/port_config.h"
  254. "${PROJECT_SOURCE_DIR}/util/testharness.cc"
  255. "${PROJECT_SOURCE_DIR}/util/testharness.h"
  256. "${PROJECT_SOURCE_DIR}/util/testutil.cc"
  257. "${PROJECT_SOURCE_DIR}/util/testutil.h"
  258. "${test_file}"
  259. )
  260. target_link_libraries("${test_target_name}" leveldb)
  261. target_compile_definitions("${test_target_name}"
  262. PRIVATE
  263. ${LEVELDB_PLATFORM_NAME}=1
  264. )
  265. if (NOT HAVE_CXX17_HAS_INCLUDE)
  266. target_compile_definitions("${test_target_name}"
  267. PRIVATE
  268. LEVELDB_HAS_PORT_CONFIG_H=1
  269. )
  270. endif(NOT HAVE_CXX17_HAS_INCLUDE)
  271. add_test(NAME "${test_target_name}" COMMAND "${test_target_name}")
  272. endfunction(leveldb_test)
  273. leveldb_test("${PROJECT_SOURCE_DIR}/db/c_test.c")
  274. leveldb_test("${PROJECT_SOURCE_DIR}/db/fault_injection_test.cc")
  275. leveldb_test("${PROJECT_SOURCE_DIR}/issues/issue178_test.cc")
  276. leveldb_test("${PROJECT_SOURCE_DIR}/issues/issue200_test.cc")
  277. leveldb_test("${PROJECT_SOURCE_DIR}/issues/issue320_test.cc")
  278. leveldb_test("${PROJECT_SOURCE_DIR}/util/env_test.cc")
  279. leveldb_test("${PROJECT_SOURCE_DIR}/util/status_test.cc")
  280. leveldb_test("${PROJECT_SOURCE_DIR}/util/no_destructor_test.cc")
  281. if(NOT BUILD_SHARED_LIBS)
  282. leveldb_test("${PROJECT_SOURCE_DIR}/db/autocompact_test.cc")
  283. leveldb_test("${PROJECT_SOURCE_DIR}/db/corruption_test.cc")
  284. leveldb_test("${PROJECT_SOURCE_DIR}/db/db_test.cc")
  285. leveldb_test("${PROJECT_SOURCE_DIR}/db/dbformat_test.cc")
  286. leveldb_test("${PROJECT_SOURCE_DIR}/db/filename_test.cc")
  287. leveldb_test("${PROJECT_SOURCE_DIR}/db/log_test.cc")
  288. leveldb_test("${PROJECT_SOURCE_DIR}/db/recovery_test.cc")
  289. leveldb_test("${PROJECT_SOURCE_DIR}/db/skiplist_test.cc")
  290. leveldb_test("${PROJECT_SOURCE_DIR}/db/version_edit_test.cc")
  291. leveldb_test("${PROJECT_SOURCE_DIR}/db/version_set_test.cc")
  292. leveldb_test("${PROJECT_SOURCE_DIR}/db/write_batch_test.cc")
  293. leveldb_test("${PROJECT_SOURCE_DIR}/helpers/memenv/memenv_test.cc")
  294. leveldb_test("${PROJECT_SOURCE_DIR}/table/filter_block_test.cc")
  295. leveldb_test("${PROJECT_SOURCE_DIR}/table/table_test.cc")
  296. leveldb_test("${PROJECT_SOURCE_DIR}/util/arena_test.cc")
  297. leveldb_test("${PROJECT_SOURCE_DIR}/util/bloom_test.cc")
  298. leveldb_test("${PROJECT_SOURCE_DIR}/util/cache_test.cc")
  299. leveldb_test("${PROJECT_SOURCE_DIR}/util/coding_test.cc")
  300. leveldb_test("${PROJECT_SOURCE_DIR}/util/crc32c_test.cc")
  301. leveldb_test("${PROJECT_SOURCE_DIR}/util/hash_test.cc")
  302. leveldb_test("${PROJECT_SOURCE_DIR}/util/logging_test.cc")
  303. # TODO(costan): This test also uses
  304. # "${PROJECT_SOURCE_DIR}/util/env_{posix|windows}_test_helper.h"
  305. if (WIN32)
  306. leveldb_test("${PROJECT_SOURCE_DIR}/util/env_windows_test.cc")
  307. else (WIN32)
  308. leveldb_test("${PROJECT_SOURCE_DIR}/util/env_posix_test.cc")
  309. endif (WIN32)
  310. endif(NOT BUILD_SHARED_LIBS)
  311. endif(LEVELDB_BUILD_TESTS)
  312. if(LEVELDB_BUILD_BENCHMARKS)
  313. function(leveldb_benchmark bench_file)
  314. get_filename_component(bench_target_name "${bench_file}" NAME_WE)
  315. add_executable("${bench_target_name}" "")
  316. target_sources("${bench_target_name}"
  317. PRIVATE
  318. "${PROJECT_BINARY_DIR}/${LEVELDB_PORT_CONFIG_DIR}/port_config.h"
  319. "${PROJECT_SOURCE_DIR}/util/histogram.cc"
  320. "${PROJECT_SOURCE_DIR}/util/histogram.h"
  321. "${PROJECT_SOURCE_DIR}/util/testharness.cc"
  322. "${PROJECT_SOURCE_DIR}/util/testharness.h"
  323. "${PROJECT_SOURCE_DIR}/util/testutil.cc"
  324. "${PROJECT_SOURCE_DIR}/util/testutil.h"
  325. "${bench_file}"
  326. )
  327. target_link_libraries("${bench_target_name}" leveldb)
  328. target_compile_definitions("${bench_target_name}"
  329. PRIVATE
  330. ${LEVELDB_PLATFORM_NAME}=1
  331. )
  332. if (NOT HAVE_CXX17_HAS_INCLUDE)
  333. target_compile_definitions("${bench_target_name}"
  334. PRIVATE
  335. LEVELDB_HAS_PORT_CONFIG_H=1
  336. )
  337. endif(NOT HAVE_CXX17_HAS_INCLUDE)
  338. endfunction(leveldb_benchmark)
  339. if(NOT BUILD_SHARED_LIBS)
  340. leveldb_benchmark("${PROJECT_SOURCE_DIR}/benchmarks/db_bench.cc")
  341. endif(NOT BUILD_SHARED_LIBS)
  342. check_library_exists(sqlite3 sqlite3_open "" HAVE_SQLITE3)
  343. if(HAVE_SQLITE3)
  344. leveldb_benchmark("${PROJECT_SOURCE_DIR}/benchmarks/db_bench_sqlite3.cc")
  345. target_link_libraries(db_bench_sqlite3 sqlite3)
  346. endif(HAVE_SQLITE3)
  347. # check_library_exists is insufficient here because the library names have
  348. # different manglings when compiled with clang or gcc, at least when installed
  349. # with Homebrew on Mac.
  350. set(OLD_CMAKE_REQURED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  351. list(APPEND CMAKE_REQUIRED_LIBRARIES kyotocabinet)
  352. check_cxx_source_compiles("
  353. #include <kcpolydb.h>
  354. int main() {
  355. kyotocabinet::TreeDB* db = new kyotocabinet::TreeDB();
  356. delete db;
  357. return 0;
  358. }
  359. " HAVE_KYOTOCABINET)
  360. set(CMAKE_REQUIRED_LIBRARIES ${OLD_CMAKE_REQURED_LIBRARIES})
  361. if(HAVE_KYOTOCABINET)
  362. leveldb_benchmark("${PROJECT_SOURCE_DIR}/benchmarks/db_bench_tree_db.cc")
  363. target_link_libraries(db_bench_tree_db kyotocabinet)
  364. endif(HAVE_KYOTOCABINET)
  365. endif(LEVELDB_BUILD_BENCHMARKS)
  366. if(LEVELDB_INSTALL)
  367. include(GNUInstallDirs)
  368. install(TARGETS leveldb
  369. EXPORT leveldbTargets
  370. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  371. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  372. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  373. )
  374. install(
  375. FILES
  376. "${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/c.h"
  377. "${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/cache.h"
  378. "${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/comparator.h"
  379. "${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/db.h"
  380. "${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/dumpfile.h"
  381. "${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/env.h"
  382. "${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/export.h"
  383. "${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/filter_policy.h"
  384. "${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/iterator.h"
  385. "${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/options.h"
  386. "${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/slice.h"
  387. "${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/status.h"
  388. "${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/table_builder.h"
  389. "${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/table.h"
  390. "${PROJECT_SOURCE_DIR}/${LEVELDB_PUBLIC_INCLUDE_DIR}/write_batch.h"
  391. DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/leveldb
  392. )
  393. include(CMakePackageConfigHelpers)
  394. write_basic_package_version_file(
  395. "${PROJECT_BINARY_DIR}/leveldbConfigVersion.cmake"
  396. COMPATIBILITY SameMajorVersion
  397. )
  398. install(
  399. EXPORT leveldbTargets
  400. NAMESPACE leveldb::
  401. DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/leveldb"
  402. )
  403. install(
  404. FILES
  405. "${PROJECT_SOURCE_DIR}/cmake/leveldbConfig.cmake"
  406. "${PROJECT_BINARY_DIR}/leveldbConfigVersion.cmake"
  407. DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/leveldb"
  408. )
  409. endif(LEVELDB_INSTALL)