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