KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > dml > declarecursor > MemoryTable


1 package com.daffodilwoods.daffodildb.server.sql99.dml.declarecursor;
2
3 import com.daffodilwoods.database.resource.*;
4 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.*;
5 import com.daffodilwoods.database.utility.P;
6 import com.daffodilwoods.daffodildb.server.datasystem.utility.*;
7 import com.daffodilwoods.database.general.QualifiedIdentifier;
8 import java.util.*;
9 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.*;
10 import com.daffodilwoods.daffodildb.server.sql99.utils._Reference;
11 import com.daffodilwoods.daffodildb.utils.comparator.CTbnfUzqfDpnqbsbups;
12 import com.daffodilwoods.daffodildb.utils.comparator.CPckfduDpnqbsbups;
13 import com.daffodilwoods.daffodildb.utils.comparator.CJoufhfsDpnqbsbups;
14 import com.daffodilwoods.daffodildb.utils.field.FieldBase;
15 import com.daffodilwoods.daffodildb.utils.BufferRange;
16 import java.io.*;
17 import com.daffodilwoods.daffodildb.server.datasystem.btree.*;
18
19 /**
20  *
21  * <p>Title: MemoryTable</p>
22  * <p>Description: Objective of the MemoryTable is to perform all the
23  * operations on the table. It is used to insert, update, delete and
24  * retreive the records of the table. It keeps track of all the records
25  * in the OrderedKeyList by maintaining a key for each record.
26  */

27 public class MemoryTable implements _DataTable{
28
29     /**
30      * RecordList keeps a list of all the records in the memory
31      * having mapping with their keys.
32      */

33
34     private OrderedKeyList recordList;
35
36     /**
37      * Counter is used to give a key to record by which record is
38      * identified in the list.It is a Integer value which is the
39      * last key number.
40      */

41
42    private int counter = Integer.MIN_VALUE;
43
44     /**
45      * TableCharacteriseics of the table to get tableinformation as
46      * columnCount in table etc.
47      */

48
49    private _TableCharacteristics tableCharacteristics;
50
51     /**
52      * Name of this table
53      */

54    private QualifiedIdentifier tableName;
55
56     /**
57      * Total size occupied in the memory by this table
58      */

59
60    private double totalSize;
61
62     /**
63      * Constructs the memoryTable and initializes tableName,table
64      * Characteristics and OrderedKeyList to store records
65      * @param tableName0 Name of the table
66      * @param tableCharacteristics0 tableCharacteristics of this table
67      */

68
69     public MemoryTable(QualifiedIdentifier tableName0,_TableCharacteristics tableCharacteristics0) throws DException {
70         tableCharacteristics = tableCharacteristics0;
71         tableName = tableName0;
72         recordList = new OrderedKeyList(new CJoufhfsDpnqbsbups());
73
74     }
75
76     /**
77      * generates a new key and inserts record in the table with the given
78      * values and new key.it returns the key at which record is inserted
79      * @param values values of record which has to insert in table.
80      * @return the key at which record is inserted
81      * @throws DException If the number of values is not equal to
82      * the number of columns in the table.
83      */

84
85     public Object JavaDoc insert(Object JavaDoc values) throws DException{
86         Object JavaDoc key = getKey();
87         recordList.insert(key,values);
88         return key;
89     }
90
91     /**
92      * updates the record in the table with the given values at given key.
93      * @param key of the record that is to be updated in the table.
94      * @param values values with which the record in the table is to be
95      * updated.
96      * @return key at which record is upadted in table.
97      * @throws DException if the number of values is not equal to the nuber
98      * of columns or record is not found at given key.
99      */

100
101     public Object JavaDoc update(Object JavaDoc key,Object JavaDoc values) throws DException{
102             Object JavaDoc[] oldValues = (Object JavaDoc[])recordList.getObject(key);
103             if(oldValues == null)
104               throw StaticExceptions.RECORD_DELETED_EXCEPTION ;
105             recordList.insert(key,values);
106
107             return key;
108     }
109
110
111     /**
112      * Deletes the record from the table at the given key.
113      * @param key key of the record to be deleted.
114      * @return key at which record is deleted from table.
115      * @throws DException if the record doesnt exist in the table
116      * at the given key
117      */

118
119     public Object JavaDoc delete(Object JavaDoc key) throws DException{
120             Object JavaDoc oldVal[] = (Object JavaDoc[])recordList.getObject(key);
121             if(oldVal == null)
122                 throw new DataException("DSE2004",null );
123             totalSize -= tableCharacteristics.getMemoryUsage(oldVal);
124             recordList.delete(key);
125             return key;
126     }
127
128     /**
129      * returns an iterator on the table.
130      * @return an iterator to navigate on the table.
131      */

132
133     public _TableIterator getIterator() throws DException {
134         return new MemoryTableIterator(this);
135     }
136
137     public int getRowCount() throws DException {
138         return recordList.size();
139     }
140
141     /**
142      * Returns key of very first record in this table.
143      */

144
145     public Object JavaDoc getFirstKey() throws DException {
146         return recordList.getTopKey();
147     }
148
149     /**
150      * Returns key of last record in this table.
151      */

152
153     public Object JavaDoc getLastKey() throws DException {
154             return recordList.getBottomKey();
155     }
156
157     /**
158      * Verifies whether the given key is valid, invalid or deleted.
159      * @param key key which has to verify.
160      * @throws DException if the key is InValid or key is deleted.
161      */

162
163     public void verifyValidity(Object JavaDoc key) throws DException{
164         if(counter < key.hashCode()){
165             throw DatabaseConstants.KEY_NOTFOUND_EXCEPTION;
166         }
167         if(!recordList.containsKey(key)){
168             throw new DException("DSE2003",null);
169         }
170     }
171
172     /**
173      * retuns the record at the given key from the table.
174      * @param key key of the record which has to retreive from the table
175      * @return the values of the record
176      * @throws DException if the record with given key does not exist
177      * in the table.
178      */

179
180     public Object JavaDoc getObjectAtKey( Object JavaDoc key ) throws DException {
181             Object JavaDoc keyToReturn = recordList.getObject(key);
182             if(keyToReturn == null)
183               throw StaticExceptions.RECORD_DELETED_EXCEPTION ;
184             return keyToReturn;
185     }
186
187     public void clearRecords() {
188             recordList = null;
189     }
190
191     /**
192      * Determines the total size consumed by the memory.
193      * @return the size of memory that is used up.
194      */

195
196     public double get_size() {
197         return totalSize;
198     }
199
200     public int getIndexType() {
201         return 0;
202     }
203
204     /**
205      * Returns the tableCharacteristices of the table.
206      */

207
208     public _TableCharacteristics getTableCharacteristics() throws DException{
209         return tableCharacteristics;
210     }
211
212     public _Record getBlankRecord() throws DException{
213         return new Record(tableCharacteristics,new Object JavaDoc[tableCharacteristics.getColumnCount()]);
214     }
215
216     /**
217      * Provides a key to new record which is to be inserted in this table.
218      * @return : key of new record
219      */

220
221     private synchronized Object JavaDoc getKey() {
222         return new Integer JavaDoc(counter++);
223     }
224 }
225
Popular Tags