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