2828 */
2929public class SQLiteProcessorState implements AutoCloseable {
3030
31+ public static final int INIT = 0x0000 , AUTH = 0x0001 , OPEN = 0x0002 ,
32+ QUERY = 0x0004 , SLEEP = 0x0008 , SLEEP_IN_TX = 0x0010 ,
33+ STOPPED = 0x0020 , CLOSED = 0x0040 , READ = 0x0080 , WRITE = 0x0100 ;
34+
3135 private final SpinLock lock = new SpinLock ();
3236
3337 protected final SQLiteProcessor processor ;
3438 protected String command ;
3539 protected long startTime ;
36- protected String state = "" ;
40+ protected int state = INIT ;
41+ protected String stateText = "" ;
3742 protected String sql ;
3843
3944 public SQLiteProcessorState (SQLiteProcessor processor ) {
4045 this .processor = processor ;
4146 this .startTime = System .currentTimeMillis ();
42- this .state = "init" ;
47+ this .stateText = "init" ;
4348 }
4449
4550 public SQLiteProcessor getProcessor () {
@@ -99,14 +104,23 @@ public void setStartTime(long startTime) {
99104 }
100105 }
101106
102- public String getState () {
103- return state ;
107+ public int getState () {
108+ this .lock .lock ();
109+ try {
110+ return this .state ;
111+ } finally {
112+ this .lock .unlock ();
113+ }
104114 }
105115
106- public void setState (String state ) {
116+ public String getStateText () {
117+ return this .stateText ;
118+ }
119+
120+ public void setStateText (String stateText ) {
107121 this .lock .lock ();
108122 try {
109- this .state = state ;
123+ this .stateText = stateText ;
110124 } finally {
111125 this .lock .unlock ();
112126 }
@@ -130,12 +144,60 @@ public String getInfo() {
130144 return getInfo (false );
131145 }
132146
147+ public void lock () {
148+ this .lock .lock ();
149+ }
150+
151+ public void unlock () {
152+ this .lock .unlock ();
153+ }
154+
155+ public void startAuth () {
156+ this .lock .lock ();
157+ try {
158+ this .command = "Auth" ;
159+ this .startTime = System .currentTimeMillis ();
160+ this .state = AUTH ;
161+ this .stateText = "start authentication" ;
162+ this .sql = null ;
163+ } finally {
164+ this .lock .unlock ();
165+ }
166+ }
167+
133168 public void startOpen () {
134169 this .lock .lock ();
135170 try {
136171 this .command = "Open" ;
137172 this .startTime = System .currentTimeMillis ();
138- this .state = "open and init database" ;
173+ this .state = OPEN ;
174+ this .stateText = "open and init database" ;
175+ this .sql = null ;
176+ } finally {
177+ this .lock .unlock ();
178+ }
179+ }
180+
181+ public void startRead () {
182+ this .lock .lock ();
183+ try {
184+ this .command = "Read" ;
185+ this .startTime = System .currentTimeMillis ();
186+ this .state = READ ;
187+ this .stateText = "read data from network" ;
188+ this .sql = null ;
189+ } finally {
190+ this .lock .unlock ();
191+ }
192+ }
193+
194+ public void startWrite () {
195+ this .lock .lock ();
196+ try {
197+ this .command = "Write" ;
198+ this .startTime = System .currentTimeMillis ();
199+ this .state = WRITE ;
200+ this .stateText = "flush data into network" ;
139201 this .sql = null ;
140202 } finally {
141203 this .lock .unlock ();
@@ -151,19 +213,33 @@ public void startQuery(SQLStatement sqlStatement, String state) {
151213 try {
152214 this .command = "Query" ;
153215 this .startTime = System .currentTimeMillis ();
154- this .state = state ;
216+ this .state = QUERY ;
217+ this .stateText = state ;
155218 this .sql = sqlStatement .toString ();
156219 } finally {
157220 this .lock .unlock ();
158221 }
159222 }
160223
161224 public void startSleep () {
225+ boolean inTx = false ;
226+ if (this .processor .isOpen ()) {
227+ if (this .processor .getConnection () != null ) {
228+ inTx = !this .processor .isAutoCommit ();
229+ }
230+ }
231+
162232 this .lock .lock ();
163233 try {
164234 this .command = "Sleep" ;
165235 this .startTime = System .currentTimeMillis ();
166- this .state = "" ;
236+ if (inTx ) {
237+ this .state = SLEEP_IN_TX ;
238+ this .stateText = "idle in transaction" ;
239+ } else {
240+ this .state = SLEEP ;
241+ this .stateText = "idle" ;
242+ }
167243 this .sql = null ;
168244 } finally {
169245 this .lock .unlock ();
@@ -177,6 +253,7 @@ public SQLiteProcessorState copy() {
177253 copy .command = this .command ;
178254 copy .startTime = this .startTime ;
179255 copy .state = this .state ;
256+ copy .stateText = this .stateText ;
180257 copy .sql = this .sql ;
181258 } finally {
182259 this .lock .unlock ();
@@ -190,7 +267,8 @@ public void stop() {
190267 try {
191268 this .command = null ;
192269 this .startTime = System .currentTimeMillis ();
193- this .state = "stopped" ;
270+ this .state = STOPPED ;
271+ this .stateText = "stopped" ;
194272 this .sql = null ;
195273 } finally {
196274 this .lock .unlock ();
@@ -207,7 +285,8 @@ public void close() {
207285 try {
208286 this .command = null ;
209287 this .startTime = System .currentTimeMillis ();
210- this .state = "closed" ;
288+ this .state = CLOSED ;
289+ this .stateText = "closed" ;
211290 this .sql = null ;
212291 } finally {
213292 this .lock .unlock ();
0 commit comments