KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > core > storage > gammaStore > FreeSpaceManager


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Core License version 1 published by ozone-db.org.
3
//
4
// Copyright (C) 2003-@year@, Leo Mekenkamp. All rights reserved.
5
//
6
// $Id: FreeSpaceManager.java,v 1.1.2.1 2004/04/10 10:06:51 per_nyfelt Exp $
7

8 package org.ozoneDB.core.storage.gammaStore;
9
10 import java.util.Collection JavaDoc;
11 import java.util.HashSet JavaDoc;
12 import java.util.LinkedList JavaDoc;
13 import java.util.Properties JavaDoc;
14 import java.util.SortedSet JavaDoc;
15 import java.util.TreeSet JavaDoc;
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 JavaDoc[] { "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 JavaDoc[] { "100", "1000" }
37     );
38
39     private TreeSet JavaDoc freeSpaces;
40     
41     private int maxSizeDifference;
42     
43     private int maxSize;
44     
45     private String JavaDoc prefix;
46     
47     public FreeSpaceManager() {
48     }
49     
50     /**
51      * As prescribed by the <code>PropertyConfigurable</code> interface.
52      */

53     public FreeSpaceManager(Properties JavaDoc properties, String JavaDoc 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 JavaDoc e) {
59             throw new ConfigurationException("invalid value", e);
60         }
61     }
62     
63     /**
64      * Returns a block of free space in a cluster when there is free space that
65      * is equal or grater than the specified size and smaller or equal to the
66      * specified size plus <code>getMaxSizeDifference()</code>. The returned
67      * <code>FreeSpace</code> is no longer regarded as 'free'. If no apropriate
68      * free space is found, then a <code>null</code> value is returned.
69      *
70      * @return a free block of space, or <code>null</code> if there is none
71      */

72     public synchronized FreeSpace findFreeSpace(int size) {
73         FreeSpace searchKey = new FreeSpace(0, 0, size);
74         SortedSet JavaDoc 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     /**
86      * Registers a block of free space.
87      */

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 JavaDoc getPropertyInfos() {
113         Collection JavaDoc result = new LinkedList JavaDoc();
114         result.add(MAXFREESPACESIZEDIFFERENCE);
115         result.add(MAXSIZE);
116         return result;
117     }
118     
119     public String JavaDoc getPrefix() {
120         return prefix;
121     }
122     
123 }
124
Popular Tags