Skip to content

Commit

Permalink
chore(swift): add cleanup method to replace all objects helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Fluf22 committed Oct 24, 2024
1 parent 0179bdb commit fe22a38
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -543,51 +543,57 @@ public extension SearchClient {
) async throws -> ReplaceAllObjectsResponse {
let tmpIndexName = "\(indexName)_tmp_\(Int.random(in: 1_000_000 ..< 10_000_000))"

var copyOperationResponse = try await operationIndex(
indexName: indexName,
operationIndexParams: OperationIndexParams(
operation: .copy,
destination: tmpIndexName,
scope: [.settings, .rules, .synonyms]
),
requestOptions: requestOptions
)
do {
var copyOperationResponse = try await operationIndex(
indexName: indexName,
operationIndexParams: OperationIndexParams(
operation: .copy,
destination: tmpIndexName,
scope: [.settings, .rules, .synonyms]
),
requestOptions: requestOptions
)

let batchResponses = try await self.chunkedBatch(
indexName: tmpIndexName,
objects: objects,
waitForTasks: true,
batchSize: batchSize,
requestOptions: requestOptions
)
try await self.waitForTask(indexName: tmpIndexName, taskID: copyOperationResponse.taskID)
let batchResponses = try await self.chunkedBatch(
indexName: tmpIndexName,
objects: objects,
waitForTasks: true,
batchSize: batchSize,
requestOptions: requestOptions
)
try await self.waitForTask(indexName: tmpIndexName, taskID: copyOperationResponse.taskID)

copyOperationResponse = try await operationIndex(
indexName: indexName,
operationIndexParams: OperationIndexParams(
operation: .copy,
destination: tmpIndexName,
scope: [.settings, .rules, .synonyms]
),
requestOptions: requestOptions
)
try await self.waitForTask(indexName: tmpIndexName, taskID: copyOperationResponse.taskID)

let moveOperationResponse = try await self.operationIndex(
indexName: tmpIndexName,
operationIndexParams: OperationIndexParams(
operation: .move,
destination: indexName
),
requestOptions: requestOptions
)
try await self.waitForTask(indexName: tmpIndexName, taskID: moveOperationResponse.taskID)
copyOperationResponse = try await operationIndex(
indexName: indexName,
operationIndexParams: OperationIndexParams(
operation: .copy,
destination: tmpIndexName,
scope: [.settings, .rules, .synonyms]
),
requestOptions: requestOptions
)
try await self.waitForTask(indexName: tmpIndexName, taskID: copyOperationResponse.taskID)

return ReplaceAllObjectsResponse(
copyOperationResponse: copyOperationResponse,
batchResponses: batchResponses,
moveOperationResponse: moveOperationResponse
)
let moveOperationResponse = try await self.operationIndex(
indexName: tmpIndexName,
operationIndexParams: OperationIndexParams(
operation: .move,
destination: indexName
),
requestOptions: requestOptions
)
try await self.waitForTask(indexName: tmpIndexName, taskID: moveOperationResponse.taskID)

return ReplaceAllObjectsResponse(
copyOperationResponse: copyOperationResponse,
batchResponses: batchResponses,
moveOperationResponse: moveOperationResponse
)
} catch {
_ = try? await deleteIndex(indexName: tmpIndexName)

throw error
}
}

/// Generate a secured API key
Expand Down
2 changes: 1 addition & 1 deletion scripts/cts/testServer/replaceAllObjectsFailed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { expect } from 'chai';
import type { Express } from 'express';
import express from 'express';

import { setupServer } from '.';
import { setupServer } from './index.js';

const raoState: Record<
string,
Expand Down

0 comments on commit fe22a38

Please sign in to comment.