This project has retired. For details please refer to its
Attic page.
CassandraUtils xref
1 package org.apache.archiva.metadata.repository.cassandra;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
37
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
144
145
146
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
161 }
162
163 }