diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 831440d..bc86b01 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,66 +1,22 @@ -name: website +name: CI -#on: [push] # debugging only -on: - push: - tags: - - '[0-9]+.[0-9]+.[0-9]+' +on: [push, pull_request] jobs: - publish: - runs-on: ubuntu-latest + test: + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-latest, macOS-latest, windows-latest] + nim: [stable, 1.6.18] steps: - - name: Checkout - uses: actions/checkout@v2 - - name: Set output - id: vars - run: echo ::set-output name=tag::${GITHUB_REF:10} - - name: Cache choosenim - id: cache-choosenim - uses: actions/cache@v1 - with: - path: ~/.choosenim - key: ${{ runner.os }}-choosenim-stable - - name: Cache nimble - id: cache-nimble - uses: actions/cache@v1 - with: - path: ~/.nimble - key: ${{ runner.os }}-nimble-stable - - uses: jiro4989/setup-nim-action@v1 - with: - nim-version: 'stable' - - name: Install libsnappy - run: sudo apt-get install libsnappy1v5 - - name: Install leveldb c library - run: | - wget http://ftp.us.debian.org/debian/pool/main/l/leveldb/libleveldb1d_1.22-3_amd64.deb - wget http://ftp.us.debian.org/debian/pool/main/l/leveldb/libleveldb-dev_1.22-3_amd64.deb - sudo dpkg -i *.deb - - name: Build and test - env: - RELEASE_VERSION: ${{ steps.vars.outputs.tag }} - run: | - nimble build -Y - nimble test -Y - - name: Build doc - env: - RELEASE_VERSION: ${{ steps.vars.outputs.tag }} - run: | - # Due to bug https://github.com/nim-lang/Nim/issues/14281, compile the documentation separately. - nimble doc --project --git.url:https://github.com/$GITHUB_REPOSITORY --git.commit:$RELEASE_VERSION src/leveldb.nim - find . - # Deploying documentation of the latest version. - mkdir -p ./public - mv src/htmldocs/* public/ - cd ./public/ - ln -s ./leveldb.html index.html - cd ../ - - name: Deploy - if: success() - uses: crazy-max/ghaction-github-pages@v1.3.0 - with: - target_branch: gh-pages - build_dir: ./public - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - uses: actions/checkout@v2 + with: + submodules: true + - uses: iffy/install-nim@v4 + with: + version: ${{ matrix.nim }} + - name: Build + run: nimble install -y + - name: Test + run: nimble test -y diff --git a/.gitignore b/.gitignore index 4444336..5536487 100644 --- a/.gitignore +++ b/.gitignore @@ -2,9 +2,10 @@ nimcache/ /test.db/ /leveldb.e tests/packagetest/packagetest -src/leveldb tests/test src/htmldocs/ leveldbtool *.html *.css +build + diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..7cf389c --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "vendor"] + path = vendor + url = https://github.com/google/leveldb diff --git a/README.md b/README.md index a594d70..d21a517 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,14 @@ # leveldb.nim -[![docs](https://img.shields.io/badge/docs-leveldb.nim-green)](https://zielmicha.github.io/leveldb.nim/) +A self-contained LevelDB wrapper for Nim in a Nim friendly way. Uses git-submodule and nimterop so that no external libraries have to be installed or linked. -A LevelDB wrapper for Nim in a Nim friendly way. +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] Create a database: ```Nim - import leveldb + import leveldbstatic import options var db = leveldb.open("/tmp/mydata") diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..20c045f --- /dev/null +++ b/build.sh @@ -0,0 +1,56 @@ +#!/bin/bash +root=$(dirname "$0") + +sourceDir="${root}/vendor" +buildDir="${root}/build" +output="${root}/leveldbstatic/raw.nim" + +# install nimterop, if not already installed +if ! [ -x "$(command -v toast)" ]; then + nimble install -y nimterop@0.6.13 +fi + +git submodule deinit -f "${root}" +git submodule update --init --recursive --checkout "${root}" + +cmake -S "${sourceDir}" -B "${buildDir}" + +# Remove testing, benchmarking, third-party libraries. +rm -Rf "${sourceDir}/third_party" +rm -Rf "${sourceDir}/benchmarks" +rm "${sourceDir}/util/testutil.cc" + +# Prelude: +cat "${root}/leveldb/prelude.nim" > "${output}" +echo >> "${output}" + +# assemble files to be compiled: +extensions="c cc cpp" +for ext in ${extensions}; do + for file in `find "${sourceDir}" -type f -name "*.${ext}" \ + | grep -v "_test" \ + | grep -v "env_windows.cc" \ + | grep -v "env_posix.cc" \ + | grep -v "leveldbutil.cc"`; do + compile="${compile} --compile=${file}" + done +done + +# generate nim wrapper with nimterop +toast \ + $compile \ + --pnim \ + --preprocess \ + --noHeader \ + --includeDirs="${sourceDir}" \ + --includeDirs="${sourceDir}/helpers" \ + --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 new file mode 100644 index 0000000..0e80eda --- /dev/null +++ b/build/include/port/port_config.h @@ -0,0 +1,38 @@ +// 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/leveldb.nimble b/leveldb.nimble deleted file mode 100644 index d113654..0000000 --- a/leveldb.nimble +++ /dev/null @@ -1,13 +0,0 @@ -# Package - -version = "0.4.1" -author = "Michał Zieliński" -description = "LevelDB wrapper for Nim" -license = "MIT" -srcDir = "src" -installExt = @["nim"] -bin = @["leveldbtool"] - -# Dependencies - -requires "nim >= 1.4.0" diff --git a/src/leveldb.nim b/leveldbstatic.nim similarity index 99% rename from src/leveldb.nim rename to leveldbstatic.nim index 2c58810..7bc69ab 100644 --- a/src/leveldb.nim +++ b/leveldbstatic.nim @@ -9,7 +9,7 @@ ## Create a database: ## ## .. code-block:: Nim -## import leveldb +## import leveldbstatic ## import options ## ## var db = leveldb.open("/tmp/mydata") @@ -52,7 +52,7 @@ ## db.close() import options, os, strutils -import leveldb/raw +import leveldbstatic/raw type LevelDb* = ref object @@ -78,7 +78,7 @@ type const version* = block: - const configFile = "leveldb.nimble" + const configFile = "leveldbstatic.nimble" const sourcePath = currentSourcePath() const parentConfig = sourcePath.parentDir.parentDir / configFile const localConfig = sourcePath.parentDir / configFile diff --git a/leveldbstatic.nimble b/leveldbstatic.nimble new file mode 100644 index 0000000..a185674 --- /dev/null +++ b/leveldbstatic.nimble @@ -0,0 +1,12 @@ +# Package + +version = "0.1.0" +author = "leveldbstatic authors" +description = "Statically linked LevelDB wrapper for Nim" +license = "MIT" +bin = @["leveldbtool"] +installDirs = @["build", "leveldbstatic", "vendor"] +installFiles = @["leveldbstatic.nim"] + +# Dependencies +requires "nim >= 1.4.0" diff --git a/leveldbstatic/prelude.nim b/leveldbstatic/prelude.nim new file mode 100644 index 0000000..82223b5 --- /dev/null +++ b/leveldbstatic/prelude.nim @@ -0,0 +1,25 @@ +import os + +const root = currentSourcePath.parentDir.parentDir +const envWindows = root/"vendor"/"util"/"env_windows.cc" +const envPosix = root/"vendor"/"util"/"env_posix.cc" + +when defined(windows): + {.compile: envWindows.} + {.passc: "-DLEVELDB_PLATFORM_WINDOWS".} + {.passc: "-D_UNICODE".} + {.passc: "-DUNICODE".} + +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 new file mode 100644 index 0000000..2a95fb4 --- /dev/null +++ b/leveldbstatic/raw.nim @@ -0,0 +1,310 @@ +import os + +const root = currentSourcePath.parentDir.parentDir +const envWindows = root/"vendor"/"util"/"env_windows.cc" +const envPosix = root/"vendor"/"util"/"env_posix.cc" + +when defined(windows): + {.compile: envWindows.} + {.passc: "-DLEVELDB_PLATFORM_WINDOWS".} + {.passc: "-D_UNICODE".} + {.passc: "-DUNICODE".} + +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 +# 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 + +{.push hint[ConvFromXtoItselfNotNeeded]: off.} +import macros + +macro defineEnum(typ: untyped): untyped = + result = newNimNode(nnkStmtList) + + # Enum mapped to distinct cint + result.add quote do: + type `typ`* = distinct cint + + for i in ["+", "-", "*", "div", "mod", "shl", "shr", "or", "and", "xor", "<", "<=", "==", ">", ">="]: + let + ni = newIdentNode(i) + typout = if i[0] in "<=>": newIdentNode("bool") else: typ # comparisons return bool + if i[0] == '>': # cannot borrow `>` and `>=` from templates + let + nopp = if i.len == 2: newIdentNode("<=") else: newIdentNode("<") + result.add quote do: + proc `ni`*(x: `typ`, y: cint): `typout` = `nopp`(y, x) + proc `ni`*(x: cint, y: `typ`): `typout` = `nopp`(y, x) + proc `ni`*(x, y: `typ`): `typout` = `nopp`(y, x) + else: + result.add quote do: + proc `ni`*(x: `typ`, y: cint): `typout` {.borrow.} + proc `ni`*(x: cint, y: `typ`): `typout` {.borrow.} + proc `ni`*(x, y: `typ`): `typout` {.borrow.} + result.add quote do: + proc `ni`*(x: `typ`, y: int): `typout` = `ni`(x, y.cint) + proc `ni`*(x: int, y: `typ`): `typout` = `ni`(x.cint, y) + + let + divop = newIdentNode("/") # `/`() + dlrop = newIdentNode("$") # `$`() + notop = newIdentNode("not") # `not`() + result.add quote do: + proc `divop`*(x, y: `typ`): `typ` = `typ`((x.float / y.float).cint) + proc `divop`*(x: `typ`, y: cint): `typ` = `divop`(x, `typ`(y)) + proc `divop`*(x: cint, y: `typ`): `typ` = `divop`(`typ`(x), y) + proc `divop`*(x: `typ`, y: int): `typ` = `divop`(x, y.cint) + proc `divop`*(x: int, y: `typ`): `typ` = `divop`(x.cint, y) + + proc `dlrop`*(x: `typ`): string {.borrow.} + proc `notop`*(x: `typ`): `typ` {.borrow.} + + +{.experimental: "codeReordering".} +{.passc: "-I" & root & "/vendor".} +{.passc: "-I" & root & "/vendor/helpers".} +{.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".} +{.compile: root & "/vendor/db/dumpfile.cc".} +{.compile: root & "/vendor/db/c.cc".} +{.compile: root & "/vendor/db/builder.cc".} +{.compile: root & "/vendor/db/filename.cc".} +{.compile: root & "/vendor/db/write_batch.cc".} +{.compile: root & "/vendor/db/table_cache.cc".} +{.compile: root & "/vendor/db/version_edit.cc".} +{.compile: root & "/vendor/db/dbformat.cc".} +{.compile: root & "/vendor/db/log_reader.cc".} +{.compile: root & "/vendor/db/memtable.cc".} +{.compile: root & "/vendor/db/version_set.cc".} +{.compile: root & "/vendor/db/repair.cc".} +{.compile: root & "/vendor/table/block.cc".} +{.compile: root & "/vendor/table/two_level_iterator.cc".} +{.compile: root & "/vendor/table/table_builder.cc".} +{.compile: root & "/vendor/table/iterator.cc".} +{.compile: root & "/vendor/table/block_builder.cc".} +{.compile: root & "/vendor/table/merger.cc".} +{.compile: root & "/vendor/table/format.cc".} +{.compile: root & "/vendor/table/filter_block.cc".} +{.compile: root & "/vendor/table/table.cc".} +{.compile: root & "/vendor/util/hash.cc".} +{.compile: root & "/vendor/util/arena.cc".} +{.compile: root & "/vendor/util/options.cc".} +{.compile: root & "/vendor/util/histogram.cc".} +{.compile: root & "/vendor/util/crc32c.cc".} +{.compile: root & "/vendor/util/env.cc".} +{.compile: root & "/vendor/util/filter_policy.cc".} +{.compile: root & "/vendor/util/bloom.cc".} +{.compile: root & "/vendor/util/logging.cc".} +{.compile: root & "/vendor/util/coding.cc".} +{.compile: root & "/vendor/util/status.cc".} +{.compile: root & "/vendor/util/cache.cc".} +{.compile: root & "/vendor/util/comparator.cc".} +{.compile: root & "/vendor/helpers/memenv/memenv.cc".} +defineEnum(Enum_ch1) +const + leveldb_no_compression* = (0).cint + leveldb_snappy_compression* = (1).cint +type + leveldb_t* {.incompleteStruct.} = object + leveldb_cache_t* {.incompleteStruct.} = object + leveldb_comparator_t* {.incompleteStruct.} = object + leveldb_env_t* {.incompleteStruct.} = object + leveldb_filelock_t* {.incompleteStruct.} = object + leveldb_filterpolicy_t* {.incompleteStruct.} = object + leveldb_iterator_t* {.incompleteStruct.} = object + leveldb_logger_t* {.incompleteStruct.} = object + leveldb_options_t* {.incompleteStruct.} = object + leveldb_randomfile_t* {.incompleteStruct.} = object + leveldb_readoptions_t* {.incompleteStruct.} = object + leveldb_seqfile_t* {.incompleteStruct.} = object + leveldb_snapshot_t* {.incompleteStruct.} = object + leveldb_writablefile_t* {.incompleteStruct.} = object + leveldb_writebatch_t* {.incompleteStruct.} = object + leveldb_writeoptions_t* {.incompleteStruct.} = object +proc leveldb_open*(options: ptr leveldb_options_t; name: cstring; + errptr: ptr cstring): ptr leveldb_t {.importc, cdecl.} +proc leveldb_close*(db: ptr leveldb_t) {.importc, cdecl.} +proc leveldb_put*(db: ptr leveldb_t; options: ptr leveldb_writeoptions_t; + key: cstring; keylen: uint; val: cstring; vallen: uint; + errptr: ptr cstring) {.importc, cdecl.} +proc leveldb_delete*(db: ptr leveldb_t; options: ptr leveldb_writeoptions_t; + key: cstring; keylen: uint; errptr: ptr cstring) {.importc, + cdecl.} +proc leveldb_write*(db: ptr leveldb_t; options: ptr leveldb_writeoptions_t; + batch: ptr leveldb_writebatch_t; errptr: ptr cstring) {. + importc, cdecl.} +proc leveldb_get*(db: ptr leveldb_t; options: ptr leveldb_readoptions_t; + key: cstring; keylen: uint; vallen: ptr uint; + errptr: ptr cstring): cstring {.importc, cdecl.} + ## ``` + ## Returns NULL if not found. A malloc()ed array otherwise. + ## Stores the length of the array invallen. + ## ``` +proc leveldb_create_iterator*(db: ptr leveldb_t; + options: ptr leveldb_readoptions_t): ptr leveldb_iterator_t {. + importc, cdecl.} +proc leveldb_create_snapshot*(db: ptr leveldb_t): ptr leveldb_snapshot_t {. + importc, cdecl.} +proc leveldb_release_snapshot*(db: ptr leveldb_t; + snapshot: ptr leveldb_snapshot_t) {.importc, + cdecl.} +proc leveldb_property_value*(db: ptr leveldb_t; propname: cstring): cstring {. + importc, cdecl.} + ## ``` + ## Returns NULL if property name is unknown. + ## Else returns a pointer to a malloc()-ed null-terminated value. + ## ``` +proc leveldb_approximate_sizes*(db: ptr leveldb_t; num_ranges: cint; + range_start_key: ptr cstring; + range_start_key_len: ptr uint; + range_limit_key: ptr cstring; + range_limit_key_len: ptr uint; sizes: ptr uint64) {. + importc, cdecl.} +proc leveldb_compact_range*(db: ptr leveldb_t; start_key: cstring; + start_key_len: uint; limit_key: cstring; + limit_key_len: uint) {.importc, cdecl.} +proc leveldb_destroy_db*(options: ptr leveldb_options_t; name: cstring; + errptr: ptr cstring) {.importc, cdecl.} +proc leveldb_repair_db*(options: ptr leveldb_options_t; name: cstring; + errptr: ptr cstring) {.importc, cdecl.} +proc leveldb_iter_destroy*(a1: ptr leveldb_iterator_t) {.importc, cdecl.} +proc leveldb_iter_valid*(a1: ptr leveldb_iterator_t): uint8 {.importc, cdecl.} +proc leveldb_iter_seek_to_first*(a1: ptr leveldb_iterator_t) {.importc, cdecl.} +proc leveldb_iter_seek_to_last*(a1: ptr leveldb_iterator_t) {.importc, cdecl.} +proc leveldb_iter_seek*(a1: ptr leveldb_iterator_t; k: cstring; klen: uint) {. + importc, cdecl.} +proc leveldb_iter_next*(a1: ptr leveldb_iterator_t) {.importc, cdecl.} +proc leveldb_iter_prev*(a1: ptr leveldb_iterator_t) {.importc, cdecl.} +proc leveldb_iter_key*(a1: ptr leveldb_iterator_t; klen: ptr uint): cstring {. + importc, cdecl.} +proc leveldb_iter_value*(a1: ptr leveldb_iterator_t; vlen: ptr uint): cstring {. + importc, cdecl.} +proc leveldb_iter_get_error*(a1: ptr leveldb_iterator_t; errptr: ptr cstring) {. + importc, cdecl.} +proc leveldb_writebatch_create*(): ptr leveldb_writebatch_t {.importc, cdecl.} +proc leveldb_writebatch_destroy*(a1: ptr leveldb_writebatch_t) {.importc, cdecl.} +proc leveldb_writebatch_clear*(a1: ptr leveldb_writebatch_t) {.importc, cdecl.} +proc leveldb_writebatch_put*(a1: ptr leveldb_writebatch_t; key: cstring; + klen: uint; val: cstring; vlen: uint) {.importc, + cdecl.} +proc leveldb_writebatch_delete*(a1: ptr leveldb_writebatch_t; key: cstring; + klen: uint) {.importc, cdecl.} +proc leveldb_writebatch_iterate*(a1: ptr leveldb_writebatch_t; state: pointer; + put: proc (a1: pointer; k: cstring; klen: uint; v: cstring; vlen: uint) {. + cdecl.}; deleted: proc (a1: pointer; k: cstring; klen: uint) {.cdecl.}) {. + importc, cdecl.} +proc leveldb_writebatch_append*(destination: ptr leveldb_writebatch_t; + source: ptr leveldb_writebatch_t) {.importc, + cdecl.} +proc leveldb_options_create*(): ptr leveldb_options_t {.importc, cdecl.} +proc leveldb_options_destroy*(a1: ptr leveldb_options_t) {.importc, cdecl.} +proc leveldb_options_set_comparator*(a1: ptr leveldb_options_t; + a2: ptr leveldb_comparator_t) {.importc, + cdecl.} +proc leveldb_options_set_filter_policy*(a1: ptr leveldb_options_t; + a2: ptr leveldb_filterpolicy_t) {. + importc, cdecl.} +proc leveldb_options_set_create_if_missing*(a1: ptr leveldb_options_t; a2: uint8) {. + importc, cdecl.} +proc leveldb_options_set_error_if_exists*(a1: ptr leveldb_options_t; a2: uint8) {. + importc, cdecl.} +proc leveldb_options_set_paranoid_checks*(a1: ptr leveldb_options_t; a2: uint8) {. + importc, cdecl.} +proc leveldb_options_set_env*(a1: ptr leveldb_options_t; a2: ptr leveldb_env_t) {. + importc, cdecl.} +proc leveldb_options_set_info_log*(a1: ptr leveldb_options_t; + a2: ptr leveldb_logger_t) {.importc, cdecl.} +proc leveldb_options_set_write_buffer_size*(a1: ptr leveldb_options_t; a2: uint) {. + importc, cdecl.} +proc leveldb_options_set_max_open_files*(a1: ptr leveldb_options_t; a2: cint) {. + importc, cdecl.} +proc leveldb_options_set_cache*(a1: ptr leveldb_options_t; + a2: ptr leveldb_cache_t) {.importc, cdecl.} +proc leveldb_options_set_block_size*(a1: ptr leveldb_options_t; a2: uint) {. + importc, cdecl.} +proc leveldb_options_set_block_restart_interval*(a1: ptr leveldb_options_t; + a2: cint) {.importc, cdecl.} +proc leveldb_options_set_max_file_size*(a1: ptr leveldb_options_t; a2: uint) {. + importc, cdecl.} +proc leveldb_options_set_compression*(a1: ptr leveldb_options_t; a2: cint) {. + importc, cdecl.} +proc leveldb_comparator_create*(state: pointer; + destructor: proc (a1: pointer) {.cdecl.}; + compare: proc (a1: pointer; a: cstring; alen: uint; b: cstring; blen: uint): cint {. + cdecl.}; name: proc (a1: pointer): cstring {.cdecl.}): ptr leveldb_comparator_t {. + importc, cdecl.} +proc leveldb_comparator_destroy*(a1: ptr leveldb_comparator_t) {.importc, cdecl.} +proc leveldb_filterpolicy_create*(state: pointer; + destructor: proc (a1: pointer) {.cdecl.}; + create_filter: proc (a1: pointer; key_array: ptr cstring; + key_length_array: ptr uint; num_keys: cint; + filter_length: ptr uint): cstring {.cdecl.}; + key_may_match: proc (a1: pointer; key: cstring; length: uint; + filter: cstring; filter_length: uint): uint8 {.cdecl.}; + name: proc (a1: pointer): cstring {.cdecl.}): ptr leveldb_filterpolicy_t {. + importc, cdecl.} +proc leveldb_filterpolicy_destroy*(a1: ptr leveldb_filterpolicy_t) {.importc, + cdecl.} +proc leveldb_filterpolicy_create_bloom*(bits_per_key: cint): ptr leveldb_filterpolicy_t {. + importc, cdecl.} +proc leveldb_readoptions_create*(): ptr leveldb_readoptions_t {.importc, cdecl.} +proc leveldb_readoptions_destroy*(a1: ptr leveldb_readoptions_t) {.importc, + cdecl.} +proc leveldb_readoptions_set_verify_checksums*(a1: ptr leveldb_readoptions_t; + a2: uint8) {.importc, cdecl.} +proc leveldb_readoptions_set_fill_cache*(a1: ptr leveldb_readoptions_t; + a2: uint8) {.importc, cdecl.} +proc leveldb_readoptions_set_snapshot*(a1: ptr leveldb_readoptions_t; + a2: ptr leveldb_snapshot_t) {.importc, + cdecl.} +proc leveldb_writeoptions_create*(): ptr leveldb_writeoptions_t {.importc, cdecl.} +proc leveldb_writeoptions_destroy*(a1: ptr leveldb_writeoptions_t) {.importc, + cdecl.} +proc leveldb_writeoptions_set_sync*(a1: ptr leveldb_writeoptions_t; a2: uint8) {. + importc, cdecl.} +proc leveldb_cache_create_lru*(capacity: uint): ptr leveldb_cache_t {.importc, + cdecl.} +proc leveldb_cache_destroy*(cache: ptr leveldb_cache_t) {.importc, cdecl.} +proc leveldb_create_default_env*(): ptr leveldb_env_t {.importc, cdecl.} +proc leveldb_env_destroy*(a1: ptr leveldb_env_t) {.importc, cdecl.} +proc leveldb_env_get_test_directory*(a1: ptr leveldb_env_t): cstring {.importc, + cdecl.} + ## ``` + ## If not NULL, the returned buffer must be released using leveldb_free(). + ## ``` +proc leveldb_free*(`ptr`: pointer) {.importc, cdecl.} + ## ``` + ## Utility + ## Calls free(ptr). + ## REQUIRES: ptr was malloc()-ed and returned by one of the routines + ## in this file. Note that in certain cases (typically on Windows), you + ## may need to call this routine instead of free(ptr) to dispose of + ## malloc()-ed memory returned by this library. + ## ``` +proc leveldb_major_version*(): cint {.importc, cdecl.} + ## ``` + ## Return the major version number for this release. + ## ``` +proc leveldb_minor_version*(): cint {.importc, cdecl.} + ## ``` + ## Return the minor version number for this release. + ## ``` +{.pop.} diff --git a/src/leveldbtool.nim b/leveldbtool.nim similarity index 98% rename from src/leveldbtool.nim rename to leveldbtool.nim index 1a485cf..27dbc5e 100644 --- a/src/leveldbtool.nim +++ b/leveldbtool.nim @@ -1,5 +1,5 @@ import options, os, strutils -import leveldb +import leveldbstatic as leveldb proc tool() = proc usage() = diff --git a/src/leveldb/raw.nim b/src/leveldb/raw.nim deleted file mode 100644 index 513692b..0000000 --- a/src/leveldb/raw.nim +++ /dev/null @@ -1,199 +0,0 @@ -## Copyright (c) 2011 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. -## -## C bindings for leveldb. May be useful as a stable ABI that can be -## used by programs that keep leveldb in a shared library, or for -## a JNI api. -## -## Does not support: -## . getters for the option types -## . custom comparators that implement key shortening -## . custom iter, db, env, cache implementations using just the C bindings -## -## Some conventions: -## -## (1) We expose just opaque struct pointers and functions to clients. -## This allows us to change internal representations without having to -## recompile clients. -## -## (2) For simplicity, there is no equivalent to the Slice type. Instead, -## the caller has to pass the pointer and length as separate -## arguments. -## -## (3) Errors are represented by a null-terminated c string. NULL -## means no error. All operations that can raise an error are passed -## a "char** errptr" as the last argument. One of the following must -## be true on entry: -## errptr == NULL -## errptr points to a malloc()ed null-terminated error message -## (On Windows, \*errptr must have been malloc()-ed by this library.) -## On success, a leveldb routine leaves \*errptr unchanged. -## On failure, leveldb frees the old value of \*errptr and -## set \*errptr to a malloc()ed error message. -## -## (4) Bools have the type uint8_t (0 == false; rest == true) -## -## (5) All of the pointer arguments must be non-NULL. -## - -{.passl: "-lleveldb".} - -## # Exported types - -type - leveldb_options_t* = object - leveldb_writeoptions_t* = object - leveldb_readoptions_t* = object - leveldb_writebatch_t* = object - leveldb_iterator_t* = object - leveldb_snapshot_t* = object - leveldb_comparator_t* = object - leveldb_filterpolicy_t* = object - leveldb_env_t* = object - leveldb_logger_t* = object - leveldb_cache_t* = object - leveldb_t* = object - -## DB operations - -proc leveldb_open*(options: ptr leveldb_options_t; name: cstring; errptr: ptr cstring): ptr leveldb_t {.importc.} -proc leveldb_close*(db: ptr leveldb_t) {.importc.} -proc leveldb_put*(db: ptr leveldb_t; options: ptr leveldb_writeoptions_t; key: cstring; - keylen: csize_t; val: cstring; vallen: csize_t; errptr: ptr cstring) {.importc.} -proc leveldb_delete*(db: ptr leveldb_t; options: ptr leveldb_writeoptions_t; - key: cstring; keylen: csize_t; errptr: ptr cstring) {.importc.} -proc leveldb_write*(db: ptr leveldb_t; options: ptr leveldb_writeoptions_t; - batch: ptr leveldb_writebatch_t; errptr: ptr cstring) {.importc.} -## Returns NULL if not found. A malloc()ed array otherwise. -## Stores the length of the array in \*vallen. - -proc leveldb_get*(db: ptr leveldb_t; options: ptr leveldb_readoptions_t; key: cstring; - keylen: csize_t; vallen: ptr csize_t; errptr: ptr cstring): cstring {.importc.} -proc leveldb_create_iterator*(db: ptr leveldb_t; options: ptr leveldb_readoptions_t): ptr leveldb_iterator_t {.importc.} -proc leveldb_create_snapshot*(db: ptr leveldb_t): ptr leveldb_snapshot_t {.importc.} -proc leveldb_release_snapshot*(db: ptr leveldb_t; snapshot: ptr leveldb_snapshot_t) {.importc.} -## Returns NULL if property name is unknown. -## Else returns a pointer to a malloc()-ed null-terminated value. - -proc leveldb_property_value*(db: ptr leveldb_t; propname: cstring): cstring {.importc.} -proc leveldb_approximate_sizes*(db: ptr leveldb_t; num_ranges: cint; - range_start_key: ptr cstring; - range_start_key_len: ptr csize_t; - range_limit_key: ptr cstring; - range_limit_key_len: ptr csize_t; sizes: ptr uint64) {.importc.} -proc leveldb_compact_range*(db: ptr leveldb_t; start_key: cstring; - start_key_len: csize_t; limit_key: cstring; - limit_key_len: csize_t) {.importc.} -## Management operations - -proc leveldb_destroy_db*(options: ptr leveldb_options_t; name: cstring; - errptr: ptr cstring) {.importc.} -proc leveldb_repair_db*(options: ptr leveldb_options_t; name: cstring; - errptr: ptr cstring) {.importc.} -## Iterator - -proc leveldb_iter_destroy*(a1: ptr leveldb_iterator_t) {.importc.} -proc leveldb_iter_valid*(a1: ptr leveldb_iterator_t): uint8 {.importc.} -proc leveldb_iter_seek_to_first*(a1: ptr leveldb_iterator_t) {.importc.} -proc leveldb_iter_seek_to_last*(a1: ptr leveldb_iterator_t) {.importc.} -proc leveldb_iter_seek*(a1: ptr leveldb_iterator_t; k: cstring; klen: csize_t) {.importc.} -proc leveldb_iter_next*(a1: ptr leveldb_iterator_t) {.importc.} -proc leveldb_iter_prev*(a1: ptr leveldb_iterator_t) {.importc.} -proc leveldb_iter_key*(a1: ptr leveldb_iterator_t; klen: ptr csize_t): cstring {.importc.} -proc leveldb_iter_value*(a1: ptr leveldb_iterator_t; vlen: ptr csize_t): cstring {.importc.} -proc leveldb_iter_get_error*(a1: ptr leveldb_iterator_t; errptr: ptr cstring) {.importc.} -## Write batch - -proc leveldb_writebatch_create*(): ptr leveldb_writebatch_t {.importc.} -proc leveldb_writebatch_destroy*(a1: ptr leveldb_writebatch_t) {.importc.} -proc leveldb_writebatch_clear*(a1: ptr leveldb_writebatch_t) {.importc.} -proc leveldb_writebatch_put*(a1: ptr leveldb_writebatch_t; key: cstring; klen: csize_t; - val: cstring; vlen: csize_t) {.importc.} -proc leveldb_writebatch_delete*(a1: ptr leveldb_writebatch_t; key: cstring; - klen: csize_t) {.importc.} -proc leveldb_writebatch_iterate*(a1: ptr leveldb_writebatch_t; state: pointer; put: proc ( - a1: pointer; k: cstring; klen: csize_t; v: cstring; vlen: csize_t); deleted: proc ( - a1: pointer; k: cstring; klen: csize_t)) {.importc.} -proc leveldb_writebatch_append*(destination: ptr leveldb_writebatch_t; - source: ptr leveldb_writebatch_t) {.importc.} -## Options - -proc leveldb_options_create*(): ptr leveldb_options_t {.importc.} -proc leveldb_options_destroy*(a1: ptr leveldb_options_t) {.importc.} -proc leveldb_options_set_comparator*(a1: ptr leveldb_options_t; - a2: ptr leveldb_comparator_t) {.importc.} -proc leveldb_options_set_filter_policy*(a1: ptr leveldb_options_t; - a2: ptr leveldb_filterpolicy_t) {.importc.} -proc leveldb_options_set_create_if_missing*(a1: ptr leveldb_options_t; a2: uint8) {.importc.} -proc leveldb_options_set_error_if_exists*(a1: ptr leveldb_options_t; a2: uint8) {.importc.} -proc leveldb_options_set_paranoid_checks*(a1: ptr leveldb_options_t; a2: uint8) {.importc.} -proc leveldb_options_set_env*(a1: ptr leveldb_options_t; a2: ptr leveldb_env_t) {.importc.} -proc leveldb_options_set_info_log*(a1: ptr leveldb_options_t; - a2: ptr leveldb_logger_t) {.importc.} -proc leveldb_options_set_write_buffer_size*(a1: ptr leveldb_options_t; a2: csize_t) {.importc.} -proc leveldb_options_set_max_open_files*(a1: ptr leveldb_options_t; a2: cint) {.importc.} -proc leveldb_options_set_cache*(a1: ptr leveldb_options_t; a2: ptr leveldb_cache_t) {.importc.} -proc leveldb_options_set_block_size*(a1: ptr leveldb_options_t; a2: csize_t) {.importc.} -proc leveldb_options_set_block_restart_interval*(a1: ptr leveldb_options_t; a2: cint) {.importc.} -proc leveldb_options_set_max_file_size*(a1: ptr leveldb_options_t; a2: csize_t) {.importc.} -const - leveldb_no_compression* = 0 - leveldb_snappy_compression* = 1 - -proc leveldb_options_set_compression*(a1: ptr leveldb_options_t; a2: cint) {.importc.} -## Comparator - -proc leveldb_comparator_create*(state: pointer; destructor: proc (a1: pointer); compare: proc ( - a1: pointer; a: cstring; alen: csize_t; b: cstring; blen: csize_t): cint; - name: proc (a1: pointer): cstring): ptr leveldb_comparator_t {.importc.} -proc leveldb_comparator_destroy*(a1: ptr leveldb_comparator_t) {.importc.} -## Filter policy - -proc leveldb_filterpolicy_create*(state: pointer; destructor: proc (a1: pointer); - create_filter: proc (a1: pointer; key_array: ptr cstring; - key_length_array: ptr csize_t; num_keys: cint; - filter_length: ptr csize_t): cstring; key_may_match: proc ( - a1: pointer; key: cstring; length: csize_t; filter: cstring; filter_length: csize_t): uint8; - name: proc (a1: pointer): cstring): ptr leveldb_filterpolicy_t {.importc.} -proc leveldb_filterpolicy_destroy*(a1: ptr leveldb_filterpolicy_t) {.importc.} -proc leveldb_filterpolicy_create_bloom*(bits_per_key: cint): ptr leveldb_filterpolicy_t {.importc.} -## Read options - -proc leveldb_readoptions_create*(): ptr leveldb_readoptions_t {.importc.} -proc leveldb_readoptions_destroy*(a1: ptr leveldb_readoptions_t) {.importc.} -proc leveldb_readoptions_set_verify_checksums*(a1: ptr leveldb_readoptions_t; - a2: uint8) {.importc.} -proc leveldb_readoptions_set_fill_cache*(a1: ptr leveldb_readoptions_t; a2: uint8) {.importc.} -proc leveldb_readoptions_set_snapshot*(a1: ptr leveldb_readoptions_t; - a2: ptr leveldb_snapshot_t) {.importc.} -## Write options - -proc leveldb_writeoptions_create*(): ptr leveldb_writeoptions_t {.importc.} -proc leveldb_writeoptions_destroy*(a1: ptr leveldb_writeoptions_t) {.importc.} -proc leveldb_writeoptions_set_sync*(a1: ptr leveldb_writeoptions_t; a2: uint8) {.importc.} -## Cache - -proc leveldb_cache_create_lru*(capacity: csize_t): ptr leveldb_cache_t {.importc.} -proc leveldb_cache_destroy*(cache: ptr leveldb_cache_t) {.importc.} -## Env - -proc leveldb_create_default_env*(): ptr leveldb_env_t {.importc.} -proc leveldb_env_destroy*(a1: ptr leveldb_env_t) {.importc.} -## If not NULL, the returned buffer must be released using leveldb_free(). - -proc leveldb_env_get_test_directory*(a1: ptr leveldb_env_t): cstring {.importc.} -## Utility -## Calls free(ptr). -## REQUIRES: ptr was malloc()-ed and returned by one of the routines -## in this file. Note that in certain cases (typically on Windows), you -## may need to call this routine instead of free(ptr) to dispose of -## malloc()-ed memory returned by this library. - -proc leveldb_free*(`ptr`: pointer) {.importc.} -## Return the major version number for this release. - -proc leveldb_major_version*(): cint {.importc.} -## Return the minor version number for this release. - -proc leveldb_minor_version*(): cint {.importc.} diff --git a/tests/packagetest/packagetest.nimble b/tests/packagetest/packagetest.nimble index a3e1c7d..59441c7 100644 --- a/tests/packagetest/packagetest.nimble +++ b/tests/packagetest/packagetest.nimble @@ -12,4 +12,4 @@ bin = @["packagetest"] # Dependencies requires "nim >= 0.18.0" -requires "leveldb" +requires "leveldbstatic" diff --git a/tests/packagetest/src/packagetest.nim b/tests/packagetest/src/packagetest.nim index 45c0b79..3f379b7 100644 --- a/tests/packagetest/src/packagetest.nim +++ b/tests/packagetest/src/packagetest.nim @@ -1,10 +1,14 @@ +import os import options -import leveldb +import leveldbstatic as leveldb when isMainModule: - let db = leveldb.open("/tmp/testleveldb/tooldb") + let tempDir = getTempDir() / "testleveldb" / "tooldb" + createdir(tempDir) + let db = leveldb.open(tempDir) db.put("hello", "world") let val = db.get("hello") if val.isSome() and val.get() == "world": echo "leveldb works." db.close() + removedir(tempDir) diff --git a/tests/test.nim b/tests/test.nim index 935fbd5..1a7d19f 100644 --- a/tests/test.nim +++ b/tests/test.nim @@ -1,8 +1,9 @@ import unittest, options, os, osproc, sequtils, strutils -import leveldb, leveldb/raw +import leveldbstatic as leveldb +import leveldbstatic/raw const - tmpDir = "/tmp/testleveldb" + tmpDir = getTempDir() / "testleveldb" tmpNimbleDir = tmpDir / "nimble" tmpDbDir = tmpDir / "testdb" @@ -31,7 +32,7 @@ proc execTool(args: varargs[string]): tuple[output: string, exitCode: int] = var quotedArgs = @args quotedArgs.insert(tmpDbDir) quotedArgs.insert("--database") - quotedArgs.insert(tmpNimbleDir / "bin" / "leveldbtool") + quotedArgs.insert(findExe(tmpNimbleDir / "bin" / "leveldbtool")) quotedArgs = quotedArgs.map(proc (x: string): string = "\"" & x & "\"") if not dirExists(tmpDbDir): @@ -212,9 +213,9 @@ suite "package": test "import as package": let (output, exitCode) = execNimble("install") check exitCode == QuitSuccess - check output.contains("leveldb installed successfully.") + check output.contains("leveldbstatic installed successfully.") - cd "tests/packagetest": + cd "tests"/"packagetest": var (output, exitCode) = execNimble("build") check exitCode == QuitSuccess check output.contains("Building") diff --git a/vendor b/vendor new file mode 160000 index 0000000..068d5ee --- /dev/null +++ b/vendor @@ -0,0 +1 @@ +Subproject commit 068d5ee1a3ac40dabd00d211d5013af44be55bea