ibessonov commented on code in PR #773:
URL: https://github.com/apache/ignite-3/pull/773#discussion_r850378528
##########
modules/storage-api/src/main/java/org/apache/ignite/internal/storage/DataStorageManager.java:
##########
@@ -118,6 +96,73 @@ public Consumer<DataStorageChange>
defaultTableDataStorageConsumer(String defaul
};
}
+ /**
+ * Returns the default data storage.
+ *
+ * @param defaultDataStorageView View of {@link
TablesConfigurationSchema#defaultDataStorage}. For the case {@link
+ * UnknownDataStorageConfigurationSchema#UNKNOWN_DATA_STORAGE} and there
is only one engine, then it will be the default.
+ */
+ public String defaultDataStorage(String defaultDataStorageView) {
+ return !defaultDataStorageView.equals(UNKNOWN_DATA_STORAGE) ||
engines.size() > 1
+ ? defaultDataStorageView : first(engines.keySet());
+ }
+
+ /**
+ * Creates a consumer that will change the {@link
DataStorageConfigurationSchema data storage} for the {@link
+ * TableConfigurationSchema#dataStorage}.
+ *
+ * @param dataStorage Data storage, {@link
UnknownDataStorageConfigurationSchema#UNKNOWN_DATA_STORAGE} is invalid.
+ * @param values {@link Value Values} for the data storage. Mapping: field
name -> field value.
+ */
+ public Consumer<DataStorageChange> tableDataStorageConsumer(String
dataStorage, Map<String, Object> values) {
+ assert !dataStorage.equals(UNKNOWN_DATA_STORAGE);
+
+ ConfigurationSource configurationSource = new ConfigurationSource() {
+ /** {@inheritDoc} */
+ @Override
+ public String polymorphicTypeId(String fieldName) {
+ throw new UnsupportedOperationException("polymorphicTypeId");
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void descend(ConstructableTreeNode node) {
+ for (Entry<String, Object> e : values.entrySet()) {
+ assert e.getKey() != null;
+ assert e.getValue() != null : e.getKey();
+
+ ConfigurationSource leafSource = new ConfigurationSource()
{
+ /** {@inheritDoc} */
+ @Override
+ public <T> T unwrap(Class<T> clazz) {
+ return clazz.cast(e.getValue());
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void descend(ConstructableTreeNode node) {
+ throw new UnsupportedOperationException("descend");
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public String polymorphicTypeId(String fieldName) {
+ throw new
UnsupportedOperationException("polymorphicTypeId");
+ }
+ };
+
+ node.construct(e.getKey(), leafSource, true);
+ }
+ }
+ };
+
+ return tableDataStorageChange -> {
+ tableDataStorageChange.convert(dataStorage);
+
+ configurationSource.descend(((ConstructableTreeNode)
tableDataStorageChange));
Review Comment:
Please remove extra parentheses
##########
modules/storage-api/src/main/java/org/apache/ignite/internal/storage/DataStorageModule.java:
##########
@@ -15,24 +15,29 @@
* limitations under the License.
*/
-package org.apache.ignite.internal.storage.engine;
+package org.apache.ignite.internal.storage;
import java.nio.file.Path;
import
org.apache.ignite.configuration.schemas.store.DataStorageConfigurationSchema;
import org.apache.ignite.internal.configuration.ConfigurationRegistry;
-import org.apache.ignite.internal.storage.StorageException;
+import org.apache.ignite.internal.storage.engine.StorageEngine;
/**
- * Factory for creating storage engines.
+ * Data storage module.
*/
-public interface StorageEngineFactory {
+public interface DataStorageModule {
/**
- * Returns the unique name of the storage engine.
+ * Returns the unique name of the data storage.
*
* <p>Used to map {@link DataStorageConfigurationSchema#name} to {@link
StorageEngine}.
*/
String name();
+ /**
+ * Returns the data storage configuration schema.
+ */
+ Class<? extends DataStorageConfigurationSchema> schema();
Review Comment:
Technically speaking, this is redundant. I don't necessarily think that you
should remove it, just saying
##########
modules/storage-api/src/test/java/org/apache/ignite/internal/storage/DataStorageModulesTest.java:
##########
@@ -0,0 +1,169 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.storage;
+
+import static
org.apache.ignite.configuration.schemas.store.UnknownDataStorageConfigurationSchema.UNKNOWN_DATA_STORAGE;
+import static
org.apache.ignite.internal.storage.DataStorageModulesTest.FirstDataStorageConfigurationSchema.FIRST;
+import static
org.apache.ignite.internal.storage.DataStorageModulesTest.SecondDataStorageConfigurationSchema.SECOND;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.aMapWithSize;
+import static org.hamcrest.Matchers.equalTo;
+import static org.junit.jupiter.api.Assertions.assertNotSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.nio.file.Path;
+import java.util.List;
+import java.util.Map;
+import org.apache.ignite.configuration.annotation.PolymorphicConfigInstance;
+import org.apache.ignite.configuration.annotation.Value;
+import
org.apache.ignite.configuration.schemas.store.DataStorageConfigurationSchema;
+import org.apache.ignite.internal.configuration.ConfigurationRegistry;
+import org.apache.ignite.internal.storage.engine.StorageEngine;
+import org.apache.ignite.internal.testframework.WorkDirectory;
+import org.apache.ignite.internal.testframework.WorkDirectoryExtension;
+import org.hamcrest.Matchers;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+/**
+ * For {@link DataStorageModules} testing.
+ */
+@ExtendWith(WorkDirectoryExtension.class)
+public class DataStorageModulesTest {
+ @WorkDirectory
+ private Path workDir;
+
+ /**
+ * Test extension of {@link DataStorageConfigurationSchema}.
+ */
+ @PolymorphicConfigInstance(FIRST)
+ public static class FirstDataStorageConfigurationSchema extends
DataStorageConfigurationSchema {
+ static final String FIRST = "first";
+
+ @Value(hasDefault = true)
+ public String strVal = "foo";
+
+ @Value(hasDefault = true)
+ public int intVal = 100;
+ }
+
+ /**
+ * Test extension of {@link DataStorageConfigurationSchema}.
+ */
+ @PolymorphicConfigInstance(SECOND)
+ public static class SecondDataStorageConfigurationSchema extends
DataStorageConfigurationSchema {
+ static final String SECOND = "second";
+
+ @Value(hasDefault = true)
+ public String strVal = "bar";
+
+ @Value(hasDefault = true)
+ public long longVal = 500L;
+ }
+
+ @Test
+ void testDuplicateName() {
+ String sameName = FIRST;
+
+ IllegalStateException exception = assertThrows(
+ IllegalStateException.class,
+ () -> new DataStorageModules(List.of(
+ createMockedStorageEngineFactory(sameName,
FirstDataStorageConfigurationSchema.class),
+ createMockedStorageEngineFactory(sameName,
SecondDataStorageConfigurationSchema.class)
+ ))
+ );
+
+ assertThat(exception.getMessage(), Matchers.startsWith("Duplicate
name"));
+ }
+
+ @Test
+ void testInvalidName() {
+ IllegalStateException exception = assertThrows(
+ IllegalStateException.class,
+ () -> new DataStorageModules(List.of(
+ createMockedStorageEngineFactory(UNKNOWN_DATA_STORAGE,
FirstDataStorageConfigurationSchema.class)
+ ))
+ );
+
+ assertThat(exception.getMessage(), Matchers.startsWith("Invalid
name"));
+ }
+
+ @Test
+ void testNamesDoNotMatch() {
+ IllegalStateException exception = assertThrows(
+ IllegalStateException.class,
+ () -> new DataStorageModules(List.of(
+ createMockedStorageEngineFactory(SECOND,
FirstDataStorageConfigurationSchema.class)
+ ))
+ );
+
+ assertThat(exception.getMessage(), Matchers.startsWith("Names do not
match"));
+ }
+
+ @Test
+ void testCreateDataEngines() {
+ DataStorageModules dataStorageModules = new DataStorageModules(List.of(
+ createMockedStorageEngineFactory(FIRST,
FirstDataStorageConfigurationSchema.class),
+ createMockedStorageEngineFactory(SECOND,
SecondDataStorageConfigurationSchema.class)
+ ));
+
+ Map<String, StorageEngine> engines =
dataStorageModules.createStorageEngines(mock(ConfigurationRegistry.class),
workDir);
+
+ assertThat(engines, aMapWithSize(2));
+
+ assertTrue(engines.containsKey(FIRST));
+ assertTrue(engines.containsKey(SECOND));
+
+ assertNotSame(engines.get(FIRST), engines.get(SECOND));
+ }
+
+ @Test
+ void testCollectSchemasFields() {
+ DataStorageModules dataStorageModules = new DataStorageModules(List.of(
+ createMockedStorageEngineFactory(FIRST,
FirstDataStorageConfigurationSchema.class),
+ createMockedStorageEngineFactory(SECOND,
SecondDataStorageConfigurationSchema.class)
+ ));
+
+ Map<String, Map<String, Class<?>>> fields =
dataStorageModules.collectSchemasFields();
+
+ assertThat(fields, aMapWithSize(2));
+
+ assertThat(fields.get(FIRST), equalTo(Map.of("strVal", String.class,
"intVal", int.class)));
+
+ assertThat(fields.get(SECOND), equalTo(Map.of("strVal", String.class,
"longVal", long.class)));
+ }
+
+ static DataStorageModule createMockedStorageEngineFactory(
Review Comment:
Please rename this method
##########
modules/storage-api/src/main/java/org/apache/ignite/internal/storage/DataStorageManager.java:
##########
@@ -118,6 +96,73 @@ public Consumer<DataStorageChange>
defaultTableDataStorageConsumer(String defaul
};
}
+ /**
+ * Returns the default data storage.
+ *
+ * @param defaultDataStorageView View of {@link
TablesConfigurationSchema#defaultDataStorage}. For the case {@link
+ * UnknownDataStorageConfigurationSchema#UNKNOWN_DATA_STORAGE} and there
is only one engine, then it will be the default.
+ */
+ public String defaultDataStorage(String defaultDataStorageView) {
Review Comment:
I'm still confident that this method is absolutely useless in practice
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]