Skip to content

Commit

Permalink
HDDS-9317. Provide an option for displaying Ozone snapshot diff outpu…
Browse files Browse the repository at this point in the history
…t as JSON format
  • Loading branch information
hemantk-12 committed May 15, 2024
1 parent b11b807 commit e0b0b10
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public String getMessage() {

@Override
public String toString() {
return message + "\n";
return message;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.apache.hadoop.ozone.snapshot;

import jakarta.annotation.Nonnull;
import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdds.conf.OzoneConfiguration;
Expand All @@ -30,7 +31,9 @@
import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.SnapshotDiffReportProto;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static org.apache.hadoop.ozone.OzoneConsts.OZONE_URI_DELIMITER;
Expand Down Expand Up @@ -72,7 +75,6 @@ public static Codec<DiffReportEntry> getDiffReportEntryCodec() {
*/
private final String token;


public SnapshotDiffReportOzone(final String snapshotRoot,
final String volumeName,
final String bucketName,
Expand All @@ -86,7 +88,9 @@ public SnapshotDiffReportOzone(final String snapshotRoot,
}

public List<DiffReportEntry> getDiffList() {
return super.getDiffList();
return super.getDiffList().stream()
.map(DiffReportEntryOzone::fromDiffReportEntry)
.collect(Collectors.toList());
}

public String getToken() {
Expand Down Expand Up @@ -182,20 +186,6 @@ public static DiffReportEntry fromProtobufDiffReportEntry(
return builder.build();
}


public static DiffReportEntry getDiffReportEntry(final DiffType type,
final String sourcePath) {
return getDiffReportEntry(type, sourcePath, null);
}

public static DiffReportEntry getDiffReportEntry(final DiffType type,
final String sourcePath, final String targetPath) {
return new DiffReportEntry(type,
sourcePath.getBytes(StandardCharsets.UTF_8),
targetPath != null ? targetPath.getBytes(StandardCharsets.UTF_8) :
null);
}

/**
* @param diffReport
* Add the diffReportEntries from given diffReport to the report.
Expand All @@ -204,4 +194,71 @@ public static DiffReportEntry getDiffReportEntry(final DiffType type,
public void aggregate(SnapshotDiffReportOzone diffReport) {
this.getDiffList().addAll(diffReport.getDiffList());
}

/**
* Ozone's diff report entry.
* This is immutable and should not be changed.
*/
public static final class DiffReportEntryOzone extends DiffReportEntry {
private final String sourceKeyPath;

private final String targetKeyPath;

public DiffReportEntryOzone(final DiffType type,
final String sourceKeyPath) {
super(type, sourceKeyPath.getBytes(StandardCharsets.UTF_8), null);
this.sourceKeyPath = sourceKeyPath;
this.targetKeyPath = null;
}

public DiffReportEntryOzone(final DiffType type,
final String sourceKeyPath,
final String targetKeyPath) {
super(type, sourceKeyPath.getBytes(StandardCharsets.UTF_8),
targetKeyPath != null ? targetKeyPath.getBytes(StandardCharsets.UTF_8) : null);
this.sourceKeyPath = sourceKeyPath;
this.targetKeyPath = targetKeyPath;
}

public DiffReportEntryOzone(DiffType type, byte[] sourcePath, byte[] targetPath) {
super(type, sourcePath, targetPath);
this.sourceKeyPath = sourcePath != null ? getPathString(sourcePath) : null;
this.targetKeyPath = targetPath != null ? getPathString(targetPath) : null;
}

public String getSourceKeyPath() {
return sourceKeyPath;
}

public String getTargetKeyPath() {
return targetKeyPath;
}

private String getPathString(@Nonnull byte[] path) {
String pathStr = org.apache.hadoop.hdds.StringUtils.bytes2String(path);
if (pathStr.isEmpty()) {
return Path.CUR_DIR;
} else {
return Path.CUR_DIR + Path.SEPARATOR + pathStr;
}
}

private static DiffReportEntryOzone fromDiffReportEntry(DiffReportEntry entry) {
return new DiffReportEntryOzone(entry.getType(), entry.getSourcePath(), entry.getTargetPath());
}

@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other instanceof DiffReportEntryOzone) {
DiffReportEntryOzone entry = (DiffReportEntryOzone) other;
return Objects.equals(this.getType(), entry.getType())
&& Objects.equals(sourceKeyPath, entry.getSourceKeyPath())
&& Objects.equals(targetKeyPath, entry.getTargetKeyPath());
}
return false;
}
}
}
Loading

0 comments on commit e0b0b10

Please sign in to comment.