A composite database index orders rows by several columns in a fixed sequence. For an index on (tenant_id, created_at, status), a query filtering by tenant_id can use the index, but one filtering only by created_at cannot skip the leading column.
Design a CompositeIndexAnalyzer class:
CompositeIndexAnalyzer() creates a stateless analyzer.int prefixLength(String[] indexColumns, String[] queryColumns) returns the number of consecutive leading index columns filtered by the query.
Column names in indexColumns are unique. Treat queryColumns as a set, so its order does not matter. Scan the index definition from left to right and stop at the first column absent from the query. Later matches do not extend the usable prefix after a gap.
Example 1:
Input:
Output:
Explanation: The query filters on a and b, the first two index columns. Missing c ends the prefix at length 2.
Example 2:
Input:
Output:
Explanation: The query can use the leading column a. Because it does not filter on b, the later filter on c cannot extend the prefix.
Constraints
1 <= indexColumns.length <= 10^50 <= queryColumns.length <= 10^5- Column names contain lowercase letters and underscores.
- Names in
indexColumns are unique; duplicate entries in queryColumns have no additional effect. - At most
100 calls are made to prefixLength.