This project has retired. For details please refer to its Attic page.
Source code
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;
031
032import org.apache.archiva.metadata.repository.cassandra.model.ColumnNames;
033import org.apache.commons.lang.StringUtils;
034
035/**
036 * @author Olivier Lamy
037 * @since 2.0.0
038 */
039public class CassandraUtils
040{
041
042    private static final String EMPTY_VALUE = "";
043
044    public static final String SEPARATOR = "->";
045
046    public static String generateKey( final String... bases )
047    {
048        final StringBuilder builder = new StringBuilder();
049        if ( bases == null || bases.length == 0 )
050        {
051            return builder.toString();
052        }
053
054        for ( final String s : bases )
055        {
056            if ( s != null )
057            {
058                builder.append( s );
059            }
060            else
061            {
062                builder.append( EMPTY_VALUE );
063            }
064            builder.append( SEPARATOR );
065        }
066        if ( builder.length() > 0 )
067        {
068            builder.setLength( builder.length() - SEPARATOR.length() );
069        }
070        return builder.toString();
071    }
072
073    public static <A, B> HColumn<A, B> column( final A name, final B value )
074    {
075
076        return HFactory.createColumn( name, //
077                                      value, //
078                                      (Serializer<A>) SerializerTypeInferer.getSerializer( name ), //
079                                      (Serializer<B>) SerializerTypeInferer.getSerializer( value ) );
080    }
081
082    public static String getStringValue( ColumnSlice<String, String> columnSlice, ColumnNames columnName )
083    {
084        return getStringValue( columnSlice, columnName.toString() );
085    }
086
087    public static String getStringValue( ColumnSlice<String, String> columnSlice, String columnName )
088    {
089        if ( StringUtils.isEmpty( columnName ) )
090        {
091            return null;
092        }
093
094        HColumn<String, String> hColumn = columnSlice.getColumnByName( columnName );
095        return hColumn == null ? null : hColumn.getValue();
096    }
097
098    public static Long getLongValue( ColumnSlice<String, ?> columnSlice, String columnName )
099    {
100        if ( StringUtils.isEmpty( columnName ) )
101        {
102            return null;
103        }
104
105        HColumn<String, Long> hColumn = (HColumn<String, Long>) columnSlice.getColumnByName( columnName );
106        return hColumn == null ? null : hColumn.getValue();
107    }
108
109    public static <T> String getAsStringValue( ColumnSlice<String, T> columnSlice, String columnName )
110    {
111        StringSerializer ss = StringSerializer.get();
112        if ( StringUtils.isEmpty( columnName ) )
113        {
114            return null;
115        }
116
117        HColumn<String, T> hColumn = columnSlice.getColumnByName( columnName );
118        return hColumn == null ? null : ss.fromByteBuffer( hColumn.getValueBytes() );
119    }
120
121    public static Long getAsLongValue( ColumnSlice<String, String> columnSlice, String columnName )
122    {
123        LongSerializer ls = LongSerializer.get();
124        if ( StringUtils.isEmpty( columnName ) )
125        {
126            return null;
127        }
128
129        HColumn<String, String> hColumn = columnSlice.getColumnByName( columnName );
130        return hColumn == null ? null : ls.fromByteBuffer( hColumn.getValueBytes() );
131    }
132
133    public static void addInsertion( Mutator<String> mutator, String key, String columnFamily, String columnName,
134                                     String value )
135    {
136        if ( value != null )
137        {
138            mutator.addInsertion( key, columnFamily, column( columnName, value ) );
139        }
140    }
141
142    /**
143     * null check on the value to prevent {@link java.lang.IllegalArgumentException}
144     * @param updater
145     * @param columnName
146     * @param value
147     */
148    public static void addUpdateStringValue(ColumnFamilyUpdater<String,String> updater, String columnName, String value )
149    {
150        if (value == null)
151        {
152            return;
153        }
154        updater.setString( columnName, value );
155
156    }
157
158    private CassandraUtils()
159    {
160        // no-op
161    }
162
163}