1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. 2 // This source code is licensed under both the GPLv2 (found in the 3 // COPYING file in the root directory) and Apache 2.0 License 4 // (found in the LICENSE.Apache file in the root directory). 5 6 package org.rocksdb; 7 8 import java.util.Arrays; 9 import java.util.List; 10 11 /** 12 * Base class for all Env implementations in RocksDB. 13 */ 14 public abstract class Env extends RocksObject { 15 16 static { RocksDB.loadLibrary()17 RocksDB.loadLibrary(); 18 } 19 20 private static final Env DEFAULT_ENV = new RocksEnv(getDefaultEnvInternal()); 21 static { 22 /** 23 * The Ownership of the Default Env belongs to C++ 24 * and so we disown the native handle here so that 25 * we cannot accidentally free it from Java. 26 */ DEFAULT_ENV.disOwnNativeHandle()27 DEFAULT_ENV.disOwnNativeHandle(); 28 } 29 30 /** 31 * <p>Returns the default environment suitable for the current operating 32 * system.</p> 33 * 34 * <p>The result of {@code getDefault()} is a singleton whose ownership 35 * belongs to rocksdb c++. As a result, the returned RocksEnv will not 36 * have the ownership of its c++ resource, and calling its dispose()/close() 37 * will be no-op.</p> 38 * 39 * @return the default {@link org.rocksdb.RocksEnv} instance. 40 */ getDefault()41 public static Env getDefault() { 42 return DEFAULT_ENV; 43 } 44 45 /** 46 * <p>Sets the number of background worker threads of the flush pool 47 * for this environment.</p> 48 * <p>Default number: 1</p> 49 * 50 * @param number the number of threads 51 * 52 * @return current {@link RocksEnv} instance. 53 */ setBackgroundThreads(final int number)54 public Env setBackgroundThreads(final int number) { 55 return setBackgroundThreads(number, Priority.LOW); 56 } 57 58 /** 59 * <p>Gets the number of background worker threads of the pool 60 * for this environment.</p> 61 * 62 * @param priority the priority id of a specified thread pool. 63 * 64 * @return the number of threads. 65 */ getBackgroundThreads(final Priority priority)66 public int getBackgroundThreads(final Priority priority) { 67 return getBackgroundThreads(nativeHandle_, priority.getValue()); 68 } 69 70 /** 71 * <p>Sets the number of background worker threads of the specified thread 72 * pool for this environment.</p> 73 * 74 * @param number the number of threads 75 * @param priority the priority id of a specified thread pool. 76 * 77 * <p>Default number: 1</p> 78 * @return current {@link RocksEnv} instance. 79 */ setBackgroundThreads(final int number, final Priority priority)80 public Env setBackgroundThreads(final int number, final Priority priority) { 81 setBackgroundThreads(nativeHandle_, number, priority.getValue()); 82 return this; 83 } 84 85 /** 86 * <p>Returns the length of the queue associated with the specified 87 * thread pool.</p> 88 * 89 * @param priority the priority id of a specified thread pool. 90 * 91 * @return the thread pool queue length. 92 */ getThreadPoolQueueLen(final Priority priority)93 public int getThreadPoolQueueLen(final Priority priority) { 94 return getThreadPoolQueueLen(nativeHandle_, priority.getValue()); 95 } 96 97 /** 98 * Enlarge number of background worker threads of a specific thread pool 99 * for this environment if it is smaller than specified. 'LOW' is the default 100 * pool. 101 * 102 * @param number the number of threads. 103 * @param priority the priority id of a specified thread pool. 104 * 105 * @return current {@link RocksEnv} instance. 106 */ incBackgroundThreadsIfNeeded(final int number, final Priority priority)107 public Env incBackgroundThreadsIfNeeded(final int number, 108 final Priority priority) { 109 incBackgroundThreadsIfNeeded(nativeHandle_, number, priority.getValue()); 110 return this; 111 } 112 113 /** 114 * Lower IO priority for threads from the specified pool. 115 * 116 * @param priority the priority id of a specified thread pool. 117 * 118 * @return current {@link RocksEnv} instance. 119 */ lowerThreadPoolIOPriority(final Priority priority)120 public Env lowerThreadPoolIOPriority(final Priority priority) { 121 lowerThreadPoolIOPriority(nativeHandle_, priority.getValue()); 122 return this; 123 } 124 125 /** 126 * Lower CPU priority for threads from the specified pool. 127 * 128 * @param priority the priority id of a specified thread pool. 129 * 130 * @return current {@link RocksEnv} instance. 131 */ lowerThreadPoolCPUPriority(final Priority priority)132 public Env lowerThreadPoolCPUPriority(final Priority priority) { 133 lowerThreadPoolCPUPriority(nativeHandle_, priority.getValue()); 134 return this; 135 } 136 137 /** 138 * Returns the status of all threads that belong to the current Env. 139 * 140 * @return the status of all threads belong to this env. 141 * 142 * @throws RocksDBException if the thread list cannot be acquired. 143 */ getThreadList()144 public List<ThreadStatus> getThreadList() throws RocksDBException { 145 return Arrays.asList(getThreadList(nativeHandle_)); 146 } 147 Env(final long nativeHandle)148 Env(final long nativeHandle) { 149 super(nativeHandle); 150 } 151 getDefaultEnvInternal()152 private static native long getDefaultEnvInternal(); setBackgroundThreads( final long handle, final int number, final byte priority)153 private native void setBackgroundThreads( 154 final long handle, final int number, final byte priority); getBackgroundThreads(final long handle, final byte priority)155 private native int getBackgroundThreads(final long handle, 156 final byte priority); getThreadPoolQueueLen(final long handle, final byte priority)157 private native int getThreadPoolQueueLen(final long handle, 158 final byte priority); incBackgroundThreadsIfNeeded(final long handle, final int number, final byte priority)159 private native void incBackgroundThreadsIfNeeded(final long handle, 160 final int number, final byte priority); lowerThreadPoolIOPriority(final long handle, final byte priority)161 private native void lowerThreadPoolIOPriority(final long handle, 162 final byte priority); lowerThreadPoolCPUPriority(final long handle, final byte priority)163 private native void lowerThreadPoolCPUPriority(final long handle, 164 final byte priority); getThreadList(final long handle)165 private native ThreadStatus[] getThreadList(final long handle) 166 throws RocksDBException; 167 } 168