Skip to content

Commit 40fcb8a

Browse files
authored
Add REST api getFilePathsByChecksum (#95)
1 parent b3162d0 commit 40fcb8a

File tree

7 files changed

+79
-2
lines changed

7 files changed

+79
-2
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<properties>
4343
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
4444
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
45-
<path-mapped-storage.version>2.8</path-mapped-storage.version>
45+
<path-mapped-storage.version>2.9-SNAPSHOT</path-mapped-storage.version>
4646
<toolchains-plugin.version>3.0.0</toolchains-plugin.version>
4747
<cassandra-maven-plugin.version>3.8</cassandra-maven-plugin.version>
4848
<quarkus.package.type>uber-jar</quarkus.package.type>

src/main/java/org/commonjava/service/storage/config/StorageServiceConfig.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ public interface StorageServiceConfig
4141
@WithName( "removableFilesystemPattern" )
4242
String removableFilesystemPattern();
4343

44+
@WithName( "deduplicatePattern" )
45+
String deduplicatePattern();
46+
4447
@WithName( "physicalFileExistenceCheck" )
4548
@WithDefault("false")
4649
boolean physicalFileExistenceCheck();

src/main/java/org/commonjava/service/storage/controller/StorageController.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.apache.commons.io.IOUtils;
2121
import org.commonjava.service.storage.dto.*;
2222
import org.commonjava.storage.pathmapped.core.PathMappedFileManager;
23+
import org.commonjava.storage.pathmapped.model.FileChecksum;
2324
import org.commonjava.storage.pathmapped.model.Filesystem;
2425
import org.commonjava.storage.pathmapped.model.PathMap;
2526
import org.commonjava.storage.pathmapped.spi.PathDB;
@@ -40,6 +41,7 @@
4041
import java.util.stream.Collectors;
4142

4243
import static java.util.Collections.emptyList;
44+
import static java.util.Collections.emptySet;
4345
import static org.apache.commons.lang3.StringUtils.isBlank;
4446
import static org.apache.commons.lang3.StringUtils.isNotBlank;
4547
import static org.commonjava.service.storage.util.Utils.getDuration;
@@ -166,6 +168,22 @@ public FileInfoObj getFileInfo(String filesystem, String path )
166168
return null;
167169
}
168170

171+
/**
172+
* Get paths (each includes the filesystem+path) by file's checksum.
173+
* @return The paths set (because each physical file may link to multiple logical files).
174+
*/
175+
public Set<String> getFilePathsByChecksum( String checksum )
176+
{
177+
final PathDB pathDB = fileManager.getPathDB();
178+
FileChecksum c = pathDB.getFileChecksum( checksum );
179+
if ( c != null )
180+
{
181+
String fileId = c.getFileId();
182+
return pathDB.getPathsByFileId( fileId );
183+
}
184+
return emptySet();
185+
}
186+
169187
public BatchCleanupResult cleanup(Set<String> paths, Set<String> filesystems )
170188
{
171189
Set<String> success = new HashSet<>();
@@ -333,5 +351,4 @@ public FileCopyResult copy(FileCopyRequest request)
333351

334352
return new FileCopyResult( true, completed, skipped );
335353
}
336-
337354
}

src/main/java/org/commonjava/service/storage/core/FileManagerProducer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public PathMappedFileManager getFileManager()
6060
config.setPhysicalFileExistenceCheckEnabled( storageConfig.physicalFileExistenceCheck() );
6161
config.setGcBatchSize( storageConfig.gcBatchSize() );
6262
config.setGcIntervalInMinutes( storageConfig.gcIntervalInMinutes() );
63+
config.setDeduplicatePattern( storageConfig.deduplicatePattern() );
6364

6465
PathDB pathDB = new CassandraPathDB( config );
6566
PhysicalStore physicalStore;

src/main/java/org/commonjava/service/storage/jaxrs/StorageResource.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import java.io.InputStream;
4646
import java.util.Collection;
4747
import java.util.List;
48+
import java.util.Set;
4849
import java.util.stream.Collectors;
4950

5051
import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON;
@@ -214,6 +215,20 @@ public Response getFileInfo(
214215
return responseHelper.formatOkResponseWithJsonEntity( result );
215216
}
216217

218+
@Operation( summary = "Get paths (each includes 'filesystem + path') by file's checksum." )
219+
@APIResponses( { @APIResponse( responseCode = "200",
220+
description = "The paths or 'empty set' if not found." ) } )
221+
@Produces( APPLICATION_JSON )
222+
@GET
223+
@Path( "checksum/{checksum}" )
224+
public Response getFilePathsByChecksum(
225+
final @Parameter( in = PATH, required = true ) @PathParam( "checksum" ) String checksum )
226+
{
227+
final Set<String> paths = controller.getFilePathsByChecksum( checksum );
228+
logger.info( "File paths: {}", paths );
229+
return responseHelper.formatOkResponseWithJsonEntity( paths );
230+
}
231+
217232
@Operation( summary = "Copy files." )
218233
@RequestBody( description = "The file copy request", name = "body", required = true,
219234
content = @Content( schema = @Schema( implementation = FileCopyRequest.class ) ) )

src/test/java/org/commonjava/service/storage/StorageResourceIT.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import org.junit.jupiter.api.Test;
2929

3030
import java.io.ByteArrayInputStream;
31+
import java.io.IOException;
32+
import java.io.InputStream;
3133
import java.util.Arrays;
3234
import java.util.HashSet;
3335
import java.util.List;
@@ -113,6 +115,44 @@ public void testGetFileInfo()
113115
assertNotEquals( -1, response.jsonPath().getLong( "fileLength" ) );
114116
}
115117

118+
@Test
119+
public void testGetPathsByChecksum() throws Exception
120+
{
121+
// filesystem must match the 'deduplicatePattern'
122+
final String g_filesystem = "generic-http:remote:g_filesystem";
123+
final String checksum = "209094962790ca300e4e387f4b3dd130ff06ef9a9c3e08c2f96889334db4cf4c";
124+
125+
// upload the file to g_filesystem
126+
try (InputStream in = url.openStream())
127+
{
128+
prepareFile( in, g_filesystem, PATH );
129+
}
130+
131+
// test with right checksum
132+
Response response = given().pathParam( "checksum", checksum )
133+
.when()
134+
.get( API_BASE + "/checksum/{checksum}" )
135+
.then()
136+
.extract()
137+
.response();
138+
139+
assertEquals( 200, response.statusCode() );
140+
//System.out.println(">>>" + response.jsonPath().getList( "" ));
141+
assertTrue( response.jsonPath().getList( "" ).contains( g_filesystem + ":/" + PATH ) );
142+
143+
// test with wrong checksum
144+
response = given().pathParam( "checksum", "wrong-094962790ca30" )
145+
.when()
146+
.get( API_BASE + "/checksum/{checksum}" )
147+
.then()
148+
.extract()
149+
.response();
150+
151+
assertEquals( 200, response.statusCode() );
152+
//System.out.println(">>>" + response.getBody().asString() );
153+
assertTrue( response.jsonPath().getList( "" ).isEmpty() );
154+
}
155+
116156
@Test
117157
public void testList()
118158
{

src/test/resources/application.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@ cassandra:
4242
storage:
4343
baseDir: "/tmp"
4444
readonly: false
45+
deduplicatePattern: "(generic-http|npm).+"
4546
removableFilesystemPattern: ".+:(remote|group):.+"

0 commit comments

Comments
 (0)