Implementation:Apache Shardingsphere StatisticsPersistService Persist
| Knowledge Sources | |
|---|---|
| Domains | Persistence, Statistics |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
StatisticsPersistService manages the persistence lifecycle of ShardingSphere statistics data at database, schema, and table levels through a hierarchical repository.
Description
StatisticsPersistService is a final class in the mode/core module responsible for loading, persisting, updating, and deleting ShardingSphereStatistics. It delegates table-level row data operations to TableRowDataPersistService and uses NodePathGenerator to resolve hierarchical statistics paths in the underlying PersistRepository. The class traverses database/schema/table hierarchies when loading statistics, filtering against existing metadata to ensure consistency.
Usage
Use this service when you need to load statistics from the persist repository on startup, persist schema-level statistics after collection, apply incremental updates (adds, updates, deletes) via AlteredDatabaseStatistics, or remove all statistics for a dropped database.
Code Reference
Source Location
- Repository: Apache_Shardingsphere
- File: StatisticsPersistService.java
- Lines: 1-146
Signature
public final class StatisticsPersistService {
public StatisticsPersistService(final PersistRepository repository)
public ShardingSphereStatistics load(final ShardingSphereMetaData metaData)
public void persist(final ShardingSphereDatabase database, final String schemaName,
final SchemaStatistics schemaStatistics)
public void update(final AlteredDatabaseStatistics alteredDatabaseStatistics)
public void delete(final String databaseName)
}
Import
import org.apache.shardingsphere.mode.metadata.persist.statistics.StatisticsPersistService;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| repository | PersistRepository | Yes | The underlying persist repository for reading/writing data |
| metaData | ShardingSphereMetaData | Yes | Metadata used to filter and validate statistics during load |
| database | ShardingSphereDatabase | Yes | Database context for persist operations |
| schemaName | String | Yes | Schema name for persist operations |
| schemaStatistics | SchemaStatistics | Yes | Schema-level statistics to persist |
| alteredDatabaseStatistics | AlteredDatabaseStatistics | Yes | Diff-based statistics containing added, updated, and deleted rows |
| databaseName | String | Yes | Database name for delete operations |
Outputs
| Name | Type | Description |
|---|---|---|
| statistics | ShardingSphereStatistics | Loaded statistics hierarchy containing database/schema/table data |
Usage Examples
// Load statistics from repository
StatisticsPersistService service = new StatisticsPersistService(repository);
ShardingSphereStatistics statistics = service.load(metaData);
// Persist schema statistics
service.persist(database, "public", schemaStatistics);
// Apply incremental updates
service.update(alteredDatabaseStatistics);
// Delete all statistics for a database
service.delete("my_database");