1 8 package org.jboss.cache.eviction; 9 10 import org.jboss.cache.TreeCache; 11 import org.jboss.logging.Logger; 12 13 import java.util.HashMap ; 14 import java.util.Map ; 15 16 25 public class RegionManager 26 { 27 private Logger log_ = Logger.getLogger(RegionManager.class); 28 public final static int CAPACITY = 200000; 29 private Map regionMap_ = new HashMap (); 30 private Region[] regions_; 32 private EvictionPolicy policy_; 33 final static String DEFAULT_REGION = "/_default_/"; 35 36 public RegionManager(EvictionPolicy policy) 37 { 38 policy_ = policy; 39 regions_ = null; 40 } 41 42 49 public Region createRegion(String fqn, EvictionAlgorithm algorithm) 50 throws RegionNameConflictException 51 { 52 if (log_.isDebugEnabled()) 53 { 54 log_.debug("createRegion(): creating region for fqn- " + fqn); 55 } 56 57 String newFqn = appendFqn(fqn); 58 checkConflict(newFqn); 59 Region region = new Region(newFqn, policy_, algorithm); 60 regionMap_.put(newFqn, region); 61 return region; 62 } 63 64 public void removeRegion(String fqn) 65 { 66 regionMap_.remove(fqn); 67 } 68 69 75 private String appendFqn(String fqn) 76 { 77 if (!fqn.endsWith(TreeCache.SEPARATOR)) 78 return fqn + TreeCache.SEPARATOR; 79 else 80 return fqn; 81 } 82 83 public boolean hasRegion(String myFqn) 84 { 85 String newFqn = appendFqn(myFqn); 86 return regionMap_.containsKey(newFqn); 87 } 88 89 public Region getRegion(String myFqn) 90 { 91 Region[] regions = getRegions(); 92 String myRFqn = appendFqn(myFqn); 94 96 for (int i = (regions.length - 1); i >= 0; i--) 98 { 99 String fqn = regions[i].getFqn(); 100 if (myRFqn.startsWith(fqn)) return regions[i]; 101 } 102 103 if (log_.isTraceEnabled()) 104 { 105 log_.trace("getRegion(): not user-specified region found for this fqn- " + myFqn 106 + " will use the global default region"); 107 } 108 return (Region) regionMap_.get(DEFAULT_REGION); 109 } 110 111 public Region[] getRegions() 112 { 113 if (regions_ != null && regions_.length == regionMap_.size()) 115 return regions_; 116 117 Object [] objs = regionMap_.values().toArray(); 118 Region[] regions = new Region[objs.length]; 119 for (int i = 0; i < objs.length; i++) 120 { 121 regions[i] = (Region) objs[i]; 122 } 123 124 if (log_.isDebugEnabled()) 125 { 126 log_.debug("getRegions(): size of region " + regions.length); 127 } 128 129 regions_ = regions; 130 return regions; 131 } 132 133 141 public void checkConflict(String myFqn) throws RegionNameConflictException 142 { 143 Region[] regions = getRegions(); 151 for (int i = 0; i < regions.length; i++) 152 { 153 String fqn = regions[i].getFqn(); 154 if (myFqn.equals(fqn) || myFqn.startsWith(fqn)) 155 { throw new RegionNameConflictException("RegionManager.checkConflict(): new region fqn " 157 + myFqn + " is in conflict with current region fqn- " + fqn); 158 } 159 } 160 } 162 } 163 | Popular Tags |