Skip to content

Commit 691e744

Browse files
committed
CDM-22 : refactor to facilitate other functions modifying PK
1 parent b213d9a commit 691e744

File tree

6 files changed

+95
-42
lines changed

6 files changed

+95
-42
lines changed

src/main/java/datastax/astra/migrate/cql/CqlHelper.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -434,10 +434,12 @@ public List<MigrateDataType> getSelectColTypes() {
434434
}
435435

436436
public List<MigrateDataType> getIdColTypes() {
437-
if (featureMap.get(Featureset.CONSTANT_COLUMNS).isEnabled())
438-
return featureMap.get(Featureset.CONSTANT_COLUMNS).getMigrateDataTypeList(ConstantColumns.Property.TARGET_PRIMARY_TYPES_WITHOUT_CONSTANT);
439-
else
440-
return propertyHelper.getMigrationTypeList(KnownProperties.TARGET_PRIMARY_KEY_TYPES);
437+
List<MigrateDataType> rtn = propertyHelper.getMigrationTypeList(KnownProperties.TARGET_PRIMARY_KEY_TYPES);
438+
rtn = (List<MigrateDataType>) featureMap.get(Featureset.CONSTANT_COLUMNS).
439+
featureFunction(ConstantColumns.Function.TARGET_PK_WITHOUT_CONSTANTS,
440+
rtn,
441+
propertyHelper.getStringList(KnownProperties.TARGET_PRIMARY_KEY));
442+
return rtn;
441443
}
442444

443445
// These getters have no usage outside this class

src/main/java/datastax/astra/migrate/cql/features/AbstractFeature.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ protected List<MigrateDataType> getRawMigrateDataTypeList(Enum key) {
138138
return migrateDataTypeLists.get(key);
139139
}
140140

141+
@Override
142+
public Object featureFunction(Enum<?> function, Object... args) {
143+
return null;
144+
}
145+
141146
protected void putString(Enum<?> key, String value) {
142147
strings.put(key, value);
143148
propertyTypes.put(key, KnownProperties.PropertyType.STRING);

src/main/java/datastax/astra/migrate/cql/features/ConstantColumns.java

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import datastax.astra.migrate.MigrateDataType;
44
import datastax.astra.migrate.properties.KnownProperties;
55
import datastax.astra.migrate.properties.PropertyHelper;
6-
import org.apache.commons.lang.StringUtils;
76
import org.slf4j.Logger;
87
import org.slf4j.LoggerFactory;
98

@@ -17,10 +16,16 @@ public class ConstantColumns extends AbstractFeature {
1716
public enum Property {
1817
COLUMN_NAMES,
1918
COLUMN_TYPES,
20-
COLUMN_VALUES,
21-
TARGET_PRIMARY_TYPES_WITHOUT_CONSTANT
19+
COLUMN_VALUES
2220
}
2321

22+
public enum Function {
23+
TARGET_PK_WITHOUT_CONSTANTS,
24+
TEST_FUNCTION
25+
}
26+
27+
private boolean valid = true;
28+
2429
@Override
2530
public boolean initialize(PropertyHelper propertyHelper) {
2631
List<String> columnNames = propertyHelper.getStringList(KnownProperties.CONSTANT_COLUMN_NAMES);
@@ -34,17 +39,26 @@ public boolean initialize(PropertyHelper propertyHelper) {
3439
propertyHelper.getString(KnownProperties.CONSTANT_COLUMN_SPLIT_REGEX));
3540
putStringList(Property.COLUMN_VALUES, columnValues);
3641

37-
List<MigrateDataType> targetPrimaryKeyTypesWithoutConstantColumns =
38-
targetPrimaryKeyTypesWithoutConstantColumns(
39-
columnNames,
40-
propertyHelper.getMigrationTypeList(KnownProperties.TARGET_PRIMARY_KEY_TYPES),
41-
propertyHelper.getStringList(KnownProperties.TARGET_PRIMARY_KEY));
42-
putMigrateDataTypeList(Property.TARGET_PRIMARY_TYPES_WITHOUT_CONSTANT, targetPrimaryKeyTypesWithoutConstantColumns);
43-
4442
isInitialized = true;
45-
if (!isValid(propertyHelper)) return false;
46-
isEnabled = null!=columnNames && !columnNames.isEmpty();
47-
return true;
43+
valid = isValid(propertyHelper);
44+
isEnabled = valid && null!=columnNames && !columnNames.isEmpty();
45+
return valid;
46+
}
47+
48+
@Override
49+
public Object featureFunction(Enum<?> function, Object... args) {
50+
switch ((Function) function) {
51+
case TARGET_PK_WITHOUT_CONSTANTS:
52+
// args[] should be List<MigrateDataType> targetPrimaryKeyTypes, List<String> targetPrimaryKeyNames
53+
if (null==args || args.length!=2 || null==args[0] || null==args[1])
54+
throw new IllegalArgumentException("Expected 2 not-null arguments, got " + (null==args ? "1" : args.length));
55+
if (!(args[0] instanceof List<?>) || ((List<?>) args[0]).isEmpty() || !(((List<?>) args[0]).get(0) instanceof MigrateDataType))
56+
throw new IllegalArgumentException("First argument should be a non-empty List<MigrateDataType>, got " + args[0]);
57+
if (!(args[1] instanceof List<?>) || ((List<?>) args[1]).isEmpty() || !(((List<?>) args[1]).get(0) instanceof String))
58+
throw new IllegalArgumentException("Second argument should be a non-empty List<String>, got " + args[1]);
59+
return targetPrimaryKeyTypesWithoutConstantColumns((List<MigrateDataType>)args[0], (List<String>)args[1]);
60+
}
61+
return null;
4862
}
4963

5064
private List<String> columnValues(String columnValueString, String regexString) {
@@ -61,21 +75,17 @@ private List<String> columnValues(String columnValueString, String regexString)
6175
return columnValues;
6276
}
6377

64-
private List<MigrateDataType> targetPrimaryKeyTypesWithoutConstantColumns(List<String> columnNames, List<MigrateDataType> targetPrimaryKeyTypes, List<String> targetPrimaryKeyNames) {
78+
private List<MigrateDataType> targetPrimaryKeyTypesWithoutConstantColumns(List<MigrateDataType> targetPrimaryKeyTypes, List<String> targetPrimaryKeyNames) {
79+
if (!isEnabled) return targetPrimaryKeyTypes;
80+
if (!valid) return null;
81+
82+
// As this is valid, we know that the column names, types, and values are all the same size
83+
List<String> columnNames = getRawStringList(Property.COLUMN_NAMES);
84+
6585
List<MigrateDataType> rtn = new ArrayList<>();
66-
if (null!=columnNames && !columnNames.isEmpty()) {
67-
if (null==targetPrimaryKeyTypes || null==targetPrimaryKeyNames) {
68-
if (null==targetPrimaryKeyTypes)
69-
logger.error("Target primary key types are not specified in property {}", KnownProperties.TARGET_PRIMARY_KEY_TYPES);
70-
if (null==targetPrimaryKeyNames)
71-
logger.error("Target primary key names are not specified in property {}", KnownProperties.TARGET_PRIMARY_KEY);
72-
}
73-
else {
74-
for (String keyName : targetPrimaryKeyNames) {
75-
if (!columnNames.contains(keyName)) {
76-
rtn.add(targetPrimaryKeyTypes.get(targetPrimaryKeyNames.indexOf(keyName)));
77-
}
78-
}
86+
for (String keyName : targetPrimaryKeyNames) {
87+
if (!columnNames.contains(keyName)) {
88+
rtn.add(targetPrimaryKeyTypes.get(targetPrimaryKeyNames.indexOf(keyName)));
7989
}
8090
}
8191
return rtn;
@@ -85,7 +95,6 @@ private boolean isValid(PropertyHelper propertyHelper) {
8595
List<String> columnNames = getRawStringList(Property.COLUMN_NAMES);
8696
List<MigrateDataType> columnTypes = getRawMigrateDataTypeList(Property.COLUMN_TYPES);
8797
List<String> columnValues = getRawStringList(Property.COLUMN_VALUES);
88-
List<MigrateDataType> targetPrimaryKeyTypesWithoutConstantColumns = getRawMigrateDataTypeList(Property.TARGET_PRIMARY_TYPES_WITHOUT_CONSTANT);
8998

9099
boolean haveColumnNames = null!=columnNames && !columnNames.isEmpty();
91100
boolean haveColumnTypes = null!=columnTypes && !columnTypes.isEmpty();
@@ -116,11 +125,6 @@ private boolean isValid(PropertyHelper propertyHelper) {
116125
valid = false;
117126
}
118127

119-
if (null==targetPrimaryKeyTypesWithoutConstantColumns || targetPrimaryKeyTypesWithoutConstantColumns.isEmpty()) {
120-
logger.warn("There are no primary key columns specified in property {} that are not constant columns. This may be intentional, but it is unusual."
121-
, KnownProperties.TARGET_PRIMARY_KEY);
122-
}
123-
124128
return valid;
125129
}
126130
}

src/main/java/datastax/astra/migrate/cql/features/Feature.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,12 @@ public interface Feature {
119119
* 3. if the property is not a MigrateDataType
120120
*/
121121
public MigrateDataType getMigrateDataType(Enum<?> prop);
122+
123+
/**
124+
* Generic feature function that can be used to implement any feature specific functionality.
125+
* @param function Feature-specific name of the feature to call
126+
* @param args Feature- and function-specific arguments
127+
* @return Function-specific
128+
*/
129+
public Object featureFunction(Enum<?> function, Object... args);
122130
}

src/test/java/datastax/astra/migrate/cql/features/AbstractFeatureTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,14 @@ public void valueSet_butDisabled() {
163163
);
164164
}
165165

166+
enum TestEnum {
167+
TEST
168+
}
169+
170+
@Test
171+
public void featureFunction_Null() {
172+
TestFeature testFeature = new TestFeature();
173+
testFeature.initialize_withValues();
174+
assertNull(testFeature.featureFunction(TestEnum.TEST,""));
175+
}
166176
}

src/test/java/datastax/astra/migrate/cql/features/ConstantColumnsTest.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package datastax.astra.migrate.cql.features;
22

3+
import datastax.astra.migrate.MigrateDataType;
34
import datastax.astra.migrate.cql.CqlHelper;
45
import datastax.astra.migrate.properties.KnownProperties;
56
import datastax.astra.migrate.properties.PropertyHelper;
@@ -8,6 +9,9 @@
89
import org.junit.jupiter.api.BeforeEach;
910
import org.junit.jupiter.api.Test;
1011

12+
import java.util.ArrayList;
13+
import java.util.Arrays;
14+
1115
import static org.junit.jupiter.api.Assertions.*;
1216

1317
public class ConstantColumnsTest {
@@ -35,7 +39,7 @@ private void setValidSparkConf() {
3539
validSparkConf.set(KnownProperties.CONSTANT_COLUMN_TYPES, "0,1");
3640
validSparkConf.set(KnownProperties.CONSTANT_COLUMN_VALUES, "'abcd',1234");
3741
validSparkConf.set(KnownProperties.TARGET_PRIMARY_KEY, "const1,key");
38-
validSparkConf.set(KnownProperties.TARGET_PRIMARY_KEY_TYPES, "0,0");
42+
validSparkConf.set(KnownProperties.TARGET_PRIMARY_KEY_TYPES, "0,4");
3943
}
4044

4145
@Test
@@ -45,10 +49,13 @@ public void smokeTest() {
4549
feature.initialize(helper);
4650
assertAll(
4751
() -> assertTrue(feature.isEnabled()),
48-
() -> assertEquals("const1,const2", feature.getAsString(ConstantColumns.Property.COLUMN_NAMES)),
49-
() -> assertEquals("0,1", feature.getAsString(ConstantColumns.Property.COLUMN_TYPES)),
50-
() -> assertEquals("'abcd',1234", feature.getAsString(ConstantColumns.Property.COLUMN_VALUES)),
51-
() -> assertEquals("0", feature.getAsString(ConstantColumns.Property.TARGET_PRIMARY_TYPES_WITHOUT_CONSTANT))
52+
() -> assertEquals("const1,const2", feature.getAsString(ConstantColumns.Property.COLUMN_NAMES), "COLUMN_NAMES"),
53+
() -> assertEquals("0,1", feature.getAsString(ConstantColumns.Property.COLUMN_TYPES), "COLUMN_TYPES"),
54+
() -> assertEquals("'abcd',1234", feature.getAsString(ConstantColumns.Property.COLUMN_VALUES), "COLUMN_VALUES"),
55+
() -> assertEquals(Arrays.asList(new MigrateDataType(("4"))), feature.featureFunction(ConstantColumns.Function.TARGET_PK_WITHOUT_CONSTANTS,
56+
helper.getMigrationTypeList(KnownProperties.TARGET_PRIMARY_KEY_TYPES),
57+
helper.getStringList(KnownProperties.TARGET_PRIMARY_KEY))
58+
, "TARGET_PK_WITHOUT_CONSTANTS")
5259
);
5360
}
5461

@@ -165,4 +172,21 @@ public void test_missingPrimaryKeyTypes() {
165172
assertTrue(feature.isEnabled());
166173
}
167174

175+
@Test
176+
public void featureFunction_PK_invalidArgs() {
177+
setValidSparkConf();
178+
helper.initializeSparkConf(validSparkConf);
179+
feature.initialize(helper);
180+
assertAll(
181+
() -> assertThrows(IllegalArgumentException.class, () -> feature.featureFunction(ConstantColumns.Function.TARGET_PK_WITHOUT_CONSTANTS), "No arguments"),
182+
() -> assertThrows(IllegalArgumentException.class, () -> feature.featureFunction(ConstantColumns.Function.TARGET_PK_WITHOUT_CONSTANTS, null), "one null argument"),
183+
() -> assertThrows(IllegalArgumentException.class, () -> feature.featureFunction(ConstantColumns.Function.TARGET_PK_WITHOUT_CONSTANTS, null, null), "two null arguments"),
184+
() -> assertThrows(IllegalArgumentException.class, () -> feature.featureFunction(ConstantColumns.Function.TARGET_PK_WITHOUT_CONSTANTS, new ArrayList<MigrateDataType>(), Arrays.asList("abc")), "empty type list"),
185+
() -> assertThrows(IllegalArgumentException.class, () -> feature.featureFunction(ConstantColumns.Function.TARGET_PK_WITHOUT_CONSTANTS, Arrays.asList(1,2,3), Arrays.asList("abc")), "wrong type on type list"),
186+
() -> assertThrows(IllegalArgumentException.class, () -> feature.featureFunction(ConstantColumns.Function.TARGET_PK_WITHOUT_CONSTANTS, Arrays.asList(new MigrateDataType("0")), new ArrayList<String>()), "empty name list"),
187+
() -> assertThrows(IllegalArgumentException.class, () -> feature.featureFunction(ConstantColumns.Function.TARGET_PK_WITHOUT_CONSTANTS, Arrays.asList(new MigrateDataType("0")), Arrays.asList(1,2,3)), "wrong type on name list"),
188+
() -> assertNull(feature.featureFunction(ConstantColumns.Function.TEST_FUNCTION, Arrays.asList(new MigrateDataType("0")), Arrays.asList("abc")), "unimplmented enum")
189+
);
190+
}
191+
168192
}

0 commit comments

Comments
 (0)