How phonepe/mustang works
1. Repository shape
- This is a multi-module Maven repository targeting Java 17+.
- The runtime split is clean: a pure domain module, a core engine module, and an optional Dropwizard HTTP bundle.
- There are also benchmarking and docs modules, which support validation and documentation but are not part of the serving path.
2. mustang-models: shared domain layer
- This module defines the engine’s vocabulary and is intended to be reused by both core and API layers.
- It contains the object model for criteria, predicates, compositions (conjunction/disjunction), details (caveats such as equality/range/regex/versioning/set relations), pre-operations, search queries, similarity stats, ratification types, and shared exceptions/error codes.
- It also carries the core inverted-index data types (InvertedIndex, posting entries, keys, index group) that the engine manipulates.
3. Criteria as the central input
- The system indexes and evaluates Boolean expressions in two normalized forms: DNF and CNF.
- A criteria is built from compositions of predicates, where each predicate is either included or excluded, and each predicate references a Detail that defines its matching semantics.
- Pre-operations sit in front of detail evaluation and transform attribute values before matching; this is important because it changes the evaluation path without changing the criteria structure.
4. mustang-core: engine and orchestration
- MustangEngine is the main entry point for programmatic use.
- The core module separates responsibilities into facades for indexing, searching, scanning, and ratification, rather than putting everything directly into the engine class.
- This module also includes JSON utilities and criteria utilities, showing that it bridges domain objects with runtime evaluation against event payloads.
5. Indexing pipeline
- Indexing is form-aware: there are dedicated builders for CNF and DNF (CNFIndexer, DNFIndexer) behind a shared CriteriaIndexBuilder.
- Criteria are converted into inverted-index structures via posting-list extractors, with separate extractors for CNF and DNF.
- The resulting IndexGroup/InvertedIndex structures are the engine’s internal retrieval layer, optimized for finding candidate matches quickly rather than brute-force evaluation.
6. Search pipeline
- Search starts from an evaluation context built from incoming data, commonly JSON, and flows into SearchFacade and CriteriaSearchHandler.
- Matching is implemented by dedicated matchers for each normal form: CNFMatcher and DNFMatcher, with a CaveatEnforcer handling detail-specific checks.
- The engine supports both raw matching and ranked/top-N search, with scoring semantics defined in the README and implemented over predicate overlap.
7. Scan and direct evaluation
- Separate from indexed search, the core exposes scan and evaluate paths.
- Scan is the in-memory path for evaluating a list of criteria directly against a context, useful for smaller batches or validation.
- This gives the repo two important execution modes: indexed retrieval for scale, and direct evaluation for inspection or smaller workloads.
8. Background processing and ratification
- Ratification is an asynchronous background capability for anomaly detection or consistency checking over an index.
- The flow is split into a processor (AsyncProcessor), task handler interface, and a concrete ratification request handler.
- This indicates a non-blocking maintenance subsystem distinct from the synchronous search/index operations.
9. Similarity and export/import utilities
- The core also includes similarity detection for comparing candidate criteria against an existing index.
- Export/import support is present for moving index state in and out of the engine, which is useful for debugging, migration, or operational recovery.
- These are secondary engine capabilities, but they share the same model and index structures as the main search path.
10. mustang-dw-bundle: optional HTTP boundary
- This module wraps the core engine in a Dropwizard bundle and exposes REST resources for search, scan, debug, and ratification-related operations.
- The bundle layer uses request/response DTOs and service interfaces/implementations to keep HTTP concerns separate from engine logic.
- Architecturally, this is the network boundary: it translates incoming API requests into core-engine operations and returns serialized responses/exceptions.
11. Operational and ecosystem boundaries
- The repo is designed to be embedded as a library or mounted as a service.
- Maven Central publishing, CI, SonarCloud checks, and docs generation are part of the delivery pipeline, not runtime behavior.
- The docs and benchmarking modules are important for understanding intended usage and performance characteristics, but they do not alter the core execution architecture.
12. Key diagram takeaway
- The main flow is: request/context in → core model objects → index/search engine → matching/scoring results out.
- Around that, there are two major supporting boundaries: Dropwizard REST for external access and background maintenance for ratification/similarity/index management.
- The most important architectural split is between shared domain model and engine/runtime implementation, with DNF/CNF treated as first-class, parallel execution paths.
Main entry points: SearchFacade.java SearchFacade · MustangEngine.java MustangEngine · criteria Criteria · index Index Types
Components 25
mustang-models
Shared domain model and value objects
- Criteria · Normalized boolean criteria forms and visitors
mustang-models/src/main/java/com/phonepe/mustang/criteria - Composition · Conjunction/disjunction structure for criteria trees
mustang-models/src/main/java/com/phonepe/mustang/composition - Predicate · Included/excluded predicates used inside criteria
mustang-models/src/main/java/com/phonepe/mustang/predicate - Detail · Equality, range, regex, set, versioning, and caveat semantics
mustang-models/src/main/java/com/phonepe/mustang/detail - PreOperation · Attribute transforms applied before detail evaluation
mustang-models/src/main/java/com/phonepe/mustang/preoperation - Query · Search query and ranking inputs
mustang-models/src/main/java/com/phonepe/mustang/search - Index Types · InvertedIndex, posting entries, keys, and index groups
mustang-models/src/main/java/com/phonepe/mustang/index - Results · Similarity stats, ratification types, and exceptions
mustang-models/src/main/java/com/phonepe/mustang
mustang-core
Engine runtime and orchestration
- MustangEngine · Main programmatic entry point
mustang-core/src/main/java/com/phonepe/mustang/MustangEngine.java - IndexingFacade · Builds inverted indexes from criteria
mustang-core/src/main/java/com/phonepe/mustang/index/IndexingFacade.java - SearchFacade · Coordinates search and ranked retrieval
mustang-core/src/main/java/com/phonepe/mustang/search/SearchFacade.java - Scanner · In-memory scan path for evaluating criteria against context
mustang-core/src/main/java/com/phonepe/mustang/scan/Scanner.java - Ratifier · Ratification and anomaly checking over indexes
mustang-core/src/main/java/com/phonepe/mustang/ratify/Ratifier.java - AsyncProcessor · Runs ratification tasks asynchronously
mustang-core/src/main/java/com/phonepe/mustang/processor/AsyncProcessor.java - CNF path · CNF indexer and matcher pipeline
mustang-core/src/main/java/com/phonepe/mustang/index/builder/CNFIndexer.java - DNF path · DNF indexer and matcher pipeline
mustang-core/src/main/java/com/phonepe/mustang/index/builder/DNFIndexer.java - CaveatEnforcer · Applies detail-specific checks during matching
mustang-core/src/main/java/com/phonepe/mustang/search/matcher/CaveatEnforcer.java - SimilarityDetector · Compares candidate criteria against existing index
mustang-core/src/main/java/com/phonepe/mustang/similarity/SimilarityDetector.java - JsonUtils · JSON helpers for contexts and runtime bridging
mustang-core/src/main/java/com/phonepe/mustang/json/JsonUtils.java
mustang-dw-bundle
Optional Dropwizard HTTP boundary
- MustangBundle · Wires the HTTP module to the core engine
mustang-dw-bundle/src/main/java/com/phonepe/mustang/MustangBundle.java - REST resources · Search, scan, and debug endpoints
mustang-dw-bundle/src/main/java/com/phonepe/mustang/resources - Services · HTTP-facing service interfaces and implementations
mustang-dw-bundle/src/main/java/com/phonepe/mustang/service - Request/Response · HTTP request objects, responses, and exception mapping
mustang-dw-bundle/src/main/java/com/phonepe/mustang/request
Support
Docs and benchmarking modules
- Docs · Algorithm, concepts, and usage documentation
docs/docs - Benchmarks · JMH-style search benchmarks and utilities
benchmarking/src/main/java/com/phonepe/mustang/benchmark
Connections 33
- Composition → Predicate: contains · Criteria are composed of predicates
- Predicate → Detail: references · Each predicate delegates matching semantics to a detail
- Predicate → PreOperation: prepares · Pre-operations transform values before detail checks
- Criteria → Composition: built from · Criteria are normalized boolean expression trees
- Criteria → Query: mapped to · Search queries wrap criteria and ranking intent
- Index Types → Criteria: stores · Index structures encode criteria for retrieval
- Results → Detail: supports · Shared outputs include exceptions and stats around matching
- MustangEngine → IndexingFacade: uses · Engine delegates indexing to the indexing facade
- MustangEngine → SearchFacade: uses · Engine delegates search to the search facade
- MustangEngine → Scanner: uses · Engine exposes direct scan/evaluate paths
- MustangEngine → Ratifier: uses · Engine exposes ratification and maintenance operations
- IndexingFacade → CNF path: routes · Indexing splits by criteria normal form
- IndexingFacade → DNF path: routes · Indexing splits by criteria normal form
- CNF path → Index Types: builds · CNF indexing produces inverted-index structures
- DNF path → Index Types: builds · DNF indexing produces inverted-index structures
- SearchFacade → CNF path: matches · CNF search path uses CNF matchers
- SearchFacade → DNF path: matches · DNF search path uses DNF matchers
- SearchFacade → CaveatEnforcer: enforces · Search applies detail-specific caveats during evaluation
- SearchFacade → JsonUtils: parses · Search contexts often arrive as JSON payloads
- Scanner → Criteria: evaluates · Scan directly evaluates criteria against a context
- Ratifier → AsyncProcessor: scheduled by · Ratification can run asynchronously in the background
- Ratifier → Index Types: checks · Ratification operates over index state
- SimilarityDetector → Index Types: compares against · Similarity detection works on existing index state
- MustangBundle → MustangEngine: wraps · Dropwizard bundle wires HTTP access to the engine
- REST resources → Services: calls · Resources delegate to service layer
- Services → SearchFacade: invokes · Search service delegates to core search
- Services → Scanner: invokes · Scan service delegates to core scan
- Services → MustangEngine: invokes · Debug and management services reach into the engine
- Request/Response → Query: serializes · HTTP requests carry query and criteria data
- Request/Response → Results: maps · HTTP responses surface shared result and error types
- Docs → Criteria: documents · Docs explain criteria, predicates, and pre-operations
- Docs → SearchFacade: documents · Docs explain search and scoring behavior
- Benchmarks → SearchFacade: measures · Benchmarks compare search performance