rdfjs / stream-spec

RDF/JS: Stream interfaces – A specification of a low level interface definition representing RDF data independent of a serialized format in a JavaScript environment.
https://rdf.js.org/stream-spec/
5 stars 2 forks source link

How to .close() a stream? #4

Open HolgerKnublauch opened 7 years ago

HolgerKnublauch commented 7 years ago

Sorry if this has been discussed before.

I am wondering how Streams (such as those returned by .match) are supposed to be walked and then, for example, stopped after the first element. Shouldn't there be some kind of a .close() function that releases the resources? As an example I am working on a Oracle Nashorn-based implementation of your API that runs a Jena Triple iterator in the background, and I need a clean way to call .close() on those Java objects. The contract would be that close() must be called unless the Stream is walked to exhaustion.

RubenVerborgh commented 7 years ago

+1 for a .close() method

jacoscaz commented 7 years ago

+1 for a .close() method from me, too.

elf-pavlik commented 7 years ago

would anyone like to submit PR?

jacoscaz commented 7 years ago

Would calling .close() have to be mandatory or could it be made optional?

k00ni commented 5 years ago

The rdfjs spec says:

Streams are used only in a readable manner. This requires only a single queue per stream, which simplifies implementations and doesn't have performance drawbacks, compared to writeable streams

What means "closing" exactly? In my opinion, if you close it manually, Streams wouldn't be readable-only anymore. Furthermore, what should happen, if there is a close function but its never called?

I suggest, that you leave that to the implementation. For instance, an iterator could implement a function, which is called, when the object itself gets removed. For instance, in PHP there is a __destruct function, which can be overridden and is called, before object removal. This way, you can remove the iterator manually the release its resources. Maybe there is something similar in Java.

RubenVerborgh commented 5 years ago

Streams are read-only. By calling close, you signal that you will not read anymore, so no more resources need to be spent on generating items. Behind the scenes, for instance, HTTP connections could be closed, or file descriptors.