]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/java/jmh/src/main/java/org/rocksdb/util/KVUtils.java
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / rocksdb / java / jmh / src / main / java / org / rocksdb / util / KVUtils.java
1 /**
2 * Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
3 * This source code is licensed under both the GPLv2 (found in the
4 * COPYING file in the root directory) and Apache 2.0 License
5 * (found in the LICENSE.Apache file in the root directory).
6 */
7 package org.rocksdb.util;
8
9 import static java.nio.charset.StandardCharsets.UTF_8;
10
11 import java.nio.ByteBuffer;
12 import java.util.ArrayList;
13 import java.util.List;
14
15 public final class KVUtils {
16
17 /**
18 * Get a byte array from a string.
19 *
20 * Assumes UTF-8 encoding
21 *
22 * @param string the string
23 *
24 * @return the bytes.
25 */
26 public static byte[] ba(final String string) {
27 return string.getBytes(UTF_8);
28 }
29
30 /**
31 * Get a string from a byte array.
32 *
33 * Assumes UTF-8 encoding
34 *
35 * @param bytes the bytes
36 *
37 * @return the string.
38 */
39 public static String str(final byte[] bytes) {
40 return new String(bytes, UTF_8);
41 }
42
43 /**
44 * Get a list of keys where the keys are named key1..key1+N
45 * in the range of {@code from} to {@code to} i.e. keyFrom..keyTo.
46 *
47 * @param from the first key
48 * @param to the last key
49 *
50 * @return the array of keys
51 */
52 public static List<byte[]> keys(final int from, final int to) {
53 final List<byte[]> keys = new ArrayList<>(to - from);
54 for (int i = from; i < to; i++) {
55 keys.add(ba("key" + i));
56 }
57 return keys;
58 }
59
60 public static List<ByteBuffer> keys(
61 final List<ByteBuffer> keyBuffers, final int from, final int to) {
62 final List<ByteBuffer> keys = new ArrayList<>(to - from);
63 for (int i = from; i < to; i++) {
64 final ByteBuffer key = keyBuffers.get(i);
65 key.clear();
66 key.put(ba("key" + i));
67 key.flip();
68 keys.add(key);
69 }
70 return keys;
71 }
72 }