Skip to content

Commit

Permalink
Fix conflicts: REST API for GET,PUT and DELETE (opensearch-project#1482)
Browse files Browse the repository at this point in the history
* REST API for GET,PUT and DELETE

Signed-off-by: vamsi-amazon <[email protected]>

* changed permission action names

Signed-off-by: vamsi-amazon <[email protected]>

---------

Signed-off-by: vamsi-amazon <[email protected]>
  • Loading branch information
vmmusings authored and acarbonetto committed Apr 18, 2023
1 parent 867e08c commit 066e8cd
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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);

}
Original file line number Diff line number Diff line change
@@ -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<DataSourceType, DataSourceFactory> dataSourceFactoryMap;
private final Cache<DataSourceMetadata, DataSource> dataSourceCache;

/**
* DataSourceLoaderCacheImpl constructor.
*
* @param dataSourceFactorySet set of {@link DataSourceFactory}.
*/
public DataSourceLoaderCacheImpl(Set<DataSourceFactory> 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;
}

}
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 066e8cd

Please sign in to comment.