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 projectVersionMetadataModelFamilyName = "projectversionmetadatamodel";
80 private String artifactMetadataModelFamilyName = "artifactmetadatamodel";
82 private String metadataFacetModelFamilyName = "metadatafacetmodel";
86 public void initialize()
88 // FIXME must come from configuration not sys props
89 String cassandraHost = System.getProperty( "cassandraHost", "localhost" );
90 String cassandraPort = System.getProperty( "cassandraPort" );
91 int maxActive = Integer.getInteger( "cassandra.maxActive", 20 );
92 String readConsistencyLevel =
93 System.getProperty( "cassandra.readConsistencyLevel", HConsistencyLevel.QUORUM.name() );
94 String writeConsistencyLevel =
95 System.getProperty( "cassandra.readConsistencyLevel", HConsistencyLevel.QUORUM.name() );
97 int replicationFactor = Integer.getInteger( "cassandra.replicationFactor", 1 );
99 String keyspaceName = System.getProperty( "cassandra.keyspace.name", KEYSPACE_NAME );
100 String clusterName = System.getProperty( "cassandra.cluster.name", CLUSTER_NAME );
102 final CassandraHostConfigurator configurator =
103 new CassandraHostConfigurator( cassandraHost + ":" + cassandraPort );
104 configurator.setMaxActive( maxActive );
106 cluster = HFactory.getOrCreateCluster( clusterName, configurator );
108 final ConfigurableConsistencyLevel consistencyLevelPolicy = new ConfigurableConsistencyLevel();
109 consistencyLevelPolicy.setDefaultReadConsistencyLevel( HConsistencyLevel.valueOf( readConsistencyLevel ) );
110 consistencyLevelPolicy.setDefaultWriteConsistencyLevel( HConsistencyLevel.valueOf( writeConsistencyLevel ) );
111 keyspace = HFactory.createKeyspace( keyspaceName, cluster, consistencyLevelPolicy );
113 List<ColumnFamilyDefinition> cfds = new ArrayList<ColumnFamilyDefinition>();
118 final ColumnFamilyDefinition namespace =
119 HFactory.createColumnFamilyDefinition( keyspace.getKeyspaceName(), //
120 getNamespaceFamilyName(), //
121 ComparatorType.UTF8TYPE );
122 cfds.add( namespace );
124 // creating indexes for cql query
126 BasicColumnDefinition nameColumn = new BasicColumnDefinition();
127 nameColumn.setName( StringSerializer.get().toByteBuffer( "name" ) );
128 nameColumn.setIndexName( "name" );
129 nameColumn.setIndexType( ColumnIndexType.KEYS );
130 nameColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
131 namespace.addColumnDefinition( nameColumn );
133 BasicColumnDefinition repositoryIdColumn = new BasicColumnDefinition();
134 repositoryIdColumn.setName( StringSerializer.get().toByteBuffer( "repositoryName" ) );
135 repositoryIdColumn.setIndexName( "repositoryName" );
136 repositoryIdColumn.setIndexType( ColumnIndexType.KEYS );
137 repositoryIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
138 namespace.addColumnDefinition( repositoryIdColumn );
143 final ColumnFamilyDefinition repository =
144 HFactory.createColumnFamilyDefinition( keyspace.getKeyspaceName(), //
145 getRepositoryFamilyName(), //
146 ComparatorType.UTF8TYPE );
148 cfds.add( repository );
150 BasicColumnDefinition nameColumn = new BasicColumnDefinition();
151 nameColumn.setName( StringSerializer.get().toByteBuffer( "repositoryName" ) );
152 nameColumn.setIndexName( "repositoryName" );
153 nameColumn.setIndexType( ColumnIndexType.KEYS );
154 nameColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
155 repository.addColumnDefinition( nameColumn );
161 final ColumnFamilyDefinition project = HFactory.createColumnFamilyDefinition( keyspace.getKeyspaceName(), //
162 getProjectFamilyName(), //
163 ComparatorType.UTF8TYPE );
166 // creating indexes for cql query
168 BasicColumnDefinition projectIdColumn = new BasicColumnDefinition();
169 projectIdColumn.setName( StringSerializer.get().toByteBuffer( "projectId" ) );
170 projectIdColumn.setIndexName( "projectId" );
171 projectIdColumn.setIndexType( ColumnIndexType.KEYS );
172 projectIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
173 project.addColumnDefinition( projectIdColumn );
175 BasicColumnDefinition repositoryIdColumn = new BasicColumnDefinition();
176 repositoryIdColumn.setName( StringSerializer.get().toByteBuffer( "repositoryName" ) );
177 repositoryIdColumn.setIndexName( "repositoryName" );
178 repositoryIdColumn.setIndexType( ColumnIndexType.KEYS );
179 repositoryIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
180 project.addColumnDefinition( repositoryIdColumn );
182 BasicColumnDefinition namespaceIdColumn = new BasicColumnDefinition();
183 namespaceIdColumn.setName( StringSerializer.get().toByteBuffer( "namespaceId" ) );
184 namespaceIdColumn.setIndexName( "namespaceId" );
185 namespaceIdColumn.setIndexType( ColumnIndexType.KEYS );
186 namespaceIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
187 project.addColumnDefinition( namespaceIdColumn );
190 //projectversionmetadatamodel
193 final ColumnFamilyDefinition projectVersionMetadataModel =
194 HFactory.createColumnFamilyDefinition( keyspace.getKeyspaceName(), //
195 getProjectVersionMetadataModelFamilyName(), //
196 ComparatorType.UTF8TYPE );
197 cfds.add( projectVersionMetadataModel );
199 // creating indexes for cql query
201 BasicColumnDefinition namespaceIdColumn = new BasicColumnDefinition();
202 namespaceIdColumn.setName( StringSerializer.get().toByteBuffer( "namespaceId" ) );
203 namespaceIdColumn.setIndexName( "namespaceId" );
204 namespaceIdColumn.setIndexType( ColumnIndexType.KEYS );
205 namespaceIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
206 projectVersionMetadataModel.addColumnDefinition( namespaceIdColumn );
208 BasicColumnDefinition repositoryNameColumn = new BasicColumnDefinition();
209 repositoryNameColumn.setName( StringSerializer.get().toByteBuffer( "repositoryName" ) );
210 repositoryNameColumn.setIndexName( "repositoryName" );
211 repositoryNameColumn.setIndexType( ColumnIndexType.KEYS );
212 repositoryNameColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
213 projectVersionMetadataModel.addColumnDefinition( repositoryNameColumn );
215 BasicColumnDefinition idColumn = new BasicColumnDefinition();
216 idColumn.setName( StringSerializer.get().toByteBuffer( "id" ) );
217 idColumn.setIndexName( "id" );
218 idColumn.setIndexType( ColumnIndexType.KEYS );
219 idColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
220 projectVersionMetadataModel.addColumnDefinition( idColumn );
222 BasicColumnDefinition projectIdColumn = new BasicColumnDefinition();
223 projectIdColumn.setName( StringSerializer.get().toByteBuffer( "projectId" ) );
224 projectIdColumn.setIndexName( "projectId" );
225 projectIdColumn.setIndexType( ColumnIndexType.KEYS );
226 projectIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
227 projectVersionMetadataModel.addColumnDefinition( projectIdColumn );
231 // artifactmetadatamodel table
234 final ColumnFamilyDefinition artifactMetadataModel =
235 HFactory.createColumnFamilyDefinition( keyspace.getKeyspaceName(), //
236 getArtifactMetadataModelFamilyName(), //
237 ComparatorType.UTF8TYPE );
238 cfds.add( artifactMetadataModel );
240 // creating indexes for cql query
242 BasicColumnDefinition idColumn = new BasicColumnDefinition();
243 idColumn.setName( StringSerializer.get().toByteBuffer( "id" ) );
244 idColumn.setIndexName( "id" );
245 idColumn.setIndexType( ColumnIndexType.KEYS );
246 idColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
247 artifactMetadataModel.addColumnDefinition( idColumn );
249 BasicColumnDefinition repositoryNameColumn = new BasicColumnDefinition();
250 repositoryNameColumn.setName( StringSerializer.get().toByteBuffer( "repositoryName" ) );
251 repositoryNameColumn.setIndexName( "repositoryName" );
252 repositoryNameColumn.setIndexType( ColumnIndexType.KEYS );
253 repositoryNameColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
254 artifactMetadataModel.addColumnDefinition( repositoryNameColumn );
256 BasicColumnDefinition namespaceIdColumn = new BasicColumnDefinition();
257 namespaceIdColumn.setName( StringSerializer.get().toByteBuffer( "namespaceId" ) );
258 namespaceIdColumn.setIndexName( "namespaceId" );
259 namespaceIdColumn.setIndexType( ColumnIndexType.KEYS );
260 namespaceIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
261 artifactMetadataModel.addColumnDefinition( namespaceIdColumn );
263 BasicColumnDefinition projectColumn = new BasicColumnDefinition();
264 projectColumn.setName( StringSerializer.get().toByteBuffer( "project" ) );
265 projectColumn.setIndexName( "project" );
266 projectColumn.setIndexType( ColumnIndexType.KEYS );
267 projectColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
268 artifactMetadataModel.addColumnDefinition( projectColumn );
270 BasicColumnDefinition projectVersionColumn = new BasicColumnDefinition();
271 projectVersionColumn.setName( StringSerializer.get().toByteBuffer( "projectVersion" ) );
272 projectVersionColumn.setIndexName( "projectVersion" );
273 projectVersionColumn.setIndexType( ColumnIndexType.KEYS );
274 projectVersionColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
275 artifactMetadataModel.addColumnDefinition( projectVersionColumn );
277 BasicColumnDefinition versionColumn = new BasicColumnDefinition();
278 versionColumn.setName( StringSerializer.get().toByteBuffer( "version" ) );
279 versionColumn.setIndexName( "version" );
280 versionColumn.setIndexType( ColumnIndexType.KEYS );
281 versionColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
282 artifactMetadataModel.addColumnDefinition( versionColumn );
284 BasicColumnDefinition whenGatheredColumn = new BasicColumnDefinition();
285 whenGatheredColumn.setName( StringSerializer.get().toByteBuffer( "whenGathered" ) );
286 whenGatheredColumn.setIndexName( "whenGathered" );
287 whenGatheredColumn.setIndexType( ColumnIndexType.KEYS );
288 whenGatheredColumn.setValidationClass( ComparatorType.LONGTYPE.getClassName() );
289 artifactMetadataModel.addColumnDefinition( whenGatheredColumn );
291 BasicColumnDefinition sha1Column = new BasicColumnDefinition();
292 sha1Column.setName( StringSerializer.get().toByteBuffer( "sha1" ) );
293 sha1Column.setIndexName( "sha1" );
294 sha1Column.setIndexType( ColumnIndexType.KEYS );
295 sha1Column.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
296 artifactMetadataModel.addColumnDefinition( sha1Column );
298 BasicColumnDefinition md5Column = new BasicColumnDefinition();
299 md5Column.setName( StringSerializer.get().toByteBuffer( "md5" ) );
300 md5Column.setIndexName( "md5" );
301 md5Column.setIndexType( ColumnIndexType.KEYS );
302 md5Column.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
303 artifactMetadataModel.addColumnDefinition( md5Column );
308 // metadatafacetmodel table
310 final ColumnFamilyDefinition metadataFacetModel =
311 HFactory.createColumnFamilyDefinition( keyspace.getKeyspaceName(), //
312 getMetadataFacetModelFamilyName(), //
313 ComparatorType.UTF8TYPE );
314 cfds.add( metadataFacetModel );
316 // creating indexes for cql query
318 BasicColumnDefinition facetIdColumn = new BasicColumnDefinition();
319 facetIdColumn.setName( StringSerializer.get().toByteBuffer( "facetId" ) );
320 facetIdColumn.setIndexName( "facetId" );
321 facetIdColumn.setIndexType( ColumnIndexType.KEYS );
322 facetIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
323 metadataFacetModel.addColumnDefinition( facetIdColumn );
325 BasicColumnDefinition repositoryNameColumn = new BasicColumnDefinition();
326 repositoryNameColumn.setName( StringSerializer.get().toByteBuffer( "repositoryName" ) );
327 repositoryNameColumn.setIndexName( "repositoryName" );
328 repositoryNameColumn.setIndexType( ColumnIndexType.KEYS );
329 repositoryNameColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
330 metadataFacetModel.addColumnDefinition( repositoryNameColumn );
332 BasicColumnDefinition nameColumn = new BasicColumnDefinition();
333 nameColumn.setName( StringSerializer.get().toByteBuffer( "name" ) );
334 nameColumn.setIndexName( "name" );
335 nameColumn.setIndexType( ColumnIndexType.KEYS );
336 nameColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
337 metadataFacetModel.addColumnDefinition( nameColumn );
339 BasicColumnDefinition namespaceColumn = new BasicColumnDefinition();
340 namespaceColumn.setName( StringSerializer.get().toByteBuffer( "namespaceId" ) );
341 namespaceColumn.setIndexName( "namespaceId" );
342 namespaceColumn.setIndexType( ColumnIndexType.KEYS );
343 namespaceColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
344 metadataFacetModel.addColumnDefinition( namespaceColumn );
346 BasicColumnDefinition projectIdColumn = new BasicColumnDefinition();
347 projectIdColumn.setName( StringSerializer.get().toByteBuffer( "projectId" ) );
348 projectIdColumn.setIndexName( "projectId" );
349 projectIdColumn.setIndexType( ColumnIndexType.KEYS );
350 projectIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
351 metadataFacetModel.addColumnDefinition( projectIdColumn );
353 BasicColumnDefinition projectVersionColumn = new BasicColumnDefinition();
354 projectVersionColumn.setName( StringSerializer.get().toByteBuffer( "projectVersion" ) );
355 projectVersionColumn.setIndexName( "projectVersion" );
356 projectVersionColumn.setIndexType( ColumnIndexType.KEYS );
357 projectVersionColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
358 metadataFacetModel.addColumnDefinition( projectVersionColumn );
363 // TODO take care of update new table!!
364 { // ensure keyspace exists, here if the keyspace doesn't exist we suppose nothing exist
365 if ( cluster.describeKeyspace( keyspaceName ) == null )
367 logger.info( "Creating Archiva Cassandra '" + keyspaceName + "' keyspace." );
368 cluster.addKeyspace( HFactory.createKeyspaceDefinition( keyspaceName, //
369 ThriftKsDef.DEF_STRATEGY_CLASS, //
370 replicationFactor, //
383 public void shutdown()
389 public boolean started()
396 public Keyspace getKeyspace()
401 public Cluster getCluster()
406 public String getRepositoryFamilyName()
408 return repositoryFamilyName;
411 public String getNamespaceFamilyName()
413 return namespaceFamilyName;
416 public String getProjectFamilyName()
418 return projectFamilyName;
421 public String getProjectVersionMetadataModelFamilyName()
423 return projectVersionMetadataModelFamilyName;
426 public String getArtifactMetadataModelFamilyName()
428 return artifactMetadataModelFamilyName;
431 public String getMetadataFacetModelFamilyName()
433 return metadataFacetModelFamilyName;