Git Product home page Git Product logo

stream-tools's Introduction

Stream Tools

Working with streams has it's benefits:

  • Just in time use - lower memory footprint.
  • Common interface and ease of manipulation - filter / collect etc.

And challenges:

  • Deferred operations and close: The deferred nature has it's merits until you accidentally close the underline resource. ** On the other hand one leave the stream as is and the last one consuming it must close it.
  • No break: Once a stream is used it is read and consumed all the way (unlike generators/iterators)

Some extra magic for Java

  • Adding autoclosable(theStream) which would would call thestream.close() when the stream is consumed
  • Parsing huge Json (long list of objects) one by one.

Better magic with Kotlin

@Test
fun testStreamTillNullWithInfiniteStream() {
    val endless = streamUtil.generatorTillNull { i -> i }.map { i-> if (i!=3) i else null }
    checkStreamOf3(endless.tillNull())
}

Leveraging Jackson to read large json having large array field as stream of objects

  • The example shows a large array of a json object read as stream,
  • while the other "lower footprint" are processed as well when the stream is done.
  • The processing of the rest was done using onConsume method (similar to the autoclose that was described above)
@Test
fun readResourceObjectArrayLargeKT() {
    val file = File(classLoader.getResource(objectFileName)!!.file)
    assertTrue(file.exists())

    val input = BufferedReader(InputStreamReader(classLoader.getResourceAsStream(objectFileName)!!))
    val parseState = KJsonStreamParser().parse(input)
    val typeReference = object : TypeReference<FooBarBaz>() {}
    val callback = { e: Exception? -> if (e != null) throw  RuntimeException("Failed", e) }
    val result = parseState.parseObject(typeReference, "tostream", callback)
            .onConsume({  parseState.parseRest(callback)})
    val streamIt = result.asSequence().iterator()
    Assert.assertEquals(parseState.fields["beforeStr"].toString(), "\"str\"")
    Assert.assertEquals(parseState.fields["beforeInt"].toString(), "-7")
    Assert.assertFalse(parseState.fields.containsKey("afterInt"))
    var cnt = 0
    streamIt.forEach {
        cnt++;
        print("read $it")
        Assert.assertFalse(parseState.fields.containsKey("afterInt"))
    }
    Assert.assertTrue(parseState.fields.containsKey("afterInt"))
    Assert.assertEquals(cnt, 100)
    Assert.assertEquals(parseState.fields["afterStr"].toString(), "\"str\"")
    Assert.assertEquals(parseState.fields["afterInt"].toString(), "7")
}

stream-tools's People

Contributors

saffih avatar

Watchers

 avatar  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.