Skip to content

Commit

Permalink
Make factory is only place to get transformer
Browse files Browse the repository at this point in the history
Signed-off-by: Balasubramanian <[email protected]>
  • Loading branch information
VijayanB committed Jan 16, 2025
1 parent 29be76c commit 6ed6fd0
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,4 @@ protected PerDimensionProcessor getPerDimensionProcessor() {
return PerDimensionProcessor.NOOP_PROCESSOR;
}

@Override
protected VectorTransformer getVectorTransformer() {
return VectorTransformer.NOOP_VECTOR_TRANSFORMER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -689,11 +689,21 @@ protected void validatePreparse() {
protected abstract PerDimensionProcessor getPerDimensionProcessor();

/**
* Getter for vector transformer after vector parsing and validation
* Retrieves the vector transformer for the KNN vector field.
* This method provides access to the vector transformer instance that will be used
* for processing vectors in the KNN field. The transformer is responsible for any
* necessary vector transformations before indexing or searching.
* This implementation delegates to the VectorTransformerFactory to obtain
* the appropriate transformer instance. The returned transformer is typically
* stateless and thread-safe.
*
* @return VectorTransformer An instance of VectorTransformer that will be used
* for vector transformations in this field
*
* @return VectorTransformer
*/
protected abstract VectorTransformer getVectorTransformer();
protected VectorTransformer getVectorTransformer() {
return VectorTransformerFactory.getVectorTransformer();
}

protected void parseCreateField(ParseContext context, int dimension, VectorDataType vectorDataType) throws IOException {
validatePreparse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,4 @@ default void transform(final byte[] vector) {
throw new IllegalArgumentException("Input vector cannot be null");
}
}

/**
* A no-operation transformer that returns vector values unchanged.
*/
VectorTransformer NOOP_VECTOR_TRANSFORMER = new VectorTransformer() {
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class VectorTransformerFactory {

/**
* A no-operation transformer that returns vector values unchanged.
*/
private final static VectorTransformer NOOP_VECTOR_TRANSFORMER = new VectorTransformer() {
};

/**
* Returns a vector transformer based on the provided KNN method context.
* For FAISS engine with cosine similarity space type, returns a NormalizeVectorTransformer
Expand All @@ -36,6 +42,20 @@ public static VectorTransformer getVectorTransformer(final KNNMethodContext cont
return getVectorTransformer(context.getKnnEngine(), context.getSpaceType());
}

/**
* Returns a vector transformer instance for vector transformations.
* This method provides access to the default no-operation vector transformer
* that performs identity transformation on vectors. The transformer does not
* modify the input vectors and returns them as-is.This implementation returns a stateless, thread-safe transformer
* instance that can be safely shared across multiple calls
*
* @return VectorTransformer A singleton instance of the no-operation vector
* transformer (NOOP_VECTOR_TRANSFORMER)
*/
public static VectorTransformer getVectorTransformer() {
return NOOP_VECTOR_TRANSFORMER;
}

/**
* Creates a VectorTransformer based on the provided model metadata.
*
Expand Down Expand Up @@ -69,7 +89,7 @@ public static VectorTransformer getVectorTransformer(final ModelMetadata metadat
* @return VectorTransformer An appropriate vector transformer instance
*/
private static VectorTransformer getVectorTransformer(final KNNEngine knnEngine, final SpaceType spaceType) {
return shouldNormalizeVector(knnEngine, spaceType) ? new NormalizeVectorTransformer() : VectorTransformer.NOOP_VECTOR_TRANSFORMER;
return shouldNormalizeVector(knnEngine, spaceType) ? new NormalizeVectorTransformer() : getVectorTransformer();
}

private static boolean shouldNormalizeVector(final KNNEngine knnEngine, final SpaceType spaceType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private static void validateTransformer(SpaceType spaceType, KNNEngine engine, V
} else {
assertSame(
"Should return NOOP transformer for " + engine + " with COSINESIMIL",
VectorTransformer.NOOP_VECTOR_TRANSFORMER,
VectorTransformerFactory.getVectorTransformer(),
transformer
);
}
Expand Down

0 comments on commit 6ed6fd0

Please sign in to comment.