1414 * See the License for the specific language governing permissions and
1515 * limitations under the License.
1616 */
17- package org .apache .rocketmq .broker ;
17+ package org .apache .rocketmq .broker . config . v1 ;
1818
1919import com .alibaba .fastjson .JSON ;
20+ import java .nio .charset .Charset ;
2021import java .nio .charset .StandardCharsets ;
2122import java .util .function .BiConsumer ;
2223import org .apache .commons .lang3 .StringUtils ;
2728import org .apache .rocketmq .remoting .protocol .DataVersion ;
2829import org .rocksdb .CompressionType ;
2930import org .rocksdb .FlushOptions ;
30- import org .rocksdb .RocksIterator ;
31+ import org .rocksdb .RocksDB ;
32+ import org .rocksdb .RocksDBException ;
3133import org .rocksdb .Statistics ;
3234import org .rocksdb .WriteBatch ;
3335
3436public class RocksDBConfigManager {
3537 protected static final Logger BROKER_LOG = LoggerFactory .getLogger (LoggerName .BROKER_LOGGER_NAME );
38+
39+ public static final Charset CHARSET = StandardCharsets .UTF_8 ;
40+
3641 public volatile boolean isStop = false ;
3742 public ConfigRocksDBStorage configRocksDBStorage = null ;
3843 private FlushOptions flushOptions = null ;
@@ -42,21 +47,44 @@ public class RocksDBConfigManager {
4247 private final CompressionType compressionType ;
4348 private DataVersion kvDataVersion = new DataVersion ();
4449
50+ public static final byte [] KV_DATA_VERSION_COLUMN_FAMILY_NAME = "kvDataVersion" .getBytes (CHARSET );
51+ public static final byte [] KV_DATA_VERSION_KEY = "kvDataVersionKey" .getBytes (CHARSET );
52+
53+ private final String defaultCF ;
54+ private final String versionCF ;
55+
56+
57+ public RocksDBConfigManager (String filePath , long memTableFlushInterval , CompressionType compressionType ,
58+ String defaultCF , String versionCF ) {
59+ this .filePath = filePath ;
60+ this .memTableFlushInterval = memTableFlushInterval ;
61+ this .compressionType = compressionType ;
62+ this .defaultCF = defaultCF ;
63+ this .versionCF = versionCF ;
64+ }
65+
4566 public RocksDBConfigManager (String filePath , long memTableFlushInterval , CompressionType compressionType ) {
4667 this .filePath = filePath ;
4768 this .memTableFlushInterval = memTableFlushInterval ;
4869 this .compressionType = compressionType ;
70+ this .defaultCF = new String (RocksDB .DEFAULT_COLUMN_FAMILY , CHARSET );
71+ this .versionCF = new String (KV_DATA_VERSION_COLUMN_FAMILY_NAME , CHARSET );
4972 }
5073
51- public boolean init () {
74+ public boolean init (boolean readOnly ) {
5275 this .isStop = false ;
53- this .configRocksDBStorage = new ConfigRocksDBStorage (filePath , compressionType );
76+ this .configRocksDBStorage = ConfigRocksDBStorage . getStore (filePath , readOnly , compressionType );
5477 return this .configRocksDBStorage .start ();
5578 }
79+
80+ public boolean init () {
81+ return this .init (false );
82+ }
83+
5684 public boolean loadDataVersion () {
5785 String currDataVersionString = null ;
5886 try {
59- byte [] dataVersion = this .configRocksDBStorage .getKvDataVersion ( );
87+ byte [] dataVersion = this .configRocksDBStorage .get ( versionCF , KV_DATA_VERSION_KEY );
6088 if (dataVersion != null && dataVersion .length > 0 ) {
6189 currDataVersionString = new String (dataVersion , StandardCharsets .UTF_8 );
6290 }
@@ -68,12 +96,11 @@ public boolean loadDataVersion() {
6896 }
6997
7098 public boolean loadData (BiConsumer <byte [], byte []> biConsumer ) {
71- try (RocksIterator iterator = this .configRocksDBStorage .iterator ()) {
72- iterator .seekToFirst ();
73- while (iterator .isValid ()) {
74- biConsumer .accept (iterator .key (), iterator .value ());
75- iterator .next ();
76- }
99+ try {
100+ configRocksDBStorage .iterate (this .defaultCF , biConsumer );
101+ } catch (Exception e ) {
102+ BROKER_LOG .error ("RocksDBConfigManager loadData failed" , e );
103+ return false ;
77104 }
78105
79106 this .flushOptions = new FlushOptions ();
@@ -87,9 +114,7 @@ public void start() {
87114
88115 public boolean stop () {
89116 this .isStop = true ;
90- if (this .configRocksDBStorage != null ) {
91- return this .configRocksDBStorage .shutdown ();
92- }
117+ ConfigRocksDBStorage .shutdown (filePath );
93118 if (this .flushOptions != null ) {
94119 this .flushOptions .close ();
95120 }
@@ -115,28 +140,38 @@ public void flushWAL() {
115140 }
116141 }
117142
118- public void put (final byte [] keyBytes , final int keyLen , final byte [] valueBytes ) throws Exception {
119- this .configRocksDBStorage .put (keyBytes , keyLen , valueBytes );
143+ public void put (final byte [] keyBytes , final byte [] valueBytes ) throws Exception {
144+ this .configRocksDBStorage .put (defaultCF , keyBytes , keyBytes .length , valueBytes );
145+ }
146+
147+ public void put (String cf , String key , String value ) throws Exception {
148+ byte [] keyBytes = key .getBytes (CHARSET );
149+ this .configRocksDBStorage .put (cf , keyBytes , keyBytes .length , value .getBytes (CHARSET ));
150+ }
151+
152+ public void put (String cf , final byte [] keyBytes , final byte [] valueBytes ) throws Exception {
153+ this .configRocksDBStorage .put (cf , keyBytes , keyBytes .length , valueBytes );
120154 }
121155
122156 public void delete (final byte [] keyBytes ) throws Exception {
123- this .configRocksDBStorage .delete (keyBytes );
157+ this .configRocksDBStorage .delete (defaultCF , keyBytes );
124158 }
125159
126160 public void updateKvDataVersion () throws Exception {
127161 kvDataVersion .nextVersion ();
128- this .configRocksDBStorage .updateKvDataVersion (JSON .toJSONString (kvDataVersion ).getBytes (StandardCharsets .UTF_8 ));
162+ this .configRocksDBStorage .put (versionCF , KV_DATA_VERSION_KEY , KV_DATA_VERSION_KEY .length ,
163+ JSON .toJSONString (kvDataVersion ).getBytes (StandardCharsets .UTF_8 ));
129164 }
130165
131166 public DataVersion getKvDataVersion () {
132167 return kvDataVersion ;
133168 }
134169
135- public void updateForbidden (String key , String value ) throws Exception {
136- this .configRocksDBStorage .updateForbidden (key .getBytes (StandardCharsets .UTF_8 ), value .getBytes (StandardCharsets .UTF_8 ));
170+ // batch operations
171+ public void writeBatchPutOperation (WriteBatch writeBatch , final byte [] key , final byte [] value ) throws RocksDBException {
172+ configRocksDBStorage .writeBatchPutOperation (defaultCF , writeBatch , key , value );
137173 }
138174
139-
140175 public void batchPutWithWal (final WriteBatch batch ) throws Exception {
141176 this .configRocksDBStorage .batchPutWithWal (batch );
142177 }
0 commit comments