|
| 1 | +package io.split.engine.cache; |
| 2 | + |
| 3 | +import com.google.common.collect.ConcurrentHashMultiset; |
| 4 | +import com.google.common.collect.Maps; |
| 5 | +import com.google.common.collect.Multiset; |
| 6 | +import com.google.common.collect.Sets; |
| 7 | +import io.split.engine.experiments.ParsedSplit; |
| 8 | +import org.slf4j.Logger; |
| 9 | +import org.slf4j.LoggerFactory; |
| 10 | + |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.Collection; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Set; |
| 15 | +import java.util.concurrent.ConcurrentMap; |
| 16 | +import java.util.concurrent.atomic.AtomicLong; |
| 17 | + |
| 18 | +public class InMemoryCacheImp implements SplitCache { |
| 19 | + |
| 20 | + private static final Logger _log = LoggerFactory.getLogger(InMemoryCacheImp.class); |
| 21 | + |
| 22 | + private final ConcurrentMap<String, ParsedSplit> _concurrentMap; |
| 23 | + private final Multiset<String> _concurrentTrafficTypeNameSet; |
| 24 | + |
| 25 | + private AtomicLong _changeNumber; |
| 26 | + |
| 27 | + public InMemoryCacheImp() { |
| 28 | + this(-1); |
| 29 | + } |
| 30 | + |
| 31 | + public InMemoryCacheImp(long startingChangeNumber) { |
| 32 | + _concurrentMap = Maps.newConcurrentMap(); |
| 33 | + _changeNumber = new AtomicLong(startingChangeNumber); |
| 34 | + _concurrentTrafficTypeNameSet = ConcurrentHashMultiset.create(); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public void put(ParsedSplit split) { |
| 39 | + _concurrentMap.put(split.feature(), split); |
| 40 | + |
| 41 | + if (split.trafficTypeName() != null) { |
| 42 | + _concurrentTrafficTypeNameSet.add(split.trafficTypeName()); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public boolean remove(String name) { |
| 48 | + ParsedSplit removed = _concurrentMap.remove(name); |
| 49 | + |
| 50 | + if (removed != null && removed.trafficTypeName() != null) { |
| 51 | + _concurrentTrafficTypeNameSet.remove(removed.trafficTypeName()); |
| 52 | + } |
| 53 | + |
| 54 | + return removed != null; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public ParsedSplit get(String name) { |
| 59 | + return _concurrentMap.get(name); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public Collection<ParsedSplit> getAll() { |
| 64 | + return _concurrentMap.values(); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public Collection<ParsedSplit> getMany(List<String> names) { |
| 69 | + List<ParsedSplit> splits = new ArrayList<>(); |
| 70 | + |
| 71 | + for (String name : names) { |
| 72 | + ParsedSplit split = _concurrentMap.get(name); |
| 73 | + |
| 74 | + if (split != null) { |
| 75 | + splits.add(split); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return splits; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public long getChangeNumber() { |
| 84 | + return _changeNumber.get(); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void setChangeNumber(long changeNumber) { |
| 89 | + if (changeNumber < _changeNumber.get()) { |
| 90 | + _log.error("ChangeNumber for splits cache is less than previous"); |
| 91 | + } |
| 92 | + |
| 93 | + _changeNumber.set(changeNumber); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public boolean trafficTypeExists(String trafficTypeName) { |
| 98 | + // If the multiset has [{"user",2}.{"account",0}], elementSet only returns |
| 99 | + // ["user"] (it ignores "account") |
| 100 | + return Sets.newHashSet(_concurrentTrafficTypeNameSet.elementSet()).contains(trafficTypeName); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void clear() { |
| 105 | + _concurrentMap.clear(); |
| 106 | + _concurrentTrafficTypeNameSet.clear(); |
| 107 | + } |
| 108 | +} |
0 commit comments