blob: b2523b476d73408d08fce774c05885b9a6fc9d42 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.tinkerpop.gremlin.process.computer.ranking.pagerank;
import org.apache.tinkerpop.gremlin.process.computer.KeyValue;
import org.apache.tinkerpop.gremlin.process.computer.util.StaticMapReduce;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.Property;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.structure.util.StringFactory;
import org.apache.commons.configuration2.Configuration;
import java.util.Iterator;
/**
* @author Marko A. Rodriguez (http://markorodriguez.com)
*/
public class PageRankMapReduce extends StaticMapReduce<Object, Double, Object, Double, Iterator<KeyValue<Object, Double>>> {
public static final String PAGE_RANK_MEMORY_KEY = "gremlin.pageRankMapReduce.memoryKey";
public static final String DEFAULT_MEMORY_KEY = "pageRank";
private String memoryKey = DEFAULT_MEMORY_KEY;
private PageRankMapReduce() {
}
private PageRankMapReduce(final String memoryKey) {
this.memoryKey = memoryKey;
}
@Override
public void storeState(final Configuration configuration) {
super.storeState(configuration);
configuration.setProperty(PAGE_RANK_MEMORY_KEY, this.memoryKey);
}
@Override
public void loadState(final Graph graph, final Configuration configuration) {
this.memoryKey = configuration.getString(PAGE_RANK_MEMORY_KEY, DEFAULT_MEMORY_KEY);
}
@Override
public boolean doStage(final Stage stage) {
return stage.equals(Stage.MAP);
}
@Override
public void map(final Vertex vertex, final MapEmitter<Object, Double> emitter) {
final Property pageRank = vertex.property(PageRankVertexProgram.PAGE_RANK);
if (pageRank.isPresent()) {
emitter.emit(vertex.id(), (Double) pageRank.value());
}
}
@Override
public Iterator<KeyValue<Object, Double>> generateFinalResult(final Iterator<KeyValue<Object, Double>> keyValues) {
return keyValues;
}
@Override
public String getMemoryKey() {
return this.memoryKey;
}
@Override
public String toString() {
return StringFactory.mapReduceString(this, this.memoryKey);
}
//////////////////////////////
public static Builder build() {
return new Builder();
}
public final static class Builder {
private String memoryKey = DEFAULT_MEMORY_KEY;
private Builder() {
}
public Builder memoryKey(final String memoryKey) {
this.memoryKey = memoryKey;
return this;
}
public PageRankMapReduce create() {
return new PageRankMapReduce(this.memoryKey);
}
}
}