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

com.gemstone.gemfire.internal.cache.OrderedTombstoneMap Maven / Gradle / Ivy

There is a newer version: 2.0-BETA
Show 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.cache;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

import com.gemstone.gemfire.internal.Assert;
import com.gemstone.gemfire.internal.cache.versions.VersionSource;
import com.gemstone.gemfire.internal.cache.versions.VersionTag;

/**
 * This class is used for sorting tombstones by region version number. Because
 * two tombstones with different members are not comparable, the iterator on
 * this class tries to return the tombstones in the order of the timestamps of the tombstones.
 * 
 * The class maintains a map, per member, of the tombstones sorted by the version
 * tag.
 * 
 * When removing entries, we pick from the sorted map that has the lowest timestamp.
 * 
 * This map is not threadsafe.
 * 
 * @author dsmith
 *
 */
public class OrderedTombstoneMap {
  
  /**
   * A map of
   * member id-> sort map of version tag-> region entry
   * 
   */
  private Map> tombstoneMap = new HashMap();
  
  /**
   * Add a new version tag to map
   */
  public void put(VersionTag tag, T entry) {
    //Add the version tag to the appropriate map
    VersionSource member = tag.getMemberID();
    TreeMap memberMap = tombstoneMap.get(member);
    if(memberMap == null) {
      memberMap = new TreeMap(new VersionTagComparator());
      tombstoneMap.put(member, memberMap);
    }
    T oldValue = memberMap.put(tag, entry);
    Assert.assertTrue(oldValue == null);
  }

  /**
   * Remove a version tag from the map.
   */
  public Map.Entry take() {
    if(tombstoneMap.isEmpty()) {
      //if there are no more entries, return null;
      return null;
    } else {
      //Otherwise, look at all of the members and find the tag with the 
      //lowest timestamp.
      long lowestTimestamp = Long.MAX_VALUE;
      TreeMap lowestMap = null;
      for(TreeMap memberMap: tombstoneMap.values()) {
        VersionTag firstTag = memberMap.firstKey();
        long stamp = firstTag.getVersionTimeStamp();
        if(stamp < lowestTimestamp) {
          lowestTimestamp = stamp;
          lowestMap = memberMap;
        }
      }
      if(lowestMap == null) {
        return null;
      }
      //Remove the lowest entry
      Entry result = lowestMap.firstEntry();
      lowestMap.remove(result.getKey());
      if(lowestMap.isEmpty()) {
        //if this is the last entry from a given member,
        //the map for that member
        tombstoneMap.remove(result.getKey().getMemberID());
      }
      
      return result;
    }
  }
  

  /**
   * A comparator that sorts version tags based on the region version, and
   * then on the timestamp.
   * @author dsmith
   *
   */
  public static class VersionTagComparator implements Comparator {

    @Override
    public int compare(VersionTag o1, VersionTag o2) {
      long result = o1.getRegionVersion() - o2.getRegionVersion();
      if(result == 0) {
        result = o1.getVersionTimeStamp() - o2.getVersionTimeStamp();
      }
      return Long.signum(result);
    }
    
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy