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 values encoded using {@link Bytes#toBytes(String)}. 027 * Intended to make it easier to transition away from direct use of {@link Bytes}. 028 * @see Bytes#toBytes(String) 029 * @see Bytes#toString(byte[]) 030 * @see RawStringTerminated 031 */ 032@InterfaceAudience.Public 033public class RawString implements DataType<String> { 034 035 public static final RawString ASCENDING = new RawString(Order.ASCENDING); 036 public static final RawString DESCENDING = new RawString(Order.DESCENDING); 037 038 protected final Order order; 039 040 protected RawString() { 041 this.order = Order.ASCENDING; 042 } 043 044 protected RawString(Order order) { 045 this.order = order; 046 } 047 048 @Override 049 public boolean isOrderPreserving() { 050 return true; 051 } 052 053 @Override 054 public Order getOrder() { 055 return order; 056 } 057 058 @Override 059 public boolean isNullable() { 060 return false; 061 } 062 063 @Override 064 public boolean isSkippable() { 065 return false; 066 } 067 068 @Override 069 public int skip(PositionedByteRange src) { 070 int skipped = src.getRemaining(); 071 src.setPosition(src.getLength()); 072 return skipped; 073 } 074 075 @Override 076 public int encodedLength(String val) { 077 return Bytes.toBytes(val).length; 078 } 079 080 @Override 081 public Class<String> encodedClass() { 082 return String.class; 083 } 084 085 @Override 086 public String decode(PositionedByteRange src) { 087 if (Order.ASCENDING == this.order) { 088 // avoid unnecessary array copy for ASC case. 089 String val = 090 Bytes.toString(src.getBytes(), src.getOffset() + src.getPosition(), src.getRemaining()); 091 src.setPosition(src.getLength()); 092 return val; 093 } else { 094 byte[] b = new byte[src.getRemaining()]; 095 src.get(b); 096 order.apply(b, 0, b.length); 097 return Bytes.toString(b); 098 } 099 } 100 101 @Override 102 public int encode(PositionedByteRange dst, String val) { 103 byte[] s = Bytes.toBytes(val); 104 order.apply(s); 105 dst.put(s); 106 return s.length; 107 } 108}