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

com.gemstone.gemfire.internal.sequencelog.model.Graph Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
 *
 * Licensed 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. See accompanying
 * LICENSE file.
 */
package com.gemstone.gemfire.internal.sequencelog.model;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * @author dsmith
 *
 * TODO - I think a better idea here would be consider
 * source vertices as "temporary" place holders that
 * will get coalesed in the nearest destination vertex
 * in time. That might help the visualization. Or
 * maybe that should happen in that layer...
 */
public class Graph {

  private GraphID id;
  
  private Set edges = new HashSet();
  //A map used to find vertices by location id and timestamp.
  //locationId-> map(timestamp->vertex)
  private Map> indexedVertices = new HashMap>();

  public Graph(GraphID id) {
    this.id = id;
  }

  /**
   * Add an edge to this graph.
   * @param timestamp
   * @param edgeName
   * @param source
   * @param dest
   * @param isFromPattern 
   */
  public void addEdge(long timestamp, String edgeName, String state, String source,
      String dest, boolean isFromPattern) {

    Vertex destVertex = new Vertex(this, dest, state, timestamp);
    SortedMap map  = this.indexedVertices.get(dest);
    if(map == null) {
      map = new TreeMap();
      this.indexedVertices.put(dest, map);
    }
    
    //If this edge is being added by a pattern event, only
    //add the edge if the destination changes state as a result
    //of this edge. This cuts down on noise in the graph.
    if(isFromPattern) {
      SortedMap headMap = map.headMap(timestamp);
      if(headMap != null && !headMap.isEmpty()) {
        Long previousKey = headMap.lastKey();
        Vertex previousVertex = headMap.get(previousKey);
        if(previousVertex.getState().equals(state)) {
          return;
        }
      } else {
        //Super hack here. Don't add a transition from the non existent state to
        //the destroyed state in a lifeline.
        if(state.equals("destroyed")) {
          return;
        }
      }
    }
    map.put(timestamp, destVertex);
    
    edges.add(new Edge(this, timestamp, edgeName, source, destVertex));
  }

  /**
   * Get the edges in the graph.
   */
  public Collection getEdges() {
    return edges;
  }

  /**
   * Get the vertices in this graph, grouped by location id and then sorted
   * by timestamp.
   */
  Map> getIndexedVertices() {
    return indexedVertices;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy