Skip to content

Core: Update ViewMetadata builder and tests#135

Merged
nastra merged 1 commit intonastra:view-requirements-and-updatesfrom
rdblue:view-requirements-and-updates
Sep 4, 2023
Merged

Core: Update ViewMetadata builder and tests#135
nastra merged 1 commit intonastra:view-requirements-and-updatesfrom
rdblue:view-requirements-and-updates

Conversation

@rdblue
Copy link

@rdblue rdblue commented Sep 3, 2023

This implements the remaining changes need to get apache#8147 in.

@github-actions github-actions bot added the CORE label Sep 3, 2023
@Value.Derived
default Map<Integer, ViewVersion> versionsById() {
return Builder.indexVersions(versions());
ImmutableMap.Builder<Integer, ViewVersion> builder = ImmutableMap.builder();
Copy link
Author

Choose a reason for hiding this comment

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

No need for separate static methods now that these are used directly by the builder in its constructor.

private final List<Schema> schemas;
private final List<ViewHistoryEntry> history;
private final Map<String, String> properties;
private final List<MetadataUpdate> changes;
Copy link
Author

Choose a reason for hiding this comment

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

Some reformatting here to keep final variables separate.

this.versions = Lists.newArrayList(base.versions());
this.versionsById = Maps.newHashMap(base.versionsById());
this.schemas = Lists.newArrayList(base.schemas());
this.schemasById = Maps.newHashMap(base.schemasById());
Copy link
Author

Choose a reason for hiding this comment

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

Rather than running the index method each time a schema or version is added, this pulls the index from the last version and keeps it up to date.


this.formatVersion = newFormatVersion;
this.changes.add(new MetadataUpdate.UpgradeFormatVersion(newFormatVersion));
changes.add(new MetadataUpdate.UpgradeFormatVersion(newFormatVersion));
Copy link
Author

Choose a reason for hiding this comment

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

A few style changes to remove this.

private List<Schema> schemas = Lists.newArrayList();
private int currentVersionId;
private Integer lastAddedVersionId;
private Integer lastAddedSchemaId;
Copy link
Author

Choose a reason for hiding this comment

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

Tracking the last added schema ID was not necessary because we never set the schema ID directly. Now the schema is added at the same time a new version is added in setCurrentVersion(ViewVersion, Schema).

// changes can be added
int highestFieldId = highestFieldId();
for (Schema schema : schemas) {
this.changes.add(new MetadataUpdate.AddSchema(schema, highestFieldId));
Copy link
Author

Choose a reason for hiding this comment

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

The AddSchema change only needs the highest field ID with respect to the existing schemas and the new schema, so these changes can be added in addSchema.

}

return newVersionId;
public Builder addVersion(ViewVersion version) {
Copy link
Author

Choose a reason for hiding this comment

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

Moved some methods around to keep version and schema methods located near one another.

.anyMatch(added -> added.viewVersion().versionId() == newVersionId);
this.lastAddedVersionId = isNewVersion ? newVersionId : null;
if (versionsById.containsKey(newVersionId)) {
boolean addedInBuilder =
Copy link
Author

Choose a reason for hiding this comment

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

The check for lastAddedVersionId != null was redundant, so I removed it. Now it's easier to read.

return newVersionId;
}

Preconditions.checkArgument(
Copy link
Author

Choose a reason for hiding this comment

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

I moved these checks to more appropriate locations.


// expire old versions, but keep at least the versions added in this builder
int numAddedVersions = (int) changes(MetadataUpdate.AddViewVersion.class).count();
int numVersionsToKeep = Math.max(numAddedVersions, historySize);
Copy link
Author

Choose a reason for hiding this comment

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

This is a behavior change. If more versions were added than should be retained, at least one metadata file should contain them.

retainedVersions = expireVersions(versionsById, numVersionsToKeep);
Set<Integer> retainedVersionIds =
retainedVersions.stream().map(ViewVersion::versionId).collect(Collectors.toSet());
retainedHistory = updateHistory(history, retainedVersionIds);
Copy link
Author

Choose a reason for hiding this comment

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

I refactored these. History now uses logic similar to tables. And versions are expired by ID since IDs are assigned in an increasing order. That way we always keep the most recent versions rather than the ones that happened to be at the start of the list.

formatVersion,
location,
schemas,
ImmutableList.copyOf(schemas),
Copy link
Author

Choose a reason for hiding this comment

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

Using immutable collections now.

Copy link
Owner

Choose a reason for hiding this comment

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

no need to do this, as those will be immutable collections internally, which is being handled when the ViewMetadata instance is created

@nastra nastra merged commit f425b1d into nastra:view-requirements-and-updates Sep 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants