mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(s3vectors): add S3 Vectors service support (#1435)
* feat(s3-vector-store): add S3Vectors service support with storage configuration and integration testing * feat(s3-vector-store): implement S3 Vectors service API with controller, service, and integration tests
This commit is contained in:
1 parent
b377940cb6
commit
93782c0142
13 files changed
+1370
-1
No files matched your search
@@ -175,6 +175,7 @@ public interface EmulatorConfig {
|
||||
CloudFrontStorageConfig cloudfront();
|
||||
AppSyncStorageConfig appsync();
|
||||
BatchStorageConfig batch();
|
||||
S3VectorsStorageConfig s3vectors();
|
||||
}
|
||||
|
||||
interface SsmStorageConfig {
|
||||
@@ -313,6 +314,13 @@ public interface EmulatorConfig {
|
||||
long flushIntervalMs();
|
||||
}
|
||||
|
||||
interface S3VectorsStorageConfig {
|
||||
Optional<String> mode();
|
||||
|
||||
@WithDefault("5000")
|
||||
long flushIntervalMs();
|
||||
}
|
||||
|
||||
interface WalConfig {
|
||||
@WithDefault("30000")
|
||||
long compactionIntervalMs();
|
||||
@@ -395,6 +403,7 @@ public interface EmulatorConfig {
|
||||
AppSyncServiceConfig appsync();
|
||||
BatchServiceConfig batch();
|
||||
UiServiceConfig ui();
|
||||
S3VectorsServiceConfig s3vectors();
|
||||
}
|
||||
|
||||
interface CloudTrailServiceConfig {
|
||||
@@ -402,6 +411,11 @@ public interface EmulatorConfig {
|
||||
boolean enabled();
|
||||
}
|
||||
|
||||
interface S3VectorsServiceConfig {
|
||||
@WithDefault("true")
|
||||
boolean enabled();
|
||||
}
|
||||
|
||||
interface TransferServiceConfig {
|
||||
@WithDefault("true")
|
||||
boolean enabled();
|
||||
|
||||
@@ -17,6 +17,7 @@ import io.github.hectorvent.floci.services.route53.Route53Controller;
|
||||
import io.github.hectorvent.floci.services.ses.SesController;
|
||||
import io.github.hectorvent.floci.services.appsync.AppSyncController;
|
||||
import io.github.hectorvent.floci.services.rdsdata.RdsDataController;
|
||||
import io.github.hectorvent.floci.services.s3vectors.S3VectorsController;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
@@ -325,7 +326,12 @@ public class ResolvedServiceCatalog {
|
||||
"appsync", storageMode(config.storage().services().appsync().mode(), config.storage().mode()),
|
||||
config.storage().services().appsync().flushIntervalMs(), null, ServiceProtocol.REST_JSON,
|
||||
protocols(ServiceProtocol.REST_JSON),
|
||||
Set.of(), Set.of("appsync"), Set.of(), Set.of(AppSyncController.class))
|
||||
Set.of(), Set.of("appsync"), Set.of(), Set.of(AppSyncController.class)),
|
||||
descriptor("s3vectors", "s3vectors", config.services().s3vectors().enabled(), true,
|
||||
"s3vectors", storageMode(config.storage().services().s3vectors().mode(), config.storage().mode()),
|
||||
config.storage().services().s3vectors().flushIntervalMs(), null, ServiceProtocol.REST_JSON,
|
||||
protocols(ServiceProtocol.REST_JSON),
|
||||
Set.of(), Set.of("s3vectors"), Set.of(), Set.of(S3VectorsController.class))
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,408 @@
|
||||
package io.github.hectorvent.floci.services.s3vectors;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.services.s3vectors.model.VectorBucket;
|
||||
import io.github.hectorvent.floci.services.s3vectors.model.VectorData;
|
||||
import io.github.hectorvent.floci.services.s3vectors.model.VectorIndex;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.Consumes;
|
||||
import jakarta.ws.rs.POST;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.ws.rs.Produces;
|
||||
import jakarta.ws.rs.core.Context;
|
||||
import jakarta.ws.rs.core.HttpHeaders;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Path("/")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
public class S3VectorsController {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(S3VectorsController.class);
|
||||
|
||||
private final S3VectorsService service;
|
||||
private final RegionResolver regionResolver;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
@Inject
|
||||
public S3VectorsController(S3VectorsService service, RegionResolver regionResolver, ObjectMapper objectMapper) {
|
||||
this.service = service;
|
||||
this.regionResolver = regionResolver;
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
@RegisterForReflection
|
||||
public record CreateVectorBucketRequest(
|
||||
String vectorBucketName,
|
||||
Object encryptionConfiguration
|
||||
) {}
|
||||
|
||||
@RegisterForReflection
|
||||
public record CreateVectorBucketResponse(
|
||||
String vectorBucketArn
|
||||
) {}
|
||||
|
||||
@RegisterForReflection
|
||||
public record GetVectorBucketRequest(
|
||||
String vectorBucketName,
|
||||
String vectorBucketArn
|
||||
) {}
|
||||
|
||||
@RegisterForReflection
|
||||
public record GetVectorBucketResponse(
|
||||
VectorBucketRepresentation vectorBucket
|
||||
) {}
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
public record VectorBucketRepresentation(
|
||||
String vectorBucketName,
|
||||
String vectorBucketArn,
|
||||
Object encryptionConfiguration
|
||||
) {}
|
||||
|
||||
@RegisterForReflection
|
||||
public record ListVectorBucketsRequest(
|
||||
Integer maxResults,
|
||||
String nextToken,
|
||||
String prefix
|
||||
) {}
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
public record ListVectorBucketsResponse(
|
||||
List<VectorBucketRepresentation> vectorBuckets,
|
||||
String nextToken
|
||||
) {}
|
||||
|
||||
@RegisterForReflection
|
||||
public record DeleteVectorBucketRequest(
|
||||
String vectorBucketName,
|
||||
String vectorBucketArn
|
||||
) {}
|
||||
|
||||
@RegisterForReflection
|
||||
public record CreateIndexRequest(
|
||||
String vectorBucketName,
|
||||
String vectorBucketArn,
|
||||
String indexName,
|
||||
String dataType,
|
||||
int dimension,
|
||||
String distanceMetric,
|
||||
Object metadataConfiguration,
|
||||
Object encryptionConfiguration
|
||||
) {}
|
||||
|
||||
@RegisterForReflection
|
||||
public record CreateIndexResponse(
|
||||
String indexArn
|
||||
) {}
|
||||
|
||||
@RegisterForReflection
|
||||
public record GetIndexRequest(
|
||||
String vectorBucketName,
|
||||
String indexName,
|
||||
String indexArn
|
||||
) {}
|
||||
|
||||
@RegisterForReflection
|
||||
public record GetIndexResponse(
|
||||
IndexRepresentation index
|
||||
) {}
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
public record IndexRepresentation(
|
||||
String vectorBucketName,
|
||||
String indexName,
|
||||
String indexArn,
|
||||
String dataType,
|
||||
int dimension,
|
||||
String distanceMetric,
|
||||
Object metadataConfiguration,
|
||||
Object encryptionConfiguration
|
||||
) {}
|
||||
|
||||
@RegisterForReflection
|
||||
public record ListIndexesRequest(
|
||||
String vectorBucketName,
|
||||
String vectorBucketArn,
|
||||
Integer maxResults,
|
||||
String nextToken,
|
||||
String prefix
|
||||
) {}
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
public record ListIndexesResponse(
|
||||
List<IndexRepresentation> indexes,
|
||||
String nextToken
|
||||
) {}
|
||||
|
||||
@RegisterForReflection
|
||||
public record DeleteIndexRequest(
|
||||
String vectorBucketName,
|
||||
String indexName,
|
||||
String indexArn
|
||||
) {}
|
||||
|
||||
@RegisterForReflection
|
||||
public record PutVectorsRequest(
|
||||
String vectorBucketName,
|
||||
String indexName,
|
||||
String indexArn,
|
||||
List<VectorRepresentation> vectors
|
||||
) {}
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
public record VectorRepresentation(
|
||||
String key,
|
||||
VectorDataRepresentation data,
|
||||
Map<String, Object> metadata
|
||||
) {}
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
public record VectorDataRepresentation(
|
||||
List<Float> float32
|
||||
) {}
|
||||
|
||||
@RegisterForReflection
|
||||
public record GetVectorsRequest(
|
||||
String vectorBucketName,
|
||||
String indexName,
|
||||
String indexArn,
|
||||
List<String> keys,
|
||||
Boolean returnData,
|
||||
Boolean returnMetadata
|
||||
) {}
|
||||
|
||||
@RegisterForReflection
|
||||
public record GetVectorsResponse(
|
||||
List<VectorGetResponseRepresentation> vectors
|
||||
) {}
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
public record VectorGetResponseRepresentation(
|
||||
String key,
|
||||
VectorDataRepresentation data,
|
||||
Map<String, Object> metadata
|
||||
) {}
|
||||
|
||||
@RegisterForReflection
|
||||
public record DeleteVectorsRequest(
|
||||
String vectorBucketName,
|
||||
String indexName,
|
||||
String indexArn,
|
||||
List<String> keys
|
||||
) {}
|
||||
|
||||
@RegisterForReflection
|
||||
public record QueryVectorsRequest(
|
||||
String vectorBucketName,
|
||||
String indexName,
|
||||
String indexArn,
|
||||
int topK,
|
||||
VectorDataRepresentation queryVector,
|
||||
Object filter,
|
||||
Boolean returnMetadata,
|
||||
Boolean returnDistance
|
||||
) {}
|
||||
|
||||
@RegisterForReflection
|
||||
public record QueryVectorsResponse(
|
||||
List<QueryResultRepresentation> vectors,
|
||||
String distanceMetric
|
||||
) {}
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
public record QueryResultRepresentation(
|
||||
String key,
|
||||
Double distance,
|
||||
Map<String, Object> metadata
|
||||
) {}
|
||||
|
||||
@POST
|
||||
@Path("/CreateVectorBucket")
|
||||
public Response createVectorBucket(@Context HttpHeaders headers, CreateVectorBucketRequest request) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
VectorBucket bucket = service.createVectorBucket(request.vectorBucketName(), request.encryptionConfiguration(), region);
|
||||
return Response.ok(new CreateVectorBucketResponse(bucket.getVectorBucketArn())).build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/GetVectorBucket")
|
||||
public Response getVectorBucket(@Context HttpHeaders headers, GetVectorBucketRequest request) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
VectorBucket bucket = service.getVectorBucket(request.vectorBucketName(), region);
|
||||
VectorBucketRepresentation rep = new VectorBucketRepresentation(bucket.getVectorBucketName(), bucket.getVectorBucketArn(), bucket.getEncryptionConfiguration());
|
||||
return Response.ok(new GetVectorBucketResponse(rep)).build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/ListVectorBuckets")
|
||||
public Response listVectorBuckets(@Context HttpHeaders headers, ListVectorBucketsRequest request) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
List<VectorBucket> buckets = service.listVectorBuckets(region);
|
||||
List<VectorBucketRepresentation> reps = buckets.stream()
|
||||
.map(b -> new VectorBucketRepresentation(b.getVectorBucketName(), b.getVectorBucketArn(), b.getEncryptionConfiguration()))
|
||||
.toList();
|
||||
return Response.ok(new ListVectorBucketsResponse(reps, null)).build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/DeleteVectorBucket")
|
||||
public Response deleteVectorBucket(@Context HttpHeaders headers, DeleteVectorBucketRequest request) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
service.deleteVectorBucket(request.vectorBucketName(), region);
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/CreateIndex")
|
||||
public Response createIndex(@Context HttpHeaders headers, CreateIndexRequest request) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
VectorIndex index = service.createIndex(
|
||||
request.vectorBucketName(),
|
||||
request.indexName(),
|
||||
request.dimension(),
|
||||
request.dataType() != null ? request.dataType() : "float32",
|
||||
request.distanceMetric() != null ? request.distanceMetric() : "cosine",
|
||||
List.of(),
|
||||
region
|
||||
);
|
||||
return Response.ok(new CreateIndexResponse(index.getIndexArn())).build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/GetIndex")
|
||||
public Response getIndex(@Context HttpHeaders headers, GetIndexRequest request) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
VectorIndex index = service.getIndex(request.vectorBucketName(), request.indexName(), region);
|
||||
IndexRepresentation rep = new IndexRepresentation(
|
||||
index.getVectorBucketName(),
|
||||
index.getIndexName(),
|
||||
index.getIndexArn(),
|
||||
index.getDataType(),
|
||||
index.getDimension(),
|
||||
index.getDistanceMetric(),
|
||||
null,
|
||||
null
|
||||
);
|
||||
return Response.ok(new GetIndexResponse(rep)).build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/ListIndexes")
|
||||
public Response listIndexes(@Context HttpHeaders headers, ListIndexesRequest request) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
List<VectorIndex> indexes = service.listIndexes(request.vectorBucketName(), region);
|
||||
List<IndexRepresentation> reps = indexes.stream()
|
||||
.map(i -> new IndexRepresentation(
|
||||
i.getVectorBucketName(),
|
||||
i.getIndexName(),
|
||||
i.getIndexArn(),
|
||||
i.getDataType(),
|
||||
i.getDimension(),
|
||||
i.getDistanceMetric(),
|
||||
null,
|
||||
null
|
||||
))
|
||||
.toList();
|
||||
return Response.ok(new ListIndexesResponse(reps, null)).build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/DeleteIndex")
|
||||
public Response deleteIndex(@Context HttpHeaders headers, DeleteIndexRequest request) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
service.deleteIndex(request.vectorBucketName(), request.indexName(), region);
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/PutVectors")
|
||||
public Response putVectors(@Context HttpHeaders headers, PutVectorsRequest request) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
List<VectorData> vectors = new ArrayList<>();
|
||||
if (request.vectors() != null) {
|
||||
for (VectorRepresentation vRep : request.vectors()) {
|
||||
List<Float> floatList = (vRep.data() != null && vRep.data().float32() != null)
|
||||
? vRep.data().float32()
|
||||
: List.of();
|
||||
Map<String, Object> metadata = vRep.metadata() != null ? vRep.metadata() : Map.of();
|
||||
vectors.add(new VectorData(vRep.key(), floatList, metadata));
|
||||
}
|
||||
}
|
||||
service.putVectors(request.vectorBucketName(), request.indexName(), vectors, region);
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/GetVectors")
|
||||
public Response getVectors(@Context HttpHeaders headers, GetVectorsRequest request) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
List<VectorData> vectors = service.getVectors(request.vectorBucketName(), request.indexName(), request.keys(), region);
|
||||
|
||||
boolean returnData = request.returnData() != null && request.returnData();
|
||||
boolean returnMetadata = request.returnMetadata() != null && request.returnMetadata();
|
||||
|
||||
List<VectorGetResponseRepresentation> reps = vectors.stream()
|
||||
.map(v -> {
|
||||
VectorDataRepresentation data = returnData ? new VectorDataRepresentation(v.getData()) : null;
|
||||
Map<String, Object> metadata = returnMetadata ? v.getMetadata() : null;
|
||||
return new VectorGetResponseRepresentation(v.getKey(), data, metadata);
|
||||
})
|
||||
.toList();
|
||||
|
||||
return Response.ok(new GetVectorsResponse(reps)).build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/DeleteVectors")
|
||||
public Response deleteVectors(@Context HttpHeaders headers, DeleteVectorsRequest request) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
service.deleteVectors(request.vectorBucketName(), request.indexName(), request.keys(), region);
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/QueryVectors")
|
||||
public Response queryVectors(@Context HttpHeaders headers, QueryVectorsRequest request) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
List<Float> queryVector = (request.queryVector() != null && request.queryVector().float32() != null)
|
||||
? request.queryVector().float32()
|
||||
: List.of();
|
||||
List<S3VectorsService.QueryResult> results = service.queryVectors(
|
||||
request.vectorBucketName(),
|
||||
request.indexName(),
|
||||
queryVector,
|
||||
request.topK() > 0 ? request.topK() : 10,
|
||||
region
|
||||
);
|
||||
|
||||
boolean returnMetadata = request.returnMetadata() != null && request.returnMetadata();
|
||||
|
||||
List<QueryResultRepresentation> reps = results.stream()
|
||||
.map(res -> {
|
||||
Map<String, Object> metadata = returnMetadata ? res.getVector().getMetadata() : null;
|
||||
return new QueryResultRepresentation(res.getVector().getKey(), res.getDistance(), metadata);
|
||||
})
|
||||
.toList();
|
||||
|
||||
VectorIndex index = service.getIndex(request.vectorBucketName(), request.indexName(), region);
|
||||
return Response.ok(new QueryVectorsResponse(reps, index.getDistanceMetric())).build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,250 @@
|
||||
package io.github.hectorvent.floci.services.s3vectors;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.s3vectors.model.VectorBucket;
|
||||
import io.github.hectorvent.floci.services.s3vectors.model.VectorIndex;
|
||||
import io.github.hectorvent.floci.services.s3vectors.model.VectorData;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ApplicationScoped
|
||||
public class S3VectorsService {
|
||||
private static final Logger LOG = Logger.getLogger(S3VectorsService.class);
|
||||
|
||||
private final StorageBackend<String, VectorBucket> store;
|
||||
private final RegionResolver regionResolver;
|
||||
|
||||
@Inject
|
||||
public S3VectorsService(StorageFactory factory, RegionResolver regionResolver) {
|
||||
this.store = factory.create("s3vectors", "s3vectors.json",
|
||||
new TypeReference<Map<String, VectorBucket>>() {});
|
||||
this.regionResolver = regionResolver;
|
||||
}
|
||||
|
||||
private String buildStorageKey(String region, String bucketName) {
|
||||
return region + "::" + bucketName;
|
||||
}
|
||||
|
||||
private String buildBucketArn(String region, String bucketName) {
|
||||
return AwsArnUtils.Arn.of("s3vectors", region, regionResolver.getAccountId(), "bucket/" + bucketName).toString();
|
||||
}
|
||||
|
||||
private String buildIndexArn(String region, String bucketName, String indexName) {
|
||||
return buildBucketArn(region, bucketName) + "/index/" + indexName;
|
||||
}
|
||||
|
||||
public VectorBucket createVectorBucket(String bucketName, Object encryptionConfiguration, String region) {
|
||||
String key = buildStorageKey(region, bucketName);
|
||||
if (store.get(key).isPresent()) {
|
||||
throw new AwsException("ConflictException", "The vector bucket " + bucketName + " already exists.", 409);
|
||||
}
|
||||
String arn = buildBucketArn(region, bucketName);
|
||||
VectorBucket bucket = new VectorBucket(bucketName, arn, encryptionConfiguration);
|
||||
store.put(key, bucket);
|
||||
LOG.infov("Created Vector Bucket: {0} in region {1}", bucketName, region);
|
||||
return bucket;
|
||||
}
|
||||
|
||||
public VectorBucket getVectorBucket(String bucketName, String region) {
|
||||
String key = buildStorageKey(region, bucketName);
|
||||
return store.get(key).orElseThrow(() ->
|
||||
new AwsException("NotFoundException", "The vector bucket " + bucketName + " does not exist.", 404));
|
||||
}
|
||||
|
||||
public List<VectorBucket> listVectorBuckets(String region) {
|
||||
return store.scan(k -> k.startsWith(region + "::")).stream()
|
||||
.sorted(Comparator.comparing(VectorBucket::getVectorBucketName))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public void deleteVectorBucket(String bucketName, String region) {
|
||||
String key = buildStorageKey(region, bucketName);
|
||||
VectorBucket bucket = store.get(key).orElseThrow(() ->
|
||||
new AwsException("NotFoundException", "The vector bucket " + bucketName + " does not exist.", 404));
|
||||
|
||||
if (!bucket.getIndexes().isEmpty()) {
|
||||
throw new AwsException("ConflictException", "The vector bucket " + bucketName + " is not empty.", 409);
|
||||
}
|
||||
store.delete(key);
|
||||
LOG.infov("Deleted Vector Bucket: {0}", bucketName);
|
||||
}
|
||||
|
||||
public VectorIndex createIndex(String bucketName, String indexName, int dimension, String dataType,
|
||||
String distanceMetric, List<String> nonFilterableMetadataKeys, String region) {
|
||||
if (!"float32".equals(dataType)) {
|
||||
throw new AwsException("ValidationException", "Unsupported dataType: " + dataType + ". Only float32 is supported.", 400);
|
||||
}
|
||||
if (!"cosine".equals(distanceMetric) && !"euclidean".equals(distanceMetric)) {
|
||||
throw new AwsException("ValidationException", "Unsupported distanceMetric: " + distanceMetric + ". Supported metrics are cosine and euclidean.", 400);
|
||||
}
|
||||
|
||||
VectorBucket bucket = getVectorBucket(bucketName, region);
|
||||
if (bucket.getIndexes().containsKey(indexName)) {
|
||||
throw new AwsException("ConflictException", "The index " + indexName + " already exists.", 409);
|
||||
}
|
||||
|
||||
String arn = buildIndexArn(region, bucketName, indexName);
|
||||
VectorIndex index = new VectorIndex(indexName, arn, bucketName, dimension, dataType, distanceMetric);
|
||||
bucket.getIndexes().put(indexName, index);
|
||||
store.put(buildStorageKey(region, bucketName), bucket);
|
||||
LOG.infov("Created Index: {0} in bucket {1}", indexName, bucketName);
|
||||
return index;
|
||||
}
|
||||
|
||||
public VectorIndex getIndex(String bucketName, String indexName, String region) {
|
||||
VectorBucket bucket = getVectorBucket(bucketName, region);
|
||||
VectorIndex index = bucket.getIndexes().get(indexName);
|
||||
if (index == null) {
|
||||
throw new AwsException("NotFoundException", "The index " + indexName + " does not exist in bucket " + bucketName + ".", 404);
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
public List<VectorIndex> listIndexes(String bucketName, String region) {
|
||||
VectorBucket bucket = getVectorBucket(bucketName, region);
|
||||
return bucket.getIndexes().values().stream()
|
||||
.sorted(Comparator.comparing(VectorIndex::getIndexName))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public void deleteIndex(String bucketName, String indexName, String region) {
|
||||
VectorBucket bucket = getVectorBucket(bucketName, region);
|
||||
if (!bucket.getIndexes().containsKey(indexName)) {
|
||||
throw new AwsException("NotFoundException", "The index " + indexName + " does not exist.", 404);
|
||||
}
|
||||
bucket.getIndexes().remove(indexName);
|
||||
store.put(buildStorageKey(region, bucketName), bucket);
|
||||
LOG.infov("Deleted Index: {0}", indexName);
|
||||
}
|
||||
|
||||
public void putVectors(String bucketName, String indexName, List<VectorData> vectors, String region) {
|
||||
VectorBucket bucket = getVectorBucket(bucketName, region);
|
||||
VectorIndex index = getIndex(bucketName, indexName, region);
|
||||
|
||||
for (VectorData v : vectors) {
|
||||
if (v.getData() != null && v.getData().size() != index.getDimension()) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Vector dimension " + v.getData().size() + " does not match index dimension " + index.getDimension(), 400);
|
||||
}
|
||||
index.getVectors().put(v.getKey(), v);
|
||||
}
|
||||
store.put(buildStorageKey(region, bucketName), bucket);
|
||||
LOG.infov("Put {0} vectors into index {1}", vectors.size(), indexName);
|
||||
}
|
||||
|
||||
public List<VectorData> getVectors(String bucketName, String indexName, List<String> keys, String region) {
|
||||
VectorIndex index = getIndex(bucketName, indexName, region);
|
||||
List<VectorData> result = new ArrayList<>();
|
||||
for (String key : keys) {
|
||||
VectorData v = index.getVectors().get(key);
|
||||
if (v != null) {
|
||||
result.add(v);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public void deleteVectors(String bucketName, String indexName, List<String> keys, String region) {
|
||||
VectorBucket bucket = getVectorBucket(bucketName, region);
|
||||
VectorIndex index = getIndex(bucketName, indexName, region);
|
||||
for (String key : keys) {
|
||||
index.getVectors().remove(key);
|
||||
}
|
||||
store.put(buildStorageKey(region, bucketName), bucket);
|
||||
LOG.infov("Deleted {0} vectors from index {1}", keys.size(), indexName);
|
||||
}
|
||||
|
||||
public List<QueryResult> queryVectors(String bucketName, String indexName, List<Float> queryVector, int topK, String region) {
|
||||
VectorIndex index = getIndex(bucketName, indexName, region);
|
||||
if (queryVector.size() != index.getDimension()) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Query vector dimension " + queryVector.size() + " does not match index dimension " + index.getDimension(), 400);
|
||||
}
|
||||
|
||||
String metric = index.getDistanceMetric() != null ? index.getDistanceMetric().toLowerCase() : "cosine";
|
||||
List<QueryResult> results = new ArrayList<>();
|
||||
|
||||
for (VectorData v : index.getVectors().values()) {
|
||||
double distance = 0.0;
|
||||
switch (metric) {
|
||||
case "euclidean":
|
||||
distance = calculateEuclideanDistance(queryVector, v.getData());
|
||||
break;
|
||||
case "cosine":
|
||||
default:
|
||||
distance = calculateCosineSimilarity(queryVector, v.getData());
|
||||
break;
|
||||
}
|
||||
results.add(new QueryResult(v, distance));
|
||||
}
|
||||
|
||||
// Sorting:
|
||||
// For Euclidean distance, smaller distance is closer (ascending)
|
||||
// For Cosine similarity, larger score is closer (descending)
|
||||
if ("euclidean".equals(metric)) {
|
||||
results.sort(Comparator.comparingDouble(QueryResult::getDistance));
|
||||
} else {
|
||||
results.sort((r1, r2) -> Double.compare(r2.getDistance(), r1.getDistance()));
|
||||
}
|
||||
|
||||
return results.stream().limit(topK).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private double calculateCosineSimilarity(List<Float> v1, List<Float> v2) {
|
||||
if (v1 == null || v2 == null || v1.size() != v2.size()) return 0.0;
|
||||
double dotProduct = 0.0;
|
||||
double normA = 0.0;
|
||||
double normB = 0.0;
|
||||
for (int i = 0; i < v1.size(); i++) {
|
||||
float a = v1.get(i);
|
||||
float b = v2.get(i);
|
||||
dotProduct += a * b;
|
||||
normA += a * a;
|
||||
normB += b * b;
|
||||
}
|
||||
if (normA == 0.0 || normB == 0.0) return 0.0;
|
||||
return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB));
|
||||
}
|
||||
|
||||
private double calculateEuclideanDistance(List<Float> v1, List<Float> v2) {
|
||||
if (v1 == null || v2 == null || v1.size() != v2.size()) return Double.MAX_VALUE;
|
||||
double sum = 0.0;
|
||||
for (int i = 0; i < v1.size(); i++) {
|
||||
double diff = v1.get(i) - v2.get(i);
|
||||
sum += diff * diff;
|
||||
}
|
||||
return Math.sqrt(sum);
|
||||
}
|
||||
|
||||
private double calculateDotProduct(List<Float> v1, List<Float> v2) {
|
||||
if (v1 == null || v2 == null || v1.size() != v2.size()) return 0.0;
|
||||
double dotProduct = 0.0;
|
||||
for (int i = 0; i < v1.size(); i++) {
|
||||
dotProduct += v1.get(i) * v2.get(i);
|
||||
}
|
||||
return dotProduct;
|
||||
}
|
||||
|
||||
public static class QueryResult {
|
||||
private final VectorData vector;
|
||||
private final double distance;
|
||||
|
||||
public QueryResult(VectorData vector, double distance) {
|
||||
this.vector = vector;
|
||||
this.distance = distance;
|
||||
}
|
||||
|
||||
public VectorData getVector() { return vector; }
|
||||
public double getDistance() { return distance; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package io.github.hectorvent.floci.services.s3vectors.model;
|
||||
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@RegisterForReflection
|
||||
public class VectorBucket {
|
||||
private String vectorBucketName;
|
||||
private String vectorBucketArn;
|
||||
private Object encryptionConfiguration;
|
||||
private Map<String, VectorIndex> indexes = new ConcurrentHashMap<>();
|
||||
|
||||
public VectorBucket() {}
|
||||
|
||||
public VectorBucket(String vectorBucketName, String vectorBucketArn, Object encryptionConfiguration) {
|
||||
this.vectorBucketName = vectorBucketName;
|
||||
this.vectorBucketArn = vectorBucketArn;
|
||||
this.encryptionConfiguration = encryptionConfiguration;
|
||||
}
|
||||
|
||||
public String getVectorBucketName() { return vectorBucketName; }
|
||||
public void setVectorBucketName(String vectorBucketName) { this.vectorBucketName = vectorBucketName; }
|
||||
|
||||
public String getVectorBucketArn() { return vectorBucketArn; }
|
||||
public void setVectorBucketArn(String vectorBucketArn) { this.vectorBucketArn = vectorBucketArn; }
|
||||
|
||||
public Object getEncryptionConfiguration() { return encryptionConfiguration; }
|
||||
public void setEncryptionConfiguration(Object encryptionConfiguration) { this.encryptionConfiguration = encryptionConfiguration; }
|
||||
|
||||
public Map<String, VectorIndex> getIndexes() { return indexes; }
|
||||
public void setIndexes(Map<String, VectorIndex> indexes) { this.indexes = indexes; }
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package io.github.hectorvent.floci.services.s3vectors.model;
|
||||
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@RegisterForReflection
|
||||
public class VectorData {
|
||||
private String key;
|
||||
private List<Float> data;
|
||||
private Map<String, Object> metadata;
|
||||
|
||||
public VectorData() {}
|
||||
|
||||
public VectorData(String key, List<Float> data, Map<String, Object> metadata) {
|
||||
this.key = key;
|
||||
this.data = data;
|
||||
this.metadata = metadata;
|
||||
}
|
||||
|
||||
public String getKey() { return key; }
|
||||
public void setKey(String key) { this.key = key; }
|
||||
|
||||
public List<Float> getData() { return data; }
|
||||
public void setData(List<Float> data) { this.data = data; }
|
||||
|
||||
public Map<String, Object> getMetadata() { return metadata; }
|
||||
public void setMetadata(Map<String, Object> metadata) { this.metadata = metadata; }
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package io.github.hectorvent.floci.services.s3vectors.model;
|
||||
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@RegisterForReflection
|
||||
public class VectorIndex {
|
||||
private String indexName;
|
||||
private String indexArn;
|
||||
private String vectorBucketName;
|
||||
private int dimension;
|
||||
private String dataType;
|
||||
private String distanceMetric;
|
||||
private Map<String, VectorData> vectors = new ConcurrentHashMap<>();
|
||||
|
||||
public VectorIndex() {}
|
||||
|
||||
public VectorIndex(String indexName, String indexArn, String vectorBucketName, int dimension, String dataType, String distanceMetric) {
|
||||
this.indexName = indexName;
|
||||
this.indexArn = indexArn;
|
||||
this.vectorBucketName = vectorBucketName;
|
||||
this.dimension = dimension;
|
||||
this.dataType = dataType;
|
||||
this.distanceMetric = distanceMetric;
|
||||
}
|
||||
|
||||
public String getIndexName() { return indexName; }
|
||||
public void setIndexName(String indexName) { this.indexName = indexName; }
|
||||
|
||||
public String getIndexArn() { return indexArn; }
|
||||
public void setIndexArn(String indexArn) { this.indexArn = indexArn; }
|
||||
|
||||
public String getVectorBucketName() { return vectorBucketName; }
|
||||
public void setVectorBucketName(String vectorBucketName) { this.vectorBucketName = vectorBucketName; }
|
||||
|
||||
public int getDimension() { return dimension; }
|
||||
public void setDimension(int dimension) { this.dimension = dimension; }
|
||||
|
||||
public String getDataType() { return dataType; }
|
||||
public void setDataType(String dataType) { this.dataType = dataType; }
|
||||
|
||||
public String getDistanceMetric() { return distanceMetric; }
|
||||
public void setDistanceMetric(String distanceMetric) { this.distanceMetric = distanceMetric; }
|
||||
|
||||
public Map<String, VectorData> getVectors() { return vectors; }
|
||||
public void setVectors(Map<String, VectorData> vectors) { this.vectors = vectors; }
|
||||
}
|
||||
Reference in new issue
Block a user