This project has retired. For details please refer to its Attic page.
CassandraUtils xref
View Javadoc
1   package org.apache.archiva.metadata.repository.cassandra;
2   
3   /*
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
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
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  
32  import org.apache.archiva.metadata.repository.cassandra.model.ColumnNames;
33  import org.apache.commons.lang.StringUtils;
34  
35  /**
36   * @author Olivier Lamy
37   * @since 2.0.0
38   */
39  public class CassandraUtils
40  {
41  
42      private static final String EMPTY_VALUE = "";
43  
44      public static final String SEPARATOR = "->";
45  
46      public static String generateKey( final String... bases )
47      {
48          final StringBuilder builder = new StringBuilder();
49          if ( bases == null || bases.length == 0 )
50          {
51              return builder.toString();
52          }
53  
54          for ( final String s : bases )
55          {
56              if ( s != null )
57              {
58                  builder.append( s );
59              }
60              else
61              {
62                  builder.append( EMPTY_VALUE );
63              }
64              builder.append( SEPARATOR );
65          }
66          if ( builder.length() > 0 )
67          {
68              builder.setLength( builder.length() - SEPARATOR.length() );
69          }
70          return builder.toString();
71      }
72  
73      public static <A, B> HColumn<A, B> column( final A name, final B value )
74      {
75  
76          return HFactory.createColumn( name, //
77                                        value, //
78                                        (Serializer<A>) SerializerTypeInferer.getSerializer( name ), //
79                                        (Serializer<B>) SerializerTypeInferer.getSerializer( value ) );
80      }
81  
82      public static String getStringValue( ColumnSlice<String, String> columnSlice, ColumnNames columnName )
83      {
84          return getStringValue( columnSlice, columnName.toString() );
85      }
86  
87      public static String getStringValue( ColumnSlice<String, String> columnSlice, String columnName )
88      {
89          if ( StringUtils.isEmpty( columnName ) )
90          {
91              return null;
92          }
93  
94          HColumn<String, String> hColumn = columnSlice.getColumnByName( columnName );
95          return hColumn == null ? null : hColumn.getValue();
96      }
97  
98      public static Long getLongValue( ColumnSlice<String, ?> columnSlice, String columnName )
99      {
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 }