| 37330 |
amit |
1 |
package com.smartdukaan.cron.offercircular;
|
|
|
2 |
|
|
|
3 |
import java.util.Collections;
|
|
|
4 |
import java.util.HashMap;
|
|
|
5 |
import java.util.List;
|
|
|
6 |
import java.util.Map;
|
|
|
7 |
|
|
|
8 |
import com.spice.profitmandi.dao.repository.offers.CircularIngestRepository;
|
|
|
9 |
|
|
|
10 |
/**
|
|
|
11 |
* Catalog rows bucketed by (brand, category), which is what makes the matcher's
|
|
|
12 |
* brand -> category -> model staging enforceable: a lookup can only ever see
|
|
|
13 |
* candidates from the right bucket, so there is no way to accidentally match a
|
|
|
14 |
* tablet offer against a phone.
|
|
|
15 |
*/
|
|
|
16 |
public final class CatalogIndex {
|
|
|
17 |
|
|
|
18 |
private final Map<String, List<ProductMatcher.Candidate>> buckets = new HashMap<>();
|
|
|
19 |
|
|
|
20 |
private CatalogIndex() { }
|
|
|
21 |
|
|
|
22 |
public static CatalogIndex load(CircularIngestRepository repository) {
|
|
|
23 |
CatalogIndex index = new CatalogIndex();
|
|
|
24 |
for (Object[] row : repository.selectCatalogCandidates()) {
|
|
|
25 |
String brand = (String) row[0];
|
|
|
26 |
int categoryId = ((Number) row[1]).intValue();
|
|
|
27 |
int catalogId = ((Number) row[2]).intValue();
|
|
|
28 |
String display = (String) row[3];
|
|
|
29 |
int superCatalogId = ((Number) row[4]).intValue();
|
|
|
30 |
index.buckets.computeIfAbsent(key(brand, categoryId), k -> new java.util.ArrayList<>())
|
|
|
31 |
.add(new ProductMatcher.Candidate(catalogId, display, superCatalogId));
|
|
|
32 |
}
|
|
|
33 |
return index;
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
private static String key(String brand, int categoryId) {
|
|
|
37 |
return (brand == null ? "" : brand.toLowerCase()) + "|" + categoryId;
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
/** Never null - an unknown bucket yields no candidates, which becomes CATALOG_GAP. */
|
|
|
41 |
public List<ProductMatcher.Candidate> candidates(String brand, int categoryId) {
|
|
|
42 |
List<ProductMatcher.Candidate> found = buckets.get(key(brand, categoryId));
|
|
|
43 |
return found == null ? Collections.emptyList() : found;
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
public int size() {
|
|
|
47 |
int total = 0;
|
|
|
48 |
for (List<ProductMatcher.Candidate> bucket : buckets.values()) {
|
|
|
49 |
total += bucket.size();
|
|
|
50 |
}
|
|
|
51 |
return total;
|
|
|
52 |
}
|
|
|
53 |
}
|