feat: Add ParquetReader/Writer streams
This commit is contained in:
1 parent
3a7f3ac608
commit
805693e170
22 files changed
+1890
-4
No files matched your search
@@ -2,20 +2,21 @@ namespace Bts.DataStream;
|
||||
|
||||
using BCG;
|
||||
|
||||
using bts;
|
||||
using bts.api.core;
|
||||
|
||||
using Bts.DataStreamCore;
|
||||
using Bts.PubSub;
|
||||
|
||||
using mbts.utils;
|
||||
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
using Spark.Core.Disposables;
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Linq;
|
||||
using System.Reactive.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public static class DataStreamExtensions {
|
||||
public static ViewStream AsViewStream(this IObservable<IDataPacket> source) =>
|
||||
@@ -146,4 +147,119 @@ public static class DataStreamExtensions {
|
||||
void Dispose() =>
|
||||
viewStream.onChange -= OnChange;
|
||||
}
|
||||
|
||||
public static IObservable<IDataPacket> NonIncrementalAsyncTransform(this IObservable<IDataPacket> source, Func<IBlueRowCollection, Task<IBlueRowCollection?>> transformAsync, IWorkScheduler workScheduler) {
|
||||
const int STATE_IDLE = 0;
|
||||
const int STATE_BUSY = 1;
|
||||
const int STATE_DISPOSED = 2;
|
||||
return Observable.Create<IDataPacket>(Subscribe);
|
||||
IDisposable Subscribe(IObserver<IDataPacket> o) {
|
||||
var syncRoot = new object();
|
||||
IBlueRowCollection? lastTableIn = null;
|
||||
BlueTableImmutable? lastTableOut = null;
|
||||
int nConcurrentDoWorks = 0;
|
||||
int state = STATE_IDLE;
|
||||
IDisposable? dispose = null;
|
||||
dispose = Disposable.Create(source.Subscribe(OnNext), workScheduler.Subscribe(DoWork));
|
||||
return Disposable.Create(Dispose);
|
||||
|
||||
void Dispose() {
|
||||
lock (syncRoot) {
|
||||
switch (state) {
|
||||
case STATE_DISPOSED:
|
||||
return;
|
||||
case STATE_BUSY:
|
||||
state = STATE_DISPOSED;
|
||||
return;
|
||||
}
|
||||
state = STATE_DISPOSED;
|
||||
}
|
||||
Disposable.dispose(ref dispose);
|
||||
}
|
||||
|
||||
void OnNext(IDataPacket value) =>
|
||||
Volatile.Write(ref lastTableIn, value.newRows);
|
||||
|
||||
async void DoWork() {
|
||||
lock (syncRoot) {
|
||||
nConcurrentDoWorks++;
|
||||
switch (state) {
|
||||
case STATE_DISPOSED:
|
||||
case STATE_BUSY:
|
||||
return;
|
||||
case STATE_IDLE:
|
||||
break;
|
||||
default:
|
||||
Log.assert(false, $"Unexpected state ({StateName(state)}) when DoWork called");
|
||||
return;
|
||||
}
|
||||
state = STATE_BUSY;
|
||||
}
|
||||
var dp = await PublishTable();
|
||||
bool isDisposed = false;
|
||||
lock (syncRoot) {
|
||||
switch (state) {
|
||||
case STATE_DISPOSED:
|
||||
isDisposed = true;
|
||||
break;
|
||||
case STATE_BUSY:
|
||||
break;
|
||||
default:
|
||||
Log.assert(false, $"Unexpected state ({StateName(state)}) after building output table (async)");
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (isDisposed) {
|
||||
Disposable.dispose(ref dispose);
|
||||
return;
|
||||
}
|
||||
if (dp is not null) {
|
||||
o.OnNext(dp);
|
||||
}
|
||||
lock (syncRoot) {
|
||||
nConcurrentDoWorks = 0;
|
||||
switch (state) {
|
||||
case STATE_DISPOSED:
|
||||
isDisposed = true;
|
||||
break;
|
||||
case STATE_BUSY:
|
||||
state = STATE_IDLE;
|
||||
break;
|
||||
default:
|
||||
Log.assert(false, $"Unexpected state ({StateName(state)}) after firing output table");
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (isDisposed) {
|
||||
Disposable.dispose(ref dispose);
|
||||
}
|
||||
|
||||
async Task<DataPacket?> PublishTable() {
|
||||
var tableIn = Interlocked.Exchange(ref lastTableIn, null);
|
||||
if (tableIn is null) {
|
||||
return null;
|
||||
}
|
||||
var tableOut = await transformAsync(tableIn);
|
||||
if (tableOut is null) {
|
||||
return null;
|
||||
}
|
||||
lastTableOut ??= new(tableOut.schema);
|
||||
var bd = lastTableOut.ToDataPacketBuilder();
|
||||
bd.Clear();
|
||||
foreach (var r in tableOut) {
|
||||
bd.AddOrUpdate(r, BlueRowEqualityComparer.Instance);
|
||||
}
|
||||
return bd.ToDataPacket(out lastTableOut);
|
||||
}
|
||||
}
|
||||
|
||||
static string StateName(int state) =>
|
||||
state switch {
|
||||
STATE_IDLE => "IDLE",
|
||||
STATE_BUSY => "BUSY",
|
||||
STATE_DISPOSED => "DISPOSED",
|
||||
_ => $"STATE_{state}",
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user