| 1 8 package org.ozoneDB.core.storage.gammaStore; 9 10 import java.util.Collection ; 11 import java.util.HashSet ; 12 import java.util.LinkedList ; 13 import java.util.Properties ; 14 import java.util.SortedSet ; 15 import java.util.TreeSet ; 16 import org.ozoneDB.core.ConfigurationException; 17 import org.ozoneDB.core.storage.PropertyConfigurable; 18 import org.ozoneDB.core.storage.PropertyInfo; 19 20 public final class FreeSpaceManager implements PropertyConfigurable { 21 22 public static final PropertyInfo MAXFREESPACESIZEDIFFERENCE = new PropertyInfo( 23 ".maxFreeSpaceSizeDifference", 24 "int", 25 "8", 26 "Maximum difference in size (bytes) when searching for free space.", 27 new String [] { "0", "5", "28" } 28 ); 29 30 public static final PropertyInfo MAXSIZE = new PropertyInfo( 31 ".maxSize", 32 "int", 33 "1000", 34 "If the number of free space entries becomes higher than this value, " + 35 "TODO: what exactly?", 36 new String [] { "100", "1000" } 37 ); 38 39 private TreeSet freeSpaces; 40 41 private int maxSizeDifference; 42 43 private int maxSize; 44 45 private String prefix; 46 47 public FreeSpaceManager() { 48 } 49 50 53 public FreeSpaceManager(Properties properties, String prefix) { 54 this.prefix = prefix; 55 try { 56 setMaxFreeSpaceSizeDifference(Integer.parseInt(properties.getProperty(prefix + MAXFREESPACESIZEDIFFERENCE.getKey(), MAXFREESPACESIZEDIFFERENCE.getDefaultValue()))); 57 setMaxSize(Integer.parseInt(properties.getProperty(prefix + MAXSIZE.getKey(), MAXSIZE.getDefaultValue()))); 58 } catch (NumberFormatException e) { 59 throw new ConfigurationException("invalid value", e); 60 } 61 } 62 63 72 public synchronized FreeSpace findFreeSpace(int size) { 73 FreeSpace searchKey = new FreeSpace(0, 0, size); 74 SortedSet found = freeSpaces.tailSet(searchKey); 75 FreeSpace result = (FreeSpace) found.first(); 76 if (result.getSize() <= size + maxSizeDifference) { 77 found.remove(result); 78 size--; 79 } else { 80 result = null; 81 } 82 return result; 83 } 84 85 88 public synchronized void registerFreespace(FreeSpace freeSpace) { 89 freeSpaces.add(freeSpace); 90 } 91 92 public synchronized void registerFreespace(int clusterId, int position, int size) { 93 registerFreespace(new FreeSpace(clusterId, position, size)); 94 } 95 96 public int getMaxFreeSpaceSizeDifference() { 97 return maxSizeDifference; 98 } 99 100 public void setMaxFreeSpaceSizeDifference(int maxSizeDifference) { 101 this.maxSizeDifference = maxSizeDifference; 102 } 103 104 public int getMaxSize() { 105 return maxSize; 106 } 107 108 public void setMaxSize(int maxSize) { 109 this.maxSize = maxSize; 110 } 111 112 public Collection getPropertyInfos() { 113 Collection result = new LinkedList (); 114 result.add(MAXFREESPACESIZEDIFFERENCE); 115 result.add(MAXSIZE); 116 return result; 117 } 118 119 public String getPrefix() { 120 return prefix; 121 } 122 123 } 124 | Popular Tags |