Skip to content

Commit

Permalink
Merge pull request #206 from eclipse-passage/560445
Browse files Browse the repository at this point in the history
Bug 560445 - [YARS] support incremental monitoring
  • Loading branch information
eparovyshnaya authored May 6, 2020
2 parents 937a26c + 13449db commit 6e02370
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,23 @@ public SingleSwoopExport(FetchedData<S, T> query) {
@Override
public void write(DosHandleMedia<T> media, Progress<T> progress) {
media.start();
writeData(media, progress);
media.finish();
}

private void writeData(DosHandleMedia<T> media, Progress<T> progress) {
List<T> fetch = query.get();
progress.estimate(fetch.size());

fetch.forEach(data -> {
for (T data : fetch) {
if (progress.cancelDemanded()) {
break;
}
progress.reportNodeSrart(data);
media.startNode(data);
data.write(media, progress);
media.finishNode(data);
progress.reportNodeFinish(data);
});
media.finish();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.eclipse.passage.loc.yars.internal.api.model.InMemoryStorage;

@SuppressWarnings("restriction")
public class All implements Query<InMemoryStorage, ExportedEntry, FetchParams> {
final class All implements Query<InMemoryStorage, ExportEntry, FetchParams> {

@Override
public String id() {
Expand All @@ -31,7 +31,7 @@ public String description() {
}

@Override
public FetchedData<InMemoryStorage, ExportedEntry> fetch(InMemoryStorage base, FetchParams params) {
public FetchedData<InMemoryStorage, ExportEntry> fetch(InMemoryStorage base, FetchParams params) {
return new Fetch(base);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*******************************************************************************
* Copyright (c) 2020 ArSysOp
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0/.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.loc.yars.internal.api.export;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.passage.loc.yars.internal.api.Progress;
import org.eclipse.passage.loc.yars.internal.api.model.InMemoryStorage;

@SuppressWarnings("restriction")
final class CountingProgress implements Progress<ExportEntry> {

private final InMemoryStorage storage;
private final int limit;
private int estimated = 0;
private int started = 0;
private int finished = 0;
private List<String> infos = new ArrayList<>();

CountingProgress(InMemoryStorage storage, int limit) {
this.storage = storage;
this.limit = limit;
}

@Override
public void estimate(int amount) {
estimated = amount;
}

@Override
public void reportNodeSrart(ExportEntry entry) {
started++;
}

@Override
public void reportNodeFinish(ExportEntry entry) {
finished++;
}

@Override
public void report(String info) {
this.infos.add(info);
}

@Override
public boolean cancelDemanded() {
return finished >= limit;
}

void assertContractIsOk() {
assertTrue(started == finished);
assertTrue(started <= estimated);
}

void assertStateIsOk() {
assertEquals(storage.size(), estimated);
assertTrue(started == limit);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.eclipse.passage.loc.yars.internal.api.ListMedia;

@SuppressWarnings("restriction")
class Csv implements ListMedia<ExportedEntry> {
final class Csv implements ListMedia<ExportEntry> {

private final StringBuilder builder;
private final List<String> header;
Expand All @@ -35,7 +35,7 @@ public final void start() {
}

@Override
public final void startNode(ExportedEntry node) {
public final void startNode(ExportEntry node) {
builder.append("\n"); //$NON-NLS-1$
}

Expand All @@ -49,7 +49,7 @@ public final void finish() {
}

@Override
public final void finishNode(ExportedEntry node) {
public final void finishNode(ExportEntry node) {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.eclipse.passage.loc.yars.internal.api.ListMedia;

@SuppressWarnings("restriction")
public class Enlistment<T> implements ListMedia<T> {
final class Enlistment<T> implements ListMedia<T> {

private final List<T> content;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@
import org.eclipse.passage.loc.yars.internal.api.Progress;

@SuppressWarnings("restriction")
public class ExportedEntry implements ExportData<ExportedEntry, DosHandleMedia<ExportedEntry>> {
final class ExportEntry implements ExportData<ExportEntry, DosHandleMedia<ExportEntry>> {

private String name;

public ExportedEntry(String name) {
public ExportEntry(String name) {
this.name = name;
}

@Override
public void write(DosHandleMedia<ExportedEntry> media, Progress<ExportedEntry> p) {
public void write(DosHandleMedia<ExportEntry> media, Progress<ExportEntry> progress) {
media.inner(name, "name"); //$NON-NLS-1$
progress.report(name);
}

@Override // generated
Expand All @@ -38,7 +39,7 @@ public boolean equals(Object o) {
return true;
if (o == null || getClass() != o.getClass())
return false;
ExportedEntry exportedEntry = (ExportedEntry) o;
ExportEntry exportedEntry = (ExportEntry) o;
return Objects.equals(name, exportedEntry.name);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
* <li>We define a {@linkplain Query} - {@linkplain All} - which fetches all
* entries from our storage. It also emulates a <i>business logic</i> that is
* implemented as a conversion stored entries to entities of another type
* ({@linkplain ExportedEntry})<i></li>
* ({@linkplain ExportEntry})<i></li>
* <li>When we as our <i>query</i> for {@linkplain Query#data()}, it actually
* does not interacts with the {@code storage}, but only instantiate a dedicated
* {@linkplain FetchedData} instance.</li>
Expand All @@ -63,7 +63,7 @@
* runtime list. All of them are unaware of storing and fetching details.</li>
*/
@SuppressWarnings("restriction")
public class ExportTest {
public final class ExportTest {

@Test
public void testCsv() {
Expand All @@ -75,12 +75,12 @@ public void testCsv() {

@Test
public void testEnlistment() {
List<ExportedEntry> output = new ArrayList<>();
queryResult(new Enlistment<ExportedEntry>(output));
List<ExportEntry> output = new ArrayList<>();
queryResult(new Enlistment<ExportEntry>(output));
assertEquals(Arrays.asList( //
new ExportedEntry("Gammy"), //$NON-NLS-1$
new ExportedEntry("Quami"), //$NON-NLS-1$
new ExportedEntry("Tsunami")), //$NON-NLS-1$
new ExportEntry("Gammy"), //$NON-NLS-1$
new ExportEntry("Quami"), //$NON-NLS-1$
new ExportEntry("Tsunami")), //$NON-NLS-1$
output);
}

Expand All @@ -102,19 +102,19 @@ public void testJson() {
output.toString());
}

private void queryResult(ListMedia<ExportedEntry> media) {
new SingleSwoopExport<InMemoryStorage, ExportedEntry>(new All().fetch(//
private void queryResult(ListMedia<ExportEntry> media) {
new SingleSwoopExport<InMemoryStorage, ExportEntry>(new All().fetch(//
new InMemoryStorage( //
new StoredEntry("Gammy", "US"), //$NON-NLS-1$ //$NON-NLS-2$
new StoredEntry("Quami", "France"), //$NON-NLS-1$ //$NON-NLS-2$
new StoredEntry("Tsunami", "Japan")//$NON-NLS-1$ //$NON-NLS-2$
), //
new FetchParams.Empty()))//
.write(//
new DosHandleMedia<ExportedEntry>(//
new DosHandleMedia<ExportEntry>(//
media, //
new DefaultDosHandler()), //
new Progress.Inane<ExportedEntry>());
new Progress.Inane<ExportEntry>());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import org.eclipse.passage.loc.yars.internal.api.model.InMemoryStorage;

@SuppressWarnings("restriction")
class Fetch implements FetchedData<InMemoryStorage, ExportedEntry> {
final class Fetch implements FetchedData<InMemoryStorage, ExportEntry> {

private final InMemoryStorage source;

Expand All @@ -28,9 +28,9 @@ class Fetch implements FetchedData<InMemoryStorage, ExportedEntry> {
}

@Override
public List<ExportedEntry> get() {
public List<ExportEntry> get() {
return source.entries().stream()//
.map(dummy -> new ExportedEntry(dummy.name()))//
.map(dummy -> new ExportEntry(dummy.name()))//
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.eclipse.passage.loc.yars.internal.api.ListMedia;

@SuppressWarnings("restriction")
class Json implements ListMedia<ExportedEntry> {
final class Json implements ListMedia<ExportEntry> {

private final StringBuilder builder;

Expand All @@ -35,12 +35,12 @@ public final void finish() {
}

@Override
public final void startNode(ExportedEntry node) {
public final void startNode(ExportEntry node) {
builder.append("\t\"node\" : {\n"); //$NON-NLS-1$
}

@Override
public final void finishNode(ExportedEntry node) {
public final void finishNode(ExportEntry node) {
builder.append("\t}\n"); //$NON-NLS-1$
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*******************************************************************************
* Copyright (c) 2020 ArSysOp
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0/.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.loc.yars.internal.api.export;

import java.util.ArrayList;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.eclipse.passage.loc.yars.internal.api.DefaultDosHandler;
import org.eclipse.passage.loc.yars.internal.api.DosHandleMedia;
import org.eclipse.passage.loc.yars.internal.api.FetchParams;
import org.eclipse.passage.loc.yars.internal.api.ListMedia;
import org.eclipse.passage.loc.yars.internal.api.Progress;
import org.eclipse.passage.loc.yars.internal.api.SingleSwoopExport;
import org.eclipse.passage.loc.yars.internal.api.model.InMemoryStorage;
import org.eclipse.passage.loc.yars.internal.api.model.StoredEntry;
import org.junit.Test;

@SuppressWarnings("restriction")
public final class ProgressTest {

@Test
public void ticks() {
exportWithProgress(InMemoryStorage::size);
}

@Test
public void cancells() {
exportWithProgress(storage -> storage.size() / 3);
}

private void exportWithProgress(Function<InMemoryStorage, Integer> limit) {
InMemoryStorage storage = storage();
CountingProgress progress = new CountingProgress(storage, limit.apply(storage));
queryResult(new Enlistment<ExportEntry>(new ArrayList<>()), progress);
progress.assertContractIsOk();
progress.assertStateIsOk();
}

private InMemoryStorage storage() {
return new InMemoryStorage( //
IntStream.range(1, 16) //
.mapToObj(i -> new StoredEntry(Integer.toString(i), Integer.toBinaryString(i))) //
.collect(Collectors.toList()) //
);
}

private void queryResult(ListMedia<ExportEntry> media, Progress<ExportEntry> progress) {
InMemoryStorage storage = new InMemoryStorage( //
IntStream.range(1, 16) //
.mapToObj(i -> new StoredEntry(Integer.toString(i), Integer.toBinaryString(i))) //
.collect(Collectors.toList()) //
);
new SingleSwoopExport<InMemoryStorage, ExportEntry>(new All().fetch(//
storage, //
new FetchParams.Empty()))//
.write(//
new DosHandleMedia<ExportEntry>(//
media, //
new DefaultDosHandler()), //
progress);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class InMemoryStorage implements Storage<StoredEntry> {

private final List<StoredEntry> dummies;

InMemoryStorage(List<StoredEntry> dummies) {
public InMemoryStorage(List<StoredEntry> dummies) {
this.dummies = dummies;
}

Expand All @@ -34,4 +34,8 @@ public List<StoredEntry> entries() {
return dummies;
}

public int size() {
return dummies.size();
}

}

0 comments on commit 6e02370

Please sign in to comment.