Skip to content

Commit

Permalink
[Standalone] Introduce FileAction.tagsOrEmpty (#3132)
Browse files Browse the repository at this point in the history
## Description
This PR introduces Introduce `FileAction.tagsOrEmpty` to factor out the
common pattern `Option(tags).getOrElse(Map.empty)`.

## How was this patch tested?
Existing unit tests.

## Does this PR introduce _any_ user-facing changes?
No

Signed-off-by: Sergiu Pocol <sergiu.pocol@databricks.com>
  • Loading branch information
sergiupoco-db authored May 22, 2024
1 parent a5263cc commit 420d9e0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,10 @@ sealed trait FileAction extends Action {
@JsonIgnore
def estLogicalFileSize: Option[Long]

/** Returns [[tags]] or an empty Map if null */
@JsonIgnore
def tagsOrEmpty: Map[String, String] = Option(tags).getOrElse(Map.empty[String, String])

/**
* Return tag value if tags is not null and the tag present.
*/
Expand Down Expand Up @@ -788,16 +792,15 @@ case class AddFile(


def copyWithTags(newTags: Map[String, String]): AddFile =
copy(tags = Option(tags).getOrElse(Map.empty) ++ newTags)

copy(tags = tagsOrEmpty ++ newTags)

def tag(tag: AddFile.Tags.KeyType): Option[String] = getTag(tag.name)

def longTag(tagKey: AddFile.Tags.KeyType): Option[Long] =
tag(tagKey).map(_.toLong)

def copyWithTag(tag: AddFile.Tags.KeyType, value: String): AddFile =
copy(tags = Option(tags).getOrElse(Map.empty) + (tag.name -> value))
copy(tags = tagsOrEmpty + (tag.name -> value))

def copyWithoutTag(tag: AddFile.Tags.KeyType): AddFile = {
if (tags == null) {
Expand Down Expand Up @@ -916,13 +919,13 @@ case class RemoveFile(
* Create a copy with the new tag. `extendedFileMetadata` is copied unchanged.
*/
def copyWithTag(tag: String, value: String): RemoveFile = copy(
tags = Option(tags).getOrElse(Map.empty) + (tag -> value))
tags = tagsOrEmpty + (tag -> value))

/**
* Create a copy without the tag.
*/
def copyWithoutTag(tag: String): RemoveFile =
copy(tags = Option(tags).getOrElse(Map.empty) - tag)
copy(tags = tagsOrEmpty - tag)

@JsonIgnore
override def getFileSize: Long = size.getOrElse(0L)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ object ZCubeInfo extends DeltaCommand {
* Update the given file's metadata to make it part of the given zCubeInfo.
*/
def setForFile(file: AddFile, zCubeInfo: ZCubeInfo): AddFile = {
val oldTags = Option(file.tags).getOrElse(Map.empty)
val newTags = oldTags ++ ZCubeInfo.toAddFileTags(zCubeInfo)
val newTags = file.tagsOrEmpty ++ ZCubeInfo.toAddFileTags(zCubeInfo)
file.copy(tags = newTags)
}

Expand Down

0 comments on commit 420d9e0

Please sign in to comment.