1 package org.apache.archiva.metadata.repository.cassandra;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import me.prettyprint.cassandra.serializers.LongSerializer;
23 import me.prettyprint.cassandra.serializers.SerializerTypeInferer;
24 import me.prettyprint.cassandra.serializers.StringSerializer;
25 import me.prettyprint.cassandra.service.template.ColumnFamilyUpdater;
26 import me.prettyprint.hector.api.Serializer;
27 import me.prettyprint.hector.api.beans.ColumnSlice;
28 import me.prettyprint.hector.api.beans.HColumn;
29 import me.prettyprint.hector.api.factory.HFactory;
30 import me.prettyprint.hector.api.mutation.Mutator;
31 import org.apache.commons.lang.StringUtils;
34 * @author Olivier Lamy
37 public class CassandraUtils
40 private static final String EMPTY_VALUE = "";
42 public static final String SEPARATOR = "->";
44 public static String generateKey( final String... bases )
46 final StringBuilder builder = new StringBuilder();
47 if ( bases == null || bases.length == 0 )
49 return builder.toString();
52 for ( final String s : bases )
60 builder.append( EMPTY_VALUE );
62 builder.append( SEPARATOR );
64 if ( builder.length() > 0 )
66 builder.setLength( builder.length() - SEPARATOR.length() );
68 return builder.toString();
71 public static <A, B> HColumn<A, B> column( final A name, final B value )
74 return HFactory.createColumn( name, //
76 (Serializer<A>) SerializerTypeInferer.getSerializer( name ), //
77 (Serializer<B>) SerializerTypeInferer.getSerializer( value ) );
80 public static String getStringValue( ColumnSlice<String, String> columnSlice, String columnName )
82 if ( StringUtils.isEmpty( columnName ) )
87 HColumn<String, String> hColumn = columnSlice.getColumnByName( columnName );
88 return hColumn == null ? null : hColumn.getValue();
91 public static Long getLongValue( ColumnSlice<String, Long> columnSlice, String columnName )
93 if ( StringUtils.isEmpty( columnName ) )
98 HColumn<String, Long> hColumn = columnSlice.getColumnByName( columnName );
99 return hColumn == null ? null : hColumn.getValue();
102 public static String getAsStringValue( ColumnSlice<String, Long> columnSlice, String columnName )
104 StringSerializer ss = StringSerializer.get();
105 if ( StringUtils.isEmpty( columnName ) )
110 HColumn<String, Long> hColumn = columnSlice.getColumnByName( columnName );
111 return hColumn == null ? null : ss.fromByteBuffer( hColumn.getValueBytes() );
114 public static Long getAsLongValue( ColumnSlice<String, String> columnSlice, String columnName )
116 LongSerializer ls = LongSerializer.get();
117 if ( StringUtils.isEmpty( columnName ) )
122 HColumn<String, String> hColumn = columnSlice.getColumnByName( columnName );
123 return hColumn == null ? null : ls.fromByteBuffer( hColumn.getValueBytes() );
126 public static void addInsertion( Mutator<String> mutator, String key, String columnFamily, String columnName,
131 mutator.addInsertion( key, columnFamily, column( columnName, value ) );
136 * null check on the value to prevent {@link java.lang.IllegalArgumentException}
141 public static void addUpdateStringValue(ColumnFamilyUpdater<String,String> updater, String columnName, String value )
147 updater.setString( columnName, value );
151 private CassandraUtils()