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 com.datastax.oss.driver.api.core.CqlSession;
23 import com.datastax.oss.driver.api.core.CqlSessionBuilder;
24 import com.datastax.oss.driver.api.core.config.DefaultDriverOption;
25 import com.datastax.oss.driver.api.core.config.DriverConfigLoader;
26 import com.datastax.oss.driver.api.core.type.DataTypes;
27 import com.datastax.oss.driver.api.querybuilder.schema.CreateIndex;
28 import com.datastax.oss.driver.api.querybuilder.schema.CreateKeyspace;
29 import com.datastax.oss.driver.api.querybuilder.schema.CreateTableWithOptions;
30 import org.apache.archiva.metadata.repository.RepositorySessionFactoryBean;
31 import org.apache.commons.lang3.StringUtils;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Value;
35 import org.springframework.context.ApplicationContext;
36 import org.springframework.stereotype.Service;
38 import javax.annotation.PostConstruct;
39 import javax.annotation.PreDestroy;
40 import javax.inject.Inject;
41 import java.time.Duration;
42 import java.util.ArrayList;
43 import java.util.Arrays;
44 import java.util.List;
46 import static com.datastax.oss.driver.api.querybuilder.SchemaBuilder.*;
47 import static org.apache.archiva.metadata.repository.cassandra.model.ColumnNames.*;
50 * FIXME make all configuration not hardcoded :-)
52 * @author Olivier Lamy
55 @Service( "archivaEntityManagerFactory#cassandra" )
56 public class DefaultCassandraArchivaManager
57 implements CassandraArchivaManager
60 private static final Logger logger = LoggerFactory.getLogger( DefaultCassandraArchivaManager.class );
63 private ApplicationContext applicationContext;
65 private static final String CLUSTER_NAME = "archiva";
67 private static final String KEYSPACE_NAME = "ArchivaKeySpace";
69 private boolean started;
72 private String repositoryFamilyName = "repository";
74 private String namespaceFamilyName = "namespace";
76 private String projectFamilyName = PROJECT.toString( );
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";
90 private String checksumFamilyName = "checksum";
93 private static String[] projectVersionMetadataColumns;
98 projectVersionMetadataColumns = new String[]{
100 NAMESPACE_ID.toString( ),
101 REPOSITORY_NAME.toString( ),
102 PROJECT_VERSION.toString( ),
103 PROJECT_ID.toString( ),
104 DESCRIPTION.toString( ),
108 VERSION_PROPERTIES.toString( ),
110 "ciManagement.system",
112 "issueManagement.system",
113 "issueManagement.url",
118 "scm.developerConnection"
120 Arrays.sort( projectVersionMetadataColumns );
123 @Value( "${cassandra.host}" )
124 private String cassandraHost;
126 @Value( "${cassandra.port}" )
127 private String cassandraPort;
129 @Value( "${cassandra.maxActive}" )
130 private int maxActive;
132 @Value( "${cassandra.readConsistencyLevel}" )
133 private String readConsistencyLevel;
135 @Value( "${cassandra.writeConsistencyLevel}" )
136 private String writeConsistencyLevel;
138 @Value( "${cassandra.replicationFactor}" )
139 private int replicationFactor;
141 @Value( "${cassandra.keyspace.name}" )
142 private String keyspaceName;
144 @Value( "${cassandra.cluster.name}" )
145 private String clusterName;
148 private RepositorySessionFactoryBean repositorySessionFactoryBean;
150 DriverConfigLoader configLoader;
152 CqlSession cqlSession;
155 public CqlSessionBuilder getSessionBuilder( )
157 return CqlSession.builder( ).withConfigLoader( configLoader ).withKeyspace( keyspaceName ).withLocalDatacenter( "datacenter1" );
161 public CqlSession getSession( )
163 if (cqlSession==null || cqlSession.isClosed()) {
164 this.cqlSession = getSessionBuilder( ).build( );
166 return this.cqlSession;
170 public void initialize( )
172 // skip initialisation if not cassandra
173 if ( !StringUtils.equals( repositorySessionFactoryBean.getId( ), "cassandra" ) )
178 List<String> hostNames = new ArrayList<>( );
179 hostNames.add( cassandraHost + ":" + cassandraPort );
180 System.out.println( "Contact point: " + cassandraHost + ":" + cassandraPort );
182 DriverConfigLoader.programmaticBuilder( )
184 .withStringList( DefaultDriverOption.CONTACT_POINTS, hostNames )
185 .withInt( DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE, maxActive )
186 .withInt( DefaultDriverOption.CONNECTION_POOL_REMOTE_SIZE, maxActive )
187 //.withInt( DefaultDriverOption.CONNECTION_MAX_REQUESTS, maxActive )
188 .withString( DefaultDriverOption.REQUEST_CONSISTENCY, readConsistencyLevel )
189 .withDuration( DefaultDriverOption.REQUEST_TIMEOUT, Duration.ofSeconds( 10 ) )
194 CreateKeyspace cKeySpace = createKeyspace( keyspaceName ).ifNotExists( ).withSimpleStrategy( replicationFactor );
195 CqlSession.builder( ).withConfigLoader( configLoader ).withLocalDatacenter( "datacenter1" ).build().execute( cKeySpace.build( ) );
198 CqlSession session = getSession( );
204 String tableName = getNamespaceFamilyName( );
205 CreateTableWithOptions table = createTable( keyspaceName, tableName ).ifNotExists( )
206 .withPartitionKey( CassandraArchivaManager.DEFAULT_PRIMARY_KEY, DataTypes.TEXT )
207 .withColumn( NAME.toString( ), DataTypes.TEXT )
208 .withColumn( REPOSITORY_NAME.toString( ), DataTypes.TEXT )
209 .withCompactStorage( );
210 session.execute( table.build( ) );
211 CreateIndex index = createIndex( NAME.toString( ) ).ifNotExists( ).onTable( tableName ).andColumn( NAME.toString( ) );
212 session.execute( index.build( ) );
213 index = createIndex( REPOSITORY_NAME.toString( ) ).ifNotExists( ).onTable( tableName ).andColumn( REPOSITORY_NAME.toString( ) );
214 session.execute( index.build( ) );
219 String tableName = getRepositoryFamilyName( );
220 CreateTableWithOptions table = createTable( keyspaceName, tableName ).ifNotExists( )
221 .withPartitionKey( CassandraArchivaManager.DEFAULT_PRIMARY_KEY, DataTypes.TEXT )
222 .withColumn( REPOSITORY_NAME.toString( ), DataTypes.TEXT )
223 .withCompactStorage( );
224 session.execute( table.build( ) );
225 CreateIndex index = createIndex( REPOSITORY_NAME.toString( ) ).ifNotExists( ).onTable( tableName ).andColumn( REPOSITORY_NAME.toString( ) );
226 session.execute( index.build( ) );
232 String tableName = getProjectFamilyName( );
233 CreateTableWithOptions table = createTable( keyspaceName, tableName ).ifNotExists( )
234 .withPartitionKey( CassandraArchivaManager.DEFAULT_PRIMARY_KEY, DataTypes.TEXT )
235 .withColumn( PROJECT_ID.toString( ), DataTypes.TEXT )
236 .withColumn( REPOSITORY_NAME.toString( ), DataTypes.TEXT )
237 .withColumn( NAMESPACE_ID.toString( ), DataTypes.TEXT )
238 .withColumn( PROJECT_PROPERTIES.toString( ), DataTypes.frozenMapOf( DataTypes.TEXT, DataTypes.TEXT ) )
239 .withCompactStorage( );
240 session.execute( table.build( ) );
241 CreateIndex index = createIndex( PROJECT_ID.toString( ) ).ifNotExists( ).onTable( tableName ).andColumn( PROJECT_ID.toString( ) );
242 session.execute( index.build( ) );
243 index = createIndex( REPOSITORY_NAME.toString( ) ).ifNotExists( ).onTable( tableName ).andColumn( REPOSITORY_NAME.toString( ) );
244 session.execute( index.build( ) );
245 index = createIndex( NAMESPACE_ID.toString( ) ).ifNotExists( ).onTable( tableName ).andColumn( NAMESPACE_ID.toString( ) );
246 session.execute( index.build( ) );
250 // Project Version Metadata Model
252 String tableName = getProjectVersionMetadataFamilyName( );
253 CreateTableWithOptions table = createTable( keyspaceName, tableName ).ifNotExists( )
254 .withPartitionKey( CassandraArchivaManager.DEFAULT_PRIMARY_KEY, DataTypes.TEXT )
255 .withColumn( NAMESPACE_ID.toString( ), DataTypes.TEXT )
256 .withColumn( REPOSITORY_NAME.toString( ), DataTypes.TEXT )
257 .withColumn( PROJECT_VERSION.toString( ), DataTypes.TEXT )
258 .withColumn( PROJECT_ID.toString( ), DataTypes.TEXT )
259 .withColumn( DESCRIPTION.toString( ), DataTypes.TEXT )
260 .withColumn( URL.toString( ), DataTypes.TEXT )
261 .withColumn( NAME.toString(), DataTypes.TEXT )
262 .withColumn( VERSION.toString(), DataTypes.TEXT )
263 .withColumn( VERSION_PROPERTIES.toString(), DataTypes.mapOf( DataTypes.TEXT, DataTypes.TEXT ) )
264 .withColumn( "incomplete", DataTypes.BOOLEAN )
265 .withColumn( "\"ciManagement.system\"", DataTypes.TEXT )
266 .withColumn( "\"ciManagement.url\"", DataTypes.TEXT )
267 .withColumn( "\"issueManagement.system\"", DataTypes.TEXT )
268 .withColumn( "\"issueManagement.url\"", DataTypes.TEXT )
269 .withColumn( "\"organization.name\"", DataTypes.TEXT )
270 .withColumn( "\"organization.url\"", DataTypes.TEXT )
271 .withColumn( "\"scm.url\"", DataTypes.TEXT )
272 .withColumn( "\"scm.connection\"", DataTypes.TEXT )
273 .withColumn( "\"scm.developerConnection\"", DataTypes.TEXT );
274 session.execute( table.build( ) );
275 CreateIndex index = createIndex( NAMESPACE_ID.toString( ) ).ifNotExists( ).onTable( tableName ).andColumn( NAMESPACE_ID.toString( ) );
276 session.execute( index.build( ) );
277 index = createIndex( REPOSITORY_NAME.toString( ) ).ifNotExists( ).onTable( tableName ).andColumn( REPOSITORY_NAME.toString( ) );
278 session.execute( index.build( ) );
279 index = createIndex( PROJECT_VERSION.toString( ) ).ifNotExists( ).onTable( tableName ).andColumn( PROJECT_VERSION.toString( ) );
280 session.execute( index.build( ) );
281 index = createIndex( PROJECT_ID.toString( ) ).ifNotExists( ).onTable( tableName ).andColumn( PROJECT_ID.toString( ) );
282 session.execute( index.build( ) );
283 index = createIndex( VERSION_PROPERTIES.toString( ) + "_idx" ).ifNotExists( ).onTable( tableName ).andColumnEntries( VERSION_PROPERTIES.toString( ) );
284 session.execute( index.build( ) );
287 // Artifact Metadata Model
289 String tableName = getArtifactMetadataFamilyName( );
290 CreateTableWithOptions table = createTable( keyspaceName, tableName ).ifNotExists( )
291 .withPartitionKey( CassandraArchivaManager.DEFAULT_PRIMARY_KEY, DataTypes.TEXT )
292 .withColumn( ID.toString( ), DataTypes.TEXT )
293 .withColumn( REPOSITORY_NAME.toString( ), DataTypes.TEXT )
294 .withColumn( NAMESPACE_ID.toString( ), DataTypes.TEXT )
295 .withColumn( PROJECT_ID.toString( ), DataTypes.TEXT )
296 .withColumn( PROJECT_VERSION.toString( ), DataTypes.TEXT )
297 .withColumn( VERSION.toString( ), DataTypes.TEXT )
298 .withColumn( WHEN_GATHERED.toString( ), DataTypes.BIGINT )
299 .withColumn( SHA1.toString( ), DataTypes.TEXT )
300 .withColumn( MD5.toString( ), DataTypes.TEXT )
301 .withColumn( FILE_LAST_MODIFIED.toString(), DataTypes.BIGINT)
302 .withColumn( SIZE.toString(), DataTypes.BIGINT )
303 .withCompactStorage( );
304 session.execute( table.build( ) );
306 CreateIndex index = createIndex( ID.toString( ) ).ifNotExists( ).onTable( tableName ).andColumn( ID.toString( ) );
307 session.execute( index.build( ) );
308 index = createIndex( REPOSITORY_NAME.toString( ) ).ifNotExists( ).onTable( tableName ).andColumn( REPOSITORY_NAME.toString( ) );
309 session.execute( index.build( ) );
310 index = createIndex( NAMESPACE_ID.toString( ) ).ifNotExists( ).onTable( tableName ).andColumn( NAMESPACE_ID.toString( ) );
311 session.execute( index.build( ) );
312 index = createIndex( PROJECT_ID.toString( ) ).ifNotExists( ).onTable( tableName ).andColumn( PROJECT_ID.toString( ) );
313 session.execute( index.build( ) );
314 index = createIndex( PROJECT_VERSION.toString( ) ).ifNotExists( ).onTable( tableName ).andColumn( PROJECT_VERSION.toString( ) );
315 session.execute( index.build( ) );
316 index = createIndex( VERSION.toString( ) ).ifNotExists( ).onTable( tableName ).andColumn( VERSION.toString( ) );
317 session.execute( index.build( ) );
318 index = createIndex( WHEN_GATHERED.toString( ) ).ifNotExists( ).onTable( tableName ).andColumn( WHEN_GATHERED.toString( ) );
319 session.execute( index.build( ) );
320 index = createIndex( SHA1.toString( ) ).ifNotExists( ).onTable( tableName ).andColumn( SHA1.toString( ) );
321 session.execute( index.build( ) );
322 index = createIndex( MD5.toString( ) ).ifNotExists( ).onTable( tableName ).andColumn( MD5.toString( ) );
323 session.execute( index.build( ) );
326 // Metadata Facet Model
328 String tableName = getMetadataFacetFamilyName( );
329 CreateTableWithOptions table = createTable( keyspaceName, tableName ).ifNotExists( )
330 .withPartitionKey( CassandraArchivaManager.DEFAULT_PRIMARY_KEY, DataTypes.TEXT )
331 .withColumn( FACET_ID.toString( ), DataTypes.TEXT )
332 .withColumn( REPOSITORY_NAME.toString( ), DataTypes.TEXT )
333 .withColumn( NAME.toString( ), DataTypes.TEXT )
334 .withColumn( NAMESPACE_ID.toString( ), DataTypes.TEXT )
335 .withColumn( PROJECT_ID.toString( ), DataTypes.TEXT )
336 .withColumn( PROJECT_VERSION.toString( ), DataTypes.TEXT )
337 .withColumn( KEY.toString(), DataTypes.TEXT )
338 .withColumn( VALUE.toString(), DataTypes.TEXT)
339 .withColumn( WHEN_GATHERED.toString(), DataTypes.BIGINT )
340 .withCompactStorage( );
341 session.execute( table.build( ) );
343 CreateIndex index = createIndex( FACET_ID.toString( ) ).ifNotExists( ).onTable( tableName ).andColumn( FACET_ID.toString( ) );
344 session.execute( index.build( ) );
345 index = createIndex( REPOSITORY_NAME.toString( ) ).ifNotExists( ).onTable( tableName ).andColumn( REPOSITORY_NAME.toString( ) );
346 session.execute( index.build( ) );
347 index = createIndex( NAME.toString( ) ).ifNotExists( ).onTable( tableName ).andColumn( NAME.toString( ) );
348 session.execute( index.build( ) );
349 index = createIndex( NAMESPACE_ID.toString( ) ).ifNotExists( ).onTable( tableName ).andColumn( NAMESPACE_ID.toString( ) );
350 session.execute( index.build( ) );
351 index = createIndex( PROJECT_ID.toString( ) ).ifNotExists( ).onTable( tableName ).andColumn( PROJECT_ID.toString( ) );
352 session.execute( index.build( ) );
353 index = createIndex( PROJECT_VERSION.toString( ) ).ifNotExists( ).onTable( tableName ).andColumn( PROJECT_VERSION.toString( ) );
354 session.execute( index.build( ) );
358 String tableName = getChecksumFamilyName( );
359 CreateTableWithOptions table = createTable( keyspaceName, tableName ).ifNotExists( )
360 .withPartitionKey( DEFAULT_PRIMARY_KEY, DataTypes.TEXT )
361 .withColumn( "\"artifactMetadataModel.key\"", DataTypes.TEXT )
362 .withColumn( CHECKSUM_ALG.toString( ), DataTypes.TEXT )
363 .withColumn( CHECKSUM_VALUE.toString( ), DataTypes.TEXT )
364 .withColumn( REPOSITORY_NAME.toString( ), DataTypes.TEXT )
365 .withCompactStorage( );
366 session.execute( table.build( ) );
368 CreateIndex index = createIndex( CHECKSUM_ALG.toString( ) ).ifNotExists( ).onTable( tableName ).andColumn( CHECKSUM_ALG.toString( ) );
369 session.execute( index.build( ) );
370 index = createIndex( CHECKSUM_VALUE.toString( ) ).ifNotExists( ).onTable( tableName ).andColumn( CHECKSUM_VALUE.toString( ) );
371 session.execute( index.build( ) );
372 index = createIndex( REPOSITORY_NAME.toString( ) ).ifNotExists( ).onTable( tableName ).andColumn( REPOSITORY_NAME.toString( ) );
373 session.execute( index.build( ) );
377 String tableName = getMailingListFamilyName( );
378 CreateTableWithOptions table = createTable( keyspaceName, tableName ).ifNotExists( )
379 .withPartitionKey( CassandraArchivaManager.DEFAULT_PRIMARY_KEY, DataTypes.TEXT )
380 .withColumn( NAME.toString(), DataTypes.TEXT )
381 .withColumn( "\"projectVersionMetadataModel.key\"", DataTypes.TEXT )
382 .withColumn( "mainArchiveUrl", DataTypes.TEXT )
383 .withColumn( "postAddress", DataTypes.TEXT )
384 .withColumn( "subscribeAddress", DataTypes.TEXT )
385 .withColumn( "unsubscribeAddress", DataTypes.TEXT )
386 .withColumn( "otherArchive", DataTypes.frozenListOf( DataTypes.TEXT ) )
387 .withCompactStorage( );
388 session.execute( table.build( ) );
390 CreateIndex index = createIndex( "\"projectVersionMetadataModel_key\"" ).ifNotExists( ).onTable( tableName ).andColumn( "\"\"projectVersionMetadataModel.key\"\"" );
391 session.execute( index.build( ) );
396 String tableName = getLicenseFamilyName( );
397 CreateTableWithOptions table = createTable( keyspaceName, tableName ).ifNotExists( )
398 .withPartitionKey( CassandraArchivaManager.DEFAULT_PRIMARY_KEY, DataTypes.TEXT )
399 .withColumn( "\"projectVersionMetadataModel.key\"", DataTypes.TEXT )
400 .withColumn( NAME.toString(), DataTypes.TEXT )
401 .withColumn( URL.toString(), DataTypes.TEXT )
402 .withCompactStorage( );
403 session.execute( table.build( ) );
405 CreateIndex index = createIndex( "\"projectVersionMetadataModel_key\"" ).ifNotExists( ).onTable( tableName ).andColumn( "\"\"projectVersionMetadataModel.key\"\"" );
406 session.execute( index.build( ) );
411 String tableName = getDependencyFamilyName( );
412 CreateTableWithOptions table = createTable( keyspaceName, tableName ).ifNotExists( )
413 .withPartitionKey( CassandraArchivaManager.DEFAULT_PRIMARY_KEY, DataTypes.TEXT )
414 .withColumn( REPOSITORY_NAME.toString( ), DataTypes.TEXT )
415 .withColumn( GROUP_ID.toString( ), DataTypes.TEXT )
416 .withColumn( ARTIFACT_ID.toString( ), DataTypes.TEXT )
417 .withColumn( VERSION.toString( ), DataTypes.TEXT )
418 .withColumn( "\"projectVersionMetadataModel.key\"", DataTypes.TEXT )
419 .withColumn( "classifier", DataTypes.TEXT )
420 .withColumn( "optional", DataTypes.TEXT )
421 .withColumn( "scope", DataTypes.TEXT )
422 .withColumn( "systemPath", DataTypes.TEXT )
423 .withColumn( "type", DataTypes.TEXT )
424 .withCompactStorage( );
426 session.execute( table.build( ) );
428 CreateIndex index = createIndex( "groupIdIdx" ).ifNotExists( ).onTable( tableName ).andColumn( GROUP_ID.toString( ) );
429 session.execute( index.build( ) );
430 index = createIndex( "\"projectVersionMetadataModel_key\"" ).ifNotExists( ).onTable( tableName ).andColumn( "\"\"projectVersionMetadataModel.key\"\"" );
431 session.execute( index.build( ) );
447 public void shutdown( )
449 if (this.cqlSession!=null) {
450 this.cqlSession.close( );
456 public boolean started( )
463 public String getRepositoryFamilyName( )
465 return repositoryFamilyName;
469 public String getNamespaceFamilyName( )
471 return namespaceFamilyName;
475 public String getProjectFamilyName( )
477 return projectFamilyName;
481 public String getProjectVersionMetadataFamilyName( )
483 return projectVersionMetadataFamilyName;
486 public String[] getProjectVersionMetadataColumns() {
487 return projectVersionMetadataColumns;
491 public String getArtifactMetadataFamilyName( )
493 return artifactMetadataFamilyName;
497 public String getMetadataFacetFamilyName( )
499 return metadataFacetFamilyName;
503 public String getMailingListFamilyName( )
505 return mailingListFamilyName;
509 public String getLicenseFamilyName( )
511 return licenseFamilyName;
515 public String getDependencyFamilyName( )
517 return dependencyFamilyName;
521 public String getChecksumFamilyName( )
523 return checksumFamilyName;
527 public DriverConfigLoader getConfigLoader( )
533 public String getKeyspaceName( )