A note about RocksDB's Column Families

What is RocksDB Column Families?

Even if the key is the same in the same database, if the Column Families are different, it will be possible to register / reference / delete. The official documentation said, "Provides a way to logically partition the database." You can think of it as a concept similar to namespaces. The features are as follows (the official is Google Translate, so please refer to the official document correctly).

Official documentation https://github.com/facebook/rocksdb/wiki/Column-Families

When do you use it?

I honestly don't know. However, it may be useful someday if you know that there is such a function. .. ..

How to use

public class SampleCulmunFamily {

	public void execute() throws Exception {

		//DB creation
		try (final RocksDB db = RocksDB.open("sample")) {
			//Creating a column family (multiple ColumnFamilyDescriptor first
			//When creating multiple columnfamily, create List of ColumnFamilyDescriptor and db.You can pass it to open
			try (final ColumnFamilyHandle columnFamilyHandle = db.createColumnFamily(
					new ColumnFamilyDescriptor("new_cf".getBytes(),
							new ColumnFamilyOptions()))) {
			}
		}

		//Creating a DB and two Culmun Family
		final List<ColumnFamilyDescriptor> columnFamilyDescriptors = new ArrayList<>();
		//Even if you don't specify CulmunFamily, a CulmunFamily called default is created behind the scenes.
		//Therefore, this time, default and new_You will create two Culmun Family called cf.
		columnFamilyDescriptors.add(new ColumnFamilyDescriptor(
				RocksDB.DEFAULT_COLUMN_FAMILY, new ColumnFamilyOptions()));
		columnFamilyDescriptors.add(new ColumnFamilyDescriptor(
				"new_cf".getBytes(), new ColumnFamilyOptions()));
		final List<ColumnFamilyHandle> columnFamilyHandles = new ArrayList<>();
		try (final DBOptions options = new DBOptions();
				final RocksDB db = RocksDB.open("sample",
						columnFamilyDescriptors, columnFamilyHandles)) {

			//Get Culumn Family identifier
			ColumnFamilyHandle defaultColumnFamily = columnFamilyHandles.get(0);
			ColumnFamilyHandle newColumnFamily = columnFamilyHandles.get(1);

			try {
				//Register with the same key. Use the identifier to sort the registration destination.
				db.put(defaultColumnFamily, new WriteOptions(),
						"key".getBytes(), "default_value".getBytes());
				db.put(newColumnFamily, new WriteOptions(),
						"key".getBytes(), "new_cf_value".getBytes());

				//reference
				System.out.println(new String(db.get(defaultColumnFamily, "key".getBytes())));
				System.out.println(new String(db.get(newColumnFamily, "key".getBytes())));

				//Delete
				db.delete(defaultColumnFamily, "key".getBytes());
				db.delete(newColumnFamily, "key".getBytes());

				//Delete column family
				db.dropColumnFamily(newColumnFamily);
			} finally {
				for (final ColumnFamilyHandle handle : columnFamilyHandles) {
					handle.close();
				}
			}
		}
	}
}

Recommended Posts

A note about RocksDB's Column Families
A note about Java GC
A note about the scope
A private note about AtomicReference
[Swift] A note about function and closure
[Note] About nil
A note about adding Junit 4 to Android Studio
A note about the Rails and Vue process
Q & A about JDK
A rough note about Ruby arrays and hash objects
A note about th: field th: each th: object of thymeleaf
A note about the seed function of Ruby on Rails
A note when examining Javalin
About adding a like function
[Note] A story about changing Java build tools with VS Code