├── goresql ├── go.mod └── LICENSE ├── lib ├── .clang-format ├── .clang-tidy ├── sc │ ├── sc_crc32.h │ ├── sc_time.h │ ├── sc_option.h │ ├── sc_mutex.h │ ├── sc_cond.h │ ├── sc_option.c │ ├── sc_thread.h │ ├── sc_ini.h │ ├── sc_mutex.c │ ├── sc.h │ ├── .clang-format │ ├── sc_uri.h │ └── sc_signal.h └── linenoise │ └── linenoise.h ├── util ├── docker │ ├── alpine │ │ ├── docker-entrypoint.sh │ │ └── Dockerfile │ └── debian │ │ ├── docker-entrypoint.sh │ │ └── Dockerfile ├── daemon │ └── resql.service ├── cli │ └── CMakeLists.txt └── benchmark │ └── CMakeLists.txt ├── .clang-tidy ├── .gitignore ├── codecov.yml ├── .github └── workflows │ ├── .java.yml │ ├── .coverage.yml │ ├── .go.yml │ ├── .macos.yml │ ├── .armv6.yml │ ├── .armv7.yml │ ├── .s390x.yml │ ├── .aarch64.yml │ ├── .ppc64le.yml │ ├── .c.yml │ ├── .ubuntu.yml │ └── codeql-analysis.yml ├── test ├── example │ ├── CMakeLists.txt │ ├── multi_test.c │ └── single_test.c ├── state_test.c ├── session_test.c ├── entry_test.c ├── config_test.c ├── test_util.h ├── single_test.c ├── remove_test.c ├── add_test.c └── restart_test.c ├── .cirrus.yml ├── cresql ├── LICENSE └── CMakeLists.txt ├── jresql ├── LICENSE ├── src │ ├── main │ │ └── java │ │ │ └── resql │ │ │ ├── PreparedStatement.java │ │ │ ├── ResqlFatalException.java │ │ │ ├── ResqlException.java │ │ │ ├── ResqlSQLException.java │ │ │ ├── Prepared.java │ │ │ ├── ResqlClient.java │ │ │ ├── ResultSet.java │ │ │ ├── Row.java │ │ │ ├── Config.java │ │ │ └── Resql.java │ └── test │ │ └── java │ │ └── resql │ │ ├── benchmark │ │ └── LatencyTest.java │ │ └── TimestampTest.java └── .gitignore ├── LICENSE ├── src ├── main.c ├── info.h ├── config.h ├── conf.h ├── metric.h ├── info.c ├── session.h ├── entry.h ├── file.h ├── client.h ├── store.h ├── meta.h ├── conn.h ├── snapshot.h ├── page.h ├── node.h ├── client.c ├── aux.h ├── state.h ├── cmd.h └── cmd.c ├── .clang-format └── bin └── resql.ini /goresql/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/tezc/resql/goresql 2 | 3 | go 1.13 4 | -------------------------------------------------------------------------------- /lib/.clang-format: -------------------------------------------------------------------------------- 1 | --- 2 | DisableFormat: true 3 | SortIncludes: Never 4 | ... 5 | 6 | -------------------------------------------------------------------------------- /lib/.clang-tidy: -------------------------------------------------------------------------------- 1 | --- 2 | Checks: '-*,misc-definitions-in-headers' 3 | CheckOptions: 4 | - { key: HeaderFileExtensions, value: "x" } -------------------------------------------------------------------------------- /util/docker/alpine/docker-entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | set -e 3 | 4 | # first arg is `-f` or `--some-option` 5 | if [ "${1#-}" != "$1" ]; then 6 | set -- resql "$@" 7 | fi 8 | 9 | exec "$@" -------------------------------------------------------------------------------- /util/docker/debian/docker-entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | set -e 3 | 4 | # first arg is `-f` or `--some-option` 5 | if [ "${1#-}" != "$1" ]; then 6 | set -- resql "$@" 7 | fi 8 | 9 | exec "$@" -------------------------------------------------------------------------------- /.clang-tidy: -------------------------------------------------------------------------------- 1 | --- 2 | Checks: '-*,clang-analyzer-*,misc-*,portability-*,bugprone-*,-clang-analyzer-security.insecureAPI*' 3 | WarningsAsErrors: '-*,clang-analyzer-*,misc-*,portability-*,bugprone-*,-clang-analyzer-security.insecureAPI*' 4 | HeaderFilterRegex: '.*' 5 | FormatStyle: file -------------------------------------------------------------------------------- /util/daemon/resql.service: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=Resql database daemon 3 | After=network.target 4 | 5 | [Service] 6 | WorkingDirectory=/tmp/resql 7 | Type=notify 8 | ExecStart=/usr/local/bin/resql -c=/etc/resql.ini --systemd 9 | Restart=always 10 | 11 | [Install] 12 | WantedBy=multi-user.target 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | CMakeLists.txt.user 2 | CMakeCache.txt 3 | CMakeFiles 4 | CMakeScripts 5 | Testing 6 | Makefile 7 | cmake_install.cmake 8 | install_manifest.txt 9 | compile_commands.json 10 | CTestTestfile.cmake 11 | _deps 12 | .idea 13 | backup 14 | cmake-build* 15 | build 16 | bin/resql 17 | bin/resql-cli 18 | bin/resql-benchmark 19 | bin/resql-history.txt 20 | bin/pgo 21 | bin/build 22 | bin/*.resql 23 | bin/.pid -------------------------------------------------------------------------------- /codecov.yml: -------------------------------------------------------------------------------- 1 | codecov: 2 | require_ci_to_pass: false 3 | 4 | coverage: 5 | precision: 2 6 | round: down 7 | range: "50...90" 8 | status: 9 | project: 10 | default: 11 | # basic 12 | target: auto 13 | threshold: 90% 14 | patch: 15 | default: 16 | # basic 17 | target: auto 18 | threshold: 90% 19 | 20 | parsers: 21 | gcov: 22 | branch_detection: 23 | conditional: yes 24 | loop: yes 25 | method: no 26 | macro: no 27 | 28 | comment: false 29 | 30 | -------------------------------------------------------------------------------- /.github/workflows/.java.yml: -------------------------------------------------------------------------------- 1 | name: "Java Client" 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | branches: 9 | - master 10 | jobs: 11 | ubuntu: 12 | runs-on: ubuntu-latest 13 | name: Java client test 14 | 15 | steps: 16 | - uses: actions/checkout@v2.1.0 17 | - name: build 18 | run: | 19 | sudo apt update 20 | sudo apt-get install valgrind cmake default-jdk maven 21 | ./build.sh 22 | cd bin 23 | ./resql & 24 | cd .. 25 | cd jresql 26 | mvn test -------------------------------------------------------------------------------- /.github/workflows/.coverage.yml: -------------------------------------------------------------------------------- 1 | name: "Coverage" 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | branches: 9 | - master 10 | jobs: 11 | coverage: 12 | runs-on: ubuntu-latest 13 | name: Coverage 14 | steps: 15 | - uses: actions/checkout@v2.1.0 16 | - name: build 17 | run: | 18 | sudo apt-get install cmake lcov 19 | mkdir build && cd build 20 | cmake .. -DRESQL_BUILD_TESTS=1 -DCMAKE_BUILD_TYPE=Coverage 21 | make -j 22 | make coverage 23 | bash <(curl -s https://codecov.io/bash) -f test/coverage.info -t ${{ secrets.CODECOV }} -------------------------------------------------------------------------------- /.github/workflows/.go.yml: -------------------------------------------------------------------------------- 1 | name: "Go Client" 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | branches: 9 | - master 10 | jobs: 11 | ubuntu: 12 | runs-on: ubuntu-latest 13 | name: Go client test 14 | 15 | steps: 16 | - uses: actions/checkout@v2.1.0 17 | - uses: actions/setup-go@v2 18 | with: 19 | go-version: '^1.13.1' 20 | - name: build 21 | run: | 22 | sudo apt update 23 | sudo apt-get install valgrind cmake 24 | ./build.sh 25 | cd bin 26 | ./resql & 27 | cd .. 28 | cd goresql 29 | go test . -count 1 -v -------------------------------------------------------------------------------- /test/example/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(CMAKE_C_STANDARD 11) 2 | include_directories(../../src) 3 | include_directories(../../cresql/) 4 | include_directories(../../test) 5 | 6 | add_library(resql-example ../test_util.c) 7 | target_link_libraries(resql-example PUBLIC resql-server) 8 | 9 | target_include_directories(resql-example PUBLIC ..) 10 | 11 | 12 | macro(resql_example name) 13 | add_executable(${name} ${name}.c) 14 | target_link_libraries(${name} ${PROJECT_LIBS} resql-test) 15 | endmacro() 16 | 17 | 18 | resql_example(single_test) 19 | resql_example(multi_test) 20 | 21 | set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/example) 22 | set(CTEST_BINARY_DIRECTORY ${PROJECT_BINARY_DIR}/example) 23 | -------------------------------------------------------------------------------- /.github/workflows/.macos.yml: -------------------------------------------------------------------------------- 1 | name: "Mac OS" 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | branches: 9 | - master 10 | jobs: 11 | macos: 12 | runs-on: macos-latest 13 | name: Build on Mac OS 14 | 15 | strategy: 16 | fail-fast: false 17 | matrix: 18 | compiler: [ gcc, clang ] 19 | 20 | steps: 21 | - uses: actions/checkout@v2.1.0 22 | - name: build 23 | env: 24 | CC: ${{ matrix.compiler }} 25 | run: | 26 | mkdir build-debug && cd build-debug 27 | cmake .. -DCMAKE_BUILD_TYPE=Debug -DRESQL_BUILD_TESTS=1 -DSANITIZER=address -DCMAKE_C_COMPILER=$CC 28 | make -j 29 | make check 30 | rm -rf * 31 | cd .. 32 | ./build.sh -------------------------------------------------------------------------------- /util/cli/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(CMAKE_C_STANDARD 11) 2 | set(CMAKE_C_STANDARD_REQUIRED ON) 3 | set(CMAKE_C_EXTENSIONS OFF) 4 | 5 | add_executable(resql-cli 6 | resql_cli.c 7 | ../../cresql/resql.h 8 | ../../cresql/resql.c 9 | ../../lib/linenoise/linenoise.h 10 | ../../lib/linenoise/linenoise.c 11 | ../../lib/sc/sc_option.h 12 | ../../lib/sc/sc_option.c 13 | ) 14 | 15 | target_include_directories(resql-cli PRIVATE ../../cresql) 16 | target_include_directories(resql-cli PRIVATE ../../lib/linenoise) 17 | target_include_directories(resql-cli PRIVATE ../../lib/sc) 18 | set_source_files_properties( 19 | ../../lib/linenoise/linenoise.h 20 | ../../lib/linenoise/linenoise.c 21 | PROPERTIES 22 | COMPILE_FLAGS "-w" 23 | ) 24 | 25 | install(TARGETS resql-cli RUNTIME DESTINATION ${CMAKE_SOURCE_DIR}/bin) 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /.github/workflows/.armv6.yml: -------------------------------------------------------------------------------- 1 | name: armv6 2 | on: 3 | schedule: 4 | - cron: '0 5 * * *' 5 | workflow_dispatch: 6 | jobs: 7 | archs: 8 | # The host should always be linux 9 | runs-on: ubuntu-18.04 10 | name: Build on armv6 11 | steps: 12 | - uses: actions/checkout@v2.1.0 13 | - uses: uraimo/run-on-arch-action@v2.0.9 14 | name: Build artifact 15 | id: build 16 | with: 17 | arch: armv6 18 | distro: alpine_latest 19 | 20 | # Not required, but speeds up builds 21 | githubToken: ${{ github.token }} 22 | 23 | # The shell to run commands with in the container 24 | shell: /bin/sh 25 | 26 | # Produce a binary artifact and place it in the mounted volume 27 | run: | 28 | apk update 29 | apk add git build-base gcc make valgrind cmake 30 | mkdir build && cd build 31 | cmake .. -DCMAKE_BUILD_TYPE=Debug -DRESQL_BUILD_TESTS=1 32 | make -j 33 | make check 34 | -------------------------------------------------------------------------------- /.github/workflows/.armv7.yml: -------------------------------------------------------------------------------- 1 | name: armv7 2 | on: 3 | schedule: 4 | - cron: '0 7 * * *' 5 | workflow_dispatch: 6 | jobs: 7 | archs: 8 | # The host should always be linux 9 | runs-on: ubuntu-18.04 10 | name: Build on armv7 11 | steps: 12 | - uses: actions/checkout@v2.1.0 13 | - uses: uraimo/run-on-arch-action@v2.0.9 14 | name: Build artifact 15 | id: build 16 | with: 17 | arch: armv7 18 | distro: buster 19 | 20 | # Not required, but speeds up builds 21 | githubToken: ${{ github.token }} 22 | 23 | # The shell to run commands with in the container 24 | shell: /bin/sh 25 | 26 | # Produce a binary artifact and place it in the mounted volume 27 | run: | 28 | apt-get update -q -y 29 | apt-get install -q -y build-essential git gcc valgrind cmake 30 | uname -a;id;uname -m;lscpu | grep Endian 31 | mkdir build && cd build 32 | cmake .. -DCMAKE_BUILD_TYPE=Debug -DRESQL_BUILD_TESTS=1 33 | make -j 34 | make check 35 | -------------------------------------------------------------------------------- /.github/workflows/.s390x.yml: -------------------------------------------------------------------------------- 1 | name: s390x 2 | on: 3 | schedule: 4 | - cron: '0 11 * * *' 5 | workflow_dispatch: 6 | jobs: 7 | archs: 8 | # The host should always be linux 9 | runs-on: ubuntu-18.04 10 | name: Build on s390x 11 | steps: 12 | - uses: actions/checkout@v2.1.0 13 | - uses: uraimo/run-on-arch-action@v2.0.9 14 | name: Build artifact 15 | id: build 16 | with: 17 | arch: s390x 18 | distro: ubuntu20.04 19 | 20 | # Not required, but speeds up builds 21 | githubToken: ${{ github.token }} 22 | 23 | # The shell to run commands with in the container 24 | shell: /bin/sh 25 | 26 | # Produce a binary artifact and place it in the mounted volume 27 | run: | 28 | apt-get update -q -y 29 | apt-get install -q -y build-essential git gcc valgrind cmake 30 | uname -a;id;uname -m;lscpu | grep Endian 31 | mkdir build && cd build 32 | cmake .. -DCMAKE_BUILD_TYPE=Debug -DRESQL_BUILD_TESTS=1 33 | make -j 34 | make check 35 | -------------------------------------------------------------------------------- /.github/workflows/.aarch64.yml: -------------------------------------------------------------------------------- 1 | name: aarch64 2 | on: 3 | schedule: 4 | - cron: '0 3 * * *' 5 | workflow_dispatch: 6 | jobs: 7 | archs: 8 | # The host should always be linux 9 | runs-on: ubuntu-18.04 10 | name: Build on aarch64 11 | steps: 12 | - uses: actions/checkout@v2.1.0 13 | - uses: uraimo/run-on-arch-action@v2.0.9 14 | name: Build artifact 15 | id: build 16 | with: 17 | arch: aarch64 18 | distro: ubuntu20.04 19 | 20 | # Not required, but speeds up builds 21 | githubToken: ${{ github.token }} 22 | 23 | # The shell to run commands with in the container 24 | shell: /bin/sh 25 | 26 | # Produce a binary artifact and place it in the mounted volume 27 | run: | 28 | apt-get update -q -y 29 | apt-get install -q -y build-essential git gcc valgrind cmake 30 | uname -a;id;uname -m;lscpu | grep Endian 31 | mkdir build && cd build 32 | cmake .. -DCMAKE_BUILD_TYPE=Debug -DRESQL_BUILD_TESTS=1 33 | make -j 34 | make check 35 | -------------------------------------------------------------------------------- /.github/workflows/.ppc64le.yml: -------------------------------------------------------------------------------- 1 | name: ppc64le 2 | on: 3 | schedule: 4 | - cron: '0 9 * * *' 5 | workflow_dispatch: 6 | jobs: 7 | archs: 8 | # The host should always be linux 9 | runs-on: ubuntu-18.04 10 | name: Build on ppc64le 11 | steps: 12 | - uses: actions/checkout@v2.1.0 13 | - uses: uraimo/run-on-arch-action@v2.0.9 14 | name: Build artifact 15 | id: build 16 | with: 17 | arch: ppc64le 18 | distro: ubuntu20.04 19 | 20 | # Not required, but speeds up builds 21 | githubToken: ${{ github.token }} 22 | 23 | # The shell to run commands with in the container 24 | shell: /bin/sh 25 | 26 | # Produce a binary artifact and place it in the mounted volume 27 | run: | 28 | apt-get update -q -y 29 | apt-get install -q -y build-essential git gcc valgrind cmake 30 | uname -a;id;uname -m;lscpu | grep Endian 31 | mkdir build && cd build 32 | cmake .. -DCMAKE_BUILD_TYPE=Debug -DRESQL_BUILD_TESTS=1 33 | make -j 34 | make check 35 | -------------------------------------------------------------------------------- /.cirrus.yml: -------------------------------------------------------------------------------- 1 | ubuntu_task: 2 | use_compute_credits: $CIRRUS_USER_COLLABORATOR == 'true' 3 | container: 4 | image: gcc:8.4.0 5 | cpu: 1 6 | memory: 2G 7 | test_script: 8 | - apt-get update && apt-get install -y gcc cmake 9 | - mkdir build-debug && cd build-debug 10 | - cmake .. -DCMAKE_BUILD_TYPE=Debug -DRESQL_BUILD_TESTS=1 && make 11 | - make check 12 | - rm -rf * 13 | - cd .. 14 | - ./build.sh 15 | 16 | fedora_task: 17 | use_compute_credits: $CIRRUS_USER_COLLABORATOR == 'true' 18 | container: 19 | image: fedora 20 | test_script: 21 | - yum -y install gcc make cmake 22 | - mkdir build-debug && cd build-debug 23 | - cmake .. -DCMAKE_BUILD_TYPE=Debug -DRESQL_BUILD_TESTS=1 && make 24 | - make check 25 | - rm -rf * 26 | - cd .. 27 | - ./build.sh 28 | - 29 | 30 | freebsd_task: 31 | use_compute_credits: $CIRRUS_USER_COLLABORATOR == 'true' 32 | freebsd_instance: 33 | image_family: freebsd-12-1 34 | test_script: 35 | - pkg install -y git cmake 36 | - mkdir build-debug && cd build-debug 37 | - cmake .. -DCMAKE_BUILD_TYPE=Debug -DRESQL_BUILD_TESTS=1 && make 38 | - make check 39 | - rm -rf * 40 | - cd .. 41 | - ./build.sh -------------------------------------------------------------------------------- /util/docker/alpine/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM alpine:3.13 2 | 3 | RUN addgroup -g 11211 resql && adduser -D -u 11211 -G resql resql 4 | 5 | ENV RESQL_VERSION 0.1.4-latest 6 | 7 | RUN set -x \ 8 | \ 9 | && apk add --no-cache --virtual .build-deps \ 10 | ca-certificates \ 11 | coreutils \ 12 | gcc \ 13 | libc-dev \ 14 | linux-headers \ 15 | make \ 16 | cmake \ 17 | \ 18 | && wget -O resql.tar.gz "https://github.com/tezc/resql/archive/v$RESQL_VERSION.tar.gz" \ 19 | && mkdir -p /usr/src/resql \ 20 | && tar -xzf resql.tar.gz -C /usr/src/resql --strip-components=1 \ 21 | && rm resql.tar.gz \ 22 | \ 23 | && cd /usr/src/resql \ 24 | && ./build.sh \ 25 | && ./build.sh --install \ 26 | \ 27 | && cd / && rm -rf /usr/src/resql \ 28 | \ 29 | && runDeps="$( \ 30 | scanelf --needed --nobanner --format '%n#p' --recursive /usr/local \ 31 | | tr ',' '\n' \ 32 | | sort -u \ 33 | | awk 'system("[ -e /usr/local/lib/" $1 " ]") == 0 { next } { print "so:" $1 }' \ 34 | )" \ 35 | && apk add --no-network --virtual .resql-rundeps $runDeps \ 36 | && apk del --no-network .build-deps \ 37 | && resql --version 38 | 39 | RUN mkdir /data && chown resql:resql /data 40 | VOLUME /data 41 | WORKDIR /data 42 | 43 | COPY docker-entrypoint.sh /usr/local/bin/ 44 | ENTRYPOINT ["docker-entrypoint.sh"] 45 | 46 | USER resql 47 | EXPOSE 7600 48 | CMD ["resql", "--node-bind-url=tcp://0.0.0.0:7600"] -------------------------------------------------------------------------------- /cresql/LICENSE: -------------------------------------------------------------------------------- 1 | BSD-3-Clause 2 | 3 | Copyright 2021 Ozan Tezcan 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 1. Redistributions of source code must retain the above copyright notice, 9 | this list of conditions and the following disclaimer. 10 | 2. Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 3. Neither the name of the copyright holder nor the names of its contributors 14 | may be used to endorse or promote products derived from this software 15 | without specific prior written permission. 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 21 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 22 | OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 | POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /goresql/LICENSE: -------------------------------------------------------------------------------- 1 | BSD-3-Clause 2 | 3 | Copyright 2021 Ozan Tezcan 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 1. Redistributions of source code must retain the above copyright notice, 9 | this list of conditions and the following disclaimer. 10 | 2. Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 3. Neither the name of the copyright holder nor the names of its contributors 14 | may be used to endorse or promote products derived from this software 15 | without specific prior written permission. 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 21 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 22 | OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 | POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /jresql/LICENSE: -------------------------------------------------------------------------------- 1 | BSD-3-Clause 2 | 3 | Copyright 2021 Ozan Tezcan 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 1. Redistributions of source code must retain the above copyright notice, 9 | this list of conditions and the following disclaimer. 10 | 2. Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 3. Neither the name of the copyright holder nor the names of its contributors 14 | may be used to endorse or promote products derived from this software 15 | without specific prior written permission. 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 21 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 22 | OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 | POSSIBILITY OF SUCH DAMAGE. 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | BSD-3-Clause 3 | 4 | Copyright 2021 Ozan Tezcan 5 | All rights reserved. 6 | 7 | Redistribution and use in source and binary forms, with or without 8 | modification, are permitted provided that the following conditions are met: 9 | 10 | 1. Redistributions of source code must retain the above copyright notice, 11 | this list of conditions and the following disclaimer. 12 | 2. Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 3. Neither the name of the copyright holder nor the names of its contributors 16 | may be used to endorse or promote products derived from this software 17 | without specific prior written permission. 18 | 19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /util/docker/debian/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM debian:buster-slim 2 | 3 | RUN groupadd --system --gid 11211 resql && useradd --system --gid resql --uid 11211 resql 4 | 5 | ENV RESQL_VERSION 0.1.4-latest 6 | 7 | RUN set -eux; \ 8 | \ 9 | savedAptMark="$(apt-mark showmanual)"; \ 10 | apt-get update; \ 11 | apt-get install -y --no-install-recommends \ 12 | ca-certificates \ 13 | dpkg-dev \ 14 | gcc \ 15 | libc6-dev \ 16 | cmake \ 17 | make \ 18 | wget \ 19 | ; \ 20 | rm -rf /var/lib/apt/lists/*; \ 21 | \ 22 | wget -O resql.tar.gz "https://github.com/tezc/resql/archive/v$RESQL_VERSION.tar.gz"; \ 23 | mkdir -p /usr/src/resql; \ 24 | tar -xzf resql.tar.gz -C /usr/src/resql --strip-components=1; \ 25 | rm resql.tar.gz; \ 26 | \ 27 | cd /usr/src/resql; \ 28 | ./build.sh; \ 29 | ./build.sh --install; \ 30 | \ 31 | cd / && rm -rf /usr/src/resql; \ 32 | \ 33 | apt-mark auto '.*' > /dev/null; \ 34 | [ -z "$savedAptMark" ] || apt-mark manual $savedAptMark > /dev/null; \ 35 | find /usr/local -type f -executable -exec ldd '{}' ';' \ 36 | | awk '/=>/ { print $(NF-1) }' \ 37 | | sort -u \ 38 | | xargs -r dpkg-query --search \ 39 | | cut -d: -f1 \ 40 | | sort -u \ 41 | | xargs -r apt-mark manual \ 42 | ; \ 43 | apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \ 44 | \ 45 | resql --version 46 | 47 | RUN mkdir /data && chown resql:resql /data 48 | VOLUME /data 49 | WORKDIR /data 50 | 51 | COPY docker-entrypoint.sh /usr/local/bin/ 52 | ENTRYPOINT ["docker-entrypoint.sh"] 53 | 54 | USER resql 55 | EXPOSE 7600 56 | CMD ["resql", "--node-bind-url=tcp://0.0.0.0:7600"] 57 | -------------------------------------------------------------------------------- /util/benchmark/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(CMAKE_C_STANDARD 11) 2 | set(CMAKE_C_STANDARD_REQUIRED ON) 3 | set(CMAKE_C_EXTENSIONS OFF) 4 | 5 | find_package(Threads REQUIRED) 6 | set(ADDITIONAL_LIBRARIES ${CMAKE_THREAD_LIBS_INIT} -ldl -lm) 7 | 8 | if ("${CMAKE_SIZEOF_VOID_P}" EQUAL "4") 9 | list(APPEND ADDITIONAL_LIBRARIES -latomic) 10 | endif () 11 | 12 | if (NOT HAVE_CLOCK_GETTIME AND ${CMAKE_SYSTEM_NAME} MATCHES "Linux") 13 | list(APPEND ADDITIONAL_LIBRARIES rt) 14 | endif () 15 | 16 | add_executable(resql-benchmark 17 | resql_benchmark.c 18 | ../../lib/sc/sc_mutex.h 19 | ../../lib/sc/sc_mutex.c 20 | ../../lib/sc/sc_time.h 21 | ../../lib/sc/sc_time.c 22 | ../../lib/sc/sc_thread.h 23 | ../../lib/sc/sc_thread.c 24 | ../../lib/sc/sc_option.h 25 | ../../lib/sc/sc_option.c 26 | ../../lib/hdr/hdr_atomic.h 27 | ../../lib/hdr/hdr_histogram.h 28 | ../../lib/hdr/hdr_histogram.c 29 | ../../cresql/resql.h 30 | ../../cresql/resql.c 31 | ) 32 | 33 | target_link_libraries(resql-benchmark ${ADDITIONAL_LIBRARIES}) 34 | target_include_directories(resql-benchmark PRIVATE ../../cresql) 35 | target_include_directories(resql-benchmark PRIVATE ../../lib/sc) 36 | target_include_directories(resql-benchmark PRIVATE ../../lib/hdr) 37 | 38 | set_source_files_properties( 39 | ../../lib/hdr/hdr_atomic.h 40 | ../../lib/hdr/hdr_histogram.h 41 | ../../lib/hdr/hdr_histogram.c 42 | PROPERTIES 43 | COMPILE_FLAGS "-w" 44 | ) 45 | 46 | install(TARGETS resql-benchmark RUNTIME DESTINATION ${CMAKE_SOURCE_DIR}/bin) 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /.github/workflows/.c.yml: -------------------------------------------------------------------------------- 1 | name: "C client" 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | branches: 9 | - master 10 | jobs: 11 | ubuntu: 12 | runs-on: ubuntu-latest 13 | name: C client test on Ubuntu 14 | 15 | strategy: 16 | fail-fast: false 17 | matrix: 18 | compiler: [ gcc, clang ] 19 | steps: 20 | - uses: actions/checkout@v2.1.0 21 | - name: build 22 | env: 23 | CC: ${{ matrix.compiler }} 24 | run: | 25 | sudo apt update 26 | sudo apt-get install valgrind cmake 27 | ./build.sh 28 | cd bin 29 | ./resql & 30 | cd .. 31 | cd cresql 32 | mkdir build && cd build 33 | cmake .. -DCMAKE_BUILD_TYPE=Debug 34 | make && make check 35 | rm -rf * 36 | cmake .. -DCMAKE_BUILD_TYPE=Debug -DSANITIZER=address 37 | make && make check 38 | rm -rf * 39 | cmake .. -DCMAKE_BUILD_TYPE=Debug -DSANITIZER=undefined 40 | make && make check 41 | rm -rf * 42 | cmake .. -DCMAKE_BUILD_TYPE=Debug 43 | make && make valgrind 44 | macos: 45 | runs-on: macos-latest 46 | name: C client test on Mac OS 47 | 48 | strategy: 49 | fail-fast: false 50 | matrix: 51 | compiler: [ gcc, clang ] 52 | 53 | steps: 54 | - uses: actions/checkout@v2.1.0 55 | - name: build 56 | env: 57 | CC: ${{ matrix.compiler }} 58 | run: | 59 | ./build.sh 60 | cd bin 61 | ./resql & 62 | cd .. 63 | cd cresql 64 | mkdir build && cd build 65 | cmake .. -DCMAKE_BUILD_TYPE=Debug -DSANITIZER=address 66 | make && make check -------------------------------------------------------------------------------- /test/state_test.c: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * BSD-3-Clause 4 | * 5 | * Copyright 2021 Ozan Tezcan 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright notice, 14 | * this list of conditions and the following disclaimer in the documentation 15 | * and/or other materials provided with the distribution. 16 | * 3. Neither the name of the copyright holder nor the names of its contributors 17 | * may be used to endorse or promote products derived from this software 18 | * without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 25 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 26 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 | * POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | #include "server.h" 34 | #include "test_util.h" 35 | 36 | void state_simple() 37 | { 38 | test_server_create(false, 0, 1); 39 | test_client_create(); 40 | } 41 | 42 | int main(void) 43 | { 44 | test_execute(state_simple); 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /jresql/src/main/java/resql/PreparedStatement.java: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package resql; 33 | 34 | public interface PreparedStatement { 35 | 36 | /** 37 | * Get prepared statement sql provided by user 38 | * 39 | * @return Prepared statement sql 40 | */ 41 | String getStatement(); 42 | } 43 | -------------------------------------------------------------------------------- /jresql/src/main/java/resql/ResqlFatalException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package resql; 33 | 34 | public class ResqlFatalException extends ResqlException { 35 | public ResqlFatalException(String s) { 36 | super(s); 37 | } 38 | 39 | public ResqlFatalException(Throwable t) { 40 | super(t); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | #include "conf.h" 33 | #include "server.h" 34 | 35 | int main(int argc, char *argv[]) 36 | { 37 | int rc; 38 | struct conf conf; 39 | 40 | rs_global_init(); 41 | 42 | conf_init(&conf); 43 | conf_read_config(&conf, true, argc, argv); 44 | 45 | rc = server_start_now(&conf); 46 | rs_global_shutdown(); 47 | 48 | return rc; 49 | } 50 | -------------------------------------------------------------------------------- /jresql/src/main/java/resql/ResqlException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package resql; 33 | 34 | /** 35 | * Exception for connection related errors. 36 | */ 37 | public class ResqlException extends RuntimeException { 38 | public ResqlException(String s) { 39 | super(s); 40 | } 41 | 42 | public ResqlException(Throwable t) { 43 | super(t); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /jresql/src/main/java/resql/ResqlSQLException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package resql; 33 | 34 | /** 35 | * Exception for unsuccessful SQL operations. 36 | */ 37 | public class ResqlSQLException extends ResqlException { 38 | public ResqlSQLException(String s) { 39 | super(s); 40 | } 41 | 42 | public ResqlSQLException(Throwable t) { 43 | super(t); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /test/session_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | #include "session.h" 33 | #include "test_util.h" 34 | 35 | #include 36 | #include 37 | #include 38 | 39 | static void session_tests(void) 40 | { 41 | } 42 | 43 | static void session_stmt_tests(void) 44 | { 45 | } 46 | 47 | int main(void) 48 | { 49 | test_execute(session_tests); 50 | test_execute(session_stmt_tests); 51 | 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /test/example/multi_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | #include "resql.h" 33 | #include "rs.h" 34 | #include "server.h" 35 | #include "test_util.h" 36 | 37 | #include 38 | 39 | static void multi() 40 | { 41 | 42 | test_server_create(true, 0, 3); 43 | test_server_create(true, 1, 3); 44 | test_server_create(true, 2, 3); 45 | 46 | pause(); 47 | } 48 | 49 | int main(void) 50 | { 51 | rs_global_init(); 52 | test_execute(multi); 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /jresql/src/main/java/resql/Prepared.java: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package resql; 33 | 34 | class Prepared implements PreparedStatement { 35 | final long id; 36 | final String statement; 37 | 38 | Prepared(long id, String statement) { 39 | this.id = id; 40 | this.statement = statement; 41 | } 42 | 43 | public String getStatement() { 44 | return statement; 45 | } 46 | 47 | public long getId() { 48 | return id; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /lib/sc/sc_crc32.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | #ifndef SC_CRC32_H 33 | #define SC_CRC32_H 34 | 35 | #include 36 | 37 | #define SC_CRC32_VERSION "2.0.0" 38 | 39 | /** 40 | * Call once globally. 41 | */ 42 | void sc_crc32_init(void); 43 | 44 | /** 45 | * @param crc initial value, if you're not calculating crc from partial buffers, 46 | * it should be zero. 47 | * @param buf buf 48 | * @param len len 49 | * @return crc value 50 | */ 51 | uint32_t sc_crc32(uint32_t crc, const uint8_t *buf, uint32_t len); 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /test/entry_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | #include "entry.h" 33 | #include "test_util.h" 34 | 35 | #include "sc/sc_buf.h" 36 | 37 | #include 38 | 39 | void encode_test() 40 | { 41 | struct sc_buf buf; 42 | 43 | sc_buf_init(&buf, 1024); 44 | 45 | for (uint64_t i = 100000000000; i < 100000000000 + 100000; i++) { 46 | entry_encode(&buf, 0, 1, 2, 3, "test", 5); 47 | assert(entry_decode(&buf) == RS_OK); 48 | 49 | if (i % 4096 == 0) { 50 | sc_buf_compact(&buf); 51 | } 52 | } 53 | 54 | sc_buf_term(&buf); 55 | } 56 | 57 | int main() 58 | { 59 | test_execute(encode_test); 60 | } 61 | -------------------------------------------------------------------------------- /src/info.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | #ifndef RESQL_INFO_H 32 | #define RESQL_INFO_H 33 | 34 | #include "sc/sc_buf.h" 35 | 36 | struct info { 37 | char *name; 38 | char *urls; 39 | char *connected; 40 | char *role; 41 | 42 | struct sc_buf stats; 43 | }; 44 | 45 | struct info *info_create(const char *name); 46 | void info_destroy(struct info *n); 47 | 48 | void info_set_connected(struct info *n, bool connected); 49 | void info_set_role(struct info *n, const char *role); 50 | void info_set_urls(struct info *n, const char *urls); 51 | void info_set_stats(struct info *n, void *data, uint32_t len); 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /jresql/src/main/java/resql/ResqlClient.java: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package resql; 33 | 34 | public final class ResqlClient { 35 | 36 | private ResqlClient() { 37 | 38 | } 39 | 40 | /** 41 | * Create client and connect to server. Client will try to connect to server 42 | * at most timeout milliseconds which is configured in 'config'. 43 | * 44 | * @param config config 45 | * @return Resql client 46 | */ 47 | public static Resql create(Config config) { 48 | try { 49 | return new Client(config); 50 | } catch (Exception e) { 51 | if (e instanceof ResqlException) { 52 | throw e; 53 | } 54 | 55 | throw new ResqlException(e); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /test/example/single_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | #include "server.h" 33 | #include "test_util.h" 34 | 35 | #include "sc/sc_str.h" 36 | #include 37 | 38 | static void single() 39 | { 40 | struct conf conf; 41 | 42 | conf_init(&conf); 43 | sc_str_set(&conf.node.dir, "/tmp/testx2"); 44 | sc_str_set(&conf.node.name, "node5"); 45 | sc_str_set(&conf.node.bind_url, "tcp://node5@127.0.0.1:7600"); 46 | sc_str_set(&conf.node.ad_url, "tcp://node5@127.0.0.1:7600"); 47 | sc_str_set(&conf.cluster.nodes, "tcp://node5@127.0.0.1:7600"); 48 | conf.node.in_memory = false; 49 | conf.cmdline.backup = true; 50 | 51 | test_server_create_conf(&conf, 0); 52 | pause(); 53 | } 54 | 55 | int main(void) 56 | { 57 | rs_global_init(); 58 | test_execute(single); 59 | 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /src/config.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | #ifndef RESQL_CONFIG_H 33 | #define RESQL_CONFIG_H 34 | 35 | #include "rs.h" 36 | 37 | #define sc_array_realloc rs_realloc 38 | #define sc_array_free rs_free 39 | 40 | #define sc_buf_calloc rs_calloc 41 | #define sc_buf_realloc rs_realloc 42 | #define sc_buf_free rs_free 43 | 44 | #define sc_map_calloc rs_calloc 45 | #define sc_map_free rs_free 46 | 47 | #define sc_queue_calloc rs_calloc 48 | #define sc_queue_free rs_free 49 | 50 | #define sc_str_malloc rs_malloc 51 | #define sc_str_realloc rs_realloc 52 | #define sc_str_free rs_free 53 | 54 | #define sc_sock_malloc malloc 55 | #define sc_sock_realloc realloc 56 | #define sc_sock_free free 57 | 58 | #define sc_timer_malloc rs_malloc 59 | #define sc_timer_free rs_free 60 | 61 | #define sc_uri_malloc rs_malloc 62 | #define sc_uri_free rs_free 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /.github/workflows/.ubuntu.yml: -------------------------------------------------------------------------------- 1 | name: "Ubuntu" 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | branches: 9 | - master 10 | jobs: 11 | ubuntu: 12 | runs-on: ubuntu-latest 13 | name: Build on Ubuntu 14 | 15 | strategy: 16 | fail-fast: false 17 | matrix: 18 | compiler: [ gcc, clang ] 19 | steps: 20 | - uses: actions/checkout@v2.1.0 21 | - name: build 22 | env: 23 | CC: ${{ matrix.compiler }} 24 | run: | 25 | sudo apt update 26 | sudo apt-get install valgrind cmake libc6-dev-i386 27 | mkdir build-debug && cd build-debug 28 | cmake .. -DCMAKE_BUILD_TYPE=Debug -DRESQL_BUILD_TESTS=1 -DSANITIZER=undefined 29 | make -j 30 | make check 31 | rm -rf * 32 | cmake .. -DCMAKE_BUILD_TYPE=Debug -DRESQL_BUILD_TESTS=1 -DSANITIZER=address 33 | make -j 34 | make check 35 | rm -rf * 36 | cd .. 37 | ./build.sh 38 | 39 | ubuntu-32bit: 40 | runs-on: ubuntu-latest 41 | name: Build on Ubuntu 32 bit 42 | 43 | strategy: 44 | fail-fast: false 45 | matrix: 46 | compiler: [ gcc ] 47 | steps: 48 | - uses: actions/checkout@v2.1.0 49 | - name: build 50 | env: 51 | CC: ${{ matrix.compiler }} 52 | run: | 53 | sudo apt update 54 | sudo apt-get install valgrind cmake libc6-dev-i386 55 | mkdir build-debug && cd build-debug 56 | cmake .. -DCMAKE_BUILD_TYPE=Debug -DRESQL_BUILD_TESTS=1 -DSANITIZER=undefined -DCMAKE_C_FLAGS=-m32 57 | make -j 58 | make check 59 | rm -rf * 60 | cmake .. -DCMAKE_BUILD_TYPE=Debug -DRESQL_BUILD_TESTS=1 -DSANITIZER=address -DCMAKE_C_FLAGS=-m32 61 | make -j 62 | make check 63 | rm -rf * 64 | cd .. 65 | ./build.sh 66 | 67 | ubuntu-release-build: 68 | runs-on: ubuntu-latest 69 | name: Release build on Ubuntu 70 | 71 | strategy: 72 | fail-fast: false 73 | matrix: 74 | compiler: [ gcc, clang ] 75 | steps: 76 | - uses: actions/checkout@v2.1.0 77 | - name: build 78 | env: 79 | CC: ${{ matrix.compiler }} 80 | run: | 81 | sudo apt update 82 | sudo apt-get install valgrind cmake libc6-dev-i386 83 | mkdir build-release && cd build-release 84 | cmake .. -DRESQL_BUILD_TESTS=1 85 | make -j 86 | make check -------------------------------------------------------------------------------- /jresql/src/main/java/resql/ResultSet.java: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package resql; 33 | 34 | public interface ResultSet extends Iterable { 35 | 36 | /** 37 | * Advance to the next result set. 38 | * 39 | * @return 'true' if next result set exists. 40 | */ 41 | boolean nextResultSet(); 42 | 43 | /** 44 | * @return Number of lines changed for the current INSERT, UPDATE or DELETE 45 | * statement. For other statements, returned value is unspecified. 46 | */ 47 | int linesChanged(); 48 | 49 | /** 50 | * @return Last insert row id for INSERT statements. For other statements, 51 | * returned value is unspecified. 52 | */ 53 | long lastRowId(); 54 | 55 | /** 56 | * Get row count for the current result set, -1 if not applicable 57 | * 58 | * @return row count. 59 | */ 60 | int rowCount(); 61 | } 62 | -------------------------------------------------------------------------------- /src/conf.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | #ifndef RESQL_CONF_H 33 | #define RESQL_CONF_H 34 | 35 | #include 36 | #include 37 | 38 | struct conf { 39 | struct { 40 | char *name; 41 | char *bind_url; 42 | char *ad_url; 43 | char *source_addr; 44 | char *source_port; 45 | char *log_level; 46 | char *log_dest; 47 | char *dir; 48 | bool in_memory; 49 | } node; 50 | 51 | struct { 52 | char *name; 53 | char *nodes; 54 | } cluster; 55 | 56 | struct { 57 | bool fsync; 58 | uint64_t heartbeat; 59 | } advanced; 60 | 61 | struct { 62 | bool systemd; 63 | bool backup; 64 | char *config_file; 65 | } cmdline; 66 | 67 | char err[128]; 68 | }; 69 | 70 | void conf_init(struct conf *c); 71 | void conf_term(struct conf *c); 72 | 73 | void conf_read_config(struct conf *c, bool read_file, int argc, char **argv); 74 | void conf_print(struct conf *c); 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /lib/sc/sc_time.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | #ifndef SC_TIME_H 32 | #define SC_TIME_H 33 | 34 | #define SC_TIME_VERSION "2.0.0" 35 | 36 | #include 37 | 38 | /** 39 | * Wall clock time. Gets CLOCK_REALTIME on Posix. 40 | * @return current timestamp in milliseconds. 41 | */ 42 | uint64_t sc_time_ms(); 43 | 44 | /** 45 | * Wall clock time. Gets CLOCK_REALTIME on Posix. 46 | * @return current timestamp in nanoseconds. 47 | */ 48 | uint64_t sc_time_ns(); 49 | 50 | /** 51 | * Monotonic timer. Gets CLOCK_MONOTONIC on Posix 52 | * @return current timestamp in milliseconds. 53 | */ 54 | uint64_t sc_time_mono_ms(); 55 | 56 | /** 57 | * Monotonic timer. Gets CLOCK_MONOTONIC on Posix 58 | * @return Current timestamp in nanoseconds. 59 | */ 60 | uint64_t sc_time_mono_ns(); 61 | 62 | /** 63 | * @param millis milliseconds to sleep. 64 | * @return '0' on success, negative on failure. 65 | */ 66 | int sc_time_sleep(uint64_t millis); 67 | 68 | #endif 69 | -------------------------------------------------------------------------------- /.github/workflows/codeql-analysis.yml: -------------------------------------------------------------------------------- 1 | # For most projects, this workflow file will not need changing; you simply need 2 | # to commit it to your repository. 3 | # 4 | # You may wish to alter this file to override the set of languages analyzed, 5 | # or to provide custom queries or build logic. 6 | # 7 | # ******** NOTE ******** 8 | # We have attempted to detect the languages in your repository. Please check 9 | # the `language` matrix defined below to confirm you have the correct set of 10 | # supported CodeQL languages. 11 | # 12 | name: "CodeQL" 13 | 14 | on: 15 | push: 16 | branches: [ master ] 17 | pull_request: 18 | # The branches below must be a subset of the branches above 19 | branches: [ master ] 20 | schedule: 21 | - cron: '22 20 * * 3' 22 | 23 | jobs: 24 | analyze: 25 | name: Analyze 26 | runs-on: ubuntu-latest 27 | 28 | strategy: 29 | fail-fast: false 30 | matrix: 31 | language: [ 'cpp', 'go', 'java' ] 32 | # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ] 33 | # Learn more: 34 | # https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed 35 | 36 | steps: 37 | - name: Checkout repository 38 | uses: actions/checkout@v2 39 | 40 | # Initializes the CodeQL tools for scanning. 41 | - name: Initialize CodeQL 42 | uses: github/codeql-action/init@v1 43 | with: 44 | languages: ${{ matrix.language }} 45 | # If you wish to specify custom queries, you can do so here or in a config file. 46 | # By default, queries listed here will override any specified in a config file. 47 | # Prefix the list here with "+" to use these queries and those in the config file. 48 | # queries: ./path/to/local/query, your-org/your-repo/queries@main 49 | 50 | # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). 51 | # If this step fails, then you should remove it and run the build manually (see below) 52 | - name: Autobuild 53 | uses: github/codeql-action/autobuild@v1 54 | 55 | # ℹ️ Command-line programs to run using the OS shell. 56 | # 📚 https://git.io/JvXDl 57 | 58 | # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines 59 | # and modify them (or add more) to build your code if your project 60 | # uses a compiled language 61 | 62 | #- run: | 63 | # make bootstrap 64 | # make release 65 | 66 | - name: Perform CodeQL Analysis 67 | uses: github/codeql-action/analyze@v1 68 | -------------------------------------------------------------------------------- /src/metric.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | #ifndef RESQL_METRIC_H 33 | #define RESQL_METRIC_H 34 | 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | 41 | struct sc_buf; 42 | 43 | struct metric { 44 | uint64_t total_memory; 45 | struct utsname utsname; 46 | int64_t pid; 47 | uint64_t start_time; 48 | char start[64]; 49 | uint64_t bytes_recv; 50 | uint64_t bytes_sent; 51 | 52 | uint64_t fsync_total; 53 | uint64_t fsync_count; 54 | uint64_t fsync_max; 55 | 56 | uint64_t ss_total; 57 | uint64_t ss_count; 58 | uint64_t ss_max; 59 | size_t ss_size; 60 | bool ss_success; 61 | 62 | char dir[PATH_MAX]; 63 | }; 64 | 65 | int metric_init(struct metric *m, const char *dir); 66 | void metric_term(struct metric *m); 67 | void metric_encode(struct metric *m, struct sc_buf *buf); 68 | void metric_recv(int64_t val); 69 | void metric_send(int64_t val); 70 | void metric_fsync(uint64_t val); 71 | void metric_snapshot(bool success, uint64_t time, size_t size); 72 | 73 | #endif 74 | -------------------------------------------------------------------------------- /lib/sc/sc_option.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | #ifndef SC_OPTION_H 32 | #define SC_OPTION_H 33 | 34 | #include 35 | #include 36 | #include 37 | 38 | #define SC_OPTION_VERSION "2.0.0" 39 | 40 | struct sc_option_item { 41 | const char letter; 42 | const char *name; 43 | }; 44 | 45 | struct sc_option { 46 | struct sc_option_item *options; 47 | int count; 48 | char **argv; 49 | }; 50 | 51 | /** 52 | * 53 | * @param opt Already initialized sc_opt struct 54 | * @param index Index for argv 55 | * @param value [out] Value for the option if exists. It should be after '=' 56 | * sign. E.g : -key=value or -k=value. If value does not exists 57 | * (*value) will point to '\0' character. It won't be NULL itself. 58 | * 59 | * To check if option has value associated : if (*value != '\0') 60 | * 61 | * @return Letter for the option. If option is not known, '?' will be 62 | * returned. 63 | */ 64 | char sc_option_at(struct sc_option *opt, int index, char **value); 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /src/info.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | #include "info.h" 33 | 34 | #include "rs.h" 35 | 36 | #include "sc/sc_str.h" 37 | 38 | struct info *info_create(const char *name) 39 | { 40 | struct info *n; 41 | 42 | n = rs_calloc(1, sizeof(*n)); 43 | 44 | n->name = sc_str_create(name); 45 | sc_buf_init(&n->stats, 1024); 46 | 47 | return n; 48 | } 49 | 50 | void info_destroy(struct info *n) 51 | { 52 | sc_str_destroy(&n->name); 53 | sc_str_destroy(&n->urls); 54 | sc_str_destroy(&n->connected); 55 | sc_str_destroy(&n->role); 56 | sc_buf_term(&n->stats); 57 | rs_free(n); 58 | } 59 | 60 | void info_set_connected(struct info *n, bool connected) 61 | { 62 | sc_str_set(&n->connected, connected ? "true" : "false"); 63 | } 64 | 65 | void info_set_role(struct info *n, const char *role) 66 | { 67 | sc_str_set(&n->role, role); 68 | } 69 | 70 | void info_set_urls(struct info *n, const char *urls) 71 | { 72 | sc_str_set(&n->urls, urls); 73 | } 74 | 75 | void info_set_stats(struct info *n, void *data, uint32_t len) 76 | { 77 | sc_buf_clear(&n->stats); 78 | sc_buf_put_raw(&n->stats, data, len); 79 | } 80 | -------------------------------------------------------------------------------- /src/session.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | #ifndef RESQL_SESSION_H 33 | #define RESQL_SESSION_H 34 | 35 | #include "sc/sc_buf.h" 36 | #include "sc/sc_list.h" 37 | #include "sc/sc_map.h" 38 | 39 | #include 40 | 41 | struct session { 42 | struct state *state; 43 | struct sc_list list; 44 | char *name; 45 | char *local; 46 | char *remote; 47 | char *connect_time; 48 | uint64_t id; 49 | uint64_t seq; 50 | uint64_t disconnect_time; 51 | 52 | struct sc_buf resp; 53 | struct sc_map_64v stmts; 54 | }; 55 | 56 | struct session *session_create(struct state *st, const char *name, uint64_t id); 57 | void session_destroy(struct session *s); 58 | 59 | void session_connected(struct session *s, const char *local, const char *remote, 60 | uint64_t ts); 61 | void session_disconnected(struct session *s, uint64_t timestamp); 62 | 63 | uint64_t session_sql_to_id(struct session *s, const char *sql); 64 | 65 | void session_add_stmt(struct session *s, uint64_t id, void *stmt); 66 | void *session_get_stmt(struct session *s, uint64_t id); 67 | int session_del_stmt(struct session *s, uint64_t id); 68 | 69 | #endif 70 | -------------------------------------------------------------------------------- /src/entry.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | #ifndef RESQL_ENTRY_H 33 | #define RESQL_ENTRY_H 34 | 35 | #include 36 | #include 37 | 38 | #define ENTRY_CRC_LEN 4 39 | #define ENTRY_HEADER_SIZE 36 40 | 41 | struct sc_buf; 42 | 43 | void entry_encode(struct sc_buf *b, uint64_t term, uint64_t seq, uint64_t cid, 44 | uint32_t flags, void *data, uint32_t len); 45 | 46 | int entry_decode(struct sc_buf *b); 47 | 48 | uint32_t entry_encoded_len(uint32_t len); 49 | uint32_t entry_crc(unsigned char *e); 50 | uint32_t entry_len(unsigned char *e); 51 | uint64_t entry_term(unsigned char *e); 52 | uint64_t entry_seq(unsigned char *e); 53 | 54 | uint64_t entry_cid(unsigned char *e); 55 | uint32_t entry_flags(unsigned char *e); 56 | 57 | void *entry_data(unsigned char *e); 58 | uint32_t entry_data_len(unsigned char *e); 59 | 60 | #define entry_foreach(buf, len, entry) \ 61 | for ((entry) = ((unsigned char *) (buf)); \ 62 | (entry) < ((unsigned char *) (buf)) + (len); \ 63 | (entry) += entry_len(entry)) 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /jresql/src/test/java/resql/benchmark/LatencyTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package resql.benchmark; 33 | 34 | import org.junit.jupiter.api.AfterEach; 35 | import org.junit.jupiter.api.BeforeEach; 36 | import org.junit.jupiter.api.Test; 37 | import resql.Config; 38 | import resql.Resql; 39 | import resql.ResqlClient; 40 | 41 | public class LatencyTest { 42 | Resql client; 43 | 44 | @BeforeEach 45 | public void setup() { 46 | client = ResqlClient.create(new Config()); 47 | client.put("DROP TABLE IF EXISTS basic;"); 48 | client.put("CREATE TABLE basic (name TEXT, lastname TEXT);"); 49 | client.execute(false); 50 | } 51 | 52 | @AfterEach 53 | public void tearDown() { 54 | if (client != null) { 55 | client.shutdown(); 56 | } 57 | } 58 | 59 | //@Test 60 | public void latency() { 61 | for (int i = 0; i < 10000000; i++) { 62 | long ts = System.nanoTime(); 63 | client.put("Select '1';"); 64 | client.execute(true); 65 | System.out.println("Took " + (System.nanoTime() - ts)); 66 | } 67 | 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /lib/sc/sc_mutex.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | #ifndef SC_MUTEX_H 33 | #define SC_MUTEX_H 34 | 35 | #define SC_MUTEX_VERSION "2.0.0" 36 | 37 | #if defined(_WIN32) || defined(_WIN64) 38 | #include 39 | #else 40 | #include 41 | #endif 42 | 43 | struct sc_mutex { 44 | #if defined(_WIN32) || defined(_WIN64) 45 | CRITICAL_SECTION mtx; 46 | #else 47 | pthread_mutex_t mtx; 48 | #endif 49 | }; 50 | 51 | /** 52 | * Create mutex. 53 | * 54 | * Be warned on Windows, mutexes are recursive, on Posix default 55 | * mutex type is not recursive. Edit code if that bothers you. Pass 56 | * PTHREAD_MUTEX_RECURSIVE instead of PTHREAD_MUTEX_NORMAL. 57 | * 58 | * @param mtx mtx 59 | * @return '0' on success, '-1' on error. 60 | */ 61 | int sc_mutex_init(struct sc_mutex *mtx); 62 | 63 | /** 64 | * Destroy mutex 65 | * 66 | * @param mtx mtx 67 | * @return '0' on success, '-1' on error. 68 | */ 69 | int sc_mutex_term(struct sc_mutex *mtx); 70 | 71 | /** 72 | * @param mtx mtx 73 | */ 74 | void sc_mutex_lock(struct sc_mutex *mtx); 75 | 76 | /** 77 | * @param mtx mtx 78 | */ 79 | void sc_mutex_unlock(struct sc_mutex *mtx); 80 | 81 | #endif 82 | -------------------------------------------------------------------------------- /test/config_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | #include "conf.h" 33 | #include "test_util.h" 34 | 35 | #define call_param_test(...) \ 36 | (param_test(((char *[]){"", __VA_ARGS__}), \ 37 | sizeof(((char *[]){"", __VA_ARGS__})) / sizeof(char *))) 38 | 39 | static void param_test(char *opt[], size_t len) 40 | { 41 | struct conf conf; 42 | 43 | conf_init(&conf); 44 | conf_read_config(&conf, false, (int) len, opt); 45 | test_server_create_conf(&conf, 0); 46 | } 47 | 48 | static void param_test1(void) 49 | { 50 | call_param_test("--node-name=node2", 51 | "--node-bind-url=tcp://node2@127.0.0.1:7600", 52 | "--node-advertise-url=tcp://node2@127.0.0.1:7600", 53 | "--node-source-addr=127.0.0.1", 54 | "--node-source-port=9000", "--node-log-level=debug", 55 | "--node-log-destination=stdout", "--node-directory=.", 56 | "--node-in-memory=true", "--cluster-name=cluster", 57 | "--cluster-nodes=tcp://node2@127.0.0.1:7600", 58 | "--advanced-fsync=true", "--advanced-heartbeat=1000"); 59 | } 60 | 61 | int main(void) 62 | { 63 | test_execute(param_test1); 64 | 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /lib/sc/sc_cond.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | #ifndef SC_COND_H 33 | #define SC_COND_H 34 | 35 | #include 36 | 37 | #define SC_COND_VERSION "2.0.0" 38 | 39 | #if defined(_WIN32) || defined(_WIN64) 40 | #include 41 | #else 42 | #include 43 | #endif 44 | 45 | struct sc_cond { 46 | bool init; 47 | bool done; 48 | void *data; 49 | 50 | #if defined(_WIN32) || defined(_WIN64) 51 | CONDITION_VARIABLE cond; 52 | CRITICAL_SECTION mtx; 53 | #else 54 | pthread_cond_t cond; 55 | pthread_mutex_t mtx; 56 | #endif 57 | }; 58 | 59 | /** 60 | * @param c cond 61 | * @return '0' on success, negative on error, errno will be set. 62 | */ 63 | int sc_cond_init(struct sc_cond *c); 64 | 65 | /** 66 | * @param c cond 67 | * @return '0' on success, negative on error, errno will be set. 68 | */ 69 | int sc_cond_term(struct sc_cond *c); 70 | 71 | /** 72 | * @param c cond 73 | * @param data data to pass to thread which will call 'sc_cond_wait'. 74 | */ 75 | void sc_cond_signal(struct sc_cond *c, void *data); 76 | 77 | /** 78 | * @param c cond 79 | * @return 'user data'.'data' argument on previous sc_cond_signal() call 80 | */ 81 | void *sc_cond_wait(struct sc_cond *c); 82 | 83 | #endif 84 | -------------------------------------------------------------------------------- /lib/sc/sc_option.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | #include "sc_option.h" 33 | 34 | #include 35 | 36 | char sc_option_at(struct sc_option *opt, int index, char **value) 37 | { 38 | char id = '?'; 39 | size_t len; 40 | char *pos; 41 | const char *curr, *name; 42 | 43 | pos = opt->argv[index]; 44 | *value = NULL; 45 | 46 | if (*pos != '-') { 47 | return id; 48 | } 49 | 50 | pos++; // Skip first '-' 51 | if (*pos != '-') { 52 | for (int i = 0; i < opt->count; i++) { 53 | if (*pos == opt->options[i].letter && 54 | strchr("= \0", *(pos + 1)) != NULL) { 55 | id = *pos; 56 | pos++; // skip letter 57 | *value = pos + (*pos != '=' ? 0 : 1); 58 | break; 59 | } 60 | } 61 | } else { 62 | while (*pos && *pos != '=') { 63 | pos++; 64 | } 65 | 66 | for (int i = 0; i < opt->count; i++) { 67 | curr = opt->argv[index] + 2; // Skip '--' 68 | name = opt->options[i].name; 69 | len = (pos - curr); 70 | 71 | if (name == NULL) { 72 | continue; 73 | } 74 | 75 | if (len == strlen(name) && 76 | memcmp(name, curr, len) == 0) { 77 | id = opt->options[i].letter; 78 | *value = pos + (*pos != '=' ? 0 : 1); 79 | break; 80 | } 81 | } 82 | } 83 | 84 | return id; 85 | } 86 | -------------------------------------------------------------------------------- /jresql/src/main/java/resql/Row.java: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package resql; 33 | 34 | public interface Row { 35 | 36 | /** 37 | * Get column value by name. 38 | * 39 | * Valid return types are Long, Double, String and byte[]. 40 | * If column value is null, return value is null. 41 | * If column name does not exist, throws ResqlSqlException. 42 | * 43 | * @param columnName column name 44 | * @param return type 45 | * @return column value 46 | */ 47 | T get(String columnName); 48 | 49 | /** 50 | * Get column value by index. 51 | * 52 | * Valid return types are Long, Double, String and byte[]. 53 | * If column value is null, return value is null. 54 | * If index is out of range, throws ResqlSqlException. 55 | * 56 | * @param index column index 57 | * @param return type 58 | * @return column value 59 | */ 60 | T get(int index); 61 | 62 | /** 63 | * Get column name by index. 64 | * If index is out of range, throws ResqlSqlException. 65 | * 66 | * @param index index 67 | * @return column name 68 | */ 69 | String getColumnName(int index); 70 | 71 | /** 72 | * Get column count 73 | * @return column count 74 | */ 75 | int columnCount(); 76 | } 77 | -------------------------------------------------------------------------------- /lib/sc/sc_thread.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | #ifndef SC_THREAD_H 33 | #define SC_THREAD_H 34 | 35 | #define SC_THREAD_VERSION "2.0.0" 36 | 37 | #if defined(_WIN32) || defined(_WIN64) 38 | 39 | #include 40 | 41 | struct sc_thread { 42 | HANDLE id; 43 | void *(*fn)(void *); 44 | void *arg; 45 | void *ret; 46 | char err[64]; 47 | }; 48 | 49 | #else 50 | 51 | #include 52 | 53 | struct sc_thread { 54 | pthread_t id; 55 | char err[128]; 56 | }; 57 | 58 | #endif 59 | 60 | /** 61 | * @param t thread 62 | */ 63 | void sc_thread_init(struct sc_thread *t); 64 | 65 | /** 66 | * @param t thread 67 | * @return '0' on success, 68 | * '-1' on error, call 'sc_thread_err()' for error string. 69 | */ 70 | int sc_thread_term(struct sc_thread *t); 71 | 72 | /** 73 | * @param t thread 74 | * @return last error message 75 | */ 76 | const char *sc_thread_err(struct sc_thread *t); 77 | 78 | /** 79 | * @param t thread 80 | * @return '0' on success, 81 | * '-1' on error, call 'sc_thread_err()' for error string. 82 | */ 83 | int sc_thread_start(struct sc_thread *t, void *(*fn)(void *), void *arg); 84 | 85 | /** 86 | * @param t thread 87 | * @return '0' on success, 88 | * '-1' on error, call 'sc_thread_err()' for error string. 89 | */ 90 | int sc_thread_join(struct sc_thread *t, void **ret); 91 | 92 | #endif 93 | -------------------------------------------------------------------------------- /src/file.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | #ifndef RESQL_FILE_H 33 | #define RESQL_FILE_H 34 | 35 | #include 36 | #include 37 | #include 38 | 39 | struct file { 40 | char *path; 41 | FILE *fp; 42 | }; 43 | 44 | struct file *file_create(); 45 | void file_destroy(struct file *file); 46 | 47 | void file_init(struct file *file); 48 | int file_term(struct file *file); 49 | int file_open(struct file *file, const char *path, const char *mode); 50 | int file_close(struct file *file); 51 | 52 | ssize_t file_size(struct file *file); 53 | 54 | int file_write(struct file *file, const void *ptr, size_t size); 55 | int file_write_at(struct file *file, size_t off, const void *ptr, size_t size); 56 | int file_read(struct file *file, void *ptr, size_t size); 57 | int file_lseek(struct file *file, size_t offset); 58 | const char *file_path(struct file *file); 59 | int file_remove(struct file *file); 60 | int file_flush(struct file *file); 61 | 62 | ssize_t file_size_at(const char *path); 63 | bool file_exists_at(const char *path); 64 | int file_remove_path(const char *path); 65 | int file_unlink(const char *path); 66 | int file_mkdir(const char *path); 67 | int file_rmdir(const char *path); 68 | int file_clear_dir(const char *path, const char *pattern); 69 | int file_copy(const char *dst, const char *src); 70 | int file_rename(const char *dst, const char *src); 71 | int file_fsync(const char *path); 72 | 73 | #endif 74 | -------------------------------------------------------------------------------- /lib/linenoise/linenoise.h: -------------------------------------------------------------------------------- 1 | /* linenoise.h -- VERSION 1.0 2 | * 3 | * Guerrilla line editing library against the idea that a line editing lib 4 | * needs to be 20,000 lines of C code. 5 | * 6 | * See linenoise.c for more information. 7 | * 8 | * ------------------------------------------------------------------------ 9 | * 10 | * Copyright (c) 2010-2014, Salvatore Sanfilippo 11 | * Copyright (c) 2010-2013, Pieter Noordhuis 12 | * 13 | * All rights reserved. 14 | * 15 | * Redistribution and use in source and binary forms, with or without 16 | * modification, are permitted provided that the following conditions are 17 | * met: 18 | * 19 | * * Redistributions of source code must retain the above copyright 20 | * notice, this list of conditions and the following disclaimer. 21 | * 22 | * * Redistributions in binary form must reproduce the above copyright 23 | * notice, this list of conditions and the following disclaimer in the 24 | * documentation and/or other materials provided with the distribution. 25 | * 26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 27 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 28 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 29 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 30 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 31 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 32 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 33 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 34 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 35 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 36 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 37 | */ 38 | 39 | #ifndef __LINENOISE_H 40 | #define __LINENOISE_H 41 | 42 | #include 43 | 44 | typedef struct linenoiseCompletions { 45 | size_t len; 46 | char **cvec; 47 | } linenoiseCompletions; 48 | 49 | typedef void(linenoiseCompletionCallback)(const char *, linenoiseCompletions *); 50 | typedef char*(linenoiseHintsCallback)(const char *, int *color, int *bold); 51 | typedef void(linenoiseFreeHintsCallback)(void *); 52 | void linenoiseSetCompletionCallback(linenoiseCompletionCallback *); 53 | void linenoiseSetHintsCallback(linenoiseHintsCallback *); 54 | void linenoiseSetFreeHintsCallback(linenoiseFreeHintsCallback *); 55 | void linenoiseAddCompletion(linenoiseCompletions *, const char *); 56 | 57 | char *linenoise(const char *prompt); 58 | void linenoiseFree(void *ptr); 59 | int linenoiseHistoryAdd(const char *line); 60 | int linenoiseHistorySetMaxLen(int len); 61 | int linenoiseHistorySave(const char *filename); 62 | int linenoiseHistoryLoad(const char *filename); 63 | void linenoiseClearScreen(void); 64 | void linenoiseSetMultiLine(int ml); 65 | void linenoisePrintKeyCodes(void); 66 | void linenoiseMaskModeEnable(void); 67 | void linenoiseMaskModeDisable(void); 68 | 69 | #endif 70 | -------------------------------------------------------------------------------- /src/client.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | #ifndef RESQL_CLIENT_H 33 | #define RESQL_CLIENT_H 34 | 35 | #include "conn.h" 36 | #include "msg.h" 37 | 38 | #include "sc/sc_buf.h" 39 | #include "sc/sc_sock.h" 40 | #include "sc/sc_str.h" 41 | 42 | struct client { 43 | bool msg_wait; // msg in-progress 44 | bool terminated; // waiting to be deallocated 45 | 46 | char *name; 47 | uint64_t id; 48 | uint64_t seq; // current sequence 49 | uint64_t commit_index; // round index for read request 50 | uint64_t round_index; // round index for read request 51 | 52 | struct conn conn; 53 | struct sc_list read; // read request list 54 | struct msg msg; // current msg 55 | }; 56 | 57 | struct client *client_create(struct conn *conn, const char *name); 58 | void client_destroy(struct client *c); 59 | void client_print(struct client *c, char *buf, size_t len); 60 | int client_processed(struct client *c); 61 | bool client_pending(struct client *c); 62 | 63 | /** 64 | * Mark client terminated for lazy destroy. 65 | * 66 | * When looping on epoll_wait, an event may cause a client to disconnect but 67 | * we may have collected that client socket's event but not yet processed. 68 | * This will trigger use-after-free undefined behavior. Rather than destroying 69 | * client, mark it terminated and call client_destroy() when you processed all 70 | * events from epoll_wait. 71 | */ 72 | void client_set_terminated(struct client *c); 73 | 74 | #endif 75 | -------------------------------------------------------------------------------- /src/store.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | #ifndef RESQL_STORE_H 33 | #define RESQL_STORE_H 34 | 35 | #include "page.h" 36 | 37 | #include 38 | #include 39 | 40 | struct store { 41 | char *path; 42 | 43 | uint64_t ss_index; 44 | uint64_t ss_term; 45 | bool ss_inprogress; 46 | 47 | struct page *pages[2]; 48 | struct page *curr; 49 | 50 | uint64_t last_term; 51 | uint64_t last_index; 52 | }; 53 | 54 | int store_init(struct store *s, const char *path, uint64_t ss_term, 55 | uint64_t ss_index); 56 | void store_term(struct store *s); 57 | 58 | void store_flush(struct store *s); 59 | void store_snapshot_taken(struct store *s); 60 | 61 | uint64_t store_ss_index(struct store *s); 62 | struct page *store_ss_page(struct store *s); 63 | int store_reserve(struct store *s, uint32_t size); 64 | bool store_last_part(struct store *s); 65 | 66 | int store_create_entry(struct store *s, uint64_t term, uint64_t seq, 67 | uint64_t cid, uint32_t flags, void *data, uint32_t len); 68 | 69 | int store_put_entry(struct store *store, uint64_t index, unsigned char *entry); 70 | 71 | unsigned char *store_get_entry(struct store *s, uint64_t index); 72 | uint64_t store_prev_term(struct store *s, uint64_t index); 73 | 74 | void store_entries(struct store *s, uint64_t index, uint32_t limit, 75 | unsigned char **entries, uint32_t *size, uint32_t *count); 76 | 77 | void store_remove_after(struct store *s, uint64_t index); 78 | 79 | #endif 80 | -------------------------------------------------------------------------------- /src/meta.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | #ifndef RESQL_META_H 33 | #define RESQL_META_H 34 | 35 | #include "sc/sc_array.h" 36 | #include "sc/sc_buf.h" 37 | 38 | #include 39 | 40 | struct sc_uri; 41 | 42 | sc_array_def(struct meta_node, node); 43 | 44 | enum meta_role 45 | { 46 | META_LEADER, 47 | META_FOLLOWER, 48 | }; 49 | 50 | extern const char *meta_role_str[]; 51 | 52 | struct meta_node { 53 | char *name; 54 | enum meta_role role; 55 | struct sc_array_ptr uris; 56 | }; 57 | 58 | struct meta { 59 | char *name; 60 | char *uris; 61 | uint64_t term; 62 | uint64_t index; 63 | uint32_t voter; 64 | struct sc_array_node nodes; 65 | struct meta *prev; 66 | }; 67 | 68 | void meta_init(struct meta *m, const char *cluster_name); 69 | void meta_term(struct meta *m); 70 | 71 | void meta_copy(struct meta *m, struct meta *src); 72 | void meta_encode(struct meta *m, struct sc_buf *buf); 73 | void meta_decode(struct meta *m, struct sc_buf *buf); 74 | bool meta_add(struct meta *meta, struct sc_uri *uri); 75 | bool meta_remove(struct meta *meta, const char *name); 76 | bool meta_exists(struct meta *m, const char *name); 77 | void meta_remove_prev(struct meta *m); 78 | void meta_rollback(struct meta *m, uint64_t index); 79 | void meta_replace(struct meta *m, void *data, uint32_t len); 80 | 81 | void meta_set_leader(struct meta *m, const char *name); 82 | bool meta_parse_uris(struct meta *m, const char *addrs); 83 | 84 | void meta_print(struct meta *m, struct sc_buf *buf); 85 | 86 | #endif 87 | -------------------------------------------------------------------------------- /src/conn.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | #ifndef RESQL_CONN_H 33 | #define RESQL_CONN_H 34 | 35 | #include "msg.h" 36 | 37 | #include "sc/sc_buf.h" 38 | #include "sc/sc_list.h" 39 | #include "sc/sc_sock.h" 40 | 41 | #include 42 | 43 | struct sc_uri; 44 | struct server; 45 | 46 | enum conn_state 47 | { 48 | CONN_DISCONNECTED, 49 | CONN_TCP_ATTEMPT, 50 | CONN_CONNECTED, 51 | }; 52 | 53 | struct conn { 54 | struct sc_list list; 55 | enum conn_state state; 56 | struct sc_sock sock; 57 | struct sc_buf out; 58 | struct sc_buf in; 59 | struct msg msg; 60 | struct server *server; 61 | uint64_t timer_id; 62 | 63 | char local[64]; 64 | char remote[64]; 65 | }; 66 | 67 | struct conn *conn_create(struct server *server, struct sc_sock *s); 68 | void conn_destroy(struct conn *c); 69 | 70 | void conn_init(struct conn *c, struct server *s); 71 | void conn_term(struct conn *c); 72 | 73 | void conn_clear_buf(struct conn *c); 74 | struct sc_buf *conn_out(struct conn *c); 75 | int conn_set(struct conn *c, struct conn *src); 76 | void conn_set_type(struct conn *c, int type); 77 | 78 | void conn_schedule(struct conn *c, uint32_t type, uint32_t timeout); 79 | 80 | int conn_try_connect(struct conn *c, struct sc_uri *uri); 81 | int conn_on_out_connected(struct conn *c); 82 | 83 | int conn_on_writable(struct conn *c); 84 | int conn_on_readable(struct conn *c); 85 | int conn_register(struct conn *c, bool read, bool write); 86 | int conn_unregister(struct conn *c, bool read, bool write); 87 | 88 | int conn_flush(struct conn *c); 89 | 90 | #endif 91 | -------------------------------------------------------------------------------- /jresql/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Created by https://www.gitignore.io/api/java,maven,intellij+all 3 | # Edit at https://www.gitignore.io/?templates=java,maven,intellij+all 4 | 5 | ### Intellij+all ### 6 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm 7 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 8 | 9 | # User-specific stuff 10 | .idea/**/workspace.xml 11 | .idea/**/tasks.xml 12 | .idea/**/usage.statistics.xml 13 | .idea/**/dictionaries 14 | .idea/**/shelf 15 | 16 | # Generated files 17 | .idea/**/contentModel.xml 18 | 19 | # Sensitive or high-churn files 20 | .idea/**/dataSources/ 21 | .idea/**/dataSources.ids 22 | .idea/**/dataSources.local.xml 23 | .idea/**/sqlDataSources.xml 24 | .idea/**/dynamic.xml 25 | .idea/**/uiDesigner.xml 26 | .idea/**/dbnavigator.xml 27 | 28 | # Gradle 29 | .idea/**/gradle.xml 30 | .idea/**/libraries 31 | 32 | # Gradle and Maven with auto-import 33 | # When using Gradle or Maven with auto-import, you should exclude module files, 34 | # since they will be recreated, and may cause churn. Uncomment if using 35 | # auto-import. 36 | # .idea/modules.xml 37 | # .idea/*.iml 38 | # .idea/modules 39 | # *.iml 40 | # *.ipr 41 | 42 | # CMake 43 | cmake-build-*/ 44 | 45 | # Mongo Explorer plugin 46 | .idea/**/mongoSettings.xml 47 | 48 | # File-based project format 49 | *.iws 50 | 51 | # IntelliJ 52 | out/ 53 | 54 | # mpeltonen/sbt-idea plugin 55 | .idea_modules/ 56 | 57 | # JIRA plugin 58 | atlassian-ide-plugin.xml 59 | 60 | # Cursive Clojure plugin 61 | .idea/replstate.xml 62 | 63 | # Crashlytics plugin (for Android Studio and IntelliJ) 64 | com_crashlytics_export_strings.xml 65 | crashlytics.properties 66 | crashlytics-build.properties 67 | fabric.properties 68 | 69 | # Editor-based Rest Client 70 | .idea/httpRequests 71 | 72 | # Android studio 3.1+ serialized cache file 73 | .idea/caches/build_file_checksums.ser 74 | 75 | ### Intellij+all Patch ### 76 | # Ignores the whole .idea folder and all .iml files 77 | # See https://github.com/joeblau/gitignore.io/issues/186 and https://github.com/joeblau/gitignore.io/issues/360 78 | 79 | .idea/ 80 | 81 | # Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-249601023 82 | 83 | *.iml 84 | modules.xml 85 | .idea/misc.xml 86 | *.ipr 87 | 88 | # Sonarlint plugin 89 | .idea/sonarlint 90 | 91 | ### Java ### 92 | # Compiled class file 93 | *.class 94 | 95 | # Log file 96 | *.log 97 | 98 | # BlueJ files 99 | *.ctxt 100 | 101 | # Mobile Tools for Java (J2ME) 102 | .mtj.tmp/ 103 | 104 | # Package Files # 105 | *.jar 106 | *.war 107 | *.nar 108 | *.ear 109 | *.zip 110 | *.tar.gz 111 | *.rar 112 | 113 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 114 | hs_err_pid* 115 | 116 | ### Maven ### 117 | target/ 118 | pom.xml.tag 119 | pom.xml.releaseBackup 120 | pom.xml.versionsBackup 121 | pom.xml.next 122 | release.properties 123 | dependency-reduced-pom.xml 124 | buildNumber.properties 125 | .mvn/timing.properties 126 | .mvn/wrapper/maven-wrapper.jar 127 | .flattened-pom.xml 128 | 129 | # End of https://www.gitignore.io/api/java,maven,intellij+all -------------------------------------------------------------------------------- /src/snapshot.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | #ifndef RESQL_SNAPSHOT_H 33 | #define RESQL_SNAPSHOT_H 34 | 35 | #include "sc/sc_cond.h" 36 | #include "sc/sc_mmap.h" 37 | #include "sc/sc_sock.h" 38 | #include "sc/sc_thread.h" 39 | 40 | #include 41 | #include 42 | 43 | #define SNAPSHOT_MAX_PAGE_COUNT 4 44 | 45 | struct server; 46 | struct page; 47 | 48 | struct snapshot { 49 | bool init; 50 | bool open; 51 | struct server *server; 52 | struct sc_mmap map; 53 | char *path; 54 | char *tmp_path; 55 | char *copy_path; 56 | 57 | // Current index and size 58 | uint64_t index; 59 | uint64_t term; 60 | 61 | // Latest snapshot 62 | uint64_t time; 63 | size_t size; 64 | uint64_t latest_index; 65 | uint64_t latest_term; 66 | 67 | // Recv 68 | uint64_t recv_index; 69 | uint64_t recv_term; 70 | char *recv_path; 71 | struct file *tmp; 72 | 73 | struct sc_thread thread; 74 | struct sc_sock_pipe efd; 75 | struct sc_cond cond; 76 | _Atomic bool running; 77 | }; 78 | 79 | int snapshot_init(struct snapshot *ss, struct server *server); 80 | int snapshot_term(struct snapshot *ss); 81 | 82 | int snapshot_open(struct snapshot *ss, const char *path, uint64_t term, 83 | uint64_t index); 84 | bool snapshot_running(struct snapshot *ss); 85 | int snapshot_wait(struct snapshot *ss); 86 | 87 | int snapshot_replace(struct snapshot *ss); 88 | 89 | int snapshot_take(struct snapshot *ss, struct page *page); 90 | int snapshot_recv(struct snapshot *ss, uint64_t term, uint64_t index, bool done, 91 | uint64_t offset, void *data, uint64_t len); 92 | void snapshot_clear(struct snapshot *ss); 93 | 94 | #endif 95 | -------------------------------------------------------------------------------- /lib/sc/sc_ini.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | #ifndef SC_INI_H 33 | #define SC_INI_H 34 | 35 | #include 36 | #include 37 | #include 38 | 39 | #define SC_INI_VERSION "2.0.0" 40 | 41 | // Set max line length. If a line is longer, it will be truncated silently. 42 | #define SC_INI_MAX_LINE_LEN 1024 43 | 44 | /** 45 | * @param arg user arg 46 | * @param line current line number 47 | * @param section section 48 | * @param key key 49 | * @param value value 50 | * @return Return '0' on success, any other value will make parser 51 | * stop and return error 52 | */ 53 | typedef int (*sc_ini_on_item)(void *arg, int line, const char *section, 54 | const char *key, const char *value); 55 | 56 | /** 57 | * @param arg user data to be passed to 'on_item' callback 58 | * @param on_item callback 59 | * @param filename filename 60 | * @return - '0' on success, 61 | * - '-1' on file IO error. 62 | * - positive line number on parsing error 63 | * - 'on_item' return value if it returns other than '0' 64 | */ 65 | int sc_ini_parse_file(void *arg, sc_ini_on_item on_item, const char *filename); 66 | 67 | /** 68 | * @param arg user data to be passed to 'on_item' callback. 69 | * @param on_item callback 70 | * @param str string to parse 71 | * @return - '0' on success, 72 | * - positive line number on parsing error 73 | * - "on_item's return value" if it returns other than '0' 74 | */ 75 | int sc_ini_parse_string(void *arg, sc_ini_on_item on_item, const char *str); 76 | 77 | #endif 78 | -------------------------------------------------------------------------------- /src/page.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | #ifndef RESQL_PAGE_H 33 | #define RESQL_PAGE_H 34 | 35 | #include "sc/sc_array.h" 36 | #include "sc/sc_buf.h" 37 | #include "sc/sc_mmap.h" 38 | 39 | #include 40 | 41 | struct page { 42 | bool init; 43 | char *path; 44 | struct sc_mmap map; 45 | 46 | uint64_t prev_index; 47 | uint32_t flush_pos; 48 | uint64_t flush_index; 49 | 50 | struct sc_buf buf; 51 | struct sc_array_ustr entries; 52 | }; 53 | 54 | int page_init(struct page *p, const char *path, int64_t len, 55 | uint64_t prev_index); 56 | void page_term(struct page *p); 57 | int page_reserve(struct page *p, uint32_t size); 58 | bool page_isempty(struct page *p); 59 | 60 | void page_clear(struct page *p, uint64_t prev_index); 61 | void page_fsync(struct page *p, uint64_t index); 62 | 63 | uint32_t page_entry_count(struct page *p); 64 | uint32_t page_quota(struct page *p); 65 | uint32_t page_cap(struct page *p); 66 | bool page_last_part(struct page *p); 67 | 68 | uint64_t page_prev_index(struct page *p); 69 | uint64_t page_last_index(struct page *p); 70 | uint64_t page_last_term(struct page *p); 71 | unsigned char *page_last_entry(struct page *p); 72 | 73 | void page_create_entry(struct page *p, uint64_t term, uint64_t seq, 74 | uint64_t cid, uint32_t flags, void *data, uint32_t len); 75 | 76 | void page_put_entry(struct page *p, unsigned char *entry); 77 | 78 | unsigned char *page_entry_at(struct page *p, uint64_t index); 79 | void page_remove_after(struct page *p, uint64_t index); 80 | 81 | void page_get_entries(struct page *p, uint64_t index, uint32_t limit, 82 | unsigned char **entries, uint32_t *size, uint32_t *count); 83 | 84 | #endif 85 | -------------------------------------------------------------------------------- /jresql/src/test/java/resql/TimestampTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package resql; 33 | 34 | import org.junit.jupiter.api.AfterEach; 35 | import org.junit.jupiter.api.Assertions; 36 | import org.junit.jupiter.api.BeforeEach; 37 | import org.junit.jupiter.api.Test; 38 | 39 | import java.nio.charset.StandardCharsets; 40 | import java.util.ArrayList; 41 | import java.util.List; 42 | 43 | public class TimestampTest { 44 | 45 | Resql client; 46 | 47 | @BeforeEach 48 | public void setup() { 49 | client = ResqlClient.create(new Config()); 50 | client.put("DROP TABLE IF EXISTS ts_ms_table;"); 51 | client.put("CREATE TABLE ts_ms_table (id INTEGER PRIMARY KEY AUTOINCREMENT, ts_ms TIMESTAMP DEFAULT (STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW')));;"); 52 | client.execute(false); 53 | } 54 | 55 | @AfterEach 56 | public void tearDown() { 57 | if (client != null) { 58 | client.clear(); 59 | client.put("DROP TABLE IF EXISTS ts_ms_table;"); 60 | client.execute(false); 61 | client.shutdown(); 62 | } 63 | } 64 | 65 | @Test 66 | public void testTimestampIncrementFor10ms() throws InterruptedException { 67 | ResultSet rs; 68 | for (int i = 0; i < 100; i++) { 69 | client.put("INSERT INTO ts_ms_table DEFAULT VALUES;"); 70 | rs = client.execute(false); 71 | assert (rs.linesChanged() == 1); 72 | assert (rs.lastRowId() == i + 1); 73 | Thread.sleep(10); 74 | } 75 | 76 | client.put("SELECT DISTINCT(ts_ms) FROM ts_ms_table;"); 77 | rs = client.execute(true); 78 | assert (rs.rowCount() > 1); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /cresql/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.9) 2 | project(resqlc C) 3 | 4 | set(CMAKE_C_STANDARD 11) 5 | set(CMAKE_C_STANDARD_REQUIRED ON) 6 | set(CMAKE_C_EXTENSIONS OFF) 7 | 8 | if (NOT CMAKE_C_COMPILER_ID MATCHES "MSVC") 9 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -Wall -Wextra -pedantic -Werror") 10 | endif () 11 | 12 | 13 | # --------------------------------------------------------------------------- # 14 | # --------------------- Test Configuration Start ---------------------------- # 15 | # --------------------------------------------------------------------------- # 16 | 17 | include(CTest) 18 | include(CheckCCompilerFlag) 19 | 20 | enable_testing() 21 | 22 | add_executable(${PROJECT_NAME}_test resql.h resql.c resqlc_test.c) 23 | 24 | message(STATUS "Compiler is ${CMAKE_C_COMPILER_ID}") 25 | 26 | if ("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang" OR 27 | "${CMAKE_C_COMPILER_ID}" STREQUAL "AppleClang" OR 28 | "${CMAKE_C_COMPILER_ID}" STREQUAL "GNU") 29 | 30 | target_compile_options(${PROJECT_NAME}_test PRIVATE -fno-omit-frame-pointer) 31 | 32 | if (SANITIZER) 33 | target_compile_options(${PROJECT_NAME}_test PRIVATE -fsanitize=${SANITIZER}) 34 | # Only supported with CMAKE 3.13 35 | # target_link_options(${PROJECT_NAME}_test PRIVATE -fsanitize=${SANITIZER}) 36 | target_link_libraries(${PROJECT_NAME}_test PRIVATE -fsanitize=${SANITIZER}) 37 | endif () 38 | endif () 39 | 40 | add_test(NAME ${PROJECT_NAME}_test COMMAND ${PROJECT_NAME}_test) 41 | 42 | SET(MEMORYCHECK_COMMAND_OPTIONS 43 | "-q --log-fd=2 --trace-children=yes --track-origins=yes \ 44 | --leak-check=full --show-leak-kinds=all \ 45 | --error-exitcode=255") 46 | 47 | add_custom_target(valgrind ${CMAKE_COMMAND} 48 | -E env CTEST_OUTPUT_ON_FAILURE=1 49 | ${CMAKE_CTEST_COMMAND} -C $ 50 | --overwrite MemoryCheckCommandOptions=${MEMORYCHECK_COMMAND_OPTIONS} 51 | --verbose -T memcheck WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) 52 | 53 | add_custom_target(check ${CMAKE_COMMAND} 54 | -E env CTEST_OUTPUT_ON_FAILURE=1 55 | ${CMAKE_CTEST_COMMAND} -C $ --verbose 56 | WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) 57 | 58 | # ----------------------- - Code Coverage Start ----------------------------- # 59 | 60 | if (${CMAKE_BUILD_TYPE} MATCHES "Coverage") 61 | if ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU") 62 | target_compile_options(${PROJECT_NAME}_test PRIVATE --coverage) 63 | target_link_libraries(${PROJECT_NAME}_test gcov) 64 | else () 65 | message(FATAL_ERROR "Only GCC is supported for coverage") 66 | endif () 67 | endif () 68 | 69 | add_custom_target(coverage) 70 | add_custom_command( 71 | TARGET coverage 72 | COMMAND lcov --capture --directory . 73 | --output-file coverage.info --rc lcov_branch_coverage=1 --rc lcov_excl_br_line='assert' 74 | COMMAND lcov --remove coverage.info '/usr/*' '*example*' '*test*' 75 | --output-file coverage.info --rc lcov_branch_coverage=1 --rc lcov_excl_br_line='assert' 76 | COMMAND lcov --list coverage.info --rc lcov_branch_coverage=1 --rc lcov_excl_br_line='assert' 77 | ) 78 | 79 | add_dependencies(coverage check) 80 | 81 | # -------------------------- Code Coverage End ------------------------------ # 82 | # ----------------------- Test Configuration End ---------------------------- # 83 | 84 | -------------------------------------------------------------------------------- /src/node.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | #ifndef RESQL_NODE_H 33 | #define RESQL_NODE_H 34 | 35 | #include "conn.h" 36 | #include "meta.h" 37 | 38 | #include "sc/sc_list.h" 39 | #include "sc/sc_map.h" 40 | #include "sc/sc_queue.h" 41 | 42 | struct node { 43 | struct server *server; 44 | struct sc_sock_poll *poll; 45 | struct sc_timer *timer; 46 | struct conn conn; 47 | 48 | struct sc_list list; 49 | struct sc_queue_ptr uris; // URLs 50 | uint64_t timeout; // Reconnect timeout 51 | uint64_t interval; // Reconnect trial interval 52 | char *name; // Node name 53 | 54 | uint64_t next; // Next entry index 55 | uint64_t match; // Entry match index 56 | uint64_t round; // Round match index 57 | 58 | uint64_t ss_pos; // Snapshot offset 59 | uint64_t ss_index; // Snapshot index 60 | uint64_t msg_inflight; // Number of inflight messages 61 | 62 | int id; // Node id 63 | const char *status; // Status, e.g online, offline, disk_full 64 | 65 | uint64_t in_timestamp; // Latest in timestamp 66 | uint64_t out_timestamp; // Latest out timestamp 67 | struct sc_buf info; // Metrics 68 | }; 69 | 70 | struct node *node_create(const char *name, struct server *server, bool connect); 71 | void node_destroy(struct node *n); 72 | 73 | void node_disconnect(struct node *n); 74 | void node_update_indexes(struct node *n, uint64_t round, uint64_t match); 75 | void node_clear_indexes(struct node *n, uint64_t match); 76 | void node_add_uris(struct node *n, struct sc_array_ptr *uris); 77 | int node_try_connect(struct node *n, unsigned int randtimer); 78 | int node_set_conn(struct node *n, struct conn *conn); 79 | bool node_connected(struct node *n); 80 | 81 | #endif 82 | -------------------------------------------------------------------------------- /src/client.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | #include "client.h" 33 | 34 | #include "server.h" 35 | 36 | struct client *client_create(struct conn *conn, const char *name) 37 | { 38 | int rc; 39 | struct client *c; 40 | 41 | c = rs_calloc(1, sizeof(*c)); 42 | 43 | rc = conn_set(&c->conn, conn); 44 | if (rc != RS_OK) { 45 | goto err; 46 | } 47 | 48 | conn_set_type(&c->conn, SERVER_FD_CLIENT_RECV); 49 | 50 | c->name = sc_str_create(name); 51 | 52 | /** 53 | * This function is called when we receive CONNECT_REQ. So, client is 54 | * waiting CONNECT_RESP which will be sent later when new connection is 55 | * applied to the state. 56 | */ 57 | c->msg_wait = true; 58 | 59 | sc_list_init(&c->read); 60 | conn_clear_buf(&c->conn); 61 | 62 | return c; 63 | err: 64 | rs_free(c); 65 | return NULL; 66 | } 67 | 68 | void client_destroy(struct client *c) 69 | { 70 | sc_list_del(NULL, &c->read); 71 | sc_str_destroy(&c->name); 72 | conn_term(&c->conn); 73 | rs_free(c); 74 | } 75 | 76 | int client_processed(struct client *c) 77 | { 78 | c->msg_wait = false; 79 | sc_list_del(NULL, &c->read); 80 | conn_clear_buf(&c->conn); 81 | 82 | return conn_register(&c->conn, true, false); 83 | } 84 | 85 | void client_print(struct client *c, char *buf, size_t len) 86 | { 87 | int rc; 88 | 89 | rc = rs_snprintf(buf, len, "Client : %s, ", c->name); 90 | sc_sock_print(&c->conn.sock, buf + rc, len - rc); 91 | } 92 | 93 | bool client_pending(struct client *c) 94 | { 95 | return c->msg_wait || sc_buf_size(&c->conn.out); 96 | } 97 | 98 | void client_set_terminated(struct client *c) 99 | { 100 | c->terminated = true; 101 | conn_clear_buf(&c->conn); 102 | sc_list_del(NULL, &c->read); 103 | } 104 | -------------------------------------------------------------------------------- /test/test_util.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | #ifndef RESQL_TEST_UTIL_H 32 | #define RESQL_TEST_UTIL_H 33 | 34 | #include "conf.h" 35 | #include "resql.h" 36 | #include "rs.h" 37 | 38 | #define test_tmp_page0 "/tmp/resql_test/page.0.resql" 39 | #define test_tmp_dir "/tmp/resql_test" 40 | #define test_tmp_page0 "/tmp/resql_test/page.0.resql" 41 | #define test_tmp_dir2 "/tmp/resql_test2" 42 | #define test_execute(A) (test_util_run(A, #A)) 43 | 44 | #define client_assert(c, b) \ 45 | do { \ 46 | if (!(b)) { \ 47 | rs_abort("%s \n", resql_errstr(c)); \ 48 | } \ 49 | } while (0) 50 | 51 | void init_all(); 52 | void test_util_run(void (*test_fn)(void), const char *fn_name); 53 | 54 | struct server *test_server_create_conf(struct conf *conf, int id); 55 | struct server *test_server_create_auto(bool in_memory, int cluster_size); 56 | struct server *test_server_create(bool in_memory, int id, int cluster_size); 57 | struct server *test_server_start_auto(bool in_memory, int cluster_size); 58 | struct server *test_server_start(bool in_memory, int id, int cluster_size); 59 | struct server *test_server_add_auto(bool in_memory); 60 | 61 | void test_wait_until_size(int size); 62 | void test_server_add(bool in_memory, int id, int cluster_size); 63 | void test_server_remove(int id); 64 | 65 | void test_server_destroy(int id); 66 | void test_server_destroy_all(); 67 | void test_server_destroy_leader(); 68 | 69 | resql *test_client_create(); 70 | resql *test_client_create_timeout(uint32_t timeout); 71 | void test_client_destroy(resql *c); 72 | void test_client_destroy_all(); 73 | 74 | #endif 75 | -------------------------------------------------------------------------------- /test/single_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | #include "resql.h" 33 | #include "test_util.h" 34 | 35 | void test_one() 36 | { 37 | test_server_create(true, 0, 1); 38 | test_client_create(); 39 | } 40 | 41 | void test_sizes() 42 | { 43 | for (int i = 1; i <= 9; i++) { 44 | for (int j = 0; j < i; j++) { 45 | test_server_create(true, j, i); 46 | } 47 | 48 | test_client_create(); 49 | test_client_destroy_all(); 50 | test_server_destroy_all(); 51 | } 52 | } 53 | 54 | void test_client() 55 | { 56 | int rc; 57 | resql *c; 58 | resql_result *rs; 59 | 60 | test_server_create(true, 0, 1); 61 | c = test_client_create(); 62 | 63 | resql_put_sql(c, "SELECT * FROM resql_clients;"); 64 | rc = resql_exec(c, true, &rs); 65 | client_assert(c, rc == RESQL_OK); 66 | } 67 | 68 | void test_one_disk() 69 | { 70 | test_server_create(false, 0, 1); 71 | test_client_create(); 72 | } 73 | 74 | void test_sizes_disk() 75 | { 76 | for (int i = 1; i <= 9; i++) { 77 | for (int j = 0; j < i; j++) { 78 | test_server_create(false, j, i); 79 | } 80 | 81 | test_client_create(); 82 | test_client_destroy_all(); 83 | test_server_destroy_all(); 84 | } 85 | } 86 | 87 | void test_client_disk() 88 | { 89 | int rc; 90 | resql *c; 91 | resql_result *rs; 92 | 93 | test_server_create(false, 0, 1); 94 | c = test_client_create(); 95 | 96 | resql_put_sql(c, "SELECT * FROM resql_clients;"); 97 | rc = resql_exec(c, true, &rs); 98 | client_assert(c, rc == RESQL_OK); 99 | } 100 | 101 | int main() 102 | { 103 | test_execute(test_one); 104 | test_execute(test_client); 105 | test_execute(test_sizes); 106 | 107 | test_execute(test_one_disk); 108 | test_execute(test_client_disk); 109 | test_execute(test_sizes_disk); 110 | return 0; 111 | } 112 | -------------------------------------------------------------------------------- /test/remove_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | #include "resql.h" 33 | #include "test_util.h" 34 | 35 | #include 36 | #include 37 | 38 | void test_one() 39 | { 40 | int rc; 41 | int64_t key, value; 42 | char blob[64]; 43 | resql *c; 44 | resql_result *rs; 45 | struct resql_column *row; 46 | 47 | test_server_create(true, 0, 2); 48 | test_server_create(false, 1, 2); 49 | c = test_client_create(); 50 | 51 | resql_put_sql( 52 | c, 53 | "CREATE TABLE test (key INTEGER PRIMARY KEY, val INTEGER, blob BLOB);"); 54 | rc = resql_exec(c, false, &rs); 55 | client_assert(c, rc == RESQL_OK); 56 | 57 | resql_put_sql(c, "SELECT random(), random(), randomblob(64);"); 58 | rc = resql_exec(c, true, &rs); 59 | client_assert(c, rc == RESQL_OK); 60 | 61 | resql_put_sql( 62 | c, 63 | "INSERT INTO test VALUES(random(), random(), randomblob(64));"); 64 | rc = resql_exec(c, false, &rs); 65 | client_assert(c, rc == RESQL_OK); 66 | 67 | resql_put_sql(c, "SELECT * FROM test;"); 68 | rc = resql_exec(c, false, &rs); 69 | client_assert(c, rc == RESQL_OK); 70 | 71 | row = resql_row(rs); 72 | key = row[0].intval; 73 | value = row[1].intval; 74 | memcpy(blob, row[2].blob, row[2].len); 75 | 76 | test_server_add(true, 2, 3); 77 | test_server_remove(0); 78 | test_server_remove(2); 79 | 80 | resql_put_sql(c, "SELECT * FROM test;"); 81 | rc = resql_exec(c, false, &rs); 82 | client_assert(c, rc == RESQL_OK); 83 | 84 | rs_assert(key == row[0].intval); 85 | rs_assert(value == row[1].intval); 86 | rs_assert(memcmp(blob, row[2].blob, row[2].len) == 0); 87 | 88 | resql_put_sql(c, "SELECT * FROM resql_nodes;"); 89 | rc = resql_exec(c, false, &rs); 90 | client_assert(c, rc == RESQL_OK); 91 | 92 | rs_assert(resql_row_count(rs) == 1); 93 | row = resql_row(rs); 94 | rs_assert(strcmp(row[0].text, "node1") == 0); 95 | } 96 | 97 | int main() 98 | { 99 | test_execute(test_one); 100 | 101 | return 0; 102 | } 103 | -------------------------------------------------------------------------------- /src/aux.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | #ifndef RESQL_AUX_H 33 | #define RESQL_AUX_H 34 | 35 | #include "sqlite/sqlite3.h" 36 | 37 | #include 38 | 39 | struct sc_buf; 40 | struct info; 41 | struct session; 42 | 43 | struct aux { 44 | sqlite3 *db; 45 | 46 | sqlite3_stmt *begin; 47 | sqlite3_stmt *commit; 48 | sqlite3_stmt *rollback; 49 | sqlite3_stmt *add_node; 50 | sqlite3_stmt *rm_node; 51 | sqlite3_stmt *add_session; 52 | sqlite3_stmt *rm_session; 53 | sqlite3_stmt *add_stmt; 54 | sqlite3_stmt *rm_stmt; 55 | sqlite3_stmt *rm_all_stmts; 56 | sqlite3_stmt *add_log; 57 | sqlite3_stmt *rotate_log; 58 | }; 59 | 60 | int aux_init(struct aux *aux, const char *path, int mode); 61 | int aux_term(struct aux *aux); 62 | 63 | int aux_load_to_memory(struct aux *aux, const char *from); 64 | 65 | // Configure db and prepare statements 66 | int aux_prepare(struct aux *aux); 67 | 68 | // resql_node table 69 | int aux_clear_nodes(struct aux *aux); 70 | int aux_write_node(struct aux *aux, struct info *n); 71 | 72 | // resql_clients table, each client has a session associated 73 | int aux_write_session(struct aux *aux, struct session *s); 74 | int aux_read_session(struct aux *aux, struct session *s, sqlite3_stmt *sess_tb, 75 | sqlite3_stmt *stmt_tb); 76 | int aux_del_session(struct aux *aux, struct session *s); 77 | int aux_clear_sessions(struct aux *aux); 78 | 79 | // 80 | int aux_write_kv(struct aux *aux, const char *key, struct sc_buf *buf); 81 | int aux_read_kv(struct aux *aux, const char *key, struct sc_buf *buf); 82 | 83 | // resql_statements table 84 | int aux_add_stmt(struct aux *aux, const char *client, uint64_t cid, uint64_t id, 85 | const char *sql); 86 | int aux_rm_stmt(struct aux *aux, uint64_t id); 87 | 88 | // resql_log table, add log entry 89 | int aux_add_log(struct aux *aux, uint64_t id, const char *level, 90 | const char *log); 91 | 92 | // Translate sqlite error codes to RS_ family error codes. 93 | int aux_rc(int rc); 94 | 95 | // clear statement, prepare for reuse 96 | void aux_clear(sqlite3_stmt *stmt); 97 | 98 | #endif 99 | -------------------------------------------------------------------------------- /lib/sc/sc_mutex.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | #ifndef _XOPEN_SOURCE 32 | #define _XOPEN_SOURCE 700 33 | #endif 34 | 35 | #include "sc_mutex.h" 36 | 37 | #include 38 | 39 | #if defined(_WIN32) || defined(_WIN64) 40 | 41 | int sc_mutex_init(struct sc_mutex *mtx) 42 | { 43 | InitializeCriticalSection(&mtx->mtx); 44 | return 0; 45 | } 46 | 47 | int sc_mutex_term(struct sc_mutex *mtx) 48 | { 49 | DeleteCriticalSection(&mtx->mtx); 50 | return 0; 51 | } 52 | 53 | void sc_mutex_lock(struct sc_mutex *mtx) 54 | { 55 | EnterCriticalSection(&mtx->mtx); 56 | } 57 | 58 | void sc_mutex_unlock(struct sc_mutex *mtx) 59 | { 60 | LeaveCriticalSection(&mtx->mtx); 61 | } 62 | 63 | #else 64 | 65 | int sc_mutex_init(struct sc_mutex *mtx) 66 | { 67 | int rc, rv; 68 | pthread_mutexattr_t attr; 69 | pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER; 70 | 71 | mtx->mtx = mut; 72 | 73 | // May fail on OOM 74 | rc = pthread_mutexattr_init(&attr); 75 | if (rc != 0) { 76 | return -1; 77 | } 78 | 79 | // This won't fail as long as we pass correct params. 80 | rc = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL); 81 | assert(rc == 0); 82 | 83 | // May fail on OOM 84 | rc = pthread_mutex_init(&mtx->mtx, &attr); 85 | 86 | // This won't fail as long as we pass correct param. 87 | rv = pthread_mutexattr_destroy(&attr); 88 | assert(rv == 0); 89 | (void) rv; 90 | 91 | return rc != 0 ? -1 : 0; 92 | } 93 | 94 | int sc_mutex_term(struct sc_mutex *mtx) 95 | { 96 | int rc; 97 | 98 | rc = pthread_mutex_destroy(&mtx->mtx); 99 | return rc != 0 ? -1 : 0; 100 | } 101 | 102 | void sc_mutex_lock(struct sc_mutex *mtx) 103 | { 104 | int rc; 105 | 106 | // This won't fail as long as we pass correct param. 107 | rc = pthread_mutex_lock(&mtx->mtx); 108 | assert(rc == 0); 109 | (void) rc; 110 | } 111 | 112 | void sc_mutex_unlock(struct sc_mutex *mtx) 113 | { 114 | int rc; 115 | 116 | // This won't fail as long as we pass correct param. 117 | rc = pthread_mutex_unlock(&mtx->mtx); 118 | assert(rc == 0); 119 | (void) rc; 120 | } 121 | 122 | #endif 123 | -------------------------------------------------------------------------------- /jresql/src/main/java/resql/Config.java: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package resql; 33 | 34 | import java.util.ArrayList; 35 | import java.util.List; 36 | 37 | public class Config { 38 | String clientName = null; 39 | String clusterName = "cluster"; 40 | String outgoingAddr = null; 41 | int outgoingPort = 0; 42 | int timeoutMillis = Integer.MAX_VALUE; 43 | List urls = new ArrayList<>(); 44 | 45 | /** 46 | * Create new Config instance 47 | * 48 | * Default timeout is Integer.MAX_VALUE. 49 | * Default url is 127.0.0.1:7600 50 | * 51 | */ 52 | public Config() { 53 | urls.add("tcp://127.0.0.1:7600"); 54 | } 55 | 56 | /** 57 | * Create new Config instance 58 | * 59 | * @param clientName client name 60 | * @param clusterName cluster name must match with cluster name of servers 61 | * @param timeoutMillis timeout milliseconds 62 | * @param urls urls 63 | */ 64 | public Config(String clientName, String clusterName, int timeoutMillis, 65 | List urls) { 66 | this.clientName = clientName; 67 | this.clusterName = clusterName; 68 | this.timeoutMillis = timeoutMillis; 69 | this.urls = urls; 70 | } 71 | 72 | public Config setClientName(String clientName) { 73 | this.clientName = clientName; 74 | return this; 75 | } 76 | 77 | public Config setClusterName(String clusterName) { 78 | this.clusterName = clusterName; 79 | return this; 80 | } 81 | 82 | public Config setTimeoutMillis(int timeoutMillis) { 83 | this.timeoutMillis = timeoutMillis; 84 | return this; 85 | } 86 | 87 | public Config setUrls(List urls) { 88 | this.urls = urls; 89 | return this; 90 | } 91 | 92 | public Config setOutgoingAddr(String outgoingAddr) { 93 | this.outgoingAddr = outgoingAddr; 94 | return this; 95 | } 96 | 97 | public Config setOutgoingPort(int outgoingPort) { 98 | this.outgoingPort = outgoingPort; 99 | return this; 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /test/add_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | #include "resql.h" 33 | #include "test_util.h" 34 | 35 | #include 36 | #include 37 | 38 | void test_one() 39 | { 40 | int rc; 41 | int64_t key, value; 42 | char blob[64]; 43 | resql *c; 44 | resql_result *rs; 45 | struct resql_column *row; 46 | 47 | test_server_create(true, 0, 1); 48 | test_server_add_auto(false); 49 | c = test_client_create(); 50 | 51 | resql_put_sql( 52 | c, 53 | "CREATE TABLE test (key INTEGER PRIMARY KEY, val INTEGER, blob BLOB);"); 54 | rc = resql_exec(c, false, &rs); 55 | client_assert(c, rc == RESQL_OK); 56 | 57 | resql_put_sql(c, "SELECT random(), random(), randomblob(64);"); 58 | rc = resql_exec(c, true, &rs); 59 | client_assert(c, rc == RESQL_OK); 60 | 61 | resql_put_sql(c, "SELECT random(), random(), randomblob(64);"); 62 | rc = resql_exec(c, false, &rs); 63 | client_assert(c, rc == RESQL_OK); 64 | 65 | resql_put_sql(c, "SELECT random(), random(), randomblob(64);"); 66 | rc = resql_exec(c, true, &rs); 67 | client_assert(c, rc == RESQL_OK); 68 | 69 | resql_put_sql( 70 | c, 71 | "INSERT INTO test VALUES(random(), random(), randomblob(64));"); 72 | rc = resql_exec(c, false, &rs); 73 | client_assert(c, rc == RESQL_OK); 74 | 75 | resql_put_sql(c, "SELECT * FROM test;"); 76 | rc = resql_exec(c, false, &rs); 77 | client_assert(c, rc == RESQL_OK); 78 | 79 | row = resql_row(rs); 80 | key = row[0].intval; 81 | value = row[1].intval; 82 | memcpy(blob, row[2].blob, row[2].len); 83 | 84 | test_server_add_auto(false); 85 | test_server_add_auto(true); 86 | test_server_add_auto(false); 87 | test_server_destroy_leader(); 88 | 89 | resql_put_sql(c, "SELECT random(), random(), randomblob(64);"); 90 | rc = resql_exec(c, true, &rs); 91 | client_assert(c, rc == RESQL_OK); 92 | 93 | resql_put_sql(c, "SELECT * FROM test;"); 94 | rc = resql_exec(c, false, &rs); 95 | client_assert(c, rc == RESQL_OK); 96 | 97 | rs_assert(key == row[0].intval); 98 | rs_assert(value == row[1].intval); 99 | rs_assert(memcmp(blob, row[2].blob, row[2].len) == 0); 100 | } 101 | 102 | int main() 103 | { 104 | test_execute(test_one); 105 | 106 | return 0; 107 | } 108 | -------------------------------------------------------------------------------- /lib/sc/sc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | #ifndef SC_H 32 | #define SC_H 33 | 34 | #include 35 | #include 36 | #include 37 | 38 | #define SC_VERSION "2.0.0" 39 | 40 | #define sc_max(a, b) (((a) > (b)) ? (a) : (b)) 41 | #define sc_min(a, b) (((a) > (b)) ? (b) : (a)) 42 | 43 | struct sc_rand { 44 | unsigned char i; 45 | unsigned char j; 46 | unsigned char init[256]; 47 | }; 48 | 49 | /** 50 | * Pseudo random number generator - RC4 51 | * e.g : 52 | * char buf[256], tmp1[16], tmp2[4]; 53 | * 54 | * int fd = open("/dev/urandom", O_RDONLY); 55 | * if (fd < 0) { 56 | * return; // Error 57 | * } 58 | * 59 | * retry: 60 | * ssize_t sz = read(fd, buf, size); 61 | * if (sz < 0 && errno == EINTR) { 62 | * goto retry; 63 | * } 64 | * close(fd); 65 | * 66 | * struct sc_rand rnd; 67 | * sc_rand_init(&rnd, buf); // Init generator 68 | * 69 | * sc_rand_read(&rnd, tmp, sizeof(tmp); // Read random 70 | * sc_rand_read(&rnd, tmp, sizeof(tmp2); // Read random 71 | * 72 | * @param r rand 73 | * @param init rand source, possibly from /dev/urandom, must be 256 bytes long. 74 | */ 75 | void sc_rand_init(struct sc_rand *r, const unsigned char *init); 76 | void sc_rand_read(struct sc_rand *r, void *buf, int size); 77 | 78 | /** 79 | * @param num num 80 | * @return 'true' if num is power of 2 81 | */ 82 | bool sc_is_pow2(uint64_t num); 83 | 84 | /** 85 | * @param num num 86 | * @return next nearest power of two of size 87 | */ 88 | uint64_t sc_to_pow2(uint64_t size); 89 | 90 | /** 91 | * Bytes to human readable form, e.g 1024 bytes to "1 KB". 92 | * 93 | * @param buf buf to write output 94 | * @param len buf len 95 | * @param val val to be converted into human readable form 96 | * @return 'buf' on success, 'NULL' on failure. 97 | */ 98 | char *sc_bytes_to_size(char *buf, size_t len, uint64_t val); 99 | 100 | /** 101 | * Human readable string to bytes, e.g "1 KB" to 1024 bytes. 102 | * 103 | * @param buf buf to write output 104 | * @return positive value on success, '-1' on error 105 | */ 106 | int64_t sc_size_to_bytes(const char *buf); 107 | 108 | #endif 109 | -------------------------------------------------------------------------------- /.clang-format: -------------------------------------------------------------------------------- 1 | --- 2 | Language: Cpp 3 | AccessModifierOffset: -4 4 | AlignAfterOpenBracket: Align 5 | AlignConsecutiveAssignments: None 6 | AlignConsecutiveDeclarations: None 7 | AlignConsecutiveMacros: Consecutive 8 | AlignEscapedNewlines: Right 9 | AlignOperands: Align 10 | AlignTrailingComments: true 11 | AllowAllArgumentsOnNextLine: false 12 | AllowAllParametersOfDeclarationOnNextLine: false 13 | AllowShortBlocksOnASingleLine: Never 14 | AllowShortCaseLabelsOnASingleLine: false 15 | AllowShortFunctionsOnASingleLine: None 16 | AllowShortIfStatementsOnASingleLine: Never 17 | AllowShortLoopsOnASingleLine: false 18 | AlwaysBreakAfterDefinitionReturnType: None 19 | AlwaysBreakAfterReturnType: None 20 | AlwaysBreakBeforeMultilineStrings: false 21 | AlwaysBreakTemplateDeclarations: No 22 | BinPackArguments: true 23 | BinPackParameters: true 24 | 25 | BraceWrapping: 26 | AfterCaseLabel: false 27 | AfterClass: false 28 | AfterControlStatement: Never 29 | AfterEnum: true 30 | AfterFunction: true 31 | AfterNamespace: false 32 | AfterObjCDeclaration: false 33 | AfterStruct: false 34 | AfterUnion: false 35 | AfterExternBlock: false 36 | BeforeCatch: false 37 | BeforeElse: false 38 | IndentBraces: false 39 | SplitEmptyFunction: true 40 | SplitEmptyRecord: true 41 | SplitEmptyNamespace: true 42 | BreakBeforeBinaryOperators: None 43 | BreakBeforeBraces: Custom 44 | BreakBeforeInheritanceComma: false 45 | BreakBeforeTernaryOperators: false 46 | BreakConstructorInitializersBeforeComma: false 47 | BreakConstructorInitializers: BeforeColon 48 | BreakStringLiterals: false 49 | ColumnLimit: 80 50 | CommentPragmas: '^ IWYU pragma:' 51 | CompactNamespaces: false 52 | ConstructorInitializerAllOnOneLineOrOnePerLine: false 53 | ConstructorInitializerIndentWidth: 8 54 | ContinuationIndentWidth: 8 55 | DerivePointerAlignment: false 56 | DisableFormat: false 57 | ExperimentalAutoDetectBinPacking: false 58 | FixNamespaceComments: false 59 | ForEachMacros: 60 | - entry_foreach 61 | - sc_array_foreach 62 | - sc_list_foreach 63 | - sc_list_foreach_safe 64 | - sc_map_foreach 65 | - sc_map_foreach_key 66 | - sc_map_foreach_value 67 | - sc_queue_foreach 68 | IncludeBlocks: Regroup 69 | IncludeCategories: 70 | - Regex: '^("[.a-zA-Z0-9_-]+\.h")' 71 | Priority: 1 72 | 73 | - Regex: '^"(resql/)/' 74 | Priority: 2 75 | 76 | # Local headers: "foo/bar.h" 77 | - Regex: '^("[.a-zA-Z0-9_/-]+\.h")' 78 | Priority: 3 79 | 80 | # C Header: , , etc 81 | - Regex: '^(<[.a-zA-Z0-9_/-]+\.h>)' 82 | Priority: 4 83 | 84 | IncludeIsMainRegex: '(test)?$' 85 | IndentPPDirectives: None 86 | IndentGotoLabels: false 87 | IndentCaseBlocks: false 88 | IndentCaseLabels: false 89 | IndentWidth: 8 90 | IndentWrappedFunctionNames: false 91 | KeepEmptyLinesAtTheStartOfBlocks: true 92 | MacroBlockBegin: '' 93 | MacroBlockEnd: '' 94 | MaxEmptyLinesToKeep: 1 95 | NamespaceIndentation: None 96 | 97 | PenaltyBreakAssignment: 10 98 | PenaltyBreakBeforeFirstCallParameter: 30 99 | PenaltyBreakComment: 100 100 | PenaltyBreakFirstLessLess: 0 101 | PenaltyBreakString: 1000 102 | PenaltyExcessCharacter: 1000 103 | PenaltyReturnTypeOnItsOwnLine: 60 104 | 105 | PointerAlignment: Right 106 | ReflowComments: true 107 | SortIncludes: CaseSensitive 108 | SortUsingDeclarations: true 109 | SpaceAfterCStyleCast: true 110 | SpaceAfterTemplateKeyword: true 111 | SpaceBeforeAssignmentOperators: true 112 | SpaceBeforeParens: ControlStatements 113 | SpaceInEmptyParentheses: false 114 | SpacesBeforeTrailingComments: 1 115 | SpacesInAngles: false 116 | SpacesInContainerLiterals: false 117 | SpacesInCStyleCastParentheses: false 118 | SpacesInParentheses: false 119 | SpacesInSquareBrackets: false 120 | Standard: Latest 121 | TabWidth: 8 122 | UseTab: Always 123 | ... 124 | 125 | -------------------------------------------------------------------------------- /lib/sc/.clang-format: -------------------------------------------------------------------------------- 1 | --- 2 | Language: Cpp 3 | AccessModifierOffset: -4 4 | AlignAfterOpenBracket: Align 5 | AlignConsecutiveAssignments: None 6 | AlignConsecutiveDeclarations: None 7 | AlignConsecutiveMacros: Consecutive 8 | AlignEscapedNewlines: Right 9 | AlignOperands: Align 10 | AlignTrailingComments: true 11 | AllowAllArgumentsOnNextLine: false 12 | AllowAllParametersOfDeclarationOnNextLine: false 13 | AllowShortBlocksOnASingleLine: Never 14 | AllowShortCaseLabelsOnASingleLine: false 15 | AllowShortFunctionsOnASingleLine: None 16 | AllowShortIfStatementsOnASingleLine: Never 17 | AllowShortLoopsOnASingleLine: false 18 | AlwaysBreakAfterDefinitionReturnType: None 19 | AlwaysBreakAfterReturnType: None 20 | AlwaysBreakBeforeMultilineStrings: false 21 | AlwaysBreakTemplateDeclarations: No 22 | BinPackArguments: true 23 | BinPackParameters: true 24 | 25 | BraceWrapping: 26 | AfterCaseLabel: false 27 | AfterClass: false 28 | AfterControlStatement: Never 29 | AfterEnum: true 30 | AfterFunction: true 31 | AfterNamespace: false 32 | AfterObjCDeclaration: false 33 | AfterStruct: false 34 | AfterUnion: false 35 | AfterExternBlock: false 36 | BeforeCatch: false 37 | BeforeElse: false 38 | IndentBraces: false 39 | SplitEmptyFunction: true 40 | SplitEmptyRecord: true 41 | SplitEmptyNamespace: true 42 | BreakBeforeBinaryOperators: None 43 | BreakBeforeBraces: Custom 44 | BreakBeforeInheritanceComma: false 45 | BreakBeforeTernaryOperators: false 46 | BreakConstructorInitializersBeforeComma: false 47 | BreakConstructorInitializers: BeforeColon 48 | BreakStringLiterals: false 49 | ColumnLimit: 80 50 | CommentPragmas: '^ IWYU pragma:' 51 | CompactNamespaces: false 52 | ConstructorInitializerAllOnOneLineOrOnePerLine: false 53 | ConstructorInitializerIndentWidth: 8 54 | ContinuationIndentWidth: 8 55 | DerivePointerAlignment: false 56 | DisableFormat: false 57 | ExperimentalAutoDetectBinPacking: false 58 | FixNamespaceComments: false 59 | ForEachMacros: 60 | - entry_foreach 61 | - sc_array_foreach 62 | - sc_list_foreach 63 | - sc_list_foreach_safe 64 | - sc_map_foreach 65 | - sc_map_foreach_key 66 | - sc_map_foreach_value 67 | - sc_queue_foreach 68 | IncludeBlocks: Regroup 69 | IncludeCategories: 70 | - Regex: '^("[.a-zA-Z0-9_-]+\.h")' 71 | Priority: 1 72 | 73 | - Regex: '^"(resql/)/' 74 | Priority: 2 75 | 76 | # Local headers: "foo/bar.h" 77 | - Regex: '^("[.a-zA-Z0-9_/-]+\.h")' 78 | Priority: 3 79 | 80 | # C Header: , , etc 81 | - Regex: '^(<[.a-zA-Z0-9_/-]+\.h>)' 82 | Priority: 4 83 | 84 | IncludeIsMainRegex: '(test)?$' 85 | IndentPPDirectives: None 86 | IndentGotoLabels: false 87 | IndentCaseBlocks: false 88 | IndentCaseLabels: false 89 | IndentWidth: 8 90 | IndentWrappedFunctionNames: false 91 | KeepEmptyLinesAtTheStartOfBlocks: true 92 | MacroBlockBegin: '' 93 | MacroBlockEnd: '' 94 | MaxEmptyLinesToKeep: 1 95 | NamespaceIndentation: None 96 | 97 | PenaltyBreakAssignment: 10 98 | PenaltyBreakBeforeFirstCallParameter: 30 99 | PenaltyBreakComment: 100 100 | PenaltyBreakFirstLessLess: 0 101 | PenaltyBreakString: 1000 102 | PenaltyExcessCharacter: 1000 103 | PenaltyReturnTypeOnItsOwnLine: 60 104 | 105 | PointerAlignment: Right 106 | ReflowComments: true 107 | SortIncludes: CaseSensitive 108 | SortUsingDeclarations: true 109 | SpaceAfterCStyleCast: true 110 | SpaceAfterTemplateKeyword: true 111 | SpaceBeforeAssignmentOperators: true 112 | SpaceBeforeParens: ControlStatements 113 | SpaceInEmptyParentheses: false 114 | SpacesBeforeTrailingComments: 1 115 | SpacesInAngles: false 116 | SpacesInContainerLiterals: false 117 | SpacesInCStyleCastParentheses: false 118 | SpacesInParentheses: false 119 | SpacesInSquareBrackets: false 120 | Standard: Latest 121 | TabWidth: 8 122 | UseTab: Always 123 | ... 124 | 125 | -------------------------------------------------------------------------------- /src/state.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | #ifndef RESQL_STATE_H 33 | #define RESQL_STATE_H 34 | 35 | #include "aux.h" 36 | #include "meta.h" 37 | #include "rs.h" 38 | 39 | #include "sc/sc.h" 40 | #include "sc/sc_list.h" 41 | #include "sc/sc_map.h" 42 | 43 | struct state_cb { 44 | void *arg; 45 | const char *(*add_node)(void *arg, const char *node); 46 | const char *(*remove_node)(void *arg, const char *node); 47 | const char *(*shutdown)(void *arg, const char *node); 48 | }; 49 | 50 | struct state { 51 | struct state_cb cb; 52 | 53 | char *path; 54 | char *ss_path; 55 | char *ss_tmp_path; 56 | char *last_err; 57 | struct session *session; // current session 58 | 59 | bool closed; 60 | 61 | // operation flags 62 | bool client; 63 | bool readonly; 64 | bool full; 65 | int64_t max_page; 66 | uint64_t session_timeout; 67 | 68 | struct aux aux; 69 | struct meta meta; 70 | uint64_t term; 71 | uint64_t index; 72 | 73 | // time 74 | uint64_t timestamp; 75 | uint64_t monotonic; 76 | uint64_t realtime; 77 | 78 | struct sc_list disconnects; 79 | 80 | struct sc_map_sv nodes; 81 | struct sc_map_sv names; 82 | struct sc_map_64v ids; 83 | struct sc_buf tmp; 84 | 85 | struct sc_rand rrand; 86 | struct sc_rand wrand; 87 | char err[128]; 88 | }; 89 | 90 | int state_global_init(); 91 | int state_global_shutdown(); 92 | 93 | void state_init(struct state *st, struct state_cb cb, const char *path, 94 | const char *name); 95 | int state_term(struct state *st); 96 | 97 | void state_config(sqlite3_context *ctx, int argc, sqlite3_value **argv); 98 | int state_randomness(sqlite3_vfs *vfs, int size, char *out); 99 | int state_currenttime(sqlite3_vfs *vfs, sqlite3_int64 *val); 100 | 101 | int state_read_snapshot(struct state *st, bool in_memory); 102 | int state_read_for_snapshot(struct state *st); 103 | 104 | int state_open(struct state *st, bool in_memory); 105 | int state_close(struct state *st); 106 | 107 | int state_initial_snapshot(struct state *st); 108 | int state_apply_readonly(struct state *st, uint64_t cid, unsigned char *buf, 109 | uint32_t len, struct sc_buf *resp); 110 | 111 | int state_apply(struct state *st, uint64_t index, unsigned char *e, 112 | struct session **s); 113 | 114 | #endif 115 | -------------------------------------------------------------------------------- /src/cmd.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | #ifndef RESQL_CMD_H 33 | #define RESQL_CMD_H 34 | 35 | #include "meta.h" 36 | 37 | #include "sc/sc_buf.h" 38 | 39 | enum cmd_id 40 | { 41 | CMD_INIT, 42 | CMD_META, 43 | CMD_TERM, 44 | CMD_REQUEST, 45 | CMD_CONNECT, 46 | CMD_DISCONNECT, 47 | CMD_TIMESTAMP, 48 | CMD_INFO, 49 | CMD_LOG 50 | }; 51 | 52 | struct cmd_init { 53 | uint64_t monotonic; 54 | uint64_t realtime; 55 | unsigned char rand[256]; 56 | }; 57 | 58 | struct cmd_meta { 59 | struct meta meta; 60 | }; 61 | 62 | struct cmd_config { 63 | bool add; 64 | const char *name; 65 | }; 66 | 67 | struct cmd_term { 68 | uint64_t monotonic; 69 | uint64_t realtime; 70 | }; 71 | 72 | struct cmd_connect { 73 | const char *name; 74 | const char *local; 75 | const char *remote; 76 | }; 77 | 78 | struct cmd_disconnect { 79 | const char *name; 80 | bool clean; 81 | }; 82 | 83 | struct cmd_timestamp { 84 | uint64_t monotonic; 85 | uint64_t realtime; 86 | }; 87 | 88 | struct cmd_log { 89 | const char *level; 90 | const char *log; 91 | }; 92 | 93 | struct cmd { 94 | union { 95 | struct cmd_init init; 96 | struct cmd_meta meta; 97 | struct cmd_config config; 98 | struct cmd_term term; 99 | struct cmd_connect connect; 100 | struct cmd_disconnect disconnect; 101 | struct cmd_timestamp timestamp; 102 | struct cmd_log log; 103 | }; 104 | }; 105 | 106 | void cmd_encode_init(struct sc_buf *b, unsigned char rand[256]); 107 | struct cmd_init cmd_decode_init(struct sc_buf *b); 108 | 109 | void cmd_encode_meta(struct sc_buf *b, struct meta *meta); 110 | struct cmd_meta cmd_decode_meta(struct sc_buf *b); 111 | 112 | void cmd_encode_term(struct sc_buf *b); 113 | struct cmd_term cmd_decode_term(struct sc_buf *b); 114 | 115 | void cmd_encode_connect(struct sc_buf *b, const char *name, const char *local, 116 | const char *remote); 117 | struct cmd_connect cmd_decode_connect(struct sc_buf *buf); 118 | 119 | void cmd_encode_disconnect(struct sc_buf *b, const char *name, bool clean); 120 | struct cmd_disconnect cmd_decode_disconnect(struct sc_buf *b); 121 | 122 | void cmd_encode_timestamp(struct sc_buf *b); 123 | struct cmd_timestamp cmd_decode_timestamp(struct sc_buf *b); 124 | 125 | void cmd_encode_log(struct sc_buf *b, const char *level, const char *log); 126 | struct cmd_log cmd_decode_log(struct sc_buf *b); 127 | 128 | #endif 129 | -------------------------------------------------------------------------------- /lib/sc/sc_uri.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | #ifndef SC_URI_H 33 | #define SC_URI_H 34 | 35 | #define SC_URI_VERSION "2.0.0" 36 | 37 | #ifdef SC_HAVE_CONFIG_H 38 | #include "config.h" 39 | #else 40 | #define sc_uri_malloc malloc 41 | #define sc_uri_free free 42 | #endif 43 | 44 | #include 45 | #include 46 | 47 | /** 48 | * Based on RFC 3986 49 | * 50 | * The following is a example URIs and their component parts: 51 | * 52 | * foo://user:password@example.com:8042/over/there?name=ferret#nose 53 | * \_/ \____________________________/\_________/ \_________/ \__/ 54 | * | | | | | 55 | * scheme authority path query fragment 56 | * 57 | * 58 | * user:password@example.com:8042 59 | * \__________/ \_________/ \__/ 60 | * | | | 61 | * userinfo host port 62 | * -------------------------------------------------------------------- 63 | * 64 | */ 65 | 66 | struct sc_uri { 67 | const char *str; // Full string 68 | const char *scheme; 69 | const char *host; 70 | const char *userinfo; 71 | const char *port; 72 | const char *path; 73 | const char *query; 74 | const char *fragment; 75 | 76 | char buf[]; 77 | }; 78 | 79 | /** 80 | * Parse uri. 81 | * 82 | * Internally, it does a single allocation but each part is also represented as 83 | * NULL ended string. 84 | * 85 | * E.g : 86 | * 87 | * struct sc_uri* uri; 88 | * 89 | * struct sc_uri* uri; 90 | * uri = 91 | * sc_uri_create("http://user:pass@any.com:8042/over/there?name=jane#doe"); 92 | * 93 | * printf("%s \n", uri->str); // prints full string. 94 | * printf("%s \n", uri->scheme); // prints "http" 95 | * printf("%s \n", uri->host); // prints "any.com" 96 | * printf("%s \n", uri->userinfo); // prints "user:pass" 97 | * printf("%s \n", uri->port); // prints "8042" 98 | * printf("%s \n", uri->path); // prints "/over/there" 99 | * printf("%s \n", uri->query); // prints "name=jane" 100 | * printf("%s \n", uri->fragment); // prints "doe" 101 | * 102 | * 103 | * @param str uri string 104 | * @return uri, NULL on error 105 | */ 106 | struct sc_uri *sc_uri_create(const char *str); 107 | 108 | /** 109 | * @param uri uri 110 | */ 111 | void sc_uri_destroy(struct sc_uri **uri); 112 | 113 | #endif 114 | -------------------------------------------------------------------------------- /lib/sc/sc_signal.h: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | #ifndef SC_SIGNAL_H 33 | #define SC_SIGNAL_H 34 | 35 | #include 36 | #include 37 | #include 38 | 39 | #define SC_SIGNAL_VERSION "2.0.0" 40 | 41 | /** 42 | * Set shutdown fd here. When shutdown signal is received e.g SIGINT, SIGTERM. 43 | * Signal handler will write 1 byte to shutdown fd. So, your app can detect 44 | * shutdown command received and shutdown properly (Assuming you observe this fd 45 | * with select() like function). Before app shutdowns, if another shutdown 46 | * signal is received, _Exit() is called without waiting. 47 | * e.g CTRL+C to shutdown, twice CTRL+C means 'I don't want to wait anything'. 48 | */ 49 | #if defined(_WIN32) 50 | #include 51 | extern volatile SOCKET sc_signal_shutdown_fd; 52 | #else 53 | extern volatile sig_atomic_t sc_signal_shutdown_fd; 54 | #endif 55 | 56 | /** 57 | * Set log file fd here, logging will be redirected to this fd, otherwise 58 | * STDERR_FILENO or STDOUT_FILENO will be used. 59 | */ 60 | extern volatile sig_atomic_t sc_signal_log_fd; 61 | 62 | // Internal variable to handle twice shutdown signal. 63 | extern volatile sig_atomic_t sc_signal_will_shutdown; 64 | 65 | /** 66 | * Init signal handler, hooks for shutdown signals and some fatal signals. 67 | * @return '0' on success, '-1' on failure 68 | */ 69 | int sc_signal_init(); 70 | 71 | /** 72 | * Signal safe logging 73 | * 74 | * @param fd fd to write log 75 | * @param buf buf 76 | * @param size size 77 | * @param fmt fmt 78 | * @param ... args 79 | */ 80 | void sc_signal_log(int fd, char *buf, size_t size, char *fmt, ...); 81 | 82 | /** 83 | * Signal safe vsnprintf. 84 | * Supports only "%s, "%u", "%lu", "%llu", "%d", "%ld", "%lld" and "%p" 85 | * 86 | * @param buf buf 87 | * @param size size 88 | * @param fmt fmt 89 | * @param va va_list 90 | * @return number of characters written on success, '-1' on failure. 91 | */ 92 | int sc_signal_vsnprintf(char *buf, size_t size, const char *fmt, va_list va); 93 | 94 | /** 95 | * Signal safe snprintf. 96 | * Supports only "%s, "%u", "%lu", "%llu", "%d", "%ld", "%lld" and "%p" 97 | * 98 | * @param buf buf 99 | * @param size size 100 | * @param fmt fmt 101 | * @param ... args 102 | * @return number of characters written on success, '-1' on failure. 103 | */ 104 | int sc_signal_snprintf(char *buf, size_t size, const char *fmt, ...); 105 | 106 | #endif 107 | -------------------------------------------------------------------------------- /bin/resql.ini: -------------------------------------------------------------------------------- 1 | # Resql config file 2 | 3 | ;comment can start like this or 4 | #like this 5 | #inline comments starts with " #" ( and #), or ";" 6 | 7 | ## Configurations can be overridden on command line by passing arguments 8 | ## e.g ./resql --node-log-level=DEBUG --advanced-fsync=false 9 | 10 | [node] 11 | # node name must be unique. You cannot change it once the server starts 12 | # No default value. You must specify this option. 13 | name = node0 14 | 15 | # bind address and port. Currently unix domain sockets and tcp are supported 16 | # Multiple addresses can be given : 17 | # bind-url = tcp://node0@127.0.0.1:7600 tcp://node0@202.100.51.10:7600 18 | # Default is tcp://127.0.0.1:7600 19 | bind-url = tcp://node0@127.0.0.1:7600 unix:///tmp/resql 20 | 21 | # This is the address that other nodes and clients will use to connect to this 22 | # server. If you are not using NAT, it's usually the same with bind-url config. 23 | # Default is tcp://127.0.0.1:7600 24 | advertise-url = tcp://node0@127.0.0.1:7600 25 | 26 | # Outgoing address and port. The server will use this address and port to open 27 | # connection to other nodes. Useful if you want to deploy firewall rules and 28 | # limit port usage. 29 | # Default is whatever OS assigns when you try to open a connection 30 | #source_addr = 127.0.0.1 31 | #source_port = 9000 32 | 33 | # Valid values are DEBUG, INFO, WARN, ERROR, OFF 34 | # Default is INFO 35 | log-level = INFO 36 | 37 | # You can give a file path to get logs to a file 38 | # Default is stdout 39 | log-destination = stdout 40 | 41 | # Persistence directory 42 | # Default is the current working directory of the executable 43 | directory = . 44 | 45 | # This option controls if the database will be in memory or on disk. There is 46 | # always a copy of your data on disk (snapshot + WAL files). On a crash or 47 | # any time required, the database will start from that copy. This option 48 | # controls current copy of your database and it's solely about performance. 49 | # It doesn't put your data at risk. If the database is in memory, operations are 50 | # completed faster. You should try and see if it makes your operations faster 51 | # though. If it's false, your DB is on disk and Resql still uses some amount of 52 | # RAM as its cache. So, the difference might not be visible for your use case. 53 | # Default is true 54 | in-memory = true 55 | 56 | [cluster] 57 | # Cluster name. All other nodes and clients must be configured with this name 58 | # Default is "cluster", this value can't be changed once the server starts 59 | name = cluster 60 | 61 | # Node addresses in the cluster. If you want to start a cluster including this 62 | # node, write all nodes here separated with a comma, e.g: 63 | # nodes = tcp://node0@127.0.0.1:7600 tcp://node1@127.0.0.1:7601 tcp://node2@127.0.0.1:7602 64 | # node names and addresses must match. 65 | # if you want to join this node to a cluster, just add a single node, so this 66 | # node will realize it's not part of a cluster and it will join others. 67 | # Default is tcp://127.0.0.1:7600 68 | nodes = tcp://node0@127.0.0.1:7600 69 | 70 | [advanced] 71 | # Resql calls fsync when required to be sure that data is persisted to the disk. 72 | # To guarantee safety, this option must be true. Otherwise, if operating system 73 | # crashes, you may lose some updates which are waiting to be persisted to the 74 | # disk. If you don't care about this, e.g you are using Resql as a cache, you 75 | # can set it to false and avoid disk performance bottleneck. 76 | # Default is true 77 | fsync = true 78 | 79 | # If a node doesn't hear from the leader node for a while, it assumes leader 80 | # is down and it starts an election to become the new leader. If the leader is 81 | # submitted a long-running query or tcp latency between nodes is high, a node 82 | # may think the leader is down even though it's not. So, this is a trade-off. 83 | # If you set this value to something low, follower nodes will elect a new leader 84 | # whenever leader can't send a message within this interval. If you set this to 85 | # something higher, when a leader disconnects from others, followers will wait 86 | # heartbeat milliseconds to elect a new leader. Until then, client operations 87 | # will be blocked. 88 | # Default is 4000 milliseconds. 89 | heartbeat = 4000 90 | -------------------------------------------------------------------------------- /test/restart_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | #include "file.h" 33 | #include "resql.h" 34 | #include "rs.h" 35 | #include "server.h" 36 | #include "test_util.h" 37 | 38 | #include "sc/sc_log.h" 39 | 40 | #include 41 | 42 | static void restart_simple() 43 | { 44 | file_clear_dir("/tmp/node0", ".resql"); 45 | 46 | int rc; 47 | resql *c; 48 | struct resql_column *row; 49 | struct resql_result *rs = NULL; 50 | 51 | test_server_create(true, 0, 1); 52 | c = test_client_create(); 53 | 54 | for (int i = 0; i < 100; i++) { 55 | 56 | resql_put_sql(c, "Select 'resql'"); 57 | 58 | rc = resql_exec(c, true, &rs); 59 | client_assert(c, rc == RESQL_OK); 60 | 61 | do { 62 | rs_assert(resql_column_count(rs) == 1); 63 | row = resql_row(rs); 64 | 65 | while (resql_row(rs)) { 66 | rs_assert(row[0].type == RESQL_TEXT); 67 | rs_assert(strcmp("resql", row[0].text) == 0); 68 | } 69 | } while (resql_next(rs) == RESQL_OK); 70 | } 71 | 72 | test_server_destroy_all(); 73 | test_server_start(true, 0, 1); 74 | 75 | for (int i = 0; i < 100; i++) { 76 | resql_put_sql(c, "Select 'resql'"); 77 | 78 | rc = resql_exec(c, false, &rs); 79 | client_assert(c, rc == RESQL_OK); 80 | 81 | do { 82 | rs_assert(resql_column_count(rs) == 1); 83 | row = resql_row(rs); 84 | 85 | while (resql_row(rs)) { 86 | rs_assert(row[0].type == RESQL_TEXT); 87 | rs_assert(strcmp("resql", row[0].text) == 0); 88 | } 89 | } while (resql_next(rs) == RESQL_OK); 90 | } 91 | } 92 | 93 | static void restart_simple2() 94 | { 95 | file_clear_dir("/tmp/node0", ".resql"); 96 | 97 | int rc; 98 | resql *c; 99 | struct resql_column *row; 100 | struct resql_result *rs = NULL; 101 | 102 | test_server_create(true, 0, 1); 103 | c = test_client_create(); 104 | 105 | for (int i = 0; i < 100; i++) { 106 | 107 | resql_put_sql(c, "Select 'resql'"); 108 | 109 | rc = resql_exec(c, true, &rs); 110 | client_assert(c, rc == RESQL_OK); 111 | 112 | do { 113 | rs_assert(resql_column_count(rs) == 1); 114 | row = resql_row(rs); 115 | 116 | while (resql_row(rs)) { 117 | rs_assert(row[0].type == RESQL_TEXT); 118 | rs_assert(strcmp("resql", row[0].text) == 0); 119 | } 120 | } while (resql_next(rs) == RESQL_OK); 121 | } 122 | 123 | test_server_destroy_all(); 124 | 125 | test_server_start(true, 0, 1); 126 | test_server_destroy_all(); 127 | 128 | test_client_destroy_all(); 129 | test_server_start(true, 0, 1); 130 | test_client_create(); 131 | } 132 | 133 | int main(void) 134 | { 135 | test_execute(restart_simple); 136 | test_execute(restart_simple2); 137 | 138 | return 0; 139 | } 140 | -------------------------------------------------------------------------------- /jresql/src/main/java/resql/Resql.java: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | package resql; 33 | 34 | public interface Resql extends AutoCloseable { 35 | 36 | String VERSION = "0.1.4-latest"; 37 | 38 | /** 39 | * Prepare Statement 40 | * 41 | * @param sql Statement sql 42 | * @return Prepared statement 43 | */ 44 | PreparedStatement prepare(String sql); 45 | 46 | /** 47 | * Delete prepared statement. 48 | * 49 | * @param statement Prepared statement 50 | */ 51 | void delete(PreparedStatement statement); 52 | 53 | /** 54 | * Add statement to the current operation batch. 55 | * 56 | * @param statement Statement 57 | */ 58 | void put(String statement); 59 | 60 | /** 61 | * Add statement to current operation batch. 62 | * 63 | * @param statement Statement 64 | */ 65 | void put(PreparedStatement statement); 66 | 67 | /** 68 | * Bind value by index. 'put' must be called before binding value. 69 | * Otherwise, throws ResqlSQLException. 70 | * 71 | * Valid 'value' types are Long, Double, String, byte[] and null. 72 | * 73 | * e.g : 74 | * client.put("SELECT * FROM test WHERE key = '?'); 75 | * client.bind(0, "jane"); 76 | * 77 | * @param index index 78 | * @param value value 79 | */ 80 | void bind(int index, Object value); 81 | 82 | /** 83 | * Bind value by index. 'put' must be called before binding value. 84 | * Otherwise, throws ResqlSQLException. 85 | * 86 | * Valid 'value' types are Long, Double, String, byte[] and null. 87 | * 88 | * e.g : 89 | * client.put("SELECT * FROM test WHERE key = :key"); 90 | * client.bind(":key", "jane"); 91 | * 92 | * @param param param 93 | * @param value value 94 | */ 95 | void bind(String param, Object value); 96 | 97 | /** 98 | * Execute statements. Set readonly to 'true' if all the statements are 99 | * readonly. This is a performance optimization. If readonly is set even 100 | * statements are not, operation will fail and ResultSet return value will 101 | * indicate the error. 102 | * 103 | * @param readonly true if all the statements are readonly. 104 | * @return Operation result list. ResultSet will contain multiple 105 | * results if multiple statements are executed. 106 | */ 107 | ResultSet execute(boolean readonly); 108 | 109 | 110 | /** 111 | * Clear current operation batch. e.g You add a few statements and before 112 | * calling execute() if you want to cancel it and start all over again. 113 | */ 114 | void clear(); 115 | 116 | /** 117 | * Shutdown client. 118 | */ 119 | void shutdown(); 120 | } 121 | -------------------------------------------------------------------------------- /src/cmd.c: -------------------------------------------------------------------------------- 1 | /* 2 | * BSD-3-Clause 3 | * 4 | * Copyright 2021 Ozan Tezcan 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 3. Neither the name of the copyright holder nor the names of its contributors 16 | * may be used to endorse or promote products derived from this software 17 | * without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 24 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 25 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | #include "cmd.h" 33 | 34 | #include "sc/sc_time.h" 35 | 36 | void cmd_encode_init(struct sc_buf *b, unsigned char rand[256]) 37 | { 38 | sc_buf_put_64(b, sc_time_ms()); 39 | sc_buf_put_64(b, sc_time_mono_ms()); 40 | sc_buf_put_raw(b, rand, 256); 41 | } 42 | 43 | struct cmd_init cmd_decode_init(struct sc_buf *b) 44 | { 45 | struct cmd_init init; 46 | 47 | init.realtime = sc_buf_get_64(b); 48 | init.monotonic = sc_buf_get_64(b); 49 | sc_buf_get_data(b, init.rand, sizeof(init.rand)); 50 | 51 | return init; 52 | } 53 | 54 | void cmd_encode_term(struct sc_buf *b) 55 | { 56 | sc_buf_put_64(b, sc_time_ms()); 57 | sc_buf_put_64(b, sc_time_mono_ms()); 58 | } 59 | 60 | struct cmd_term cmd_decode_term(struct sc_buf *b) 61 | { 62 | struct cmd_term start; 63 | 64 | start.realtime = sc_buf_get_64(b); 65 | start.monotonic = sc_buf_get_64(b); 66 | 67 | return start; 68 | } 69 | 70 | void cmd_encode_meta(struct sc_buf *b, struct meta *meta) 71 | { 72 | meta_encode(meta, b); 73 | } 74 | 75 | struct cmd_meta cmd_decode_meta(struct sc_buf *b) 76 | { 77 | struct cmd_meta cmd; 78 | 79 | meta_init(&cmd.meta, ""); 80 | meta_decode(&cmd.meta, b); 81 | 82 | return cmd; 83 | } 84 | 85 | void cmd_encode_connect(struct sc_buf *b, const char *name, const char *local, 86 | const char *remote) 87 | { 88 | sc_buf_put_str(b, name); 89 | sc_buf_put_str(b, local); 90 | sc_buf_put_str(b, remote); 91 | } 92 | 93 | struct cmd_connect cmd_decode_connect(struct sc_buf *buf) 94 | { 95 | struct cmd_connect cmd; 96 | 97 | cmd.name = sc_buf_get_str(buf); 98 | cmd.local = sc_buf_get_str(buf); 99 | cmd.remote = sc_buf_get_str(buf); 100 | 101 | return cmd; 102 | } 103 | 104 | void cmd_encode_disconnect(struct sc_buf *b, const char *name, bool clean) 105 | { 106 | sc_buf_put_str(b, name); 107 | sc_buf_put_bool(b, clean); 108 | } 109 | 110 | struct cmd_disconnect cmd_decode_disconnect(struct sc_buf *b) 111 | { 112 | struct cmd_disconnect cmd; 113 | 114 | cmd.name = sc_buf_get_str(b); 115 | cmd.clean = sc_buf_get_bool(b); 116 | 117 | return cmd; 118 | } 119 | 120 | void cmd_encode_timestamp(struct sc_buf *b) 121 | { 122 | sc_buf_put_64(b, sc_time_ms()); 123 | sc_buf_put_64(b, sc_time_mono_ms()); 124 | } 125 | 126 | struct cmd_timestamp cmd_decode_timestamp(struct sc_buf *b) 127 | { 128 | struct cmd_timestamp time; 129 | 130 | time.realtime = sc_buf_get_64(b); 131 | time.monotonic = sc_buf_get_64(b); 132 | 133 | return time; 134 | } 135 | 136 | void cmd_encode_log(struct sc_buf *b, const char *level, const char *log) 137 | { 138 | sc_buf_put_str(b, level); 139 | sc_buf_put_str(b, log); 140 | } 141 | 142 | struct cmd_log cmd_decode_log(struct sc_buf *b) 143 | { 144 | struct cmd_log log; 145 | 146 | log.level = sc_buf_get_str(b); 147 | log.log = sc_buf_get_str(b); 148 | 149 | return log; 150 | } 151 | --------------------------------------------------------------------------------