How to get a system key from FoundationDB Java

(Hieu Nguyen) #1

In fdbcli, I can use these commands to get the primaryDatacenter:

fdb> option on ACCESS_SYSTEM_KEYS
Option enabled for all transactions
fdb> get \xff/primaryDatacenter
`\xff/primaryDatacenter' is `dc1'

How to do this in Java? I tried several ways but they do not work. All returns null result.

        tx.options().setAccessSystemKeys();
        byte[] subspace = new Subspace(new byte[] { (byte) 0xff}).getKey();
        byte[] result = tx.get(Tuple.from(subspace, "/primaryDatacenter").pack()).join();
        byte[] result = tx.get(Tuple.from(new byte[] { (byte) 0xff }, "/primaryDatacenter").pack()).join();
        byte[] result = tx.get(Tuple.from(new byte[] { (byte) 0xff }, "/primaryDatacenter".getBytes()).pack()).join();
(gaurav) #2

You should try forming the byte key directly without using Tuple layer. Most likely Tuple is adding some more bytes somewhere to the key while encoding.

I would suggest to create a byte by appending the bytes() of /primaryDatacenter to 0xFF directly.

(Hieu Nguyen) #3

I was able to do it. This is the way to construct the key:

        ByteBuffer buffer = ByteBuffer.allocate("/primaryDatacenter".length() + 1);
        buffer.put((byte)0xff);
        buffer.put("/primaryDatacenter".getBytes(Charset.defaultCharset()));
        byte[] checkPrimaryDatacenterKey = buffer.array();

and use it in transaction:

            Transaction tx = db.createTransaction();
            tx.options().setAccessSystemKeys();
            byte[] result = tx.get(checkPrimaryDatacenterKey).join();
            String dc = new String(result);
(Alex Miller) #4

Indeed! Tuple doesn’t concatenate, it produces a tuple. The resulting string was actually \x01\xff\x00\x01/primaryDatacenter\x00.