KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > datasystem > persistentsystem > FreeClusterList


1 package com.daffodilwoods.daffodildb.server.datasystem.persistentsystem;
2
3 import java.util.*;
4
5 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.*;
6 import com.daffodilwoods.daffodildb.utils.*;
7 import com.daffodilwoods.daffodildb.utils.byteconverter.*;
8 import com.daffodilwoods.database.resource.*;
9
10 /**
11  * It maintains the list of all free Clusters, when all Records from a cluster are deleted or any table is
12  * dropped, then address of all these free clusters are stored in ClusterInfo SystemTable. when any new
13  * cluster is required for any table, then cluster is allocted from these free clusters.
14  */

15 public class FreeClusterList {
16
17    /**
18     * System Table to maintain the list of all free Clusters in database
19     */

20
21    private _Table clusterInfo;
22
23    /**
24     * List to store free cluster address for testing of free cluster added or not.
25     * It is used in test cases of data system.
26     */

27    public ArrayList freeAddressList;
28
29    /**
30       * It is used to check that we are executing test case for free cluster list
31       * so it has to use free address list. It is seted true in test cases of data system.
32       */

33     public static boolean flagForTestingOfFreeClusterList;
34
35    public FreeClusterList(_Table clusterInfo1) throws DException {
36       clusterInfo = clusterInfo1;
37       freeAddressList = new ArrayList(5);
38    }
39
40    /**
41     * Inserts free Cluster address in ClusterInfo System Table
42     *
43     * @param user To perform write operations
44     * @param address address of free cluster
45     */

46
47    public synchronized void addFreeCluster(_DatabaseUser user, int address) throws DException {
48       DatabaseUserTableIterator tableIterator = (DatabaseUserTableIterator) ( (Table) clusterInfo).getIterator();
49       BufferRange[] bytes = new BufferRange[] {new BufferRange(CCzufDpowfsufs.getBytes(address))}; //tc.getBytes(value);
50
tableIterator.insert(user, bytes);
51       if(flagForTestingOfFreeClusterList)
52         freeAddressList.add(new Integer JavaDoc(address));
53    }
54
55    /**
56     * Checks whether any free cluster exist in ClusterInfo system table, if exists then return that cluster
57     * address otherwise return -1
58     *
59     * @param user To perform write operations
60     * @return address of free cluster or -1
61     */

62
63    public int checkFreeClusterList(_DatabaseUser user) throws DException {
64       DatabaseUserTableIterator tableIterator = (DatabaseUserTableIterator) ( (Table) clusterInfo).getIterator();
65       if (tableIterator.first()) {
66          BufferRange values = (BufferRange) tableIterator.getColumnValues(0);
67          tableIterator.delete(user);
68          if(flagForTestingOfFreeClusterList)
69            freeAddressList.remove(new Integer JavaDoc(CCzufDpowfsufs.getInt(values.getBytes()).intValue()));
70          return CCzufDpowfsufs.getInt(values.getBytes()).intValue();
71       }
72       return -1;
73    }
74    /**
75     * It is used in getStatus method it is used for testing to show that at any stage how many clusters are free.
76     * @throws DException
77     */

78    public void showFreeClusterList() throws DException {
79       DatabaseUserTableIterator tableIterator = (DatabaseUserTableIterator) ( (Table) clusterInfo).getIterator();
80       int count = 0;
81       if (tableIterator.first()) {
82          do {
83             BufferRange values = (BufferRange) tableIterator.getColumnValues(0);
84             int add = CCzufDpowfsufs.getInt(values.getBytes()).intValue();
85             count++;
86          } while (tableIterator.next());
87       }
88    }
89 }
90
Popular Tags