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

com.aliyun.odps.graph.local.LocalVertexMutations 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;

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

import com.aliyun.odps.graph.Edge;
import com.aliyun.odps.graph.Vertex;
import com.aliyun.odps.graph.VertexChanges;
import com.aliyun.odps.io.Writable;
import com.aliyun.odps.io.WritableComparable;

/**
 * Structure to hold all the possible graph mutations that can occur during a
 * superstep.
 *
 * @param 
 *     Vertex index value
 * @param 
 *     Vertex value
 * @param 
 *     Edge value
 * @param 
 *     Message value
 */
@SuppressWarnings("rawtypes")
public class LocalVertexMutations
    implements VertexChanges {

  /**
   * List of added vertices during the last superstep
   */
  private List> addedVertexList;
  /**
   * Count of remove vertex requests
   */
  private int removedVertexCount = 0;
  /**
   * List of added edges
   */
  private List> addedEdgeList;
  /**
   * List of removed edges
   */
  private List removedEdgeList;

  public LocalVertexMutations() {

  }

  /**
   * Copy the vertex mutations.
   *
   * @return Copied vertex mutations
   */

  @Override
  public List> getAddedVertexList() {
    return addedVertexList;
  }

  /**
   * Add a vertex mutation
   *
   * @param vertex
   *     Vertex to be added
   */
  public void addVertex(Vertex vertex) {
    if (addedVertexList == null) {
      addedVertexList = new ArrayList>(1);
    }
    addedVertexList.add(vertex);
  }

  @Override
  public int getRemovedVertexCount() {
    return removedVertexCount;
  }

  /**
   * Removed a vertex mutation (increments a count)
   */
  public void removeVertex() {
    ++removedVertexCount;
  }

  @Override
  public List> getAddedEdgeList() {
    return addedEdgeList;
  }

  /**
   * Add an edge to this vertex
   *
   * @param edge
   *     Edge to be added
   */
  public void addEdge(Edge edge) {
    if (addedEdgeList == null) {
      addedEdgeList = new ArrayList>(1);
    }
    addedEdgeList.add(edge);
  }

  @Override
  public List getRemovedEdgeList() {
    return removedEdgeList;
  }

  /**
   * Remove an edge on this vertex
   *
   * @param destinationVertexId
   *     Vertex index of the destination of the edge
   */
  public void removeEdge(I destinationVertexId) {
    if (removedEdgeList == null) {
      removedEdgeList = new ArrayList(1);
    }
    removedEdgeList.add(destinationVertexId);
  }

  /**
   * Add one vertex mutations to another
   *
   * @param vertexMutations
   *     Object to be added
   */
  public void addVertexMutations(LocalVertexMutations other) {
    List> otherAddedVertexList = other.getAddedVertexList();
    if (otherAddedVertexList != null) {
      if (addedVertexList == null) {
        addedVertexList = new ArrayList>(
            otherAddedVertexList);
      } else {
        addedVertexList.addAll(otherAddedVertexList);
      }
    }
    removedVertexCount += other.getRemovedVertexCount();
    List> otherAddedEdgeList = other.getAddedEdgeList();
    if (otherAddedEdgeList != null) {
      if (addedEdgeList == null) {
        addedEdgeList = new ArrayList>(otherAddedEdgeList);
      } else {
        addedEdgeList.addAll(otherAddedEdgeList);
      }
    }
    List otherRemovedEdgeList = other.getRemovedEdgeList();
    if (otherRemovedEdgeList != null) {
      if (removedEdgeList == null) {
        removedEdgeList = new ArrayList(otherRemovedEdgeList);
      } else {
        removedEdgeList.addAll(otherRemovedEdgeList);
      }
    }
  }

}