Skip to content

Commit

Permalink
switch to Kotlin serialization, migrate to kotest
Browse files Browse the repository at this point in the history
  • Loading branch information
sanity committed Sep 3, 2020
1 parent 56844e8 commit 359b4a3
Show file tree
Hide file tree
Showing 23 changed files with 578 additions and 608 deletions.
2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 0 additions & 8 deletions .idea/modules.xml

This file was deleted.

9 changes: 1 addition & 8 deletions .idea/shoebox.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 22 additions & 34 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
group 'kweb'
version '0.3.1'
version '0.4.0'

buildscript {
ext.kotlin_version = '1.3.72'
ext.dokka_version = '0.10.1'
ext.kotlin_version = '1.4.0'
ext.dokka_version = '1.4.0-rc'

repositories {
jcenter()
Expand All @@ -20,16 +20,16 @@ buildscript {
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'info.solidsoft.gradle.pitest:gradle-pitest-plugin:1.3.0'
classpath "org.jetbrains.dokka:dokka-gradle-plugin:$dokka_version"
classpath 'com.github.ben-manes:gradle-versions-plugin:0.20.0'
classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
}

}

apply plugin: 'kotlin'
apply plugin: 'org.jetbrains.kotlin.plugin.serialization'
apply plugin: 'org.jetbrains.dokka'
apply plugin: "info.solidsoft.pitest"
apply plugin: "com.github.ben-manes.versions"

repositories {
Expand All @@ -46,39 +46,27 @@ repositories {
}

dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
compile 'com.github.salomonbrys.kotson:kotson:2.5.0'
compile 'com.google.guava:guava:27.1-jre'
compile 'net.incongru.watchservice:barbary-watchservice:1.0'
compile 'com.fatboyindustrial.gson-javatime-serialisers:gson-javatime-serialisers:1.1.1'
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
implementation 'com.google.guava:guava:27.1-jre'
implementation 'net.incongru.watchservice:barbary-watchservice:1.0'
implementation 'org.lmdbjava:lmdbjava:0.7.0'
compile "org.jetbrains.kotlinx:kotlinx-serialization-core:1.0.0-RC"
implementation "org.jetbrains.kotlinx:kotlinx-serialization-protobuf:1.0.0-RC"

testCompile 'io.kotlintest:kotlintest:2.0.7'
}
/*
task dokkaJavadoc(type: org.jetbrains.dokka.gradle.DokkaTask) {
outputFormat = 'html'
outputDirectory = javadoc.destinationDir
inputs.dir 'src/main/kotlin'
samples = ["src/main/kotlin/com/github/sanity/shoebox/samples/samples.kt"]
includes = ['packages.md']
}

task javadocJar(type: Jar, dependsOn: dokkaJavadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
testImplementation "io.kotest:kotest-runner-junit5-jvm:4.2.3"
testImplementation "io.kotest:kotest-assertions-core-jvm:4.2.3"
testImplementation "io.kotest:kotest-property:4.2.3"
}
*/
pitest {
targetClasses = ['com.github.sanity.shoebox.*'] //by default "${project.group}.*"
threads = 8
outputFormats = ['HTML']
jvmArgs = ['-Xmx1024m']

test {
useJUnitPlatform()
}
/*
artifacts {
archives javadocJar

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
kotlinOptions {
jvmTarget = "1.8"
}
}
*/
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
5 changes: 2 additions & 3 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#Thu Apr 23 08:58:13 CDT 2020
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
51 changes: 31 additions & 20 deletions gradlew
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
#!/usr/bin/env sh

#
# Copyright 2015 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

##############################################################################
##
## Gradle start up script for UN*X
Expand Down Expand Up @@ -28,7 +44,7 @@ APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`

# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m"'
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"
Expand Down Expand Up @@ -109,8 +125,8 @@ if $darwin; then
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
fi

# For Cygwin, switch paths to Windows format before running java
if $cygwin ; then
# For Cygwin or MSYS, switch paths to Windows format before running java
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
JAVACMD=`cygpath --unix "$JAVACMD"`
Expand Down Expand Up @@ -138,19 +154,19 @@ if $cygwin ; then
else
eval `echo args$i`="\"$arg\""
fi
i=$((i+1))
i=`expr $i + 1`
done
case $i in
(0) set -- ;;
(1) set -- "$args0" ;;
(2) set -- "$args0" "$args1" ;;
(3) set -- "$args0" "$args1" "$args2" ;;
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
0) set -- ;;
1) set -- "$args0" ;;
2) set -- "$args0" "$args1" ;;
3) set -- "$args0" "$args1" "$args2" ;;
4) set -- "$args0" "$args1" "$args2" "$args3" ;;
5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
esac
fi

Expand All @@ -159,14 +175,9 @@ save () {
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
echo " "
}
APP_ARGS=$(save "$@")
APP_ARGS=`save "$@"`

# Collect all arguments for the java command, following the shell quoting and substitution rules
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"

# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
cd "$(dirname "$0")"
fi

exec "$JAVACMD" "$@"
18 changes: 17 additions & 1 deletion gradlew.bat
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem

@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
Expand All @@ -14,7 +30,7 @@ set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m"
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"

@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
Expand Down
6 changes: 4 additions & 2 deletions src/main/kotlin/kweb/shoebox/OrderedViewSet.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package kweb.shoebox

import kweb.shoebox.BinarySearchResult.*
import java.util.concurrent.*
import kweb.shoebox.BinarySearchResult.Between
import kweb.shoebox.BinarySearchResult.Exact
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.CopyOnWriteArrayList

/**
* Created by ian on 3/14/17.
Expand Down
31 changes: 12 additions & 19 deletions src/main/kotlin/kweb/shoebox/Shoebox.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package kweb.shoebox

import kotlinx.serialization.KSerializer
import kweb.shoebox.Source.LOCAL
import kweb.shoebox.View.*
import kweb.shoebox.View.Reference
import kweb.shoebox.View.VerifyBehavior
import kweb.shoebox.View.VerifyBehavior.BLOCKING_VERIFY
import kweb.shoebox.stores.*
import kweb.shoebox.stores.DirectoryStore
import kweb.shoebox.stores.LmdbStore
import kweb.shoebox.stores.MemoryStore
import java.nio.file.Path
import java.util.concurrent.ConcurrentHashMap
import kotlin.reflect.KClass


/*
Expand All @@ -16,19 +19,7 @@ import kotlin.reflect.KClass
* TODO: (then remove the previous lockfile mechanism)
*/

/**
* Create a [Shoebox], use this in preference to the Shoebox constructor to avoid having to provide a `KClass`
*
* @param T The type of the objects to store, these must be serializable with [Gson](https://github.com/google/gson),
*
* @param directory The path to a directory in which data will be stored, will be created if it doesn't already exist
*
* @sample com.github.sanity.shoebox.samples.basic usage sample
**/
inline fun <reified T : Any> Shoebox(store : Store<T>) = Shoebox(store, T::class)
inline fun <reified T : Any> Shoebox(dir : Path) = Shoebox(DirectoryStore(dir), T::class)
inline fun <reified T : Any> Shoebox() = Shoebox(MemoryStore(), T::class)
inline fun <reified T : Any> LmdbShoebox(name: String) = Shoebox(LmdbStore(name), T::class)
fun <T : Any> shoebox(dir : Path, kSerializer: KSerializer<T>) = Shoebox(DirectoryStore(dir, kSerializer))

/**
* Can persistently store and retrieve objects, and notify listeners of changes to those objects
Expand All @@ -38,7 +29,9 @@ inline fun <reified T : Any> LmdbShoebox(name: String) = Shoebox(LmdbStore(name)
* @param directory The path to a directory in which data will be stored, will be created if it doesn't already exist
* @param kc The KClass associated with T. To avoid having to provide this use `Shoebox<T>(directory)`
*/
class Shoebox<T : Any>(val store: Store<T>, private val kc: KClass<T>) {
class Shoebox<T : Any>(val store: Store<T>) {

constructor() : this(MemoryStore())

private val keySpecificChangeListeners = ConcurrentHashMap<String, ConcurrentHashMap<Long, (T, T, Source) -> Unit>>()
private val newListeners = ConcurrentHashMap<Long, (KeyValue<T>, Source) -> Unit>()
Expand Down Expand Up @@ -169,8 +162,8 @@ class Shoebox<T : Any>(val store: Store<T>, private val kc: KClass<T>) {
val store = when (store) {
is MemoryStore<T> -> MemoryStore<Reference>()
is DirectoryStore<T> ->
DirectoryStore<Reference>(store.directory.parent.resolve("${store.directory.fileName}-$name-view"))
is LmdbStore<T> -> LmdbStore<Reference>("${store.name}-$name-view")
DirectoryStore(store.directory.parent.resolve("${store.directory.fileName}-$name-view"), Reference.serializer())
is LmdbStore<T> -> LmdbStore("${store.name}-$name-view", Reference.serializer())
else -> throw RuntimeException("Shoebox doesn't currently support creating a view for store type ${store::class.simpleName}")
}
return View<T>(Shoebox(store), this, verify, by)
Expand Down
5 changes: 4 additions & 1 deletion src/main/kotlin/kweb/shoebox/View.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package kweb.shoebox

import kotlinx.serialization.Serializable
import kweb.shoebox.Source.LOCAL
import kweb.shoebox.View.VerifyBehavior.*
import kweb.shoebox.View.VerifyBehavior.ASYNC_VERIFY
import kweb.shoebox.View.VerifyBehavior.BLOCKING_VERIFY
import java.util.*
import java.util.concurrent.ConcurrentHashMap
import kotlin.concurrent.thread
Expand Down Expand Up @@ -150,6 +152,7 @@ class View<T : Any>(val references: Shoebox<Reference>,
BLOCKING_VERIFY, ASYNC_VERIFY
}

@Serializable
data class Reference(val keys: Set<String>) {
constructor() : this(Collections.emptySet())

Expand Down
12 changes: 6 additions & 6 deletions src/main/kotlin/kweb/shoebox/samples/samples.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package kweb.shoebox.samples

import kweb.shoebox.Shoebox
import kweb.shoebox.View
import kotlinx.serialization.Serializable
import kweb.shoebox.shoebox
import java.nio.file.Files

/**
Expand All @@ -11,9 +11,9 @@ import java.nio.file.Files

fun basic_usage_sample() {
val dir = Files.createTempDirectory("sb-")
val userStore = Shoebox<User>(dir.resolve("users"))
val usersByEmail = View(Shoebox(dir.resolve("usersByEmail")), userStore, viewBy = User::email)
val usersByGender = View(Shoebox(dir.resolve("usersByGender")), userStore, viewBy = User::gender)
val userStore = shoebox(dir.resolve("users"), User.serializer())
val usersByEmail = userStore.view("usersByEmail", User::email)
val usersByGender = userStore.view("usersByGender", User::gender)

userStore["ian"] = User("Ian Clarke", "male", "ian@blah.com")
userStore["fred"] = User("Fred Smith", "male", "fred@blah.com")
Expand All @@ -33,4 +33,4 @@ fun basic_usage_sample() {
userStore["fred"] = userStore["fred"]!!.copy(gender = "female") // Prints "fred ceased to be male"
}

data class User(val name : String, val gender : String, val email : String)
@Serializable data class User(val name : String, val gender : String, val email : String)
Loading

0 comments on commit 359b4a3

Please sign in to comment.