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 * @deprecated since 3.0.0 and will be removed in 4.0.0 036 */ 037 @Deprecated 038 public static final RawString ASCENDING = new RawString(Order.ASCENDING); 039 /** 040 * @deprecated since 3.0.0 and will be removed in 4.0.0 041 */ 042 @Deprecated 043 public static final RawString DESCENDING = new RawString(Order.DESCENDING); 044 045 protected final Order order; 046 047 /** 048 * @deprecated since 3.0.0 and will be removed in 4.0.0 049 */ 050 @Deprecated 051 public RawString() { 052 this.order = Order.ASCENDING; 053 } 054 055 /** 056 * Creates a new {@link DataType} for interacting with values encoded using 057 * {@link Bytes#toBytes(String)}. 058 * @param order the {@link Order} to use 059 */ 060 public RawString(Order order) { 061 this.order = order; 062 } 063 064 @Override 065 public boolean isOrderPreserving() { 066 return true; 067 } 068 069 @Override 070 public Order getOrder() { 071 return order; 072 } 073 074 @Override 075 public boolean isNullable() { 076 return false; 077 } 078 079 @Override 080 public boolean isSkippable() { 081 return false; 082 } 083 084 @Override 085 public int skip(PositionedByteRange src) { 086 int skipped = src.getRemaining(); 087 src.setPosition(src.getLength()); 088 return skipped; 089 } 090 091 @Override 092 public int encodedLength(String val) { 093 return Bytes.toBytes(val).length; 094 } 095 096 @Override 097 public Class<String> encodedClass() { 098 return String.class; 099 } 100 101 @Override 102 public String decode(PositionedByteRange src) { 103 if (Order.ASCENDING == this.order) { 104 // avoid unnecessary array copy for ASC case. 105 String val = 106 Bytes.toString(src.getBytes(), src.getOffset() + src.getPosition(), src.getRemaining()); 107 src.setPosition(src.getLength()); 108 return val; 109 } else { 110 byte[] b = new byte[src.getRemaining()]; 111 src.get(b); 112 order.apply(b, 0, b.length); 113 return Bytes.toString(b); 114 } 115 } 116 117 @Override 118 public int encode(PositionedByteRange dst, String val) { 119 byte[] s = Bytes.toBytes(val); 120 order.apply(s); 121 dst.put(s); 122 return s.length; 123 } 124}