io.camunda.operate.webapp.backup.Metadata Maven / Gradle / Ivy
The newest version!
/*
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH under
* one or more contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright ownership.
* Licensed under the Camunda License 1.0. You may not use this file
* except in compliance with the Camunda License 1.0.
*/
package io.camunda.operate.webapp.backup;
import io.camunda.operate.exceptions.OperateRuntimeException;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Metadata {
public static final String SNAPSHOT_NAME_PREFIX = "camunda_operate_";
private static final String SNAPSHOT_NAME_PATTERN =
"{prefix}{version}_{prefix}{version}_part_{index}_of_{count}{index}_of_{count}";
private static final String SNAPSHOT_NAME_PREFIX_PATTERN = SNAPSHOT_NAME_PREFIX + "{backupId}_";
private static final Pattern BACKUPID_PATTERN =
Pattern.compile(SNAPSHOT_NAME_PREFIX + "(\\d*)_.*");
private Long backupId;
private String version;
private Integer partNo;
private Integer partCount;
public static String buildSnapshotNamePrefix(final Long backupId) {
return SNAPSHOT_NAME_PREFIX_PATTERN.replace("{backupId}", String.valueOf(backupId));
}
// backward compatibility with v. 8.1
public static Long extractBackupIdFromSnapshotName(final String snapshotName) {
final Matcher matcher = BACKUPID_PATTERN.matcher(snapshotName);
if (matcher.matches()) {
return Long.valueOf(matcher.group(1));
} else {
throw new OperateRuntimeException(
"Unable to extract backupId. Snapshot name: " + snapshotName);
}
}
public Long getBackupId() {
return backupId;
}
public Metadata setBackupId(final Long backupId) {
this.backupId = backupId;
return this;
}
public String getVersion() {
return version;
}
public Metadata setVersion(final String version) {
this.version = version;
return this;
}
public Integer getPartNo() {
return partNo;
}
public Metadata setPartNo(final Integer partNo) {
this.partNo = partNo;
return this;
}
public Integer getPartCount() {
return partCount;
}
public Metadata setPartCount(final Integer partCount) {
this.partCount = partCount;
return this;
}
public String buildSnapshotName() {
return SNAPSHOT_NAME_PATTERN
.replace("{prefix}", buildSnapshotNamePrefix(backupId))
.replace("{version}", version)
.replace("{index}", partNo + "")
.replace("{count}", partCount + "");
}
@Override
public int hashCode() {
return Objects.hash(version, partNo, partCount);
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final Metadata that = (Metadata) o;
return Objects.equals(version, that.version)
&& Objects.equals(partNo, that.partNo)
&& Objects.equals(partCount, that.partCount);
}
}