Optimize()
This operation triggers optimize compaction for a collection and returns an asynchronous task handle that can be polled, cancelled, or awaited.
Status Optimize(const OptimizeRequest& request, OptimizeTaskPtr& task)
Request Syntax
auto request = OptimizeRequest()
.WithDatabaseName(db_name)
.WithCollectionName(collection_name)
.WithTargetSize("512MB")
.WithAsync(true)
.WithTimeoutMs(0);
OptimizeRequest
REQUEST METHODS:
WithDatabaseName(const std::string& db_name)Sets the target database.
WithCollectionName(const std::string& collection_name)Sets the collection to optimize.
WithTargetSize(const std::string& target_size)Sets desired compacted segment size such as
"512MB"or"1GB".WithAsync(bool async)When
true, optimization is scheduled asynchronously.WithTimeoutMs(int64_t timeout_ms)Sets the overall task timeout in milliseconds.
0means no overall timeout.
RETURNS:
Status with OptimizeTaskPtr
OptimizeResponse
This class represents optimize task output including normalized target size, compaction ID, and progress history.
const OptimizeResponse& response = resp;
METHODS:
const std::string& StatusText() constReturns the current status text reported by optimize execution.
const std::string& CollectionName() constReturns the collection being optimized.
int64_t CompactionID() constReturns the compaction task ID.
const std::string& TargetSize() constReturns the normalized target size used by the optimizer.
const std::vector<std::string>& ProgressHistory() constReturns progress messages collected during task execution.
OptimizeTask
This class represents an asynchronous optimize task that can be cancelled, awaited, and queried for progress.
const OptimizeTaskPtr& task = optimize_task;
METHODS:
Status GetResult(OptimizeResponse& response, int64_t timeout_ms = 0)Waits for completion and fills
response.timeout_ms = 0waits indefinitely.bool Cancel()Requests cooperative cancellation of the task.
bool IsDone() constReturns whether task execution has finished.
bool IsCancelled() constReturns whether cancellation was requested and accepted.
std::string CurrentProgress() constReturns the latest progress message.
std::vector<std::string> ProgressHistory() constReturns all recorded progress messages.
Status TaskStatus() constReturns the final task status when done, otherwise an OK status.
EXCEPTIONS:
StatusCode
Check
status.Code()andstatus.Message()for invalid request parameters, optimize scheduling failures, or timeout errors.
Example
#include <milvus/MilvusClientV2.h>
auto client = milvus::MilvusClientV2::Create();
milvus::ConnectParam connect_param{"http://localhost:19530", "root:Milvus"};
auto status = client->Connect(connect_param);
if (!status.IsOk()) {
std::cout << status.Message() << std::endl;
}
milvus::OptimizeTaskPtr task;
status = client->Optimize(
milvus::OptimizeRequest()
.WithCollectionName("my_collection")
.WithTargetSize("512MB")
.WithAsync(true),
task);
if (!status.IsOk()) {
std::cout << status.Message() << std::endl;
}
milvus::OptimizeResponse response;
status = task->GetResult(response, 60000);
if (!status.IsOk()) {
std::cout << status.Message() << std::endl;
}