Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*******************************************************************************
* Copyright 2011 Netflix
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package com.netflix.astyanax.thrift;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;
import org.apache.cassandra.thrift.CfDef;
import org.apache.cassandra.thrift.ColumnDef;
import org.apache.cassandra.thrift.SliceRange;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.apache.thrift.TEnum;
import org.apache.thrift.TFieldIdEnum;
import org.apache.thrift.meta_data.FieldMetaData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.netflix.astyanax.Serializer;
public class ThriftUtils {
private final static Logger LOG = LoggerFactory.getLogger(ThriftUtils.class);
public static final ByteBuffer EMPTY_BYTE_BUFFER = ByteBuffer.wrap(new byte[0]);
// private static final SliceRange RANGE_ALL = new SliceRange(EMPTY_BYTE_BUFFER, EMPTY_BYTE_BUFFER, false, Integer.MAX_VALUE);
public static final int MUTATION_OVERHEAD = 20;
public static SliceRange createAllInclusiveSliceRange() {
return new SliceRange(EMPTY_BYTE_BUFFER, EMPTY_BYTE_BUFFER, false, Integer.MAX_VALUE);
}
public static SliceRange createSliceRange(Serializer serializer, C startColumn, C endColumn,
boolean reversed, int limit) {
return new SliceRange((startColumn == null) ? EMPTY_BYTE_BUFFER : serializer.toByteBuffer(startColumn),
(endColumn == null) ? EMPTY_BYTE_BUFFER : serializer.toByteBuffer(endColumn), reversed, limit);
}
public static Properties getPropertiesFromThrift(T entity) throws Exception {
Properties props = new Properties();
setPropertiesFromThrift("", props, entity);
return props;
}
/**
* Quick and dirty implementation that converts thrift DDL to a Properties object by flattening
* the parameters
* @param prefix
* @param properties
* @param entity
* @throws Exception
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void setPropertiesFromThrift(String prefix, Properties properties, org.apache.thrift.TBase entity) throws Exception {
Field field = entity.getClass().getDeclaredField("metaDataMap");
Map fields = (Map) field.get(entity);
for (Entry f : fields.entrySet()) {
ThriftTypes type = ThriftTypes.values()[f.getValue().valueMetaData.type];
Object value = entity.getFieldValue(f.getKey());
if (value == null)
continue;
switch (type) {
case VOID :
break;
case BOOL :
case BYTE :
case DOUBLE :
case I16 :
case I32 :
case I64 :
case STRING :
case ENUM :
if (value instanceof byte[]) {
properties.put(prefix + f.getKey().getFieldName(), Base64.encodeBase64String((byte[])value));
}
else if (value instanceof ByteBuffer) {
properties.put(prefix + f.getKey().getFieldName(), base64Encode((ByteBuffer)value));
}
else {
properties.put(prefix + f.getKey().getFieldName(), value.toString());
}
break;
case MAP : {
String newPrefix = prefix + f.getKey().getFieldName() + ".";
org.apache.thrift.meta_data.MapMetaData meta = (org.apache.thrift.meta_data.MapMetaData)f.getValue().valueMetaData;
if (!meta.keyMetaData.isStruct() && !meta.keyMetaData.isContainer()) {
Map