KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > txn > LockType


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: LockType.java,v 1.18 2006/10/30 21:14:27 bostic Exp $
7  */

8
9 package com.sleepycat.je.txn;
10
11 /**
12  * LockType is a type safe enumeration of all lock types. Methods on LockType
13  * objects can be used to determine whether a type conflicts with another
14  * type or can be upgraded to another type.
15  */

16 public class LockType {
17
18     /**
19      * Lock types. Indexes must be kept manually synchronized in the matrixes
20      * below.
21      */

22     public static final LockType READ
23                   = new LockType(0, false, "READ");
24     public static final LockType WRITE
25                   = new LockType(1, true, "WRITE");
26     public static final LockType RANGE_READ
27                   = new LockType(2, false, "RANGE_READ");
28     public static final LockType RANGE_WRITE
29                   = new LockType(3, true, "RANGE_WRITE");
30     public static final LockType RANGE_INSERT
31                   = new LockType(4, false, "RANGE_INSERT");
32
33     /**
34      * NONE is used for requesting a dirty read and does not appear in the
35      * conflict or upgrade matrices.
36      */

37     public static final LockType NONE
38                   = new LockType(5, false, "NONE");
39
40     /**
41      * RESTART is used for waiting for a restart and does not appear in the
42      * conflict or upgrade matrices.
43      */

44     public static final LockType RESTART
45                   = new LockType(6, false, "RESTART");
46
47     /**
48      * Whenever the conflict matrix is changed be sure to update this. For
49      * every type that can cause a RESTART result call setCausesRestart. This
50      * could have been determined programmatically but I chose to maintain it
51      * manually to avoid extra code size.
52      */

53     static {
54         RANGE_READ.setCausesRestart();
55         RANGE_WRITE.setCausesRestart();
56     }
57
58     /**
59      * Lock conflict matrix.
60      * @see #getConflict
61      */

62     private static LockConflict[][] conflictMatrix = {
63         { // READ is held and there is a request for:
64
LockConflict.ALLOW, // READ
65
LockConflict.BLOCK, // WRITE
66
LockConflict.ALLOW, // RANGE_READ
67
LockConflict.BLOCK, // RANGE_WRITE
68
LockConflict.ALLOW, // RANGE_INSERT
69
},
70         { // WRITE is held and there is a request for:
71
LockConflict.BLOCK, // READ
72
LockConflict.BLOCK, // WRITE
73
LockConflict.BLOCK, // RANGE_READ
74
LockConflict.BLOCK, // RANGE_WRITE
75
LockConflict.ALLOW, // RANGE_INSERT
76
},
77         { // RANGE_READ is held and there is a request for:
78
LockConflict.ALLOW, // READ
79
LockConflict.BLOCK, // WRITE
80
LockConflict.ALLOW, // RANGE_READ
81
LockConflict.BLOCK, // RANGE_WRITE
82
LockConflict.BLOCK, // RANGE_INSERT
83
},
84         { // RANGE_WRITE is held and there is a request for:
85
LockConflict.BLOCK, // READ
86
LockConflict.BLOCK, // WRITE
87
LockConflict.BLOCK, // RANGE_READ
88
LockConflict.BLOCK, // RANGE_WRITE
89
LockConflict.BLOCK, // RANGE_INSERT
90
},
91         { // RANGE_INSERT is held and there is a request for:
92
LockConflict.ALLOW, // READ
93
LockConflict.ALLOW, // WRITE
94
LockConflict.RESTART, // RANGE_READ
95
LockConflict.RESTART, // RANGE_WRITE
96
LockConflict.ALLOW, // RANGE_INSERT
97
},
98     };
99
100     /**
101      * Lock upgrade matrix.
102      * @see #getUpgrade
103      */

104     private static LockUpgrade[][] upgradeMatrix = {
105         { // READ is held and there is a request for:
106
LockUpgrade.EXISTING, // READ
107
LockUpgrade.WRITE_PROMOTE, // WRITE
108
LockUpgrade.RANGE_READ_IMMED, // RANGE_READ
109
LockUpgrade.RANGE_WRITE_PROMOTE, // RANGE_WRITE
110
LockUpgrade.ILLEGAL, // RANGE_INSERT
111
},
112         { // WRITE is held and there is a request for:
113
LockUpgrade.EXISTING, // READ
114
LockUpgrade.EXISTING, // WRITE
115
LockUpgrade.RANGE_WRITE_IMMED, // RANGE_READ
116
LockUpgrade.RANGE_WRITE_IMMED, // RANGE_WRITE
117
LockUpgrade.ILLEGAL, // RANGE_INSERT
118
},
119         { // RANGE_READ is held and there is a request for:
120
LockUpgrade.EXISTING, // READ
121
LockUpgrade.RANGE_WRITE_PROMOTE, // WRITE
122
LockUpgrade.EXISTING, // RANGE_READ
123
LockUpgrade.RANGE_WRITE_PROMOTE, // RANGE_WRITE
124
LockUpgrade.ILLEGAL, // RANGE_INSERT
125
},
126         { // RANGE_WRITE is held and there is a request for:
127
LockUpgrade.EXISTING, // READ
128
LockUpgrade.EXISTING, // WRITE
129
LockUpgrade.EXISTING, // RANGE_READ
130
LockUpgrade.EXISTING, // RANGE_WRITE
131
LockUpgrade.ILLEGAL, // RANGE_INSERT
132
},
133         { // RANGE_INSERT is held and there is a request for:
134
LockUpgrade.ILLEGAL, // READ
135
LockUpgrade.ILLEGAL, // WRITE
136
LockUpgrade.ILLEGAL, // RANGE_READ
137
LockUpgrade.ILLEGAL, // RANGE_WRITE
138
LockUpgrade.EXISTING, // RANGE_INSERT
139
},
140     };
141
142     private int index;
143     private boolean write;
144     private String JavaDoc name;
145     private boolean causesRestart;
146
147     /**
148      * No lock types can be defined outside this class.
149      */

150     private LockType(int index, boolean write, String JavaDoc name) {
151         this.index = index;
152         this.write = write;
153         this.name = name;
154     }
155
156     /**
157      * Returns true if this is a WRITE or RANGE_WRITE lock. For RANGE_INSERT,
158      * false is returned because RANGE_INSERT is used to lock the key following
159      * the insertion key, not the insertion key itself.
160      */

161     public final boolean isWriteLock() {
162         return write;
163     }
164
165     /**
166      * Specifies that when this type is requested it can result in
167      * LockGrantType.RESTART.
168      */

169     private void setCausesRestart() {
170         causesRestart = true;
171     }
172
173     /**
174      * Returns whether when this type is requested it can result in
175      * LockGrantType.RESTART.
176      */

177     final boolean getCausesRestart() {
178         return causesRestart;
179     }
180
181     /**
182      * Returns the LockConfict that results when this lock type is held and the
183      * given lock type is requested by another locker.
184      */

185     LockConflict getConflict(LockType requestedType) {
186         return conflictMatrix[index][requestedType.index];
187     }
188
189     /**
190      * Returns the LockUpgrade that results when this lock type is held and the
191      * given lock type is requested by the same locker.
192      *
193      * <p>For the returned LockUpgrade object, getIllegal will never return
194      * true because this method fires an assertion if getIllegal returns true.
195      */

196     LockUpgrade getUpgrade(LockType requestedType) {
197         LockUpgrade upgrade = upgradeMatrix[index][requestedType.index];
198         assert !upgrade.getIllegal() : toString() + " to " + requestedType;
199         return upgrade;
200     }
201
202     public String JavaDoc toString() {
203         return name;
204     }
205 }
206
Popular Tags