Table of Contents

Method OptimizeDataSourceAsync

Namespace
LMKit.Data
Assembly
LM-Kit.NET.dll

OptimizeDataSourceAsync(string, CancellationToken)

Asynchronously optimizes a file-based DataSource by compacting its data and migrating its internal format to the latest supported version.

public static Task OptimizeDataSourceAsync(string path, CancellationToken cancellationToken = default)

Parameters

path string

The file path of the DataSource to be optimized. The path must point to an existing file.

cancellationToken CancellationToken

A CancellationToken to monitor for cancellation requests during asynchronous operations such as deserialization. If cancellation is requested, the operation may throw an OperationCanceledException.

Returns

Task

A task that represents the asynchronous optimization operation.

Remarks

The optimization process performs the following key steps:

  • Opens the specified file for reading and asynchronously deserializes the existing data source.
  • Associates the opened file stream with the deserialized DataSource.
  • Creates a temporary file and serializes the optimized data source to it.
  • Replaces the original file with the optimized file, either by deleting the original and moving the temporary file (for NETSTANDARD2_0) or by overwriting it.

This process minimizes storage fragmentation and ensures that the file conforms to the latest internal format, thus improving performance and future compatibility.

Exceptions

ArgumentNullException

Thrown if path is null, empty, or consists solely of whitespace.

FileNotFoundException

Thrown if the file at the specified path cannot be found.