1 package org.apache.archiva.metadata.repository.cassandra;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import me.prettyprint.cassandra.model.BasicColumnDefinition;
23 import me.prettyprint.cassandra.model.ConfigurableConsistencyLevel;
24 import me.prettyprint.cassandra.serializers.StringSerializer;
25 import me.prettyprint.cassandra.service.CassandraHostConfigurator;
26 import me.prettyprint.cassandra.service.ThriftKsDef;
27 import me.prettyprint.hector.api.Cluster;
28 import me.prettyprint.hector.api.HConsistencyLevel;
29 import me.prettyprint.hector.api.Keyspace;
30 import me.prettyprint.hector.api.ddl.ColumnFamilyDefinition;
31 import me.prettyprint.hector.api.ddl.ColumnIndexType;
32 import me.prettyprint.hector.api.ddl.ComparatorType;
33 import me.prettyprint.hector.api.factory.HFactory;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.context.ApplicationContext;
37 import org.springframework.stereotype.Service;
39 import javax.annotation.PostConstruct;
40 import javax.annotation.PreDestroy;
41 import javax.inject.Inject;
42 import java.util.ArrayList;
43 import java.util.List;
46 * FIXME make all configuration not hardcoded :-)
48 * @author Olivier Lamy
51 @Service("archivaEntityManagerFactory#cassandra")
52 public class DefaultCassandraArchivaManager
53 implements CassandraArchivaManager
56 private Logger logger = LoggerFactory.getLogger( getClass() );
59 private ApplicationContext applicationContext;
61 private static final String CLUSTER_NAME = "archiva";
63 private static final String KEYSPACE_NAME = "ArchivaKeySpace";
65 private boolean started;
67 private Cluster cluster;
69 private Keyspace keyspace;
72 private String repositoryFamilyName = "repository";
74 private String namespaceFamilyName = "namespace";
76 private String projectFamilyName = "project";
78 private String projectVersionMetadataFamilyName = "projectversionmetadata";
80 private String artifactMetadataFamilyName = "artifactmetadata";
82 private String metadataFacetFamilyName = "metadatafacet";
84 private String mailingListFamilyName = "mailinglist";
86 private String licenseFamilyName = "license";
88 private String dependencyFamilyName = "dependency";
92 public void initialize()
94 // FIXME must come from configuration not sys props
95 String cassandraHost = System.getProperty( "cassandraHost", "localhost" );
96 String cassandraPort = System.getProperty( "cassandraPort" );
97 int maxActive = Integer.getInteger( "cassandra.maxActive", 20 );
98 String readConsistencyLevel =
99 System.getProperty( "cassandra.readConsistencyLevel", HConsistencyLevel.QUORUM.name() );
100 String writeConsistencyLevel =
101 System.getProperty( "cassandra.readConsistencyLevel", HConsistencyLevel.QUORUM.name() );
103 int replicationFactor = Integer.getInteger( "cassandra.replicationFactor", 1 );
105 String keyspaceName = System.getProperty( "cassandra.keyspace.name", KEYSPACE_NAME );
106 String clusterName = System.getProperty( "cassandra.cluster.name", CLUSTER_NAME );
108 final CassandraHostConfigurator configurator =
109 new CassandraHostConfigurator( cassandraHost + ":" + cassandraPort );
110 configurator.setMaxActive( maxActive );
112 cluster = HFactory.getOrCreateCluster( clusterName, configurator );
114 final ConfigurableConsistencyLevel consistencyLevelPolicy = new ConfigurableConsistencyLevel();
115 consistencyLevelPolicy.setDefaultReadConsistencyLevel( HConsistencyLevel.valueOf( readConsistencyLevel ) );
116 consistencyLevelPolicy.setDefaultWriteConsistencyLevel( HConsistencyLevel.valueOf( writeConsistencyLevel ) );
117 keyspace = HFactory.createKeyspace( keyspaceName, cluster, consistencyLevelPolicy );
119 List<ColumnFamilyDefinition> cfds = new ArrayList<ColumnFamilyDefinition>();
124 final ColumnFamilyDefinition namespace =
125 HFactory.createColumnFamilyDefinition( keyspace.getKeyspaceName(), //
126 getNamespaceFamilyName(), //
127 ComparatorType.UTF8TYPE );
128 cfds.add( namespace );
130 // creating indexes for cql query
132 BasicColumnDefinition nameColumn = new BasicColumnDefinition();
133 nameColumn.setName( StringSerializer.get().toByteBuffer( "name" ) );
134 nameColumn.setIndexName( "name" );
135 nameColumn.setIndexType( ColumnIndexType.KEYS );
136 nameColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
137 namespace.addColumnDefinition( nameColumn );
139 BasicColumnDefinition repositoryIdColumn = new BasicColumnDefinition();
140 repositoryIdColumn.setName( StringSerializer.get().toByteBuffer( "repositoryName" ) );
141 repositoryIdColumn.setIndexName( "repositoryName" );
142 repositoryIdColumn.setIndexType( ColumnIndexType.KEYS );
143 repositoryIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
144 namespace.addColumnDefinition( repositoryIdColumn );
149 final ColumnFamilyDefinition repository =
150 HFactory.createColumnFamilyDefinition( keyspace.getKeyspaceName(), //
151 getRepositoryFamilyName(), //
152 ComparatorType.UTF8TYPE );
154 cfds.add( repository );
156 BasicColumnDefinition nameColumn = new BasicColumnDefinition();
157 nameColumn.setName( StringSerializer.get().toByteBuffer( "repositoryName" ) );
158 nameColumn.setIndexName( "repositoryName" );
159 nameColumn.setIndexType( ColumnIndexType.KEYS );
160 nameColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
161 repository.addColumnDefinition( nameColumn );
167 final ColumnFamilyDefinition project = HFactory.createColumnFamilyDefinition( keyspace.getKeyspaceName(), //
168 getProjectFamilyName(), //
169 ComparatorType.UTF8TYPE );
172 // creating indexes for cql query
174 BasicColumnDefinition projectIdColumn = new BasicColumnDefinition();
175 projectIdColumn.setName( StringSerializer.get().toByteBuffer( "projectId" ) );
176 projectIdColumn.setIndexName( "projectId" );
177 projectIdColumn.setIndexType( ColumnIndexType.KEYS );
178 projectIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
179 project.addColumnDefinition( projectIdColumn );
181 BasicColumnDefinition repositoryIdColumn = new BasicColumnDefinition();
182 repositoryIdColumn.setName( StringSerializer.get().toByteBuffer( "repositoryName" ) );
183 repositoryIdColumn.setIndexName( "repositoryName" );
184 repositoryIdColumn.setIndexType( ColumnIndexType.KEYS );
185 repositoryIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
186 project.addColumnDefinition( repositoryIdColumn );
188 BasicColumnDefinition namespaceIdColumn = new BasicColumnDefinition();
189 namespaceIdColumn.setName( StringSerializer.get().toByteBuffer( "namespaceId" ) );
190 namespaceIdColumn.setIndexName( "namespaceId" );
191 namespaceIdColumn.setIndexType( ColumnIndexType.KEYS );
192 namespaceIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
193 project.addColumnDefinition( namespaceIdColumn );
196 //projectversionmetadatamodel
199 final ColumnFamilyDefinition projectVersionMetadataModel =
200 HFactory.createColumnFamilyDefinition( keyspace.getKeyspaceName(), //
201 getProjectVersionMetadataFamilyName(), //
202 ComparatorType.UTF8TYPE );
203 cfds.add( projectVersionMetadataModel );
205 // creating indexes for cql query
207 BasicColumnDefinition namespaceIdColumn = new BasicColumnDefinition();
208 namespaceIdColumn.setName( StringSerializer.get().toByteBuffer( "namespaceId" ) );
209 namespaceIdColumn.setIndexName( "namespaceId" );
210 namespaceIdColumn.setIndexType( ColumnIndexType.KEYS );
211 namespaceIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
212 projectVersionMetadataModel.addColumnDefinition( namespaceIdColumn );
214 BasicColumnDefinition repositoryNameColumn = new BasicColumnDefinition();
215 repositoryNameColumn.setName( StringSerializer.get().toByteBuffer( "repositoryName" ) );
216 repositoryNameColumn.setIndexName( "repositoryName" );
217 repositoryNameColumn.setIndexType( ColumnIndexType.KEYS );
218 repositoryNameColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
219 projectVersionMetadataModel.addColumnDefinition( repositoryNameColumn );
221 BasicColumnDefinition idColumn = new BasicColumnDefinition();
222 idColumn.setName( StringSerializer.get().toByteBuffer( "id" ) );
223 idColumn.setIndexName( "id" );
224 idColumn.setIndexType( ColumnIndexType.KEYS );
225 idColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
226 projectVersionMetadataModel.addColumnDefinition( idColumn );
228 BasicColumnDefinition projectIdColumn = new BasicColumnDefinition();
229 projectIdColumn.setName( StringSerializer.get().toByteBuffer( "projectId" ) );
230 projectIdColumn.setIndexName( "projectId" );
231 projectIdColumn.setIndexType( ColumnIndexType.KEYS );
232 projectIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
233 projectVersionMetadataModel.addColumnDefinition( projectIdColumn );
237 // artifactmetadatamodel table
240 final ColumnFamilyDefinition artifactMetadataModel =
241 HFactory.createColumnFamilyDefinition( keyspace.getKeyspaceName(), //
242 getArtifactMetadataFamilyName(), //
243 ComparatorType.UTF8TYPE );
244 cfds.add( artifactMetadataModel );
246 // creating indexes for cql query
248 BasicColumnDefinition idColumn = new BasicColumnDefinition();
249 idColumn.setName( StringSerializer.get().toByteBuffer( "id" ) );
250 idColumn.setIndexName( "id" );
251 idColumn.setIndexType( ColumnIndexType.KEYS );
252 idColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
253 artifactMetadataModel.addColumnDefinition( idColumn );
255 BasicColumnDefinition repositoryNameColumn = new BasicColumnDefinition();
256 repositoryNameColumn.setName( StringSerializer.get().toByteBuffer( "repositoryName" ) );
257 repositoryNameColumn.setIndexName( "repositoryName" );
258 repositoryNameColumn.setIndexType( ColumnIndexType.KEYS );
259 repositoryNameColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
260 artifactMetadataModel.addColumnDefinition( repositoryNameColumn );
262 BasicColumnDefinition namespaceIdColumn = new BasicColumnDefinition();
263 namespaceIdColumn.setName( StringSerializer.get().toByteBuffer( "namespaceId" ) );
264 namespaceIdColumn.setIndexName( "namespaceId" );
265 namespaceIdColumn.setIndexType( ColumnIndexType.KEYS );
266 namespaceIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
267 artifactMetadataModel.addColumnDefinition( namespaceIdColumn );
269 BasicColumnDefinition projectColumn = new BasicColumnDefinition();
270 projectColumn.setName( StringSerializer.get().toByteBuffer( "project" ) );
271 projectColumn.setIndexName( "project" );
272 projectColumn.setIndexType( ColumnIndexType.KEYS );
273 projectColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
274 artifactMetadataModel.addColumnDefinition( projectColumn );
276 BasicColumnDefinition projectVersionColumn = new BasicColumnDefinition();
277 projectVersionColumn.setName( StringSerializer.get().toByteBuffer( "projectVersion" ) );
278 projectVersionColumn.setIndexName( "projectVersion" );
279 projectVersionColumn.setIndexType( ColumnIndexType.KEYS );
280 projectVersionColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
281 artifactMetadataModel.addColumnDefinition( projectVersionColumn );
283 BasicColumnDefinition versionColumn = new BasicColumnDefinition();
284 versionColumn.setName( StringSerializer.get().toByteBuffer( "version" ) );
285 versionColumn.setIndexName( "version" );
286 versionColumn.setIndexType( ColumnIndexType.KEYS );
287 versionColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
288 artifactMetadataModel.addColumnDefinition( versionColumn );
290 BasicColumnDefinition whenGatheredColumn = new BasicColumnDefinition();
291 whenGatheredColumn.setName( StringSerializer.get().toByteBuffer( "whenGathered" ) );
292 whenGatheredColumn.setIndexName( "whenGathered" );
293 whenGatheredColumn.setIndexType( ColumnIndexType.KEYS );
294 whenGatheredColumn.setValidationClass( ComparatorType.LONGTYPE.getClassName() );
295 artifactMetadataModel.addColumnDefinition( whenGatheredColumn );
297 BasicColumnDefinition sha1Column = new BasicColumnDefinition();
298 sha1Column.setName( StringSerializer.get().toByteBuffer( "sha1" ) );
299 sha1Column.setIndexName( "sha1" );
300 sha1Column.setIndexType( ColumnIndexType.KEYS );
301 sha1Column.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
302 artifactMetadataModel.addColumnDefinition( sha1Column );
304 BasicColumnDefinition md5Column = new BasicColumnDefinition();
305 md5Column.setName( StringSerializer.get().toByteBuffer( "md5" ) );
306 md5Column.setIndexName( "md5" );
307 md5Column.setIndexType( ColumnIndexType.KEYS );
308 md5Column.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
309 artifactMetadataModel.addColumnDefinition( md5Column );
314 // metadatafacetmodel table
316 final ColumnFamilyDefinition metadataFacetModel =
317 HFactory.createColumnFamilyDefinition( keyspace.getKeyspaceName(), //
318 getMetadataFacetFamilyName(), //
319 ComparatorType.UTF8TYPE );
320 cfds.add( metadataFacetModel );
322 // creating indexes for cql query
324 BasicColumnDefinition facetIdColumn = new BasicColumnDefinition();
325 facetIdColumn.setName( StringSerializer.get().toByteBuffer( "facetId" ) );
326 facetIdColumn.setIndexName( "facetId" );
327 facetIdColumn.setIndexType( ColumnIndexType.KEYS );
328 facetIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
329 metadataFacetModel.addColumnDefinition( facetIdColumn );
331 BasicColumnDefinition repositoryNameColumn = new BasicColumnDefinition();
332 repositoryNameColumn.setName( StringSerializer.get().toByteBuffer( "repositoryName" ) );
333 repositoryNameColumn.setIndexName( "repositoryName" );
334 repositoryNameColumn.setIndexType( ColumnIndexType.KEYS );
335 repositoryNameColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
336 metadataFacetModel.addColumnDefinition( repositoryNameColumn );
338 BasicColumnDefinition nameColumn = new BasicColumnDefinition();
339 nameColumn.setName( StringSerializer.get().toByteBuffer( "name" ) );
340 nameColumn.setIndexName( "name" );
341 nameColumn.setIndexType( ColumnIndexType.KEYS );
342 nameColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
343 metadataFacetModel.addColumnDefinition( nameColumn );
345 BasicColumnDefinition namespaceColumn = new BasicColumnDefinition();
346 namespaceColumn.setName( StringSerializer.get().toByteBuffer( "namespaceId" ) );
347 namespaceColumn.setIndexName( "namespaceId" );
348 namespaceColumn.setIndexType( ColumnIndexType.KEYS );
349 namespaceColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
350 metadataFacetModel.addColumnDefinition( namespaceColumn );
352 BasicColumnDefinition projectIdColumn = new BasicColumnDefinition();
353 projectIdColumn.setName( StringSerializer.get().toByteBuffer( "projectId" ) );
354 projectIdColumn.setIndexName( "projectId" );
355 projectIdColumn.setIndexType( ColumnIndexType.KEYS );
356 projectIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
357 metadataFacetModel.addColumnDefinition( projectIdColumn );
359 BasicColumnDefinition projectVersionColumn = new BasicColumnDefinition();
360 projectVersionColumn.setName( StringSerializer.get().toByteBuffer( "projectVersion" ) );
361 projectVersionColumn.setIndexName( "projectVersion" );
362 projectVersionColumn.setIndexType( ColumnIndexType.KEYS );
363 projectVersionColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
364 metadataFacetModel.addColumnDefinition( projectVersionColumn );
370 final ColumnFamilyDefinition mailingListCf =
371 HFactory.createColumnFamilyDefinition( keyspace.getKeyspaceName(), //
372 getMailingListFamilyName(), //
373 ComparatorType.UTF8TYPE );
374 cfds.add( mailingListCf );
376 // creating indexes for cql query
382 final ColumnFamilyDefinition licenseCf =
383 HFactory.createColumnFamilyDefinition( keyspace.getKeyspaceName(), //
384 getLicenseFamilyName(), //
385 ComparatorType.UTF8TYPE );
386 cfds.add( licenseCf );
388 // creating indexes for cql query
394 final ColumnFamilyDefinition dependencyCf =
395 HFactory.createColumnFamilyDefinition( keyspace.getKeyspaceName(), //
396 getDependencyFamilyName(), //
397 ComparatorType.UTF8TYPE );
398 cfds.add( dependencyCf );
400 // creating indexes for cql query
402 BasicColumnDefinition groupIdColumn = new BasicColumnDefinition();
403 groupIdColumn.setName( StringSerializer.get().toByteBuffer( "groupId" ) );
404 groupIdColumn.setIndexName( "groupIdIdx" );
405 groupIdColumn.setIndexType( ColumnIndexType.KEYS );
406 groupIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
407 dependencyCf.addColumnDefinition( groupIdColumn );
411 // TODO take care of update new table!!
412 { // ensure keyspace exists, here if the keyspace doesn't exist we suppose nothing exist
413 if ( cluster.describeKeyspace( keyspaceName ) == null )
415 logger.info( "Creating Archiva Cassandra '" + keyspaceName + "' keyspace." );
416 cluster.addKeyspace( HFactory.createKeyspaceDefinition( keyspaceName, //
417 ThriftKsDef.DEF_STRATEGY_CLASS, //
418 replicationFactor, //
431 public void shutdown()
437 public boolean started()
444 public Keyspace getKeyspace()
449 public Cluster getCluster()
454 public String getRepositoryFamilyName()
456 return repositoryFamilyName;
459 public String getNamespaceFamilyName()
461 return namespaceFamilyName;
464 public String getProjectFamilyName()
466 return projectFamilyName;
469 public String getProjectVersionMetadataFamilyName()
471 return projectVersionMetadataFamilyName;
474 public String getArtifactMetadataFamilyName()
476 return artifactMetadataFamilyName;
479 public String getMetadataFacetFamilyName()
481 return metadataFacetFamilyName;
484 public String getMailingListFamilyName()
486 return mailingListFamilyName;
489 public String getLicenseFamilyName()
491 return licenseFamilyName;
494 public String getDependencyFamilyName()
496 return dependencyFamilyName;