Skip to content

Commit 8706632

Browse files
committed
CNDB-13031: change Descriptor#fileFor to use the same file system as Descriptor#directory
1 parent 4a6a668 commit 8706632

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

src/java/org/apache/cassandra/io/sstable/Component.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,35 @@ public static Component parse(String name)
161161
}
162162
}
163163

164+
/**
165+
* @return file that uses the resolved file system as provided absolutePath URI
166+
*/
164167
public File getFile(String absolutePath)
165168
{
169+
// new file will have the default file open options from file system
166170
File ret;
167171
if (absolutePath.lastIndexOf(separator) != (absolutePath.length() - 1))
168172
ret = new File(PathUtils.getPath(absolutePath + separator + name));
169173
else
170174
ret = new File(PathUtils.getPath(absolutePath + name));
171175

176+
// update open options
177+
return StorageProvider.instance.withOpenOptions(ret, this);
178+
}
179+
180+
/**
181+
* @return file that uses the same file system as provided directory and resolved from provided directory
182+
*/
183+
public File getFile(File directory, String filenamePart)
184+
{
185+
// new file will have the same file open options as provided directory
186+
File ret;
187+
if (filenamePart.lastIndexOf(separator) != (filenamePart.length() - 1))
188+
ret = directory.resolve(filenamePart + separator + name);
189+
else
190+
ret = directory.resolve(filenamePart + name);
191+
192+
// update open options
172193
return StorageProvider.instance.withOpenOptions(ret, this);
173194
}
174195

src/java/org/apache/cassandra/io/sstable/Descriptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public String filenameFor(Component component)
150150

151151
public File fileFor(Component component)
152152
{
153-
return componentFileMap.computeIfAbsent(component, c -> component.getFile(baseFileURI));
153+
return componentFileMap.computeIfAbsent(component, c -> component.getFile(directory, filenamePart));
154154
}
155155

156156
public String baseFilename()

test/unit/org/apache/cassandra/io/sstable/DescriptorTest.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818
package org.apache.cassandra.io.sstable;
1919

20-
import java.nio.file.Path;
20+
import java.net.URI;
2121
import java.util.UUID;
2222

2323
import org.apache.commons.lang3.StringUtils;
@@ -31,8 +31,8 @@
3131
import org.apache.cassandra.io.util.File;
3232
import org.apache.cassandra.io.util.FileUtils;
3333
import org.apache.cassandra.utils.ByteBufferUtil;
34-
import org.apache.cassandra.utils.DseLegacy;
3534
import org.apache.cassandra.utils.Pair;
35+
import org.mockito.Mockito;
3636

3737
import static org.junit.Assert.assertEquals;
3838
import static org.junit.Assert.assertFalse;
@@ -114,6 +114,29 @@ private void checkFromFilename(Descriptor original)
114114
assertEquals(Component.DATA, Descriptor.validFilenameWithComponent(file.name()));
115115
}
116116

117+
@Test
118+
public void testFromFileWithResolveByDirectory() throws Exception
119+
{
120+
String rawUri = tempDataDir.absolutePath() + File.pathSeparator() + ksname + File.pathSeparator() + cfname + '-' + cfId;
121+
122+
File cfDirectory = Mockito.mock(File.class);
123+
Mockito.when(cfDirectory.toCanonical()).thenReturn(cfDirectory);
124+
Mockito.when(cfDirectory.toUri()).thenReturn(URI.create(rawUri));
125+
126+
File componentFile = Mockito.mock(File.class);
127+
Mockito.when(cfDirectory.resolve(Mockito.anyString())).thenReturn(componentFile);
128+
129+
Descriptor desc = new Descriptor(cfDirectory, ksname, cfname, new SequenceBasedSSTableId(1), SSTableFormat.Type.BIG);
130+
assertEquals(cfDirectory.toCanonical(), desc.directory);
131+
assertEquals(ksname, desc.ksname);
132+
assertEquals(cfname, desc.cfname);
133+
assertEquals(new SequenceBasedSSTableId(1), desc.id);
134+
135+
// verify data component is resolved from given directory
136+
File dataComponent = desc.fileFor(Component.DATA);
137+
assertEquals(dataComponent, componentFile);
138+
}
139+
117140
@Test
118141
public void testEquality()
119142
{

0 commit comments

Comments
 (0)