| 1 | package edu.ucsb.cs156.diningcachingproxy.services; | |
| 2 | ||
| 3 | import java.util.Comparator; | |
| 4 | import java.util.List; | |
| 5 | import java.util.Map; | |
| 6 | import java.util.concurrent.ConcurrentHashMap; | |
| 7 | import java.util.concurrent.atomic.AtomicLong; | |
| 8 | import org.springframework.stereotype.Service; | |
| 9 | ||
| 10 | /** | |
| 11 | * In-memory implementation of {@link HostTrackingService}. Everything is keyed by the raw remote | |
| 12 | * address internally, since a hostname (if any) is only known once {@link #resolveHostname(String, | |
| 13 | * String)} is called - which may happen well after the first request from that address was already | |
| 14 | * counted, since resolution runs as an asynchronous job. | |
| 15 | */ | |
| 16 | @Service | |
| 17 | public class HostTrackingServiceImpl extends HostTrackingService { | |
| 18 | ||
| 19 | private final Map<String, String> displayNames = new ConcurrentHashMap<>(); | |
| 20 | private final Map<String, AtomicLong> requestCounts = new ConcurrentHashMap<>(); | |
| 21 | private final Map<String, Map<String, AtomicLong>> pathCounts = new ConcurrentHashMap<>(); | |
| 22 | ||
| 23 | @Override | |
| 24 | public boolean recordRequest(String remoteAddress, String requestPath) { | |
| 25 | displayNames.putIfAbsent(remoteAddress, remoteAddress); | |
| 26 |
1
1. lambda$recordRequest$0 : replaced return value with null for edu/ucsb/cs156/diningcachingproxy/services/HostTrackingServiceImpl::lambda$recordRequest$0 → KILLED |
AtomicLong counter = requestCounts.computeIfAbsent(remoteAddress, key -> new AtomicLong(0)); |
| 27 |
1
1. recordRequest : negated conditional → KILLED |
boolean isFirstRequest = counter.getAndIncrement() == 0; |
| 28 | ||
| 29 | Map<String, AtomicLong> hostPathCounts = | |
| 30 |
1
1. lambda$recordRequest$1 : replaced return value with Collections.emptyMap for edu/ucsb/cs156/diningcachingproxy/services/HostTrackingServiceImpl::lambda$recordRequest$1 → KILLED |
pathCounts.computeIfAbsent(remoteAddress, key -> new ConcurrentHashMap<>()); |
| 31 |
1
1. lambda$recordRequest$2 : replaced return value with null for edu/ucsb/cs156/diningcachingproxy/services/HostTrackingServiceImpl::lambda$recordRequest$2 → KILLED |
hostPathCounts.computeIfAbsent(requestPath, key -> new AtomicLong(0)).incrementAndGet(); |
| 32 | ||
| 33 |
2
1. recordRequest : replaced boolean return with false for edu/ucsb/cs156/diningcachingproxy/services/HostTrackingServiceImpl::recordRequest → KILLED 2. recordRequest : replaced boolean return with true for edu/ucsb/cs156/diningcachingproxy/services/HostTrackingServiceImpl::recordRequest → KILLED |
return isFirstRequest; |
| 34 | } | |
| 35 | ||
| 36 | @Override | |
| 37 | public void resolveHostname(String remoteAddress, String hostname) { | |
| 38 | displayNames.put(remoteAddress, hostname); | |
| 39 | } | |
| 40 | ||
| 41 | @Override | |
| 42 | public List<HostCount> getHostCounts() { | |
| 43 |
1
1. getHostCounts : replaced return value with Collections.emptyList for edu/ucsb/cs156/diningcachingproxy/services/HostTrackingServiceImpl::getHostCounts → KILLED |
return requestCounts.entrySet().stream() |
| 44 | .map( | |
| 45 | entry -> | |
| 46 |
1
1. lambda$getHostCounts$0 : replaced return value with null for edu/ucsb/cs156/diningcachingproxy/services/HostTrackingServiceImpl::lambda$getHostCounts$0 → KILLED |
new HostCount( |
| 47 | displayNames.getOrDefault(entry.getKey(), entry.getKey()), | |
| 48 | entry.getValue().get(), | |
| 49 | pathCountsFor(entry.getKey()))) | |
| 50 | .sorted(Comparator.comparing(HostCount::host)) | |
| 51 | .toList(); | |
| 52 | } | |
| 53 | ||
| 54 | private List<PathCount> pathCountsFor(String remoteAddress) { | |
| 55 |
1
1. pathCountsFor : replaced return value with Collections.emptyList for edu/ucsb/cs156/diningcachingproxy/services/HostTrackingServiceImpl::pathCountsFor → KILLED |
return pathCounts.getOrDefault(remoteAddress, Map.of()).entrySet().stream() |
| 56 |
1
1. lambda$pathCountsFor$0 : replaced return value with null for edu/ucsb/cs156/diningcachingproxy/services/HostTrackingServiceImpl::lambda$pathCountsFor$0 → KILLED |
.map(entry -> new PathCount(entry.getKey(), entry.getValue().get())) |
| 57 | .sorted(Comparator.comparing(PathCount::path)) | |
| 58 | .toList(); | |
| 59 | } | |
| 60 | } | |
Mutations | ||
| 26 |
1.1 |
|
| 27 |
1.1 |
|
| 30 |
1.1 |
|
| 31 |
1.1 |
|
| 33 |
1.1 2.2 |
|
| 43 |
1.1 |
|
| 46 |
1.1 |
|
| 55 |
1.1 |
|
| 56 |
1.1 |