Salome HOME
Refactoring: configuration files are moved into Siman web project.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / kernel / IDPool.java
1 package org.splat.kernel;
2 /**
3  * Optimized generator of persistent identifiers.<br/>
4  * This generator minimizes the database hits by grouping a bunch of identifiers in memory and only hitting the database
5  * when the in-memory value group is exhausted.
6  * Only one IDPool object held by the Database class is created during a session.
7  * 
8  * @author    Daniel Brunier-Coulin
9  * @copyright OPEN CASCADE 2012
10  */
11
12 import java.sql.Connection;
13 import java.sql.ResultSet;
14 import java.sql.SQLException;
15 import java.sql.Statement;
16
17 import org.hibernate.Session;
18 import org.hibernate.jdbc.Work;
19
20
21 public class IDPool {
22
23 //  Persistent fields
24     private        Integer lastid;         // Last generated ID
25     private        String  version;        // Version of the database schema
26
27 //  Transient fields
28     private        int     remaining;      // Remaining available ID held in this pool
29     private static int     poolsize = 1;   // No pool by default
30
31     private class  LoadNewIDs implements Work {
32 //  -----------------------------------------
33       public void execute(Connection connex) throws SQLException
34       {         
35         Statement    request = connex.createStatement();
36         ResultSet    result  = request.executeQuery("SELECT MAX(rid) AS lastid FROM any");
37         StringBuffer command = new StringBuffer("UPDATE any SET rid=");
38
39         result.first();
40         lastid  = result.getInt("lastid");
41
42         command.append(lastid + poolsize).append(" WHERE rid=").append(lastid);
43         request.execute(command.toString());
44
45         remaining = poolsize;
46       }
47     }
48     private class  LoadLastID implements Work {
49 //  -----------------------------------------
50       public void execute(Connection connex) throws SQLException
51       {         
52         Statement    request = connex.createStatement();
53         ResultSet    result  = request.executeQuery("SELECT MAX(rid) AS lastid FROM any");
54         StringBuffer command = new StringBuffer("SELECT version FROM any WHERE rid=");
55
56         result.first();
57         lastid    = result.getInt("lastid");
58
59         command.append(lastid);
60         result = request.executeQuery(command.toString());
61         result.first();
62         version   = result.getString("version");
63         remaining = 0;
64       }
65     }
66
67 //  ==============================================================================================================================
68 //  Constructors
69 //  ==============================================================================================================================
70
71     protected IDPool () {
72     }
73 /**
74  * Constructor called only once, when initializing the database.
75  * 
76  * @param version the version of the constructed database schema.
77  */
78     protected IDPool (String version) {
79 //  ---------------------------------
80       this.lastid    = 0;         //TODO: Get the current last ID if a previous version exists
81       this.version   = version;
82       this.remaining = 0;
83     }
84 /**
85  * Constructor called at start of every database session.
86  * 
87  * @param base the started database session
88  */
89     protected IDPool (Session base) {
90 //  -------------------------------
91       base.doWork( new LoadLastID() );
92     }
93
94 //  ==============================================================================================================================
95 //  Protected member functions
96 //  ==============================================================================================================================
97
98     protected Integer getNextID () {
99 //  ------------------------------
100       if (remaining <= 0) Database.getSession().doWork( new LoadNewIDs() );
101       lastid    += 1;
102       remaining -= 1;
103       return lastid;
104     }
105
106     protected String getSchemaVersion () {
107 //  ------------------------------------
108       return version;
109     }
110
111     protected static void setPoolSize (int size) {
112 //  --------------------------------------------
113       if (size > 1) poolsize = size;
114     }
115 }