Skip to content

Refactor index_data.painless script and params#1207

Open
ellisandrews-toast wants to merge 1 commit into
block:mainfrom
ellisandrews-toast:refactor-painless-script
Open

Refactor index_data.painless script and params#1207
ellisandrews-toast wants to merge 1 commit into
block:mainfrom
ellisandrews-toast:refactor-painless-script

Conversation

@ellisandrews-toast
Copy link
Copy Markdown
Contributor

Summary

This is a preparatory refactoring of the index_data Painless update script to make it ready for nested sourced_from support. No behavior change.

  • Restructure the flat script into helper functions
  • Rename params.dataparams.topLevelFields to clarify intent (since putAll only applies top-level fields)

Script structure after refactoring

setup(...)                // Initializes bookkeeping structures (__sources, __versions, __counts)
validateSource(...)       // Checks for relationship mutation and version staleness (throws if stale)
applyTopLevelFields(...)  // Does putAll for top-level fields and merges __counts
recordSource(...)         // Sets id, records version, maintains __sources sorted list

Why rename params.data → params.topLevelFields?

When nested sourced_from support is added, the script will also receive params.nestedFields (data that gets stored in __nested_sourced_data and applied to nested elements). Renaming data to topLevelFields now makes the distinction clear and avoids a confusing rename later when both params coexist.

@ellisandrews-toast ellisandrews-toast changed the title Refactor index_data.painless script Refactor index_data.painless script and params May 22, 2026
Copy link
Copy Markdown
Collaborator

@myronmarston myronmarston left a comment

Choose a reason for hiding this comment

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

Looking good! Left some minor suggestions.

);
}

Number maybeDocVersion = source.__versions.get(params.relationship)?.get(params.sourceId);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

On the surface, it looks like there may be a risk of a null pointer exception here--previously we did map?.get(params.sourceId) which would evaluate to null if map was null; nwo we do map.get(sourceId) which will throw an NPE if map is null.

But looking at the setup function, it looks like we initialize source.__versions[relationship] if it's not already set, so I'm thinking it's impossible for the map to be null here.

Can you confirm that analysis matches your own?


source.id = params.id;
void recordSource(Map source, String id, String relationship, String sourceId, long eventVersion) {
source.id = id;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Setting source.id doesn't feel like it should be part of recordSource. Can we move this into applyTopLevelFields? After all, source.id = id is setting the top-level id field.

def self.merge_list_counts_into(params, mapping:, list_counts_field_paths_for_source:)
# Here we compute the counts of our list elements so that we can index it.
data = compute_list_counts_of(params.fetch("data"), CountAccumulator.new_parent(
data = compute_list_counts_of(params.fetch("topLevelFields"), CountAccumulator.new_parent(
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Worth renaming data = to top_level_fields =?

)

without_id_or_data = params.except("id", "data")
without_id_or_data = params.except("id", "topLevelFields")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
without_id_or_data = params.except("id", "topLevelFields")
without_id_or_top_level_fields = params.except("id", "topLevelFields")

(And you'll need to update the reference to it below).

end

def params_for(doc_id:, event:, prepared_record:)
data = data_params.to_h do |name, param|
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Worth renaming data = to top_level_fields = ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants