GithubHelp home page GithubHelp logo

isabella232 / ion-kotlin-builder Goto Github PK

View Code? Open in Web Editor NEW

This project forked from amazon-ion/ion-kotlin-builder

0.0 0.0 0.0 83 KB

This library provides Kotlin style type-safe builders for ion-java.

License: Apache License 2.0

Kotlin 100.00%

ion-kotlin-builder's Introduction

Build Status Maven Central Javadoc

Ion Kotlin builder

This library provides Kotlin Type-Safe Builders for manually writing Ion documents. It helps avoiding common ion-java streaming api runtime errors such as forgetting to step out of a container or not setting a filed name for a struct element.

Examples

// Ion Document:
/*
1
"text"
a_symbol
[
    1.,
    { foo: "bar", t: a::b::2019T }
]
*/

// with builder
val outBuilder = ByteArrayOutputStream()

IonTextWriterBuilder.standard().build(outBuilder).use { writer ->

    // writeIonWith() is the entry point for the builder.
    // Also available as an extension function: IonWriter.dsl()
    writeIonWith(writer) {
        int(1)
        string("text")
        symbol("a_symbol")
        list {
            decimal(BigDecimal.ONE)
            struct {
                string(field = "foo", value = "bar")
                timestamp(field = "t",
                          value = Timestamp.valueOf("2019T"),
                          annotations = listOf("a", "b"))
            }
        }
    }
}

// with plain ion-java streaming API

val outIonJava = ByteArrayOutputStream()
IonTextWriterBuilder.standard().build(outIonJava).use { w ->
    w.writeInt(1)
    w.writeString("text")
    w.writeSymbol("a_symbol")

    w.stepIn(IonType.LIST)

    w.writeDecimal(BigDecimal.ONE)

    w.stepIn(IonType.STRUCT)

    w.setFieldName("foo")
    w.writeString("bar")

    w.setFieldName("t")
    w.setTypeAnnotations("a", "b")
    w.writeTimestamp(Timestamp.valueOf("2019T"))

    // struct
    w.stepOut()

    // list
    w.stepOut()

}

Performance

There is small performance penalty in using the builders instead of directly using the Streaming API as the builder adds a level of indirection to calls to the streaming API. Bellow are the results for a microbenchmark comparing the two, code in DslBenchmark.

to run execute: ./gradlew jmh --no-daemon.

Throughput, higher is better

Benchmark                                    Mode  Cnt         Score         Error  Units
DslBenchmark.multipleValuesBinaryDsl        thrpt   10   1141172.250 ±  122609.229  ops/s
DslBenchmark.multipleValuesBinaryStreaming  thrpt   10   1487947.880 ±  125854.681  ops/s
DslBenchmark.multipleValuesTextDsl          thrpt   10   1300583.629 ±  136815.481  ops/s
DslBenchmark.multipleValuesTextStreaming    thrpt   10   1358832.587 ±  377357.250  ops/s
DslBenchmark.singleScalarBinaryDsl          thrpt   10  11508758.807 ± 2432243.881  ops/s
DslBenchmark.singleScalarBinaryStreaming    thrpt   10  18911878.323 ±  622044.132  ops/s
DslBenchmark.singleScalarTextDsl            thrpt   10  12702660.292 ± 1522103.747  ops/s
DslBenchmark.singleScalarTextStreaming      thrpt   10  19155615.372 ±  712453.902  ops/s

License

This library is licensed under the Apache 2.0 License.

ion-kotlin-builder's People

Contributors

jpeddicord avatar popematt avatar raganhan avatar therapon avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.