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 = "artifactmetadatamodel";
82 private String metadataFacetFamilyName = "metadatafacetmodel";
84 private String mailingListFamilyName = "mailinglist";
88 public void initialize()
90 // FIXME must come from configuration not sys props
91 String cassandraHost = System.getProperty( "cassandraHost", "localhost" );
92 String cassandraPort = System.getProperty( "cassandraPort" );
93 int maxActive = Integer.getInteger( "cassandra.maxActive", 20 );
94 String readConsistencyLevel =
95 System.getProperty( "cassandra.readConsistencyLevel", HConsistencyLevel.QUORUM.name() );
96 String writeConsistencyLevel =
97 System.getProperty( "cassandra.readConsistencyLevel", HConsistencyLevel.QUORUM.name() );
99 int replicationFactor = Integer.getInteger( "cassandra.replicationFactor", 1 );
101 String keyspaceName = System.getProperty( "cassandra.keyspace.name", KEYSPACE_NAME );
102 String clusterName = System.getProperty( "cassandra.cluster.name", CLUSTER_NAME );
104 final CassandraHostConfigurator configurator =
105 new CassandraHostConfigurator( cassandraHost + ":" + cassandraPort );
106 configurator.setMaxActive( maxActive );
108 cluster = HFactory.getOrCreateCluster( clusterName, configurator );
110 final ConfigurableConsistencyLevel consistencyLevelPolicy = new ConfigurableConsistencyLevel();
111 consistencyLevelPolicy.setDefaultReadConsistencyLevel( HConsistencyLevel.valueOf( readConsistencyLevel ) );
112 consistencyLevelPolicy.setDefaultWriteConsistencyLevel( HConsistencyLevel.valueOf( writeConsistencyLevel ) );
113 keyspace = HFactory.createKeyspace( keyspaceName, cluster, consistencyLevelPolicy );
115 List<ColumnFamilyDefinition> cfds = new ArrayList<ColumnFamilyDefinition>();
120 final ColumnFamilyDefinition namespace =
121 HFactory.createColumnFamilyDefinition( keyspace.getKeyspaceName(), //
122 getNamespaceFamilyName(), //
123 ComparatorType.UTF8TYPE );
124 cfds.add( namespace );
126 // creating indexes for cql query
128 BasicColumnDefinition nameColumn = new BasicColumnDefinition();
129 nameColumn.setName( StringSerializer.get().toByteBuffer( "name" ) );
130 nameColumn.setIndexName( "name" );
131 nameColumn.setIndexType( ColumnIndexType.KEYS );
132 nameColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
133 namespace.addColumnDefinition( nameColumn );
135 BasicColumnDefinition repositoryIdColumn = new BasicColumnDefinition();
136 repositoryIdColumn.setName( StringSerializer.get().toByteBuffer( "repositoryName" ) );
137 repositoryIdColumn.setIndexName( "repositoryName" );
138 repositoryIdColumn.setIndexType( ColumnIndexType.KEYS );
139 repositoryIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
140 namespace.addColumnDefinition( repositoryIdColumn );
145 final ColumnFamilyDefinition repository =
146 HFactory.createColumnFamilyDefinition( keyspace.getKeyspaceName(), //
147 getRepositoryFamilyName(), //
148 ComparatorType.UTF8TYPE );
150 cfds.add( repository );
152 BasicColumnDefinition nameColumn = new BasicColumnDefinition();
153 nameColumn.setName( StringSerializer.get().toByteBuffer( "repositoryName" ) );
154 nameColumn.setIndexName( "repositoryName" );
155 nameColumn.setIndexType( ColumnIndexType.KEYS );
156 nameColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
157 repository.addColumnDefinition( nameColumn );
163 final ColumnFamilyDefinition project = HFactory.createColumnFamilyDefinition( keyspace.getKeyspaceName(), //
164 getProjectFamilyName(), //
165 ComparatorType.UTF8TYPE );
168 // creating indexes for cql query
170 BasicColumnDefinition projectIdColumn = new BasicColumnDefinition();
171 projectIdColumn.setName( StringSerializer.get().toByteBuffer( "projectId" ) );
172 projectIdColumn.setIndexName( "projectId" );
173 projectIdColumn.setIndexType( ColumnIndexType.KEYS );
174 projectIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
175 project.addColumnDefinition( projectIdColumn );
177 BasicColumnDefinition repositoryIdColumn = new BasicColumnDefinition();
178 repositoryIdColumn.setName( StringSerializer.get().toByteBuffer( "repositoryName" ) );
179 repositoryIdColumn.setIndexName( "repositoryName" );
180 repositoryIdColumn.setIndexType( ColumnIndexType.KEYS );
181 repositoryIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
182 project.addColumnDefinition( repositoryIdColumn );
184 BasicColumnDefinition namespaceIdColumn = new BasicColumnDefinition();
185 namespaceIdColumn.setName( StringSerializer.get().toByteBuffer( "namespaceId" ) );
186 namespaceIdColumn.setIndexName( "namespaceId" );
187 namespaceIdColumn.setIndexType( ColumnIndexType.KEYS );
188 namespaceIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
189 project.addColumnDefinition( namespaceIdColumn );
192 //projectversionmetadatamodel
195 final ColumnFamilyDefinition projectVersionMetadataModel =
196 HFactory.createColumnFamilyDefinition( keyspace.getKeyspaceName(), //
197 getProjectVersionMetadataFamilyName(), //
198 ComparatorType.UTF8TYPE );
199 cfds.add( projectVersionMetadataModel );
201 // creating indexes for cql query
203 BasicColumnDefinition namespaceIdColumn = new BasicColumnDefinition();
204 namespaceIdColumn.setName( StringSerializer.get().toByteBuffer( "namespaceId" ) );
205 namespaceIdColumn.setIndexName( "namespaceId" );
206 namespaceIdColumn.setIndexType( ColumnIndexType.KEYS );
207 namespaceIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
208 projectVersionMetadataModel.addColumnDefinition( namespaceIdColumn );
210 BasicColumnDefinition repositoryNameColumn = new BasicColumnDefinition();
211 repositoryNameColumn.setName( StringSerializer.get().toByteBuffer( "repositoryName" ) );
212 repositoryNameColumn.setIndexName( "repositoryName" );
213 repositoryNameColumn.setIndexType( ColumnIndexType.KEYS );
214 repositoryNameColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
215 projectVersionMetadataModel.addColumnDefinition( repositoryNameColumn );
217 BasicColumnDefinition idColumn = new BasicColumnDefinition();
218 idColumn.setName( StringSerializer.get().toByteBuffer( "id" ) );
219 idColumn.setIndexName( "id" );
220 idColumn.setIndexType( ColumnIndexType.KEYS );
221 idColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
222 projectVersionMetadataModel.addColumnDefinition( idColumn );
224 BasicColumnDefinition projectIdColumn = new BasicColumnDefinition();
225 projectIdColumn.setName( StringSerializer.get().toByteBuffer( "projectId" ) );
226 projectIdColumn.setIndexName( "projectId" );
227 projectIdColumn.setIndexType( ColumnIndexType.KEYS );
228 projectIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
229 projectVersionMetadataModel.addColumnDefinition( projectIdColumn );
233 // artifactmetadatamodel table
236 final ColumnFamilyDefinition artifactMetadataModel =
237 HFactory.createColumnFamilyDefinition( keyspace.getKeyspaceName(), //
238 getArtifactMetadataFamilyName(), //
239 ComparatorType.UTF8TYPE );
240 cfds.add( artifactMetadataModel );
242 // creating indexes for cql query
244 BasicColumnDefinition idColumn = new BasicColumnDefinition();
245 idColumn.setName( StringSerializer.get().toByteBuffer( "id" ) );
246 idColumn.setIndexName( "id" );
247 idColumn.setIndexType( ColumnIndexType.KEYS );
248 idColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
249 artifactMetadataModel.addColumnDefinition( idColumn );
251 BasicColumnDefinition repositoryNameColumn = new BasicColumnDefinition();
252 repositoryNameColumn.setName( StringSerializer.get().toByteBuffer( "repositoryName" ) );
253 repositoryNameColumn.setIndexName( "repositoryName" );
254 repositoryNameColumn.setIndexType( ColumnIndexType.KEYS );
255 repositoryNameColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
256 artifactMetadataModel.addColumnDefinition( repositoryNameColumn );
258 BasicColumnDefinition namespaceIdColumn = new BasicColumnDefinition();
259 namespaceIdColumn.setName( StringSerializer.get().toByteBuffer( "namespaceId" ) );
260 namespaceIdColumn.setIndexName( "namespaceId" );
261 namespaceIdColumn.setIndexType( ColumnIndexType.KEYS );
262 namespaceIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
263 artifactMetadataModel.addColumnDefinition( namespaceIdColumn );
265 BasicColumnDefinition projectColumn = new BasicColumnDefinition();
266 projectColumn.setName( StringSerializer.get().toByteBuffer( "project" ) );
267 projectColumn.setIndexName( "project" );
268 projectColumn.setIndexType( ColumnIndexType.KEYS );
269 projectColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
270 artifactMetadataModel.addColumnDefinition( projectColumn );
272 BasicColumnDefinition projectVersionColumn = new BasicColumnDefinition();
273 projectVersionColumn.setName( StringSerializer.get().toByteBuffer( "projectVersion" ) );
274 projectVersionColumn.setIndexName( "projectVersion" );
275 projectVersionColumn.setIndexType( ColumnIndexType.KEYS );
276 projectVersionColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
277 artifactMetadataModel.addColumnDefinition( projectVersionColumn );
279 BasicColumnDefinition versionColumn = new BasicColumnDefinition();
280 versionColumn.setName( StringSerializer.get().toByteBuffer( "version" ) );
281 versionColumn.setIndexName( "version" );
282 versionColumn.setIndexType( ColumnIndexType.KEYS );
283 versionColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
284 artifactMetadataModel.addColumnDefinition( versionColumn );
286 BasicColumnDefinition whenGatheredColumn = new BasicColumnDefinition();
287 whenGatheredColumn.setName( StringSerializer.get().toByteBuffer( "whenGathered" ) );
288 whenGatheredColumn.setIndexName( "whenGathered" );
289 whenGatheredColumn.setIndexType( ColumnIndexType.KEYS );
290 whenGatheredColumn.setValidationClass( ComparatorType.LONGTYPE.getClassName() );
291 artifactMetadataModel.addColumnDefinition( whenGatheredColumn );
293 BasicColumnDefinition sha1Column = new BasicColumnDefinition();
294 sha1Column.setName( StringSerializer.get().toByteBuffer( "sha1" ) );
295 sha1Column.setIndexName( "sha1" );
296 sha1Column.setIndexType( ColumnIndexType.KEYS );
297 sha1Column.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
298 artifactMetadataModel.addColumnDefinition( sha1Column );
300 BasicColumnDefinition md5Column = new BasicColumnDefinition();
301 md5Column.setName( StringSerializer.get().toByteBuffer( "md5" ) );
302 md5Column.setIndexName( "md5" );
303 md5Column.setIndexType( ColumnIndexType.KEYS );
304 md5Column.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
305 artifactMetadataModel.addColumnDefinition( md5Column );
310 // metadatafacetmodel table
312 final ColumnFamilyDefinition metadataFacetModel =
313 HFactory.createColumnFamilyDefinition( keyspace.getKeyspaceName(), //
314 getMetadataFacetFamilyName(), //
315 ComparatorType.UTF8TYPE );
316 cfds.add( metadataFacetModel );
318 // creating indexes for cql query
320 BasicColumnDefinition facetIdColumn = new BasicColumnDefinition();
321 facetIdColumn.setName( StringSerializer.get().toByteBuffer( "facetId" ) );
322 facetIdColumn.setIndexName( "facetId" );
323 facetIdColumn.setIndexType( ColumnIndexType.KEYS );
324 facetIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
325 metadataFacetModel.addColumnDefinition( facetIdColumn );
327 BasicColumnDefinition repositoryNameColumn = new BasicColumnDefinition();
328 repositoryNameColumn.setName( StringSerializer.get().toByteBuffer( "repositoryName" ) );
329 repositoryNameColumn.setIndexName( "repositoryName" );
330 repositoryNameColumn.setIndexType( ColumnIndexType.KEYS );
331 repositoryNameColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
332 metadataFacetModel.addColumnDefinition( repositoryNameColumn );
334 BasicColumnDefinition nameColumn = new BasicColumnDefinition();
335 nameColumn.setName( StringSerializer.get().toByteBuffer( "name" ) );
336 nameColumn.setIndexName( "name" );
337 nameColumn.setIndexType( ColumnIndexType.KEYS );
338 nameColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
339 metadataFacetModel.addColumnDefinition( nameColumn );
341 BasicColumnDefinition namespaceColumn = new BasicColumnDefinition();
342 namespaceColumn.setName( StringSerializer.get().toByteBuffer( "namespaceId" ) );
343 namespaceColumn.setIndexName( "namespaceId" );
344 namespaceColumn.setIndexType( ColumnIndexType.KEYS );
345 namespaceColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
346 metadataFacetModel.addColumnDefinition( namespaceColumn );
348 BasicColumnDefinition projectIdColumn = new BasicColumnDefinition();
349 projectIdColumn.setName( StringSerializer.get().toByteBuffer( "projectId" ) );
350 projectIdColumn.setIndexName( "projectId" );
351 projectIdColumn.setIndexType( ColumnIndexType.KEYS );
352 projectIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
353 metadataFacetModel.addColumnDefinition( projectIdColumn );
355 BasicColumnDefinition projectVersionColumn = new BasicColumnDefinition();
356 projectVersionColumn.setName( StringSerializer.get().toByteBuffer( "projectVersion" ) );
357 projectVersionColumn.setIndexName( "projectVersion" );
358 projectVersionColumn.setIndexType( ColumnIndexType.KEYS );
359 projectVersionColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
360 metadataFacetModel.addColumnDefinition( projectVersionColumn );
366 final ColumnFamilyDefinition mailingListCf =
367 HFactory.createColumnFamilyDefinition( keyspace.getKeyspaceName(), //
368 getMailingListFamilyName(), //
369 ComparatorType.UTF8TYPE );
370 cfds.add( mailingListCf );
372 // creating indexes for cql query
374 BasicColumnDefinition projectVersionMetadataIdColumn = new BasicColumnDefinition();
375 projectVersionMetadataIdColumn.setName( StringSerializer.get().toByteBuffer( "projectVersionMetadataId" ) );
376 projectVersionMetadataIdColumn.setIndexName( "projectVersionMetadataId" );
377 projectVersionMetadataIdColumn.setIndexType( ColumnIndexType.KEYS );
378 projectVersionMetadataIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
379 mailingListCf.addColumnDefinition( projectVersionMetadataIdColumn );
384 // TODO take care of update new table!!
385 { // ensure keyspace exists, here if the keyspace doesn't exist we suppose nothing exist
386 if ( cluster.describeKeyspace( keyspaceName ) == null )
388 logger.info( "Creating Archiva Cassandra '" + keyspaceName + "' keyspace." );
389 cluster.addKeyspace( HFactory.createKeyspaceDefinition( keyspaceName, //
390 ThriftKsDef.DEF_STRATEGY_CLASS, //
391 replicationFactor, //
404 public void shutdown()
410 public boolean started()
417 public Keyspace getKeyspace()
422 public Cluster getCluster()
427 public String getRepositoryFamilyName()
429 return repositoryFamilyName;
432 public String getNamespaceFamilyName()
434 return namespaceFamilyName;
437 public String getProjectFamilyName()
439 return projectFamilyName;
442 public String getProjectVersionMetadataFamilyName()
444 return projectVersionMetadataFamilyName;
447 public String getArtifactMetadataFamilyName()
449 return artifactMetadataFamilyName;
452 public String getMetadataFacetFamilyName()
454 return metadataFacetFamilyName;
457 public String getMailingListFamilyName()
459 return mailingListFamilyName;