|
| 1 | +/* |
| 2 | + * Copyright 2025, Seqera Labs |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +package io.seqera.data.range.impl; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | + |
| 25 | +import io.micronaut.context.annotation.Requires; |
| 26 | +import jakarta.inject.Singleton; |
| 27 | +import org.slf4j.Logger; |
| 28 | +import org.slf4j.LoggerFactory; |
| 29 | + |
| 30 | +/** |
| 31 | + * Local based implementation for a range set |
| 32 | + * |
| 33 | + * @author Paolo Di Tommaso <paolo.ditommaso@gmail.com> |
| 34 | + */ |
| 35 | +@Requires(missingProperty = "redis.uri") |
| 36 | +@Singleton |
| 37 | +public class LocalRangeProvider implements RangeProvider { |
| 38 | + |
| 39 | + private static final Logger log = LoggerFactory.getLogger(LocalRangeProvider.class); |
| 40 | + |
| 41 | + private final Map<String, Map<String, Double>> store = new HashMap<>(); |
| 42 | + |
| 43 | + @Override |
| 44 | + public void add(String key, String element, double score) { |
| 45 | + Map<String, Double> map = store.computeIfAbsent(key, k -> new HashMap<>()); |
| 46 | + map.put(element, score); |
| 47 | + log.trace("* add range - store: {}", store); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public List<String> getRange(String key, double min, double max, int count, boolean remove) { |
| 52 | + Map<String, Double> map = store.getOrDefault(key, new HashMap<>()); |
| 53 | + List<String> result = new ArrayList<>(); |
| 54 | + |
| 55 | + // Sort entries by value and iterate |
| 56 | + List<Map.Entry<String, Double>> sortedEntries = new ArrayList<>(map.entrySet()); |
| 57 | + sortedEntries.sort(Map.Entry.comparingByValue()); |
| 58 | + |
| 59 | + List<String> toRemove = new ArrayList<>(); |
| 60 | + for (Map.Entry<String, Double> entry : sortedEntries) { |
| 61 | + if (result.size() >= count) { |
| 62 | + break; |
| 63 | + } |
| 64 | + if (entry.getValue() >= min && entry.getValue() <= max) { |
| 65 | + result.add(entry.getKey()); |
| 66 | + if (remove) { |
| 67 | + toRemove.add(entry.getKey()); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // Remove after iteration to avoid ConcurrentModificationException |
| 73 | + for (String k : toRemove) { |
| 74 | + map.remove(k); |
| 75 | + } |
| 76 | + |
| 77 | + log.trace("* get range result={} - store: {}", result, store); |
| 78 | + return result; |
| 79 | + } |
| 80 | +} |
0 commit comments