@Override public String toString() { return MoreObjects.toStringHelper(this) .add("revision", revision()) .add("type", type()) .add("content", contentAsText()) .add("paths", paths()) .toString(); } }
/** * Returns a newly-created {@link MergedEntry}. * * @param revision the revision of the {@link MergedEntry} * @param type the type of the {@link MergedEntry} * @param content the content of the {@link MergedEntry} * @param <T> the content type. It is {@link JsonNode} because only JSON merge is currently supported. * @param paths the paths which participated to compose the {@link MergedEntry} */ public static <T> MergedEntry<T> of(Revision revision, EntryType type, T content, Iterable<String> paths) { return new MergedEntry<>(revision, type, content, paths); }
future.complete(MergedEntry.of(revision, EntryType.JSON, result, pathsBuilder.build())); return null; });
@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; }
@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()); }
@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; }
@Override public String toString() { return MoreObjects.toStringHelper(this) .add("revision", revision()) .add("type", type()) .add("content", contentAsText()) .add("paths", paths()) .toString(); } }
future.complete(MergedEntry.of(revision, EntryType.JSON, result, pathsBuilder.build())); return null; });
/** * Returns a newly-created {@link MergedEntry}. * * @param revision the revision of the {@link MergedEntry} * @param type the type of the {@link MergedEntry} * @param content the content of the {@link MergedEntry} * @param <T> the content type. It is {@link JsonNode} because only JSON merge is currently supported. * @param paths the paths which participated to compose the {@link MergedEntry} */ public static <T> MergedEntry<T> of(Revision revision, EntryType type, T content, Iterable<String> paths) { return new MergedEntry<>(revision, type, content, paths); }
@Override public <T> CompletableFuture<MergedEntry<T>> mergeFiles(String projectName, String repositoryName, Revision revision, MergeQuery<T> mergeQuery) { final CompletableFuture<com.linecorp.centraldogma.internal.thrift.MergedEntry> future = run(callback -> { client.mergeFiles(projectName, repositoryName, RevisionConverter.TO_DATA.convert(revision), MergeQueryConverter.TO_DATA.convert(mergeQuery), callback); }); return future.thenApply(entry -> { final EntryType entryType = EntryConverter.convertEntryType(entry.getType()); assert entryType != null; switch (entryType) { case JSON: try { @SuppressWarnings("unchecked") final MergedEntry<T> converted = (MergedEntry<T>) MergedEntry.of( RevisionConverter.TO_MODEL.convert(entry.revision), entryType, Jackson.readTree(entry.content), entry.paths); return converted; } catch (IOException e) { throw new CompletionException( "failed to parse the content: " + entry.content, e); } default: throw new Error("unsupported entry type: " + entryType); } }); }
/** * Returns a newly-created {@link MergedEntry}. * * @param revision the revision of the {@link MergedEntry} * @param type the type of the {@link MergedEntry} * @param content the content of the {@link MergedEntry} * @param <T> the content type. It is {@link JsonNode} because only JSON merge is currently supported. * @param paths the paths which participated to compose the {@link MergedEntry} */ public static <T> MergedEntry<T> of(Revision revision, EntryType type, T content, String... paths) { return new MergedEntry<>(revision, type, content, ImmutableList.copyOf(requireNonNull(paths, "paths"))); }
@Override public <T> CompletableFuture<MergedEntry<T>> mergeFiles(String projectName, String repositoryName, Revision revision, MergeQuery<T> mergeQuery) { final CompletableFuture<com.linecorp.centraldogma.internal.thrift.MergedEntry> future = run(callback -> { client.mergeFiles(projectName, repositoryName, RevisionConverter.TO_DATA.convert(revision), MergeQueryConverter.TO_DATA.convert(mergeQuery), callback); }); return future.thenApply(entry -> { final EntryType entryType = EntryConverter.convertEntryType(entry.getType()); assert entryType != null; switch (entryType) { case JSON: try { @SuppressWarnings("unchecked") final MergedEntry<T> converted = (MergedEntry<T>) MergedEntry.of( RevisionConverter.TO_MODEL.convert(entry.revision), entryType, Jackson.readTree(entry.content), entry.paths); return converted; } catch (IOException e) { throw new CompletionException( "failed to parse the content: " + entry.content, e); } default: throw new Error("unsupported entry type: " + entryType); } }); }
/** * Returns a newly-created {@link MergedEntry}. * * @param revision the revision of the {@link MergedEntry} * @param type the type of the {@link MergedEntry} * @param content the content of the {@link MergedEntry} * @param <T> the content type. It is {@link JsonNode} because only JSON merge is currently supported. * @param paths the paths which participated to compose the {@link MergedEntry} */ public static <T> MergedEntry<T> of(Revision revision, EntryType type, T content, String... paths) { return new MergedEntry<>(revision, type, content, ImmutableList.copyOf(requireNonNull(paths, "paths"))); }
@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); }