Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ToObjectStream #92

Closed
Grauenwolf opened this issue Apr 14, 2016 · 8 comments
Closed

ToObjectStream #92

Grauenwolf opened this issue Apr 14, 2016 · 8 comments
Labels
Dapper-Equivalent NPoco-Equivalent Research More research is needed before any implementation can be started.

Comments

@Grauenwolf
Copy link
Collaborator

When working with really large result sets, it may be preferable to stream the data instead of materializing a list.

Both NPoco and Dapper already support this:

To do better we need to marry it with something that still manages lifecycles. For example, the exposed enumerator needs to close the connection.

Maybe include Reactive Extensions support with this.

@Grauenwolf Grauenwolf added Research More research is needed before any implementation can be started. NPoco-Equivalent Dapper-Equivalent labels Apr 14, 2016
@Grauenwolf
Copy link
Collaborator Author

See also #281

@Grauenwolf
Copy link
Collaborator Author

Grauenwolf commented Jul 9, 2022

class AsyncObectStream : IAsyncEnumerator<T>, IAsyncDisposable

async using var stream = dataSource.From<T>().ToObjectStream().ExecuteAsync();
async foreach (var item in stream )
{
    //do something
}

@Grauenwolf
Copy link
Collaborator Author

This won't implement ILink<T>. You can't perform a transformation or caching operation on it.

@Grauenwolf
Copy link
Collaborator Author

This is going to need new Data Source methods. Namely ExecuteStream and ExecuteStreamAsync.

These methods will create a connection, but never close it. The result object will have to handle closing itself via Dispose/DisposeAsync.

@Grauenwolf
Copy link
Collaborator Author

For a open or transactional data source, the Dispose call will be a no-op.

@Grauenwolf
Copy link
Collaborator Author

Before we implement this, we should document the full pipeline for a normal Execute call.

@Grauenwolf
Copy link
Collaborator Author

We're going to need the pre-existing StreamingObjectConstructor class for this.

@Grauenwolf
Copy link
Collaborator Author

Previously, Chain would fully manage database connections by default. Specifically, it would open and close connections automatically unless a transaction was involved. In that case, the developer only needed to manage the transactional data source itself.

However, there are times when a result set is too large to handle at one time. In this case the developer will want an IEnumerable or IAsyncEnumerable instead of a collection. To support this, the ToObjectStream materializer was created.

When used in place of ToCollection, the caller gets a ObjectStream object. This object implements IEnumerable<TObject>, IDisposable, IAsyncDisposable, abd IAsyncEnumerable<TObject>. (That latter two are only available in .NET 6 or later.)

This object stream may be used directly, as shown below, or attached to an RX Observable or TPL Dataflow just like any other enumerable data structure.

//sync pattern

using var objectStream = dataSource.From<Employee>(new { Title = uniqueKey }).ToObjectStream<Employee>().Execute();
foreach (var item in objectStream)
{
	Assert.AreEqual(uniqueKey, item.Title);
}

//async pattern

await using var objectStream = await dataSource.From<Employee>(new { Title = uniqueKey }).ToObjectStream<Employee>().ExecuteAsync();
await foreach (var item in objectStream)
{
	Assert.AreEqual(uniqueKey, item.Title);
}

It is vital that the object stream is disposed after use. If that doesn't occur, the database can suffer from thread exhaustion or deadlocks.

@Grauenwolf Grauenwolf changed the title ExecuteStreaming ToObjectStream Jul 12, 2022
@Grauenwolf Grauenwolf added this to the Tortuga Chain 4.3 milestone Jul 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Dapper-Equivalent NPoco-Equivalent Research More research is needed before any implementation can be started.
Projects
None yet
Development

No branches or pull requests

1 participant