diff --git a/.github/workflows/tiledb-csharp.yml b/.github/workflows/tiledb-csharp.yml index d6841a41..ccba2d74 100644 --- a/.github/workflows/tiledb-csharp.yml +++ b/.github/workflows/tiledb-csharp.yml @@ -2,9 +2,9 @@ name: CI on: push: - branches: [ main ] + branches: [ main, 'release/*' ] pull_request: - branches: [ main ] + branches: [ main, 'release/*' ] workflow_dispatch: workflow_call: diff --git a/sources/TileDB.CSharp/Array.cs b/sources/TileDB.CSharp/Array.cs index 738fe998..36eeec78 100644 --- a/sources/TileDB.CSharp/Array.cs +++ b/sources/TileDB.CSharp/Array.cs @@ -5,6 +5,8 @@ using TileDB.CSharp.Marshalling; using TileDB.CSharp.Marshalling.SafeHandles; using TileDB.Interop; +using ArrayHandle = TileDB.CSharp.Marshalling.SafeHandles.ArrayHandle; +using ArraySchemaHandle = TileDB.CSharp.Marshalling.SafeHandles.ArraySchemaHandle; namespace TileDB.CSharp { diff --git a/sources/TileDB.CSharp/ArraySchema.cs b/sources/TileDB.CSharp/ArraySchema.cs index f85f4ef6..0fb6434f 100644 --- a/sources/TileDB.CSharp/ArraySchema.cs +++ b/sources/TileDB.CSharp/ArraySchema.cs @@ -2,6 +2,10 @@ using System.Collections.Generic; using TileDB.CSharp.Marshalling.SafeHandles; using TileDB.Interop; +using ArraySchemaHandle = TileDB.CSharp.Marshalling.SafeHandles.ArraySchemaHandle; +using FilterListHandle = TileDB.CSharp.Marshalling.SafeHandles.FilterListHandle; +using DomainHandle = TileDB.CSharp.Marshalling.SafeHandles.DomainHandle; +using AttributeHandle = TileDB.CSharp.Marshalling.SafeHandles.AttributeHandle; namespace TileDB.CSharp { diff --git a/sources/TileDB.CSharp/ArraySchemaEvolution.cs b/sources/TileDB.CSharp/ArraySchemaEvolution.cs index 905dcfca..415a5e59 100644 --- a/sources/TileDB.CSharp/ArraySchemaEvolution.cs +++ b/sources/TileDB.CSharp/ArraySchemaEvolution.cs @@ -1,6 +1,7 @@ using System; using TileDB.CSharp.Marshalling.SafeHandles; using TileDB.Interop; +using ArraySchemaEvolutionHandle = TileDB.CSharp.Marshalling.SafeHandles.ArraySchemaEvolutionHandle; namespace TileDB.CSharp { diff --git a/sources/TileDB.CSharp/Attribute.cs b/sources/TileDB.CSharp/Attribute.cs index 54227966..b8021a78 100644 --- a/sources/TileDB.CSharp/Attribute.cs +++ b/sources/TileDB.CSharp/Attribute.cs @@ -2,8 +2,10 @@ using System.Linq; using System.Runtime.InteropServices; using System.Text; -using TileDB.CSharp.Marshalling.SafeHandles; using TileDB.Interop; +using TileDB.CSharp.Marshalling.SafeHandles; +using AttributeHandle = TileDB.CSharp.Marshalling.SafeHandles.AttributeHandle; +using FilterListHandle = TileDB.CSharp.Marshalling.SafeHandles.FilterListHandle; namespace TileDB.CSharp { diff --git a/sources/TileDB.CSharp/Config.cs b/sources/TileDB.CSharp/Config.cs index 2280704a..eb0d68af 100644 --- a/sources/TileDB.CSharp/Config.cs +++ b/sources/TileDB.CSharp/Config.cs @@ -2,6 +2,7 @@ using System.Text; using TileDB.CSharp.Marshalling.SafeHandles; using TileDB.Interop; +using ConfigHandle = TileDB.CSharp.Marshalling.SafeHandles.ConfigHandle; namespace TileDB.CSharp { diff --git a/sources/TileDB.CSharp/ConfigIterator.cs b/sources/TileDB.CSharp/ConfigIterator.cs index 0b570668..6e56e36b 100644 --- a/sources/TileDB.CSharp/ConfigIterator.cs +++ b/sources/TileDB.CSharp/ConfigIterator.cs @@ -1,6 +1,8 @@ using System; using TileDB.CSharp.Marshalling.SafeHandles; using TileDB.Interop; +using ConfigHandle = TileDB.CSharp.Marshalling.SafeHandles.ConfigHandle; +using ConfigIteratorHandle = TileDB.CSharp.Marshalling.SafeHandles.ConfigIteratorHandle; namespace TileDB.CSharp { diff --git a/sources/TileDB.CSharp/Context.cs b/sources/TileDB.CSharp/Context.cs index b84c1df9..e3129fb2 100644 --- a/sources/TileDB.CSharp/Context.cs +++ b/sources/TileDB.CSharp/Context.cs @@ -7,6 +7,7 @@ using System.Text; using TileDB.CSharp.Marshalling.SafeHandles; using TileDB.Interop; +using ContextHandle = TileDB.CSharp.Marshalling.SafeHandles.ContextHandle; namespace TileDB.CSharp { diff --git a/sources/TileDB.CSharp/Dimension.cs b/sources/TileDB.CSharp/Dimension.cs index 305cbda2..fb037095 100644 --- a/sources/TileDB.CSharp/Dimension.cs +++ b/sources/TileDB.CSharp/Dimension.cs @@ -4,6 +4,8 @@ using TileDB.CSharp.Marshalling; using TileDB.CSharp.Marshalling.SafeHandles; using TileDB.Interop; +using DimensionHandle = TileDB.CSharp.Marshalling.SafeHandles.DimensionHandle; +using FilterListHandle = TileDB.CSharp.Marshalling.SafeHandles.FilterListHandle; namespace TileDB.CSharp { diff --git a/sources/TileDB.CSharp/Domain.cs b/sources/TileDB.CSharp/Domain.cs index 24a25f81..f23a3ea8 100644 --- a/sources/TileDB.CSharp/Domain.cs +++ b/sources/TileDB.CSharp/Domain.cs @@ -1,6 +1,8 @@ using System; using TileDB.CSharp.Marshalling.SafeHandles; using TileDB.Interop; +using DomainHandle = TileDB.CSharp.Marshalling.SafeHandles.DomainHandle; +using DimensionHandle = TileDB.CSharp.Marshalling.SafeHandles.DimensionHandle; namespace TileDB.CSharp { diff --git a/sources/TileDB.CSharp/File.cs b/sources/TileDB.CSharp/File.cs index 4a38e751..0d7df6e3 100644 --- a/sources/TileDB.CSharp/File.cs +++ b/sources/TileDB.CSharp/File.cs @@ -1,6 +1,7 @@ using System.ComponentModel; using TileDB.CSharp.Marshalling.SafeHandles; using TileDB.Interop; +using ArraySchemaHandle = TileDB.CSharp.Marshalling.SafeHandles.ArraySchemaHandle; namespace TileDB.CSharp { diff --git a/sources/TileDB.CSharp/Filter.cs b/sources/TileDB.CSharp/Filter.cs index ba866ec4..9d4c9d81 100644 --- a/sources/TileDB.CSharp/Filter.cs +++ b/sources/TileDB.CSharp/Filter.cs @@ -3,6 +3,7 @@ using System.Runtime.InteropServices; using TileDB.CSharp.Marshalling.SafeHandles; using TileDB.Interop; +using FilterHandle = TileDB.CSharp.Marshalling.SafeHandles.FilterHandle; namespace TileDB.CSharp { diff --git a/sources/TileDB.CSharp/FilterList.cs b/sources/TileDB.CSharp/FilterList.cs index 89c4fa59..ec8e627b 100644 --- a/sources/TileDB.CSharp/FilterList.cs +++ b/sources/TileDB.CSharp/FilterList.cs @@ -1,6 +1,8 @@ using System; using TileDB.CSharp.Marshalling.SafeHandles; using TileDB.Interop; +using FilterHandle = TileDB.CSharp.Marshalling.SafeHandles.FilterHandle; +using FilterListHandle = TileDB.CSharp.Marshalling.SafeHandles.FilterListHandle; namespace TileDB.CSharp { diff --git a/sources/TileDB.CSharp/FragmentInfo.cs b/sources/TileDB.CSharp/FragmentInfo.cs index 3e4a22cf..f5740052 100644 --- a/sources/TileDB.CSharp/FragmentInfo.cs +++ b/sources/TileDB.CSharp/FragmentInfo.cs @@ -3,6 +3,8 @@ using TileDB.CSharp.Marshalling; using TileDB.CSharp.Marshalling.SafeHandles; using TileDB.Interop; +using ArraySchemaHandle = TileDB.CSharp.Marshalling.SafeHandles.ArraySchemaHandle; +using ConfigHandle = TileDB.CSharp.Marshalling.SafeHandles.ConfigHandle; namespace TileDB.CSharp { diff --git a/sources/TileDB.CSharp/Group.cs b/sources/TileDB.CSharp/Group.cs index fbf7ee59..b77e1884 100644 --- a/sources/TileDB.CSharp/Group.cs +++ b/sources/TileDB.CSharp/Group.cs @@ -1,6 +1,7 @@ using System; using TileDB.CSharp.Marshalling.SafeHandles; using TileDB.Interop; +using GroupHandle = TileDB.CSharp.Marshalling.SafeHandles.GroupHandle; namespace TileDB.CSharp { diff --git a/sources/TileDB.CSharp/Interop/InteropAux.cs b/sources/TileDB.CSharp/Interop/InteropAux.cs new file mode 100644 index 00000000..2354148b --- /dev/null +++ b/sources/TileDB.CSharp/Interop/InteropAux.cs @@ -0,0 +1,13 @@ +using System.ComponentModel; +using System; +using TileDB.CSharp; + +namespace TileDB.Interop +{ + [Obsolete(Obsoletions.TileDBInteropMessage, DiagnosticId = Obsoletions.TileDBInteropDiagId, UrlFormat = Obsoletions.SharedUrlFormat)] + [EditorBrowsable(EditorBrowsableState.Never)] + // placeholder struct to avoid errors loading generated code + public partial struct __sFILE + { + } +} \ No newline at end of file diff --git a/sources/TileDB.CSharp/Interop/SpanExtensions.cs b/sources/TileDB.CSharp/Interop/SpanExtensions.cs new file mode 100644 index 00000000..99a4ae4b --- /dev/null +++ b/sources/TileDB.CSharp/Interop/SpanExtensions.cs @@ -0,0 +1,31 @@ +// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information. +//https://github.com/dotnet/ClangSharp/blob/67c1e5243b9d58f2b28f10e3f9a82f7537fd9d88/sources/ClangSharp.Interop/Internals/SpanExtensions.cs + +using System; +using System.ComponentModel; +using System.Text; +using TileDB.CSharp; + +namespace TileDB.Interop +{ + [Obsolete(Obsoletions.TileDBInteropMessage, DiagnosticId = Obsoletions.TileDBInteropDiagId, UrlFormat = Obsoletions.SharedUrlFormat)] + [EditorBrowsable(EditorBrowsableState.Never)] + public static unsafe class SpanExtensions + { + public static string AsString(this Span self) => AsString((ReadOnlySpan)self); + + public static string AsString(this ReadOnlySpan self) + { + if (self.IsEmpty) + { + return string.Empty; + } + + fixed (byte* pSelf = self) + { + return Encoding.ASCII.GetString(pSelf, self.Length); + } + } + } + +}//namespace \ No newline at end of file diff --git a/sources/TileDB.CSharp/Interop/TileDBSafeHandle.cs b/sources/TileDB.CSharp/Interop/TileDBSafeHandle.cs new file mode 100644 index 00000000..144bb461 --- /dev/null +++ b/sources/TileDB.CSharp/Interop/TileDBSafeHandle.cs @@ -0,0 +1,664 @@ +using System; +using System.ComponentModel; +using System.Runtime.InteropServices; +using TileDB.CSharp; + +namespace TileDB.Interop +{ + + [Obsolete(Obsoletions.TileDBInteropMessage, DiagnosticId = Obsoletions.TileDBInteropDiagId, UrlFormat = Obsoletions.SharedUrlFormat)] + [EditorBrowsable(EditorBrowsableState.Never)] + public unsafe class ConfigHandle : SafeHandle + { + // Constructor for a Handle + // - calls native allocator + // - exception on failure + + public ConfigHandle() : base(IntPtr.Zero, ownsHandle: true) + { + var h = stackalloc tiledb_config_t*[1]; + var e = stackalloc tiledb_error_t*[1]; + Methods.tiledb_config_alloc(h, e); + + if (h[0] == (void*)0) + { + throw new Exception("Failed to allocate!"); + } + SetHandle(h[0]); + } + + // Deallocator: call native free with CER guarantees from SafeHandle + override protected bool ReleaseHandle() + { + // Free the native object + var p = (tiledb_config_t*)handle; + Methods.tiledb_config_free(&p); + // Invalidate the contained pointer + SetHandle(IntPtr.Zero); + + return true; + } + + // Conversions, getters, operators + public ulong Get() { return (ulong)handle; } + private protected void SetHandle(tiledb_config_t* h) { SetHandle((IntPtr)h); } + private protected ConfigHandle(IntPtr value) : base(value, ownsHandle: false) { } + public override bool IsInvalid => handle == IntPtr.Zero; + public static implicit operator IntPtr(ConfigHandle h) => h.handle; + public static implicit operator tiledb_config_t*(ConfigHandle h) => (tiledb_config_t*)h.handle; + public static implicit operator ConfigHandle(tiledb_config_t* value) => new ConfigHandle((IntPtr)value); + }//public unsafe partial class ConfigHandle + + [Obsolete(Obsoletions.TileDBInteropMessage, DiagnosticId = Obsoletions.TileDBInteropDiagId, UrlFormat = Obsoletions.SharedUrlFormat)] + [EditorBrowsable(EditorBrowsableState.Never)] + public unsafe class ConfigIteratorHandle : SafeHandle + { + // Constructor for a Handle + // - calls native allocator + // - exception on failure + + public ConfigIteratorHandle(ConfigHandle hconfig, string prefix) : base(IntPtr.Zero, ownsHandle: true) + { + var h = stackalloc tiledb_config_iter_t*[1]; + var e = stackalloc tiledb_error_t*[1]; + var ms_prefix = new MarshaledString(prefix); + Methods.tiledb_config_iter_alloc(hconfig, ms_prefix, h, e); + + if (h[0] == (void*)0) + { + throw new Exception("Failed to allocate!"); + } + SetHandle(h[0]); + } + + // Deallocator: call native free with CER guarantees from SafeHTha andle + override protected bool ReleaseHandle() + { + // Free the native object + var p = (tiledb_config_iter_t*)handle; + Methods.tiledb_config_iter_free(&p); + // Invalidate the contained pointer + SetHandle(IntPtr.Zero); + + return true; + } + + // Conversions, getters, operators + public ulong Get() { return (ulong)handle; } + private protected void SetHandle(tiledb_config_iter_t* h) { SetHandle((IntPtr)h); } + private protected ConfigIteratorHandle(IntPtr value) : base(value, ownsHandle: false) { } + public override bool IsInvalid => handle == IntPtr.Zero; + public static implicit operator IntPtr(ConfigIteratorHandle h) => h.handle; + public static implicit operator tiledb_config_iter_t*(ConfigIteratorHandle h) => (tiledb_config_iter_t*)h.handle; + public static implicit operator ConfigIteratorHandle(tiledb_config_iter_t* value) => new ConfigIteratorHandle((IntPtr)value); + }//public unsafe partial class ConfigHandle + + [Obsolete(Obsoletions.TileDBInteropMessage, DiagnosticId = Obsoletions.TileDBInteropDiagId, UrlFormat = Obsoletions.SharedUrlFormat)] + [EditorBrowsable(EditorBrowsableState.Never)] + public unsafe class ContextHandle : SafeHandle + { + // Constructor for a Handle + // - calls native allocator + // - exception on failure + public ContextHandle() : base(IntPtr.Zero, ownsHandle: true) + { + var h = stackalloc tiledb_ctx_t*[1]; + var hconfig = new ConfigHandle(); + Methods.tiledb_ctx_alloc(hconfig, h); + + if (h[0] == (void*)0) + { + throw new Exception("Failed to allocate!"); + } + SetHandle(h[0]); + } + + public ContextHandle(ConfigHandle hconfig) : base(IntPtr.Zero, ownsHandle: true) + { + var h = stackalloc tiledb_ctx_t*[1]; + Methods.tiledb_ctx_alloc(hconfig, h); + + if (h[0] == (void*)0) + { + throw new Exception("Failed to allocate!"); + } + SetHandle(h[0]); + } + + // Deallocator: call native free with CER guarantees from SafeHandle + override protected bool ReleaseHandle() + { + // Free the native object + var p = (tiledb_ctx_t*)handle; + Methods.tiledb_ctx_free(&p); + // Invalidate the contained pointer + SetHandle(IntPtr.Zero); + + return true; + } + + // Conversions, getters, operators + public ulong Get() { return (ulong)handle; } + private protected void SetHandle(tiledb_ctx_t* h) { SetHandle((IntPtr)h); } + private protected ContextHandle(IntPtr value) : base(value, ownsHandle: false) { } + public override bool IsInvalid => handle == IntPtr.Zero; + public static implicit operator IntPtr(ContextHandle h) => h.handle; + public static implicit operator tiledb_ctx_t*(ContextHandle h) => (tiledb_ctx_t*)h.handle; + public static implicit operator ContextHandle(tiledb_ctx_t* value) => new ContextHandle((IntPtr)value); + }//public unsafe partial class ContextHandle + + [Obsolete(Obsoletions.TileDBInteropMessage, DiagnosticId = Obsoletions.TileDBInteropDiagId, UrlFormat = Obsoletions.SharedUrlFormat)] + [EditorBrowsable(EditorBrowsableState.Never)] + public unsafe class FilterHandle : SafeHandle + { + // Constructor for a Handle + // - calls native allocator + // - exception on failure + + public FilterHandle(ContextHandle hcontext, tiledb_filter_type_t filterType) : base(IntPtr.Zero, ownsHandle: true) + { + var h = stackalloc tiledb_filter_t*[1]; + + Methods.tiledb_filter_alloc(hcontext, filterType, h); + + if (h[0] == (void*)0) + { + throw new Exception("Failed to allocate!"); + } + SetHandle(h[0]); + } + + // Deallocator: call native free with CER guarantees from SafeHandle + override protected bool ReleaseHandle() + { + // Free the native object + var p = (tiledb_filter_t*)handle; + Methods.tiledb_filter_free(&p); + // Invalidate the contained pointer + SetHandle(IntPtr.Zero); + + return true; + } + + // Conversions, getters, operators + public ulong Get() { return (ulong)handle; } + private protected void SetHandle(tiledb_filter_t* h) { SetHandle((IntPtr)h); } + private protected FilterHandle(IntPtr value) : base(value, ownsHandle: false) { } + public override bool IsInvalid => handle == IntPtr.Zero; + public static implicit operator IntPtr(FilterHandle h) => h.handle; + public static implicit operator tiledb_filter_t*(FilterHandle h) => (tiledb_filter_t*)h.handle; + public static implicit operator FilterHandle(tiledb_filter_t* value) => new FilterHandle((IntPtr)value); + }//public unsafe partial class FilterHandle + + + [Obsolete(Obsoletions.TileDBInteropMessage, DiagnosticId = Obsoletions.TileDBInteropDiagId, UrlFormat = Obsoletions.SharedUrlFormat)] + [EditorBrowsable(EditorBrowsableState.Never)] + public unsafe class FilterListHandle : SafeHandle + { + // Constructor for a Handle + // - calls native allocator + // - exception on failure + public FilterListHandle(ContextHandle hcontext) : base(IntPtr.Zero, ownsHandle: true) + { + var h = stackalloc tiledb_filter_list_t*[1]; + + Methods.tiledb_filter_list_alloc(hcontext, h); + + if (h[0] == (void*)0) + { + throw new Exception("Failed to allocate!"); + } + SetHandle(h[0]); + } + + // Deallocator: call native free with CER guarantees from SafeHandle + override protected bool ReleaseHandle() + { + // Free the native object + var p = (tiledb_filter_list_t*)handle; + Methods.tiledb_filter_list_free(&p); + // Invalidate the contained pointer + SetHandle(IntPtr.Zero); + + return true; + } + + // Conversions, getters, operators + public ulong Get() { return (ulong)handle; } + private protected void SetHandle(tiledb_filter_list_t* h) { SetHandle((IntPtr)h); } + private protected FilterListHandle(IntPtr value) : base(value, ownsHandle: false) { } + public override bool IsInvalid => handle == IntPtr.Zero; + public static implicit operator IntPtr(FilterListHandle h) => h.handle; + public static implicit operator tiledb_filter_list_t*(FilterListHandle h) => (tiledb_filter_list_t*)h.handle; + public static implicit operator FilterListHandle(tiledb_filter_list_t* value) => new FilterListHandle((IntPtr)value); + }//public unsafe partial class FilterListHandle + + [Obsolete(Obsoletions.TileDBInteropMessage, DiagnosticId = Obsoletions.TileDBInteropDiagId, UrlFormat = Obsoletions.SharedUrlFormat)] + [EditorBrowsable(EditorBrowsableState.Never)] + public unsafe partial class VFSHandle : SafeHandle + { + // Constructor for a Handle + // - calls native allocator + // - exception on failure + + public VFSHandle(ContextHandle hcontext, ConfigHandle hconfig) : base(IntPtr.Zero, ownsHandle: true) + { + var h = stackalloc tiledb_vfs_t*[1]; + + int status = TileDB.Interop.Methods.tiledb_vfs_alloc(hcontext, hconfig, h); + + if (h[0] == (void*)0) + { + throw new Exception("Failed to allocate!"); + } + SetHandle(h[0]); + } + + // Deallocator: call native free with CER guarantees from SafeHandle + override protected bool ReleaseHandle() + { + // Free the native object + tiledb_vfs_t* p = (tiledb_vfs_t*)handle; + TileDB.Interop.Methods.tiledb_vfs_free(&p); + // Invalidate the contained pointer + SetHandle(IntPtr.Zero); + + return true; + } + + // Conversions, getters, operators + public UInt64 get() { return (UInt64)this.handle; } + private protected void SetHandle(tiledb_vfs_t* h) { SetHandle((IntPtr)h); } + private protected VFSHandle(IntPtr value) : base(value, ownsHandle: false) { } + public override bool IsInvalid => this.handle == IntPtr.Zero; + public static implicit operator IntPtr(VFSHandle h) => h.handle; + public static implicit operator tiledb_vfs_t*(VFSHandle h) => (tiledb_vfs_t*)h.handle; + public static implicit operator VFSHandle(tiledb_vfs_t* value) => new VFSHandle((IntPtr)value); + }//public unsafe partial class VFSHandle + + + [Obsolete(Obsoletions.TileDBInteropMessage, DiagnosticId = Obsoletions.TileDBInteropDiagId, UrlFormat = Obsoletions.SharedUrlFormat)] + [EditorBrowsable(EditorBrowsableState.Never)] + public unsafe class AttributeHandle : SafeHandle + { + // Constructor for a Handle + // - calls native allocator + // - exception on failure + public AttributeHandle(ContextHandle hcontext, string name, tiledb_datatype_t datatype) : base(IntPtr.Zero, ownsHandle: true) + { + var h = stackalloc tiledb_attribute_t*[1]; + + var ms_name = new MarshaledString(name); + Methods.tiledb_attribute_alloc(hcontext, ms_name, datatype, h); + + if (h[0] == (void*)0) + { + throw new Exception("Failed to allocate!"); + } + SetHandle(h[0]); + } + + // Deallocator: call native free with CER guarantees from SafeHandle + override protected bool ReleaseHandle() + { + // Free the native object + var p = (tiledb_attribute_t*)handle; + Methods.tiledb_attribute_free(&p); + // Invalidate the contained pointer + SetHandle(IntPtr.Zero); + + return true; + } + + // Conversions, getters, operators + public ulong Get() { return (ulong)handle; } + private protected void SetHandle(tiledb_attribute_t* h) { SetHandle((IntPtr)h); } + private protected AttributeHandle(IntPtr value) : base(value, ownsHandle: false) { } + public override bool IsInvalid => handle == IntPtr.Zero; + public static implicit operator IntPtr(AttributeHandle h) => h.handle; + public static implicit operator tiledb_attribute_t*(AttributeHandle h) => (tiledb_attribute_t*)h.handle; + public static implicit operator AttributeHandle(tiledb_attribute_t* value) => new AttributeHandle((IntPtr)value); + }//public unsafe partial class AttributeHandle + + [Obsolete(Obsoletions.TileDBInteropMessage, DiagnosticId = Obsoletions.TileDBInteropDiagId, UrlFormat = Obsoletions.SharedUrlFormat)] + [EditorBrowsable(EditorBrowsableState.Never)] + public unsafe class DimensionHandle : SafeHandle + { + // Constructor for a Handle + // - calls native allocator + // - exception on failure + + public DimensionHandle(ContextHandle hcontext, string name, tiledb_datatype_t datatype, void* dimDomain, void* tileExtent) : base(IntPtr.Zero, ownsHandle: true) + { + var h = stackalloc tiledb_dimension_t*[1]; + + var ms_name = new MarshaledString(name); + + Methods.tiledb_dimension_alloc(hcontext, ms_name, datatype, dimDomain, tileExtent, h); + + if (h[0] == (void*)0) + { + throw new Exception("Failed to allocate!"); + } + SetHandle(h[0]); + } + + // Deallocator: call native free with CER guarantees from SafeHandle + override protected bool ReleaseHandle() + { + // Free the native object + var p = (tiledb_dimension_t*)handle; + Methods.tiledb_dimension_free(&p); + // Invalidate the contained pointer + SetHandle(IntPtr.Zero); + + return true; + } + + // Conversions, getters, operators + public ulong Get() { return (ulong)handle; } + private protected void SetHandle(tiledb_dimension_t* h) { SetHandle((IntPtr)h); } + private protected DimensionHandle(IntPtr value) : base(value, ownsHandle: false) { } + public override bool IsInvalid => handle == IntPtr.Zero; + public static implicit operator IntPtr(DimensionHandle h) => h.handle; + public static implicit operator tiledb_dimension_t*(DimensionHandle h) => (tiledb_dimension_t*)h.handle; + public static implicit operator DimensionHandle(tiledb_dimension_t* value) => new DimensionHandle((IntPtr)value); + }//public unsafe partial class DimensionHandle + + + [Obsolete(Obsoletions.TileDBInteropMessage, DiagnosticId = Obsoletions.TileDBInteropDiagId, UrlFormat = Obsoletions.SharedUrlFormat)] + [EditorBrowsable(EditorBrowsableState.Never)] + public unsafe class DomainHandle : SafeHandle + { + // Constructor for a Handle + // - calls native allocator + // - exception on failure + + public DomainHandle(ContextHandle hcontext) : base(IntPtr.Zero, ownsHandle: true) + { + var h = stackalloc tiledb_domain_t*[1]; + + Methods.tiledb_domain_alloc(hcontext, h); + + if (h[0] == (void*)0) + { + throw new Exception("Failed to allocate!"); + } + SetHandle(h[0]); + } + + // Deallocator: call native free with CER guarantees from SafeHandle + override protected bool ReleaseHandle() + { + // Free the native object + var p = (tiledb_domain_t*)handle; + Methods.tiledb_domain_free(&p); + // Invalidate the contained pointer + SetHandle(IntPtr.Zero); + + return true; + } + + // Conversions, getters, operators + public ulong Get() { return (ulong)handle; } + private protected void SetHandle(tiledb_domain_t* h) { SetHandle((IntPtr)h); } + private protected DomainHandle(IntPtr value) : base(value, ownsHandle: false) { } + public override bool IsInvalid => handle == IntPtr.Zero; + public static implicit operator IntPtr(DomainHandle h) => h.handle; + public static implicit operator tiledb_domain_t*(DomainHandle h) => (tiledb_domain_t*)h.handle; + public static implicit operator DomainHandle(tiledb_domain_t* value) => new DomainHandle((IntPtr)value); + }//public unsafe partial class DomainHandle + + + [Obsolete(Obsoletions.TileDBInteropMessage, DiagnosticId = Obsoletions.TileDBInteropDiagId, UrlFormat = Obsoletions.SharedUrlFormat)] + [EditorBrowsable(EditorBrowsableState.Never)] + public unsafe class ArraySchemaHandle : SafeHandle + { + // Constructor for a Handle + // - calls native allocator + // - exception on failure + public ArraySchemaHandle(ContextHandle contextHandle, tiledb_array_type_t arrayType) : base(IntPtr.Zero, ownsHandle: true) + { + var h = stackalloc tiledb_array_schema_t*[1]; + + Methods.tiledb_array_schema_alloc(contextHandle, arrayType, h); + + if (h[0] == (void*)0) + { + throw new Exception("Failed to allocate!"); + } + SetHandle(h[0]); + } + + // Deallocator: call native free with CER guarantees from SafeHandle + override protected bool ReleaseHandle() + { + // Free the native object + var p = (tiledb_array_schema_t*)handle; + Methods.tiledb_array_schema_free(&p); + // Invalidate the contained pointer + SetHandle(IntPtr.Zero); + + return true; + } + + // Conversions, getters, operators + public ulong Get() { return (ulong)handle; } + private protected void SetHandle(tiledb_array_schema_t* h) { SetHandle((IntPtr)h); } + private protected ArraySchemaHandle(IntPtr value) : base(value, ownsHandle: false) { } + public override bool IsInvalid => handle == IntPtr.Zero; + public static implicit operator IntPtr(ArraySchemaHandle h) => h.handle; + public static implicit operator tiledb_array_schema_t*(ArraySchemaHandle h) => (tiledb_array_schema_t*)h.handle; + public static implicit operator ArraySchemaHandle(tiledb_array_schema_t* value) => new ArraySchemaHandle((IntPtr)value); + }//public unsafe partial class ArraySchemaHandle + + [Obsolete(Obsoletions.TileDBInteropMessage, DiagnosticId = Obsoletions.TileDBInteropDiagId, UrlFormat = Obsoletions.SharedUrlFormat)] + [EditorBrowsable(EditorBrowsableState.Never)] + public unsafe class ArrayHandle : SafeHandle + { + // Constructor for a Handle + // - calls native allocator + // - exception on failure + public ArrayHandle(ContextHandle contextHandle, sbyte* uri) : base(IntPtr.Zero, ownsHandle: true) + { + var h = stackalloc tiledb_array_t*[1]; + + Methods.tiledb_array_alloc(contextHandle, uri, h); + + if (h[0] == (void*)0) + { + throw new Exception("Failed to allocate!"); + } + SetHandle(h[0]); + } + + // Deallocator: call native free with CER guarantees from SafeHandle + override protected bool ReleaseHandle() + { + // Free the native object + var p = (tiledb_array_t*)handle; + Methods.tiledb_array_free(&p); + // Invalidate the contained pointer + SetHandle(IntPtr.Zero); + + return true; + } + + // Conversions, getters, operators + public ulong Get() { return (ulong)handle; } + private protected void SetHandle(tiledb_array_t* h) { SetHandle((IntPtr)h); } + private protected ArrayHandle(IntPtr value) : base(value, ownsHandle: false) { } + public override bool IsInvalid => handle == IntPtr.Zero; + public static implicit operator IntPtr(ArrayHandle h) => h.handle; + public static implicit operator tiledb_array_t*(ArrayHandle h) => (tiledb_array_t*)h.handle; + public static implicit operator ArrayHandle(tiledb_array_t* value) => new ArrayHandle((IntPtr)value); + }//public unsafe partial class ArrayHandle + + [Obsolete(Obsoletions.TileDBInteropMessage, DiagnosticId = Obsoletions.TileDBInteropDiagId, UrlFormat = Obsoletions.SharedUrlFormat)] + [EditorBrowsable(EditorBrowsableState.Never)] + public unsafe class ArraySchemaEvolutionHandle : SafeHandle + { + // Constructor for a Handle + // - calls native allocator + // - exception on failure + public ArraySchemaEvolutionHandle(ContextHandle contextHandle) : base(IntPtr.Zero, ownsHandle: true) + { + var h = stackalloc tiledb_array_schema_evolution_t*[1]; + Methods.tiledb_array_schema_evolution_alloc(contextHandle, h); + + if (h[0] == (void*)0) + { + throw new Exception("Failed to allocate ArraySchemaEvolutionHandle!"); + } + SetHandle(h[0]); + } + + // Deallocator: call native free with CER guarantees from SafeHandle + protected override bool ReleaseHandle() + { + var p = (tiledb_array_schema_evolution_t*)handle; + Methods.tiledb_array_schema_evolution_free(&p); + SetHandle(IntPtr.Zero); + return true; + } + + // Conversions, getters, operators + public ulong Get() => (ulong)handle; + private protected void SetHandle(tiledb_array_schema_evolution_t* h) => SetHandle((IntPtr)h); + private protected ArraySchemaEvolutionHandle(IntPtr value) : base(value, ownsHandle: false) { } + public override bool IsInvalid => handle == IntPtr.Zero; + public static implicit operator IntPtr(ArraySchemaEvolutionHandle h) => h.handle; + public static implicit operator tiledb_array_schema_evolution_t*(ArraySchemaEvolutionHandle h) => + (tiledb_array_schema_evolution_t*)h.handle; + public static implicit operator ArraySchemaEvolutionHandle(tiledb_array_schema_evolution_t* value) => + new ArraySchemaEvolutionHandle((IntPtr)value); + }//public unsafe class ArraySchemaEvolutionHandle + + [Obsolete(Obsoletions.TileDBInteropMessage, DiagnosticId = Obsoletions.TileDBInteropDiagId, UrlFormat = Obsoletions.SharedUrlFormat)] + [EditorBrowsable(EditorBrowsableState.Never)] + public unsafe class QueryHandle : SafeHandle + { + // Constructor for a Handle + // - calls native allocator + // - exception on failure + public QueryHandle(ContextHandle contextHandle, ArrayHandle arrayHandle, tiledb_query_type_t queryType) : base(IntPtr.Zero, ownsHandle: true) + { + var h = stackalloc tiledb_query_t*[1]; + + Methods.tiledb_query_alloc(contextHandle, arrayHandle, queryType, h); + + if (h[0] == (void*)0) + { + throw new Exception("Failed to allocate!"); + } + SetHandle(h[0]); + } + + // Deallocator: call native free with CER guarantees from SafeHandle + override protected bool ReleaseHandle() + { + // Free the native object + var p = (tiledb_query_t*)handle; + Methods.tiledb_query_free(&p); + // Invalidate the contained pointer + SetHandle(IntPtr.Zero); + + return true; + } + + // Conversions, getters, operators + public ulong Get() { return (ulong)handle; } + private protected void SetHandle(tiledb_query_t* h) { SetHandle((IntPtr)h); } + private protected QueryHandle(IntPtr value) : base(value, ownsHandle: false) { } + public override bool IsInvalid => handle == IntPtr.Zero; + public static implicit operator IntPtr(QueryHandle h) => h.handle; + public static implicit operator tiledb_query_t*(QueryHandle h) => (tiledb_query_t*)h.handle; + public static implicit operator QueryHandle(tiledb_query_t* value) => new QueryHandle((IntPtr)value); + }//public unsafe partial class QueryHandle + + + [Obsolete(Obsoletions.TileDBInteropMessage, DiagnosticId = Obsoletions.TileDBInteropDiagId, UrlFormat = Obsoletions.SharedUrlFormat)] + [EditorBrowsable(EditorBrowsableState.Never)] + public unsafe class QueryConditionHandle : SafeHandle + { + // Constructor for a Handle + // - calls native allocator + // - exception on failure + public QueryConditionHandle(ContextHandle contextHandle) : base(IntPtr.Zero, ownsHandle: true) + { + var h = stackalloc tiledb_query_condition_t*[1]; + + Methods.tiledb_query_condition_alloc(contextHandle, h); + + if (h[0] == (void*)0) + { + throw new Exception("Failed to allocate!"); + } + SetHandle(h[0]); + } + + // Deallocator: call native free with CER guarantees from SafeHandle + override protected bool ReleaseHandle() + { + // Free the native object + var p = (tiledb_query_condition_t*)handle; + Methods.tiledb_query_condition_free(&p); + // Invalidate the contained pointer + SetHandle(IntPtr.Zero); + + return true; + } + + // Conversions, getters, operators + public ulong Get() { return (ulong)handle; } + private protected void SetHandle(tiledb_query_condition_t* h) { SetHandle((IntPtr)h); } + private protected QueryConditionHandle(IntPtr value) : base(value, ownsHandle: false) { } + public override bool IsInvalid => handle == IntPtr.Zero; + public static implicit operator IntPtr(QueryConditionHandle h) => h.handle; + public static implicit operator tiledb_query_condition_t*(QueryConditionHandle h) => (tiledb_query_condition_t*)h.handle; + public static implicit operator QueryConditionHandle(tiledb_query_condition_t* value) => new QueryConditionHandle((IntPtr)value); + }//public unsafe partial class QueryConditionHandle + + [Obsolete(Obsoletions.TileDBInteropMessage, DiagnosticId = Obsoletions.TileDBInteropDiagId, UrlFormat = Obsoletions.SharedUrlFormat)] + [EditorBrowsable(EditorBrowsableState.Never)] + public unsafe class GroupHandle : SafeHandle + { + // Constructor for a Handle + // - calls native allocator + // - exception on failure + public GroupHandle(ContextHandle contextHandle, sbyte* uri) : base(IntPtr.Zero, ownsHandle: true) + { + var h = stackalloc tiledb_group_t*[1]; + + Methods.tiledb_group_alloc(contextHandle, uri, h); + + if (h[0] == (void*)0) + { + throw new Exception("Failed to allocate!"); + } + SetHandle(h[0]); + } + + // Deallocator: call native free with CER guarantees from SafeHandle + override protected bool ReleaseHandle() + { + // Free the native object + var p = (tiledb_group_t*)handle; + Methods.tiledb_group_free(&p); + // Invalidate the contained pointer + SetHandle(IntPtr.Zero); + + return true; + } + + // Conversions, getters, operators + public ulong Get() { return (ulong)handle; } + private protected void SetHandle(tiledb_group_t* h) { SetHandle((IntPtr)h); } + private protected GroupHandle(IntPtr value) : base(value, ownsHandle: false) { } + public override bool IsInvalid => handle == IntPtr.Zero; + public static implicit operator IntPtr(GroupHandle h) => h.handle; + public static implicit operator tiledb_group_t*(GroupHandle h) => (tiledb_group_t*)h.handle; + public static implicit operator GroupHandle(tiledb_group_t* value) => new GroupHandle((IntPtr)value); + }//public unsafe partial class GroupHandle + +}//namespace \ No newline at end of file diff --git a/sources/TileDB.CSharp/Interop/tiledb_filter_webp_format_t.cs b/sources/TileDB.CSharp/Interop/tiledb_filter_webp_format_t.cs index 10791055..b911f0c2 100644 --- a/sources/TileDB.CSharp/Interop/tiledb_filter_webp_format_t.cs +++ b/sources/TileDB.CSharp/Interop/tiledb_filter_webp_format_t.cs @@ -1,7 +1,11 @@ // +using System; +using TileDB.CSharp; + namespace TileDB.Interop { + [Obsolete(Obsoletions.TileDBInteropMessage + " Use the TileDB.CSharp.WebpInputFormat type instead.", DiagnosticId = Obsoletions.TileDBInteropDiagId, UrlFormat = Obsoletions.SharedUrlFormat)] public enum tiledb_filter_webp_format_t { TILEDB_WEBP_NONE = 0, diff --git a/sources/TileDB.CSharp/Marshalling/MarshaledString.cs b/sources/TileDB.CSharp/Marshalling/MarshaledString.cs index 4bebefa4..ae56bf06 100644 --- a/sources/TileDB.CSharp/Marshalling/MarshaledString.cs +++ b/sources/TileDB.CSharp/Marshalling/MarshaledString.cs @@ -1,13 +1,17 @@ // Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information. using System; +using System.ComponentModel; using System.Diagnostics; using System.Runtime.InteropServices; using System.Text; +using TileDB.CSharp; namespace TileDB.Interop { - internal unsafe struct MarshaledString : IDisposable + [Obsolete(Obsoletions.TileDBInteropMessage, DiagnosticId = Obsoletions.TileDBInteropDiagId, UrlFormat = Obsoletions.SharedUrlFormat)] + [EditorBrowsable(EditorBrowsableState.Never)] + public unsafe struct MarshaledString : IDisposable { public MarshaledString(string input) { @@ -15,7 +19,7 @@ public MarshaledString(string input) Value = (sbyte*)ptr; } - public static (IntPtr Pointer, int Length) AllocNullTerminated(string str) + internal static (IntPtr Pointer, int Length) AllocNullTerminated(string str) { if (str is null) { @@ -30,7 +34,9 @@ public static (IntPtr Pointer, int Length) AllocNullTerminated(string str) return ((IntPtr)ptr, length); } - public static void FreeNullTerminated(IntPtr ptr) => Marshal.FreeHGlobal(ptr); + internal static void FreeNullTerminated(IntPtr ptr) => Marshal.FreeHGlobal(ptr); + + public ReadOnlySpan AsSpan() => new ReadOnlySpan(Value, Length); public int Length { get; private set; } diff --git a/sources/TileDB.CSharp/Marshalling/MarshaledStringOut.cs b/sources/TileDB.CSharp/Marshalling/MarshaledStringOut.cs index f6437494..9e6645bb 100644 --- a/sources/TileDB.CSharp/Marshalling/MarshaledStringOut.cs +++ b/sources/TileDB.CSharp/Marshalling/MarshaledStringOut.cs @@ -1,18 +1,36 @@ using System; +using System.ComponentModel; using System.Text; using TileDB.CSharp; namespace TileDB.Interop { - internal static class MarshaledStringOut + [Obsolete(Obsoletions.TileDBInteropMessage, DiagnosticId = Obsoletions.TileDBInteropDiagId, UrlFormat = Obsoletions.SharedUrlFormat)] + [EditorBrowsable(EditorBrowsableState.Never)] + public unsafe class LibC { + // public struct handle_t {} + // [DllImport(LibDllImport.LibCPath)] + // public static extern void free(void* p); + } + + [Obsolete(Obsoletions.TileDBInteropMessage, DiagnosticId = Obsoletions.TileDBInteropDiagId, UrlFormat = Obsoletions.SharedUrlFormat)] + [EditorBrowsable(EditorBrowsableState.Never)] + public unsafe class MarshaledStringOut + { + public sbyte* Value; + public MarshaledStringOut() + { + Value = null; + } + /// /// Encodes a read-only span of bytes into a string, using the default encoding. /// - public static string GetString(ReadOnlySpan span) => + internal static string GetString(ReadOnlySpan span) => Encoding.ASCII.GetString(span); - public static string GetString(ReadOnlySpan span, DataType dataType) => + internal static string GetString(ReadOnlySpan span, DataType dataType) => dataType switch { DataType.StringAscii => Encoding.ASCII.GetString(span), @@ -25,7 +43,7 @@ public static string GetString(ReadOnlySpan span, DataType dataType) => /// /// Encodes a null-terminated pointer of bytes into a string, using the default encoding. /// - public static unsafe string GetStringFromNullTerminated(sbyte* ptr) + internal static unsafe string GetStringFromNullTerminated(sbyte* ptr) { if (ptr == null) { @@ -36,5 +54,7 @@ public static unsafe string GetStringFromNullTerminated(sbyte* ptr) span = span[0..span.IndexOf((byte)0)]; return GetString(span); } + + public static implicit operator string(MarshaledStringOut s) => GetStringFromNullTerminated(s.Value); } } diff --git a/sources/TileDB.CSharp/Query.cs b/sources/TileDB.CSharp/Query.cs index c8d40ceb..0666db8e 100644 --- a/sources/TileDB.CSharp/Query.cs +++ b/sources/TileDB.CSharp/Query.cs @@ -10,6 +10,8 @@ using System.Threading.Tasks; using TileDB.CSharp.Marshalling.SafeHandles; using TileDB.Interop; +using QueryHandle = TileDB.CSharp.Marshalling.SafeHandles.QueryHandle; +using ConfigHandle = TileDB.CSharp.Marshalling.SafeHandles.ConfigHandle; namespace TileDB.CSharp { diff --git a/sources/TileDB.CSharp/QueryCondition.cs b/sources/TileDB.CSharp/QueryCondition.cs index 1a92eb65..be6f6fa1 100644 --- a/sources/TileDB.CSharp/QueryCondition.cs +++ b/sources/TileDB.CSharp/QueryCondition.cs @@ -1,6 +1,7 @@ using System; using TileDB.CSharp.Marshalling.SafeHandles; using TileDB.Interop; +using QueryConditionHandle = TileDB.CSharp.Marshalling.SafeHandles.QueryConditionHandle; // We are allowed to construct and initialize query conditions. #pragma warning disable TILEDB0007 diff --git a/sources/TileDB.CSharp/TileDB.CSharp.csproj b/sources/TileDB.CSharp/TileDB.CSharp.csproj index d0f6d0e0..f71f573d 100644 --- a/sources/TileDB.CSharp/TileDB.CSharp.csproj +++ b/sources/TileDB.CSharp/TileDB.CSharp.csproj @@ -6,7 +6,7 @@ true enable TileDB.CSharp - 5.3.0 + 5.3.1 C# wrapper of the TileDB Embedded universal data engine. README.md true diff --git a/sources/TileDB.CSharp/VFS.cs b/sources/TileDB.CSharp/VFS.cs index 20d8dd6d..a4d762af 100644 --- a/sources/TileDB.CSharp/VFS.cs +++ b/sources/TileDB.CSharp/VFS.cs @@ -6,6 +6,8 @@ using System.Runtime.InteropServices; using TileDB.CSharp.Marshalling.SafeHandles; using TileDB.Interop; +using ConfigHandle = TileDB.CSharp.Marshalling.SafeHandles.ConfigHandle; +using VFSHandle = TileDB.CSharp.Marshalling.SafeHandles.VFSHandle; namespace TileDB.CSharp {