-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: vamsi-amazon <reddyvam@amazon.com>
- Loading branch information
1 parent
23cc0f6
commit ed3c0a9
Showing
30 changed files
with
1,370 additions
and
133 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
core/src/main/java/org/opensearch/sql/datasource/DataSourceLoaderCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
core/src/main/java/org/opensearch/sql/datasource/DataSourceLoaderCacheImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
core/src/main/java/org/opensearch/sql/datasource/exceptions/DataSourceNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.