-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(csharpclient): First pass at a C# client (#5703)
This implements a C# client for Deephaven. It requires the Deephaven C++ client to be installed. In a future PR we will provide more detailed installation instructions.
- Loading branch information
Showing
51 changed files
with
5,725 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.vs/ | ||
bin/ | ||
obj/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
using Deephaven.DeephavenClient.Interop; | ||
|
||
namespace Deephaven.DeephavenClient; | ||
|
||
public class AggregateCombo { | ||
private readonly Aggregate[] _aggregates; | ||
|
||
public AggregateCombo(IEnumerable<Aggregate> aggregates) => _aggregates = aggregates.ToArray(); | ||
|
||
internal InternalAggregateCombo Invoke() { | ||
return new InternalAggregateCombo(_aggregates); | ||
} | ||
} | ||
|
||
internal class InternalAggregateCombo : IDisposable { | ||
internal NativePtr<NativeAggregateCombo> Self; | ||
|
||
internal InternalAggregateCombo(Aggregate[] aggregates) { | ||
var internalAggregates = new List<InternalAggregate>(); | ||
try { | ||
// Invoke the lazy method on the aggregate to get its C++ wrapper | ||
foreach (var agg in aggregates) { | ||
internalAggregates.Add(agg.Materialize()); | ||
} | ||
|
||
var internalAggPtrs = internalAggregates.Select(ag => ag.Self).ToArray(); | ||
NativeAggregateCombo.deephaven_client_AggregateCombo_Create( | ||
internalAggPtrs, internalAggPtrs.Length, out var result, out var status); | ||
status.OkOrThrow(); | ||
Self = result; | ||
} finally { | ||
foreach (var agg in internalAggregates) { | ||
agg.Dispose(); | ||
} | ||
} | ||
} | ||
|
||
~InternalAggregateCombo() { | ||
ReleaseUnmanagedResources(); | ||
} | ||
|
||
public void Dispose() { | ||
ReleaseUnmanagedResources(); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
private void ReleaseUnmanagedResources() { | ||
if (!Self.TryRelease(out var old)) { | ||
return; | ||
} | ||
NativeAggregateCombo.deephaven_client_AggregateCombo_dtor(old); | ||
} | ||
} | ||
|
||
internal partial class NativeAggregateCombo { | ||
[LibraryImport(LibraryPaths.Dhclient, StringMarshalling = StringMarshalling.Utf8)] | ||
public static partial void deephaven_client_AggregateCombo_Create( | ||
NativePtr<NativeAggregate>[] aggregates, Int32 numAggregates, | ||
out NativePtr<NativeAggregateCombo> self, out ErrorStatus status); | ||
[LibraryImport(LibraryPaths.Dhclient, StringMarshalling = StringMarshalling.Utf8)] | ||
public static partial void deephaven_client_AggregateCombo_dtor(NativePtr<NativeAggregateCombo> self); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
using Deephaven.DeephavenClient.Interop; | ||
|
||
namespace Deephaven.DeephavenClient; | ||
|
||
public class Aggregate { | ||
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Unicode)] | ||
private delegate void AggregateMethod( | ||
string[] columns, Int32 numColumns, out NativePtr<NativeAggregate> result, out ErrorStatus status); | ||
|
||
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Unicode)] | ||
private delegate void LazyMaterializer(out NativePtr<NativeAggregate> result, out ErrorStatus status); | ||
|
||
public static Aggregate AbsSum(IEnumerable<string> columnSpecs) { | ||
return CreateHelper(columnSpecs, NativeAggregate.deephaven_client_Aggregate_AbsSum); | ||
} | ||
|
||
public static Aggregate Group(IEnumerable<string> columnSpecs) { | ||
return CreateHelper(columnSpecs, NativeAggregate.deephaven_client_Aggregate_Group); | ||
} | ||
|
||
public static Aggregate Avg(IEnumerable<string> columnSpecs) { | ||
return CreateHelper(columnSpecs, NativeAggregate.deephaven_client_Aggregate_Avg); | ||
} | ||
|
||
public static Aggregate First(IEnumerable<string> columnSpecs) { | ||
return CreateHelper(columnSpecs, NativeAggregate.deephaven_client_Aggregate_First); | ||
} | ||
|
||
public static Aggregate Last(IEnumerable<string> columnSpecs) { | ||
return CreateHelper(columnSpecs, NativeAggregate.deephaven_client_Aggregate_Last); | ||
} | ||
|
||
public static Aggregate Max(IEnumerable<string> columnSpecs) { | ||
return CreateHelper(columnSpecs, NativeAggregate.deephaven_client_Aggregate_Max); | ||
} | ||
|
||
public static Aggregate Med(IEnumerable<string> columnSpecs) { | ||
return CreateHelper(columnSpecs, NativeAggregate.deephaven_client_Aggregate_Med); | ||
} | ||
|
||
public static Aggregate Min(IEnumerable<string> columnSpecs) { | ||
return CreateHelper(columnSpecs, NativeAggregate.deephaven_client_Aggregate_Min); | ||
} | ||
|
||
public static Aggregate Std(IEnumerable<string> columnSpecs) { | ||
return CreateHelper(columnSpecs, NativeAggregate.deephaven_client_Aggregate_Std); | ||
} | ||
|
||
public static Aggregate Sum(IEnumerable<string> columnSpecs) { | ||
return CreateHelper(columnSpecs, NativeAggregate.deephaven_client_Aggregate_Sum); | ||
} | ||
|
||
public static Aggregate Var(IEnumerable<string> columnSpecs) { | ||
return CreateHelper(columnSpecs, NativeAggregate.deephaven_client_Aggregate_Var); | ||
} | ||
|
||
public static Aggregate WAvg(IEnumerable<string> columnSpecs) { | ||
return CreateHelper(columnSpecs, NativeAggregate.deephaven_client_Aggregate_WAvg); | ||
} | ||
|
||
public static Aggregate Count(string columnSpec) { | ||
LazyMaterializer lazyMaterializer = (out NativePtr<NativeAggregate> result, out ErrorStatus status) => | ||
NativeAggregate.deephaven_client_Aggregate_Count(columnSpec, out result, out status); | ||
return new Aggregate(lazyMaterializer); | ||
} | ||
|
||
public static Aggregate Pct(double percentile, bool avgMedian, IEnumerable<string> columnSpecs) { | ||
var cols = columnSpecs.ToArray(); | ||
LazyMaterializer lazyMaterializer = (out NativePtr<NativeAggregate> result, out ErrorStatus status) => | ||
NativeAggregate.deephaven_client_Aggregate_Pct(percentile, (InteropBool)avgMedian, | ||
cols, cols.Length, out result, out status); | ||
return new Aggregate(lazyMaterializer); | ||
} | ||
|
||
/// <summary> | ||
/// Helper method for all the Aggregate functions except Count, which is special because | ||
/// it takes a string rather than an IEnumerable<string> | ||
/// </summary> | ||
private static Aggregate CreateHelper(IEnumerable<string> columnSpecs, AggregateMethod aggregateMethod) { | ||
var cs = columnSpecs.ToArray(); | ||
|
||
LazyMaterializer lazyMaterializer = (out NativePtr<NativeAggregate> result, out ErrorStatus status) => | ||
aggregateMethod(cs, cs.Length, out result, out status); | ||
|
||
return new Aggregate(lazyMaterializer); | ||
} | ||
|
||
private readonly LazyMaterializer _lazyMaterializer; | ||
|
||
private Aggregate(LazyMaterializer lazyMaterializer) => _lazyMaterializer = lazyMaterializer; | ||
|
||
internal InternalAggregate Materialize() { | ||
_lazyMaterializer(out var result, out var status); | ||
status.OkOrThrow(); | ||
return new InternalAggregate(result); | ||
} | ||
} | ||
|
||
internal class InternalAggregate : IDisposable { | ||
internal NativePtr<NativeAggregate> Self; | ||
|
||
internal InternalAggregate(NativePtr<NativeAggregate> self) => Self = self; | ||
|
||
~InternalAggregate() { | ||
ReleaseUnmanagedResources(); | ||
} | ||
|
||
public void Dispose() { | ||
ReleaseUnmanagedResources(); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
private void ReleaseUnmanagedResources() { | ||
if (!Self.TryRelease(out var old)) { | ||
return; | ||
} | ||
NativeAggregate.deephaven_client_Aggregate_dtor(old); | ||
} | ||
} | ||
|
||
internal partial class NativeAggregate { | ||
[LibraryImport(LibraryPaths.Dhclient, StringMarshalling = StringMarshalling.Utf8)] | ||
public static partial void deephaven_client_Aggregate_dtor(NativePtr<NativeAggregate> self); | ||
[LibraryImport(LibraryPaths.Dhclient, StringMarshalling = StringMarshalling.Utf8)] | ||
public static partial void deephaven_client_Aggregate_AbsSum( | ||
string[] columns, Int32 numColumns, out NativePtr<NativeAggregate> result, out ErrorStatus status); | ||
[LibraryImport(LibraryPaths.Dhclient, StringMarshalling = StringMarshalling.Utf8)] | ||
public static partial void deephaven_client_Aggregate_Group( | ||
string[] columns, Int32 numColumns, out NativePtr<NativeAggregate> result, out ErrorStatus status); | ||
[LibraryImport(LibraryPaths.Dhclient, StringMarshalling = StringMarshalling.Utf8)] | ||
public static partial void deephaven_client_Aggregate_Avg( | ||
string[] columns, Int32 numColumns, out NativePtr<NativeAggregate> result, out ErrorStatus status); | ||
[LibraryImport(LibraryPaths.Dhclient, StringMarshalling = StringMarshalling.Utf8)] | ||
public static partial void deephaven_client_Aggregate_Count( | ||
string column, out NativePtr<NativeAggregate> result, out ErrorStatus status); | ||
[LibraryImport(LibraryPaths.Dhclient, StringMarshalling = StringMarshalling.Utf8)] | ||
public static partial void deephaven_client_Aggregate_First( | ||
string[] columns, Int32 numColumns, out NativePtr<NativeAggregate> result, out ErrorStatus status); | ||
[LibraryImport(LibraryPaths.Dhclient, StringMarshalling = StringMarshalling.Utf8)] | ||
public static partial void deephaven_client_Aggregate_Last( | ||
string[] columns, Int32 numColumns, out NativePtr<NativeAggregate> result, out ErrorStatus status); | ||
[LibraryImport(LibraryPaths.Dhclient, StringMarshalling = StringMarshalling.Utf8)] | ||
public static partial void deephaven_client_Aggregate_Max( | ||
string[] columns, Int32 numColumns, out NativePtr<NativeAggregate> result, out ErrorStatus status); | ||
[LibraryImport(LibraryPaths.Dhclient, StringMarshalling = StringMarshalling.Utf8)] | ||
public static partial void deephaven_client_Aggregate_Med( | ||
string[] columns, Int32 numColumns, out NativePtr<NativeAggregate> result, out ErrorStatus status); | ||
[LibraryImport(LibraryPaths.Dhclient, StringMarshalling = StringMarshalling.Utf8)] | ||
public static partial void deephaven_client_Aggregate_Min( | ||
string[] columns, Int32 numColumns, out NativePtr<NativeAggregate> result, out ErrorStatus status); | ||
[LibraryImport(LibraryPaths.Dhclient, StringMarshalling = StringMarshalling.Utf8)] | ||
public static partial void deephaven_client_Aggregate_Pct( | ||
double percentile, InteropBool avgMedian, | ||
string[] columns, Int32 numColumns, out NativePtr<NativeAggregate> result, out ErrorStatus status); | ||
[LibraryImport(LibraryPaths.Dhclient, StringMarshalling = StringMarshalling.Utf8)] | ||
public static partial void deephaven_client_Aggregate_Std( | ||
string[] columns, Int32 numColumns, out NativePtr<NativeAggregate> result, out ErrorStatus status); | ||
[LibraryImport(LibraryPaths.Dhclient, StringMarshalling = StringMarshalling.Utf8)] | ||
public static partial void deephaven_client_Aggregate_Sum( | ||
string[] columns, Int32 numColumns, out NativePtr<NativeAggregate> result, out ErrorStatus status); | ||
[LibraryImport(LibraryPaths.Dhclient, StringMarshalling = StringMarshalling.Utf8)] | ||
public static partial void deephaven_client_Aggregate_Var( | ||
string[] columns, Int32 numColumns, out NativePtr<NativeAggregate> result, out ErrorStatus status); | ||
[LibraryImport(LibraryPaths.Dhclient, StringMarshalling = StringMarshalling.Utf8)] | ||
public static partial void deephaven_client_Aggregate_WAvg( | ||
string[] columns, Int32 numColumns, out NativePtr<NativeAggregate> result, out ErrorStatus status); | ||
} |
Oops, something went wrong.