diff --git a/README.md b/README.md index d21a517..bc93154 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,8 @@ Original nim LevelDB wrapper: [HERE](https://github.com/zielmicha/leveldb.nim) Replacing of system library dependency with self-contained C/CPP interoperability by (Codex.Storage)[https://codex.storage] +## Usage + Create a database: ```Nim import leveldbstatic @@ -49,3 +51,25 @@ Iterate over subset of database content: db.close() ``` + +## Compiling with optimizations + +CMake is used during compilation to determine which of the following optimization options are enabled. You can set the following nim compiler flags to 0 or 1 to override them: + - fdatasync from `--passC:-DHAVE_FDATASYNC=1` + - F_FULLSYNC from `--passC:-DHAVE_FULLFSYNC=1` + - O_CLOEXEC from `--passC:-DHAVE_O_CLOEXEC=1` + - crc32c from `--passC:-DHAVE_CRC32C=1` + - snappy from `--passC:-DHAVE_SNAPPY=1` + - zstd from `--passC:-DHAVE_ZSTD=1` + +## Updating + +When you want to update this library to a new version of LevelDB, follow these steps: +- Update LevelDB submodule to new version. +- Run 'build.sh'. +- Run 'nimble build' and 'nimble test'. +- Make sure everything's working. +- Increment version of this library in 'leveldbstatic.nimble'. +- Commit the changes. +- Tag the commit with the new version number. +- Push. diff --git a/build.sh b/build.sh index 55bbe9c..a02fcd1 100755 --- a/build.sh +++ b/build.sh @@ -47,10 +47,8 @@ toast \ --includeDirs="${sourceDir}/helpers/memenv" \ --includeDirs="${sourceDir}/port" \ --includeDirs="${sourceDir}/include" \ - --includeDirs="${buildDir}/include" \ "${sourceDir}/include/leveldb/c.h" >> "${output}" sed -i 's/\bpassC\b/passc/g' "${output}" sed -i 's/{\.compile\:\ \"\./{\.compile\:\ root\ \&\ \"/g' "${output}" sed -i 's/{\.passc\:\ \"-I\./{\.passc\:\ \"-I\"\ \&\ root\ \&\ \"/g' "${output}" - diff --git a/build/include/port/port_config.h b/build/include/port/port_config.h deleted file mode 100644 index 0e80eda..0000000 --- a/build/include/port/port_config.h +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright 2017 The LevelDB Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. See the AUTHORS file for names of contributors. - -#ifndef STORAGE_LEVELDB_PORT_PORT_CONFIG_H_ -#define STORAGE_LEVELDB_PORT_PORT_CONFIG_H_ - -// Define to 1 if you have a definition for fdatasync() in . -#if !defined(HAVE_FDATASYNC) -#define HAVE_FDATASYNC 1 -#endif // !defined(HAVE_FDATASYNC) - -// Define to 1 if you have a definition for F_FULLFSYNC in . -#if !defined(HAVE_FULLFSYNC) -#define HAVE_FULLFSYNC 0 -#endif // !defined(HAVE_FULLFSYNC) - -// Define to 1 if you have a definition for O_CLOEXEC in . -#if !defined(HAVE_O_CLOEXEC) -#define HAVE_O_CLOEXEC 1 -#endif // !defined(HAVE_O_CLOEXEC) - -// Define to 1 if you have Google CRC32C. -#if !defined(HAVE_CRC32C) -#define HAVE_CRC32C 0 -#endif // !defined(HAVE_CRC32C) - -// Define to 1 if you have Google Snappy. -#if !defined(HAVE_SNAPPY) -#define HAVE_SNAPPY 0 -#endif // !defined(HAVE_SNAPPY) - -// Define to 1 if you have Zstd. -#if !defined(HAVE_Zstd) -#define HAVE_ZSTD 0 -#endif // !defined(HAVE_ZSTD) - -#endif // STORAGE_LEVELDB_PORT_PORT_CONFIG_H_ diff --git a/leveldbstatic/prelude.nim b/leveldbstatic/prelude.nim index 82223b5..ee10adf 100644 --- a/leveldbstatic/prelude.nim +++ b/leveldbstatic/prelude.nim @@ -1,8 +1,37 @@ import os -const root = currentSourcePath.parentDir.parentDir -const envWindows = root/"vendor"/"util"/"env_windows.cc" -const envPosix = root/"vendor"/"util"/"env_posix.cc" +const + root = currentSourcePath.parentDir.parentDir + envWindows = root/"vendor"/"util"/"env_windows.cc" + envPosix = root/"vendor"/"util"/"env_posix.cc" + + LevelDbCMakeFlags {.strdefine.} = + when defined(macosx): + "-DCMAKE_BUILD_TYPE=Release" + elif defined(windows): + "-G\"MSYS Makefiles\" -DCMAKE_BUILD_TYPE=Release" + else: + "-DCMAKE_BUILD_TYPE=Release" + + LevelDbDir {.strdefine.} = $(root/"vendor") + buildDir = $(root/"build") + +static: + echo "Initializing submodule..." + discard gorge "git submodule deinit -f \"" & root & "\"" + discard gorge "git submodule update --init --recursive --checkout \"" & root & "\"" + + echo "\nClean dir: \"" & buildDir & "\"" + discard gorge "rm -rf " & buildDir + discard gorge "mkdir -p " & buildDir + + let cmd = "cmake -S \"" & LevelDbDir & "\" -B \"" & buildDir & "\" " & LevelDbCmakeFlags + echo "\nBuilding LevelDB: " & cmd + let (output, exitCode) = gorgeEx cmd + if exitCode != 0: + discard gorge "rm -rf " & buildDir + echo output + raise (ref Defect)(msg: "Failed to build LevelDB") when defined(windows): {.compile: envWindows.} @@ -13,13 +42,3 @@ when defined(windows): when defined(posix): {.compile: envPosix.} {.passc: "-DLEVELDB_PLATFORM_POSIX".} - - -{.passc: "-DHAVE_FDATASYNC=0".} -{.passc: "-DHAVE_FULLFSYNC=0".} -{.passc: "-DHAVE_O_CLOEXEC=0".} -{.passc: "-DHAVE_CRC32C=0".} -{.passc: "-DHAVE_SNAPPY=0".} -{.passc: "-DHAVE_ZSTD=0".} -{.passc: "-DHAVE_Zstd=0".} - diff --git a/leveldbstatic/raw.nim b/leveldbstatic/raw.nim index 2a95fb4..b7fd307 100644 --- a/leveldbstatic/raw.nim +++ b/leveldbstatic/raw.nim @@ -1,8 +1,37 @@ import os -const root = currentSourcePath.parentDir.parentDir -const envWindows = root/"vendor"/"util"/"env_windows.cc" -const envPosix = root/"vendor"/"util"/"env_posix.cc" +const + root = currentSourcePath.parentDir.parentDir + envWindows = root/"vendor"/"util"/"env_windows.cc" + envPosix = root/"vendor"/"util"/"env_posix.cc" + + LevelDbCMakeFlags {.strdefine.} = + when defined(macosx): + "-DCMAKE_BUILD_TYPE=Release" + elif defined(windows): + "-G\"MSYS Makefiles\" -DCMAKE_BUILD_TYPE=Release" + else: + "-DCMAKE_BUILD_TYPE=Release" + + LevelDbDir {.strdefine.} = $(root/"vendor") + buildDir = $(root/"build") + +static: + echo "Initializing submodule..." + discard gorge "git submodule deinit -f \"" & root & "\"" + discard gorge "git submodule update --init --recursive --checkout \"" & root & "\"" + + echo "\nClean dir: \"" & buildDir & "\"" + discard gorge "rm -rf " & buildDir + discard gorge "mkdir -p " & buildDir + + let cmd = "cmake -S \"" & LevelDbDir & "\" -B \"" & buildDir & "\" " & LevelDbCmakeFlags + echo "\nBuilding LevelDB: " & cmd + let (output, exitCode) = gorgeEx cmd + if exitCode != 0: + discard gorge "rm -rf " & buildDir + echo output + raise (ref Defect)(msg: "Failed to build LevelDB") when defined(windows): {.compile: envWindows.} @@ -14,19 +43,9 @@ when defined(posix): {.compile: envPosix.} {.passc: "-DLEVELDB_PLATFORM_POSIX".} - -{.passc: "-DHAVE_FDATASYNC=0".} -{.passc: "-DHAVE_FULLFSYNC=0".} -{.passc: "-DHAVE_O_CLOEXEC=0".} -{.passc: "-DHAVE_CRC32C=0".} -{.passc: "-DHAVE_SNAPPY=0".} -{.passc: "-DHAVE_ZSTD=0".} -{.passc: "-DHAVE_Zstd=0".} - - -# Generated @ 2024-05-13T12:00:58+02:00 +# Generated @ 2024-05-22T10:00:37+02:00 # Command line: -# /home/ben/.nimble/pkgs/nimterop-0.6.13/nimterop/toast --compile=./vendor/db/log_writer.cc --compile=./vendor/db/db_impl.cc --compile=./vendor/db/db_iter.cc --compile=./vendor/db/dumpfile.cc --compile=./vendor/db/c.cc --compile=./vendor/db/builder.cc --compile=./vendor/db/filename.cc --compile=./vendor/db/write_batch.cc --compile=./vendor/db/table_cache.cc --compile=./vendor/db/version_edit.cc --compile=./vendor/db/dbformat.cc --compile=./vendor/db/log_reader.cc --compile=./vendor/db/memtable.cc --compile=./vendor/db/version_set.cc --compile=./vendor/db/repair.cc --compile=./vendor/table/block.cc --compile=./vendor/table/two_level_iterator.cc --compile=./vendor/table/table_builder.cc --compile=./vendor/table/iterator.cc --compile=./vendor/table/block_builder.cc --compile=./vendor/table/merger.cc --compile=./vendor/table/format.cc --compile=./vendor/table/filter_block.cc --compile=./vendor/table/table.cc --compile=./vendor/util/hash.cc --compile=./vendor/util/arena.cc --compile=./vendor/util/options.cc --compile=./vendor/util/histogram.cc --compile=./vendor/util/crc32c.cc --compile=./vendor/util/env.cc --compile=./vendor/util/filter_policy.cc --compile=./vendor/util/bloom.cc --compile=./vendor/util/logging.cc --compile=./vendor/util/coding.cc --compile=./vendor/util/status.cc --compile=./vendor/util/cache.cc --compile=./vendor/util/comparator.cc --compile=./vendor/helpers/memenv/memenv.cc --pnim --preprocess --noHeader --includeDirs=./vendor --includeDirs=./vendor/helpers --includeDirs=./vendor/helpers/memenv --includeDirs=./vendor/port --includeDirs=./vendor/include --includeDirs=./build/include ./vendor/include/leveldb/c.h +# /home/ben/.nimble/pkgs/nimterop-0.6.13/nimterop/toast --compile=./vendor/db/log_writer.cc --compile=./vendor/db/db_impl.cc --compile=./vendor/db/db_iter.cc --compile=./vendor/db/dumpfile.cc --compile=./vendor/db/c.cc --compile=./vendor/db/builder.cc --compile=./vendor/db/filename.cc --compile=./vendor/db/write_batch.cc --compile=./vendor/db/table_cache.cc --compile=./vendor/db/version_edit.cc --compile=./vendor/db/dbformat.cc --compile=./vendor/db/log_reader.cc --compile=./vendor/db/memtable.cc --compile=./vendor/db/version_set.cc --compile=./vendor/db/repair.cc --compile=./vendor/table/block.cc --compile=./vendor/table/two_level_iterator.cc --compile=./vendor/table/table_builder.cc --compile=./vendor/table/iterator.cc --compile=./vendor/table/block_builder.cc --compile=./vendor/table/merger.cc --compile=./vendor/table/format.cc --compile=./vendor/table/filter_block.cc --compile=./vendor/table/table.cc --compile=./vendor/util/hash.cc --compile=./vendor/util/arena.cc --compile=./vendor/util/options.cc --compile=./vendor/util/histogram.cc --compile=./vendor/util/crc32c.cc --compile=./vendor/util/env.cc --compile=./vendor/util/filter_policy.cc --compile=./vendor/util/bloom.cc --compile=./vendor/util/logging.cc --compile=./vendor/util/coding.cc --compile=./vendor/util/status.cc --compile=./vendor/util/cache.cc --compile=./vendor/util/comparator.cc --compile=./vendor/helpers/memenv/memenv.cc --pnim --preprocess --noHeader --includeDirs=./vendor --includeDirs=./vendor/helpers --includeDirs=./vendor/helpers/memenv --includeDirs=./vendor/port --includeDirs=./vendor/include ./vendor/include/leveldb/c.h {.push hint[ConvFromXtoItselfNotNeeded]: off.} import macros @@ -79,7 +98,6 @@ macro defineEnum(typ: untyped): untyped = {.passc: "-I" & root & "/vendor/helpers/memenv".} {.passc: "-I" & root & "/vendor/port".} {.passc: "-I" & root & "/vendor/include".} -{.passc: "-I" & root & "/build/include".} {.compile: root & "/vendor/db/log_writer.cc".} {.compile: root & "/vendor/db/db_impl.cc".} {.compile: root & "/vendor/db/db_iter.cc".} diff --git a/tests/compileme.c b/tests/compileme.c deleted file mode 100644 index d756ad7..0000000 --- a/tests/compileme.c +++ /dev/null @@ -1,8 +0,0 @@ -#include - -int main(int argc, char** argv) -{ - fdatasync(-1); - return 0; -} - diff --git a/tests/compileme2.c b/tests/compileme2.c deleted file mode 100644 index e44c0ab..0000000 --- a/tests/compileme2.c +++ /dev/null @@ -1,8 +0,0 @@ -#include - -int main(int argc, char** argv) -{ - int i = F_FULLSYNC; - return 0; -} - diff --git a/tests/compileme3.c b/tests/compileme3.c deleted file mode 100644 index e4d5988..0000000 --- a/tests/compileme3.c +++ /dev/null @@ -1,8 +0,0 @@ -#include - -int main(int argc, char** argv) -{ - int i = O_CLOEXEC; - return 0; -} -