Implementation:Apache Shardingsphere PersistRepository SPI
| Knowledge Sources | |
|---|---|
| Domains | SPI, Persistence |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
PersistRepository is the core SPI interface for persistent data storage, defining CRUD operations for hierarchical key-value data in ShardingSphere's mode subsystem.
Description
PersistRepository extends TypedSPI and AutoCloseable to define a standard contract for metadata persistence backends. It declares methods for querying data by key, listing children keys, checking key existence, persisting new data, updating existing data, deleting data, and closing the repository. A PATH_SEPARATOR constant (/) establishes the hierarchical path convention. Implementations include JDBCRepository, MemoryRepository for standalone mode, and cluster-backed repositories (ZooKeeper, etcd) for cluster mode.
Usage
Use this interface as the SPI extension point for all metadata persistence operations. It is loaded via TypedSPILoader based on the configured repository type. All persistence services in the mode subsystem depend on this interface for data access.
Code Reference
Source Location
- Repository: Apache_Shardingsphere
- File: PersistRepository.java
- Lines: 1-83
Signature
public interface PersistRepository extends TypedSPI, AutoCloseable {
String PATH_SEPARATOR = "/";
String query(String key);
List<String> getChildrenKeys(String key);
boolean isExisted(String key);
void persist(String key, String value);
void update(String key, String value);
void delete(String key);
void close();
}
Import
import org.apache.shardingsphere.mode.spi.repository.PersistRepository;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| key | String | Yes | Hierarchical path key for all operations |
| value | String | Yes | Data value for persist and update operations |
Outputs
| Name | Type | Description |
|---|---|---|
| queryResult | String | Data stored at the given key |
| childrenKeys | List<String> | Names of child nodes under the given key |
| isExisted | boolean | Whether the key exists in the repository |
Usage Examples
// Query data by key
String value = repository.query("/metadata/my_db/rules/sharding");
// List children
List<String> children = repository.getChildrenKeys("/metadata/my_db/rules");
// Persist data
repository.persist("/metadata/my_db/rules/sharding/tables/t_order", yamlContent);
// Check existence
boolean exists = repository.isExisted("/metadata/my_db");
// Delete
repository.delete("/metadata/my_db/rules/sharding/tables/t_order");