Skip to content

Commit

Permalink
Merge pull request #194 from jembi/dev_delete_indexs
Browse files Browse the repository at this point in the history
Updating indexes before linking, and after linking
  • Loading branch information
walisc authored Mar 12, 2024
2 parents eb25db9 + dd6d1a2 commit 2109005
Show file tree
Hide file tree
Showing 19 changed files with 554 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,13 @@
}
}
],
"indexes": {
"linking": {
"national_id": {
"props": "@index(exact,trigram)"
}
}
},
"rules": {
"link": {
"deterministic": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,16 @@ case class Rules(
matchNotification: Option[AllRules]
)

case class IndexProps(props: String)
case class Indexes(
linking: Option[Map[String, IndexProps]],
matching: Option[Map[String, IndexProps]]
)
case class Config(
uniqueInteractionFields: Option[Array[UniqueField]],
uniqueGoldenRecordFields: Option[Array[UniqueField]],
additionalNodes: Option[Array[AdditionalNode]],
demographicFields: Array[DemographicField],
indexes: Indexes,
rules: Rules
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
package configuration

import java.io.{File, PrintWriter}

private object CustomDgraphIndexes {

private val classLocation =
"../JeMPI_LibMPI/src/main/java/org/jembi/jempi/libmpi/dgraph"
private val custom_className = "CustomDgraphIndexes"
private val packageText = "org.jembi.jempi.libmpi.dgraph"

def generate(config: Config): Unit = {
val classFile: String =
classLocation + File.separator + custom_className + ".java"
println("Creating " + classFile)
val file: File = new File(classFile)
val writer: PrintWriter = new PrintWriter(file)
writer.println(
s"""package $packageText;
|
|
|final class $custom_className {
|
| private $custom_className() {
| }
|
| public static Boolean shouldUpdateLinkingIndexes() {
| return ${get_should_update_linking_indexes()};
| }
|""".stripMargin);

get_default_indexes()
get_remove_all_indexes()
get_linking_indexes()

writer.println(
s"""
|}
|""".stripMargin)

writer.flush()
writer.close()


def get_should_update_linking_indexes(): String = {
if (config.rules.link.isDefined){
val linkRules = config.rules.link.get
if (linkRules.deterministic.isDefined && !linkRules.probabilistic.isDefined){
return "true"
}
}

return "false"
}

def get_default_indexes(): Unit = {
write_out_indexs_fields("LOAD_DEFAULT_INDEXES", write_field_with_index)
}

def get_remove_all_indexes(): Unit = {
write_out_indexs_fields("REMOVE_ALL_INDEXES", write_field_without_index)
}

def get_linking_indexes(): Unit = {
write_out_linking_fields("LOAD_LINKING_INDEXES", "linking")
}

def write_out_linking_fields(prop_name: String, index_type:String): Unit = {
writer.println(
s""" static final String $prop_name =
| \"\"\"
|""".stripMargin)

var indexsObjOption:Option[Map[String, IndexProps]] = null;

if (index_type == "linking") {
indexsObjOption = config.indexes.linking
} else if (index_type == "matching") {
indexsObjOption = config.indexes.matching
}

if (indexsObjOption.isDefined){
val indexsObj = indexsObjOption.get

val index_names = indexsObj.keySet
val valid_index_fields = config.demographicFields.filter(f => index_names.contains(f.fieldName))

if (valid_index_fields.size != indexsObj.size) {
throw new IllegalArgumentException("Some fields defined in the index property do not exist. Make sure you have defined them in the demographicFields property")
}

valid_index_fields.foreach(field => {
val indexPropsOpt = indexsObj.get(field.fieldName)

if (indexPropsOpt.isDefined){
write_field_with_index(field, "GoldenRecord", indexPropsOpt.get.props, 29)
write_field_with_index(field, "Interaction", indexPropsOpt.get.props, 30)
}
})

val index_to_remove = config.demographicFields.filter(f => !index_names.contains(f.fieldName))
index_to_remove.foreach(field => {
val indexGoldenRecord = field.indexGoldenRecord.getOrElse("")
val indexInteraction = field.indexInteraction.getOrElse("")

if (indexGoldenRecord != "") {
write_field_without_index(field, "GoldenRecord", indexGoldenRecord, 29)
}

if (indexInteraction != "") {
write_field_without_index(field, "Interaction", indexInteraction, 30)
}
})
}

writer.println(
s"""
| \"\"\";
|""".stripMargin)

}

def write_out_indexs_fields(prop_name: String, write_func: (field: DemographicField
, recordType: String
, index: String
, spacing: Int) => Unit): Unit = {
writer.println(
s""" static final String $prop_name =
| \"\"\"
|""".stripMargin)

config.demographicFields
.foreach(field => {
val indexGoldenRecord = field.indexGoldenRecord.getOrElse("")
val indexInteraction = field.indexInteraction.getOrElse("")

if (indexGoldenRecord != "") {
write_func(field, "GoldenRecord", indexGoldenRecord, 29)
}

if (indexInteraction != "") {
write_func(field, "Interaction", indexInteraction, 30)
}
})

removeUniqueIndexes()

writer.println(
s"""
| \"\"\";
|""".stripMargin)
}

def write_field_with_index(field: DemographicField, recordType: String, index: String, spacing: Int): Unit = {
val name = field.fieldName

val fieldType =
(if field.isList.isDefined && field.isList.get then "["
else "") + field.fieldType.toLowerCase + (if field.isList.isDefined && field.isList.get
then "]"
else "")
writer.println(
s"""${" " * 9}$recordType.$name:${" " * (spacing - name.length)}$fieldType${" " * (10 - fieldType.length)}$index${" " * (35 - index.length)}.""".stripMargin
)
}

def write_field_without_index(field: DemographicField, recordType: String, index: String, spacing: Int): Unit = {
val name = field.fieldName

val fieldType =
(if field.isList.isDefined && field.isList.get then "["
else "") + field.fieldType.toLowerCase + (if field.isList.isDefined && field.isList.get
then "]"
else "")
writer.println(
s"""${" " * 9}$recordType.$name:${" " * (spacing - name.length)}$fieldType${" " * (10 - fieldType.length)}${" " * 35}.""".stripMargin
)
}

def removeUniqueIndexes(): Unit = {
List(config.indexes.linking, config.indexes.matching).foreach(indexsObjOption => {
if (indexsObjOption.isDefined) {
val indexsObj = indexsObjOption.get

val index_names = indexsObj.keySet
val uniqueIndexes = config.demographicFields.filter(f => index_names.contains(f.fieldName) && f.indexGoldenRecord.getOrElse("") == "" && f.indexInteraction.getOrElse("") == "")

uniqueIndexes.foreach(field => {
write_field_without_index(field, "GoldenRecord", "", 29)
write_field_without_index(field, "Interaction", "", 30)
})

}
})
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ object Main {
CustomDgraphGoldenRecord.generate(config)
CustomDgraphExpandedGoldenRecord.generate(config)
CustomDgraphExpandedInteraction.generate(config)
CustomDgraphIndexes.generate(config)
CustomDgraphMutations.generate(config)
CustomDgraphQueries.generate(config)
CustomLinkerDeterministic.generate(config)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jembi.jempi.libmpi.dgraph.LibDgraph;
import org.jembi.jempi.libmpi.lib.hooks.HooksRunner;
import org.jembi.jempi.libmpi.postgresql.LibPostgresql;
import org.jembi.jempi.shared.kafka.MyKafkaProducer;
import org.jembi.jempi.shared.models.*;
Expand All @@ -21,8 +22,8 @@ public final class LibMPI {

private static final Logger LOGGER = LogManager.getLogger(LibMPI.class);
private final LibMPIClientInterface client;

private final MyKafkaProducer<String, AuditEvent> topicAuditEvents;
private final HooksRunner hooksRunner;

public LibMPI(
final Level level,
Expand All @@ -37,6 +38,7 @@ public LibMPI(
new JsonPojoSerializer<>(),
kafkaClientId);
client = new LibDgraph(level, host, port);
hooksRunner = new HooksRunner(client);
}

public LibMPI(
Expand All @@ -52,6 +54,7 @@ public LibMPI(
new JsonPojoSerializer<>(),
kafkaClientId);
client = new LibPostgresql(URL, USR, PSW);
hooksRunner = new HooksRunner(client);
}

private void sendAuditEvent(
Expand Down Expand Up @@ -358,4 +361,20 @@ public LinkInfo createInteractionAndLinkToClonedGoldenRecord(
return result;
}

/*
* *****************************************************************************
* *
* Hooks
* *****************************************************************************
* *
*/

public List<MpiGeneralError> beforeLinkingHook() {
return hooksRunner.beforeLinkingHook();
}

public List<MpiGeneralError> afterLinkingHook() {
return hooksRunner.afterLinkingHook();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ LinkInfo createInteractionAndLinkToClonedGoldenRecord(
Interaction interaction,
float score);

Option<MpiGeneralError> deleteAllIndexes();
Option<MpiGeneralError> loadLinkingIndexes();
Option<MpiGeneralError> loadDefaultIndexes();
Boolean shouldUpdateLinkingIndexes();


record GoldenIdScore(
String goldenId,
float score) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,4 @@ record CandidatesNotFoundError(
String error,
String interactionID) implements MpiServiceError {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.jembi.jempi.libmpi.dgraph;


final class CustomDgraphIndexes {

private CustomDgraphIndexes() {
}

public static Boolean shouldUpdateLinkingIndexes() {
return false;
}

static final String LOAD_DEFAULT_INDEXES =
"""
GoldenRecord.given_name: string @index(exact,trigram) .
Interaction.given_name: string @index(exact,trigram) .
GoldenRecord.family_name: string @index(exact,trigram) .
Interaction.family_name: string @index(exact,trigram) .
GoldenRecord.gender: string @index(exact,trigram) .
GoldenRecord.city: string @index(trigram) .
GoldenRecord.phone_number: string @index(exact,trigram) .
GoldenRecord.national_id: string @index(exact,trigram) .
Interaction.national_id: string @index(exact,trigram) .
""";

static final String REMOVE_ALL_INDEXES =
"""
GoldenRecord.given_name: string .
Interaction.given_name: string .
GoldenRecord.family_name: string .
Interaction.family_name: string .
GoldenRecord.gender: string .
GoldenRecord.city: string .
GoldenRecord.phone_number: string .
GoldenRecord.national_id: string .
Interaction.national_id: string .
""";

static final String LOAD_LINKING_INDEXES =
"""
GoldenRecord.national_id: string @index(exact,trigram) .
Interaction.national_id: string @index(exact,trigram) .
GoldenRecord.given_name: string .
Interaction.given_name: string .
GoldenRecord.family_name: string .
Interaction.family_name: string .
GoldenRecord.gender: string .
GoldenRecord.city: string .
GoldenRecord.phone_number: string .
""";


}

Loading

0 comments on commit 2109005

Please sign in to comment.