All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.microsoft.semantickernel.data.VolatileVectorStore Maven / Gradle / Ivy

There is a newer version: 1.3.0
Show newest version
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.data;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.Nonnull;

import com.microsoft.semantickernel.data.vectorstorage.VectorStore;
import com.microsoft.semantickernel.data.vectorstorage.VectorStoreRecordCollection;
import com.microsoft.semantickernel.data.vectorstorage.VectorStoreRecordCollectionOptions;
import com.microsoft.semantickernel.exceptions.SKException;
import reactor.core.publisher.Mono;

/**
 * Represents a volatile vector store.
 * A volatile vector store is an in-memory vector store
 * that does not persist data.
 */
public class VolatileVectorStore implements VectorStore {

    private final Map> collections;

    /**
     * Creates a new instance of the volatile vector store.
     */
    public VolatileVectorStore() {
        this.collections = new ConcurrentHashMap<>();
    }

    /**
     * Gets a collection from the vector store.
     *
     * @param collectionName The name of the collection.
     * @param options        The options for the collection.
     * @return The collection.
     */
    @Override
    public  VectorStoreRecordCollection getCollection(
        @Nonnull String collectionName,
        @Nonnull VectorStoreRecordCollectionOptions options) {
        if (options.getKeyClass() != String.class) {
            throw new SKException("Volatile only supports string keys");
        }
        if (options.getRecordClass() == null) {
            throw new SKException("Record class is required");
        }

        return (VectorStoreRecordCollection) new VolatileVectorStoreRecordCollection<>(
            collectionName,
            collections,
            (VolatileVectorStoreRecordCollectionOptions) options);
    }

    /**
     * Gets the names of all collections in the vector store.
     *
     * @return A list of collection names.
     */
    @Override
    public Mono> getCollectionNamesAsync() {
        return Mono.just(new ArrayList<>(collections.keySet()));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy