From 066e8cde8a28c6ae64d164165899b5969950ecaf Mon Sep 17 00:00:00 2001 From: Vamsi Manohar Date: Thu, 6 Apr 2023 10:29:39 -0700 Subject: [PATCH] Fix conflicts: REST API for GET,PUT and DELETE (#1482) * REST API for GET,PUT and DELETE Signed-off-by: vamsi-amazon * changed permission action names Signed-off-by: vamsi-amazon --------- Signed-off-by: vamsi-amazon --- .../sql/datasource/DataSourceLoaderCache.java | 20 ++++++++ .../datasource/DataSourceLoaderCacheImpl.java | 50 +++++++++++++++++++ .../DataSourceNotFoundException.java | 18 +++++++ .../sql/storage/DataSourceFactory.java | 1 + 4 files changed, 89 insertions(+) create mode 100644 core/src/main/java/org/opensearch/sql/datasource/DataSourceLoaderCache.java create mode 100644 core/src/main/java/org/opensearch/sql/datasource/DataSourceLoaderCacheImpl.java create mode 100644 core/src/main/java/org/opensearch/sql/datasource/exceptions/DataSourceNotFoundException.java diff --git a/core/src/main/java/org/opensearch/sql/datasource/DataSourceLoaderCache.java b/core/src/main/java/org/opensearch/sql/datasource/DataSourceLoaderCache.java new file mode 100644 index 0000000000..cce70fe584 --- /dev/null +++ b/core/src/main/java/org/opensearch/sql/datasource/DataSourceLoaderCache.java @@ -0,0 +1,20 @@ +package org.opensearch.sql.datasource; + +import org.opensearch.sql.datasource.model.DataSource; +import org.opensearch.sql.datasource.model.DataSourceMetadata; + +/** + * Interface for DataSourceLoaderCache which provides methods for + * fetch, loading and invalidating DataSource cache. + */ +public interface DataSourceLoaderCache { + + /** + * Returns cached datasource object or loads a new one if not present. + * + * @param dataSourceMetadata {@link DataSourceMetadata}. + * @return {@link DataSource} + */ + DataSource getOrLoadDataSource(DataSourceMetadata dataSourceMetadata); + +} diff --git a/core/src/main/java/org/opensearch/sql/datasource/DataSourceLoaderCacheImpl.java b/core/src/main/java/org/opensearch/sql/datasource/DataSourceLoaderCacheImpl.java new file mode 100644 index 0000000000..56b7bec08b --- /dev/null +++ b/core/src/main/java/org/opensearch/sql/datasource/DataSourceLoaderCacheImpl.java @@ -0,0 +1,50 @@ +package org.opensearch.sql.datasource; + +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; +import org.opensearch.sql.datasource.model.DataSource; +import org.opensearch.sql.datasource.model.DataSourceMetadata; +import org.opensearch.sql.datasource.model.DataSourceType; +import org.opensearch.sql.storage.DataSourceFactory; + +/** + * Default implementation of DataSourceLoaderCache. This implementation + * utilizes Google Guava Cache {@link Cache} for caching DataSource objects + * against {@link DataSourceMetadata}. Expires the cache objects every 24 hrs after + * the last access. + */ +public class DataSourceLoaderCacheImpl implements DataSourceLoaderCache { + private final Map dataSourceFactoryMap; + private final Cache dataSourceCache; + + /** + * DataSourceLoaderCacheImpl constructor. + * + * @param dataSourceFactorySet set of {@link DataSourceFactory}. + */ + public DataSourceLoaderCacheImpl(Set dataSourceFactorySet) { + this.dataSourceFactoryMap = dataSourceFactorySet.stream() + .collect(Collectors.toMap(DataSourceFactory::getDataSourceType, f -> f)); + this.dataSourceCache = CacheBuilder.newBuilder() + .maximumSize(1000) + .expireAfterAccess(24, TimeUnit.HOURS) + .build(); + } + + @Override + public DataSource getOrLoadDataSource(DataSourceMetadata dataSourceMetadata) { + DataSource dataSource = this.dataSourceCache.getIfPresent(dataSourceMetadata); + if (dataSource == null) { + dataSource = this.dataSourceFactoryMap.get(dataSourceMetadata.getConnector()) + .createDataSource(dataSourceMetadata); + this.dataSourceCache.put(dataSourceMetadata, dataSource); + return dataSource; + } + return dataSource; + } + +} diff --git a/core/src/main/java/org/opensearch/sql/datasource/exceptions/DataSourceNotFoundException.java b/core/src/main/java/org/opensearch/sql/datasource/exceptions/DataSourceNotFoundException.java new file mode 100644 index 0000000000..0a068ccdfc --- /dev/null +++ b/core/src/main/java/org/opensearch/sql/datasource/exceptions/DataSourceNotFoundException.java @@ -0,0 +1,18 @@ +/* + * + * * Copyright OpenSearch Contributors + * * SPDX-License-Identifier: Apache-2.0 + * + */ + +package org.opensearch.sql.datasource.exceptions; + +/** + * DataSourceNotFoundException. + */ +public class DataSourceNotFoundException extends RuntimeException { + public DataSourceNotFoundException(String message) { + super(message); + } + +} diff --git a/core/src/main/java/org/opensearch/sql/storage/DataSourceFactory.java b/core/src/main/java/org/opensearch/sql/storage/DataSourceFactory.java index 8512eddbe3..d0f24d0e5a 100644 --- a/core/src/main/java/org/opensearch/sql/storage/DataSourceFactory.java +++ b/core/src/main/java/org/opensearch/sql/storage/DataSourceFactory.java @@ -7,6 +7,7 @@ package org.opensearch.sql.storage; +import java.util.Map; import org.opensearch.sql.datasource.DataSourceService; import org.opensearch.sql.datasource.model.DataSource; import org.opensearch.sql.datasource.model.DataSourceMetadata;