Skip to content

Commit d8f035c

Browse files
committed
core: Cache NFS file handle bytes in Inode
Currently, we regularly convert between Inode and NFS file handles, but keep regenerating the byte representation, even if it is unchanged. Cache the byte representation whenever possible, returning clones of the byte array upon calling Inode#toNfsHandle instead of using ByteBuffer. Related: #149 Signed-off-by: Christian Kohlschütter <[email protected]>
1 parent bd99da3 commit d8f035c

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

core/src/main/java/org/dcache/nfs/vfs/Inode.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public class Inode {
5151
private final int exportIdx;
5252
private final int type;
5353
private final byte[] fs_opaque;
54+
private final byte[] nfsHandle;
5455

5556
@Deprecated(forRemoval = true)
5657
public Inode(FileHandle fh) {
@@ -73,6 +74,8 @@ public Inode(int generation, int exportIdx, int type, byte[] fs_opaque) {
7374
this.exportIdx = exportIdx;
7475
this.type = type;
7576
this.fs_opaque = fs_opaque;
77+
78+
this.nfsHandle = buildNfsHandle();
7679
}
7780

7881
/**
@@ -105,6 +108,7 @@ public Inode(byte[] bytes) {
105108
fs_opaque = new byte[olen];
106109
b.get(fs_opaque);
107110

111+
this.nfsHandle = bytes.clone();
108112
} else if (arrayEquals(bytes, FH_V0_REG, FH_V0_REG.length)
109113
|| arrayEquals(bytes, FH_V0_PFS, FH_V0_PFS.length)) {
110114
magic = MAGIC;
@@ -125,6 +129,8 @@ public Inode(byte[] bytes) {
125129
exportIdx = -1;
126130
fs_opaque = bytes;
127131
}
132+
133+
this.nfsHandle = buildNfsHandle();
128134
} else {
129135
throw new IllegalArgumentException("Unsupported version: " + geussVersion);
130136
}
@@ -157,7 +163,7 @@ protected byte[] getFsOpaque() {
157163

158164
@Override
159165
public String toString() {
160-
return BaseEncoding.base16().lowerCase().encode(this.toNfsHandle());
166+
return BaseEncoding.base16().lowerCase().encode(nfsHandle);
161167
}
162168

163169
private static boolean arrayEquals(byte[] a1, byte[] a2, int len) {
@@ -184,6 +190,10 @@ public byte[] getFileId() {
184190
}
185191

186192
public byte[] toNfsHandle() {
193+
return nfsHandle.clone();
194+
}
195+
196+
private byte[] buildNfsHandle() {
187197
int len = fs_opaque.length + MIN_LEN;
188198
byte[] bytes = new byte[len];
189199
ByteBuffer b = ByteBuffer.wrap(bytes);
@@ -196,11 +206,11 @@ public byte[] toNfsHandle() {
196206
b.put((byte) fs_opaque.length);
197207
b.put(fs_opaque);
198208
return bytes;
199-
}
209+
}
200210

201211
@Override
202212
public int hashCode() {
203-
return Arrays.hashCode(toNfsHandle());
213+
return Arrays.hashCode(nfsHandle);
204214
}
205215

206216
@Override
@@ -212,7 +222,7 @@ public boolean equals(Object obj) {
212222
return false;
213223
}
214224
final Inode other = (Inode) obj;
215-
return Arrays.equals(toNfsHandle(), other.toNfsHandle());
225+
return Arrays.equals(nfsHandle, other.nfsHandle);
216226
}
217227

218228
public boolean isPseudoInode() {

0 commit comments

Comments
 (0)