-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change CatalogService to DataSourceService
Signed-off-by: Peng Huo <[email protected]>
- Loading branch information
Showing
33 changed files
with
953 additions
and
502 deletions.
There are no files selected for viewing
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
40 changes: 0 additions & 40 deletions
40
core/src/main/java/org/opensearch/sql/catalog/CatalogService.java
This file was deleted.
Oops, something went wrong.
43 changes: 43 additions & 0 deletions
43
core/src/main/java/org/opensearch/sql/catalog/DataSourceService.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,43 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.catalog; | ||
|
||
import java.util.Set; | ||
import org.opensearch.sql.catalog.model.DataSource; | ||
import org.opensearch.sql.catalog.model.DataSourceMetadata; | ||
|
||
/** | ||
* DataSource Service manage {@link DataSource}. | ||
*/ | ||
public interface DataSourceService { | ||
|
||
/** | ||
* Returns all DataSource objects. | ||
* | ||
* @return set of {@link DataSource}. | ||
*/ | ||
Set<DataSource> getDataSources(); | ||
|
||
/** | ||
* Returns {@link DataSource} with corresponding to the DataSource name. | ||
* | ||
* @param dataSourceName Name of the {@link DataSource}. | ||
* @return {@link DataSource}. | ||
*/ | ||
DataSource getDataSource(String dataSourceName); | ||
|
||
/** | ||
* Register {@link DataSource} defined by {@link DataSourceMetadata}. | ||
* | ||
* @param dataSourceMetadata {@link DataSourceMetadata}. | ||
*/ | ||
void addDataSource(DataSourceMetadata dataSourceMetadata); | ||
|
||
/** | ||
* remove all the registered {@link DataSource}. | ||
*/ | ||
void clear(); | ||
} |
93 changes: 93 additions & 0 deletions
93
core/src/main/java/org/opensearch/sql/catalog/DataSourceServiceImpl.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,93 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.catalog; | ||
|
||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.stream.Collectors; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.opensearch.sql.catalog.model.DataSource; | ||
import org.opensearch.sql.catalog.model.DataSourceMetadata; | ||
import org.opensearch.sql.catalog.model.DataSourceType; | ||
import org.opensearch.sql.storage.DataSourceFactory; | ||
|
||
/** | ||
* This class manages catalogs and responsible for creating connectors to these catalogs. | ||
*/ | ||
public class DataSourceServiceImpl implements DataSourceService { | ||
|
||
private static String DATASOURCE_NAME_REGEX = "[@*A-Za-z]+?[*a-zA-Z_\\-0-9]*"; | ||
|
||
private static final Logger LOG = LogManager.getLogger(); | ||
|
||
private final ConcurrentHashMap<String, DataSource> dataSourceMap; | ||
|
||
private final Map<DataSourceType, DataSourceFactory> dataSourceFactoryMap; | ||
|
||
/** | ||
* Constructor. | ||
*/ | ||
public DataSourceServiceImpl(Set<DataSourceFactory> dataSourceFactories) { | ||
dataSourceFactoryMap = | ||
dataSourceFactories.stream() | ||
.collect(Collectors.toMap(DataSourceFactory::getDataSourceType, f -> f)); | ||
dataSourceMap = new ConcurrentHashMap<>(); | ||
} | ||
|
||
@Override | ||
public Set<DataSource> getDataSources() { | ||
return new HashSet<>(dataSourceMap.values()); | ||
} | ||
|
||
@Override | ||
public DataSource getDataSource(String dataSourceName) { | ||
if (!dataSourceMap.containsKey(dataSourceName)) { | ||
throw new IllegalArgumentException( | ||
String.format("DataSource with name %s doesn't exist.", dataSourceName)); | ||
} | ||
return dataSourceMap.get(dataSourceName); | ||
} | ||
|
||
@Override | ||
public void addDataSource(DataSourceMetadata metadata) { | ||
validateDataSource(metadata); | ||
dataSourceMap.put( | ||
metadata.getName(), | ||
dataSourceFactoryMap.get(metadata.getConnector()).createDataSource(metadata)); | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
dataSourceMap.clear(); | ||
} | ||
|
||
/** | ||
* This can be moved to a different validator class when we introduce more connectors. | ||
* | ||
* @param metadata {@link DataSourceMetadata}. | ||
*/ | ||
private void validateDataSource(DataSourceMetadata metadata) { | ||
if (StringUtils.isEmpty(metadata.getName())) { | ||
throw new IllegalArgumentException( | ||
"Missing Name Field from a DataSource. Name is a required parameter."); | ||
} | ||
if (!metadata.getName().matches(DATASOURCE_NAME_REGEX)) { | ||
throw new IllegalArgumentException( | ||
String.format( | ||
"DataSource Name: %s contains illegal characters. Allowed characters: a-zA-Z0-9_-*@.", | ||
metadata.getName())); | ||
} | ||
if (Objects.isNull(metadata.getProperties())) { | ||
throw new IllegalArgumentException( | ||
"Missing properties field in catalog configuration. Properties are required parameters."); | ||
} | ||
} | ||
} |
30 changes: 0 additions & 30 deletions
30
core/src/main/java/org/opensearch/sql/catalog/model/CatalogMetadata.java
This file was deleted.
Oops, something went wrong.
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
47 changes: 47 additions & 0 deletions
47
core/src/main/java/org/opensearch/sql/catalog/model/DataSourceMetadata.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,47 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.catalog.model; | ||
|
||
import static org.opensearch.sql.analysis.CatalogSchemaIdentifierNameResolver.DEFAULT_CATALOG_NAME; | ||
|
||
import com.fasterxml.jackson.annotation.JsonFormat; | ||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.google.common.collect.ImmutableMap; | ||
import java.util.Map; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.opensearch.sql.catalog.DataSourceService; | ||
|
||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
@Getter | ||
@Setter | ||
@EqualsAndHashCode | ||
public class DataSourceMetadata { | ||
|
||
@JsonProperty(required = true) | ||
private String name; | ||
|
||
@JsonProperty(required = true) | ||
@JsonFormat(with = JsonFormat.Feature.ACCEPT_CASE_INSENSITIVE_PROPERTIES) | ||
private DataSourceType connector; | ||
|
||
@JsonProperty(required = true) | ||
private Map<String, String> properties; | ||
|
||
/** | ||
* Default OpenSearch {@link DataSourceMetadata}. Which is used to register default OpenSearch | ||
* {@link DataSource} to {@link DataSourceService}. | ||
*/ | ||
public static DataSourceMetadata defaultOpenSearchDataSourceMetadata() { | ||
DataSourceMetadata dataSourceMetadata = new DataSourceMetadata(); | ||
dataSourceMetadata.setName(DEFAULT_CATALOG_NAME); | ||
dataSourceMetadata.setConnector(DataSourceType.OPENSEARCH); | ||
dataSourceMetadata.setProperties(ImmutableMap.of()); | ||
return dataSourceMetadata; | ||
} | ||
} |
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.