001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.types; 019 020import org.apache.hadoop.hbase.util.Bytes; 021import org.apache.hadoop.hbase.util.Order; 022import org.apache.hadoop.hbase.util.PositionedByteRange; 023import org.apache.yetus.audience.InterfaceAudience; 024 025/** 026 * An {@code DataType} for interacting with variable-length values encoded using 027 * {@link Bytes#putBytes(byte[], int, byte[], int, int)}. Intended to make it easier to transition 028 * away from direct use of {@link Bytes}. 029 * @see Bytes#putBytes(byte[], int, byte[], int, int) 030 * @see RawBytesTerminated 031 * @see RawBytesFixedLength 032 * @see OrderedBlob 033 * @see OrderedBlobVar 034 */ 035@InterfaceAudience.Public 036public class RawBytes implements DataType<byte[]> { 037 038 public static final RawBytes ASCENDING = new RawBytes(Order.ASCENDING); 039 public static final RawBytes DESCENDING = new RawBytes(Order.DESCENDING); 040 041 protected final Order order; 042 043 protected RawBytes() { 044 this.order = Order.ASCENDING; 045 } 046 047 protected RawBytes(Order order) { 048 this.order = order; 049 } 050 051 @Override 052 public boolean isOrderPreserving() { 053 return true; 054 } 055 056 @Override 057 public Order getOrder() { 058 return order; 059 } 060 061 @Override 062 public boolean isNullable() { 063 return false; 064 } 065 066 @Override 067 public boolean isSkippable() { 068 return false; 069 } 070 071 @Override 072 public int skip(PositionedByteRange src) { 073 int skipped = src.getRemaining(); 074 src.setPosition(src.getLength()); 075 return skipped; 076 } 077 078 @Override 079 public int encodedLength(byte[] val) { 080 return val.length; 081 } 082 083 @Override 084 public Class<byte[]> encodedClass() { 085 return byte[].class; 086 } 087 088 @Override 089 public byte[] decode(PositionedByteRange src) { 090 return decode(src, src.getRemaining()); 091 } 092 093 @Override 094 public int encode(PositionedByteRange dst, byte[] val) { 095 return encode(dst, val, 0, val.length); 096 } 097 098 /** 099 * Read a {@code byte[]} from the buffer {@code src}. 100 */ 101 public byte[] decode(PositionedByteRange src, int length) { 102 byte[] val = new byte[length]; 103 src.get(val); 104 return val; 105 } 106 107 /** 108 * Write {@code val} into {@code dst}, respecting {@code voff} and {@code vlen}. 109 * @return number of bytes written. 110 */ 111 public int encode(PositionedByteRange dst, byte[] val, int voff, int vlen) { 112 Bytes.putBytes(dst.getBytes(), dst.getOffset() + dst.getPosition(), val, voff, vlen); 113 dst.setPosition(dst.getPosition() + vlen); 114 return vlen; 115 } 116}