Skip to content

Commit

Permalink
TRY-198: review fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-kripakov-m10 committed Dec 26, 2023
1 parent 5fc0da8 commit b9f4567
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public static EmbeddingCollection from(final Stream<EnhancedEmbeddingProjection>
}

// TODO: provide immutable wrapper for EmbeddingCollection here
public <T> T doWithReadLock(Function<EmbeddingCollection, T> readAndDo) {
public <T> T doWithReadLock(Function<Map<EmbeddingProjection, RealVector>, T> readAndDo) {
readLock.lock();
try {
return readAndDo.apply(this);
return readAndDo.apply(exposeMap());
} finally {
readLock.unlock();
}
Expand All @@ -69,7 +69,8 @@ Collection<RealVector> getEmbeddings() {
return Collections.unmodifiableCollection(mapping.values());
}

public Map<EmbeddingProjection, RealVector> exposeMap() {
// package private for test purposes
Map<EmbeddingProjection, RealVector> exposeMap() {
return Collections.unmodifiableMap(mapping);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public List<Pair<Double, String>> predict(final double[] input, final String api
var coefficients = getSimilarityCoefficients();
return embeddingCacheProvider.getOrLoad(apiKey)
.doWithReadLock(
ec -> ec.exposeMap().entrySet()
ec -> ec.entrySet()
.stream()
.map(e -> Pair.of(e.getKey(), e.getValue().getDistance(inputFace)))
.map(p -> Pair.of(p.getKey(), calculateSimilarity(p.getValue(), coefficients)))
Expand Down

0 comments on commit b9f4567

Please sign in to comment.