Skip to content

Commit

Permalink
pool: introduce clone method for FileAttributes
Browse files Browse the repository at this point in the history
Motivation:

Occationally, we wish to make a copy of an existing FileAttributes
object to make temporary changes.  By using the standard 'clone' Java
feature, the important changes are visible, and not drowned out by all
the copying.

Modification:

Add the clone method in FileAttributes class.

Update pool to use this method.

Result:

No user or admin visible changes.

Hopefully we have easier to maintain code.

Target: master
Requires-notes: no
Requires-book: no
Patch: https://rb.dcache.org/r/11536
Acked-by: Tigran Mkrtchyan
  • Loading branch information
paulmillar committed Feb 11, 2019
1 parent 9fde33d commit b3d0f02
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import diskCacheV111.util.AccessLatency;
import diskCacheV111.util.PnfsId;
import diskCacheV111.util.RetentionPolicy;
import diskCacheV111.vehicles.GenericStorageInfo;
import diskCacheV111.vehicles.StorageInfo;

import org.dcache.acl.ACL;
Expand Down Expand Up @@ -56,7 +58,7 @@
*
* @since 1.9.5
*/
public class FileAttributes implements Serializable {
public class FileAttributes implements Serializable, Cloneable {


private static final long serialVersionUID = -3689129805631724432L;
Expand Down Expand Up @@ -172,6 +174,106 @@ public class FileAttributes implements Serializable {
*/
private String _cacheClass;

@Override
public FileAttributes clone()
{
try {
FileAttributes clone = (FileAttributes) super.clone();

if (isDefined(ACL)) {
clone.setAcl(getAcl());
}

if (isDefined(SIZE)) {
clone.setSize(getSize());
}

if (isDefined(CHANGE_TIME)) {
clone.setChangeTime(getChangeTime());
}

if (isDefined(CREATION_TIME)) {
clone.setCreationTime(getCreationTime());
}

if (isDefined(ACCESS_TIME)) {
clone.setAccessTime(getAccessTime());
}

if (isDefined(MODIFICATION_TIME)) {
clone.setModificationTime(getModificationTime());
}

if (isDefined(CHECKSUM)) {
Set<Checksum> checksums = new HashSet<>(getChecksums());
clone.setChecksums(checksums);
}

if (isDefined(OWNER)) {
clone.setOwner(getOwner());
}

if (isDefined(OWNER_GROUP)) {
clone.setGroup(getGroup());
}

if (isDefined(MODE)) {
clone.setMode(getMode());
}

if (isDefined(NLINK)) {
clone.setNlink(getNlink());
}

if (isDefined(ACCESS_LATENCY)) {
clone.setAccessLatency(getAccessLatency());
}

if (isDefined(RETENTION_POLICY)) {
clone.setRetentionPolicy(getRetentionPolicy());
}

if (isDefined(TYPE)) {
clone.setFileType(getFileType());
}

if (isDefined(LOCATIONS)) {
Collection<String> locations = new ArrayList<>(getLocations());
clone.setLocations(locations);
}

if (isDefined(FLAGS)) {
Map<String,String> flags = new HashMap<>(getFlags());
clone.setFlags(flags);
}

if (isDefined(PNFSID)) {
clone.setPnfsId(getPnfsId());
}

if (isDefined(STORAGEINFO)) {
StorageInfo oldValue = getStorageInfo();
clone.setStorageInfo(oldValue.clone());
}

if (isDefined(STORAGECLASS)) {
clone.setStorageClass(getStorageClass());
}

if (isDefined(HSM)) {
clone.setHsm(getHsm());
}

if (isDefined(CACHECLASS)) {
clone.setCacheClass(getCacheClass());
}

return clone;
} catch (CloneNotSupportedException e) {
throw new RuntimeException("Bad clone: " + e, e);
}
}

/** Throws IllegalStateException if attribute is not defined. */
private void guard(FileAttribute attribute)
throws IllegalStateException
Expand Down
22 changes: 2 additions & 20 deletions modules/dcache/src/main/java/org/dcache/pool/classic/PoolV4.java
Original file line number Diff line number Diff line change
Expand Up @@ -915,27 +915,9 @@ private void initiateReplication(PnfsId id, String source)

private void _initiateReplication(CacheEntry entry, String source)
{
PnfsId pnfsId = entry.getPnfsId();
FileAttributes fileAttributes = entry.getFileAttributes();
StorageInfo storageInfo = fileAttributes.getStorageInfo().clone();

storageInfo.setKey("replication.source", source);

FileAttributes attributes = new FileAttributes();
attributes.setPnfsId(pnfsId);
attributes.setStorageInfo(storageInfo);
attributes.setStorageClass(fileAttributes.getStorageClass());
if (fileAttributes.isDefined(CHECKSUM)) {
attributes.setChecksums(fileAttributes.getChecksums());
}
attributes.setSize(fileAttributes.getSize());
attributes.setCacheClass(fileAttributes.getCacheClass());
attributes.setHsm(fileAttributes.getHsm());
attributes.setFlags(fileAttributes.getFlags());
FileAttributes attributes = entry.getFileAttributes().clone();
attributes.setLocations(Collections.singleton(_poolName));
attributes.setSize(fileAttributes.getSize());
attributes.setAccessLatency(fileAttributes.getAccessLatency());
attributes.setRetentionPolicy(fileAttributes.getRetentionPolicy());
attributes.getStorageInfo().setKey("replication.source", source);

PoolMgrReplicateFileMsg req =
new PoolMgrReplicateFileMsg(attributes,
Expand Down

0 comments on commit b3d0f02

Please sign in to comment.