From 529501c6784aeba7d844f24abde9757d377fa4fe Mon Sep 17 00:00:00 2001 From: David Su Date: Tue, 14 Jul 2020 13:55:01 -0700 Subject: [PATCH 1/3] Expose getPrimaryKeys() API at HollowStateEngine --- .../hollow/core/HollowStateEngine.java | 20 +++++++++++ .../hollow/core/util/StateEngineUtil.java | 36 +++++++++++++++++++ .../hollow/tools/combine/HollowCombiner.java | 24 ++----------- 3 files changed, 59 insertions(+), 21 deletions(-) create mode 100644 hollow/src/main/java/com/netflix/hollow/core/util/StateEngineUtil.java diff --git a/hollow/src/main/java/com/netflix/hollow/core/HollowStateEngine.java b/hollow/src/main/java/com/netflix/hollow/core/HollowStateEngine.java index e4123c8bec..761053edba 100644 --- a/hollow/src/main/java/com/netflix/hollow/core/HollowStateEngine.java +++ b/hollow/src/main/java/com/netflix/hollow/core/HollowStateEngine.java @@ -17,9 +17,14 @@ package com.netflix.hollow.core; import com.netflix.hollow.api.error.SchemaNotFoundException; +import com.netflix.hollow.core.index.key.PrimaryKey; import com.netflix.hollow.core.read.engine.HollowReadStateEngine; +import com.netflix.hollow.core.schema.HollowObjectSchema; import com.netflix.hollow.core.schema.HollowSchema; +import com.netflix.hollow.core.util.StateEngineUtil; import com.netflix.hollow.core.write.HollowWriteStateEngine; + +import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -59,4 +64,19 @@ public interface HollowStateEngine extends HollowDataset { Map getHeaderTags(); String getHeaderTag(String name); + + default List getPrimaryKeys() { + List schemas = getSchemas(); + + List primaryKeys = new ArrayList<>(); + for (HollowSchema schema : schemas) { + if (schema.getSchemaType() == HollowSchema.SchemaType.OBJECT) { + PrimaryKey pk = ((HollowObjectSchema) schema).getPrimaryKey(); + if (pk != null) + primaryKeys.add(pk); + } + } + + return StateEngineUtil.sortPrimaryKeys(primaryKeys, this); + } } diff --git a/hollow/src/main/java/com/netflix/hollow/core/util/StateEngineUtil.java b/hollow/src/main/java/com/netflix/hollow/core/util/StateEngineUtil.java new file mode 100644 index 0000000000..13fc83638c --- /dev/null +++ b/hollow/src/main/java/com/netflix/hollow/core/util/StateEngineUtil.java @@ -0,0 +1,36 @@ +package com.netflix.hollow.core.util; + +import com.netflix.hollow.core.HollowStateEngine; +import com.netflix.hollow.core.index.key.PrimaryKey; +import com.netflix.hollow.core.schema.HollowSchema; +import com.netflix.hollow.core.schema.HollowSchemaSorter; + +import java.util.Comparator; +import java.util.List; + +public final class StateEngineUtil { + + /** + * Sort Primary Key based on the schemas of specified HollowStateEngine + */ + public static List sortPrimaryKeys(List primaryKeys, HollowStateEngine stateEngine) { + if (primaryKeys.isEmpty()) return primaryKeys; + + final List dependencyOrderedSchemas = HollowSchemaSorter.dependencyOrderedSchemaList(stateEngine.getSchemas()); + primaryKeys.sort(new Comparator() { + public int compare(PrimaryKey o1, PrimaryKey o2) { + return schemaDependencyIdx(o1) - schemaDependencyIdx(o2); + } + + private int schemaDependencyIdx(PrimaryKey key) { + for (int i = 0; i < dependencyOrderedSchemas.size(); i++) { + if (dependencyOrderedSchemas.get(i).getName().equals(key.getType())) + return i; + } + throw new IllegalArgumentException("Primary key defined for non-existent type: " + key.getType()); + } + }); + + return primaryKeys; + } +} \ No newline at end of file diff --git a/hollow/src/main/java/com/netflix/hollow/tools/combine/HollowCombiner.java b/hollow/src/main/java/com/netflix/hollow/tools/combine/HollowCombiner.java index ca9582cfb9..74d561f133 100644 --- a/hollow/src/main/java/com/netflix/hollow/tools/combine/HollowCombiner.java +++ b/hollow/src/main/java/com/netflix/hollow/tools/combine/HollowCombiner.java @@ -32,6 +32,7 @@ import com.netflix.hollow.core.schema.HollowSetSchema; import com.netflix.hollow.core.util.HollowWriteStateCreator; import com.netflix.hollow.core.util.SimultaneousExecutor; +import com.netflix.hollow.core.util.StateEngineUtil; import com.netflix.hollow.core.write.HollowHashableWriteRecord; import com.netflix.hollow.core.write.HollowHashableWriteRecord.HashBehavior; import com.netflix.hollow.core.write.HollowTypeWriteState; @@ -172,7 +173,7 @@ public void setPrimaryKeys(PrimaryKey... newKeys) { keysByType.put(primaryKey.getType(), primaryKey); } - this.primaryKeys = sortPrimaryKeys(new ArrayList<>(keysByType.values())); + this.primaryKeys = StateEngineUtil.sortPrimaryKeys(new ArrayList<>(keysByType.values()), output); } public List getPrimaryKeys() { @@ -194,26 +195,7 @@ private void initializePrimaryKeys() { } } - this.primaryKeys = sortPrimaryKeys(keys); - } - - private List sortPrimaryKeys(List primaryKeys) { - final List dependencyOrderedSchemas = HollowSchemaSorter.dependencyOrderedSchemaList(output.getSchemas()); - primaryKeys.sort(new Comparator() { - public int compare(PrimaryKey o1, PrimaryKey o2) { - return schemaDependencyIdx(o1) - schemaDependencyIdx(o2); - } - - private int schemaDependencyIdx(PrimaryKey key) { - for (int i = 0; i < dependencyOrderedSchemas.size(); i++) { - if (dependencyOrderedSchemas.get(i).getName().equals(key.getType())) - return i; - } - throw new IllegalArgumentException("Primary key defined for non-existent type: " + key.getType()); - } - }); - - return primaryKeys; + this.primaryKeys = StateEngineUtil.sortPrimaryKeys(keys, output); } /** From db35716fccba466cda95a39e26f0ddd7423c62d7 Mon Sep 17 00:00:00 2001 From: David Su Date: Tue, 14 Jul 2020 14:23:51 -0700 Subject: [PATCH 2/3] Add getPrimaryKey(String typeName) --- .../com/netflix/hollow/core/HollowStateEngine.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/hollow/src/main/java/com/netflix/hollow/core/HollowStateEngine.java b/hollow/src/main/java/com/netflix/hollow/core/HollowStateEngine.java index 761053edba..de7fe9a30e 100644 --- a/hollow/src/main/java/com/netflix/hollow/core/HollowStateEngine.java +++ b/hollow/src/main/java/com/netflix/hollow/core/HollowStateEngine.java @@ -65,6 +65,16 @@ public interface HollowStateEngine extends HollowDataset { String getHeaderTag(String name); + default PrimaryKey getPrimaryKey(String typeName) { + PrimaryKey pk=null; + HollowSchema schema = getSchema(typeName); + if (schema.getSchemaType() == HollowSchema.SchemaType.OBJECT) { + pk = ((HollowObjectSchema) schema).getPrimaryKey(); + } + + return pk; + } + default List getPrimaryKeys() { List schemas = getSchemas(); From 9f92644115c2c26c8686a16b53736c88f682ec67 Mon Sep 17 00:00:00 2001 From: David Su Date: Thu, 16 Jul 2020 15:33:13 -0700 Subject: [PATCH 3/3] make sure schema is not null --- .../main/java/com/netflix/hollow/core/HollowStateEngine.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hollow/src/main/java/com/netflix/hollow/core/HollowStateEngine.java b/hollow/src/main/java/com/netflix/hollow/core/HollowStateEngine.java index de7fe9a30e..2bce79de43 100644 --- a/hollow/src/main/java/com/netflix/hollow/core/HollowStateEngine.java +++ b/hollow/src/main/java/com/netflix/hollow/core/HollowStateEngine.java @@ -68,7 +68,7 @@ public interface HollowStateEngine extends HollowDataset { default PrimaryKey getPrimaryKey(String typeName) { PrimaryKey pk=null; HollowSchema schema = getSchema(typeName); - if (schema.getSchemaType() == HollowSchema.SchemaType.OBJECT) { + if (schema!=null && schema.getSchemaType() == HollowSchema.SchemaType.OBJECT) { pk = ((HollowObjectSchema) schema).getPrimaryKey(); }