Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose getPrimaryKeys() API at HollowStateEngine #474

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -59,4 +64,29 @@ public interface HollowStateEngine extends HollowDataset {
Map<String, String> getHeaderTags();

String getHeaderTag(String name);

default PrimaryKey getPrimaryKey(String typeName) {
PrimaryKey pk=null;
HollowSchema schema = getSchema(typeName);
if (schema.getSchemaType() == HollowSchema.SchemaType.OBJECT) {
Copy link
Contributor

@Sunjeet Sunjeet Jul 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: null check here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea.

pk = ((HollowObjectSchema) schema).getPrimaryKey();
}

return pk;
}

default List<PrimaryKey> getPrimaryKeys() {
List<HollowSchema> schemas = getSchemas();

List<PrimaryKey> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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<PrimaryKey> sortPrimaryKeys(List<PrimaryKey> primaryKeys, HollowStateEngine stateEngine) {
if (primaryKeys.isEmpty()) return primaryKeys;

final List<HollowSchema> dependencyOrderedSchemas = HollowSchemaSorter.dependencyOrderedSchemaList(stateEngine.getSchemas());
primaryKeys.sort(new Comparator<PrimaryKey>() {
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<PrimaryKey> getPrimaryKeys() {
Expand All @@ -194,26 +195,7 @@ private void initializePrimaryKeys() {
}
}

this.primaryKeys = sortPrimaryKeys(keys);
}

private List<PrimaryKey> sortPrimaryKeys(List<PrimaryKey> primaryKeys) {
final List<HollowSchema> dependencyOrderedSchemas = HollowSchemaSorter.dependencyOrderedSchemaList(output.getSchemas());
primaryKeys.sort(new Comparator<PrimaryKey>() {
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);
}

/**
Expand Down