Skip to content

Commit 8f4c511

Browse files
committed
core: Opaque: Improve reuse of file id data for PseudoFS innerInode
... and add Opaque#numBytes and #putBytes. This simplifies the dance between public-facing and inner Inode objects, allowing Opaque objects to be reused between these. Related: #149 Signed-off-by: Christian Kohlschütter <[email protected]>
1 parent d1bc72d commit 8f4c511

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

core/src/main/java/org/dcache/nfs/util/Opaque.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,29 @@ static boolean defaultEquals(Opaque obj, Object other) {
8888
*/
8989
byte[] toBytes();
9090

91+
/**
92+
* Returns the number of bytes in this opaque object;
93+
*
94+
* @return The number of bytes;
95+
*/
96+
int numBytes();
97+
9198
/**
9299
* Returns a Base64 string representing this opaque object.
93100
*
94101
* @return A Base64 string.
95102
*/
96103
String toBase64();
97104

105+
/**
106+
* Writes the bytes of this {@link Opaque} to the given {@link ByteBuffer}.
107+
*
108+
* @param buf The target buffer.
109+
*/
110+
default void putBytes(ByteBuffer buf) {
111+
buf.put(toBytes());
112+
}
113+
98114
/**
99115
* Returns the hashCode based on the byte-representation of this instance.
100116
* <p>
@@ -136,6 +152,11 @@ public String toBase64() {
136152
return base64;
137153
}
138154

155+
@Override
156+
public void putBytes(ByteBuffer buf) {
157+
buf.put(_opaque);
158+
}
159+
139160
@Override
140161
public int hashCode() {
141162
return Arrays.hashCode(_opaque);
@@ -166,5 +187,10 @@ public boolean equals(Object o) {
166187
public String toString() {
167188
return super.toString() + "[" + toBase64() + "]";
168189
}
190+
191+
@Override
192+
public int numBytes() {
193+
return _opaque.length;
194+
}
169195
}
170196
}

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,18 @@ public Inode(FileHandle fh) {
6969
*/
7070
@Deprecated
7171
public Inode(int generation, int exportIdx, int type, byte[] fs_opaque) {
72+
this(generation, exportIdx, type, Opaque.forBytes(fs_opaque));
73+
}
74+
75+
private Inode(int generation, int exportIdx, int type, Opaque fileIdKey) {
7276
this.version = VERSION;
7377
this.magic = MAGIC;
7478
this.generation = generation;
7579
this.exportIdx = exportIdx;
7680
this.type = type;
77-
this.opaqueKey = Opaque.forBytes(fs_opaque);
81+
this.opaqueKey = fileIdKey;
7882

79-
this.nfsHandle = buildNfsHandle(fs_opaque);
83+
this.nfsHandle = buildNfsHandle();
8084
}
8185

8286
/**
@@ -146,6 +150,10 @@ public static Inode forFileIdKey(Opaque key) {
146150
return forFile(key.toBytes());
147151
}
148152

153+
public static Inode innerInode(Inode outerInode) {
154+
return new Inode(0, 0, 0, outerInode.getFileIdKey());
155+
}
156+
149157
@Deprecated(forRemoval = true)
150158
public byte[] getFileId() {
151159
return opaqueKey.toBytes();
@@ -178,8 +186,13 @@ public byte[] toNfsHandle() {
178186
return nfsHandle.clone();
179187
}
180188

181-
private byte[] buildNfsHandle(byte[] fs_opaque) {
182-
int len = fs_opaque.length + MIN_LEN;
189+
private byte[] buildNfsHandle() {
190+
int opaqueLen = opaqueKey.numBytes();
191+
if (opaqueLen < 0 || opaqueLen > 255) {
192+
throw new IllegalStateException("Invalid opaque key length");
193+
}
194+
195+
int len = MIN_LEN + opaqueLen;
183196
byte[] bytes = new byte[len];
184197
ByteBuffer b = ByteBuffer.wrap(bytes);
185198
b.order(ByteOrder.BIG_ENDIAN);
@@ -188,8 +201,8 @@ private byte[] buildNfsHandle(byte[] fs_opaque) {
188201
b.putInt(generation);
189202
b.putInt(exportIdx);
190203
b.put((byte) type);
191-
b.put((byte) fs_opaque.length);
192-
b.put(fs_opaque);
204+
b.put((byte) opaqueLen);
205+
opaqueKey.putBytes(b);
193206
return bytes;
194207
}
195208

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,6 @@ private boolean inheritUidGid(Inode inode) {
748748
* @return The {@link Inode} as passed from {@link PseudoFs} to the underlying {@link VirtualFileSystem}.
749749
*/
750750
private Inode innerInode(Inode inode) {
751-
return Inode.forFile(inode.getFileId());
751+
return Inode.innerInode(inode);
752752
}
753753
}

0 commit comments

Comments
 (0)