This is an automated email from the ASF dual-hosted git repository.

jermy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-hugegraph.git


The following commit(s) were added to refs/heads/master by this push:
     new 09619597d refactor: improve rocksdb code (#2002)
09619597d is described below

commit 09619597d660e62791d351968df87bfa68a2c53f
Author: Jade Peng <[email protected]>
AuthorDate: Thu Nov 3 23:47:32 2022 +0800

    refactor: improve rocksdb code (#2002)
    
    Co-authored-by: jadepeng <[email protected]>
---
 .../backend/serializer/BinarySerializer.java       | 10 +++---
 .../backend/store/rocksdb/RocksDBStdSessions.java  | 10 +++---
 .../backend/store/rocksdb/RocksDBStore.java        | 37 +++++++++++-----------
 .../backend/store/rocksdb/RocksDBTable.java        |  7 ++--
 .../backend/store/rocksdb/RocksDBTables.java       |  2 +-
 .../store/rocksdbsst/RocksDBSstSessions.java       |  4 +--
 6 files changed, 35 insertions(+), 35 deletions(-)

diff --git 
a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/BinarySerializer.java
 
b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/BinarySerializer.java
index 4702d3a97..9bf6c7842 100644
--- 
a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/BinarySerializer.java
+++ 
b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/serializer/BinarySerializer.java
@@ -924,26 +924,26 @@ public class BinarySerializer extends AbstractSerializer {
         return false;
     }
 
-    public static final byte[] increaseOne(byte[] bytes) {
+    public static void increaseOne(byte[] bytes) {
         final byte BYTE_MAX_VALUE = (byte) 0xff;
+        final byte INCREASE_STEP = 0x01;
         assert bytes.length > 0;
         byte last = bytes[bytes.length - 1];
         if (last != BYTE_MAX_VALUE) {
-            bytes[bytes.length - 1] += 0x01;
+            bytes[bytes.length - 1] += INCREASE_STEP;
         } else {
             // Process overflow (like [1, 255] => [2, 0])
             int i = bytes.length - 1;
             for (; i > 0 && bytes[i] == BYTE_MAX_VALUE; --i) {
-                bytes[i] += 0x01;
+                bytes[i] += INCREASE_STEP;
             }
             if (bytes[i] == BYTE_MAX_VALUE) {
                 assert i == 0;
                 throw new BackendException("Unable to increase bytes: %s",
                                            Bytes.toHex(bytes));
             }
-            bytes[i] += 0x01;
+            bytes[i] += INCREASE_STEP;
         }
-        return bytes;
     }
 
     @Override
diff --git 
a/hugegraph-rocksdb/src/main/java/com/baidu/hugegraph/backend/store/rocksdb/RocksDBStdSessions.java
 
b/hugegraph-rocksdb/src/main/java/com/baidu/hugegraph/backend/store/rocksdb/RocksDBStdSessions.java
index 06652152c..81eb76836 100644
--- 
a/hugegraph-rocksdb/src/main/java/com/baidu/hugegraph/backend/store/rocksdb/RocksDBStdSessions.java
+++ 
b/hugegraph-rocksdb/src/main/java/com/baidu/hugegraph/backend/store/rocksdb/RocksDBStdSessions.java
@@ -744,11 +744,11 @@ public class RocksDBStdSessions extends RocksDBSessions {
         return tableConfig;
     }
 
-    public static final byte[] encode(String string) {
+    public static byte[] encode(String string) {
         return StringEncoding.encode(string);
     }
 
-    public static final String decode(byte[] bytes) {
+    public static String decode(byte[] bytes) {
         return StringEncoding.decode(bytes);
     }
 
@@ -758,7 +758,7 @@ public class RocksDBStdSessions extends RocksDBSessions {
     private final class StdSession extends RocksDBSessions.Session {
 
         private WriteBatch batch;
-        private WriteOptions writeOptions;
+        private final WriteOptions writeOptions;
 
         public StdSession(HugeConfig conf) {
             this.batch = new WriteBatch();
@@ -955,8 +955,8 @@ public class RocksDBStdSessions extends RocksDBSessions {
         @Override
         public void deletePrefix(String table, byte[] key) {
             byte[] keyFrom = key;
-            byte[] keyTo = Arrays.copyOf(key, key.length);
-            keyTo = BinarySerializer.increaseOne(keyTo);
+            byte[] keyTo = Arrays.copyOf(keyFrom, keyFrom.length);
+            BinarySerializer.increaseOne(keyTo);
             try (CFHandle cf = cf(table)) {
                 this.batch.deleteRange(cf.get(), keyFrom, keyTo);
             } catch (RocksDBException e) {
diff --git 
a/hugegraph-rocksdb/src/main/java/com/baidu/hugegraph/backend/store/rocksdb/RocksDBStore.java
 
b/hugegraph-rocksdb/src/main/java/com/baidu/hugegraph/backend/store/rocksdb/RocksDBStore.java
index c55d92e33..7b6137203 100644
--- 
a/hugegraph-rocksdb/src/main/java/com/baidu/hugegraph/backend/store/rocksdb/RocksDBStore.java
+++ 
b/hugegraph-rocksdb/src/main/java/com/baidu/hugegraph/backend/store/rocksdb/RocksDBStore.java
@@ -26,6 +26,7 @@ import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
@@ -182,7 +183,7 @@ public abstract class RocksDBStore extends 
AbstractBackendStore<Session> {
     }
 
     protected List<String> tableNames(HugeType type) {
-        return type != HugeType.OLAP ? Arrays.asList(this.table(type).table()) 
:
+        return type != HugeType.OLAP ? 
Collections.singletonList(this.table(type).table()) :
                                        this.olapTables();
     }
 
@@ -287,7 +288,7 @@ public abstract class RocksDBStore extends 
AbstractBackendStore<Session> {
                                        "open-pool");
         }
 
-        boolean terminated = false;
+        boolean terminated;
         openPool.shutdown();
         try {
             terminated = openPool.awaitTermination(OPEN_TIMEOUT,
@@ -803,24 +804,11 @@ public abstract class RocksDBStore extends 
AbstractBackendStore<Session> {
         }
     }
 
-    private void closeSessions() {
-        Iterator<Map.Entry<String, RocksDBSessions>> iter = this.dbs.entrySet()
-                                                                    
.iterator();
-        while (iter.hasNext()) {
-            Map.Entry<String, RocksDBSessions> entry = iter.next();
-            RocksDBSessions sessions = entry.getValue();
-            boolean closed = sessions.close();
-            if (closed) {
-                iter.remove();
-            }
-        }
-    }
-
     private List<Session> session() {
         this.checkOpened();
 
         if (this.tableDiskMapping.isEmpty()) {
-            return Arrays.asList(this.sessions.session());
+            return Collections.singletonList(this.sessions.session());
         }
 
         // Collect session of each table with optimized disk
@@ -832,6 +820,19 @@ public abstract class RocksDBStore extends 
AbstractBackendStore<Session> {
         return list;
     }
 
+    private void closeSessions() {
+        Iterator<Map.Entry<String, RocksDBSessions>> iter = this.dbs.entrySet()
+                                                                    
.iterator();
+        while (iter.hasNext()) {
+            Map.Entry<String, RocksDBSessions> entry = iter.next();
+            RocksDBSessions sessions = entry.getValue();
+            boolean closed = sessions.close();
+            if (closed) {
+                iter.remove();
+            }
+        }
+    }
+
     private Collection<RocksDBSessions> sessions() {
         return this.dbs.values();
     }
@@ -1094,7 +1095,7 @@ public abstract class RocksDBStore extends 
AbstractBackendStore<Session> {
             String name = this.olapTableName(id);
             RocksDBTable table = this.table(name);
             RocksDBSessions db = this.db(HugeType.OLAP);
-            if (table == null || !db.existsTable(table.table())) {
+            if (!db.existsTable(table.table())) {
                 throw new HugeException("Not exist table '%s''", name);
             }
             this.dropTable(db, table.table());
@@ -1106,7 +1107,7 @@ public abstract class RocksDBStore extends 
AbstractBackendStore<Session> {
             String name = this.olapTableName(id);
             RocksDBTable table = this.table(name);
             RocksDBSessions db = this.db(HugeType.OLAP);
-            if (table == null || !db.existsTable(table.table())) {
+            if (!db.existsTable(table.table())) {
                 throw new HugeException("Not exist table '%s''", name);
             }
             this.dropTable(db, table.table());
diff --git 
a/hugegraph-rocksdb/src/main/java/com/baidu/hugegraph/backend/store/rocksdb/RocksDBTable.java
 
b/hugegraph-rocksdb/src/main/java/com/baidu/hugegraph/backend/store/rocksdb/RocksDBTable.java
index e87316b8e..c173fac70 100644
--- 
a/hugegraph-rocksdb/src/main/java/com/baidu/hugegraph/backend/store/rocksdb/RocksDBTable.java
+++ 
b/hugegraph-rocksdb/src/main/java/com/baidu/hugegraph/backend/store/rocksdb/RocksDBTable.java
@@ -291,9 +291,8 @@ public class RocksDBTable extends BackendTable<Session, 
BackendEntry> {
         return false;
     }
 
-    protected static final BackendEntryIterator newEntryIterator(
-                                                BackendColumnIterator cols,
-                                                Query query) {
+    protected static BackendEntryIterator 
newEntryIterator(BackendColumnIterator cols,
+                                                           Query query) {
         return new BinaryEntryIterator<>(cols, query, (entry, col) -> {
             if (entry == null || !entry.belongToMe(col)) {
                 HugeType type = query.resultType();
@@ -307,7 +306,7 @@ public class RocksDBTable extends BackendTable<Session, 
BackendEntry> {
         });
     }
 
-    protected static final long sizeOfBackendEntry(BackendEntry entry) {
+    protected static long sizeOfBackendEntry(BackendEntry entry) {
         return BinaryEntryIterator.sizeOfEntry(entry);
     }
 
diff --git 
a/hugegraph-rocksdb/src/main/java/com/baidu/hugegraph/backend/store/rocksdb/RocksDBTables.java
 
b/hugegraph-rocksdb/src/main/java/com/baidu/hugegraph/backend/store/rocksdb/RocksDBTables.java
index c4b69d385..7d399dff1 100644
--- 
a/hugegraph-rocksdb/src/main/java/com/baidu/hugegraph/backend/store/rocksdb/RocksDBTables.java
+++ 
b/hugegraph-rocksdb/src/main/java/com/baidu/hugegraph/backend/store/rocksdb/RocksDBTables.java
@@ -334,7 +334,7 @@ public class RocksDBTables {
             E.checkArgumentNotNull(min, "Range index begin key is missing");
             byte[] begin = min.asBytes();
             if (!minEq) {
-                begin = BinarySerializer.increaseOne(begin);
+                BinarySerializer.increaseOne(begin);
             }
 
             if (max == null) {
diff --git 
a/hugegraph-rocksdb/src/main/java/com/baidu/hugegraph/backend/store/rocksdbsst/RocksDBSstSessions.java
 
b/hugegraph-rocksdb/src/main/java/com/baidu/hugegraph/backend/store/rocksdbsst/RocksDBSstSessions.java
index 69e9951db..fc31b7e28 100644
--- 
a/hugegraph-rocksdb/src/main/java/com/baidu/hugegraph/backend/store/rocksdbsst/RocksDBSstSessions.java
+++ 
b/hugegraph-rocksdb/src/main/java/com/baidu/hugegraph/backend/store/rocksdbsst/RocksDBSstSessions.java
@@ -184,7 +184,7 @@ public class RocksDBSstSessions extends RocksDBSessions {
     }
 
     @Override
-    public void reloadRocksDB() throws RocksDBException {
+    public void reloadRocksDB() {
         throw new UnsupportedOperationException("reloadRocksDB");
     }
 
@@ -235,7 +235,7 @@ public class RocksDBSstSessions extends RocksDBSessions {
      */
     private final class SstSession extends Session {
 
-        private Map<String, Changes> batch;
+        private final Map<String, Changes> batch;
 
         public SstSession() {
             this.batch = new HashMap<>();

Reply via email to