以太坊数据库RocksDB操作示例

  1. package com.test;  
      
    import java.util.ArrayList;  
    import java.util.List;  
    import java.util.Map;  
      
    import org.rocksdb.ColumnFamilyDescriptor;  
    import org.rocksdb.ColumnFamilyHandle;  
    import org.rocksdb.ColumnFamilyOptions;  
    import org.rocksdb.DBOptions;  
    import org.rocksdb.Options;  
    import org.rocksdb.RocksDB;  
    import org.rocksdb.RocksDBException;  
    import org.rocksdb.RocksIterator;  
      
    public class RocksJavaTest {  
          
        private static final String dbPath = "/Users/xiaolongli/Documents/workspace/rocksdb/rocksdb/java/data/";  
        static {  
            RocksDB.loadLibrary();  
        }  
          
        RocksDB rocksDB;  
          
        public RocksJavaTest() throws RocksDBException {  
              
        }  
          
    //  RocksDB.DEFAULT_COLUMN_FAMILY  
        public void testDefaultColumnFamily() throws RocksDBException {  
            Options options = new Options();  
            options.setCreateIfMissing(true);  
              
            rocksDB = RocksDB.open(options, dbPath);  
            byte[] key = "Hello".getBytes();  
            byte[] value = "World".getBytes();  
            rocksDB.put(key, value);  
              
            List<byte[]> cfs = RocksDB.listColumnFamilies(options, dbPath);  
            for(byte[] cf : cfs) {  
                System.out.println(new String(cf));  
            }  
              
            byte[] getValue = rocksDB.get(key);  
            System.out.println(new String(getValue));  
              
            rocksDB.put("SecondKey".getBytes(), "SecondValue".getBytes());  
              
            List<byte[]> keys = new ArrayList<>();  
            keys.add(key);  
            keys.add("SecondKey".getBytes());  
              
            Map<byte[], byte[]> valueMap = rocksDB.multiGet(keys);  
            for(Map.Entry<byte[], byte[]> entry : valueMap.entrySet()) {  
                System.out.println(new String(entry.getKey()) + ":" + new String(entry.getValue()));  
            }  
              
            RocksIterator iter = rocksDB.newIterator();  
            for(iter.seekToFirst(); iter.isValid(); iter.next()) {  
                System.out.println("iter key:" + new String(iter.key()) + ", iter value:" + new String(iter.value()));  
            }  
              
            rocksDB.remove(key);  
            System.out.println("after remove key:" + new String(key));  
              
            iter = rocksDB.newIterator();  
            for(iter.seekToFirst(); iter.isValid(); iter.next()) {  
                System.out.println("iter key:" + new String(iter.key()) + ", iter value:" + new String(iter.value()));  
            }  
              
        }  
          
        public void testCertainColumnFamily() throws RocksDBException {  
            String table = "CertainColumnFamilyTest";  
            String key = "certainKey";  
            String value = "certainValue";  
              
            List<ColumnFamilyDescriptor> columnFamilyDescriptors = new ArrayList<>();  
            Options options = new Options();  
            options.setCreateIfMissing(true);  
              
            List<byte[]> cfs = RocksDB.listColumnFamilies(options, dbPath);  
            if(cfs.size() > 0) {  
                for(byte[] cf : cfs) {  
                    columnFamilyDescriptors.add(new ColumnFamilyDescriptor(cf, new ColumnFamilyOptions()));  
                }  
            } else {  
                columnFamilyDescriptors.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, new ColumnFamilyOptions()));  
            }  
              
            List<ColumnFamilyHandle> columnFamilyHandles = new ArrayList<>();  
            DBOptions dbOptions = new DBOptions();  
            dbOptions.setCreateIfMissing(true);  
              
            rocksDB = RocksDB.open(dbOptions, dbPath, columnFamilyDescriptors, columnFamilyHandles);  
            for(int i = 0; i < columnFamilyDescriptors.size(); i++) {  
                if(new String(columnFamilyDescriptors.get(i).columnFamilyName()).equals(table)) {  
                    rocksDB.dropColumnFamily(columnFamilyHandles.get(i));  
                }  
            }  
              
            ColumnFamilyHandle columnFamilyHandle = rocksDB.createColumnFamily(new ColumnFamilyDescriptor(table.getBytes(), new ColumnFamilyOptions()));  
            rocksDB.put(columnFamilyHandle, key.getBytes(), value.getBytes());  
              
            byte[] getValue = rocksDB.get(columnFamilyHandle, key.getBytes());  
            System.out.println("get Value : " + new String(getValue));  
              
            rocksDB.put(columnFamilyHandle, "SecondKey".getBytes(), "SecondValue".getBytes());  
              
            List<byte[]> keys = new ArrayList<byte[]>();  
            keys.add(key.getBytes());  
            keys.add("SecondKey".getBytes());  
              
            List<ColumnFamilyHandle> handleList = new ArrayList<>();  
            handleList.add(columnFamilyHandle);  
            handleList.add(columnFamilyHandle);  
              
            Map<byte[], byte[]> multiGet = rocksDB.multiGet(handleList, keys);  
            for(Map.Entry<byte[], byte[]> entry : multiGet.entrySet()) {  
                System.out.println(new String(entry.getKey()) + "--" + new String(entry.getValue()));  
            }  
              
            rocksDB.remove(columnFamilyHandle, key.getBytes());  
              
            RocksIterator iter = rocksDB.newIterator(columnFamilyHandle);  
            for(iter.seekToFirst(); iter.isValid(); iter.next()) {  
                System.out.println(new String(iter.key()) + ":" + new String(iter.value()));   
            }  
        }  
      
        public static void main(String[] args) throws RocksDBException {  
            RocksJavaTest test = new RocksJavaTest();  
    //      test.testDefaultColumnFamily();  
            test.testCertainColumnFamily();  
        }  
      
    }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值