|
| 1 | +// BLite.Client — RemoteDocumentCollection<TId, T> |
| 2 | +// Copyright (C) 2026 Luca Fabbri — AGPL-3.0 |
| 3 | +// |
| 4 | +// IDocumentCollection<TId,T> wrapper around RemoteCollection<TId,T>. |
| 5 | +// Sync operations and local-engine-only features throw NotSupportedException. |
| 6 | + |
| 7 | +using BLite.Bson; |
| 8 | +using BLite.Core.Collections; |
| 9 | +using BLite.Core.Indexing; |
| 10 | +using BLite.Core.Query; |
| 11 | +using System.Linq.Expressions; |
| 12 | + |
| 13 | +namespace BLite.Client.Collections; |
| 14 | + |
| 15 | +/// <summary> |
| 16 | +/// Remote implementation of <see cref="IDocumentCollection{TId,T}"/> that |
| 17 | +/// delegates typed CRUD operations to a <see cref="RemoteCollection{TId,T}"/> |
| 18 | +/// over gRPC. |
| 19 | +/// |
| 20 | +/// <para> |
| 21 | +/// Sync operations and local-engine-only features (indexes, scans, ForcePrune) |
| 22 | +/// throw <see cref="NotSupportedException"/>. Use the <c>Async</c> overloads. |
| 23 | +/// </para> |
| 24 | +/// </summary> |
| 25 | +public sealed class RemoteDocumentCollection<TId, T> : IDocumentCollection<TId, T> |
| 26 | + where T : class |
| 27 | +{ |
| 28 | + private readonly RemoteCollection<TId, T> _inner; |
| 29 | + |
| 30 | + internal RemoteDocumentCollection(RemoteCollection<TId, T> inner) => _inner = inner; |
| 31 | + |
| 32 | + // ── Metadata ────────────────────────────────────────────────────────────── |
| 33 | + |
| 34 | + public SchemaVersion? CurrentSchemaVersion => null; |
| 35 | + |
| 36 | + // ── Insert ──────────────────────────────────────────────────────────────── |
| 37 | + |
| 38 | + public TId Insert(T entity) => |
| 39 | + throw new NotSupportedException("Use InsertAsync for remote collections."); |
| 40 | + |
| 41 | + public Task<TId> InsertAsync(T entity, CancellationToken ct = default) => |
| 42 | + _inner.InsertAsync(entity, null, ct); |
| 43 | + |
| 44 | + public List<TId> InsertBulk(IEnumerable<T> entities) => |
| 45 | + throw new NotSupportedException("Use InsertBulkAsync for remote collections."); |
| 46 | + |
| 47 | + public async Task<List<TId>> InsertBulkAsync(IEnumerable<T> entities, CancellationToken ct = default) => |
| 48 | + (await _inner.InsertBulkAsync(entities, null, ct)).ToList(); |
| 49 | + |
| 50 | + // ── Read ────────────────────────────────────────────────────────────────── |
| 51 | + |
| 52 | + public T? FindById(TId id) => |
| 53 | + throw new NotSupportedException("Use FindByIdAsync for remote collections."); |
| 54 | + |
| 55 | + public async ValueTask<T?> FindByIdAsync(TId id, CancellationToken ct = default) => |
| 56 | + await _inner.FindByIdAsync(id, ct); |
| 57 | + |
| 58 | + public IAsyncEnumerable<T> FindAllAsync(CancellationToken ct = default) => |
| 59 | + _inner.FindAllAsync(ct); |
| 60 | + |
| 61 | + public IAsyncEnumerable<T> FindAsync(Func<T, bool> predicate, CancellationToken ct = default) => |
| 62 | + _inner.FindAsync(predicate, ct); |
| 63 | + |
| 64 | + public IBLiteQueryable<T> AsQueryable() => _inner.AsQueryable(); |
| 65 | + |
| 66 | + // ── Update ──────────────────────────────────────────────────────────────── |
| 67 | + |
| 68 | + public bool Update(T entity) => |
| 69 | + throw new NotSupportedException("Use UpdateAsync for remote collections."); |
| 70 | + |
| 71 | + public Task<bool> UpdateAsync(T entity, CancellationToken ct = default) => |
| 72 | + _inner.UpdateAsync(entity, null, ct); |
| 73 | + |
| 74 | + public int UpdateBulk(IEnumerable<T> entities) => |
| 75 | + throw new NotSupportedException("Use UpdateBulkAsync for remote collections."); |
| 76 | + |
| 77 | + public Task<int> UpdateBulkAsync(IEnumerable<T> entities, CancellationToken ct = default) => |
| 78 | + _inner.UpdateBulkAsync(entities, null, ct); |
| 79 | + |
| 80 | + // ── Delete ──────────────────────────────────────────────────────────────── |
| 81 | + |
| 82 | + public bool Delete(TId id) => |
| 83 | + throw new NotSupportedException("Use DeleteAsync for remote collections."); |
| 84 | + |
| 85 | + public Task<bool> DeleteAsync(TId id, CancellationToken ct = default) => |
| 86 | + _inner.DeleteAsync(id, null, ct); |
| 87 | + |
| 88 | + public int DeleteBulk(IEnumerable<TId> ids) => |
| 89 | + throw new NotSupportedException("Use DeleteBulkAsync for remote collections."); |
| 90 | + |
| 91 | + public Task<int> DeleteBulkAsync(IEnumerable<TId> ids, CancellationToken ct = default) => |
| 92 | + _inner.DeleteBulkAsync(ids, null, ct); |
| 93 | + |
| 94 | + // ── Index management (not supported on remote) ──────────────────────────── |
| 95 | + |
| 96 | + public CollectionSecondaryIndex<TId, T> CreateIndex<TKey>( |
| 97 | + Expression<Func<T, TKey>> keySelector, string? name = null, bool unique = false) => |
| 98 | + throw new NotSupportedException("Index operations are not supported on remote collections."); |
| 99 | + |
| 100 | + public Task<CollectionSecondaryIndex<TId, T>> CreateIndexAsync<TKey>( |
| 101 | + Expression<Func<T, TKey>> keySelector, string? name = null, bool unique = false, |
| 102 | + CancellationToken ct = default) => |
| 103 | + throw new NotSupportedException("Index operations are not supported on remote collections."); |
| 104 | + |
| 105 | + public CollectionSecondaryIndex<TId, T> CreateVectorIndex<TKey>( |
| 106 | + Expression<Func<T, TKey>> keySelector, int dimensions, |
| 107 | + VectorMetric metric = VectorMetric.Cosine, string? name = null) => |
| 108 | + throw new NotSupportedException("Index operations are not supported on remote collections."); |
| 109 | + |
| 110 | + public Task<CollectionSecondaryIndex<TId, T>> CreateVectorIndexAsync<TKey>( |
| 111 | + Expression<Func<T, TKey>> keySelector, int dimensions, |
| 112 | + VectorMetric metric = VectorMetric.Cosine, string? name = null, |
| 113 | + CancellationToken ct = default) => |
| 114 | + throw new NotSupportedException("Index operations are not supported on remote collections."); |
| 115 | + |
| 116 | + public CollectionSecondaryIndex<TId, T> EnsureIndex<TKey>( |
| 117 | + Expression<Func<T, TKey>> keySelector, string? name = null, bool unique = false) => |
| 118 | + throw new NotSupportedException("Index operations are not supported on remote collections."); |
| 119 | + |
| 120 | + public Task<CollectionSecondaryIndex<TId, T>> EnsureIndexAsync<TKey>( |
| 121 | + Expression<Func<T, TKey>> keySelector, string? name = null, bool unique = false, |
| 122 | + CancellationToken ct = default) => |
| 123 | + throw new NotSupportedException("Index operations are not supported on remote collections."); |
| 124 | + |
| 125 | + public bool DropIndex(string name) => |
| 126 | + throw new NotSupportedException("Index operations are not supported on remote collections."); |
| 127 | + |
| 128 | + public Task<bool> DropIndexAsync(string name, CancellationToken ct = default) => |
| 129 | + throw new NotSupportedException("Index operations are not supported on remote collections."); |
| 130 | + |
| 131 | + public IEnumerable<CollectionIndexInfo> GetIndexes() => |
| 132 | + throw new NotSupportedException("Index operations are not supported on remote collections."); |
| 133 | + |
| 134 | + public CollectionSecondaryIndex<TId, T>? GetIndex(string name) => |
| 135 | + throw new NotSupportedException("Index operations are not supported on remote collections."); |
| 136 | + |
| 137 | + public IEnumerable<T> QueryIndex(string indexName, object? minKey, object? maxKey, bool ascending = true) => |
| 138 | + throw new NotSupportedException("Index operations are not supported on remote collections."); |
| 139 | + |
| 140 | + public IAsyncEnumerable<T> QueryIndexAsync( |
| 141 | + string indexName, object? minKey, object? maxKey, bool ascending = true, |
| 142 | + CancellationToken ct = default) => |
| 143 | + throw new NotSupportedException("Index operations are not supported on remote collections."); |
| 144 | + |
| 145 | + // ── Scan (not supported on remote) ─────────────────────────────────────── |
| 146 | + |
| 147 | + public IEnumerable<T> Scan(BsonReaderPredicate predicate) => |
| 148 | + throw new NotSupportedException("Scan is not supported on remote collections."); |
| 149 | + |
| 150 | + public IAsyncEnumerable<T> ScanAsync(BsonReaderPredicate predicate, CancellationToken ct = default) => |
| 151 | + throw new NotSupportedException("Scan is not supported on remote collections."); |
| 152 | + |
| 153 | + public IEnumerable<TResult> Scan<TResult>(BsonReaderProjector<TResult> projector) => |
| 154 | + throw new NotSupportedException("Scan is not supported on remote collections."); |
| 155 | + |
| 156 | + public IAsyncEnumerable<TResult> ScanAsync<TResult>( |
| 157 | + BsonReaderProjector<TResult> projector, CancellationToken ct = default) => |
| 158 | + throw new NotSupportedException("Scan is not supported on remote collections."); |
| 159 | + |
| 160 | + public IEnumerable<T> ParallelScan(BsonReaderPredicate predicate, int degreeOfParallelism = -1) => |
| 161 | + throw new NotSupportedException("Scan is not supported on remote collections."); |
| 162 | + |
| 163 | + public IAsyncEnumerable<T> ParallelScanAsync( |
| 164 | + BsonReaderPredicate predicate, int degreeOfParallelism = -1, |
| 165 | + CancellationToken ct = default) => |
| 166 | + throw new NotSupportedException("Scan is not supported on remote collections."); |
| 167 | + |
| 168 | + // ── TimeSeries (not supported on remote) ───────────────────────────────── |
| 169 | + |
| 170 | + public void ForcePrune() => |
| 171 | + throw new NotSupportedException("ForcePrune is not supported on remote collections."); |
| 172 | +} |
0 commit comments