13
13
* along with this program. If not, see <http://www.gnu.org/licenses/>.
14
14
*/
15
15
package org .tron .storage .leveldb ;
16
+
16
17
import org .iq80 .leveldb .*;
17
18
import org .slf4j .Logger ;
18
19
import org .slf4j .LoggerFactory ;
@@ -47,7 +48,6 @@ public class LevelDbDataSourceImpl implements DbSourceInter<byte[]> {
47
48
boolean alive ;
48
49
49
50
50
-
51
51
private ReadWriteLock resetDbLock = new ReentrantReadWriteLock ();
52
52
53
53
public LevelDbDataSourceImpl () {
@@ -143,14 +143,11 @@ public String getDBName() {
143
143
@ Override
144
144
public byte [] getData (byte [] key ) {
145
145
resetDbLock .readLock ().lock ();
146
-
147
- try {
148
- return database .get (key );
149
- } catch (DBException e ) {
150
- byte [] ret = database .get (key );
151
- return ret ;
152
- }
153
- finally {
146
+ try {
147
+ return database .get (key );
148
+ } catch (DBException e ) {
149
+ return database .get (key );
150
+ } finally {
154
151
resetDbLock .readLock ().unlock ();
155
152
}
156
153
}
@@ -178,17 +175,14 @@ public void deleteData(byte[] key) {
178
175
@ Override
179
176
public Set <byte []> allKeys () {
180
177
resetDbLock .readLock ().lock ();
181
- try {
182
- try (DBIterator iterator = database .iterator ()) {
183
- Set <byte []> result = new HashSet <>();
184
- for (iterator .seekToFirst (); iterator .hasNext (); iterator .next ()) {
185
- result .add (iterator .peekNext ().getKey ());
186
- }
187
-
188
- return result ;
189
- } catch (IOException e ) {
190
- throw new RuntimeException (e );
178
+ try (DBIterator iterator = database .iterator ()) {
179
+ Set <byte []> result = new HashSet <>();
180
+ for (iterator .seekToFirst (); iterator .hasNext (); iterator .next ()) {
181
+ result .add (iterator .peekNext ().getKey ());
191
182
}
183
+ return result ;
184
+ } catch (IOException e ) {
185
+ throw new RuntimeException (e );
192
186
} finally {
193
187
resetDbLock .readLock ().unlock ();
194
188
}
@@ -211,14 +205,12 @@ private void updateByBatchInner(Map<byte[], byte[]> rows) throws Exception {
211
205
public void updateByBatch (Map <byte [], byte []> rows ) {
212
206
resetDbLock .readLock ().lock ();
213
207
try {
208
+ updateByBatchInner (rows );
209
+ } catch (Exception e ) {
214
210
try {
215
211
updateByBatchInner (rows );
216
- } catch (Exception e ) {
217
- try {
218
- updateByBatchInner (rows );
219
- } catch (Exception e1 ) {
220
- throw new RuntimeException (e );
221
- }
212
+ } catch (Exception e1 ) {
213
+ throw new RuntimeException (e );
222
214
}
223
215
} finally {
224
216
resetDbLock .readLock ().unlock ();
@@ -233,15 +225,12 @@ public boolean flush() {
233
225
@ Override
234
226
public void closeDB () {
235
227
resetDbLock .writeLock ().lock ();
228
+ if (!isAlive ()) return ;
236
229
try {
237
- if (!isAlive ()) return ;
238
-
239
- try {
240
- database .close ();
241
- alive = false ;
242
- } catch (IOException e ) {
243
- logger .error ("Failed to find the dbStore file on the closeDB: {} " , dataBaseName );
244
- }
230
+ database .close ();
231
+ alive = false ;
232
+ } catch (IOException e ) {
233
+ logger .error ("Failed to find the dbStore file on the closeDB: {} " , dataBaseName );
245
234
} finally {
246
235
resetDbLock .writeLock ().unlock ();
247
236
}
0 commit comments