KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sessionsystem > TableRowIdGetter


1 package com.daffodilwoods.daffodildb.server.sessionsystem;
2
3 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator._Iterator;
4
5 import com.daffodilwoods.database.general.*;
6 import com.daffodilwoods.database.resource.*;
7 import com.daffodilwoods.database.sqlinitiator.*;
8 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.*;
9 import com.daffodilwoods.daffodildb.utils.FieldUtility;
10 import com.daffodilwoods.daffodildb.utils.field.FieldBase;
11 import com.daffodilwoods.daffodildb.server.datasystem.indexsystem.FileNodeManager;
12
13
14 /**
15  * <p>Title: </p> Table Row ID Getter
16  * <p>Description: </p>
17  * Provides the new(unique) rowId for a perticular table. RowId always remains
18  * unique in a table. A table cannot contain two valid record with the same rowId.
19  * It loads the last rowid from the table and maintains it in a variable on the time
20  * of inialization.
21  */

22
23 public class TableRowIdGetter {
24
25    /**
26     * Name of the table
27     */

28
29   private String JavaDoc tableName;
30
31   /**
32    * Object of merge table to get the last rowId
33    */

34
35   private _Table mergeTable;
36
37   /**
38    * Uniqe no for a table
39    */

40
41   private long rowId;
42
43   public String JavaDoc toString() {
44       return " " + mergeTable;
45   }
46
47   /**
48    * Constructs TableRowIdGetter
49    * @param tableName name of the table
50    * @param cacheTable Object of the Merged Table to get the last stored rowId
51    * @throws DException
52    */

53
54   public TableRowIdGetter( String JavaDoc tableName0, _Table cacheTable0 ) throws DException {
55     tableName = tableName0;
56     mergeTable = cacheTable0;
57   }
58
59   /**
60    * returns the next row id for a table .
61    * @return Long
62    * @throws DException
63    */

64
65   public synchronized FieldBase getNextRowId() throws DException {
66      initialize();
67      return FieldUtility.getField(new Long JavaDoc(++rowId));
68  }
69
70  /**
71   * returns the Last or current row id assigned to row of a table.
72   * @return Long
73   * @throws DException
74   */

75
76   public synchronized FieldBase getLastRowId() throws DException {
77      initialize();
78      return FieldUtility.getField(new Long JavaDoc(rowId));
79  }
80
81
82  /**
83   * Fetch the last rowId from the table and initialize the rowId variable
84   * @throws DException
85   */

86
87  public synchronized void initialize() throws DException {
88      if( rowId == 0 ){
89          Object JavaDoc max = null;
90          _Iterator iterator = ((_IndexTable)mergeTable).getDefaultIterator();
91          if(iterator.last()){
92             max = ((Object JavaDoc[])iterator.getColumnValues())[0];
93          }
94          rowId = max == null ? 0 : ((Long JavaDoc)((FieldBase)max).getObject()).longValue();
95      }
96   }
97 }
98
Popular Tags