-
Notifications
You must be signed in to change notification settings - Fork 37
feat(dart): add TransformationOptions for ingestion transporter configuration #6321
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
Open
MarioAlexandruDan
wants to merge
22
commits into
main
Choose a base branch
from
feat/dart-transformation-options
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
8275a31
feat(dart): add transformationOptions and *WithTransformation helpers
MarioAlexandruDan 542a47e
Merge branch 'main' into feat/dart-transformation-options
MarioAlexandruDan 2bebb0b
feat(dart): enhance TransformationOptions and update ingestion transp…
MarioAlexandruDan 01ba397
Merge branch 'main' into feat/dart-transformation-options
MarioAlexandruDan 8fd0d02
feat(dart): add gradle wrapper and update transformation options
MarioAlexandruDan 44910b3
fix(dart): add algolia_client_ingestion dependency to client_search p…
MarioAlexandruDan ee6ec15
fix(dart): add missing extension.dart to client_ingestion package
MarioAlexandruDan 026e618
fix(dart): add algolia_client_ingestion to test pubspec and fix trail…
MarioAlexandruDan 5149674
Merge branch 'main' into feat/dart-transformation-options
MarioAlexandruDan 3555bd9
fix(dart): use Uri.parse instead of Uri.dataFromString for query para…
MarioAlexandruDan 00436de
fix(dart): fix CTS test generation for ingestion - camelCase params, …
MarioAlexandruDan 051d012
fix(dart): fix ingestion client default timeouts, AlgoliaApiException…
MarioAlexandruDan 72c3612
Merge branch 'main' into feat/dart-transformation-options
MarioAlexandruDan dfa8bc0
fix(dart): use flat serverTimeouts map for default ClientOptions to f…
MarioAlexandruDan 6756b21
fix(dart): add scalar server timeout values to bundle for reliable Mu…
MarioAlexandruDan daae490
fix(dart): simplify processOpenAPI - no try/catch needed since Config…
MarioAlexandruDan a05cf6f
fix(dart): apply spec server timeouts at request level for methods wi…
MarioAlexandruDan d2fa25b
fix(dart): add dart to assertValidReplaceAllObjectsWithTransformation…
MarioAlexandruDan 97ac919
fix(dart): add dart to assertPushMockValid count in runCts
MarioAlexandruDan f9b572b
Merge branch 'main' into feat/dart-transformation-options
MarioAlexandruDan d6e5ae0
Merge branch 'main' into feat/dart-transformation-options
MarioAlexandruDan 2eb6cd7
Merge branch 'main' into feat/dart-transformation-options
MarioAlexandruDan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
41 changes: 41 additions & 0 deletions
41
...algoliasearch-client-dart/packages/client_core/lib/src/config/transformation_options.dart
This file contains hidden or 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,41 @@ | ||
| import 'package:algolia_client_core/src/config/client_options.dart'; | ||
|
|
||
| /// Options for the ingestion transporter used by `*WithTransformation` helpers. | ||
| final class TransformationOptions { | ||
| /// The Algolia region for the Ingestion API (e.g. `'us'` or `'eu'`). Required. | ||
| final String region; | ||
|
|
||
| /// Optional overrides for the ingestion transporter's [ClientOptions]. | ||
| /// Only the fields you set here replace the Ingestion API defaults (25 s timeouts). | ||
| /// Do not set [ClientOptions.transformationOptions] here — it is ignored. | ||
| final ClientOptions? ingestionClientOptions; | ||
|
|
||
| /// Constructs a [TransformationOptions] instance. | ||
| TransformationOptions({ | ||
| required this.region, | ||
| this.ingestionClientOptions, | ||
| }) { | ||
| if (region.isEmpty) { | ||
| throw ArgumentError( | ||
| 'region is required in transformationOptions.' | ||
| ' See https://www.algolia.com/doc/libraries/sdk/methods/ingestion', | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| @override | ||
| bool operator ==(Object other) => | ||
| identical(this, other) || | ||
| other is TransformationOptions && | ||
| runtimeType == other.runtimeType && | ||
| region == other.region && | ||
| ingestionClientOptions == other.ingestionClientOptions; | ||
|
|
||
| @override | ||
| int get hashCode => region.hashCode ^ ingestionClientOptions.hashCode; | ||
|
|
||
| @override | ||
| String toString() { | ||
| return 'TransformationOptions{region: $region, ingestionClientOptions: $ingestionClientOptions}'; | ||
| } | ||
| } |
This file contains hidden or 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
Empty file.
1 change: 1 addition & 0 deletions
1
clients/algoliasearch-client-dart/packages/client_search/lib/src/extension.dart
This file contains hidden or 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 |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| export 'extension/search.dart'; | ||
| export 'extension/transformation.dart'; | ||
| export 'extension/wait_task.dart'; |
258 changes: 258 additions & 0 deletions
258
...ts/algoliasearch-client-dart/packages/client_search/lib/src/extension/transformation.dart
This file contains hidden or 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,258 @@ | ||
| import 'dart:math'; | ||
|
|
||
| import 'package:algolia_client_core/algolia_client_core.dart'; | ||
| import 'package:algolia_client_ingestion/algolia_client_ingestion.dart' as ingestion; | ||
| import 'package:algolia_client_search/src/api/search_client.dart'; | ||
| import 'package:algolia_client_search/src/extension/wait_task.dart'; | ||
| import 'package:algolia_client_search/src/model/event.dart'; | ||
| import 'package:algolia_client_search/src/model/event_status.dart'; | ||
| import 'package:algolia_client_search/src/model/event_type.dart'; | ||
| import 'package:algolia_client_search/src/model/operation_index_params.dart'; | ||
| import 'package:algolia_client_search/src/model/operation_type.dart'; | ||
| import 'package:algolia_client_search/src/model/replace_all_objects_with_transformation_response.dart'; | ||
| import 'package:algolia_client_search/src/model/scope_type.dart'; | ||
| import 'package:algolia_client_search/src/model/watch_response.dart'; | ||
|
|
||
| extension Transformation on SearchClient { | ||
| static const _notSetError = | ||
| 'transformationOptions must be set in the client config before calling this method.' | ||
| ' It defaults to the Ingestion API defaults.' | ||
| ' See https://www.algolia.com/doc/libraries/sdk/methods/ingestion'; | ||
|
|
||
| /// Chunks [objects] and pushes them through the Ingestion pipeline with [action]. | ||
| Future<List<WatchResponse>> chunkedPush({ | ||
| required String indexName, | ||
| required Iterable<Map<String, dynamic>> objects, | ||
| required ingestion.Action action, | ||
| bool waitForTasks = false, | ||
| int batchSize = 1000, | ||
| String? referenceIndexName, | ||
| RequestOptions? requestOptions, | ||
| }) async { | ||
| if (batchSize < 1) throw ArgumentError('`batchSize` must be greater than 0'); | ||
| final transporter = ingestionTransporter; | ||
| if (transporter == null) throw StateError(_notSetError); | ||
|
|
||
| final responses = <WatchResponse>[]; | ||
| final batch = <ingestion.PushTaskRecords>[]; | ||
| final pollInterval = (batchSize ~/ 10).clamp(1, batchSize); | ||
| int polledUpTo = 0; | ||
|
|
||
| final iter = objects.iterator; | ||
| if (!iter.moveNext()) return responses; | ||
|
|
||
| while (true) { | ||
| batch.add(_toRecord(iter.current)); | ||
| final isLast = !iter.moveNext(); | ||
|
|
||
| if (batch.length == batchSize || isLast) { | ||
| final raw = await transporter.push( | ||
| indexName: indexName, | ||
| pushTaskPayload: ingestion.PushTaskPayload(action: action, records: List.of(batch)), | ||
| referenceIndexName: referenceIndexName, | ||
| requestOptions: requestOptions, | ||
| ); | ||
| responses.add(_convertWatchResponse(raw)); | ||
| batch.clear(); | ||
|
|
||
| if (waitForTasks && | ||
| (responses.length % pollInterval == 0 || isLast)) { | ||
| await _pollBatch( | ||
| transporter: transporter, | ||
| responses: responses, | ||
| from: polledUpTo, | ||
| to: responses.length, | ||
| requestOptions: requestOptions, | ||
| ); | ||
| polledUpTo = responses.length; | ||
| } | ||
| } | ||
|
|
||
| if (isLast) break; | ||
| } | ||
|
|
||
| return responses; | ||
| } | ||
|
|
||
| /// Saves objects through the Ingestion pipeline. Requires [TransformationOptions] to be set. | ||
| Future<List<WatchResponse>> saveObjectsWithTransformation({ | ||
| required String indexName, | ||
| required Iterable<Map<String, dynamic>> objects, | ||
| bool waitForTasks = false, | ||
| int batchSize = 1000, | ||
| RequestOptions? requestOptions, | ||
| }) { | ||
| return chunkedPush( | ||
| indexName: indexName, | ||
| objects: objects, | ||
| action: ingestion.Action.addObject, | ||
| waitForTasks: waitForTasks, | ||
| batchSize: batchSize, | ||
| requestOptions: requestOptions, | ||
| ); | ||
| } | ||
|
|
||
| /// Partially updates objects through the Ingestion pipeline. Requires [TransformationOptions] to be set. | ||
| Future<List<WatchResponse>> partialUpdateObjectsWithTransformation({ | ||
| required String indexName, | ||
| required Iterable<Map<String, dynamic>> objects, | ||
| bool createIfNotExists = true, | ||
| bool waitForTasks = false, | ||
| int batchSize = 1000, | ||
| RequestOptions? requestOptions, | ||
| }) { | ||
| return chunkedPush( | ||
| indexName: indexName, | ||
| objects: objects, | ||
| action: createIfNotExists | ||
| ? ingestion.Action.partialUpdateObject | ||
| : ingestion.Action.partialUpdateObjectNoCreate, | ||
| waitForTasks: waitForTasks, | ||
| batchSize: batchSize, | ||
| requestOptions: requestOptions, | ||
| ); | ||
| } | ||
|
|
||
| /// Replaces all objects in [indexName] via the Ingestion pipeline without downtime. | ||
| /// Requires [TransformationOptions] to be set. | ||
| Future<ReplaceAllObjectsWithTransformationResponse> replaceAllObjectsWithTransformation({ | ||
| required String indexName, | ||
| required Iterable<Map<String, dynamic>> objects, | ||
| int batchSize = 1000, | ||
| List<ScopeType>? scopes, | ||
| RequestOptions? requestOptions, | ||
| }) async { | ||
| if (ingestionTransporter == null) throw StateError(_notSetError); | ||
|
|
||
| final effectiveScopes = scopes ?? [ScopeType.settings, ScopeType.rules, ScopeType.synonyms]; | ||
| final tmpIndex = '${indexName}_tmp_${Random().nextInt(900000) + 100000}'; | ||
|
|
||
| try { | ||
| var copyResponse = await operationIndex( | ||
| indexName: indexName, | ||
| operationIndexParams: OperationIndexParams( | ||
| operation: OperationType.copy, | ||
| destination: tmpIndex, | ||
| scope: effectiveScopes, | ||
| ), | ||
| requestOptions: requestOptions, | ||
| ); | ||
|
|
||
| final watchResponses = await chunkedPush( | ||
| indexName: tmpIndex, | ||
| objects: objects, | ||
| action: ingestion.Action.addObject, | ||
| waitForTasks: true, | ||
| batchSize: batchSize, | ||
| referenceIndexName: indexName, | ||
| requestOptions: requestOptions, | ||
| ); | ||
|
|
||
| await waitTask(indexName: tmpIndex, taskID: copyResponse.taskID, requestOptions: requestOptions); | ||
|
|
||
| copyResponse = await operationIndex( | ||
| indexName: indexName, | ||
| operationIndexParams: OperationIndexParams( | ||
| operation: OperationType.copy, | ||
| destination: tmpIndex, | ||
| scope: effectiveScopes, | ||
| ), | ||
| requestOptions: requestOptions, | ||
| ); | ||
| await waitTask(indexName: tmpIndex, taskID: copyResponse.taskID, requestOptions: requestOptions); | ||
|
|
||
| final moveResponse = await operationIndex( | ||
| indexName: tmpIndex, | ||
| operationIndexParams: OperationIndexParams( | ||
| operation: OperationType.move, | ||
| destination: indexName, | ||
| ), | ||
| requestOptions: requestOptions, | ||
| ); | ||
| await waitTask(indexName: tmpIndex, taskID: moveResponse.taskID, requestOptions: requestOptions); | ||
|
|
||
| return ReplaceAllObjectsWithTransformationResponse( | ||
| copyOperationResponse: copyResponse, | ||
| watchResponses: watchResponses, | ||
| moveOperationResponse: moveResponse, | ||
| ); | ||
| } catch (_) { | ||
| try { | ||
| await deleteIndex(indexName: tmpIndex); | ||
| } catch (_) {} | ||
| rethrow; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Future<void> _pollBatch({ | ||
| required ingestion.IngestionClient transporter, | ||
| required List<WatchResponse> responses, | ||
| required int from, | ||
| required int to, | ||
| RequestOptions? requestOptions, | ||
| }) async { | ||
| for (final resp in responses.sublist(from, to)) { | ||
| final eventID = resp.eventID; | ||
| if (eventID == null) continue; | ||
| await _waitForEvent( | ||
| transporter: transporter, | ||
| runID: resp.runID, | ||
| eventID: eventID, | ||
| requestOptions: requestOptions, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| Future<void> _waitForEvent({ | ||
| required ingestion.IngestionClient transporter, | ||
| required String runID, | ||
| required String eventID, | ||
| RequestOptions? requestOptions, | ||
| }) async { | ||
| for (var retries = 0; retries < 50; retries++) { | ||
| try { | ||
| await transporter.getEvent( | ||
| runID: runID, | ||
| eventID: eventID, | ||
| requestOptions: requestOptions, | ||
| ); | ||
| return; | ||
| } on AlgoliaApiException catch (e) { | ||
| if (e.statusCode != 404) rethrow; | ||
| } | ||
| await Future<void>.delayed( | ||
| Duration(milliseconds: (retries * 1500).clamp(0, 5000)), | ||
| ); | ||
| } | ||
|
Comment on lines
+213
to
+227
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Silently returns after 50 retries |
||
| } | ||
|
|
||
| ingestion.PushTaskRecords _toRecord(Map<String, dynamic> obj) { | ||
| final objectID = obj['objectID']; | ||
| if (objectID == null || objectID is! String) { | ||
| throw ArgumentError('each object must have an `objectID` key in order to be indexed'); | ||
| } | ||
| final rest = Map<String, dynamic>.from(obj)..remove('objectID'); | ||
| return ingestion.PushTaskRecords(objectID: objectID, additionalProperties: rest); | ||
| } | ||
|
|
||
| WatchResponse _convertWatchResponse(ingestion.WatchResponse r) { | ||
| return WatchResponse( | ||
| runID: r.runID, | ||
| eventID: r.eventID, | ||
| data: r.data, | ||
| events: r.events | ||
| ?.map((e) => Event( | ||
| eventID: e.eventID, | ||
| runID: e.runID, | ||
| status: e.status != null ? EventStatus.fromJson(e.status!.toJson()) : null, | ||
| type: EventType.fromJson(e.type.toJson()), | ||
| batchSize: e.batchSize, | ||
| data: e.data, | ||
| publishedAt: e.publishedAt, | ||
| )) | ||
| .toList(), | ||
| message: r.message, | ||
| createdAt: r.createdAt, | ||
| ); | ||
| } | ||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default is 100 now, and we want to pass maxRetries from the calling method I believe (check Dart jira ticket for maxRetries update)