Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Apache Shardingsphere ShadowDataSourceMappingsRetrieverFactory NewInstance

From Leeroopedia


Knowledge Sources
Domains Database_Routing, Shadow_Testing
Last Updated 2026-02-10 00:00 GMT

Overview

Concrete tool for classifying SQL statements by type and instantiating the appropriate shadow data source mappings retriever, provided by the ShardingSphere shadow module.

Description

ShadowDataSourceMappingsRetrieverFactory is a final utility class with a private constructor (enforced by @NoArgsConstructor(access = AccessLevel.PRIVATE)). It exposes a single static factory method, newInstance, which accepts a QueryContext and returns the correct ShadowDataSourceMappingsRetriever implementation.

The method delegates to an internal getShadowOperationType method that performs instanceof checks against the SQLStatementContext to identify whether it is an InsertStatementContext, DeleteStatementContext, UpdateStatementContext, or SelectStatementContext. Each of these maps to a corresponding ShadowOperationType enum value (INSERT, DELETE, UPDATE, SELECT).

If a DML operation type is identified, the factory returns a ShadowDMLStatementDataSourceMappingsRetriever initialized with the full query context and the identified operation type. This retriever supports both hint-based and column-based shadow routing.

If the statement does not match any DML type (e.g., DDL, DCL, or utility statements), getShadowOperationType returns Optional.empty(), and the factory returns a ShadowHintDataSourceMappingsRetriever initialized with only the HintValueContext. This retriever supports only global hint-based routing.

Usage

This factory is called by ShadowSQLRouter.decorateRouteContext on every SQL statement that passes through the shadow routing pipeline. It is not intended for direct invocation by application code.

Code Reference

Source Location

  • Repository: Apache ShardingSphere
  • File: features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/ShadowDataSourceMappingsRetrieverFactory.java
  • Lines: 46-51

Signature

public static ShadowDataSourceMappingsRetriever newInstance(final QueryContext queryContext)

Import

import org.apache.shardingsphere.shadow.route.retriever.ShadowDataSourceMappingsRetrieverFactory;

I/O Contract

Inputs

Name Type Required Description
queryContext QueryContext Yes Contains the SQL statement context (used to determine DML type via instanceof checks) and hint value context (used for hint-based routing fallback)

Outputs

Name Type Description
return ShadowDataSourceMappingsRetriever A ShadowDMLStatementDataSourceMappingsRetriever if the statement is a recognized DML type (INSERT, DELETE, UPDATE, SELECT), or a ShadowHintDataSourceMappingsRetriever if the statement is not a DML type

Usage Examples

Basic Usage

// Called internally by ShadowSQLRouter.decorateRouteContext:
Map<String, String> shadowDataSourceMappings =
    ShadowDataSourceMappingsRetrieverFactory.newInstance(queryContext).retrieve(rule);

// For an INSERT statement, this creates a ShadowDMLStatementDataSourceMappingsRetriever
// with operationType = ShadowOperationType.INSERT, which will check both hint-based
// and column-based shadow algorithms for each shadow table.

// For a CREATE TABLE statement, this creates a ShadowHintDataSourceMappingsRetriever
// which will only check global hint shadow algorithms.

Classification Flow

// The internal classification logic works as follows:
// InsertStatementContext  -> Optional.of(ShadowOperationType.INSERT)
// DeleteStatementContext  -> Optional.of(ShadowOperationType.DELETE)
// UpdateStatementContext  -> Optional.of(ShadowOperationType.UPDATE)
// SelectStatementContext  -> Optional.of(ShadowOperationType.SELECT)
// Any other context       -> Optional.empty() -> hint-only retriever

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment