Jal
Jal

Reputation: 2312

How to refer to a particular column family in put and get in rocksdb with java client?

Consider the following code

    try (final ColumnFamilyOptions cfOpts = new ColumnFamilyOptions().optimizeUniversalStyleCompaction()) {
        final List<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList(
                new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, cfOpts),
                new ColumnFamilyDescriptor("my-first-columnfamily".getBytes(), cfOpts)
        );

        final List<ColumnFamilyHandle> columnFamilyHandleList =
                new ArrayList<>();

        try (final DBOptions options = new DBOptions()
                .setCreateIfMissing(true)
                .setCreateMissingColumnFamilies(true);
             final RocksDB db = RocksDB.open(options,
                     "/mnt/my_db", cfDescriptors,
                     columnFamilyHandleList)) {
            try {
                // Question: How do I refer to a particular column family by name, for example my-first-columnfamily
                db.put(columnFamilyHandleList.get(0), "myKey".getBytes(), "myValue".getBytes());

            } finally {
                for (final ColumnFamilyHandle columnFamilyHandle :
                        columnFamilyHandleList) {
                    columnFamilyHandle.close();
                }
            }
        } catch(Exception e) {

        }
    }
}

In the following line

db.put(columnFamilyHandleList.get(0), "myKey".getBytes(), "myValue".getBytes());

I want put to be able to select which column family to put to, but put takes a ColumnFamilyHandle and ColumnFamilyHandle does not contain the descriptor name so its hard to select a put destination.

Can someone point me a direction to achieve the above?

Thanks

Upvotes: 3

Views: 1674

Answers (1)

Zelldon
Zelldon

Reputation: 5516

The ColumnFamilyHandle has a getName method, see here.

So you can create a helper method, which searches for the name in the column family handle list.

 public ColumnFamilyHandle getColumnFamilyHandle(byte[] name) {
    return columnFamilyHandles
        .stream()
        .filter(
            handle -> {
              try {
                return Arrays.equals(handle.getName(), name);
              } catch (Exception ex) {
                throw new RuntimeException(ex);
              }
            })
        .findAny()
        .orElse(null);
  }

Upvotes: 1

Related Questions