KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.daffodilwoods.daffodildb.server.datasystem.persistentsystem;
2
3 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.*;
4 import com.daffodilwoods.database.general.*;
5 import com.daffodilwoods.database.resource.*;
6 import java.util.*;
7 import java.lang.ref.*;
8 import com.daffodilwoods.daffodildb.server.sql99.utils._Reference;
9
10
11 /**
12  *
13  It is something like a lock ,necessary for writing on database file.In daffodildb at a time only a
14  single user can have the permission to write.
15
16  */

17 public final class DatabaseUser implements _DatabaseUser{
18
19     /**
20      * map of clusters which are taken for write
21      */

22
23     private HashMap usedClusters;
24
25     /**
26      * To write Cluster bytes on database File
27      */

28     private PersistentDatabase persistentDatabase;
29
30     /**
31      * code by which user is identified to provide lock
32      */

33     private long userCode;
34
35     private boolean isUnLocked;
36
37     /**
38      * List of tables on which user has performed operations
39      */

40     private ArrayList tablesList;
41
42     DatabaseUser(PersistentDatabase persistentDatabase1,long userCode1) throws DException{
43         persistentDatabase = persistentDatabase1;
44         usedClusters = new HashMap(15);
45         tablesList = new ArrayList();
46         userCode = userCode1;
47         persistentDatabase.getUserLock().getLock(this);
48     }
49
50     DatabaseUser(PersistentDatabase persistentDatabase1,long userCode1,boolean flag) throws DException{
51     }
52
53     DatabaseUser(PersistentDatabase persistentDatabase1,long userCode1,ArrayList tableNames) throws DException{
54         persistentDatabase = persistentDatabase1;
55         usedClusters = new HashMap(15);
56         tablesList = new ArrayList();
57         userCode = userCode1;
58         persistentDatabase.getUserLock().getLock(this,tableNames);
59     }
60
61
62     /**
63      * writes all cluster bytes in databaseFile which were taken for write.
64      * and unlock this user.
65      *
66      */

67
68     public final boolean writeToFile() throws DException {
69         persistentDatabase.writeAllClusterToFile(usedClusters);
70         releaseCluster();
71         return true;
72     }
73     /**
74      * writes all cluster bytes in databaseFile which were taken for write.
75      * but doesn't unlock itself we only clear usedClusters but doesn't release lock.
76      * It is used where we write data at regular basis not all clusters at a time
77      * but is small chunks of cluster's written once a time it is used for temporary work
78      * done in TempIndexDatabase,tempIndexTable etc..
79      * @throws DException
80      * @return boolean
81      */

82     public final boolean writeToFileWithLock() throws DException {
83           persistentDatabase.writeAllClusterToFile(usedClusters);
84           usedClusters.clear();
85           return true;
86     }
87     /**
88      * puts Cluster in map with its clusterCharacteristics
89      * @param cluster which is taken for write
90      */

91
92     public final void addCluster(Object JavaDoc cluster){
93       ClusterCharacteristics cc = ((Cluster)cluster).clusterCharacteristics;
94       if(!usedClusters.containsKey(cc))
95         usedClusters.put(cc,new WeakReference(cluster));
96     }
97     /**
98      * clears used clusters list and release lock .
99      * @throws DException
100      */

101     public void releaseCluster() throws DException{
102         usedClusters.clear();
103         if(!isUnLocked){
104             persistentDatabase.getUserLock().unLock(this);
105             isUnLocked = true;
106         }
107     }
108
109     public boolean equals(Object JavaDoc object) {
110         return object == null ? false : userCode == ((DatabaseUser)object).userCode;
111     }
112
113     public String JavaDoc toString() {
114         return hashCode()+" --- "+userCode;
115     }
116
117     /**
118      * To restore all previously stored information in clusters and tables
119      */

120
121     public void rollback() throws DException{
122         throw new DException("PROBLEM IN COMMIT",null);
123     }
124
125     public ArrayList getTableNames() throws DException{
126         return tablesList;
127     }
128
129     public void addTable(QualifiedIdentifier parm1) {
130         tablesList.add(parm1);
131     }
132
133     public final void removeCluster(Object JavaDoc clusterCC) throws DException{
134         usedClusters.remove(clusterCC);
135     }
136
137     public int getSize() throws DException{
138       return usedClusters.size();
139     }
140
141
142
143 }
144
Popular Tags