Skip to content

Commit

Permalink
Merge pull request #241 from NDLANO/10-chars-in-context
Browse files Browse the repository at this point in the history
10 chars in context
  • Loading branch information
gunnarvelle authored Mar 18, 2024
2 parents d0dfe70 + b7979fe commit 80a4cbc
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private Set<Context> createContexts(Node node) {
.flatMap(relevance -> Optional.of(
relevance.getPublicId().toString()))
.orElse("urn:relevance:core"),
HashUtil.semiHash(parentContext.rootId() + parentConnection.getPublicId()),
HashUtil.mediumHash(parentContext.contextId() + parentConnection.getPublicId()),
parentConnection.getRank(),
parentConnection.getPublicId().toString());
})
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/no/ndla/taxonomy/service/NodeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -515,11 +515,7 @@ public List<TaxonomyContextDTO> getContextByPath(Optional<String> path, String l
if (path.isPresent()) {
String contextId = TitleUtil.getHashFromPath(path.get());
List<Integer> ids = nodeRepository.findIdsByContextId(Optional.of(contextId));
var nodes = ids.stream()
.map(nodeRepository::findById)
.filter(Optional::isPresent)
.map(Optional::get)
.toList();
var nodes = nodeRepository.findByIds(ids);
var contexts = nodesToContexts(nodes, false, language);
return contexts.stream()
.filter(c -> c.contextId().equals(contextId))
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/no/ndla/taxonomy/util/HashUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static String shortHash(Object original) {
}

public static String mediumHash(Object original) {
return generateHash(original, 8);
return generateHash(original, 10);
}

public static String semiHash(Object original) {
Expand Down
13 changes: 4 additions & 9 deletions src/test/java/no/ndla/taxonomy/rest/v1/QueryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import no.ndla.taxonomy.rest.v1.dtos.searchapi.LanguageFieldDTO;
import no.ndla.taxonomy.rest.v1.dtos.searchapi.TaxonomyContextDTO;
import no.ndla.taxonomy.service.dtos.NodeDTO;
import no.ndla.taxonomy.util.HashUtil;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.mock.web.MockHttpServletResponse;
Expand Down Expand Up @@ -402,14 +401,10 @@ public void that_contexts_can_be_found_by_path() throws Exception {
.contentUri("urn:article:1")
.publicId("urn:resource:1"))));
Node resource = nodeRepository.getByPublicId(URI.create("urn:resource:1"));
String hash = HashUtil.semiHash(root.getPublicId().toString()
+ resource.getParentConnections().stream()
.findFirst()
.get()
.getPublicId()
.toString());

var response = testUtils.getResource("/v1/queries/path?path=" + String.format("/one-fine-resource__%s", hash));
var response = testUtils.getResource("/v1/queries/path?path="
+ String.format(
"/one-fine-resource__%s",
resource.getContexts().stream().findFirst().get().contextId()));
var result = testUtils.getObject(TaxonomyContextDTO[].class, response);

assertEquals(1, result.length);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/no/ndla/taxonomy/util/HashUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void get_different_length_hashes() {
String fullHash = HashUtil.fullHash("original");

assertEquals(4, shortHash.length());
assertEquals(8, mediumHash.length());
assertEquals(10, mediumHash.length());
assertEquals(12, semiHash.length());
assertEquals(16, longHash.length());
assertTrue(fullHash.length() > 16);
Expand Down

0 comments on commit 80a4cbc

Please sign in to comment.