All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aliyun.odps.graph.local.message.SuperStepBuffer Maven / Gradle / Ivy

The newest version!
/*
 * 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 com.aliyun.odps.graph.local.message;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import com.aliyun.odps.graph.local.RuntimeContext;
import com.aliyun.odps.io.Writable;
import com.aliyun.odps.io.WritableComparable;

public class SuperStepBuffer {

  private long superStep;
  private Map, MsgBuffer> msgBuffer = null;

  public SuperStepBuffer(long superstep) {
    superStep = superstep;
    msgBuffer = new HashMap, MsgBuffer>();
  }

  public void setSuperStep(long superstep) {
    this.superStep = superstep;
  }

  public long getSuperStep() {
    return this.superStep;
  }

  public void pushMsg(WritableComparable vertexId, Writable msg) {
    if (msgBuffer.containsKey(vertexId)) {
      msgBuffer.get(vertexId).addMessage(msg);
    } else {
      MsgBuffer buffer = new MsgBuffer();
      buffer.addMessage(msg);
      msgBuffer.put(vertexId, buffer);
    }
  }

  public boolean hasMsg(WritableComparable vertexId) {
    return msgBuffer.containsKey(vertexId)
           && msgBuffer.get(vertexId).hasMessages();
  }

  public Iterable popMsges(final WritableComparable vertexId) {
    return new MsgIterable(vertexId);
  }

  public void dump(RuntimeContext context) throws IOException {
    File superStepDir = new File(context.getSuperStepDir(),
                                 String.valueOf(superStep));
    for (Map.Entry, MsgBuffer> entry : msgBuffer
        .entrySet()) {
      File vertexDir = new File(superStepDir, entry.getKey().toString());
      entry.getValue().dump(new File(vertexDir, "inputs"));
    }
  }

  public Set> getVertexIDList() {
    return msgBuffer.keySet();
  }

  public boolean hasMessages() {
    return !msgBuffer.isEmpty();
  }

  class MsgIterable implements Iterable {

    private WritableComparable vertexId;

    public MsgIterable(WritableComparable id) {
      this.vertexId = id;
    }

    @Override
    public Iterator iterator() {
      // TODO Auto-generated method stub
      if (msgBuffer.containsKey(vertexId)) {
        return msgBuffer.get(vertexId).getMessages().iterator();
      } else {
        return new ArrayList().iterator();
      }
    }

  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy