1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.core.partition.impl.btree.jdbm;
21
22
23 import jdbm.RecordManager;
24 import jdbm.helper.LongSerializer;
25 import jdbm.helper.Serializer;
26 import jdbm.helper.StringComparator;
27
28 import org.apache.directory.server.xdbm.MasterTable;
29 import org.apache.directory.server.core.entry.ServerEntrySerializer;
30 import org.apache.directory.server.schema.SerializableComparator;
31 import org.apache.directory.server.schema.registries.Registries;
32
33
34
35
36
37
38
39
40 public class JdbmMasterTable<E> extends JdbmTable<Long,E> implements MasterTable<E>
41 {
42 private static final StringComparator STRCOMP = new StringComparator();
43
44
45 private static final SerializableComparator<Long> LONG_COMPARATOR =
46 new SerializableComparator<Long>( "1.3.6.1.4.1.18060.0.4.1.1.2" )
47 {
48 private static final long serialVersionUID = 4048791282048841016L;
49
50
51 public int compare( Long o1, Long o2 )
52 {
53 if ( o1 == null )
54 {
55 throw new IllegalArgumentException( "Argument 'obj1' is null" );
56 }
57 else if ( o2 == null )
58 {
59 throw new IllegalArgumentException( "Argument 'obj2' is null" );
60 }
61
62 if ( o1 == ( long ) o2 )
63 {
64 return 0;
65 }
66
67 if ( o1 == ( long ) o2 )
68 {
69 return 0;
70 }
71
72 if ( o1 >= 0 )
73 {
74 if ( o2 >= 0 )
75 {
76 return ( o1 > ( long ) o2 ) ? 1 : -1;
77 }
78 else
79 {
80 return -1;
81 }
82 }
83 else if ( o2 >= 0 )
84 {
85 return 1;
86 }
87 else
88 {
89 return ( o1 < ( long ) o2 ) ? -1 : 1;
90 }
91 }
92 };
93
94
95 private static final SerializableComparator<String> STRING_COMPARATOR =
96 new SerializableComparator<String>( "1.3.6.1.4.1.18060.0.4.1.1.3" )
97 {
98 private static final long serialVersionUID = 3258689922792961845L;
99
100
101 public int compare( String o1, String o2 )
102 {
103 return STRCOMP.compare( o1, o2 );
104 }
105 };
106
107
108 protected final JdbmTable<String,String> adminTbl;
109
110
111
112
113
114
115
116
117
118 public JdbmMasterTable( RecordManager recMan, Registries registries ) throws Exception
119 {
120 super( DBF, recMan, LONG_COMPARATOR, LongSerializer.INSTANCE, new ServerEntrySerializer( registries ) );
121 adminTbl = new JdbmTable<String,String>( "admin", recMan, STRING_COMPARATOR, null, null );
122 String seqValue = adminTbl.get( SEQPROP_KEY );
123
124 if ( null == seqValue )
125 {
126 adminTbl.put( SEQPROP_KEY, "0" );
127 }
128 }
129
130
131 protected JdbmMasterTable( RecordManager recMan, String dbName, Serializer serializer ) throws Exception
132 {
133 super( DBF, recMan, LONG_COMPARATOR, LongSerializer.INSTANCE, serializer );
134 adminTbl = new JdbmTable<String,String>( dbName, recMan, STRING_COMPARATOR, null, null );
135 String seqValue = adminTbl.get( SEQPROP_KEY );
136
137 if ( null == seqValue )
138 {
139 adminTbl.put( SEQPROP_KEY, "0" );
140 }
141 }
142
143
144
145
146
147
148
149
150 public E get( Long id ) throws Exception
151 {
152 return super.get( id );
153 }
154
155
156
157
158
159
160
161
162
163
164
165 public void put( Long id, E entry ) throws Exception
166 {
167 super.put( id, entry );
168 }
169
170
171
172
173
174
175
176
177 public void delete( Long id ) throws Exception
178 {
179 super.remove( id );
180 }
181
182
183 public Long getCurrentId() throws Exception
184 {
185 Long id;
186
187 synchronized ( adminTbl )
188 {
189 id = new Long( adminTbl.get( SEQPROP_KEY ) );
190 }
191
192 return id;
193 }
194
195
196
197
198
199
200
201
202
203
204
205
206 public Long getNextId() throws Exception
207 {
208 Long nextVal;
209 Long lastVal;
210
211 synchronized ( adminTbl )
212 {
213 lastVal = new Long( adminTbl.get( SEQPROP_KEY ) );
214 nextVal = lastVal + 1L;
215 adminTbl.put( SEQPROP_KEY, nextVal.toString() );
216 }
217
218 return nextVal;
219 }
220
221
222
223
224
225
226
227
228
229 public String getProperty( String property ) throws Exception
230 {
231 synchronized ( adminTbl )
232 {
233 return adminTbl.get( property );
234 }
235 }
236
237
238
239
240
241
242
243
244
245 public void setProperty( String property, String value ) throws Exception
246 {
247 synchronized ( adminTbl )
248 {
249 adminTbl.put( property, value );
250 }
251 }
252 }