Skip to content

Commit

Permalink
Uniform variable name: Iterator iter (#438)
Browse files Browse the repository at this point in the history
Change-Id: Iabf708702778a8ecd5eb19bdcf012c4133edff8f
  • Loading branch information
javeme authored and Linary committed Apr 9, 2019
1 parent 490b0cd commit 727a0e4
Show file tree
Hide file tree
Showing 26 changed files with 255 additions and 219 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,10 @@ public void delete(@Context GraphManager manager,
HugeGraph g = graph(manager, graph);
// TODO: add removeVertex(id) to improve
commit(g, () -> {
Iterator<Vertex> itor = g.vertices(id);
E.checkArgument(itor.hasNext(),
Iterator<Vertex> iter = g.vertices(id);
E.checkArgument(iter.hasNext(),
"No such vertex with id: '%s'", idValue);
itor.next().remove();
iter.next().remove();
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public Map<String, List<Object>> list(@Context GraphManager manager,

TaskScheduler scheduler = graph(manager, graph).taskScheduler();

Iterator<HugeTask<Object>> itor;
Iterator<HugeTask<Object>> iter;

if (!ids.isEmpty()) {
LOG.debug("Graph [{}] list tasks with ids {}, limit {}",
Expand All @@ -90,20 +90,20 @@ public Map<String, List<Object>> list(@Context GraphManager manager,
limit = NO_LIMIT;
List<Id> idList = ids.stream().map(IdGenerator::of)
.collect(Collectors.toList());
itor = scheduler.tasks(idList);
iter = scheduler.tasks(idList);
} else {
LOG.debug("Graph [{}] list tasks with status {}, limit {}",
graph, status, limit);
if (status == null) {
itor = scheduler.findAllTask(limit);
iter = scheduler.findAllTask(limit);
} else {
itor = scheduler.findTask(parseStatus(status), limit);
iter = scheduler.findTask(parseStatus(status), limit);
}
}

List<Object> tasks = new ArrayList<>();
while (itor.hasNext()) {
tasks.add(itor.next().asMap(false));
while (iter.hasNext()) {
tasks.add(iter.next().asMap(false));
}
if (limit != NO_LIMIT && tasks.size() > limit) {
tasks = tasks.subList(0, (int) limit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

package com.baidu.hugegraph.api.traversers;

import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_CAPACITY;
import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_DEGREE;

import java.util.List;

import javax.inject.Singleton;
Expand All @@ -41,12 +44,9 @@
import com.baidu.hugegraph.server.RestServer;
import com.baidu.hugegraph.traversal.algorithm.ShortestPathTraverser;
import com.baidu.hugegraph.type.define.Directions;
import com.baidu.hugegraph.util.E;
import com.baidu.hugegraph.util.Log;
import com.codahale.metrics.annotation.Timed;

import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.*;

@Path("graphs/{graph}/traversers/shortestpath")
@Singleton
public class ShortestPathAPI extends API {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ public class MetricsModule extends Module {
"com.baidu.hugegraph",
"hugegraph-api");

@SuppressWarnings("rawtypes")
private static class GaugeSerializer extends StdSerializer<Gauge> {

private static final long serialVersionUID = -5347786455542725809L;

private GaugeSerializer() {
super(Gauge.class);
}
Expand All @@ -70,6 +73,8 @@ public void serialize(Gauge gauge, JsonGenerator json,

private static class CounterSerializer extends StdSerializer<Counter> {

private static final long serialVersionUID = -209508117719806468L;

private CounterSerializer() {
super(Counter.class);
}
Expand All @@ -85,6 +90,8 @@ public void serialize(Counter counter, JsonGenerator json,

private static class HistogramSerializer extends StdSerializer<Histogram> {

private static final long serialVersionUID = -775852382644934747L;

private final boolean showSamples;

private HistogramSerializer(boolean showSamples) {
Expand All @@ -108,7 +115,7 @@ public void serialize(Histogram histogram, JsonGenerator json,
json.writeNumberField("p99", snapshot.get99thPercentile());
json.writeNumberField("p999", snapshot.get999thPercentile());

if (showSamples) {
if (this.showSamples) {
json.writeObjectField("values", snapshot.getValues());
}

Expand All @@ -119,6 +126,8 @@ public void serialize(Histogram histogram, JsonGenerator json,

private static class MeterSerializer extends StdSerializer<Meter> {

private static final long serialVersionUID = 5418467941358294770L;

private final String rateUnit;
private final double rateFactor;

Expand All @@ -133,17 +142,23 @@ public void serialize(Meter meter, JsonGenerator json,
SerializerProvider provider) throws IOException {
json.writeStartObject();
json.writeNumberField("count", meter.getCount());
json.writeNumberField("m15_rate", meter.getFifteenMinuteRate() * rateFactor);
json.writeNumberField("m1_rate", meter.getOneMinuteRate() * rateFactor);
json.writeNumberField("m5_rate", meter.getFiveMinuteRate() * rateFactor);
json.writeNumberField("mean_rate", meter.getMeanRate() * rateFactor);
json.writeStringField("units", rateUnit);
json.writeNumberField("m15_rate", meter.getFifteenMinuteRate() *
this.rateFactor);
json.writeNumberField("m1_rate", meter.getOneMinuteRate() *
this.rateFactor);
json.writeNumberField("m5_rate", meter.getFiveMinuteRate() *
this.rateFactor);
json.writeNumberField("mean_rate", meter.getMeanRate() *
this.rateFactor);
json.writeStringField("units", this.rateUnit);
json.writeEndObject();
}
}

private static class TimerSerializer extends StdSerializer<Timer> {

private static final long serialVersionUID = 6283520188524929099L;

private final String rateUnit;
private final double rateFactor;
private final String durationUnit;
Expand All @@ -166,40 +181,56 @@ public void serialize(Timer timer, JsonGenerator json,
json.writeStartObject();
final Snapshot snapshot = timer.getSnapshot();
json.writeNumberField("count", timer.getCount());
json.writeNumberField("max", snapshot.getMax() * durationFactor);
json.writeNumberField("mean", snapshot.getMean() * durationFactor);
json.writeNumberField("min", snapshot.getMin() * durationFactor);

json.writeNumberField("p50", snapshot.getMedian() * durationFactor);
json.writeNumberField("p75", snapshot.get75thPercentile() * durationFactor);
json.writeNumberField("p95", snapshot.get95thPercentile() * durationFactor);
json.writeNumberField("p98", snapshot.get98thPercentile() * durationFactor);
json.writeNumberField("p99", snapshot.get99thPercentile() * durationFactor);
json.writeNumberField("p999", snapshot.get999thPercentile() * durationFactor);

if (showSamples) {
json.writeNumberField("max", snapshot.getMax() *
this.durationFactor);
json.writeNumberField("mean", snapshot.getMean() *
this.durationFactor);
json.writeNumberField("min", snapshot.getMin() *
this.durationFactor);

json.writeNumberField("p50", snapshot.getMedian() *
this.durationFactor);
json.writeNumberField("p75", snapshot.get75thPercentile() *
this.durationFactor);
json.writeNumberField("p95", snapshot.get95thPercentile() *
this.durationFactor);
json.writeNumberField("p98", snapshot.get98thPercentile() *
this.durationFactor);
json.writeNumberField("p99", snapshot.get99thPercentile() *
this.durationFactor);
json.writeNumberField("p999", snapshot.get999thPercentile() *
this.durationFactor);

if (this.showSamples) {
final long[] values = snapshot.getValues();
final double[] scaledValues = new double[values.length];
for (int i = 0; i < values.length; i++) {
scaledValues[i] = values[i] * durationFactor;
scaledValues[i] = values[i] * this.durationFactor;
}
json.writeObjectField("values", scaledValues);
}

json.writeNumberField("stddev", snapshot.getStdDev() * durationFactor);
json.writeNumberField("m15_rate", timer.getFifteenMinuteRate() * rateFactor);
json.writeNumberField("m1_rate", timer.getOneMinuteRate() * rateFactor);
json.writeNumberField("m5_rate", timer.getFiveMinuteRate() * rateFactor);
json.writeNumberField("mean_rate", timer.getMeanRate() * rateFactor);
json.writeStringField("duration_units", durationUnit);
json.writeStringField("rate_units", rateUnit);
json.writeNumberField("stddev", snapshot.getStdDev() *
this.durationFactor);
json.writeNumberField("m15_rate", timer.getFifteenMinuteRate() *
this.rateFactor);
json.writeNumberField("m1_rate", timer.getOneMinuteRate() *
this.rateFactor);
json.writeNumberField("m5_rate", timer.getFiveMinuteRate() *
this.rateFactor);
json.writeNumberField("mean_rate", timer.getMeanRate() *
this.rateFactor);
json.writeStringField("duration_units", this.durationUnit);
json.writeStringField("rate_units", this.rateUnit);
json.writeEndObject();
}
}

private static class MetricRegistrySerializer
extends StdSerializer<MetricRegistry> {

private static final long serialVersionUID = 3717001164181726933L;

private final MetricFilter filter;

private MetricRegistrySerializer(MetricFilter filter) {
Expand All @@ -212,11 +243,13 @@ public void serialize(MetricRegistry registry, JsonGenerator json,
SerializerProvider provider) throws IOException {
json.writeStartObject();
json.writeStringField("version", VERSION.toString());
json.writeObjectField("gauges", registry.getGauges(filter));
json.writeObjectField("counters", registry.getCounters(filter));
json.writeObjectField("histograms", registry.getHistograms(filter));
json.writeObjectField("meters", registry.getMeters(filter));
json.writeObjectField("timers", registry.getTimers(filter));
json.writeObjectField("gauges", registry.getGauges(this.filter));
json.writeObjectField("counters",
registry.getCounters(this.filter));
json.writeObjectField("histograms",
registry.getHistograms(this.filter));
json.writeObjectField("meters", registry.getMeters(this.filter));
json.writeObjectField("timers", registry.getTimers(this.filter));
json.writeEndObject();
}
}
Expand Down Expand Up @@ -254,10 +287,11 @@ public void setupModule(Module.SetupContext context) {
context.addSerializers(new SimpleSerializers(Arrays.asList(
new GaugeSerializer(),
new CounterSerializer(),
new HistogramSerializer(showSamples),
new MeterSerializer(rateUnit),
new TimerSerializer(rateUnit, durationUnit, showSamples),
new MetricRegistrySerializer(filter)
new HistogramSerializer(this.showSamples),
new MeterSerializer(this.rateUnit),
new TimerSerializer(this.rateUnit, this.durationUnit,
this.showSamples),
new MetricRegistrySerializer(this.filter)
)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,17 @@
import com.google.common.collect.ImmutableSortedMap;

public class ServerReporter extends ScheduledReporter {

private static volatile ServerReporter instance = null;

private SortedMap<String, Gauge> gauges;
private SortedMap<String, Gauge<?>> gauges;
private SortedMap<String, Counter> counters;
private SortedMap<String, Histogram> histograms;
private SortedMap<String, Meter> meters;
private SortedMap<String, Timer> timers;

public static synchronized ServerReporter instance(MetricRegistry registry) {
public static synchronized ServerReporter instance(
MetricRegistry registry) {
if (instance == null) {
synchronized (ServerReporter.class) {
if (instance == null) {
Expand Down Expand Up @@ -82,7 +83,7 @@ public Map<String, Timer> timers() {
return Collections.unmodifiableMap(this.timers);
}

public Map<String, Gauge> gauges() {
public Map<String, Gauge<?>> gauges() {
return Collections.unmodifiableMap(this.gauges);
}

Expand All @@ -98,13 +99,14 @@ public Map<String, Meter> meters() {
return Collections.unmodifiableMap(this.meters);
}

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void report(SortedMap<String, Gauge> gauges,
SortedMap<String, Counter> counters,
SortedMap<String, Histogram> histograms,
SortedMap<String, Meter> meters,
SortedMap<String, Timer> timers) {
this.gauges = gauges;
this.gauges = (SortedMap) gauges;
this.counters = counters;
this.histograms = histograms;
this.meters = meters;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private String writeList(String label, List<?> list) {
}
}

private String writeIterator(String label, Iterator<?> itor,
private String writeIterator(String label, Iterator<?> iter,
boolean paging) {
try (ByteArrayOutputStream out = new ByteArrayOutputStream(LBUF_SIZE)) {
out.write("{".getBytes(API.CHARSET));
Expand All @@ -90,26 +90,26 @@ private String writeIterator(String label, Iterator<?> itor,

// Write data
boolean first = true;
while (itor.hasNext()) {
while (iter.hasNext()) {
if (!first) {
out.write(",".getBytes(API.CHARSET));
} else {
first = false;
}
out.write(JsonUtil.toJson(itor.next()).getBytes(API.CHARSET));
out.write(JsonUtil.toJson(iter.next()).getBytes(API.CHARSET));
}
out.write("]".getBytes(API.CHARSET));

// Write page
if (paging) {
String page;
if (itor instanceof GraphTraversal<?, ?>) {
page = TraversalUtil.page((GraphTraversal<?, ?>) itor);
} else if (itor instanceof Metadatable) {
page = PageState.page(itor);
if (iter instanceof GraphTraversal<?, ?>) {
page = TraversalUtil.page((GraphTraversal<?, ?>) iter);
} else if (iter instanceof Metadatable) {
page = PageState.page(iter);
} else {
throw new HugeException("Invalid paging iterator: %s",
itor.getClass());
iter.getClass());
}
if (page != null) {
page = String.format(",\"page\": \"%s\"", page);
Expand All @@ -127,7 +127,7 @@ private String writeIterator(String label, Iterator<?> itor,
throw new HugeException("Failed to serialize %s", e, label);
} finally {
try {
CloseableIterator.closeIterator(itor);
CloseableIterator.closeIterator(iter);
} catch (Exception e) {
throw new HugeException("Failed to close for %s", e, label);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,8 @@
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.http.client.utils.CloneUtils;
import org.apache.lucene.queryparser.xml.QueryBuilderFactory;
import org.slf4j.Logger;

import com.baidu.hugegraph.HugeException;
import com.baidu.hugegraph.backend.BackendException;
import com.baidu.hugegraph.backend.id.Id;
import com.baidu.hugegraph.backend.query.Condition;
Expand Down
Loading

0 comments on commit 727a0e4

Please sign in to comment.