Skip to content

Commit

Permalink
#320 Front and StickyFutures
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Apr 8, 2018
1 parent 4aed283 commit 45c88e5
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 31 deletions.
16 changes: 10 additions & 6 deletions research/2018-April/05-draw-graphs/draw.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
mutable = []
File.open(metrics).read.each_line.map do |l|
cohesion, flag, size = l.split(',', 3)
hash = { cohesion: cohesion.to_f, size: size.to_i }
hash = { cohesion: cohesion.to_f.round(2), size: size.to_i }
if flag == 'true'
immutable << hash
else
Expand All @@ -15,12 +15,16 @@
end

def draw(array)
unique = array.uniq { |c| "#{c[:cohesion]}/#{c[:size]}" }
xavg = unique.map { |c| c[:size] }.inject(:+) / unique.size
yavg = unique.map { |c| c[:cohesion] }.inject(:+) / unique.size
txt = '\begin{tikzpicture}' +
'\begin{axis}[axis lines=middle, xlabel={size}, ylabel={cohesion}, x post scale=1.2,' +
"xmin=#{array.map{ |c| c[:size] }.min}," +
"xmax=#{array.map{ |c| c[:size] }.max}," +
'ymin=0, ymax=1]\addplot [only marks] table {' + "\n"
array.each do |c|
'\begin{axis}[width=12cm,height=6cm,' +
'axis lines=middle, xlabel={$S_i$}, ylabel={$C_i$},' +
'xmin=0, xmax=100, ymin=0, ymax=1,' +
"extra x ticks={#{xavg}},extra y ticks={#{yavg}},extra tick style={major grid style=black}," +
'grid=major]\addplot [only marks, mark size=1pt] table {' + "\n"
unique.each do |c|
txt += "#{c[:size]} #{c[:cohesion]}\n"
end
txt + '};\end{axis}\end{tikzpicture}'
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/jpeek/web/AsyncReports.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ final class AsyncReports implements
/**
* Cache.
*/
private final BiFunc<String, String, Future<Func<String, Response>>> cache;
private final BiFunc<String, String, Future<Front>> cache;

/**
* Starts.
Expand All @@ -65,15 +65,15 @@ final class AsyncReports implements
* @param func Original bi-function
*/
AsyncReports(
final BiFunc<String, String, Future<Func<String, Response>>> func) {
final BiFunc<String, String, Future<Front>> func) {
this.cache = func;
this.starts = new ConcurrentHashMap<>(0);
}

@Override
public Func<String, Response> apply(final String group,
final String artifact) throws IOException {
final Future<Func<String, Response>> future =
final Future<Front> future =
new IoCheckedBiFunc<>(this.cache).apply(group, artifact);
final Func<String, Response> output;
if (future.isDone()) {
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/org/jpeek/web/Front.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017-2018 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.jpeek.web;

import org.cactoos.Func;
import org.takes.Response;

/**
* Web front.
*
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @since 0.8
*/
interface Front extends Func<String, Response> {
}
12 changes: 5 additions & 7 deletions src/main/java/org/jpeek/web/Futures.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,10 @@
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.cactoos.BiFunc;
import org.cactoos.Func;
import org.cactoos.Text;
import org.cactoos.collection.Mapped;
import org.cactoos.scalar.AvgOf;
import org.cactoos.text.JoinedText;
import org.takes.Response;

/**
* Futures for {@link AsyncReports}.
Expand All @@ -53,12 +51,12 @@
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
*/
final class Futures implements
BiFunc<String, String, Future<Func<String, Response>>>, Text {
BiFunc<String, String, Future<Front>>, Text {

/**
* Original func.
*/
private final BiFunc<String, String, Func<String, Response>> origin;
private final BiFunc<String, String, Front> origin;

/**
* Service.
Expand All @@ -79,7 +77,7 @@ final class Futures implements
* Ctor.
* @param func Original bi-function
*/
Futures(final BiFunc<String, String, Func<String, Response>> func) {
Futures(final BiFunc<String, String, Front> func) {
this.origin = func;
this.service = Executors.newFixedThreadPool(
Runtime.getRuntime().availableProcessors(),
Expand All @@ -90,7 +88,7 @@ final class Futures implements
}

@Override
public Future<Func<String, Response>> apply(final String group,
public Future<Front> apply(final String group,
final String artifact) {
final String target = String.format("%s:%s", group, artifact);
this.queue.put(target, System.currentTimeMillis());
Expand All @@ -101,7 +99,7 @@ public Future<Func<String, Response>> apply(final String group,
return this.service.submit(
new VerboseCallable<>(
() -> {
final Func<String, Response> func =
final Front func =
this.origin.apply(group, artifact);
this.times.add(
System.currentTimeMillis() - this.queue.remove(target)
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/jpeek/web/Pages.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import java.io.IOException;
import java.nio.file.Path;
import org.cactoos.Func;
import org.cactoos.text.TextOf;
import org.takes.Response;
import org.takes.rs.RsWithBody;
Expand All @@ -40,7 +39,7 @@
* @since 0.8
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
*/
final class Pages implements Func<String, Response> {
final class Pages implements Front {

/**
* Directory with files.
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/org/jpeek/web/Reports.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@
import java.nio.file.Files;
import java.nio.file.Path;
import org.cactoos.BiFunc;
import org.cactoos.Func;
import org.cactoos.io.LengthOf;
import org.cactoos.io.TeeInput;
import org.cactoos.scalar.IoCheckedScalar;
import org.cactoos.text.TextOf;
import org.jpeek.App;
import org.takes.Response;

/**
* All reports.
Expand All @@ -48,7 +46,7 @@
* @since 0.7
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
*/
final class Reports implements BiFunc<String, String, Func<String, Response>> {
final class Reports implements BiFunc<String, String, Front> {

/**
* Directory with sources.
Expand Down Expand Up @@ -80,7 +78,7 @@ final class Reports implements BiFunc<String, String, Func<String, Response>> {

// @checkstyle ExecutableStatementCountCheck (100 lines)
@Override
public Func<String, Response> apply(final String group,
public Front apply(final String group,
final String artifact) throws IOException {
final String grp = group.replace(".", "/");
final Path input = this.sources.resolve(grp).resolve(artifact);
Expand Down
86 changes: 86 additions & 0 deletions src/main/java/org/jpeek/web/StickyFutures.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017-2018 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.jpeek.web;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Future;
import org.cactoos.BiFunc;

/**
* Futures for {@link AsyncReports}.
*
* <p>There is no thread-safety guarantee.
*
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @since 0.8
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
*/
final class StickyFutures implements BiFunc<String, String, Future<Front>> {

/**
* Original func.
*/
private final BiFunc<String, String, Future<Front>> origin;

/**
* Cache.
*/
private final Map<String, Future<Front>> cache;

/**
* Max size.
*/
private final int max;

/**
* Ctor.
* @param func Original bi-function
* @param size Max size of cache before full clean up
*/
StickyFutures(final BiFunc<String, String, Future<Front>> func,
final int size) {
this.origin = func;
this.cache = new ConcurrentHashMap<>(0);
this.max = size;
}

@Override
public Future<Front> apply(final String group, final String artifact)
throws Exception {
synchronized (this.cache) {
if (this.cache.size() > this.max) {
this.cache.clear();
}
final String target = String.format("%s:%s", group, artifact);
if (!this.cache.containsKey(target)
|| this.cache.get(target).isCancelled()) {
this.cache.put(target, this.origin.apply(group, artifact));
}
return this.cache.get(target);
}
}

}
3 changes: 1 addition & 2 deletions src/main/java/org/jpeek/web/TkApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.nio.file.Files;
import java.nio.file.Path;
import org.cactoos.BiFunc;
import org.cactoos.func.SolidBiFunc;
import org.cactoos.io.ResourceOf;
import org.cactoos.iterable.PropertiesOf;
import org.cactoos.text.TextOf;
Expand Down Expand Up @@ -140,7 +139,7 @@ private static Take make(final Path home) throws IOException {
new AsyncReports(
new BiFunc.NoNulls<>(
// @checkstyle MagicNumber (1 line)
new SolidBiFunc<>(futures, 100)
new StickyFutures(futures, 100)
)
)
)
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/jpeek/web/TkMistakes.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.takes.Take;
import org.takes.rs.xe.XeAppend;
import org.takes.rs.xe.XeDirectives;
import org.xembly.Directive;

/**
* Mistakes page.
Expand All @@ -53,7 +54,7 @@ public Response act(final Request req) {
"worst",
new XeDirectives(
new Joined<>(
new Limited<>(
new Limited<Iterable<Directive>>(
// @checkstyle MagicNumber (1 line)
20, new Mistakes().worst()
)
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/jpeek/web/TkReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ public Response act(final RqRegex req) throws IOException {
}
return new IoCheckedFunc<>(
new IoCheckedBiFunc<>(this.reports).apply(
matcher.group(1),
matcher.group(2)
matcher.group(1), matcher.group(2)
)
).apply(path.substring(1));
}
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/org/jpeek/web/TypedPages.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
package org.jpeek.web;

import java.io.IOException;
import org.cactoos.Func;
import org.cactoos.func.IoCheckedFunc;
import org.takes.Response;
import org.takes.rs.RsWithType;
Expand All @@ -39,18 +38,18 @@
* @since 0.8
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
*/
final class TypedPages implements Func<String, Response> {
final class TypedPages implements Front {

/**
* Origin.
*/
private final Func<String, Response> origin;
private final Front origin;

/**
* Ctor.
* @param func The func
*/
TypedPages(final Func<String, Response> func) {
TypedPages(final Front func) {
this.origin = func;
}

Expand Down

0 comments on commit 45c88e5

Please sign in to comment.