From 1537030212c6f25e19908f2f561fe9427737b605 Mon Sep 17 00:00:00 2001 From: Hirotaka Wakabayashi Date: Wed, 23 Oct 2024 07:16:55 +0900 Subject: [PATCH] Updates tests (#14) --- .github/workflows/cron.yml | 23 -- .github/workflows/main.yml | 98 ++++-- .github/workflows/upload.sh | 139 -------- .gitignore | 109 +++++- ChangeLog | 16 + README.md | 4 + checkstyle.xml | 11 +- .../java/ax/antpick/k2hash/example/Attr.java | 164 +++++++++ k2hash/pom.xml | 150 +++++++++ .../src/main/java}/CLibrary.java | 27 +- .../src/main/java}/K2hash.java | 250 +++++++++----- .../src/main/java}/K2hashAttrPack.java | 15 +- .../src/main/java}/K2hashKeyQueue.java | 100 +++--- .../src/main/java}/K2hashLibrary.java | 123 +++---- .../src/main/java}/K2hashQueue.java | 80 +++-- k2hash/src/main/java/package-info.java | 31 ++ .../src/test/java}/K2hashFileTest.java | 310 ++++++++++-------- .../src/test/java}/K2hashKeyQueueTest.java | 108 +++--- .../src/test/java}/K2hashQueueTest.java | 85 +++-- .../src}/test/resources/logback-test.xml | 0 lib/install.sh | 244 -------------- pom.xml | 195 +++-------- settings_github.xml | 34 ++ 23 files changed, 1288 insertions(+), 1028 deletions(-) delete mode 100644 .github/workflows/cron.yml delete mode 100755 .github/workflows/upload.sh create mode 100644 example/src/main/java/ax/antpick/k2hash/example/Attr.java create mode 100644 k2hash/pom.xml rename {src/main/java/ax/antpick/k2hash => k2hash/src/main/java}/CLibrary.java (83%) rename {src/main/java/ax/antpick/k2hash => k2hash/src/main/java}/K2hash.java (86%) rename {src/main/java/ax/antpick/k2hash => k2hash/src/main/java}/K2hashAttrPack.java (95%) rename {src/main/java/ax/antpick/k2hash => k2hash/src/main/java}/K2hashKeyQueue.java (91%) rename {src/main/java/ax/antpick/k2hash => k2hash/src/main/java}/K2hashLibrary.java (93%) rename {src/main/java/ax/antpick/k2hash => k2hash/src/main/java}/K2hashQueue.java (91%) create mode 100644 k2hash/src/main/java/package-info.java rename {src/test/java/ax/antpick/k2hash => k2hash/src/test/java}/K2hashFileTest.java (61%) rename {src/test/java/ax/antpick/k2hash => k2hash/src/test/java}/K2hashKeyQueueTest.java (79%) rename {src/test/java/ax/antpick/k2hash => k2hash/src/test/java}/K2hashQueueTest.java (79%) rename {src => k2hash/src}/test/resources/logback-test.xml (100%) delete mode 100755 lib/install.sh diff --git a/.github/workflows/cron.yml b/.github/workflows/cron.yml deleted file mode 100644 index f0be168..0000000 --- a/.github/workflows/cron.yml +++ /dev/null @@ -1,23 +0,0 @@ -name: Periodic build for compatibility problems detection -on: - schedule: - - cron: '0 0 * * 1' - -jobs: - build: - - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v2 - - name: Setup Java JDK - uses: actions/setup-java@v2 - with: - java-version: '11' - distribution: 'adopt' - server-id: ossrh - settings-path: ${{ github.workspace }} - server-username: OSSRH_USERNAME - server-password: OSSRH_PASSWORD - gpg-private-key: ${{ secrets.MAVEN_GPG_PRIVATE_KEY }} # Value of the GPG private key to import - gpg-passphrase: MAVEN_GPG_PASSPHRASE # env variable of GPG private key passphrase diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 32345d1..0237e1f 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,4 +1,4 @@ -name: CI +name: K2hash Java CI on: # Trigger the workflow on push or pull request, @@ -19,41 +19,67 @@ jobs: build: if: "! contains(toJSON(github.event.commits.*.message), '[skip ci]') && ! contains(toJSON(github.event.commits.*.message), '[ci skip]')" + runs-on: ubuntu-latest + strategy: + matrix: + java: [ '8', '11', '17', '21' ] + name: Java ${{ matrix.java }} steps: - - uses: actions/checkout@v2 - - name: Setup Java JDK - uses: actions/setup-java@v2 - with: - java-version: '11' - distribution: 'adopt' - server-id: ossrh - settings-path: ${{ github.workspace }} - server-username: OSSRH_USERNAME - server-password: OSSRH_PASSWORD - gpg-private-key: ${{ secrets.MAVEN_GPG_PRIVATE_KEY }} # Value of the GPG private key to import - gpg-passphrase: MAVEN_GPG_PASSPHRASE # env variable of GPG private key passphrase - - - name: Install dependent packages, build package and run tests - run: mvn -B clean exec:exec package - - - name: Upload a jar file to asset - if: github.event_name == 'release' - run: mvn -B exec:exec -Pupload - env: - GITHUB_TOKEN: ${{ github.token }} - - - name: Deploy the docs to github - if: github.event_name == 'release' - run: mvn -B site -s settings_github.xml - env: - GITPAGES_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKE_FOR_GITPAGES }} - - - name: Publish to Apache Maven Central - if: github.event_name == 'release' - run: mvn -B deploy -Prelease -s $GITHUB_WORKSPACE/settings.xml - env: - OSSRH_USERNAME: ${{ secrets.OSSRH_USERNAME }} - OSSRH_PASSWORD: ${{ secrets.OSSRH_PASSWORD }} - MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }} + - uses: actions/checkout@v4 + - name: Setup Java JDK + uses: actions/setup-java@v3 + with: + java-version: ${{ matrix.java }} + distribution: 'zulu' + server-id: ossrh + settings-path: ${{ github.workspace }} + server-username: OSSRH_USERNAME + server-password: OSSRH_PASSWORD + gpg-private-key: ${{ secrets.MAVEN_GPG_PRIVATE_KEY }} # Value of the GPG private key to import + gpg-passphrase: MAVEN_GPG_PASSPHRASE # env variable of GPG private key passphrase + + - name: Set environments + run: | + echo "COMMON_MAVEN_OPTS=-Dmaven.compiler.failOnWarning=true -Dmaven.compiler.showDeprecation=true -Dmaven.compiler.verbose=true -Dmaven.compiler.debuglevel=lines,vars,source" >> "$GITHUB_ENV" + + - name: Install dependent packages + run: | + sudo apt-get install -y curl + curl -s https://packagecloud.io/install/repositories/antpickax/stable/script.deb.sh | sudo bash + sudo apt-get install -y k2hash-dev + + - name: Run checkstyle + run: | + mvn ${{ env.COMMON_MAVEN_OPTS }} checkstyle:check + + - name: Run package on version 8 + if: "matrix.java == '8'" + run: | + mvn ${{ env.COMMON_MAVEN_OPTS }} -Dmaven.compiler.target=8 package + + - name: Run package on version other than 8 + if: "matrix.java != '8'" + run: | + mvn ${{ env.COMMON_MAVEN_OPTS }} -Dmaven.compiler.target=${{ matrix.java }} -Dmaven.compiler.source=${{ matrix.java }} -Dmaven.compiler.release=${{ matrix.java }} package + + - name: Run javadoc + run: | + mvn ${{ env.COMMON_MAVEN_OPTS }} javadoc:javadoc + + - name: Deploy the docs to github + if: "github.event_name == 'release' && matrix.java == '21'" + run: | + mvn ${{ env.COMMON_MAVEN_OPTS }} site -s settings_github.xml + env: + GITPAGES_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKE_FOR_GITPAGES }} + + - name: Publish to Apache Maven Central + if: "github.event_name == 'release' && matrix.java == '21'" + run: | + mvn ${{ env.COMMON_MAVEN_OPTS }} deploy -Prelease -s $GITHUB_WORKSPACE/settings.xml + env: + OSSRH_USERNAME: ${{ secrets.OSSRH_USERNAME }} + OSSRH_PASSWORD: ${{ secrets.OSSRH_PASSWORD }} + MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }} diff --git a/.github/workflows/upload.sh b/.github/workflows/upload.sh deleted file mode 100755 index 16e5285..0000000 --- a/.github/workflows/upload.sh +++ /dev/null @@ -1,139 +0,0 @@ -#!/bin/sh -# -# The MIT License -# -# Copyright 2018 Yahoo Japan Corporation. -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -# THE SOFTWARE. -# -# AUTHOR: Hirotaka Wakabayashi -# CREATE: Fri, 14 Sep 2018 -# REVISION: -# - -# Sets the default locale. LC_ALL has precedence over other LC* variables. -unset LANG -unset LANGUAGE -LC_ALL=en_US.utf8 -export LC_ALL - -# Sets PATH. setup_*.sh uses useradd command -PATH=${PATH}:/usr/sbin:/sbin - -# an unset parameter expansion will fail -set -u - -# umask 022 is enough -umask 022 - -# environments -REPOSITORY_PATH=${GITHUB_REPOSITORY} -SRCDIR=$(cd $(dirname "$0") && pwd) -DEBUG=1 -if test "${DEBUG}" -eq 1; then - TAG="$(basename $0) -s" -else - TAG=$(basename $0) -fi - -# GITHUB_TOKEN should be defined. -if test -z "${GITHUB_TOKEN}"; then - logger -t ${TAG} -p user.error "No GITHUB_TOKEN variable defined." - exit 1 -fi -# GITHUB_REPOSITORY should be defined. -# https://help.github.com/en/actions/automating-your-workflow-with-github-actions/using-environment-variables -if test -z "${GITHUB_REPOSITORY}"; then - logger -t ${TAG} -p user.error "No GITHUB_REPOSITORY variable defined." - exit 1 -fi - -# API SPEC -# https://developer.github.com/v3/repos/releases/#get-the-latest-release -TEMP_FILE=$(mktemp) -curl -sH "Authorization: token ${GITHUB_TOKEN}" "https://api.github.com/repos/${REPOSITORY_PATH}/releases/latest" -o ${TEMP_FILE} -if test "${?}" != "0"; then - logger -t ${TAG} -p user.error "GitHub API get-the-latest-release returned error." - rm -f ${TEMP_FILE} - exit 1 -fi -UPLOAD_URL=$(cat ${TEMP_FILE} | python3 -c "import sys, json; print(json.load(sys.stdin)['upload_url'])") -if test -z "${UPLOAD_URL}"; then - logger -t ${TAG} -p user.error "UPLOAD_URL is empty(or undefined), which should be defined." - rm -f ${TEMP_FILE} - exit 1 -fi -TAG_NAME=$(cat ${TEMP_FILE} | python3 -c "import sys, json; print(json.load(sys.stdin)['tag_name'])" | perl -ne '/v(.*)/; print $1;') -if test -z "${TAG_NAME}"; then - logger -t ${TAG} -p user.error "TAG_NAME is empty(or undefined), which should be defined." - rm -f ${TEMP_FILE} - exit 1 -fi - -# Get the jar file in local target directory. -TARGET_FILE="./target/k2hash-${TAG_NAME}.jar" -if ! test -f "${TARGET_FILE}"; then - logger -t ${TAG} -p user.error "${TARGET_FILE} doesn't exist, which should exist." - rm -f ${TEMP_FILE} - exit 1 -fi -ASSET_FILE_NAME=$(basename ${TARGET_FILE}) -if test -z "${ASSET_FILE_NAME}"; then - logger -t ${TAG} -p user.error "ASSET_FILE_NAME is empty(or undefined), which should be defined." - rm -f ${TEMP_FILE} - exit 1 -fi -UPLOAD_URL_WITH_QUERY=$(echo ${UPLOAD_URL} | perl -pe "s|{\?name,label}|?name=${ASSET_FILE_NAME}|g") -if test -z "${UPLOAD_URL_WITH_QUERY}"; then - logger -t ${TAG} -p user.error "UPLOAD_URL_WITH_QUERY is empty(or undefined), which should be defined." - rm -f ${TEMP_FILE} - exit 1 -fi -logger -t ${TAG} -p user.debug "TARGET_FILE:${TARGET_FILE} ASSET_FILE_NAME:${ASSET_FILE_NAME} UPLOAD_URL_WITH_QUERY:${UPLOAD_URL_WITH_QUERY}" - -# API SPEC -# https://developer.github.com/v3/repos/releases/#upload-a-release-asset -# Note: curl exits with zero if the jar file already exists. -curl -s --request PATCH -L# --data-binary @"${TARGET_FILE}" -H "Authorization: token ${GITHUB_TOKEN}" -H "Content-Type: application/octet-stream" ${UPLOAD_URL_WITH_QUERY} -RET=${?} -if test "${RET}" != "0"; then - logger -t ${TAG} -p user.error "GitHub API upload-a-release-asset returned error." -else - logger -t ${TAG} -p user.info "GitHub API upload-a-release-asset success" -fi - -POM_FILE="./pom.xml" -if ! test -f "${POM_FILE}"; then - logger -t ${TAG} -p user.error "${POM_FILE} doesn't exist, which should exist." - rm -f ${TEMP_FILE} - exit 1 -fi -POM_ASSET_FILE_NAME="k2hash-${TAG_NAME}.pom" -POM_UPLOAD_URL_WITH_QUERY=$(echo ${UPLOAD_URL} | perl -pe "s|{\?name,label}|?name=${POM_ASSET_FILE_NAME}|g") -curl -s --request PATCH -L# --data-binary @"${POM_FILE}" -H "Authorization: token ${GITHUB_TOKEN}" -H "Content-Type: application/xml" ${POM_UPLOAD_URL_WITH_QUERY} -RET=${?} -if test "${RET}" != "0"; then - logger -t ${TAG} -p user.error "GitHub API upload-a-release-asset returned error." -else - logger -t ${TAG} -p user.info "GitHub API upload-a-release-asset success" -fi - -rm -f ${TEMP_FILE} -exit ${RET} - diff --git a/.gitignore b/.gitignore index 076a451..bb50865 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,58 @@ +# -*- mode: gitignore; -*- +# +# Emacs +# +*~ +\#*\# +/.emacs.desktop +/.emacs.desktop.lock +*.elc +auto-save-list +tramp +.\#* + +# Org-mode +.org-id-locations +*_archive + +# flymake-mode +*_flymake.* + +# eshell files +/eshell/history +/eshell/lastdir + +# elpa packages +/elpa/ + +# reftex files +*.rel + +# AUCTeX auto folder +/auto/ + +# cask packages +.cask/ +dist/ + +# Flycheck +flycheck_*.el + +# server auth directory +/server/ + +# projectiles files +.projectile + +# directory configuration +.dir-locals.el + +# network security +/network-security.data + +# +# Java +# # Compiled class file *.class @@ -13,6 +68,7 @@ # Package Files # *.jar *.war +*.nar *.ear *.zip *.tar.gz @@ -20,4 +76,55 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* -/target/ \ No newline at end of file +replay_pid* + +# +# Maven +# +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +# https://github.com/takari/maven-wrapper#usage-without-binary-jar +.mvn/wrapper/maven-wrapper.jar + +# Eclipse m2e generated files +# Eclipse Core +.project +# JDT-specific (Eclipse Java Development Tools) +.classpath + +# +# MacOS +# +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk diff --git a/ChangeLog b/ChangeLog index 3f10303..8dbd15d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,19 @@ +k2hash-java (1.0.6) stable; urgency=low + + * Bump up + + -- Hirotaka Wakabayashi Thu, 30 Nov 2023 13:57:40 +0900 + +k2hash-java (1.0.5) stable; urgency=low + + * Adds new API to K2hash class + * Fixes internal implementations + * Fixes memory leaks + * Fixes boundary violations + * Uses JDK17 + + -- Hirotaka Wakabayashi Wed, 29 Nov 2023 08:44:13 +0900 + k2hash-java (1.0.4) stable; urgency=low * Adds the release profile for Maven Central diff --git a/README.md b/README.md index f50e8ae..acb30da 100644 --- a/README.md +++ b/README.md @@ -61,3 +61,7 @@ MIT License. See the LICENSE file. [AntPickax](https://antpick.ax/) is - an open source team in [Yahoo Japan Corporation](https://about.yahoo.co.jp/info/en/company/). - a product family of open source software developed by [AntPickax](https://antpick.ax/). + +## Copyright + +Copyright(C) 2023 Yahoo Japan Corporation. diff --git a/checkstyle.xml b/checkstyle.xml index 3452a62..0fe91df 100644 --- a/checkstyle.xml +++ b/checkstyle.xml @@ -4,15 +4,10 @@ "https://www.puppycrawl.com/dtds/configuration_1_3.dtd"> - - - - - - - - + + + diff --git a/example/src/main/java/ax/antpick/k2hash/example/Attr.java b/example/src/main/java/ax/antpick/k2hash/example/Attr.java new file mode 100644 index 0000000..bd1cd5c --- /dev/null +++ b/example/src/main/java/ax/antpick/k2hash/example/Attr.java @@ -0,0 +1,164 @@ +/* + * K2hash Java Driver + * + * Copyright 2023 Yahoo Japan Corporation. + * + * K2hash is a key-value store base library. + * K2hash Java Driver is the one for Java. + * + * See the LICENSE file with this source code for + * the details of the MIT License. + * + * AUTHOR: Hirotaka Wakabayashi + * CREATE: Fri, 30 Sep 2023 + * REVISION: + * + */ +package ax.antpick.k2hash.example; + +import com.sun.jna.*; +import com.sun.jna.ptr.*; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +// +// javac -cp \ +// "logback-classic-1.3.11.jar:logback-core-1.3.11.jar:slf4j-api-2.0.7.jar:jna-5.13.0.jar:jna-platform-5.13.0.jar" \ +// ax/antpick/k2hash/example/Attr.java +// + +public class Attr { + static final Logger logger = LoggerFactory.getLogger(Attr.class); + public static final int DEFAULT_MASK_BITS = 8; + public static final int DEFAULT_CMASK_BITS = 4; + public static final int DEFAULT_MAX_ELEMENT = 32; + public static final int DEFAULT_PAGE_SIZE = 4096; + + public static class K2hashAttrPack extends Structure { + /** Fields of K2hashAttrPack. */ + public static final List FIELDS = + createFieldsOrder("pkey", "keylength", "pval", "vallength"); + + /** An attribute key. */ + public String pkey; + // public byte[] pkey; + /** The length of an attribute key. */ + public int keylength; + /** An attribute value. */ + public String pval; + // public byte[] pval; + /** The length of an attribute value. */ + public int vallength; + + /** Constructor of a K2hash attribute. */ + public K2hashAttrPack() {} + + /** + * Returns fields in the same order with the K2hash C API K2hashAttrPack structure. + * + * @return fields + */ + @Override + protected List getFieldOrder() { + return FIELDS; + } + } + + public interface CLibrary extends Library { + // prototypes + void free(Pointer ptr); + } + + public interface K2hashLibrary extends Library { + // prototypes + long k2h_open_mem(int maskbitcnt, int cmaskbitcnt, int maxelementcnt, long pagesize); + + boolean k2h_close(long handle); + + boolean k2h_set_str_value_wa( + long handle, String pkey, String pval, String pass, LongByReference expirationDuration); + + Pointer k2h_get_str_direct_value_wp(long handle, String pkey, String pass); + + boolean k2h_add_str_attr(long handle, String pkey, String attrName, String attrVal); + + // extern PK2HATTRPCK k2h_get_str_direct_attrs(k2h_h handle, const char* pkey, int* + // pattrspckcnt); + K2hashAttrPack k2h_get_str_direct_attrs(long handle, String pkey, IntByReference pattrspckcnt); + + // extern bool k2h_free_attrpack(PK2HATTRPCK pattrs, int attrcnt); + boolean k2h_free_attrpack(K2hashAttrPack pattrs, int attrcnt); + } + + public static class time_t extends IntegerType { + public time_t(long value) { + super(Native.LONG_SIZE, value); + } + } + + public static void main(String[] args) { + System.setProperty("jna.dump_memory", "true"); + CLibrary C_INSTANCE = (CLibrary) Native.synchronizedLibrary(Native.load("c", CLibrary.class)); + K2hashLibrary INSTANCE = + (K2hashLibrary) Native.synchronizedLibrary(Native.load("k2hash", K2hashLibrary.class)); + long handle = + INSTANCE.k2h_open_mem( + DEFAULT_MASK_BITS, DEFAULT_CMASK_BITS, DEFAULT_MAX_ELEMENT, DEFAULT_PAGE_SIZE); + INSTANCE.k2h_set_str_value_wa(handle, "testkey", "testval", "", null); + Pointer val = INSTANCE.k2h_get_str_direct_value_wp(handle, "testkey", ""); + logger.debug("val {}", val.getString(0)); + C_INSTANCE.free(val); + + String attrName = "testkey_attr"; + String attrVal = "testkey_attr_val"; + boolean isSuccess = INSTANCE.k2h_add_str_attr(handle, "testkey", attrName, attrVal); + if (!isSuccess) { + logger.error("INSTANCE.k2h_add_attr returns false"); + } + + isSuccess = INSTANCE.k2h_add_str_attr(handle, "testkey", "testkey_attr2", "testkey_attr2_val"); + if (!isSuccess) { + logger.error("INSTANCE.k2h_add_attr returns false"); + } + + IntByReference iref = new IntByReference(); + K2hashAttrPack pp = INSTANCE.k2h_get_str_direct_attrs(handle, "testkey", iref); + pp.setAutoRead(false); + logger.debug("pp {}", pp); + + if (pp == null || iref.getValue() == 0) { + logger.warn("no attribute exists"); + return; + } + K2hashAttrPack[] attrs = (K2hashAttrPack[]) pp.toArray(iref.getValue()); + logger.debug("iref.getValue() {}", iref.getValue()); + + Map map = new HashMap<>(); + Stream stream = Stream.of(attrs); + stream.forEach( + attr -> { + logger.debug("{} {} {} {}", attr.pkey, attr.keylength, attr.pval, attr.vallength); + logger.debug("attr.toString() {} ", attr.toString()); + logger.debug("attr.getPointer() {} ", attr.getPointer()); + logger.debug("attr.pkey.toString() {} ", attr.pkey); + logger.debug("attr.pval.toString() {} ", attr.pval); + map.put(attr.pkey, attr.pval); + }); + logger.debug("K2hashAttrPack address={} size={}", pp.toString(), pp.size()); + + // INSTANCE.k2h_free_attrpack(pp, iref.getValue()); + INSTANCE.k2h_close(handle); + } +} +// +// Local variables: +// tab-width: 2 +// c-basic-offset: 2 +// End: +// vim600: noexpandtab sw=2 ts=2 fdm=marker +// vim<600: noexpandtab sw=2 ts=2 +// diff --git a/k2hash/pom.xml b/k2hash/pom.xml new file mode 100644 index 0000000..8911ad5 --- /dev/null +++ b/k2hash/pom.xml @@ -0,0 +1,150 @@ + + + 4.0.0 + + ax.antpick + k2hash-parent + 1.0.6 + + + k2hash + jar + 1.0.6 + + k2hash library + An official java driver for k2hash, which is a highly available and scalable distributed KVS library. + https://github.com/yahoojapan/k2hash_java + + + scm:git:git://github.com:yahoojapan/k2hash_java.git + scm:git:ssh://github.com:yahoojapan/k2hash_java.git + https://github.com/yahoojapan/k2hash_java + + + + + ossrh + Central Repository OSSRH + https://oss.sonatype.org/service/local/staging/deploy/maven2/ + + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + ${maven-javadoc-plugin.version} + + + org.apache.maven.plugins + maven-project-info-reports-plugin + ${maven-project-info-reports-plugin.version} + + + + + + + + org.apache.maven.plugins + maven-checkstyle-plugin + ${maven-checkstyle-plugin.version} + + + ${project.parent.basedir}/${checkstyle.configLocation} + ${project.parent.basedir}/${checkstyle.suppressionsLocation} + + + + + + + + upload + + + assets + + + + + + org.codehaus.mojo + exec-maven-plugin + ${exec-maven-plugin.version} + + + upload_jar + + exec + + + + + .github/workflows/upload.sh + + + + + + + + release + + + + org.apache.maven.plugins + maven-source-plugin + ${maven-source-plugin.version} + + + attach-sources + + jar-no-fork + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + ${maven-javadoc-plugin.version} + + + attach-javadocs + + jar + + + + + + org.apache.maven.plugins + maven-gpg-plugin + ${maven-gpg-plugin.version} + + + sign-artifacts + verify + + sign + + + + + --pinentry-mode + loopback + + + + + + + + + + diff --git a/src/main/java/ax/antpick/k2hash/CLibrary.java b/k2hash/src/main/java/CLibrary.java similarity index 83% rename from src/main/java/ax/antpick/k2hash/CLibrary.java rename to k2hash/src/main/java/CLibrary.java index 693d1c4..d2a7cf9 100644 --- a/src/main/java/ax/antpick/k2hash/CLibrary.java +++ b/k2hash/src/main/java/CLibrary.java @@ -28,14 +28,14 @@ */ package ax.antpick.k2hash; -import com.sun.jna.*; -import com.sun.jna.ptr.*; +import com.sun.jna.Library; +import com.sun.jna.Pointer; /** * This JNA interface provides functions in the C library. * *

Usage Examples. Suppose you want to set the K2HDBGMODE enviroment "INFO" and print it, - * You could write this as: + * you could write this as: * *

{@code
  * package ax.antpick;
@@ -46,7 +46,8 @@
  * public class App {
  *   public static void main(String[] args) {
  *     CLibrary INSTANCE =
- *         (CLibrary) Native.synchronizedLibrary(Native.loadLibrary("c", CLibrary.class));
+ *         (CLibrary) Native.synchronizedLibrary(
+ *             Native.load("c", CLibrary.class));
  *     INSTANCE.setenv("K2HDBGMODE", "INFO", 1);
  *     System.out.println(INSTANCE.getenv("K2HDBGMODE"));
  *   }
@@ -55,7 +56,7 @@
  */
 public interface CLibrary extends Library {
   /**
-   * Frees the memory space pointed to by ptr
+   * Frees the memory space pointed to by ptr.
    *
    * @param ptr a pointer to be free
    */
@@ -63,13 +64,13 @@ public interface CLibrary extends Library {
   void free(Pointer ptr);
 
   /**
-   * Adds an enviroments
+   * Adds an enviroments.
    *
    * @param name an environment string
    * @param value the value
    * @param overwrite if name already exists in the environment, then its value
-   *      is changed to value if overwrite is nonzero. If 
-   *     overwrite is zero, then the value of name is not changed.
+   *      is changed to value i overwrite is nonzero. If 
+   *      overwrite i zero, then the value of name is not changed.
    * @return returns zero on success, or -1 on error, with errno set to indicate the cause of the
    *     error.
    */
@@ -97,7 +98,11 @@ public interface CLibrary extends Library {
 }
 
 //
-// VIM modelines
-//
-// vim:set ts=4 fenc=utf-8:
+// Local variables:
+// tab-width: 2
+// c-basic-offset: 2
+// indent-tabs-mode: nil
+// End:
+// vim600: noexpandtab sw=2 ts=2 fdm=marker
+// vim<600: noexpandtab sw=2 ts=2
 //
diff --git a/src/main/java/ax/antpick/k2hash/K2hash.java b/k2hash/src/main/java/K2hash.java
similarity index 86%
rename from src/main/java/ax/antpick/k2hash/K2hash.java
rename to k2hash/src/main/java/K2hash.java
index e81113b..c7b7c66 100644
--- a/src/main/java/ax/antpick/k2hash/K2hash.java
+++ b/k2hash/src/main/java/K2hash.java
@@ -28,8 +28,14 @@
  */
 package ax.antpick.k2hash;
 
-import com.sun.jna.*;
-import com.sun.jna.ptr.*;
+import com.sun.jna.IntegerType;
+import com.sun.jna.Memory;
+import com.sun.jna.Native;
+import com.sun.jna.Pointer;
+import com.sun.jna.StringArray;
+import com.sun.jna.ptr.IntByReference;
+import com.sun.jna.ptr.LongByReference;
+import com.sun.jna.ptr.PointerByReference;
 import java.io.Closeable;
 import java.io.File;
 import java.io.IOException;
@@ -42,7 +48,7 @@
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.TimeUnit;
-import java.util.stream.*;
+import java.util.stream.Stream;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -71,7 +77,7 @@
  * }
  * }
*/ -public class K2hash implements Closeable { +public final class K2hash implements Closeable { /* -- private Static members -- */ /** A logger instance. */ @@ -130,10 +136,7 @@ public enum STATS_DUMP_LEVEL { }; /* -- private instance members -- */ - /** The file mapped to k2hash data */ - private final Path path; - - /** K2hash data handle */ + /** K2hash data handle. */ private long handle = K2H_INVALID_HANDLE; /** @@ -169,7 +172,7 @@ public String toString() { * @exception IllegalArgumentException If {@code val} is null * @throws IllegalArgumentException If {@code val} is negative or zero */ - static void isStringNotNull(String val, String varName) { + static void isStringNotNull(final String val, final String varName) { if (val == null) { throw new IllegalArgumentException(varName + "is null"); } @@ -183,7 +186,7 @@ static void isStringNotNull(String val, String varName) { * @exception IllegalArgumentException If {@code val} is null * @throws IllegalArgumentException If {@code val} is negative or zero */ - static void isStringNotEmpty(String val, String varName) { + static void isStringNotEmpty(final String val, final String varName) { if (val == null) { throw new IllegalArgumentException(varName + "is null"); } @@ -202,7 +205,7 @@ static void isStringNotEmpty(String val, String varName) { * @exception IllegalArgumentException If {@code val} is null * @exception IllegalArgumentException If {@code val} is negative or zero */ - static void isStringArrayNotEmpty(String[] array, String varName) { + static void isStringArrayNotEmpty(final String[] array, final String varName) { if (array == null || array.length == 0) { throw new IllegalArgumentException(varName + "is null"); } @@ -218,7 +221,7 @@ static void isStringArrayNotEmpty(String[] array, String varName) { * @exception IllegalArgumentException If {@code val} is null * @exception IllegalArgumentException If {@code val} is negative or zero */ - static void isByteArrayNotEmpty(byte[] array, String varName) { + static void isByteArrayNotEmpty(final byte[] array, final String varName) { if (array == null || array.length == 0) { throw new IllegalArgumentException(varName + "is null"); } @@ -231,7 +234,7 @@ static void isByteArrayNotEmpty(byte[] array, String varName) { * @param varName variable name used for the error message * @throws IllegalArgumentException If {@code val} is negative or zero */ - static void isPositive(int val, String varName) { + static void isPositive(final int val, final String varName) { if (val < 0) { throw new IllegalArgumentException(varName + " is less than equal zero"); } @@ -244,7 +247,7 @@ static void isPositive(int val, String varName) { * @param varName variable name used for the error message * @throws IllegalArgumentException If {@code val} is negative or zero */ - static void isPositive(long val, String varName) { + static void isPositive(final long val, final String varName) { if (val < 0) { throw new IllegalArgumentException(varName + "is less than equal zero"); } @@ -260,17 +263,16 @@ static void isPositive(long val, String varName) { * @throws IllegalArgumentException if pathname is null * @throws IOException if a k2hash file is unavailable */ - private K2hash(Path pathname, OPEN_MODE mode) throws IOException { + private K2hash(final Path pathname, final OPEN_MODE mode) throws IOException { if (pathname == null || pathname.toString().isEmpty()) { throw new IllegalArgumentException("pathname is null"); } logger.debug("try to open {} with {}", pathname, mode.name()); - this.path = pathname; switch (mode) { case DEFAULT: this.handle = INSTANCE.k2h_open( - this.path.toString(), + pathname.toString(), DEFAULT_IS_READONLY, DEFAULT_IS_REMOVE_FILE, DEFAULT_IS_FULLMAP, @@ -282,7 +284,7 @@ private K2hash(Path pathname, OPEN_MODE mode) throws IOException { case RDWR: this.handle = INSTANCE.k2h_open_rw( - this.path.toString(), + pathname.toString(), DEFAULT_IS_FULLMAP, DEFAULT_MASK_BITS, DEFAULT_CMASK_BITS, @@ -292,7 +294,7 @@ private K2hash(Path pathname, OPEN_MODE mode) throws IOException { case RDONLY: this.handle = INSTANCE.k2h_open_ro( - this.path.toString(), + pathname.toString(), DEFAULT_IS_FULLMAP, DEFAULT_MASK_BITS, DEFAULT_CMASK_BITS, @@ -302,7 +304,7 @@ private K2hash(Path pathname, OPEN_MODE mode) throws IOException { case TMPFILE: this.handle = INSTANCE.k2h_open_rw( - this.path.toString(), + pathname.toString(), DEFAULT_IS_FULLMAP, DEFAULT_MASK_BITS, DEFAULT_CMASK_BITS, @@ -314,12 +316,34 @@ private K2hash(Path pathname, OPEN_MODE mode) throws IOException { INSTANCE.k2h_open_mem( DEFAULT_MASK_BITS, DEFAULT_CMASK_BITS, DEFAULT_MAX_ELEMENT, DEFAULT_PAGE_SIZE); break; + default: + break; } if (this.handle <= K2H_INVALID_HANDLE) { throw new IOException("k2hash.open failed"); } } + /** + * Creates a K2hash instance using k2h_open_mem. + * + * @param mode file open mode + * @throws IllegalArgumentException when mode is invalid + * @throws IOException if a k2hash file is unavailable + */ + private K2hash(final K2hash.OPEN_MODE mode) throws IOException { + if (mode == K2hash.OPEN_MODE.MEM) { + this.handle = + INSTANCE.k2h_open_mem( + DEFAULT_MASK_BITS, DEFAULT_CMASK_BITS, DEFAULT_MAX_ELEMENT, DEFAULT_PAGE_SIZE); + if (this.handle <= K2H_INVALID_HANDLE) { + throw new IOException("k2hash.open failed"); + } + } else { + throw new IllegalArgumentException("only MEM mode is currently supported"); + } + } + /* -- public Static methods -- */ /** * Creates a K2hashLibrary instance. @@ -330,8 +354,7 @@ private K2hash(Path pathname, OPEN_MODE mode) throws IOException { public static synchronized K2hashLibrary getLibrary() throws IOException { if (INSTANCE == null) { INSTANCE = - (K2hashLibrary) - Native.synchronizedLibrary(Native.loadLibrary("k2hash", K2hashLibrary.class)); + (K2hashLibrary) Native.synchronizedLibrary(Native.load("k2hash", K2hashLibrary.class)); } if (INSTANCE == null) { throw new IOException("loading shared library error"); @@ -347,7 +370,7 @@ public static synchronized K2hashLibrary getLibrary() throws IOException { */ public static synchronized CLibrary getCLibrary() throws IOException { if (C_INSTANCE == null) { - C_INSTANCE = (CLibrary) Native.synchronizedLibrary(Native.loadLibrary("c", CLibrary.class)); + C_INSTANCE = (CLibrary) Native.synchronizedLibrary(Native.load("c", CLibrary.class)); } if (C_INSTANCE == null) { throw new IOException("loading shared library error"); @@ -363,19 +386,33 @@ public static synchronized CLibrary getCLibrary() throws IOException { * @throws IllegalArgumentException if pathname is null * @throws IOException if creating a k2hash file failed */ - public static synchronized K2hash of(String pathname) throws IOException { + public static synchronized K2hash of(final String pathname) throws IOException { if (pathname == null) { throw new IllegalArgumentException(); } Path path = fs.getPath(pathname).toAbsolutePath(); if (INSTANCE == null) { INSTANCE = - (K2hashLibrary) - Native.synchronizedLibrary(Native.loadLibrary("k2hash", K2hashLibrary.class)); + (K2hashLibrary) Native.synchronizedLibrary(Native.load("k2hash", K2hashLibrary.class)); } return new K2hash(path, K2hash.OPEN_MODE.DEFAULT); } + /** + * Creates a k2hash instance. + * + * @param mode an access mode to open a file + * @return a K2hash instance + * @throws IOException if creating a k2hash file failed + */ + public static synchronized K2hash of(final K2hash.OPEN_MODE mode) throws IOException { + if (INSTANCE == null) { + INSTANCE = + (K2hashLibrary) Native.synchronizedLibrary(Native.load("k2hash", K2hashLibrary.class)); + } + return new K2hash(mode); + } + /** * Creates a k2hash instance. * @@ -385,15 +422,15 @@ public static synchronized K2hash of(String pathname) throws IOException { * @throws IllegalArgumentException if pathname is null * @throws IOException if creating a k2hash file failed */ - public static synchronized K2hash of(String pathname, K2hash.OPEN_MODE mode) throws IOException { + public static synchronized K2hash of(final String pathname, final K2hash.OPEN_MODE mode) + throws IOException { if (pathname == null) { throw new IllegalArgumentException("pathname is null"); } Path path = fs.getPath(pathname).toAbsolutePath(); if (INSTANCE == null) { INSTANCE = - (K2hashLibrary) - Native.synchronizedLibrary(Native.loadLibrary("k2hash", K2hashLibrary.class)); + (K2hashLibrary) Native.synchronizedLibrary(Native.load("k2hash", K2hashLibrary.class)); } return new K2hash(path, mode); } @@ -405,7 +442,7 @@ public static synchronized K2hash of(String pathname, K2hash.OPEN_MODE mode) thr * @return true if success false otherwise * @throws IllegalArgumentException if an illegal augment exists */ - public static boolean create(String pathname) { + public static boolean create(final String pathname) { try { isStringNotEmpty(pathname, "pathname"); } catch (IllegalArgumentException ex) { @@ -428,7 +465,11 @@ public static boolean create(String pathname) { * @throws IllegalArgumentException if an illegal augment exists */ public static boolean create( - String pathname, int maskBit, int cmaskBit, int maxElement, int pageSize) { + final String pathname, + final int maskBit, + final int cmaskBit, + final int maxElement, + final int pageSize) { try { isStringNotEmpty(pathname, "pathname"); isPositive(maskBit, "maskBit"); @@ -474,7 +515,7 @@ public void close() throws IOException { * @return true if success false otherwise * @throws IllegalArgumentException if an illegal augment exists */ - public boolean beginTx(String txFile) { + public boolean beginTx(final String txFile) { assert (this.handle > K2H_INVALID_HANDLE); try { isStringNotEmpty(txFile, "txFile"); @@ -482,7 +523,19 @@ public boolean beginTx(String txFile) { logger.error(ex.getMessage()); /* we catch the first exception only */ throw ex; } - return this.beginTx(txFile, null, null, 0); + return this.beginTx(txFile, null, null, 0L); + } + + /** The class {@code time_t} encapsulates the C.time_t structure. */ + public final class time_t extends IntegerType { + /** + * Constructs a time_t instance. + * + * @param value an integer(32 bit) value holding the number of seconds 1970-01-01T00:00:00 UTC + */ + public time_t(final long value) { + super(Native.LONG_SIZE, value); + } } /** @@ -495,7 +548,8 @@ public boolean beginTx(String txFile) { * @return true if success false otherwise * @throws IllegalArgumentException if an illegal augment exists */ - public boolean beginTx(String txFile, String prefix, String param, long expirationDuration) { + public boolean beginTx( + final String txFile, final String prefix, final String param, final long expirationDuration) { assert (this.handle > K2H_INVALID_HANDLE); try { isStringNotEmpty(txFile, "txFile"); @@ -504,7 +558,11 @@ public boolean beginTx(String txFile, String prefix, String param, long expirati logger.error(ex.getMessage()); /* we catch the first exception only */ throw ex; } - LongByReference lref = new LongByReference(expirationDuration); + time_t timeExpirationDuration = new time_t(expirationDuration); + Pointer ptr = new Memory(Long.SIZE); + int offset = 0; + ptr.setLong(offset, timeExpirationDuration.longValue()); // java.lang.Long.longValue() + boolean isSuccess = INSTANCE.k2h_transaction_param_we( this.handle, @@ -514,7 +572,7 @@ public boolean beginTx(String txFile, String prefix, String param, long expirati (prefix != null) ? prefix.length() : 0, param, (param != null) ? param.length() : 0, - (expirationDuration != 0) ? lref : (LongByReference) null); + (expirationDuration != 0) ? ptr : (Pointer) null); if (!isSuccess) { logger.error("k2h_transaction_param_we returns false"); } @@ -563,7 +621,7 @@ public int getTxPoolSize() { * @return true if success false otherwise * @throws IllegalArgumentException if an illegal augment exists */ - public boolean setTxPoolSize(int txPoolSize) { + public boolean setTxPoolSize(final int txPoolSize) { assert (this.handle > K2H_INVALID_HANDLE); try { isPositive(txPoolSize, "txPoolSize"); @@ -585,7 +643,7 @@ public boolean setTxPoolSize(int txPoolSize) { * @return true if success false otherwise * @throws IllegalArgumentException if an illegal augment exists */ - public boolean dumpToFile(String path) { + public boolean dumpToFile(final String path) { assert (this.handle > K2H_INVALID_HANDLE); try { isStringNotEmpty(path, "path"); @@ -604,7 +662,7 @@ public boolean dumpToFile(String path) { * @return true if success false otherwise * @throws IllegalArgumentException if an illegal augment exists */ - public boolean dumpToFile(String path, boolean isSkipError) { + public boolean dumpToFile(final String path, final boolean isSkipError) { assert (this.handle > K2H_INVALID_HANDLE); try { isStringNotEmpty(path, "path"); @@ -627,7 +685,7 @@ public boolean dumpToFile(String path, boolean isSkipError) { * @throws IllegalArgumentException if an illegal augment exists * @throws IOException if a path doesn't exist */ - public boolean loadFromFile(String path) throws IOException { + public boolean loadFromFile(final String path) throws IOException { assert (this.handle > K2H_INVALID_HANDLE); try { isStringNotEmpty(path, "path"); @@ -647,7 +705,7 @@ public boolean loadFromFile(String path) throws IOException { * @throws IllegalArgumentException if an illegal augment exists * @throws IOException if a path doesn't exist */ - public boolean loadFromFile(String path, boolean isSkipError) throws IOException { + public boolean loadFromFile(final String path, final boolean isSkipError) throws IOException { assert (this.handle > K2H_INVALID_HANDLE); try { isStringNotEmpty(path, "path"); @@ -676,7 +734,7 @@ public boolean loadFromFile(String path, boolean isSkipError) throws IOException * K2H_ATTR_DEFAULT * @return true if success false otherwise */ - public boolean enableMtime(boolean enable) { + public boolean enableMtime(final boolean enable) { assert (this.handle > K2H_INVALID_HANDLE); IntByReference enableMtime = new IntByReference(); if (enable) { @@ -699,7 +757,7 @@ public boolean enableMtime(boolean enable) { * K2H_ATTR_DEFAULT * @return true if success false otherwise */ - public boolean enableEncryption(boolean enable) { + public boolean enableEncryption(final boolean enable) { assert (this.handle > K2H_INVALID_HANDLE); IntByReference enableEncryption = new IntByReference(); if (enable) { @@ -724,7 +782,7 @@ public boolean enableEncryption(boolean enable) { * @throws IllegalArgumentException if an illegal augment exists * @throws IOException if a path doesn't exist */ - public boolean setEncryptionPasswordFile(String path) throws IOException { + public boolean setEncryptionPasswordFile(final String path) throws IOException { assert (this.handle > K2H_INVALID_HANDLE); try { isStringNotEmpty(path, "path"); @@ -752,7 +810,7 @@ public boolean setEncryptionPasswordFile(String path) throws IOException { * K2H_ATTR_DEFAULT * @return true if success false otherwise */ - public boolean enableHistory(boolean enable) { + public boolean enableHistory(final boolean enable) { assert (this.handle > K2H_INVALID_HANDLE); IntByReference enableHistory = new IntByReference(); if (enable) { @@ -776,7 +834,7 @@ public boolean enableHistory(boolean enable) { * @return true if success false otherwise * @throws IllegalArgumentException if an illegal augment exists */ - public boolean setExpirationDuration(int duration, TimeUnit unit) { + public boolean setExpirationDuration(final int duration, final TimeUnit unit) { assert (this.handle > K2H_INVALID_HANDLE); try { isPositive(duration, "expirationDuration"); @@ -839,7 +897,7 @@ public boolean setExpirationDuration(int duration, TimeUnit unit) { * @return true if success false otherwise * @throws IllegalArgumentException if an illegal augment exists */ - public boolean addDecryptionPassword(String password) { + public boolean addDecryptionPassword(final String password) { assert (this.handle > K2H_INVALID_HANDLE); try { isStringNotEmpty(password, "password"); @@ -861,7 +919,7 @@ public boolean addDecryptionPassword(String password) { * @return true if success false otherwise * @throws IllegalArgumentException if an illegal augment exists */ - public boolean setDefaultEncryptionPassword(String password) { + public boolean setDefaultEncryptionPassword(final String password) { assert (this.handle > K2H_INVALID_HANDLE); try { isStringNotEmpty(password, "password"); @@ -884,7 +942,7 @@ public boolean setDefaultEncryptionPassword(String password) { * @throws IllegalArgumentException if an illegal augment exists * @throws IOException if a path does't exist */ - public boolean addAttributePluginLib(String path) throws IOException { + public boolean addAttributePluginLib(final String path) throws IOException { assert (this.handle > K2H_INVALID_HANDLE); try { isStringNotEmpty(path, "path"); @@ -923,7 +981,7 @@ public void printAttributes() { * @return the value of the key * @throws IllegalArgumentException if an illegal augment exists */ - public String getValue(String key) { + public String getValue(final String key) { assert (this.handle > K2H_INVALID_HANDLE); try { isStringNotEmpty(key, "key"); @@ -945,7 +1003,7 @@ public String getValue(String key) { * @return value of the key * @throws IllegalArgumentException if an illegal augment exists */ - public String getValue(String key, String pass) { + public String getValue(final String key, final String pass) { assert (this.handle > K2H_INVALID_HANDLE); try { isStringNotEmpty(key, "key"); @@ -954,9 +1012,17 @@ public String getValue(String key, String pass) { throw ex; } // TODO use k2h_get_value_wp instead of k2h_get_str_direct_value_wp because of performance. - String rval = INSTANCE.k2h_get_str_direct_value_wp(this.handle, key, pass); - if (rval == null) { + Pointer ptr = INSTANCE.k2h_get_str_direct_value_wp(this.handle, key, pass); + if (ptr == null) { logger.warn("k2h_get_str_direct_value_wp returns null"); + return null; + } + String rval = ptr.getString(0); + try { + getCLibrary().free(ptr); + } catch (IOException ex) { + logger.error(ex.getMessage()); /* we catch the first exception only */ + return null; } return rval; } @@ -974,7 +1040,7 @@ public String getValue(String key, String pass) { * @return value of the key * @throws IllegalArgumentException if an illegal augment exists */ - public byte[] getValue(byte[] key, String pass) { + public byte[] getValue(final byte[] key, final String pass) { assert (this.handle > K2H_INVALID_HANDLE); try { isByteArrayNotEmpty(key, "key"); @@ -1006,7 +1072,7 @@ public byte[] getValue(byte[] key, String pass) { * @return list of subkeys of the key. null if no subkeys exist. * @throws IllegalArgumentException if an illegal augment exists */ - public List getSubkeys(String key) { + public List getSubkeys(final String key) { assert (this.handle > K2H_INVALID_HANDLE); try { isStringNotEmpty(key, "key"); @@ -1015,16 +1081,16 @@ public List getSubkeys(String key) { throw ex; } - PointerByReference ppskeypck = INSTANCE.k2h_get_str_direct_subkeys(this.handle, key); - if (ppskeypck != null) { - Pointer ptr = ppskeypck.getPointer(); + PointerByReference pskeyarray = INSTANCE.k2h_get_str_direct_subkeys(this.handle, key); + if (pskeyarray != null) { + Pointer ptr = pskeyarray.getPointer(); if (ptr != null) { String[] array = ptr.getStringArray(0); if (array.length > 0) { // we need a copy and use it before we free the original value. String[] newArray = Arrays.copyOfRange(array, 0, array.length); List list = Arrays.asList(newArray); - boolean isSuccess = INSTANCE.k2h_free_keyarray(array); + boolean isSuccess = INSTANCE.k2h_free_keyarray(pskeyarray); if (!isSuccess) { logger.error("INSTANCE.free_keyarray error"); } @@ -1049,7 +1115,7 @@ public List getSubkeys(String key) { * @return true if succeeded. false otherwise. * @throws IllegalArgumentException if an illegal augment exists */ - public boolean setSubkey(String key, String subkey) { + public boolean setSubkey(final String key, final String subkey) { assert (this.handle > K2H_INVALID_HANDLE); try { isStringNotEmpty(key, "key"); @@ -1071,7 +1137,7 @@ public boolean setSubkey(String key, String subkey) { * @return true if succeeded. false otherwise. * @throws IllegalArgumentException if an illegal augment exists */ - public boolean setSubkeys(String key, String[] subkeys) { + public boolean setSubkeys(final String key, final String[] subkeys) { assert (this.handle > K2H_INVALID_HANDLE); try { isStringNotEmpty(key, "key"); @@ -1098,7 +1164,7 @@ public boolean setSubkeys(String key, String[] subkeys) { * @return true if succeeded. false otherwise. * @throws IllegalArgumentException if an illegal augment exists */ - public boolean addSubkey(String key, String subkey) { + public boolean addSubkey(final String key, final String subkey) { assert (this.handle > K2H_INVALID_HANDLE); try { isStringNotEmpty(key, "key"); @@ -1131,12 +1197,12 @@ public boolean addSubkeys(String key, String[] newKeys) { } // 1. get current subkeys - PointerByReference ppskeypck; - ppskeypck = INSTANCE.k2h_get_str_direct_subkeys(this.handle, key); + PointerByReference pskeyarray; + pskeyarray = INSTANCE.k2h_get_str_direct_subkeys(this.handle, key); String[] currentKeys = null; - if (ppskeypck != null) { - Pointer ptr = ppskeypck.getPointer(); + if (pskeyarray != null) { + Pointer ptr = pskeyarray.getPointer(); if (ptr != null) { currentKeys = ptr.getStringArray(0); if (currentKeys.length == 0) { @@ -1157,8 +1223,7 @@ public boolean addSubkeys(String key, String[] newKeys) { String[] allKeys = new String[currentKeys.length + newKeys.length]; System.arraycopy(currentKeys, 0, allKeys, 0, currentKeys.length); System.arraycopy(newKeys, 0, allKeys, currentKeys.length, newKeys.length); - INSTANCE.k2h_free_keyarray(currentKeys); - INSTANCE.k2h_free_keyarray(newKeys); + INSTANCE.k2h_free_keyarray(pskeyarray); return setSubkeys(key, allKeys); } @@ -1171,7 +1236,7 @@ public boolean addSubkeys(String key, String[] newKeys) { * @return true if succeeded. false otherwise. * @throws IllegalArgumentException if an illegal augment exists */ - public boolean setAttribute(String key, String attrName, String attrVal) { + public boolean setAttribute(final String key, final String attrName, final String attrVal) { assert (this.handle > K2H_INVALID_HANDLE); try { isStringNotEmpty(key, "key"); @@ -1195,7 +1260,7 @@ public boolean setAttribute(String key, String attrName, String attrVal) { * @return attributes of the key * @throws IllegalArgumentException if an illegal augment exists */ - public Map getAttributes(String key) { + public Map getAttributes(final String key) { assert (this.handle > K2H_INVALID_HANDLE); try { isStringNotEmpty(key, "key"); @@ -1205,22 +1270,25 @@ public Map getAttributes(String key) { } IntByReference iref = new IntByReference(); - K2hashAttrPack pp = INSTANCE.k2h_get_str_direct_attrs(this.handle, key, iref); + K2hashAttrPack pp = INSTANCE.k2h_get_str_direct_attrs(handle, key, iref); if (pp == null || iref.getValue() == 0) { logger.warn("no attribute exists"); return null; + } else { + pp.setAutoRead(false); + logger.warn("{} attribute exists", iref.getValue()); } + K2hashAttrPack[] attrs = (K2hashAttrPack[]) pp.toArray(iref.getValue()); - // Note: We must copy data because attrs must be free later. - K2hashAttrPack[] newAttrs = Arrays.copyOfRange(attrs, 0, attrs.length); + logger.debug("iref.getValue() {}", iref.getValue()); Map map = new HashMap<>(); - Stream stream = Stream.of(newAttrs); + Stream stream = Stream.of(attrs); stream.forEach( attr -> { map.put(attr.pkey, attr.pval); }); - INSTANCE.k2h_free_attrpack(attrs, iref.getValue()); + INSTANCE.k2h_free_attrpack(pp, iref.getValue()); INSTANCE.k2h_close(handle); return map; } @@ -1233,7 +1301,7 @@ public Map getAttributes(String key) { * @return the value of the attribute name of the key * @throws IllegalArgumentException if an illegal augment exists */ - public String getAttribute(String key, String attribute) { + public String getAttribute(final String key, final String attribute) { assert (this.handle > K2H_INVALID_HANDLE); try { isStringNotEmpty(key, "key"); @@ -1262,7 +1330,7 @@ public String getAttribute(String key, String attribute) { * @return true if succeeded. false otherwise. * @throws IllegalArgumentException if an illegal augment exists */ - public boolean setValue(String key, String val) { + public boolean setValue(final String key, final String val) { assert (this.handle > K2H_INVALID_HANDLE); try { isStringNotEmpty(key, "key"); @@ -1284,7 +1352,12 @@ public boolean setValue(String key, String val) { * @return true if succeeded. false otherwise. * @throws IllegalArgumentException if an illegal augment exists */ - public boolean setValue(String key, String val, String password, long duration, TimeUnit unit) { + public boolean setValue( + final String key, + final String val, + final String password, + final long duration, + final TimeUnit unit) { assert (this.handle > K2H_INVALID_HANDLE); try { isStringNotEmpty(key, "key"); @@ -1342,7 +1415,7 @@ public boolean setValue(String key, String val, String password, long duration, * @return true if success false otherwise * @throws IllegalArgumentException if an illegal augment exists */ - public boolean remove(String key) { + public boolean remove(final String key) { assert (this.handle > K2H_INVALID_HANDLE); try { isStringNotEmpty(key, "key"); @@ -1365,7 +1438,7 @@ public boolean remove(String key) { * @return true if success false otherwise * @throws IllegalArgumentException if an illegal augment exists */ - public boolean remove(String key, boolean removeAllSubkeys) { + public boolean remove(final String key, final boolean removeAllSubkeys) { assert (this.handle > K2H_INVALID_HANDLE); try { isStringNotEmpty(key, "key"); @@ -1398,7 +1471,7 @@ public boolean remove(String key, boolean removeAllSubkeys) { * @return true if success false otherwise * @throws IllegalArgumentException if an illegal augment exists */ - public boolean remove(String key, String subkey) { + public boolean remove(final String key, final String subkey) { assert (this.handle > K2H_INVALID_HANDLE); try { isStringNotEmpty(key, "key"); @@ -1422,7 +1495,7 @@ public boolean remove(String key, String subkey) { * @return true if success false otherwise * @throws IllegalArgumentException if an illegal augment exists */ - public boolean rename(String key, String newkey) { + public boolean rename(final String key, final String newkey) { assert (this.handle > K2H_INVALID_HANDLE); try { isStringNotEmpty(key, "key"); @@ -1439,7 +1512,7 @@ public boolean rename(String key, String newkey) { * * @param level level of details of dump table information */ - public void printTableStats(STATS_DUMP_LEVEL level) { + public void printTableStats(final STATS_DUMP_LEVEL level) { assert (this.handle > K2H_INVALID_HANDLE); // TODO put stats to the logger file pointer. switch (level) { @@ -1463,6 +1536,8 @@ public void printTableStats(STATS_DUMP_LEVEL level) { // call the k2h_dump_full INSTANCE.k2h_dump_full(this.handle, null); break; + default: + break; } } @@ -1478,3 +1553,12 @@ public void version() { INSTANCE.k2h_print_version(null); } } +// +// Local variables: +// tab-width: 2 +// c-basic-offset: 2 +// indent-tabs-mode: nil +// End: +// vim600: noexpandtab sw=2 ts=2 fdm=marker +// vim<600: noexpandtab sw=2 ts=2 +// diff --git a/src/main/java/ax/antpick/k2hash/K2hashAttrPack.java b/k2hash/src/main/java/K2hashAttrPack.java similarity index 95% rename from src/main/java/ax/antpick/k2hash/K2hashAttrPack.java rename to k2hash/src/main/java/K2hashAttrPack.java index d7dc415..54b2b8d 100644 --- a/src/main/java/ax/antpick/k2hash/K2hashAttrPack.java +++ b/k2hash/src/main/java/K2hashAttrPack.java @@ -28,9 +28,7 @@ */ package ax.antpick.k2hash; -import com.sun.jna.*; -import com.sun.jna.Structure.*; -import com.sun.jna.ptr.*; +import com.sun.jna.Structure; import java.util.List; // @@ -122,4 +120,15 @@ public K2hashAttrPack() {} protected List getFieldOrder() { return FIELDS; } + } + +// +// Local variables: +// tab-width: 2 +// c-basic-offset: 2 +// indent-tabs-mode: nil +// End: +// vim600: noexpandtab sw=2 ts=2 fdm=marker +// vim<600: noexpandtab sw=2 ts=2 +// diff --git a/src/main/java/ax/antpick/k2hash/K2hashKeyQueue.java b/k2hash/src/main/java/K2hashKeyQueue.java similarity index 91% rename from src/main/java/ax/antpick/k2hash/K2hashKeyQueue.java rename to k2hash/src/main/java/K2hashKeyQueue.java index ee8cd5b..0c6489b 100644 --- a/src/main/java/ax/antpick/k2hash/K2hashKeyQueue.java +++ b/k2hash/src/main/java/K2hashKeyQueue.java @@ -28,8 +28,10 @@ */ package ax.antpick.k2hash; -import com.sun.jna.*; -import com.sun.jna.ptr.*; +import com.sun.jna.Pointer; +import com.sun.jna.ptr.IntByReference; +import com.sun.jna.ptr.LongByReference; +import com.sun.jna.ptr.PointerByReference; import java.io.Closeable; import java.io.IOException; import java.lang.reflect.Field; @@ -66,7 +68,11 @@ * public class App { * public static void main(String[] args) { * // 1. setvalues - * HashMap data = new HashMap() {{ put("k1","v1"); put("k2","v2"); put("k3","v3");}}; + * HashMap data = new HashMap() { + * put("k1","v1"); + * put("k2","v2"); + * put("k3","v3"); + * }}; * try (K2hash db = K2hash.of("App.k2h")) { * for(Map.Entry entry : data.entrySet()) { * db.setValue(entry.getKey(), entry.getValue()); // setValue @@ -110,7 +116,7 @@ * } * } */ -public class K2hashKeyQueue implements Closeable { +public final class K2hashKeyQueue implements Closeable { /** A logger instance. */ private static final Logger logger = LoggerFactory.getLogger(K2hashKeyQueue.class); @@ -138,17 +144,17 @@ public class K2hashKeyQueue implements Closeable { public static int DEFAULT_ATTRPACK_SIZE = 0; /* -- Members -- */ - /** a K2hash data handle */ + /** a K2hash data handle. */ private long handle = K2H_INVALID_HANDLE; - /** FIFO(Queue) or LIFO(Stack) */ + /** FIFO(Queue) or LIFO(Stack). */ private boolean fifo = true; - /** a prefix string of this queue */ + /** a prefix string of this queue. */ private String prefix = ""; - /** a K2hashQueue data handle */ + /** a K2hashQueue data handle. */ private long queueHandle = K2H_INVALID_HANDLE; - /** a password string */ + /** a password string. */ private String pass; - /** data expiration duration */ + /** data expiration duration. */ private long expirationDuration; /* -- debug methods -- */ @@ -181,7 +187,7 @@ public String toString() { * @return a K2hashKeyQueue instance * @throws IOException if a K2hash data handle is invalid */ - public static K2hashKeyQueue of(long handle) throws IOException { + public static K2hashKeyQueue of(final long handle) throws IOException { if (handle <= K2H_INVALID_HANDLE) { throw new IOException("K2hashKeyQueue.open failed"); } @@ -197,7 +203,7 @@ public static K2hashKeyQueue of(long handle) throws IOException { * @return a K2hashKeyQueue instance * @throws IOException if a K2hash data handle is invalid */ - public static K2hashKeyQueue of(long handle, boolean fifo) throws IOException { + public static K2hashKeyQueue of(final long handle, final boolean fifo) throws IOException { if (handle <= K2H_INVALID_HANDLE) { throw new IOException("K2hashKeyQueue.open failed"); } @@ -214,7 +220,8 @@ public static K2hashKeyQueue of(long handle, boolean fifo) throws IOException { * @return a K2hashKeyQueue instance * @throws IOException if a K2hash data handle is invalid */ - public static K2hashKeyQueue of(long handle, boolean fifo, String prefix) throws IOException { + public static K2hashKeyQueue of(final long handle, final boolean fifo, final String prefix) + throws IOException { if (handle <= K2H_INVALID_HANDLE) { throw new IOException("K2H_INVALID_HANDLE"); } @@ -234,7 +241,11 @@ public static K2hashKeyQueue of(long handle, boolean fifo, String prefix) throws * @throws IllegalArgumentException if position is less than zero */ public static K2hashKeyQueue of( - long handle, boolean fifo, String prefix, String pass, long expirationDuration) + final long handle, + final boolean fifo, + final String prefix, + final String pass, + final long expirationDuration) throws IOException { if (handle <= K2H_INVALID_HANDLE) { throw new IOException("K2H_INVALID_HANDLE"); @@ -248,24 +259,30 @@ public static K2hashKeyQueue of( * @param handle a K2hash data handle * @param fifo true if elements are in a FIFO (first-in-first-out) manner * @param prefix a prefix string of this queue + * @param pass a password to decrypt the value + * @param expirationDuration second to expire the value * @throws IllegalArgumentException if expirationDuration is negative * @throws IOException if a k2hash data handle is invalid */ private K2hashKeyQueue( - long handle, boolean fifo, String prefix, String pass, long expirationDuration) + final long paramHandle, + final boolean paramFifo, + final String paramPrefix, + final String paramPass, + final long paramExpirationDuration) throws IOException { - if (handle <= K2H_INVALID_HANDLE) { + if (paramHandle <= K2H_INVALID_HANDLE) { throw new IOException("handle is K2H_INVALID_HANDLE"); } - if (expirationDuration < 0) { + if (paramExpirationDuration < 0) { throw new IllegalArgumentException("expirationDuration is greater than equal zero"); } - this.handle = handle; - this.fifo = fifo; - this.prefix = prefix; - this.pass = pass; - this.expirationDuration = expirationDuration; + this.handle = paramHandle; + this.fifo = paramFifo; + this.prefix = paramPrefix; + this.pass = paramPass; + this.expirationDuration = paramExpirationDuration; // gets the library address if (INSTANCE == null) { @@ -284,7 +301,7 @@ private K2hashKeyQueue( } /* -- Instance methods -- */ - /** Free KeyQueueHandle */ + /** Free KeyQueueHandle. */ @Override public void close() throws IOException { assert (this.queueHandle > K2H_INVALID_HANDLE); @@ -352,7 +369,7 @@ public Map peek() { * @return an object at the position of this queue * @throws IllegalArgumentException if the position is less than zero */ - public Map peek(int position) { + public Map peek(final int position) { assert (this.handle > K2H_INVALID_HANDLE && this.queueHandle > K2H_INVALID_HANDLE); if (position < 0) { throw new IllegalArgumentException("position is greater than equal zero"); @@ -369,7 +386,7 @@ public Map peek(int position) { // key String key = null; - if (ppkey != null && pkeylen != null && pkeylen.getValue() != 0) { + if (pkeylen.getValue() != 0) { Pointer p = ppkey.getValue(); if (p != null) { byte[] buffer = p.getByteArray(0, pkeylen.getValue()); @@ -381,7 +398,7 @@ public Map peek(int position) { } // val String val = null; - if (ppval != null && pvallen != null && pvallen.getValue() != 0) { + if (pvallen.getValue() != 0) { Pointer p = ppval.getValue(); if (p != null) { byte[] buffer = p.getByteArray(0, pvallen.getValue()); @@ -410,7 +427,7 @@ public Map peek(int position) { * @return true if success false otherwise * @throws IllegalArgumentException - if arguments are illegal. */ - public boolean offer(Object obj) { + public boolean offer(final Object obj) { assert (this.handle > K2H_INVALID_HANDLE && this.queueHandle > K2H_INVALID_HANDLE); if (obj == null) { throw new IllegalArgumentException("obj is null. shouldn't be null."); @@ -471,7 +488,7 @@ public Map poll() { // key String key = null; - if (ppkey != null && pkeylen != null && pkeylen.getValue() != 0) { + if (pkeylen.getValue() != 0) { Pointer p = ppkey.getValue(); if (p != null) { byte[] buffer = p.getByteArray(0, pkeylen.getValue()); @@ -483,7 +500,7 @@ public Map poll() { } // val String val = null; - if (ppval != null && pvallen != null && pvallen.getValue() != 0) { + if (pvallen.getValue() != 0) { Pointer p = ppval.getValue(); if (p != null) { byte[] buffer = p.getByteArray(0, pvallen.getValue()); @@ -512,7 +529,7 @@ public Map poll() { * @throws NoSuchElementException - if this queue is empty * @return true if success false otherwise */ - public boolean add(Object obj) { + public boolean add(final Object obj) { assert (this.handle > K2H_INVALID_HANDLE && this.queueHandle > K2H_INVALID_HANDLE); @SuppressWarnings("unchecked") Map map = (Map) obj; @@ -540,7 +557,7 @@ public Map remove() { // key String key = null; - if (ppkey != null && pkeylen != null && pkeylen.getValue() != 0) { + if (pkeylen.getValue() != 0) { Pointer p = ppkey.getValue(); if (p != null) { byte[] buffer = p.getByteArray(0, pkeylen.getValue()); @@ -552,7 +569,7 @@ public Map remove() { } // val String val = null; - if (ppval != null && pvallen != null && pvallen.getValue() != 0) { + if (pvallen.getValue() != 0) { Pointer p = ppval.getValue(); if (p != null) { byte[] buffer = p.getByteArray(0, pvallen.getValue()); @@ -581,7 +598,7 @@ public Map remove() { * @param o element to be removed from this collection, if present * @return true if an element was removed as a result of this call */ - public boolean remove(Object o) { + public boolean remove(final Object o) { assert (this.handle > K2H_INVALID_HANDLE && this.queueHandle > K2H_INVALID_HANDLE); if (o == null) { throw new IllegalArgumentException("count is negative. should be positive"); @@ -603,7 +620,7 @@ public boolean remove(Object o) { // key String key = null; - if (ppkey != null && pkeylen != null && pkeylen.getValue() != 0) { + if (pkeylen.getValue() != 0) { Pointer p = ppkey.getValue(); if (p != null) { byte[] buffer = p.getByteArray(0, pkeylen.getValue()); @@ -616,7 +633,7 @@ public boolean remove(Object o) { } // val String val = null; - if (ppval != null && pvallen != null && pvallen.getValue() != 0) { + if (pvallen.getValue() != 0) { Pointer p = ppval.getValue(); if (p != null) { byte[] buffer = p.getByteArray(0, pvallen.getValue()); @@ -649,7 +666,7 @@ public boolean remove(Object o) { * @throws NoSuchElementException - if this queue is empty * @throws IllegalArgumentException if an illegal augment exists */ - public List> removeList(long count) { + public List> removeList(final long count) { assert (this.handle > K2H_INVALID_HANDLE && this.queueHandle > K2H_INVALID_HANDLE); if (count < 0) { throw new IllegalArgumentException("count is negative. should be positive"); @@ -696,7 +713,7 @@ public Map element() { * @throws NoSuchElementException - if this queue is empty * @throws IllegalArgumentException if an illegal augment exists */ - public Map element(int position) { + public Map element(final int position) { assert (this.handle > K2H_INVALID_HANDLE && this.queueHandle > K2H_INVALID_HANDLE); if (position < 0) { throw new IllegalArgumentException("position is negative. should be positive"); @@ -728,3 +745,12 @@ public void print() { INSTANCE.k2h_keyq_dump(this.queueHandle, null); } } +// +// Local variables: +// tab-width: 2 +// c-basic-offset: 2 +// indent-tabs-mode: nil +// End: +// vim600: noexpandtab sw=2 ts=2 fdm=marker +// vim<600: noexpandtab sw=2 ts=2 +// diff --git a/src/main/java/ax/antpick/k2hash/K2hashLibrary.java b/k2hash/src/main/java/K2hashLibrary.java similarity index 93% rename from src/main/java/ax/antpick/k2hash/K2hashLibrary.java rename to k2hash/src/main/java/K2hashLibrary.java index 553d116..78a306d 100644 --- a/src/main/java/ax/antpick/k2hash/K2hashLibrary.java +++ b/k2hash/src/main/java/K2hashLibrary.java @@ -28,8 +28,12 @@ */ package ax.antpick.k2hash; -import com.sun.jna.*; -import com.sun.jna.ptr.*; +import com.sun.jna.Library; +import com.sun.jna.Pointer; +import com.sun.jna.StringArray; +import com.sun.jna.ptr.IntByReference; +import com.sun.jna.ptr.LongByReference; +import com.sun.jna.ptr.PointerByReference; /** * This JNA interface provides functions defined in the K2hash C library. This class is a @@ -53,7 +57,7 @@ * } * } */ -interface K2hashLibrary extends Library { +public interface K2hashLibrary extends Library { /** * Changes the logging level up. The level will go back to the "silent" level after the "debug" * level. @@ -78,7 +82,7 @@ interface K2hashLibrary extends Library { /** * Redirects stderr to a log file. * - * @param filepath A filepath string + * @param filepath A file path string * @return true if set the debug file, false otherwise */ boolean k2h_set_debug_file(String filepath); @@ -137,8 +141,8 @@ interface K2hashLibrary extends Library { * stream. * * @param handle a k2hash data handle - * @param stream the file pointer to which prints elementtables - * @return true if prints elementtables, false otherwise + * @param stream the file pointer to which prints element tables + * @return true if prints element tables, false otherwise */ boolean k2h_dump_elementtable(long handle, Pointer stream); @@ -147,8 +151,8 @@ interface K2hashLibrary extends Library { * the stream. * * @param handle a k2hash data handle - * @param stream the file pointer to which prints headers, tables, elementtables and others - * @return true if prints headers, tables, elementtables and others false + * @param stream the file pointer to which prints headers, tables, element tables and others + * @return true if prints headers, tables, element tables and others false * otherwise */ boolean k2h_dump_full(long handle, Pointer stream); @@ -172,7 +176,7 @@ interface K2hashLibrary extends Library { /** * Initializes a k2hash file. * - * @param filepath a filepath string + * @param filepath a file path string * @param maskbitcnt a mask bit * @param cmaskbitcnt a cmask bit * @param pagesize a page size @@ -185,7 +189,7 @@ boolean k2h_create( /** * Opens a k2hash file. * - * @param filepath a filepath string + * @param filepath a file path string * @param readonly the flag whether readonly * @param removefile the flag whether remove file after detaching the memory * @param fullmap the flag whether mmap whole of a file into memory @@ -208,7 +212,7 @@ long k2h_open( /** * Opens a k2hash file with read and write mode. * - * @param filepath a filepath string + * @param filepath a file path string * @param fullmap the flag whether mmap whole of a file into memory * @param maskbitcnt a mask bit * @param cmaskbitcnt a cmask bit @@ -227,7 +231,7 @@ long k2h_open_rw( /** * Opens a k2hash file with read mode. * - * @param filepath a filepath string + * @param filepath a file path string * @param fullmap the flag whether mmap whole of a file into memory * @param maskbitcnt a mask bit * @param cmaskbitcnt a cmask bit @@ -246,7 +250,7 @@ long k2h_open_ro( /** * Opens a k2hash file with temporary file mode. * - * @param filepath a filepath string + * @param filepath a file path string * @param fullmap the flag whether mmap whole of a file into memory * @param maskbitcnt a mask bit * @param cmaskbitcnt a cmask bit @@ -300,7 +304,7 @@ long k2h_open_tempfile( * @param prefixlen the length of a prefix string of a transaction log entry * @param pparam a parameter passes to transaction log parser program * @param paramlen the length of parameter passes to transaction log parser program - * @param expirationDuration the duration to expire a log + * @param pExpirationDuration the duration to expire a log * @return true if success false otherwise */ boolean k2h_transaction_param_we( @@ -311,7 +315,7 @@ boolean k2h_transaction_param_we( int prefixlen, String pparam, int paramlen, - LongByReference expirationDuration); + Pointer pExpirationDuration); /** * Gets the number of workers for transactions. @@ -347,7 +351,7 @@ boolean k2h_transaction_param_we( * Loads data from serialized data in a file. * * @param handle a k2hash data handle - * @param filepath a filepath string + * @param filepath a file path string * @param errskip true if skip errors while loading a file * @return true if success false otherwise */ @@ -357,7 +361,7 @@ boolean k2h_transaction_param_we( * Saves serialized data to an file. * * @param handle a k2hash data handle - * @param filepath a filepath string + * @param filepath a file path string * @param errskip true if skip errors while writing data to a file * @return true if success false otherwise */ @@ -369,7 +373,7 @@ boolean k2h_transaction_param_we( * @param handle a k2hash data handle * @param is_mtime the flag whether the library records when files are created and modified * @param is_defenc the flag whether the library encrypts data - * @param passfile a password filepath string + * @param passfile a password file path string * @param is_history the flag whether the library save its history * @param expirationDuration the duration data expires * @return true if success false otherwise @@ -386,7 +390,7 @@ boolean k2h_set_common_attr( * Enables attributes by using shared library. * * @param handle a k2hash data handle - * @param libpath a shared library filepath string + * @param libpath a shared library file path string * @return true if success false otherwise */ boolean k2h_add_attr_plugin_library(long handle, String libpath); @@ -446,10 +450,10 @@ boolean k2h_get_value_wp( * @param handle a k2hash data handle * @param pkey a key string * @param pass a passphrase string - * @return a value string + * @return the pointer to the value string */ // extern char* k2h_get_str_direct_value_wp(k2h_h handle, const char* pkey, const char* pass); - String k2h_get_str_direct_value_wp(long handle, String pkey, String pass); + Pointer k2h_get_str_direct_value_wp(long handle, String pkey, String pass); /** * Sets the value of a key in binary format. @@ -494,7 +498,7 @@ boolean k2h_set_str_value_wa( * * @param handle a k2hash data handle * @param pkey a key string - * @return an arraylist of a subkey + * @return an array list of a subkey */ // extern char** k2h_get_str_direct_subkeys(k2h_h handle, const char* pkey); PointerByReference k2h_get_str_direct_subkeys(long handle, String pkey); // TODO text format @@ -535,7 +539,7 @@ boolean k2h_get_subkeys( * @return true if success false otherwise */ // extern bool k2h_free_keyarray(char** pkeys); - boolean k2h_free_keyarray(String[] pkeys); + boolean k2h_free_keyarray(PointerByReference pkeys); /** * Sets the subkeys of a key in binary format. @@ -633,7 +637,7 @@ boolean k2h_add_attr( * @param pkey a key byte array * @param keylength the number of a key * @param ppattrspck a pointer to an array of a pointer of an attribute pack structure - * @param pattrspckcnt the number of attribure structures + * @param pattrspckcnt the number of attribute structures * @return true if success false otherwise */ // extern bool k2h_get_attrs(k2h_h handle, const unsigned char* pkey, size_t keylength, @@ -656,7 +660,7 @@ boolean k2h_get_attrs( */ // extern PK2HATTRPCK k2h_get_direct_attrs(k2h_h handle, const unsigned char* pkey, size_t // keylength, int* pattrspckcnt); - K2hashAttrPack k2h_get_direct_attrs( + PointerByReference k2h_get_direct_attrs( long handle, byte[] pkey, long keylength, IntByReference pattrspckcnt); /** @@ -664,7 +668,7 @@ K2hashAttrPack k2h_get_direct_attrs( * * @param handle a k2hash data handle * @param pkey a key string - * @param pattrspckcnt the number of attribure structures + * @param pattrspckcnt the number of attribute structures * @return a pointer to an array of a pointer of an attribute pack structure */ // extern PK2HATTRPCK k2h_get_str_direct_attrs(k2h_h handle, const char* pkey, int* pattrspckcnt); @@ -674,11 +678,11 @@ K2hashAttrPack k2h_get_direct_attrs( * Releases the memory area pointed by the K2HATTRPCK object. * * @param pattrs an array of a pointer of an attribute pack structure - * @param attrcnt the number of attribure structures + * @param attrcnt the number of attribute structures * @return true if success false otherwise */ // extern bool k2h_free_attrpack(PK2HATTRPCK pattrs, int attrcnt); - boolean k2h_free_attrpack(K2hashAttrPack[] pattrs, int attrcnt); + boolean k2h_free_attrpack(K2hashAttrPack pattrs, int attrcnt); /** * Removes the key and all the subkeys of the key. @@ -864,8 +868,8 @@ boolean k2h_q_read_wp( * Adds an item to the tail of the queue. * * @param qhandle a queue handle - * @param bydata a queueing string - * @param datalen the length of a queueing string + * @param bydata a queue string + * @param datalen the length of a queue string * @return true if success false otherwise */ // extern bool k2h_q_push(k2h_q_h qhandle, const unsigned char* bydata, size_t datalen); @@ -896,10 +900,10 @@ boolean k2h_q_read_wp( * Adds an item to the tail of the queue. * * @param qhandle a queue handle - * @param bydata a queueing string - * @param datalen the length of a queueing string + * @param bydata a queue string + * @param datalen the length of a queue string * @param attrspck a pointer to an array of a pointer of an attribute pack structure - * @param attrspckcnt the number of attribure structures + * @param attrspckcnt the number of attribute structures * @param encpass a passphrase string * @param expirationDuration a duration in seconds to expire the data * @return true if success false otherwise @@ -919,10 +923,10 @@ boolean k2h_q_push_wa( * Retrives an item from the front of the queue. * * @param qhandle a queue handle - * @param ppdata a pointer to a queueing string - * @param pdatalen the length of a pointer of a queueing string + * @param ppdata a pointer to a queue string + * @param pdatalen the length of a pointer of a queue string * @param ppattrspck a pointer to an array of a pointer of an attribute pack structure - * @param pattrspckcnt the number of attribure structures + * @param pattrspckcnt the number of attribute structures * @param encpass a passphrase string * @return true if success false otherwise */ @@ -940,8 +944,8 @@ boolean k2h_q_pop_wa( * Retrieves an item from the front of the queue and removes it. * * @param qhandle a queue handle - * @param ppdata a pointer to a queueing string - * @param pdatalen the length of a pointer of a queueing string + * @param ppdata a pointer to a queue string + * @param pdatalen the length of a pointer of a queue string * @param encpass a passphrase string * @return true if success false otherwise */ @@ -951,7 +955,7 @@ boolean k2h_q_pop_wp( long qhandle, PointerByReference ppdata, IntByReference pdatalen, String encpass); /** - * Removes data in a queue + * Removes data in a queue. * * @param qhandle a queue handle * @param count the number of data to be removed @@ -962,7 +966,7 @@ boolean k2h_q_pop_wp( boolean k2h_q_remove_wp(long qhandle, int count, String encpass); /** - * Prints data in a queue + * Prints data in a queue. * * @param qhandle a queue handle * @param stream the file pointer to which prints @@ -1035,11 +1039,11 @@ boolean k2h_q_pop_wp( int k2h_keyq_count(long keyqhandle); /** - * Retrives an item from the key queue. + * Retrieves an item from the key queue. * * @param keyqhandle a key queue handle - * @param ppdata a pointer to a queueing string - * @param pdatalen the length of a pointer of a queueing string + * @param ppdata a pointer to a queue string + * @param pdatalen the length of a pointer of a queue string * @param pos starting position to read in this queue * @return true if success false otherwise */ @@ -1049,7 +1053,7 @@ boolean k2h_keyq_read( long keyqhandle, PointerByReference ppdata, IntByReference pdatalen, int pos); /** - * Retrives an item from the key queue. + * Retrieves an item from the key queue. * * @param keyqhandle a key queue handle * @param ppkey a key string @@ -1095,7 +1099,7 @@ boolean k2h_keyq_read_keyval( boolean k2h_keyq_push_keyval(long keyqhandle, String bykey, int keylen, String byval, int vallen); /** - * Retrives an item from the front of the key queue. + * Retrieves an item from the front of the key queue. * * @param keyqhandle a key queue handle * @param ppval a pointer to a value string @@ -1106,7 +1110,7 @@ boolean k2h_keyq_read_keyval( boolean k2h_keyq_pop(long keyqhandle, PointerByReference ppval, IntByReference pvallen); /** - * Retrives an item from the front of the key queue. + * Retrieves an item from the front of the key queue. * * @param keyqhandle a key queue handle * @param ppkey a key string @@ -1124,7 +1128,7 @@ boolean k2h_keyq_pop_keyval( IntByReference pvallen); /** - * Retrives an item from the front of the key queue. + * Retrieves an item from the front of the key queue. * * @param keyqhandle a key queue handle * @param ppkey a key string @@ -1158,8 +1162,8 @@ boolean k2h_keyq_pop_keyval_wp( * Retrieves an item from the key queue. The item will not be removed from the key queue. * * @param keyqhandle a key queue handle - * @param ppdata a pointer to a queueing string - * @param pdatalen the length of a pointer of a queueing string + * @param ppdata a pointer to a queue string + * @param pdatalen the length of a pointer of a queue string * @param pos starting position to read in this queue * @param encpass a passphrase string * @return true if success false otherwise @@ -1193,7 +1197,7 @@ boolean k2h_keyq_read_keyval_wp( String encpass); /** - * Adds a key to the tail of the queue + * Adds a key to the tail of the queue. * * @param keyqhandle a key queue handle * @param bykey a key string @@ -1212,7 +1216,7 @@ boolean k2h_keyq_push_wa( LongByReference expirationDuration); /** - * Adds a key and a value to the tail of the queue + * Adds a key and a value to the tail of the queue. * * @param keyqhandle a key queue handle * @param bykey a key string @@ -1235,11 +1239,11 @@ boolean k2h_keyq_push_keyval_wa( LongByReference expirationDuration); /** - * Retrieve and removes an value from the front of the queue + * Retrieve and removes an value from the front of the queue. * * @param keyqhandle a key queue handle * @param ppval a pointer to a key string - * @param pvallen the number of a keye string + * @param pvallen the number of a key string * @param encpass a passphrase string * @return true if success false otherwise */ @@ -1270,7 +1274,7 @@ boolean k2h_keyq_pop_keyval_wp( String encpass); /** - * Removes data in a key queue + * Removes data in a key queue. * * @param keyqhandle a key queue handle * @param count the number of data to be removed @@ -1281,7 +1285,7 @@ boolean k2h_keyq_pop_keyval_wp( boolean k2h_keyq_remove_wp(long keyqhandle, int count, String encpass); /** - * Prints data in a key queue + * Prints data in a key queue. * * @param keyqhandle a key queue handle * @param stream the file pointer to which prints @@ -1290,3 +1294,12 @@ boolean k2h_keyq_pop_keyval_wp( // extern bool k2h_keyq_dump(k2h_keyq_h keyqhandle, FILE* stream); boolean k2h_keyq_dump(long keyqhandle, Pointer stream); } +// +// Local variables: +// tab-width: 2 +// c-basic-offset: 2 +// indent-tabs-mode: nil +// End: +// vim600: noexpandtab sw=2 ts=2 fdm=marker +// vim<600: noexpandtab sw=2 ts=2 +// diff --git a/src/main/java/ax/antpick/k2hash/K2hashQueue.java b/k2hash/src/main/java/K2hashQueue.java similarity index 91% rename from src/main/java/ax/antpick/k2hash/K2hashQueue.java rename to k2hash/src/main/java/K2hashQueue.java index 096408c..62349a4 100644 --- a/src/main/java/ax/antpick/k2hash/K2hashQueue.java +++ b/k2hash/src/main/java/K2hashQueue.java @@ -28,8 +28,10 @@ */ package ax.antpick.k2hash; -import com.sun.jna.*; -import com.sun.jna.ptr.*; +import com.sun.jna.Pointer; +import com.sun.jna.ptr.IntByReference; +import com.sun.jna.ptr.LongByReference; +import com.sun.jna.ptr.PointerByReference; import java.io.Closeable; import java.io.IOException; import java.lang.reflect.Field; @@ -41,7 +43,7 @@ /** * Queue holds elements in a FIFO (first-in-first-out) manner. Order of elements is configurable by - * passing a boolean variable to the 2nd parameter of constructors. Passing a true variable + * passing a Boolean variable to the 2nd parameter of constructors. Passing a true variable * constructs a Queue instance, Otherwise a Stack instance is created. * *

The difference of KeyQueue and Queue: KeyQueue stores references to keys in k2hash database @@ -92,7 +94,7 @@ * * } */ -public class K2hashQueue implements Closeable { +public final class K2hashQueue implements Closeable { /** A logger instance. */ private static final Logger logger = LoggerFactory.getLogger(K2hashQueue.class); @@ -118,17 +120,17 @@ public class K2hashQueue implements Closeable { public static final int DEFAULT_ATTRPACK_SIZE = 0; /* -- Members -- */ - /** a K2hash data handle */ + /** a K2hash data handle. */ private long handle = K2H_INVALID_HANDLE; - /** FIFO(Queue) or LIFO(Stack) */ + /** FIFO(Queue) or LIFO(Stack). */ private boolean fifo = true; - /** a prefix string of this queue */ + /** a prefix string of this queue. */ private String prefix = ""; - /** a K2hashQueue data handle */ + /** a K2hashQueue data handle. */ private long queueHandle = K2H_INVALID_HANDLE; - /** a password string */ + /** a password string. */ private String pass; - /** data expiration duration */ + /** data expiration duration. */ private long expirationDuration; /* -- debug methods -- */ @@ -161,7 +163,7 @@ public String toString() { * @return a K2hashQueue instance * @throws IOException if a K2hash data handle is invalid */ - public static K2hashQueue of(long handle) throws IOException { + public static K2hashQueue of(final long handle) throws IOException { if (handle <= K2H_INVALID_HANDLE) { throw new IOException("K2hashQueue.open failed"); } @@ -178,7 +180,8 @@ public static K2hashQueue of(long handle) throws IOException { * @return a K2hashQueue instance * @throws IOException if a K2hash data handle is invalid */ - public static K2hashQueue of(long handle, boolean fifo, String prefix) throws IOException { + public static K2hashQueue of(final long handle, final boolean fifo, final String prefix) + throws IOException { if (handle <= K2H_INVALID_HANDLE) { throw new IOException("K2H_INVALID_HANDLE"); } @@ -198,7 +201,11 @@ public static K2hashQueue of(long handle, boolean fifo, String prefix) throws IO * @throws IllegalArgumentException if an illegal augment exists */ public static K2hashQueue of( - long handle, boolean fifo, String prefix, String pass, long expirationDuration) + final long handle, + final boolean fifo, + final String prefix, + final String pass, + final long expirationDuration) throws IOException { if (handle <= K2H_INVALID_HANDLE) { throw new IOException("K2H_INVALID_HANDLE"); @@ -218,20 +225,24 @@ public static K2hashQueue of( * @throws IOException if a K2hash handle is invalid */ private K2hashQueue( - long handle, boolean fifo, String prefix, String pass, long expirationDuration) + final long paramHandle, + final boolean paramFifo, + final String paramPrefix, + final String paramPass, + final long paramExpirationDuration) throws IOException { - if (handle <= K2H_INVALID_HANDLE) { + if (paramHandle <= K2H_INVALID_HANDLE) { throw new IOException("handle is K2H_INVALID_HANDLE"); } - if (expirationDuration < 0) { + if (paramExpirationDuration < 0) { throw new IllegalArgumentException("expirationDuration is greater than equal zero"); } - this.handle = handle; - this.prefix = prefix; - this.fifo = fifo; - this.pass = pass; - this.expirationDuration = expirationDuration; + this.handle = paramHandle; + this.prefix = paramPrefix; + this.fifo = paramFifo; + this.pass = paramPass; + this.expirationDuration = paramExpirationDuration; // gets the library address if (INSTANCE == null) { @@ -248,7 +259,7 @@ private K2hashQueue( } /* -- Instance methods -- */ - /** Free KeyQueueHandle */ + /** Free KeyQueueHandle. */ @Override public void close() throws IOException { assert (this.queueHandle > K2H_INVALID_HANDLE); @@ -316,7 +327,7 @@ public String peek() { * @return an object at the position of this queue * @throws IllegalArgumentException if the position is less than zero */ - public String peek(int position) { + public String peek(final int position) { assert (this.handle > K2H_INVALID_HANDLE && this.queueHandle > K2H_INVALID_HANDLE); if (position < 0) { @@ -329,7 +340,7 @@ public String peek(int position) { boolean isSuccess = INSTANCE.k2h_q_read_wp(this.queueHandle, ppdata, pdatalen, position, this.pass); - if (ppdata != null && pdatalen != null && pdatalen.getValue() != 0) { + if (pdatalen.getValue() != 0) { Pointer p = ppdata.getValue(); byte[] buffer = p.getByteArray(0, pdatalen.getValue()); String data = new String(buffer); @@ -348,7 +359,7 @@ public String peek(int position) { * @return true if success false otherwise * @throws IllegalArgumentException - if arguments are illegal. */ - public boolean offer(Object obj) { + public boolean offer(final Object obj) { assert (this.handle > K2H_INVALID_HANDLE && this.queueHandle > K2H_INVALID_HANDLE); if (obj == null) { throw new IllegalArgumentException("obj is null. shouldn't be null."); @@ -408,7 +419,7 @@ public String poll() { IntByReference pdatalen = new IntByReference(); boolean isSuccess = INSTANCE.k2h_q_pop_wa(this.queueHandle, ppdata, pdatalen, null, null, this.pass); - if (ppdata != null && pdatalen != null && pdatalen.getValue() != 0) { + if (pdatalen.getValue() != 0) { Pointer p = ppdata.getValue(); if (p != null) { byte[] buffer = p.getByteArray(0, pdatalen.getValue()); @@ -430,7 +441,7 @@ public String poll() { * @return true if success false otherwise * @throws IllegalArgumentException if an illegal augment exists */ - public boolean add(Object obj) { + public boolean add(final Object obj) { if (obj == null) { throw new IllegalArgumentException("obj is null. shouldn't be null."); } @@ -461,7 +472,7 @@ public String remove() { PointerByReference ppdata = new PointerByReference(); IntByReference pdatalen = new IntByReference(); boolean isSuccess = INSTANCE.k2h_q_pop_wp(this.queueHandle, ppdata, pdatalen, this.pass); - if (ppdata != null && pdatalen != null && pdatalen.getValue() != 0) { + if (pdatalen.getValue() != 0) { Pointer p = ppdata.getValue(); if (p != null) { byte[] buffer = p.getByteArray(0, pdatalen.getValue()); @@ -482,7 +493,7 @@ public String remove() { * @throws NoSuchElementException - if this queue is empty * @throws IllegalArgumentException if an illegal augment exists */ - public List removeList(long count) { + public List removeList(final long count) { assert (this.handle > K2H_INVALID_HANDLE && this.queueHandle > K2H_INVALID_HANDLE); if (count < 0) { throw new IllegalArgumentException("count is negative. should be positive"); @@ -529,7 +540,7 @@ public String element() { * @throws NoSuchElementException - if this queue is empty * @throws IllegalArgumentException if an illegal augment exists */ - public String element(int position) { + public String element(final int position) { assert (this.handle > K2H_INVALID_HANDLE && this.queueHandle > K2H_INVALID_HANDLE); if (position < 0) { throw new IllegalArgumentException("position is negative. should be positive"); @@ -563,3 +574,12 @@ public void print() { INSTANCE.k2h_q_dump(this.queueHandle, null); } } +// +// Local variables: +// tab-width: 2 +// c-basic-offset: 2 +// indent-tabs-mode: nil +// End: +// vim600: noexpandtab sw=2 ts=2 fdm=marker +// vim<600: noexpandtab sw=2 ts=2 +// diff --git a/k2hash/src/main/java/package-info.java b/k2hash/src/main/java/package-info.java new file mode 100644 index 0000000..e63c45b --- /dev/null +++ b/k2hash/src/main/java/package-info.java @@ -0,0 +1,31 @@ +/* + * K2hash Java Driver + * + * Copyright 2023 Yahoo Japan Corporation. + * + * K2hash is a key-value store base library. + * K2hash Java Driver is the one for Java. + * + * See the LICENSE file with this source code for + * the details of the MIT License. + * + * AUTHOR: Hirotaka Wakabayashi + * CREATE: Fri, 30 Sep 2023 + * REVISION: + * + */ +/** + * Provides k2hash implementations in Java. + * + * @author Hirotaka Wakabayashi + */ +package ax.antpick.k2hash; +// +// Local variables: +// tab-width: 2 +// c-basic-offset: 2 +// indent-tabs-mode: nil +// End: +// vim600: noexpandtab sw=2 ts=2 fdm=marker +// vim<600: noexpandtab sw=2 ts=2 +// diff --git a/src/test/java/ax/antpick/k2hash/K2hashFileTest.java b/k2hash/src/test/java/K2hashFileTest.java similarity index 61% rename from src/test/java/ax/antpick/k2hash/K2hashFileTest.java rename to k2hash/src/test/java/K2hashFileTest.java index 74612b8..f28c3d3 100644 --- a/src/test/java/ax/antpick/k2hash/K2hashFileTest.java +++ b/k2hash/src/test/java/K2hashFileTest.java @@ -37,7 +37,6 @@ import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; - import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Disabled; @@ -69,7 +68,7 @@ public void tearDown() { /** K2hash Constructor */ @Test public void testOfArg1() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { assertTrue(true); } catch (IOException e) { System.out.println("IOException " + e.getMessage()); @@ -113,9 +112,15 @@ public void testBeginTxArg1() { if (fileDb.exists()) { fileDb.delete(); } - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { assertTrue(db.beginTx("/tmp/testTxArg1.log")); assertTrue(db.setValue("key", "val")); + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + System.out.println("InterruptedException " + e.getMessage()); + assertFalse(true); + } assertTrue(fileDb.length() > 0); assertTrue(db.getTxFileFd() != -1); } catch (IOException e) { @@ -131,10 +136,16 @@ public void testBeginTxArg5() { if (fileDb.exists()) { fileDb.delete(); } - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { assertTrue( db.beginTx("/tmp/testTxArg5.log", "testTxArg5", "testTxArg5", 8)); // 8 secs to expire assertTrue(db.setValue("key", "val")); + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + System.out.println("InterruptedException " + e.getMessage()); + assertFalse(true); + } assertTrue(fileDb.length() > 0); assertTrue(db.getTxFileFd() != -1); } catch (IOException e) { @@ -150,11 +161,9 @@ public void testStopTx() { if (fileDb.exists()) { fileDb.delete(); } - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { assertTrue(db.beginTx("/tmp/testTxArg1.log")); assertTrue(db.stopTx()); - assertTrue(db.setValue("key", "val")); - assertTrue(fileDb.length() == 0); } catch (IOException e) { System.out.println("IOException " + e.getMessage()); assertFalse(true); @@ -164,7 +173,7 @@ public void testStopTx() { /** K2hash getTxPoolSize */ @Test public void testGetTxPoolSize() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { assertEquals(0, db.getTxPoolSize()); } catch (IOException e) { System.out.println("IOException " + e.getMessage()); @@ -175,7 +184,7 @@ public void testGetTxPoolSize() { /** K2hash setTxPoolSize */ @Test public void testSetTxPoolSize() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { assertTrue(db.setTxPoolSize(1)); assertEquals(1, db.getTxPoolSize()); } catch (IOException e) { @@ -191,8 +200,8 @@ public void testDumpToFileArg1() { if (fileDb.exists()) { fileDb.delete(); } - try (K2hash db = K2hash.of(FILEDB)) { - assertTrue(db.setValue("key", "val")); + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { + assertTrue(db.setValue("testDumpToFileArg1", "val")); assertTrue(db.dumpToFile("/tmp/testDumpToFileArg1.log")); assertTrue(fileDb.length() != 0); } catch (IOException e) { @@ -208,8 +217,8 @@ public void testDumpToFileArg2() { if (fileDb.exists()) { fileDb.delete(); } - try (K2hash db = K2hash.of(FILEDB)) { - assertTrue(db.setValue("key", "val")); + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { + assertTrue(db.setValue("testDumpToFileArg2", "val")); assertTrue(db.dumpToFile("/tmp/testDumpToFileArg2.log", false)); assertTrue(fileDb.length() != 0); } catch (IOException e) { @@ -225,13 +234,13 @@ public void testLoadFromFileArg1() { if (fileDb.exists()) { fileDb.delete(); } - try (K2hash db = K2hash.of(FILEDB)) { - assertTrue(db.setValue("key", "val")); + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { + assertTrue(db.setValue("testLoadFromFileArg1", "val")); assertTrue(db.dumpToFile("/tmp/testLoadFromFileArg1.log")); assertTrue(fileDb.length() != 0); - assertTrue(db.remove("key")); + assertTrue(db.remove("testLoadFromFileArg1")); assertTrue(db.loadFromFile("/tmp/testLoadFromFileArg1.log")); - assertTrue(db.getValue("key") != null); + assertTrue(db.getValue("testLoadFromFileArg1") != null); } catch (IOException e) { System.out.println("IOException " + e.getMessage()); assertFalse(true); @@ -245,13 +254,13 @@ public void testLoadFromFileArg2() { if (fileDb.exists()) { fileDb.delete(); } - try (K2hash db = K2hash.of(FILEDB)) { - assertTrue(db.setValue("key", "val")); + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { + assertTrue(db.setValue("testLoadFromFileArg2", "val")); assertTrue(db.dumpToFile("/tmp/testLoadFromFileArg2.log")); assertTrue(fileDb.length() != 0); - assertTrue(db.remove("key")); + assertTrue(db.remove("testLoadFromFileArg2")); assertTrue(db.loadFromFile("/tmp/testLoadFromFileArg2.log", false)); - assertTrue(db.getValue("key") != null); + assertTrue(db.getValue("testLoadFromFileArg2") != null); } catch (IOException e) { System.out.println("IOException " + e.getMessage()); assertFalse(true); @@ -261,7 +270,7 @@ public void testLoadFromFileArg2() { /** K2hash enableMtime */ @Test public void testEnableMtime() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { assertTrue(db.enableMtime(true)); } catch (IOException e) { System.out.println("IOException " + e.getMessage()); @@ -272,7 +281,7 @@ public void testEnableMtime() { /** K2hash enableEncrypt */ @Test public void testEnableEncryptFalse() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { assertTrue(db.enableEncryption(false)); } catch (IOException e) { System.out.println("IOException " + e.getMessage()); @@ -283,7 +292,7 @@ public void testEnableEncryptFalse() { /** K2hash enableEncrypt */ @Test public void testEnableEncryptTrue() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { assertTrue(db.setDefaultEncryptionPassword("secretstring")); assertTrue(db.enableEncryption(true)); } catch (IOException e) { @@ -296,7 +305,7 @@ public void testEnableEncryptTrue() { @Test public void testSetEncryptionPassword() { File file = new File("password.txt"); - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { if (!file.exists()) { file.createNewFile(); } @@ -314,7 +323,7 @@ public void testSetEncryptionPasswordException() { if (file.exists()) { file.delete(); } - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { db.setEncryptionPasswordFile("password.txt"); assertTrue(false); } catch (IOException e) { @@ -325,7 +334,7 @@ public void testSetEncryptionPasswordException() { /** K2hash addDecryptionPassword */ @Test public void testAddDecryptionPassword() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { assertTrue(db.addDecryptionPassword("secretstring")); } catch (IOException e) { System.out.println("IOException " + e.getMessage()); @@ -336,7 +345,7 @@ public void testAddDecryptionPassword() { /** K2hash enableHistory */ @Test public void testEnableHistory() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { assertTrue(db.enableHistory(true)); } catch (IOException e) { System.out.println("IOException " + e.getMessage()); @@ -347,26 +356,32 @@ public void testEnableHistory() { /** K2hash setExpirationDuration */ @Test public void testSetExpireDuration() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { assertTrue(db.setExpirationDuration(3, TimeUnit.SECONDS)); - assertTrue(db.setValue("key", "val")); + assertTrue(db.setValue("testSetExpireDuration", "val")); try { Thread.sleep(5000); } catch (InterruptedException e) { System.out.println("InterruptedException " + e.getMessage()); assertFalse(true); } - assertTrue(db.getValue("key") == null); + assertTrue(db.getValue("testSetExpireDuration") == null); } catch (IOException e) { System.out.println("IOException " + e.getMessage()); assertFalse(true); } } - /** K2hash printAttributePlugins */ + /** + * K2hash printAttributePlugins + * + *

NOTE: This test is disabled because k2hash library corrupted the channel used by the plugin + * in order to transmit events with test status back to Maven process. + */ + @Test @Disabled public void testPrintAttributePlugins() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { db.printAttributePlugins(); assertTrue(true); } catch (IOException e) { @@ -375,10 +390,16 @@ public void testPrintAttributePlugins() { } } - /** K2hash printAttributes */ + /** + * K2hash printAttributes + * + *

NOTE: This test is disabled because k2hash library corrupted the channel used by the plugin + * in order to transmit events with test status back to Maven process. + */ + @Test @Disabled public void testPrintAttributes() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { db.printAttributes(); assertTrue(true); } catch (IOException e) { @@ -390,11 +411,11 @@ public void testPrintAttributes() { /** K2hash getValue */ @Test public void testGetValueArg1() { - try (K2hash db = K2hash.of(FILEDB)) { - String val = db.getValue("key"); + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { + String val = db.getValue("testGetValueArg1"); assertTrue(val == null); - assertTrue(db.setValue("key", "val")); - assertTrue(db.getValue("key").equals("val")); + assertTrue(db.setValue("testGetValueArg1", "val")); + assertTrue(db.getValue("testGetValueArg1").equals("val")); } catch (IOException e) { System.out.println("IOException " + e.getMessage()); assertFalse(true); @@ -404,11 +425,11 @@ public void testGetValueArg1() { /** K2hash getValue */ @Test public void testGetValueArg2() { - try (K2hash db = K2hash.of(FILEDB)) { - String val = db.getValue("key", "pass"); + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { + String val = db.getValue("testGetValueArg2", "pass"); assertTrue(val == null); - assertTrue(db.setValue("key", "value", "pass", 0, TimeUnit.SECONDS)); - assertTrue(db.getValue("key", "pass").equals("value")); + assertTrue(db.setValue("testGetValueArg2", "value", "pass", 0, TimeUnit.SECONDS)); + assertTrue(db.getValue("testGetValueArg2", "pass").equals("value")); } catch (IOException e) { System.out.println("IOException " + e.getMessage()); assertFalse(true); @@ -418,12 +439,12 @@ public void testGetValueArg2() { /** K2hash getSubkeys */ @Test public void testGetSubkeysArg1() { - try (K2hash db = K2hash.of(FILEDB)) { - assertTrue(db.getSubkeys("key") == null); - assertTrue(db.setSubkey("key", "subkey")); - List list = db.getSubkeys("key"); + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { + assertTrue(db.getSubkeys("testGetSubkeysArg1") == null); + assertTrue(db.setSubkey("testGetSubkeysArg1", "testGetSubkeysArg1_subkey")); + List list = db.getSubkeys("testGetSubkeysArg1"); assertTrue(list != null); - assertTrue(list.get(0).equals("subkey")); + assertTrue(list.get(0).equals("testGetSubkeysArg1_subkey")); } catch (IOException e) { System.out.println("IOException " + e.getMessage()); assertFalse(true); @@ -433,11 +454,11 @@ public void testGetSubkeysArg1() { /** K2hash setSubkey */ @Test public void testSetSubkey() { - try (K2hash db = K2hash.of(FILEDB)) { - assertTrue(db.setSubkey("key", "subkey")); - List list = db.getSubkeys("key"); + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { + assertTrue(db.setSubkey("testSetSubkey", "testSetSubkey_subkey")); + List list = db.getSubkeys("testSetSubkey"); assertTrue(list != null); - assertTrue(list.get(0).equals("subkey")); + assertTrue(list.get(0).equals("testSetSubkey_subkey")); } catch (IOException e) { System.out.println("IOException " + e.getMessage()); assertFalse(true); @@ -447,14 +468,14 @@ public void testSetSubkey() { /** K2hash setSubkeys */ @Test public void testSetSubkeys() { - try (K2hash db = K2hash.of(FILEDB)) { - String[] subkeys = {"subkey1", "subkey2"}; - assertTrue(db.setSubkeys("key", subkeys)); - List list = db.getSubkeys("key"); + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { + String[] subkeys = {"testSetSubkeys_subkey1", "testSetSubkeys_subkey2"}; + assertTrue(db.setSubkeys("testSetSubkeys", subkeys)); + List list = db.getSubkeys("testSetSubkeys"); assertTrue(list != null); assertTrue(list.size() == 2); - assertTrue(list.get(0).equals("subkey1")); - assertTrue(list.get(1).equals("subkey2")); + assertTrue(list.get(0).equals("testSetSubkeys_subkey1")); + assertTrue(list.get(1).equals("testSetSubkeys_subkey2")); } catch (IOException e) { System.out.println("IOException " + e.getMessage()); assertFalse(true); @@ -464,12 +485,12 @@ public void testSetSubkeys() { /** K2hash addSubkey */ @Test public void testAddSubkey() { - try (K2hash db = K2hash.of(FILEDB)) { - assertTrue(db.addSubkey("key", "subkey")); - List list = db.getSubkeys("key"); + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { + assertTrue(db.addSubkey("testAddSubkey", "testAddSubkey_subkey")); + List list = db.getSubkeys("testAddSubkey"); assertTrue(list != null); assertTrue(list.size() == 1); - assertTrue(list.get(0).equals("subkey")); + assertTrue(list.get(0).equals("testAddSubkey_subkey")); } catch (IOException e) { System.out.println("IOException " + e.getMessage()); assertFalse(true); @@ -479,14 +500,14 @@ public void testAddSubkey() { /** K2hash addSubkeys */ @Test public void testAddSubkeys() { - try (K2hash db = K2hash.of(FILEDB)) { - String[] subkeys = {"subkey1", "subkey2"}; - assertTrue(db.addSubkeys("key", subkeys)); - List list = db.getSubkeys("key"); + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { + String[] subkeys = {"testAddSubkeys_subkey1", "testAddSubkeys_subkey2"}; + assertTrue(db.addSubkeys("testAddSubkeys", subkeys)); + List list = db.getSubkeys("testAddSubkeys"); assertTrue(list != null); assertTrue(list.size() == 2); - assertTrue(list.get(0).equals("subkey1")); - assertTrue(list.get(1).equals("subkey2")); + assertTrue(list.get(0).equals("testAddSubkeys_subkey1")); + assertTrue(list.get(1).equals("testAddSubkeys_subkey2")); } catch (IOException e) { System.out.println("IOException " + e.getMessage()); assertFalse(true); @@ -496,15 +517,14 @@ public void testAddSubkeys() { /** K2hash getAttributes */ @Test public void testGetAttributes() { - try (K2hash db = K2hash.of(FILEDB)) { - assertTrue(db.setValue("key", "value")); - Map val = db.getAttributes("key"); + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { + assertTrue(db.setValue("testGetAttributes", "value")); + Map val = db.getAttributes("testGetAttributes"); assertTrue(val == null); - assertTrue(db.setAttribute("key", "attrname", "attrval")); - val = db.getAttributes("key"); - assertTrue(val != null); - assertTrue(val.containsKey("attrname")); - assertTrue(val.containsValue("attrval")); + assertTrue(db.setAttribute("testGetAttributes", "testGetAttributes_attrname", "attrval")); + Map val2 = db.getAttributes("testGetAttributes"); + assertTrue(val2.containsKey("testGetAttributes_attrname")); + assertTrue(val2.containsValue("attrval")); } catch (IOException e) { System.out.println("IOException " + e.getMessage()); assertFalse(true); @@ -514,12 +534,12 @@ public void testGetAttributes() { /** K2hash getAttribute */ @Test public void testGetAttribute() { - try (K2hash db = K2hash.of(FILEDB)) { - assertTrue(db.setValue("key", "value")); - String val = db.getAttribute("key", "attrname"); + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { + assertTrue(db.setValue("testGetAttribute", "value")); + String val = db.getAttribute("testGetAttribute", "testGetAttribute_attrname"); assertTrue(val == null); - assertTrue(db.setAttribute("key", "attrname", "attrval")); - String attrVal = db.getAttribute("key", "attrname"); + assertTrue(db.setAttribute("testGetAttribute", "testGetAttribute_attrname", "attrval")); + String attrVal = db.getAttribute("testGetAttribute", "testGetAttribute_attrname"); assertTrue(attrVal != null); assertTrue(attrVal.equals("attrval")); } catch (IOException e) { @@ -531,9 +551,9 @@ public void testGetAttribute() { /** K2hash setValue */ @Test public void testSetValueArg2() { - try (K2hash db = K2hash.of(FILEDB)) { - assertTrue(db.setValue("key", "value")); - assertTrue(db.getValue("key").equals("value")); + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { + assertTrue(db.setValue("testSetValueArg2", "value")); + assertTrue(db.getValue("testSetValueArg2").equals("value")); } catch (IOException e) { System.out.println("IOException " + e.getMessage()); assertFalse(true); @@ -543,15 +563,15 @@ public void testSetValueArg2() { /** K2hash setValue */ @Test public void testSetValueArg5() { - try (K2hash db = K2hash.of(FILEDB)) { - assertTrue(db.setValue("key", "value", "password", 3, TimeUnit.SECONDS)); + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { + assertTrue(db.setValue("testSetValueArg5", "value", "password", 3, TimeUnit.SECONDS)); try { Thread.sleep(5000); } catch (InterruptedException e) { System.out.println("InterruptedException " + e.getMessage()); assertFalse(true); } - assertTrue(db.getValue("key") == null); + assertTrue(db.getValue("testSetValueArg5") == null); } catch (IOException e) { System.out.println("IOException " + e.getMessage()); assertFalse(true); @@ -561,10 +581,10 @@ public void testSetValueArg5() { /** K2hash setAttribute */ @Test public void testSetAttribute() { - try (K2hash db = K2hash.of(FILEDB)) { - assertTrue(db.setValue("key", "value")); - assertTrue(db.setAttribute("key", "attrname", "attrval")); - // assertTrue(db.getAttribute("key", "attrname").equals("attrval")); + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { + assertTrue(db.setValue("testSetAttribute", "value")); + assertTrue(db.setAttribute("testSetAttribute", "attrname", "attrval")); + assertTrue(db.getAttribute("testSetAttribute", "attrname").equals("attrval")); } catch (IOException e) { System.out.println("IOException " + e.getMessage()); assertFalse(true); @@ -574,11 +594,11 @@ public void testSetAttribute() { /** K2hash remove */ @Test public void testRemoveArg1() { - try (K2hash db = K2hash.of(FILEDB)) { - assertTrue(db.setValue("key", "val")); - assertTrue(db.getValue("key") != null); - assertTrue(db.remove("key")); - assertTrue(db.getValue("key") == null); + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { + assertTrue(db.setValue("testRemoveArg1", "val")); + assertTrue(db.getValue("testRemoveArg1") != null); + assertTrue(db.remove("testRemoveArg1")); + assertTrue(db.getValue("testRemoveArg1") == null); } catch (IOException e) { System.out.println("IOException " + e.getMessage()); assertFalse(true); @@ -588,24 +608,24 @@ public void testRemoveArg1() { /** K2hash remove */ @Test public void testRemoveArg2_1() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { /* 1. setvalue */ - assertTrue(db.setValue("key", "val")); - assertTrue(db.setValue("subkey1", "subval1")); - assertTrue(db.setValue("subkey2", "subval2")); - String[] subkeys = {"subkey1", "subkey2"}; - assertTrue(db.addSubkeys("key", subkeys)); + assertTrue(db.setValue("testRemoveArg2_1", "val")); + assertTrue(db.setValue("testRemoveArg2_1_subkey1", "subval1")); + assertTrue(db.setValue("testRemoveArg2_1_subkey2", "subval2")); + String[] subkeys = {"testRemoveArg2_1_subkey1", "testRemoveArg2_1_subkey2"}; + assertTrue(db.addSubkeys("testRemoveArg2_1", subkeys)); /* 2. remove subkey1 */ - assertTrue(db.remove("key", "subkey1")); + assertTrue(db.remove("testRemoveArg2_1", "testRemoveArg2_1_subkey1")); // make sure subkey1 itself is removed whe subkey1 is removed from subkeys - List list = db.getSubkeys("key"); + List list = db.getSubkeys("testRemoveArg2_1"); // TODO core dump here assertTrue(list != null); assertTrue(list.size() == 1); - assertTrue(list.get(0).equals("subkey2")); - assertTrue(db.getValue("key") != null); - assertTrue(db.getValue("subkey1") == null); - assertTrue(db.getValue("subkey2") != null); + assertTrue(list.get(0).equals("testRemoveArg2_1_subkey2")); + assertTrue(db.getValue("testRemoveArg2_1") != null); + assertTrue(db.getValue("testRemoveArg2_1_subkey1") == null); + assertTrue(db.getValue("testRemoveArg2_1_subkey2") != null); } catch (IOException e) { System.out.println("IOException " + e.getMessage()); assertFalse(true); @@ -615,16 +635,16 @@ public void testRemoveArg2_1() { /** K2hash remove */ @Test public void testRemoveArg2_2() { - try (K2hash db = K2hash.of(FILEDB)) { - assertTrue(db.setValue("key", "val")); - assertTrue(db.setValue("subkey1", "subval1")); - assertTrue(db.setValue("subkey2", "subval2")); - String[] subkeys = {"subkey1", "subkey2"}; - assertTrue(db.addSubkeys("key", subkeys)); - assertTrue(db.remove("key", true)); - assertTrue(db.getValue("key") == null); - assertTrue(db.getValue("subkey1") == null); - assertTrue(db.getValue("subkey2") == null); + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { + assertTrue(db.setValue("testRemoveArg2_2", "val")); + assertTrue(db.setValue("testRemoveArg2_2_subkey1", "subval1")); + assertTrue(db.setValue("testRemoveArg2_2_subkey2", "subval2")); + String[] subkeys = {"testRemoveArg2_2_subkey1", "testRemoveArg2_2_subkey2"}; + assertTrue(db.addSubkeys("testRemoveArg2_2", subkeys)); + assertTrue(db.remove("testRemoveArg2_2", true)); + assertTrue(db.getValue("testRemoveArg2_2") == null); + assertTrue(db.getValue("testRemoveArg2_2_subkey1") == null); + assertTrue(db.getValue("testRemoveArg2_2_subkey2") == null); } catch (IOException e) { System.out.println("IOException " + e.getMessage()); assertFalse(true); @@ -634,23 +654,28 @@ public void testRemoveArg2_2() { /** K2hash rename */ @Test public void testRename() { - try (K2hash db = K2hash.of(FILEDB)) { - assertTrue(db.setValue("key", "val")); - assertTrue(db.rename("key", "newkey")); - assertTrue(db.getValue("key") == null); - assertTrue(db.getValue("newkey") != null); - assertTrue(db.getValue("newkey").equals("val")); + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { + assertTrue(db.setValue("testRename", "val")); + assertTrue(db.rename("testRename", "testRename_newkey")); + assertTrue(db.getValue("testRename") == null); + assertTrue(db.getValue("testRename_newkey") != null); + assertTrue(db.getValue("testRename_newkey").equals("val")); } catch (IOException e) { System.out.println("IOException " + e.getMessage()); assertFalse(true); } } - /** K2hash printTableStats */ - @Disabled + /** + * K2hash printTableStats + * + *

NOTE: This test is disabled because k2hash library corrupted the channel used by the plugin + * in order to transmit events with test status back to Maven process. + */ @Test + @Disabled public void testPrintTableStatsArg1() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { db.printTableStats(K2hash.STATS_DUMP_LEVEL.HEADER); assertTrue(true); } catch (IOException e) { @@ -659,11 +684,16 @@ public void testPrintTableStatsArg1() { } } - /** K2hash printDataStats */ - @Disabled + /** + * K2hash printDataStats + * + *

NOTE: This test is disabled because k2hash library corrupted the channel used by the plugin + * in order to transmit events with test status back to Maven process. + */ @Test + @Disabled public void testPrintDataStats() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { db.printDataStats(); assertTrue(true); } catch (IOException e) { @@ -672,11 +702,16 @@ public void testPrintDataStats() { } } - /** K2hash version */ - @Disabled + /** + * K2hash version + * + *

NOTE: This test is disabled because k2hash library corrupted the channel used by the plugin + * in order to transmit events with test status back to Maven process. + */ @Test + @Disabled public void testVersion() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { db.version(); assertTrue(true); } catch (IOException e) { @@ -685,3 +720,12 @@ public void testVersion() { } } } +// +// Local variables: +// tab-width: 2 +// c-basic-offset: 2 +// indent-tabs-mode: nil +// End: +// vim600: noexpandtab sw=2 ts=2 fdm=marker +// vim<600: noexpandtab sw=2 ts=2 +// diff --git a/src/test/java/ax/antpick/k2hash/K2hashKeyQueueTest.java b/k2hash/src/test/java/K2hashKeyQueueTest.java similarity index 79% rename from src/test/java/ax/antpick/k2hash/K2hashKeyQueueTest.java rename to k2hash/src/test/java/K2hashKeyQueueTest.java index 888c70e..d39716f 100644 --- a/src/test/java/ax/antpick/k2hash/K2hashKeyQueueTest.java +++ b/k2hash/src/test/java/K2hashKeyQueueTest.java @@ -36,9 +36,10 @@ import java.util.HashMap; import java.util.Map; import java.util.NoSuchElementException; - import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -65,8 +66,9 @@ public void tearDown() { } /** K2hashKeyQueue Constructor */ + @Test public void testOfArg1() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { long handle = db.getHandle(); K2hashKeyQueue queue = K2hashKeyQueue.of(handle); assertTrue(queue.getQueueHandle() > K2hashKeyQueue.K2H_INVALID_HANDLE); @@ -77,10 +79,11 @@ public void testOfArg1() { } /** K2hashKeyQueue Constructor */ + @Test public void testOfArg2() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { long handle = db.getHandle(); - K2hashKeyQueue queue = K2hashKeyQueue.of(handle, true, "prefix"); + K2hashKeyQueue queue = K2hashKeyQueue.of(handle, true, "testOfArg2"); assertTrue(queue.getQueueHandle() > K2hashKeyQueue.K2H_INVALID_HANDLE); } catch (IOException e) { System.out.println("IOException " + e.getMessage()); @@ -89,10 +92,11 @@ public void testOfArg2() { } /** K2hashKeyQueue Constructor */ + @Test public void testOfArg3() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { long handle = db.getHandle(); - K2hashKeyQueue queue = K2hashKeyQueue.of(handle, true, "prefix"); + K2hashKeyQueue queue = K2hashKeyQueue.of(handle, true, "testOfArg3"); assertTrue(queue.getQueueHandle() > K2hashKeyQueue.K2H_INVALID_HANDLE); } catch (IOException e) { System.out.println("IOException " + e.getMessage()); @@ -101,10 +105,11 @@ public void testOfArg3() { } /** K2hashKeyQueue isEmpty */ + @Test public void testEmpty() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { long handle = db.getHandle(); - K2hashKeyQueue queue = K2hashKeyQueue.of(handle, true, "prefix"); + K2hashKeyQueue queue = K2hashKeyQueue.of(handle, true, "testEmpty"); // 1. assure it's empty boolean isEmpty = queue.isEmpty(); assertTrue(isEmpty); @@ -122,10 +127,11 @@ public void testEmpty() { } /** K2hashKeyQueue count */ + @Test public void testCount() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { long handle = db.getHandle(); - K2hashKeyQueue queue = K2hashKeyQueue.of(handle, true, "prefix"); + K2hashKeyQueue queue = K2hashKeyQueue.of(handle, true, "testCount"); // 1. assure it's zero count long size = queue.count(); assertTrue(size == 0); @@ -143,10 +149,11 @@ public void testCount() { } /** K2hashKeyQueue peek */ + @Test public void testPeek() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { long handle = db.getHandle(); - K2hashKeyQueue queue = K2hashKeyQueue.of(handle, true, "prefix"); + K2hashKeyQueue queue = K2hashKeyQueue.of(handle, true, "testPeek"); // 1. assure it's null Map data = queue.peek(); assertTrue(data == null); // null if queue is empty. @@ -164,10 +171,11 @@ public void testPeek() { } /** K2hashKeyQueue peek */ + @Test public void testPeekArg2() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { long handle = db.getHandle(); - K2hashKeyQueue queue = K2hashKeyQueue.of(handle, true, "prefix"); + K2hashKeyQueue queue = K2hashKeyQueue.of(handle, true, "testPeekArg2"); // 1. assure it's null Map data = queue.peek(0); assertTrue(data == null); // null if queue is empty. @@ -188,10 +196,11 @@ public void testPeekArg2() { } /** K2hashKeyQueue offer */ + @Test public void testOfferArg1() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { long handle = db.getHandle(); - K2hashKeyQueue queue = K2hashKeyQueue.of(handle, true, "prefix"); + K2hashKeyQueue queue = K2hashKeyQueue.of(handle, true, "testOfferArg1"); Map data = new HashMap<>(); data.put("key", "val"); // 1. assure it's true @@ -209,10 +218,11 @@ public void testOfferArg1() { } /** K2hashKeyQueue offer */ + @Test public void testOfferArg4() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { long handle = db.getHandle(); - K2hashKeyQueue queue = K2hashKeyQueue.of(handle, true, "prefix", "pass", 10L); + K2hashKeyQueue queue = K2hashKeyQueue.of(handle, true, "testOfferArg4", "pass", 10L); Map data = new HashMap<>(); data.put("key", "val"); // 1. assure it's true @@ -230,10 +240,11 @@ public void testOfferArg4() { } /** K2hashKeyQueue poll */ + @Test public void testPoll() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { long handle = db.getHandle(); - K2hashKeyQueue queue = K2hashKeyQueue.of(handle, true, "prefix"); + K2hashKeyQueue queue = K2hashKeyQueue.of(handle, true, "testPoll"); // 1. assure it's null Map val = queue.poll(); assertTrue(val == null); @@ -258,12 +269,17 @@ public void testPoll() { } /** K2hashKeyQueue poll */ + @Test public void testPollArgSleep() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { long handle = db.getHandle(); K2hashKeyQueue queue = K2hashKeyQueue.of( - handle, true, "prefix", "pass", K2hashKeyQueue.DEFAULT_EXPIRATION_DURATION + 3); + handle, + true, + "testPollArgSleep", + "pass", + K2hashKeyQueue.DEFAULT_EXPIRATION_DURATION + 3); // 1. assure it's null Map val = queue.poll(); @@ -288,18 +304,18 @@ public void testPollArgSleep() { } /** K2hashKeyQueue add */ + @Test public void testAddArg1() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { long handle = db.getHandle(); - K2hashKeyQueue queue = K2hashKeyQueue.of(handle, false, "prefix"); + K2hashKeyQueue queue = K2hashKeyQueue.of(handle, false, "testAddArg1"); Map data = new HashMap<>(); data.put("key", "val"); // 1. assure it's true boolean isSuccess = queue.add(data); assertTrue(isSuccess); // 2. assure it's not null - Map data2 = null; - data2 = queue.poll(); + Map data2 = queue.poll(); assertTrue(data2 != null); assertTrue(data2.containsKey("key")); assertTrue(data2.containsValue("val")); @@ -310,17 +326,17 @@ public void testAddArg1() { } /** K2hashKeyQueue add */ + @Test public void testAddArg4() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { long handle = db.getHandle(); - K2hashKeyQueue queue = K2hashKeyQueue.of(handle, false, "prefix", "pass", 10L); + K2hashKeyQueue queue = K2hashKeyQueue.of(handle, false, "testAddArg4", "pass", 10L); Map data = new HashMap<>(); data.put("key", "val"); boolean isSuccess = queue.add(data); assertTrue(isSuccess); // 2. assure it's not null - Map data2 = null; - data2 = queue.poll(); + Map data2 = queue.poll(); assertTrue(data2 != null); assertTrue(data2.containsKey("key")); assertTrue(data2.containsValue("val")); @@ -331,10 +347,11 @@ public void testAddArg4() { } /** K2hashKeyQueue remove */ + @Test public void testRemoveArg() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { long handle = db.getHandle(); - K2hashKeyQueue queue = K2hashKeyQueue.of(handle, false, "prefix"); + K2hashKeyQueue queue = K2hashKeyQueue.of(handle, false, "testRemoveArg"); Map data = new HashMap<>(); data.put("key", "val"); boolean isSuccess = queue.add(data); @@ -351,11 +368,12 @@ public void testRemoveArg() { } /** K2hashKeyQueue remove */ + @Test public void testRemoveArgException() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { long handle = db.getHandle(); - K2hashKeyQueue queue = K2hashKeyQueue.of(handle, false, "prefix"); - Map data = queue.remove(); + K2hashKeyQueue queue = K2hashKeyQueue.of(handle, false, "testRemoveArgException"); + queue.remove(); System.out.println("NoSuchElementException should be thrown"); assertTrue(false); } catch (IOException e) { @@ -367,10 +385,11 @@ public void testRemoveArgException() { } /** K2hashKeyQueue remove */ + @Test public void testRemoveArg1() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { long handle = db.getHandle(); - K2hashKeyQueue queue = K2hashKeyQueue.of(handle, false, "prefix"); + K2hashKeyQueue queue = K2hashKeyQueue.of(handle, false, "testRemoveArg1"); Map data = new HashMap<>(); data.put("key", "val"); boolean isSuccess = queue.remove(data); @@ -382,10 +401,12 @@ public void testRemoveArg1() { } /** K2hashKeyQueue print */ + @Test + @Disabled public void testPrint() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { long handle = db.getHandle(); - K2hashKeyQueue queue = K2hashKeyQueue.of(handle, false, "prefix"); + K2hashKeyQueue queue = K2hashKeyQueue.of(handle, false, "testPrint"); queue.print(); assertTrue(true); } catch (IOException e) { @@ -394,3 +415,12 @@ public void testPrint() { } } } +// +// Local variables: +// tab-width: 2 +// c-basic-offset: 2 +// indent-tabs-mode: nil +// End: +// vim600: noexpandtab sw=2 ts=2 fdm=marker +// vim<600: noexpandtab sw=2 ts=2 +// diff --git a/src/test/java/ax/antpick/k2hash/K2hashQueueTest.java b/k2hash/src/test/java/K2hashQueueTest.java similarity index 79% rename from src/test/java/ax/antpick/k2hash/K2hashQueueTest.java rename to k2hash/src/test/java/K2hashQueueTest.java index 62031e8..8b97352 100644 --- a/src/test/java/ax/antpick/k2hash/K2hashQueueTest.java +++ b/k2hash/src/test/java/K2hashQueueTest.java @@ -34,9 +34,10 @@ import java.io.File; import java.io.IOException; import java.util.NoSuchElementException; - import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -63,8 +64,9 @@ public void tearDown() { } /** K2hashQueue Constructor */ + @Test public void testOfArg1() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { long handle = db.getHandle(); K2hashQueue queue = K2hashQueue.of(handle); assertTrue(queue.getQueueHandle() > K2hashQueue.K2H_INVALID_HANDLE); @@ -75,10 +77,11 @@ public void testOfArg1() { } /** K2hashQueue Constructor */ + @Test public void testOfArg3() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { long handle = db.getHandle(); - K2hashQueue queue = K2hashQueue.of(handle, true, "prefix"); + K2hashQueue queue = K2hashQueue.of(handle, true, "testOfArg3"); /* do nothing */ assertTrue(queue.getQueueHandle() > K2hashQueue.K2H_INVALID_HANDLE); } catch (IOException e) { @@ -88,10 +91,11 @@ public void testOfArg3() { } /** K2hashQueue isEmpty */ + @Test public void testEmpty() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { long handle = db.getHandle(); - K2hashQueue queue = K2hashQueue.of(handle, true, "prefix"); + K2hashQueue queue = K2hashQueue.of(handle, true, "testEmpty"); // 1. assure it's empty boolean isEmpty = queue.isEmpty(); assertTrue(isEmpty); @@ -108,10 +112,11 @@ public void testEmpty() { } /** K2hashQueue count */ + @Test public void testCount() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { long handle = db.getHandle(); - K2hashQueue queue = K2hashQueue.of(handle, true, "prefix"); + K2hashQueue queue = K2hashQueue.of(handle, true, "testCount"); long size = queue.count(); assertTrue(size == 0); // 2. assure it's 1 count @@ -127,10 +132,11 @@ public void testCount() { } /** K2hashQueue peek */ + @Test public void testPeek() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { long handle = db.getHandle(); - K2hashQueue queue = K2hashQueue.of(handle, true, "prefix"); + K2hashQueue queue = K2hashQueue.of(handle, true, "testPeek"); String data = queue.peek(); assertTrue(data == null); // null if queue is empty. // 2. assure it's not null @@ -150,10 +156,11 @@ public void testPeek() { } /** K2hashQueue offer */ + @Test public void testOfferArg1() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { long handle = db.getHandle(); - K2hashQueue queue = K2hashQueue.of(handle, true, "prefix"); + K2hashQueue queue = K2hashQueue.of(handle, true, "testOfferArg1"); boolean isSuccess = queue.offer("val"); assertTrue(isSuccess); // 2. assure it's not null @@ -167,10 +174,11 @@ public void testOfferArg1() { } /** K2hashQueue offer */ + @Test public void testOfferArg4() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { long handle = db.getHandle(); - K2hashQueue queue = K2hashQueue.of(handle, true, "prefix", "pass", 10L); + K2hashQueue queue = K2hashQueue.of(handle, true, "testOfferArg4", "pass", 10L); boolean isSuccess = queue.offer("val"); assertTrue(isSuccess); // 2. assure it's not null @@ -184,10 +192,11 @@ public void testOfferArg4() { } /** K2hashQueue poll */ + @Test public void testPoll() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { long handle = db.getHandle(); - K2hashQueue queue = K2hashQueue.of(handle, true, "prefix"); + K2hashQueue queue = K2hashQueue.of(handle, true, "testPoll"); String val = queue.poll(); assertTrue(val == null); @@ -209,12 +218,17 @@ public void testPoll() { } /** K2hashQueue poll */ + @Test public void testPollArgSleep() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { long handle = db.getHandle(); K2hashQueue queue = K2hashQueue.of( - handle, true, "prefix", "pass", K2hashQueue.DEFAULT_EXPIRATION_DURATION + 3); + handle, + true, + "testPollArgSleep", + "pass", + K2hashQueue.DEFAULT_EXPIRATION_DURATION + 3); // 1. assure it's null String val = queue.poll(); @@ -238,10 +252,11 @@ public void testPollArgSleep() { } /** K2hashQueue add */ + @Test public void testAddArg1() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { long handle = db.getHandle(); - K2hashQueue queue = K2hashQueue.of(handle, true, "prefix"); + K2hashQueue queue = K2hashQueue.of(handle, true, "testAddArg1"); boolean isSuccess = queue.add("val"); assertTrue(isSuccess); // 2. assure it's not null @@ -256,10 +271,11 @@ public void testAddArg1() { } /** K2hashQueue add */ + @Test public void testAddArg4() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { long handle = db.getHandle(); - K2hashQueue queue = K2hashQueue.of(handle, true, "prefix", "pass", 10L); + K2hashQueue queue = K2hashQueue.of(handle, true, "testAddArg4", "pass", 10L); boolean isSuccess = queue.add("val"); assertTrue(isSuccess); // 2. assure it's not null @@ -274,10 +290,11 @@ public void testAddArg4() { } /** K2hashQueue remove */ + @Test public void testRemoveArg() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { long handle = db.getHandle(); - K2hashQueue queue = K2hashQueue.of(handle, true, "prefix"); + K2hashQueue queue = K2hashQueue.of(handle, true, "testRemoveArg"); String data = "val"; boolean isSuccess = queue.add(data); assertTrue(isSuccess); @@ -292,10 +309,11 @@ public void testRemoveArg() { } /** K2hashQueue remove */ + @Test public void testRemoveArgException() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { long handle = db.getHandle(); - K2hashQueue queue = K2hashQueue.of(handle, true, "prefix"); + K2hashQueue queue = K2hashQueue.of(handle, true, "testRemoveArgException"); String val = queue.remove(); System.out.println("NoSuchElementException should be thrown"); assertFalse(true); @@ -308,10 +326,12 @@ public void testRemoveArgException() { } /** K2hashQueue print */ + @Test + @Disabled public void testPrint() { - try (K2hash db = K2hash.of(FILEDB)) { + try (K2hash db = K2hash.of(K2hash.OPEN_MODE.MEM)) { long handle = db.getHandle(); - K2hashQueue queue = K2hashQueue.of(handle, true, "prefix"); + K2hashQueue queue = K2hashQueue.of(handle, true, "testPrint"); queue.print(); assertTrue(true); } catch (IOException e) { @@ -320,3 +340,12 @@ public void testPrint() { } } } +// +// Local variables: +// tab-width: 2 +// c-basic-offset: 2 +// indent-tabs-mode: nil +// End: +// vim600: noexpandtab sw=2 ts=2 fdm=marker +// vim<600: noexpandtab sw=2 ts=2 +// diff --git a/src/test/resources/logback-test.xml b/k2hash/src/test/resources/logback-test.xml similarity index 100% rename from src/test/resources/logback-test.xml rename to k2hash/src/test/resources/logback-test.xml diff --git a/lib/install.sh b/lib/install.sh deleted file mode 100755 index d0368a1..0000000 --- a/lib/install.sh +++ /dev/null @@ -1,244 +0,0 @@ -#!/bin/sh -# -# The MIT License -# -# Copyright 2018 Yahoo Japan Corporation. -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -# THE SOFTWARE. -# -# AUTHOR: Hirotaka Wakabayashi -# CREATE: Fri, 14 Sep 2018 -# REVISION: -# - -# Sets the default locale. LC_ALL has precedence over other LC* variables. -unset LANG -unset LANGUAGE -LC_ALL=en_US.utf8 -export LC_ALL - -# Sets PATH. setup_*.sh uses useradd command -PATH=${PATH}:/usr/sbin:/sbin - -# an unset parameter expansion will fail -set -u - -# umask 022 is enough -umask 022 - -# environments -DEBUG=1 -if test "${DEBUG}" -eq 1; then - TAG="$(basename $0) -s" -else - TAG=$(basename $0) -fi -USER=$(whoami) -LOGLEVEL=info - -# Checks if k2hash is installed -# -# Params:: -# no params -# -# Returns:: -# 0 on installed -# 1 on not installed -# -which_k2hash() { - which k2hlinetool >/dev/null 2>&1 - if test "${?}" != "0"; then - logger -t ${TAG} -p user.error "no k2hlinetool installed" - return 1 - fi - return 0 -} - -# Determines the current OS -# -# Params:: -# no params -# -# Returns:: -# 0 on success -# 1 on failure -# -setup_os_env() { - if test -f "/etc/os-release"; then - . /etc/os-release - OS_NAME=$ID - OS_VERSION=$VERSION_ID - else - logger -t ${TAG} -p user.warn "unknown OS, no /etc/os-release and /etc/centos-release falling back to CentOS-7" - OS_NAME=centos - OS_VERSION=7 - fi - - if test "${OS_NAME}" = "ubuntu"; then - logger -t ${TAG} -p user.notice "ubuntu configurations are currently equal to debian one" - OS_NAME=debian - elif test "${OS_NAME}" = "centos"; then - if test "${OS_VERSION}" != "7"; then - logger -t ${TAG} -p user.err "centos7 only currently supported, not ${OS_NAME} ${OS_VERSION}" - exit 1 - fi - fi - - HOSTNAME=$(hostname) - logger -t ${TAG} -p user.debug "HOSTNAME=${HOSTNAME} OS_NAME=${OS_NAME} OS_VERSION=${OS_VERSION}" -} - -# Builds k2hash from source code -# -# Params:: -# $1 os_name -# -# Returns:: -# 0 on success -# 1 on failure(exit) -# -make_k2hash() { - - _os_name=${1:?"os_name should be nonzero"} - - if test "${_os_name}" = "debian" -o "${_os_name}" = "ubuntu"; then - _configure_opt="--with-gnutls" - sudo apt-get update -y - sudo apt-get install -y git curl autoconf autotools-dev gcc g++ make gdb libtool pkg-config libyaml-dev libgnutls28-dev - elif test "${_os_name}" = "fedora"; then - _configure_opt="--with-nss" - sudo dnf install -y git curl autoconf automake gcc gcc-c++ gdb make libtool pkgconfig libyaml-devel nss-devel - elif test "${_os_name}" = "centos" -o "${_os_name}" = "rhel"; then - _configure_opt="--with-nss" - sudo yum install -y git curl autoconf automake gcc gcc-c++ gdb make libtool pkgconfig libyaml-devel nss-devel - else - logger -t ${TAG} -p user.error "OS should be debian, ubuntu, fedora, centos or rhel" - exit 1 - fi - - logger -t ${TAG} -p user.debug "git clone https://github.com/yahoojapan/k2hash" - git clone https://github.com/yahoojapan/k2hash; cd k2hash - - logger -t ${TAG} -p user.debug "git clone https://github.com/yahoojapan/fullock" - git clone https://github.com/yahoojapan/fullock; cd fullock - - ./autogen.sh - ./configure --prefix=/usr - make - sudo make install - - cd .. - ./autogen.sh - ./configure --prefix=/usr ${_configure_opt} - make - sudo make install - - return 0 -} - -# -# main loop -# - -setup_os_env - -which_k2hash -if test "${?}" = "0"; then - logger -t ${TAG} -p user.info "k2hash installed." - exit 0 -fi - -if test "${OS_NAME}" = "fedora"; then - which java - if test "${?}" = "1"; then - sudo dnf install -y java-latest-openjdk - fi - which mvn - if test "${?}" = "1"; then - sudo dnf install -y maven - fi - which bc - if test "${?}" = "1"; then - sudo dnf install -y bc - fi - if test "${OS_VERSION}" = "28"; then - sudo dnf install -y curl - curl -s https://packagecloud.io/install/repositories/antpickax/stable/script.rpm.sh | sudo bash - sudo dnf install k2hash-devel -y - elif test "${OS_VERSION}" = "29"; then - sudo dnf install -y curl - curl -s https://packagecloud.io/install/repositories/antpickax/current/script.rpm.sh | sudo bash - sudo dnf install k2hash-devel -y - else - make_k2hash ${OS_NAME} - fi -elif test "${OS_NAME}" = "debian" -o "${OS_NAME}" = "ubuntu"; then - which java - if test "${?}" = "1"; then - sudo apt-get install -y openjdk-8-jdk - fi - which mvn - if test "${?}" = "1"; then - sudo apt-get install -y maven - fi - which bc - if test "${?}" = "1"; then - sudo apt-get install -y bc - fi - if test "${OS_VERSION}" = "9"; then - sudo apt-get install -y curl - curl -s https://packagecloud.io/install/repositories/antpickax/stable/script.deb.sh | sudo bash - sudo apt-get install -y k2hash-dev - else - make_k2hash ${OS_NAME} - fi -elif test "${OS_NAME}" = "centos" -o "${OS_NAME}" = "rhel"; then - which java - if test "${?}" = "1"; then - sudo yum install -y java-1.8.0-openjdk - fi - which mvn - if test "${?}" = "1"; then - sudo yum install -y maven - fi - which bc - if test "${?}" = "1"; then - sudo yum install -y bc - fi - sudo yum install -y k2hash-devel - if test "${?}" != "0"; then - sudo yum install -y curl - curl -s https://packagecloud.io/install/repositories/antpickax/stable/script.rpm.sh | sudo bash - sudo yum install -y k2hash-devel - if test "${?}" != "0"; then - make_k2hash ${OS_NAME} - fi - fi -else - logger -t ${TAG} -p user.error "OS must be either fedora or centos or debian or ubuntu, not ${OS_NAME}" - exit 1 -fi - -which_k2hash -if test "${?}" = "0"; then - logger -t ${TAG} -p user.info "k2hash installed." - exit 0 -fi - -exit 0 diff --git a/pom.xml b/pom.xml index b45a2c6..799103f 100644 --- a/pom.xml +++ b/pom.xml @@ -2,14 +2,17 @@ xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd"> 4.0.0 ax.antpick - k2hash - jar - - 1.0.4 + pom + k2hash-parent k2hash + 1.0.6 An official java driver for k2hash, which is a highly available and scalable distributed KVS library. https://github.com/yahoojapan/k2hash_java + + k2hash + + The MIT License @@ -27,7 +30,7 @@ Takeshi Nakatani - ggtakec@gmail.com + nakatani@yahoo-corp.jp Yahoo Japan Corporation https://www.yahoo.co.jp @@ -63,31 +66,37 @@ + true + lines,vars,source + true + true UTF-8 github 3.3.0 - 5.9.0 - 5.9.0 - 1.3.0-alpha10 - 1.3.0-alpha10 - 5.8.1 - 3.28.0-GA - 3.3.0 - 3.1.2 - 3.8.1 + 5.13.0 + 5.13.0 + 1.3.11 + 1.3.11 + 5.10.0 + 3.29.2-GA + 3.6.0 + 3.3.0 + 3.11.0 - 11 - 3.0.1 - 3.3.1 - 3.1.2 - 3.9.1 - 3.2.1 - 2.22.2 - 3.0.0 - >4.4.1 - 2.8.1 + 3.1.0 + 3.6.0 + 3.4.5 + 4.0.0-M9 + 3.3.0 + 3.1.2 + 3.1.2 + 3.1.0 + 2.16.0 0.12 + 3.6.1 + 5.10.0 + checkstyle.xml @@ -112,9 +121,10 @@ ${logback-classic.version} - org.junit.jupiter - junit-jupiter-engine - test + org.junit.jupiter + junit-jupiter-api + ${junit-jupiter-api.version} + test org.javassist @@ -146,57 +156,6 @@ maven-compiler-plugin ${maven-compiler-plugin.version} - - org.apache.maven.plugins - maven-checkstyle-plugin - ${maven-checkstyle-plugin.version} - - checkstyle.xml - UTF-8 - true - true - false - - - - validate - validate - - check - - - - - - org.apache.maven.plugins - maven-surefire-plugin - ${maven-surefire-plugin.version} - - -Xmx2048m - -Xms2048m - - - - org.openclover - clover-maven-plugin - 4.4.1 - - - org.codehaus.mojo - exec-maven-plugin - ${exec-maven-plugin.version} - - - Configures local k2hash filedb for test - - exec - - - - - lib/install.sh - - org.apache.maven.plugins maven-site-plugin @@ -232,86 +191,6 @@ - - - example - - - cli-test - - - - - - maven-assembly-plugin - ${maven-assembly-plugin.version} - - - jar-with-dependencies - - - - ax.antpick.k2hash.example.App - - - - - - make-assembly - package - - single - - - - - - org.codehaus.mojo - exec-maven-plugin - ${exec-maven-plugin.version} - - - Executes an example command - - exec - - - - - ax.antpick.k2hash.example.App - - - - - - - upload - - - assets - - - - - - org.codehaus.mojo - exec-maven-plugin - ${exec-maven-plugin.version} - - - upload_jar - - exec - - - - - .github/workflows/upload.sh - - - - - release diff --git a/settings_github.xml b/settings_github.xml index 39dacd7..f62f9c1 100644 --- a/settings_github.xml +++ b/settings_github.xml @@ -7,10 +7,44 @@ github + + + github + + + central + https://repo1.maven.org/maven2 + true + false + + + github + GitHub yahoojapan Apache Maven Packages + https://maven.pkg.github.com/yahoojapan/k2hash_java + + + + + ossrh + + true + + + gpg2 + ${env.GNUPG_TOKEN} + + + + github ${env.GITPAGES_TOKEN} + + ossrh + Wakabayashi + ${env.SONATYPE_TOKEN} +