001package org.apache.archiva.metadata.repository.cassandra; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import me.prettyprint.cassandra.serializers.LongSerializer; 023import me.prettyprint.cassandra.serializers.SerializerTypeInferer; 024import me.prettyprint.cassandra.serializers.StringSerializer; 025import me.prettyprint.cassandra.service.template.ColumnFamilyUpdater; 026import me.prettyprint.hector.api.Serializer; 027import me.prettyprint.hector.api.beans.ColumnSlice; 028import me.prettyprint.hector.api.beans.HColumn; 029import me.prettyprint.hector.api.factory.HFactory; 030import me.prettyprint.hector.api.mutation.Mutator; 031import org.apache.archiva.metadata.repository.cassandra.model.ColumnNames; 032import org.apache.commons.lang3.StringUtils; 033 034/** 035 * @author Olivier Lamy 036 * @since 2.0.0 037 */ 038public class CassandraUtils 039{ 040 041 private static final String EMPTY_VALUE = ""; 042 043 public static final String SEPARATOR = "->"; 044 045 public static String generateKey( final String... bases ) 046 { 047 final StringBuilder builder = new StringBuilder(); 048 if ( bases == null || bases.length == 0 ) 049 { 050 return builder.toString(); 051 } 052 053 for ( final String s : bases ) 054 { 055 if ( s != null ) 056 { 057 builder.append( s ); 058 } 059 else 060 { 061 builder.append( EMPTY_VALUE ); 062 } 063 builder.append( SEPARATOR ); 064 } 065 if ( builder.length() > 0 ) 066 { 067 builder.setLength( builder.length() - SEPARATOR.length() ); 068 } 069 return builder.toString(); 070 } 071 072 public static <A, B> HColumn<A, B> column( final A name, final B value ) 073 { 074 075 return HFactory.createColumn( name, // 076 value, // 077 SerializerTypeInferer.getSerializer( name ), // 078 SerializerTypeInferer.getSerializer( value ) ); 079 } 080 081 public static String getStringValue( ColumnSlice<String, String> columnSlice, ColumnNames columnName ) 082 { 083 return getStringValue( columnSlice, columnName.toString() ); 084 } 085 086 public static String getStringValue( ColumnSlice<String, String> columnSlice, String columnName ) 087 { 088 if ( StringUtils.isEmpty( columnName ) ) 089 { 090 return null; 091 } 092 093 HColumn<String, String> hColumn = columnSlice.getColumnByName( columnName ); 094 return hColumn == null ? null : hColumn.getValue(); 095 } 096 097 public static Long getLongValue( ColumnSlice<String, Long> columnSlice, String columnName ) 098 { 099 if ( StringUtils.isEmpty( columnName ) ) 100 { 101 return null; 102 } 103 104 HColumn<String, Long> hColumn = columnSlice.getColumnByName( columnName ); 105 return hColumn == null ? null : hColumn.getValue(); 106 } 107 108 public static <T> String getAsStringValue( ColumnSlice<String, T> columnSlice, String columnName ) 109 { 110 StringSerializer ss = StringSerializer.get(); 111 if ( StringUtils.isEmpty( columnName ) ) 112 { 113 return null; 114 } 115 116 HColumn<String, T> hColumn = columnSlice.getColumnByName( columnName ); 117 return hColumn == null ? null : ss.fromByteBuffer( hColumn.getValueBytes() ); 118 } 119 120 public static Long getAsLongValue( ColumnSlice<String, String> columnSlice, String columnName ) 121 { 122 LongSerializer ls = LongSerializer.get(); 123 if ( StringUtils.isEmpty( columnName ) ) 124 { 125 return null; 126 } 127 128 HColumn<String, String> hColumn = columnSlice.getColumnByName( columnName ); 129 return hColumn == null ? null : ls.fromByteBuffer( hColumn.getValueBytes() ); 130 } 131 132 public static void addInsertion( Mutator<String> mutator, String key, String columnFamily, String columnName, 133 String value ) 134 { 135 if ( value != null ) 136 { 137 mutator.addInsertion( key, columnFamily, column( columnName, value ) ); 138 } 139 } 140 141 /** 142 * null check on the value to prevent {@link java.lang.IllegalArgumentException} 143 * @param updater 144 * @param columnName 145 * @param value 146 */ 147 public static void addUpdateStringValue(ColumnFamilyUpdater<String,String> updater, String columnName, String value ) 148 { 149 if (value == null) 150 { 151 return; 152 } 153 updater.setString( columnName, value ); 154 155 } 156 157 private CassandraUtils() 158 { 159 // no-op 160 } 161 162}