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 /** 9 * Base class for TraceWriters. 10 */ 11 public abstract class AbstractTraceWriter 12 extends RocksCallbackObject implements TraceWriter { 13 14 @Override initializeNative(final long... nativeParameterHandles)15 protected long initializeNative(final long... nativeParameterHandles) { 16 return createNewTraceWriter(); 17 } 18 19 /** 20 * Called from JNI, proxy for {@link TraceWriter#write(Slice)}. 21 * 22 * @param sliceHandle the native handle of the slice (which we do not own) 23 * 24 * @return short (2 bytes) where the first byte is the 25 * {@link Status.Code#getValue()} and the second byte is the 26 * {@link Status.SubCode#getValue()}. 27 */ writeProxy(final long sliceHandle)28 private short writeProxy(final long sliceHandle) { 29 try { 30 write(new Slice(sliceHandle)); 31 return statusToShort(Status.Code.Ok, Status.SubCode.None); 32 } catch (final RocksDBException e) { 33 return statusToShort(e.getStatus()); 34 } 35 } 36 37 /** 38 * Called from JNI, proxy for {@link TraceWriter#closeWriter()}. 39 * 40 * @return short (2 bytes) where the first byte is the 41 * {@link Status.Code#getValue()} and the second byte is the 42 * {@link Status.SubCode#getValue()}. 43 */ closeWriterProxy()44 private short closeWriterProxy() { 45 try { 46 closeWriter(); 47 return statusToShort(Status.Code.Ok, Status.SubCode.None); 48 } catch (final RocksDBException e) { 49 return statusToShort(e.getStatus()); 50 } 51 } 52 statusToShort( final Status status)53 private static short statusToShort(/*@Nullable*/ final Status status) { 54 final Status.Code code = status != null && status.getCode() != null 55 ? status.getCode() 56 : Status.Code.IOError; 57 final Status.SubCode subCode = status != null && status.getSubCode() != null 58 ? status.getSubCode() 59 : Status.SubCode.None; 60 return statusToShort(code, subCode); 61 } 62 statusToShort(final Status.Code code, final Status.SubCode subCode)63 private static short statusToShort(final Status.Code code, 64 final Status.SubCode subCode) { 65 short result = (short)(code.getValue() << 8); 66 return (short)(result | subCode.getValue()); 67 } 68 createNewTraceWriter()69 private native long createNewTraceWriter(); 70 } 71