/** * Returns a newly-created {@link MergeSource} which contains an optional path. */ public static MergeSource ofOptional(String path) { return new MergeSource(path, true); }
private static List<com.linecorp.centraldogma.common.MergeSource> convertMergeSources( MergeQuery mergeQuery) { return mergeQuery.mergeSources .stream() .map(mergeSource -> { if (mergeSource.isIsOptional()) { return com.linecorp.centraldogma.common.MergeSource.ofOptional(mergeSource.getPath()); } return com.linecorp.centraldogma.common.MergeSource.ofRequired(mergeSource.getPath()); }) .collect(toImmutableList()); }
private static ImmutableList<MergeSource> convertMergeSources( com.linecorp.centraldogma.common.MergeQuery<?> mergeQuery) { return mergeQuery.mergeSources().stream() .map(mergeSource -> new MergeSource( mergeSource.path(), mergeSource.isOptional())) .collect(toImmutableList()); }
@Test @SuppressWarnings("unchecked") public void mergeQuery() throws JsonParseException { final Repository repo = setMockNames(newCachingRepo()); final MergeQuery<JsonNode> query = MergeQuery.ofJson(MergeSource.ofRequired("/foo.json"), MergeSource.ofRequired("/bar.json")); final MergedEntry<JsonNode> queryResult = MergedEntry.of(new Revision(10), JSON, Jackson.readTree("{\"a\": \"bar\"}"), "/foo.json", "/bar.json"); doReturn(new Revision(10)).when(delegateRepo).normalizeNow(new Revision(10)); doReturn(new Revision(10)).when(delegateRepo).normalizeNow(HEAD); // Uncached when(delegateRepo.find(any(), any(), any())) .thenReturn(completedFuture(ImmutableMap.of("/foo.json", Entry.ofJson( new Revision(10), "/foo.json", "{\"a\": \"foo\"}")))) .thenReturn(completedFuture(ImmutableMap.of("/bar.json", Entry.ofJson( new Revision(10), "/bar.json", "{\"a\": \"bar\"}")))); assertThat(repo.mergeFiles(HEAD, query).join()).isEqualTo(queryResult); verify(delegateRepo).find(new Revision(10), "/foo.json", FIND_ONE_WITH_CONTENT); verify(delegateRepo).find(new Revision(10), "/bar.json", FIND_ONE_WITH_CONTENT); verifyNoMoreInteractions(delegateRepo); // Cached clearInvocations(delegateRepo); assertThat(repo.mergeFiles(HEAD, query).join()).isEqualTo(queryResult); assertThat(repo.mergeFiles(new Revision(10), query).join()).isEqualTo(queryResult); verify(delegateRepo, never()).find(any(), any(), any()); verifyNoMoreInteractions(delegateRepo); }
CacheableMergeQueryCall(Repository repo, Revision revision, MergeQuery<?> query) { super(repo); this.revision = requireNonNull(revision, "revision"); this.query = requireNonNull(query, "query"); // Only JSON files can currently be merged. query.mergeSources().forEach(path -> validateJsonFilePath(path.path(), "path")); hashCode = Objects.hash(revision, query) * 31 + System.identityHashCode(repo); assert !revision.isRelative(); }
final String value = query.substring(index + 1); if ("path".equals(key)) { mergeSourceBuilder.add(MergeSource.ofRequired(value)); } else if ("optional_path".equals(key)) { mergeSourceBuilder.add(MergeSource.ofOptional(value)); } else if ("jsonpath".equals(key)) { jsonPathsBuilder.add(value);
mergeSources.forEach(path -> validateJsonFilePath(path.path(), "path")); if (!path.isOptional()) { entryFutures.add(get(normalizedRevision, path.path())); } else { entryFutures.add(getOrNull(normalizedRevision, path.path()));
CacheableMergeQueryCall(Repository repo, Revision revision, MergeQuery<?> query) { super(repo); this.revision = requireNonNull(revision, "revision"); this.query = requireNonNull(query, "query"); // Only JSON files can currently be merged. query.mergeSources().forEach(path -> validateJsonFilePath(path.path(), "path")); hashCode = Objects.hash(revision, query) * 31 + System.identityHashCode(repo); assert !revision.isRelative(); }
@Test public void convert() throws Exception { final ServiceRequestContext ctx = mock(ServiceRequestContext.class); final String queryString = "path=/foo.json" + '&' + "pa%22th=/foo1.json" + '&' + "optional_path=/foo2.json" + '&' + "path=/foo3.json" + '&' + "jsonpath=$.a" + '&' + "revision=9999"; when(ctx.query()).thenReturn(queryString); @SuppressWarnings("unchecked") final MergeQuery<JsonNode> mergeQuery = (MergeQuery<JsonNode>) converter.convertRequest( ctx, mock(AggregatedHttpMessage.class), null); assertThat(mergeQuery).isEqualTo( MergeQuery.ofJsonPath( ImmutableList.of(MergeSource.ofRequired("/foo.json"), MergeSource.ofOptional("/foo2.json"), MergeSource.ofRequired("/foo3.json")), ImmutableList.of("$.a"))); } }
mergeSources.forEach(path -> validateJsonFilePath(path.path(), "path")); if (!path.isOptional()) { entryFutures.add(get(normalizedRevision, path.path())); } else { entryFutures.add(getOrNull(normalizedRevision, path.path()));
@Override int weigh(MergedEntry<?> value) { int weight = 0; final List<MergeSource> mergeSources = query.mergeSources(); weight += mergeSources.size(); for (MergeSource mergeSource : mergeSources) { weight += mergeSource.path().length(); } final List<String> expressions = query.expressions(); weight += expressions.size(); for (String expression : expressions) { weight += expression.length(); } if (value != null) { weight += value.contentAsText().length(); } return weight; }
/** * Returns a newly-created {@link MergeSource} which contains a required path. */ public static MergeSource ofRequired(String path) { return new MergeSource(path, false); }
final String value = query.substring(index + 1); if ("path".equals(key)) { mergeSourceBuilder.add(MergeSource.ofRequired(value)); } else if ("optional_path".equals(key)) { mergeSourceBuilder.add(MergeSource.ofOptional(value)); } else if ("jsonpath".equals(key)) { jsonPathsBuilder.add(value);
@Override int weigh(MergedEntry<?> value) { int weight = 0; final List<MergeSource> mergeSources = query.mergeSources(); weight += mergeSources.size(); for (MergeSource mergeSource : mergeSources) { weight += mergeSource.path().length(); } final List<String> expressions = query.expressions(); weight += expressions.size(); for (String expression : expressions) { weight += expression.length(); } if (value != null) { weight += value.contentAsText().length(); } return weight; }
/** * Returns a newly-created {@link MergeSource} which contains a required path. */ public static MergeSource ofRequired(String path) { return new MergeSource(path, false); }
@Test public void mergeFiles() throws Exception { doAnswer(invocation -> { final AsyncMethodCallback<MergedEntry> callback = invocation.getArgument(4); callback.onComplete(new MergedEntry(new TRevision(1), TEntryType.JSON, "{\"foo\": \"bar\"}", ImmutableList.of("/a.json", "/b.json"))); return null; }).when(iface).mergeFiles(any(), any(), any(), any(), any()); assertThat(client.mergeFiles("project", "repo", new Revision(1), MergeQuery.ofJson(ImmutableList.of(MergeSource.ofOptional("/a.json"), MergeSource.ofRequired("/b.json")))) .get()) .isEqualTo(com.linecorp.centraldogma.common.MergedEntry.of( new Revision(1), EntryType.JSON, Jackson.readTree("{\"foo\": \"bar\"}"), ImmutableList.of("/a.json", "/b.json"))); verify(iface).mergeFiles(eq("project"), eq("repo"), any(), any(), any()); }
/** * Returns a newly-created {@link MergeSource} which contains an optional path. */ public static MergeSource ofOptional(String path) { return new MergeSource(path, true); }