Search in sources :

Example 1 with CFHandle

use of com.baidu.hugegraph.backend.store.rocksdb.OpenedRocksDB.CFHandle in project incubator-hugegraph by apache.

the class RocksDBStdSessions method openRocksDB.

private static OpenedRocksDB openRocksDB(HugeConfig config, List<String> cfNames, String dataPath, String walPath) throws RocksDBException {
    // Old CFs should always be opened
    Set<String> mergedCFs = RocksDBStdSessions.mergeOldCFs(dataPath, cfNames);
    List<String> cfs = ImmutableList.copyOf(mergedCFs);
    // Init CFs options
    List<ColumnFamilyDescriptor> cfds = new ArrayList<>(cfs.size());
    for (String cf : cfs) {
        ColumnFamilyDescriptor cfd = new ColumnFamilyDescriptor(encode(cf));
        ColumnFamilyOptions options = cfd.getOptions();
        RocksDBStdSessions.initOptions(config, null, null, options, options);
        cfds.add(cfd);
    }
    // Init DB options
    DBOptions options = new DBOptions();
    RocksDBStdSessions.initOptions(config, options, options, null, null);
    if (walPath != null) {
        options.setWalDir(walPath);
    }
    SstFileManager sstFileManager = new SstFileManager(Env.getDefault());
    options.setSstFileManager(sstFileManager);
    // Open RocksDB with CFs
    List<ColumnFamilyHandle> cfhs = new ArrayList<>();
    RocksDB rocksdb = RocksDB.open(options, dataPath, cfds, cfhs);
    E.checkState(cfhs.size() == cfs.size(), "Expect same size of cf-handles and cf-names");
    // Collect CF Handles
    Map<String, CFHandle> cfHandles = new ConcurrentHashMap<>();
    for (int i = 0; i < cfs.size(); i++) {
        cfHandles.put(cfs.get(i), new CFHandle(rocksdb, cfhs.get(i)));
    }
    return new OpenedRocksDB(rocksdb, cfHandles, sstFileManager);
}
Also used : CFHandle(com.baidu.hugegraph.backend.store.rocksdb.OpenedRocksDB.CFHandle) RocksDB(org.rocksdb.RocksDB) ArrayList(java.util.ArrayList) ColumnFamilyDescriptor(org.rocksdb.ColumnFamilyDescriptor) ColumnFamilyHandle(org.rocksdb.ColumnFamilyHandle) ColumnFamilyOptions(org.rocksdb.ColumnFamilyOptions) DBOptions(org.rocksdb.DBOptions) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SstFileManager(org.rocksdb.SstFileManager)

Example 2 with CFHandle

use of com.baidu.hugegraph.backend.store.rocksdb.OpenedRocksDB.CFHandle in project incubator-hugegraph by apache.

the class RocksDBStdSessions method ingestExternalFile.

private void ingestExternalFile() throws RocksDBException {
    String directory = this.config().get(RocksDBOptions.SST_PATH);
    if (directory == null || directory.isEmpty()) {
        return;
    }
    RocksDBIngester ingester = new RocksDBIngester(this.rocksdb());
    // Ingest all *.sst files in each directory named cf name
    for (String cf : this.rocksdb.cfs()) {
        Path path = Paths.get(directory, cf);
        if (path.toFile().isDirectory()) {
            try (CFHandle cfh = this.cf(cf)) {
                ingester.ingest(path, cfh.get());
            }
        }
    }
}
Also used : Path(java.nio.file.Path) CFHandle(com.baidu.hugegraph.backend.store.rocksdb.OpenedRocksDB.CFHandle)

Example 3 with CFHandle

use of com.baidu.hugegraph.backend.store.rocksdb.OpenedRocksDB.CFHandle in project incubator-hugegraph by apache.

the class RocksDBStdSessions method createTable.

@Override
public synchronized void createTable(String... tables) throws RocksDBException {
    this.checkValid();
    List<ColumnFamilyDescriptor> cfds = new ArrayList<>();
    for (String table : tables) {
        if (this.rocksdb.existCf(table)) {
            continue;
        }
        ColumnFamilyDescriptor cfd = new ColumnFamilyDescriptor(encode(table));
        ColumnFamilyOptions options = cfd.getOptions();
        initOptions(this.config(), null, null, options, options);
        cfds.add(cfd);
    }
    /*
         * To speed up the creation of tables, like truncate() for tinkerpop
         * test, we call createColumnFamilies instead of createColumnFamily.
         */
    List<ColumnFamilyHandle> cfhs = this.rocksdb().createColumnFamilies(cfds);
    for (ColumnFamilyHandle cfh : cfhs) {
        String table = decode(cfh.getName());
        this.rocksdb.addCf(table, new CFHandle(this.rocksdb(), cfh));
    }
    this.ingestExternalFile();
}
Also used : ColumnFamilyOptions(org.rocksdb.ColumnFamilyOptions) CFHandle(com.baidu.hugegraph.backend.store.rocksdb.OpenedRocksDB.CFHandle) ArrayList(java.util.ArrayList) ColumnFamilyDescriptor(org.rocksdb.ColumnFamilyDescriptor) ColumnFamilyHandle(org.rocksdb.ColumnFamilyHandle)

Example 4 with CFHandle

use of com.baidu.hugegraph.backend.store.rocksdb.OpenedRocksDB.CFHandle in project incubator-hugegraph by apache.

the class RocksDBStdSessions method dropTable.

@Override
public synchronized void dropTable(String... tables) throws RocksDBException {
    this.checkValid();
    /*
         * May cause bug to drop CF when someone is reading or writing this CF,
         * use CFHandle to wait for others and then do drop:
         * https://github.com/hugegraph/hugegraph/issues/697
         */
    List<ColumnFamilyHandle> cfhs = new ArrayList<>();
    for (String table : tables) {
        CFHandle cfh = this.rocksdb.cf(table);
        if (cfh == null) {
            continue;
        }
        cfhs.add(cfh.waitForDrop());
    }
    /*
         * To speed up the creation of tables, like truncate() for tinkerpop
         * test, we call dropColumnFamilies instead of dropColumnFamily.
         */
    this.rocksdb().dropColumnFamilies(cfhs);
    for (String table : tables) {
        CFHandle cfh = this.rocksdb.cf(table);
        if (cfh == null) {
            continue;
        }
        cfh.destroy();
        this.rocksdb.removeCf(table);
    }
}
Also used : CFHandle(com.baidu.hugegraph.backend.store.rocksdb.OpenedRocksDB.CFHandle) ArrayList(java.util.ArrayList) ColumnFamilyHandle(org.rocksdb.ColumnFamilyHandle)

Example 5 with CFHandle

use of com.baidu.hugegraph.backend.store.rocksdb.OpenedRocksDB.CFHandle in project incubator-hugegraph by apache.

the class RocksDBStdSessions method cf.

private CFHandle cf(String cfName) {
    CFHandle cfh = this.rocksdb.cf(cfName);
    if (cfh == null) {
        throw new BackendException("Table '%s' is not opened", cfName);
    }
    cfh.open();
    return cfh;
}
Also used : CFHandle(com.baidu.hugegraph.backend.store.rocksdb.OpenedRocksDB.CFHandle) BackendException(com.baidu.hugegraph.backend.BackendException)

Aggregations

CFHandle (com.baidu.hugegraph.backend.store.rocksdb.OpenedRocksDB.CFHandle)6 ArrayList (java.util.ArrayList)3 ColumnFamilyHandle (org.rocksdb.ColumnFamilyHandle)3 ColumnFamilyOptions (org.rocksdb.ColumnFamilyOptions)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ColumnFamilyDescriptor (org.rocksdb.ColumnFamilyDescriptor)2 DBOptions (org.rocksdb.DBOptions)2 RocksDB (org.rocksdb.RocksDB)2 SstFileManager (org.rocksdb.SstFileManager)2 BackendException (com.baidu.hugegraph.backend.BackendException)1 CoreOptions (com.baidu.hugegraph.config.CoreOptions)1 Path (java.nio.file.Path)1 Options (org.rocksdb.Options)1 WriteOptions (org.rocksdb.WriteOptions)1