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 import org.apache.archiva.metadata.repository.cassandra.model.ColumnNames;
32 import org.apache.commons.lang3.StringUtils;
33
34
35
36
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
143
144
145
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
160 }
161
162 }