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.google.common.base.Predicate;
23 import com.google.common.collect.Iterables;
24 import me.prettyprint.cassandra.serializers.LongSerializer;
25 import me.prettyprint.cassandra.serializers.StringSerializer;
26 import me.prettyprint.cassandra.service.template.ColumnFamilyResult;
27 import me.prettyprint.cassandra.service.template.ColumnFamilyTemplate;
28 import me.prettyprint.cassandra.service.template.ColumnFamilyUpdater;
29 import me.prettyprint.cassandra.service.template.ThriftColumnFamilyTemplate;
30 import me.prettyprint.hector.api.Keyspace;
31 import me.prettyprint.hector.api.beans.ColumnSlice;
32 import me.prettyprint.hector.api.beans.OrderedRows;
33 import me.prettyprint.hector.api.beans.Row;
34 import me.prettyprint.hector.api.exceptions.HInvalidRequestException;
35 import me.prettyprint.hector.api.factory.HFactory;
36 import me.prettyprint.hector.api.mutation.MutationResult;
37 import me.prettyprint.hector.api.mutation.Mutator;
38 import me.prettyprint.hector.api.query.QueryResult;
39 import me.prettyprint.hector.api.query.RangeSlicesQuery;
40 import org.apache.archiva.configuration.ArchivaConfiguration;
41 import org.apache.archiva.metadata.model.ArtifactMetadata;
42 import org.apache.archiva.metadata.model.CiManagement;
43 import org.apache.archiva.metadata.model.Dependency;
44 import org.apache.archiva.metadata.model.FacetedMetadata;
45 import org.apache.archiva.metadata.model.IssueManagement;
46 import org.apache.archiva.metadata.model.License;
47 import org.apache.archiva.metadata.model.MailingList;
48 import org.apache.archiva.metadata.model.MetadataFacet;
49 import org.apache.archiva.metadata.model.MetadataFacetFactory;
50 import org.apache.archiva.metadata.model.Organization;
51 import org.apache.archiva.metadata.model.ProjectMetadata;
52 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
53 import org.apache.archiva.metadata.model.ProjectVersionReference;
54 import org.apache.archiva.metadata.model.Scm;
55 import org.apache.archiva.metadata.repository.MetadataRepository;
56 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
57 import org.apache.archiva.metadata.repository.MetadataResolutionException;
58 import org.apache.archiva.metadata.repository.cassandra.model.ArtifactMetadataModel;
59 import org.apache.archiva.metadata.repository.cassandra.model.MetadataFacetModel;
60 import org.apache.archiva.metadata.repository.cassandra.model.Namespace;
61 import org.apache.archiva.metadata.repository.cassandra.model.Project;
62 import org.apache.archiva.metadata.repository.cassandra.model.ProjectVersionMetadataModel;
63 import org.apache.archiva.metadata.repository.cassandra.model.Repository;
64 import org.apache.commons.lang.StringUtils;
65 import org.modelmapper.ModelMapper;
66 import org.slf4j.Logger;
67 import org.slf4j.LoggerFactory;
69 import javax.persistence.PersistenceException;
70 import java.util.ArrayList;
71 import java.util.Collection;
72 import java.util.Collections;
73 import java.util.Date;
74 import java.util.HashMap;
75 import java.util.HashSet;
76 import java.util.Iterator;
77 import java.util.List;
80 import java.util.UUID;
82 import static org.apache.archiva.metadata.repository.cassandra.CassandraUtils.*;
83 import static org.apache.archiva.metadata.repository.cassandra.model.ColumnNames.*;
86 * @author Olivier Lamy
89 public class CassandraMetadataRepository
90 implements MetadataRepository
93 private Logger logger = LoggerFactory.getLogger( getClass() );
95 private ArchivaConfiguration configuration;
97 private final Map<String, MetadataFacetFactory> metadataFacetFactories;
99 private final CassandraArchivaManager cassandraArchivaManager;
101 private final ColumnFamilyTemplate<String, String> projectVersionMetadataTemplate;
103 private final ColumnFamilyTemplate<String, String> projectTemplate;
105 private final ColumnFamilyTemplate<String, String> artifactMetadataTemplate;
107 private final ColumnFamilyTemplate<String, String> metadataFacetTemplate;
109 private final ColumnFamilyTemplate<String, String> mailingListTemplate;
111 private final ColumnFamilyTemplate<String, String> licenseTemplate;
113 private final ColumnFamilyTemplate<String, String> dependencyTemplate;
115 private final Keyspace keyspace;
117 private final StringSerializer ss = StringSerializer.get();
119 public CassandraMetadataRepository( Map<String, MetadataFacetFactory> metadataFacetFactories,
120 ArchivaConfiguration configuration,
121 CassandraArchivaManager cassandraArchivaManager )
123 this.metadataFacetFactories = metadataFacetFactories;
124 this.configuration = configuration;
125 this.cassandraArchivaManager = cassandraArchivaManager;
126 this.keyspace = cassandraArchivaManager.getKeyspace();
128 this.projectVersionMetadataTemplate =
129 new ThriftColumnFamilyTemplate<>( cassandraArchivaManager.getKeyspace(), //
130 cassandraArchivaManager.getProjectVersionMetadataFamilyName(), //
131 StringSerializer.get(), //
132 StringSerializer.get() );
134 this.projectTemplate = new ThriftColumnFamilyTemplate<>( cassandraArchivaManager.getKeyspace(), //
135 cassandraArchivaManager.getProjectFamilyName(), //
137 StringSerializer.get(), //
138 StringSerializer.get() );
140 this.artifactMetadataTemplate = new ThriftColumnFamilyTemplate<>( cassandraArchivaManager.getKeyspace(), //
141 cassandraArchivaManager.getArtifactMetadataFamilyName(),
142 StringSerializer.get(), //
143 StringSerializer.get() );
145 this.metadataFacetTemplate = new ThriftColumnFamilyTemplate<>( cassandraArchivaManager.getKeyspace(), //
146 cassandraArchivaManager.getMetadataFacetFamilyName(),
148 StringSerializer.get(), //
149 StringSerializer.get() );
151 this.mailingListTemplate = new ThriftColumnFamilyTemplate<>( cassandraArchivaManager.getKeyspace(), //
152 cassandraArchivaManager.getMailingListFamilyName(),
154 StringSerializer.get(), //
155 StringSerializer.get() );
157 this.licenseTemplate = new ThriftColumnFamilyTemplate<>( cassandraArchivaManager.getKeyspace(), //
158 cassandraArchivaManager.getLicenseFamilyName(),
160 StringSerializer.get(), //
161 StringSerializer.get() );
163 this.dependencyTemplate = new ThriftColumnFamilyTemplate<>( cassandraArchivaManager.getKeyspace(), //
164 cassandraArchivaManager.getDependencyFamilyName(),
166 StringSerializer.get(), //
167 StringSerializer.get() );
172 * if the repository doesn't exist it will be created
174 * @param repositoryId
177 public Repository getOrCreateRepository( String repositoryId )
178 throws MetadataRepositoryException
180 String cf = cassandraArchivaManager.getRepositoryFamilyName();
182 QueryResult<OrderedRows<String, String, String>> result = HFactory //
183 .createRangeSlicesQuery( keyspace, StringSerializer.get(), StringSerializer.get(),
184 StringSerializer.get() ) //
185 .setColumnFamily( cf ) //
186 .setColumnNames( REPOSITORY_NAME.toString() ) //
187 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
190 if ( result.get().getCount() < 1 )
192 // we need to create the repository
193 Repository repository = new Repository( repositoryId );
197 MutationResult mutationResult = HFactory.createMutator( keyspace, StringSerializer.get() ) //
198 .addInsertion( repositoryId, cf,
199 CassandraUtils.column( REPOSITORY_NAME.toString(), repository.getName() ) ) //
201 logger.debug( "time to insert repository: {}", mutationResult.getExecutionTimeMicro() );
204 catch ( HInvalidRequestException e )
206 logger.error( e.getMessage(), e );
207 throw new MetadataRepositoryException( e.getMessage(), e );
212 return new Repository(
213 result.get().getList().get( 0 ).getColumnSlice().getColumnByName( REPOSITORY_NAME.toString() ).getValue() );
217 protected Repository getRepository( String repositoryId )
218 throws MetadataRepositoryException
221 QueryResult<OrderedRows<String, String, String>> result = HFactory //
222 .createRangeSlicesQuery( keyspace, StringSerializer.get(), StringSerializer.get(),
223 StringSerializer.get() ) //
224 .setColumnFamily( cassandraArchivaManager.getRepositoryFamilyName() ) //
225 .setColumnNames( REPOSITORY_NAME.toString() ) //
226 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
228 return ( result.get().getCount() > 0 ) ? new Repository( repositoryId ) : null;
232 public void updateNamespace( String repositoryId, String namespaceId )
233 throws MetadataRepositoryException
235 updateOrAddNamespace( repositoryId, namespaceId );
238 private Namespace updateOrAddNamespace( String repositoryId, String namespaceId )
239 throws MetadataRepositoryException
243 Repository repository = getOrCreateRepository( repositoryId );
246 new Namespace.KeyBuilder().withNamespace( namespaceId ).withRepositoryId( repositoryId ).build();
248 Namespace namespace = getNamespace( repositoryId, namespaceId );
249 if ( namespace == null )
251 String cf = cassandraArchivaManager.getNamespaceFamilyName();
252 namespace = new Namespace( namespaceId, repository );
253 HFactory.createMutator( keyspace, StringSerializer.get() )
255 .addInsertion( key, cf, CassandraUtils.column( NAME.toString(), namespace.getName() ) ) //
256 .addInsertion( key, cf, CassandraUtils.column( REPOSITORY_NAME.toString(), repository.getName() ) ) //
262 catch ( HInvalidRequestException e )
264 logger.error( e.getMessage(), e );
265 throw new MetadataRepositoryException( e.getMessage(), e );
269 protected Namespace getNamespace( String repositoryId, String namespaceId )
272 QueryResult<OrderedRows<String, String, String>> result = HFactory //
273 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
274 .setColumnFamily( cassandraArchivaManager.getNamespaceFamilyName() ) //
275 .setColumnNames( REPOSITORY_NAME.toString(), NAME.toString() ) //
276 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
277 .addEqualsExpression( NAME.toString(), namespaceId ) //
279 if ( result.get().getCount() > 0 )
281 ColumnSlice<String, String> columnSlice = result.get().getList().get( 0 ).getColumnSlice();
282 return new Namespace( getStringValue( columnSlice, NAME.toString() ), //
283 new Repository( getStringValue( columnSlice, REPOSITORY_NAME.toString() ) ) );
291 public void removeNamespace( String repositoryId, String namespaceId )
292 throws MetadataRepositoryException
297 String key = new Namespace.KeyBuilder() //
298 .withNamespace( namespaceId ) //
299 .withRepositoryId( repositoryId ) //
302 HFactory.createMutator( cassandraArchivaManager.getKeyspace(), new StringSerializer() ) //
303 .addDeletion( key, cassandraArchivaManager.getNamespaceFamilyName() ) //
306 QueryResult<OrderedRows<String, String, String>> result = HFactory //
307 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
308 .setColumnFamily( cassandraArchivaManager.getProjectFamilyName() ) //
309 .setColumnNames( REPOSITORY_NAME.toString() ) //
310 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
311 .addEqualsExpression( NAMESPACE_ID.toString(), namespaceId ) //
314 for ( Row<String, String, String> row : result.get() )
316 this.projectTemplate.deleteRow( row.getKey() );
320 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
321 .setColumnFamily( cassandraArchivaManager.getProjectVersionMetadataFamilyName() ) //
322 .setColumnNames( REPOSITORY_NAME.toString() ) //
323 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
324 .addEqualsExpression( NAMESPACE_ID.toString(), namespaceId ) //
327 for ( Row<String, String, String> row : result.get() )
329 this.projectVersionMetadataTemplate.deleteRow( row.getKey() );
330 removeMailingList( row.getKey() );
334 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
335 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataFamilyName() ) //
336 .setColumnNames( REPOSITORY_NAME.toString() ) //
337 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
338 .addEqualsExpression( NAMESPACE_ID.toString(), namespaceId ) //
341 for ( Row<String, String, String> row : result.get() )
343 this.artifactMetadataTemplate.deleteRow( row.getKey() );
347 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
348 .setColumnFamily( cassandraArchivaManager.getMetadataFacetFamilyName() ) //
349 .setColumnNames( REPOSITORY_NAME.toString() ) //
350 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
351 .addEqualsExpression( NAMESPACE_ID.toString(), namespaceId ) //
354 for ( Row<String, String, String> row : result.get() )
356 this.metadataFacetTemplate.deleteRow( row.getKey() );
360 catch ( HInvalidRequestException e )
362 logger.error( e.getMessage(), e );
363 throw new MetadataRepositoryException( e.getMessage(), e );
369 public void removeRepository( final String repositoryId )
370 throws MetadataRepositoryException
373 // TODO use cql queries to delete all
374 List<String> namespacesKey = new ArrayList<>();
376 QueryResult<OrderedRows<String, String, String>> result = HFactory //
377 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
378 .setColumnFamily( cassandraArchivaManager.getNamespaceFamilyName() ) //
379 .setColumnNames( REPOSITORY_NAME.toString() ) //
380 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
383 for ( Row<String, String, String> row : result.get().getList() )
385 namespacesKey.add( row.getKey() );
388 HFactory.createMutator( cassandraArchivaManager.getKeyspace(), ss ) //
389 .addDeletion( namespacesKey, cassandraArchivaManager.getNamespaceFamilyName() ) //
392 //delete repositoryId
393 HFactory.createMutator( cassandraArchivaManager.getKeyspace(), ss ) //
394 .addDeletion( repositoryId, cassandraArchivaManager.getRepositoryFamilyName() ) //
398 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
399 .setColumnFamily( cassandraArchivaManager.getProjectFamilyName() ) //
400 .setColumnNames( REPOSITORY_NAME.toString() ) //
401 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
404 for ( Row<String, String, String> row : result.get() )
406 this.projectTemplate.deleteRow( row.getKey() );
410 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
411 .setColumnFamily( cassandraArchivaManager.getProjectVersionMetadataFamilyName() ) //
412 .setColumnNames( REPOSITORY_NAME.toString() ) //
413 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
416 for ( Row<String, String, String> row : result.get() )
418 this.projectVersionMetadataTemplate.deleteRow( row.getKey() );
419 removeMailingList( row.getKey() );
423 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
424 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataFamilyName() ) //
425 .setColumnNames( REPOSITORY_NAME.toString() ) //
426 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
429 for ( Row<String, String, String> row : result.get() )
431 this.artifactMetadataTemplate.deleteRow( row.getKey() );
435 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
436 .setColumnFamily( cassandraArchivaManager.getMetadataFacetFamilyName() ) //
437 .setColumnNames( REPOSITORY_NAME.toString() ) //
438 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
441 for ( Row<String, String, String> row : result.get() )
443 this.metadataFacetTemplate.deleteRow( row.getKey() );
450 public Collection<String> getRepositories()
451 throws MetadataRepositoryException
455 logger.debug( "getRepositories" );
457 final QueryResult<OrderedRows<String, String, String>> cResult = //
458 HFactory.createRangeSlicesQuery( cassandraArchivaManager.getKeyspace(), //
460 .setColumnFamily( cassandraArchivaManager.getRepositoryFamilyName() ) //
461 .setColumnNames( REPOSITORY_NAME.toString() ) //
462 .setRange( null, null, false, Integer.MAX_VALUE ) //
465 List<String> repoIds = new ArrayList<>( cResult.get().getCount() );
467 for ( Row<String, String, String> row : cResult.get() )
469 repoIds.add( getStringValue( row.getColumnSlice(), REPOSITORY_NAME.toString() ) );
474 catch ( PersistenceException e )
476 throw new MetadataRepositoryException( e.getMessage(), e );
481 // FIXME this one need peformance improvement maybe a cache?
483 public Collection<String> getRootNamespaces( final String repoId )
484 throws MetadataResolutionException
487 QueryResult<OrderedRows<String, String, String>> result = HFactory //
488 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
489 .setColumnFamily( cassandraArchivaManager.getNamespaceFamilyName() ) //
490 .setColumnNames( NAME.toString() ) //
491 .addEqualsExpression( REPOSITORY_NAME.toString(), repoId ) //
494 Set<String> namespaces = new HashSet<String>( result.get().getCount() );
496 for ( Row<String, String, String> row : result.get() )
498 namespaces.add( StringUtils.substringBefore( getStringValue( row.getColumnSlice(), NAME.toString() ), "." ) );
504 // FIXME this one need peformance improvement maybe a cache?
506 public Collection<String> getNamespaces( final String repoId, final String namespaceId )
507 throws MetadataResolutionException
510 QueryResult<OrderedRows<String, String, String>> result = HFactory //
511 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
512 .setColumnFamily( cassandraArchivaManager.getNamespaceFamilyName() ) //
513 .setColumnNames( NAME.toString() ) //
514 .addEqualsExpression( REPOSITORY_NAME.toString(), repoId ) //
517 List<String> namespaces = new ArrayList<>( result.get().getCount() );
519 for ( Row<String, String, String> row : result.get() )
521 String currentNamespace = getStringValue( row.getColumnSlice(), NAME.toString() );
522 if ( StringUtils.startsWith( currentNamespace, namespaceId ) //
523 && ( StringUtils.length( currentNamespace ) > StringUtils.length( namespaceId ) ) )
525 // store after namespaceId '.' but before next '.'
526 // call org namespace org.apache.maven.shared -> stored apache
528 String calledNamespace = StringUtils.endsWith( namespaceId, "." ) ? namespaceId : namespaceId + ".";
529 String storedNamespace = StringUtils.substringAfter( currentNamespace, calledNamespace );
531 storedNamespace = StringUtils.substringBefore( storedNamespace, "." );
533 namespaces.add( storedNamespace );
541 // only use for testing purpose
542 protected List<String> getNamespaces( final String repoId )
543 throws MetadataResolutionException
546 QueryResult<OrderedRows<String, String, String>> result = HFactory //
547 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
548 .setColumnFamily( cassandraArchivaManager.getNamespaceFamilyName() ) //
549 .setColumnNames( NAME.toString() ) //
550 .addEqualsExpression( REPOSITORY_NAME.toString(), repoId ) //
553 List<String> namespaces = new ArrayList<>( result.get().getCount() );
555 for ( Row<String, String, String> row : result.get() )
557 namespaces.add( getStringValue( row.getColumnSlice(), NAME.toString() ) );
565 public void updateProject( String repositoryId, ProjectMetadata projectMetadata )
566 throws MetadataRepositoryException
569 QueryResult<OrderedRows<String, String, String>> result = HFactory //
570 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
571 .setColumnFamily( cassandraArchivaManager.getProjectFamilyName() ) //
572 .setColumnNames( PROJECT_ID.toString() ) //
573 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
574 .addEqualsExpression( NAMESPACE_ID.toString(), projectMetadata.getNamespace() ) //
575 .addEqualsExpression( PROJECT_ID.toString(), projectMetadata.getId() ) //
578 // project exists ? if yes return nothing to update here
579 if ( result.get().getCount() > 0 )
585 Namespace namespace = updateOrAddNamespace( repositoryId, projectMetadata.getNamespace() );
588 new Project.KeyBuilder().withProjectId( projectMetadata.getId() ).withNamespace( namespace ).build();
590 String cf = cassandraArchivaManager.getProjectFamilyName();
591 projectTemplate.createMutator()
593 .addInsertion( key, cf, CassandraUtils.column( PROJECT_ID.toString(), projectMetadata.getId() ) ) //
594 .addInsertion( key, cf, CassandraUtils.column( REPOSITORY_NAME.toString(), repositoryId ) ) //
595 .addInsertion( key, cf, CassandraUtils.column( NAMESPACE_ID.toString(), projectMetadata.getNamespace() ) )//
601 public Collection<String> getProjects( final String repoId, final String namespace )
602 throws MetadataResolutionException
605 QueryResult<OrderedRows<String, String, String>> result = HFactory //
606 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
607 .setColumnFamily( cassandraArchivaManager.getProjectFamilyName() ) //
608 .setColumnNames( PROJECT_ID.toString() ) //
609 .addEqualsExpression( REPOSITORY_NAME.toString(), repoId ) //
610 .addEqualsExpression( NAMESPACE_ID.toString(), namespace ) //
613 final Set<String> projects = new HashSet<String>( result.get().getCount() );
615 for ( Row<String, String, String> row : result.get() )
617 projects.add( getStringValue( row.getColumnSlice(), PROJECT_ID.toString() ) );
624 public void removeProject( final String repositoryId, final String namespaceId, final String projectId )
625 throws MetadataRepositoryException
628 String key = new Project.KeyBuilder() //
629 .withProjectId( projectId ) //
630 .withNamespace( new Namespace( namespaceId, new Repository( repositoryId ) ) ) //
633 this.projectTemplate.deleteRow( key );
635 QueryResult<OrderedRows<String, String, String>> result = HFactory //
636 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
637 .setColumnFamily( cassandraArchivaManager.getProjectVersionMetadataFamilyName() ) //
638 .setColumnNames( ID.toString() ) //
639 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
640 .addEqualsExpression( NAMESPACE_ID.toString(), namespaceId ) //
641 .addEqualsExpression( PROJECT_ID.toString(), projectId ) //
644 for ( Row<String, String, String> row : result.get() )
646 this.projectVersionMetadataTemplate.deleteRow( row.getKey() );
647 removeMailingList( row.getKey() );
651 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
652 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataFamilyName() ) //
653 .setColumnNames( PROJECT_ID.toString() ) //
654 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
655 .addEqualsExpression( NAMESPACE_ID.toString(), namespaceId ) //
656 .addEqualsExpression( PROJECT_ID.toString(), projectId ) //
659 for ( Row<String, String, String> row : result.get() )
661 this.artifactMetadataTemplate.deleteRow( row.getKey() );
666 public Collection<String> getProjectVersions( final String repoId, final String namespace, final String projectId )
667 throws MetadataResolutionException
670 QueryResult<OrderedRows<String, String, String>> result = HFactory //
671 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
672 .setColumnFamily( cassandraArchivaManager.getProjectVersionMetadataFamilyName() ) //
673 .setColumnNames( PROJECT_VERSION.toString() ) //
674 .addEqualsExpression( REPOSITORY_NAME.toString(), repoId ) //
675 .addEqualsExpression( NAMESPACE_ID.toString(), namespace ) //
676 .addEqualsExpression( PROJECT_ID.toString(), projectId ) //
679 int count = result.get().getCount();
683 return Collections.emptyList();
686 Set<String> versions = new HashSet<String>( count );
688 for ( Row<String, String, String> orderedRows : result.get() )
690 versions.add( getStringValue( orderedRows.getColumnSlice(), PROJECT_VERSION.toString() ) );
698 public ProjectMetadata getProject( final String repoId, final String namespace, final String id )
699 throws MetadataResolutionException
702 QueryResult<OrderedRows<String, String, String>> result = HFactory //
703 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
704 .setColumnFamily( cassandraArchivaManager.getProjectFamilyName() ) //
705 .setColumnNames( PROJECT_ID.toString() ) //
706 .addEqualsExpression( REPOSITORY_NAME.toString(), repoId ) //
707 .addEqualsExpression( NAMESPACE_ID.toString(), namespace ) //
708 .addEqualsExpression( PROJECT_ID.toString(), id ) //
711 int count = result.get().getCount();
718 ProjectMetadata projectMetadata = new ProjectMetadata();
719 projectMetadata.setId( id );
720 projectMetadata.setNamespace( namespace );
722 logger.debug( "getProject repoId: {}, namespace: {}, projectId: {} -> {}", repoId, namespace, id,
725 return projectMetadata;
728 protected ProjectVersionMetadataModel mapProjectVersionMetadataModel( ColumnSlice<String, String> columnSlice )
730 ProjectVersionMetadataModel projectVersionMetadataModel = new ProjectVersionMetadataModel();
731 projectVersionMetadataModel.setId( getStringValue( columnSlice, ID.toString() ) );
732 projectVersionMetadataModel.setDescription( getStringValue( columnSlice, DESCRIPTION.toString() ) );
733 projectVersionMetadataModel.setName( getStringValue( columnSlice, NAME.toString() ) );
734 Namespace namespace = new Namespace( getStringValue( columnSlice, NAMESPACE_ID.toString() ), //
735 new Repository( getStringValue( columnSlice, REPOSITORY_NAME.toString() ) ) );
736 projectVersionMetadataModel.setNamespace( namespace );
737 projectVersionMetadataModel.setIncomplete(
738 Boolean.parseBoolean( getStringValue( columnSlice, "incomplete" ) ) );
739 projectVersionMetadataModel.setProjectId( getStringValue( columnSlice, PROJECT_ID.toString() ) );
740 projectVersionMetadataModel.setUrl( getStringValue( columnSlice, URL.toString() ) );
741 return projectVersionMetadataModel;
746 public void updateProjectVersion( String repositoryId, String namespaceId, String projectId,
747 ProjectVersionMetadata versionMetadata )
748 throws MetadataRepositoryException
752 Namespace namespace = getNamespace( repositoryId, namespaceId );
754 if ( namespace == null )
756 updateOrAddNamespace( repositoryId, namespaceId );
759 if ( getProject( repositoryId, namespaceId, projectId ) == null )
761 ProjectMetadata projectMetadata = new ProjectMetadata();
762 projectMetadata.setNamespace( namespaceId );
763 projectMetadata.setId( projectId );
764 updateProject( repositoryId, projectMetadata );
768 catch ( MetadataResolutionException e )
770 throw new MetadataRepositoryException( e.getMessage(), e );
773 QueryResult<OrderedRows<String, String, String>> result = HFactory //
774 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
775 .setColumnFamily( cassandraArchivaManager.getProjectVersionMetadataFamilyName() ) //
776 .setColumnNames( PROJECT_VERSION.toString() ) //
777 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
778 .addEqualsExpression( NAMESPACE_ID.toString(), namespaceId ) //
779 .addEqualsExpression( PROJECT_ID.toString(), projectId ) //
780 .addEqualsExpression( PROJECT_VERSION.toString(), versionMetadata.getId() ) //
783 ProjectVersionMetadataModel projectVersionMetadataModel = null;
784 boolean creation = true;
785 if ( result.get().getCount() > 0 )
787 projectVersionMetadataModel =
788 mapProjectVersionMetadataModel( result.get().getList().get( 0 ).getColumnSlice() );
793 projectVersionMetadataModel = getModelMapper().map( versionMetadata, ProjectVersionMetadataModel.class );
796 projectVersionMetadataModel.setProjectId( projectId );
797 projectVersionMetadataModel.setNamespace( new Namespace( namespaceId, new Repository( repositoryId ) ) );
799 projectVersionMetadataModel.setCiManagement( versionMetadata.getCiManagement() );
800 projectVersionMetadataModel.setIssueManagement( versionMetadata.getIssueManagement() );
801 projectVersionMetadataModel.setOrganization( versionMetadata.getOrganization() );
802 projectVersionMetadataModel.setScm( versionMetadata.getScm() );
804 projectVersionMetadataModel.setMailingLists( versionMetadata.getMailingLists() );
805 projectVersionMetadataModel.setDependencies( versionMetadata.getDependencies() );
806 projectVersionMetadataModel.setLicenses( versionMetadata.getLicenses() );
808 // we don't test of repository and namespace really exist !
809 String key = new ProjectVersionMetadataModel.KeyBuilder() //
810 .withRepository( repositoryId ) //
811 .withNamespace( namespaceId ) //
812 .withProjectId( projectId ) //
813 .withProjectVersion( versionMetadata.getVersion() ) //
814 .withId( versionMetadata.getId() ) //
817 // FIXME nested objects to store!!!
820 String cf = cassandraArchivaManager.getProjectVersionMetadataFamilyName();
821 Mutator<String> mutator = projectVersionMetadataTemplate.createMutator()
823 .addInsertion( key, cf, column( PROJECT_ID.toString(), projectId ) ) //
824 .addInsertion( key, cf, column( REPOSITORY_NAME.toString(), repositoryId ) ) //
825 .addInsertion( key, cf, column( NAMESPACE_ID.toString(), namespaceId ) )//
826 .addInsertion( key, cf, column( PROJECT_VERSION.toString(), versionMetadata.getVersion() ) ); //
828 addInsertion( mutator, key, cf, DESCRIPTION.toString(), versionMetadata.getDescription() );
830 addInsertion( mutator, key, cf, NAME.toString(), versionMetadata.getName() );
832 addInsertion( mutator, key, cf, "incomplete", Boolean.toString( versionMetadata.isIncomplete() ) );
834 addInsertion( mutator, key, cf, URL.toString(), versionMetadata.getUrl() );
836 CiManagement ci = versionMetadata.getCiManagement();
839 addInsertion( mutator, key, cf, "ciManagement.system", ci.getSystem() );
840 addInsertion( mutator, key, cf, "ciManagement.url", ci.getUrl() );
845 IssueManagement issueManagement = versionMetadata.getIssueManagement();
847 if ( issueManagement != null )
849 addInsertion( mutator, key, cf, "issueManagement.system", issueManagement.getSystem() );
850 addInsertion( mutator, key, cf, "issueManagement.url", issueManagement.getUrl() );
855 Organization organization = versionMetadata.getOrganization();
856 if ( organization != null )
858 addInsertion( mutator, key, cf, "organization.name", organization.getName() );
859 addInsertion( mutator, key, cf, "organization.url", organization.getUrl() );
864 Scm scm = versionMetadata.getScm();
867 addInsertion( mutator, key, cf, "scm.url", scm.getUrl() );
868 addInsertion( mutator, key, cf, "scm.connection", scm.getConnection() );
869 addInsertion( mutator, key, cf, "scm.developerConnection", scm.getDeveloperConnection() );
873 recordMailingList( key, versionMetadata.getMailingLists() );
875 recordLicenses( key, versionMetadata.getLicenses() );
877 recordDependencies( key, versionMetadata.getDependencies(), repositoryId );
879 MutationResult mutationResult = mutator.execute();
883 ColumnFamilyUpdater<String, String> updater = projectVersionMetadataTemplate.createUpdater( key );
884 addUpdateStringValue( updater, PROJECT_ID.toString(), projectId );
885 addUpdateStringValue( updater, REPOSITORY_NAME.toString(), repositoryId );
886 addUpdateStringValue( updater, NAMESPACE_ID.toString(), namespaceId );
887 addUpdateStringValue( updater, PROJECT_VERSION.toString(), versionMetadata.getVersion() );
888 addUpdateStringValue( updater, DESCRIPTION.toString(), versionMetadata.getDescription() );
890 addUpdateStringValue( updater, NAME.toString(), versionMetadata.getName() );
892 updater.setString( "incomplete", Boolean.toString( versionMetadata.isIncomplete() ) );
893 addUpdateStringValue( updater, URL.toString(), versionMetadata.getUrl() );
896 CiManagement ci = versionMetadata.getCiManagement();
899 addUpdateStringValue( updater, "ciManagement.system", ci.getSystem() );
900 addUpdateStringValue( updater, "ciManagement.url", ci.getUrl() );
904 IssueManagement issueManagement = versionMetadata.getIssueManagement();
905 if ( issueManagement != null )
907 addUpdateStringValue( updater, "issueManagement.system", issueManagement.getSystem() );
908 addUpdateStringValue( updater, "issueManagement.url", issueManagement.getUrl() );
912 Organization organization = versionMetadata.getOrganization();
913 if ( organization != null )
915 addUpdateStringValue( updater, "organization.name", organization.getName() );
916 addUpdateStringValue( updater, "organization.url", organization.getUrl() );
920 Scm scm = versionMetadata.getScm();
923 addUpdateStringValue( updater, "scm.url", scm.getUrl() );
924 addUpdateStringValue( updater, "scm.connection", scm.getConnection() );
925 addUpdateStringValue( updater, "scm.developerConnection", scm.getDeveloperConnection() );
929 // update is a delete record
930 removeMailingList( key );
931 recordMailingList( key, versionMetadata.getMailingLists() );
933 removeLicenses( key );
934 recordLicenses( key, versionMetadata.getLicenses() );
936 removeDependencies( key );
937 recordDependencies( key, versionMetadata.getDependencies(), repositoryId );
939 projectVersionMetadataTemplate.update( updater );
943 ArtifactMetadataModel artifactMetadataModel = new ArtifactMetadataModel();
944 artifactMetadataModel.setRepositoryId( repositoryId );
945 artifactMetadataModel.setNamespace( namespaceId );
946 artifactMetadataModel.setProject( projectId );
947 artifactMetadataModel.setProjectVersion( versionMetadata.getVersion() );
948 artifactMetadataModel.setVersion( versionMetadata.getVersion() );
949 updateFacets( versionMetadata, artifactMetadataModel );
955 public ProjectVersionMetadata getProjectVersion( final String repoId, final String namespace,
956 final String projectId, final String projectVersion )
957 throws MetadataResolutionException
960 QueryResult<OrderedRows<String, String, String>> result = HFactory //
961 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
962 .setColumnFamily( cassandraArchivaManager.getProjectVersionMetadataFamilyName() ) //
963 .setColumnNames( PROJECT_VERSION.toString() ) //
964 .addEqualsExpression( REPOSITORY_NAME.toString(), repoId ) //
965 .addEqualsExpression( NAMESPACE_ID.toString(), namespace ) //
966 .addEqualsExpression( PROJECT_ID.toString(), projectId ) //
967 .addEqualsExpression( PROJECT_VERSION.toString(), projectVersion ) //
970 if ( result.get().getCount() < 1 )
975 String key = result.get().iterator().next().getKey();
977 ColumnFamilyResult<String, String> columnFamilyResult = this.projectVersionMetadataTemplate.queryColumns( key );
979 if ( !columnFamilyResult.hasResults() )
984 ProjectVersionMetadata projectVersionMetadata = new ProjectVersionMetadata();
985 projectVersionMetadata.setId( columnFamilyResult.getString( PROJECT_VERSION.toString() ) );
986 projectVersionMetadata.setDescription( columnFamilyResult.getString( DESCRIPTION.toString() ) );
987 projectVersionMetadata.setName( columnFamilyResult.getString( NAME.toString() ) );
989 projectVersionMetadata.setIncomplete( Boolean.parseBoolean( columnFamilyResult.getString( "incomplete" ) ) );
991 projectVersionMetadata.setUrl( columnFamilyResult.getString( URL.toString() ) );
993 String ciUrl = columnFamilyResult.getString( "ciManagement.url" );
994 String ciSystem = columnFamilyResult.getString( "ciManagement.system" );
996 if ( StringUtils.isNotEmpty( ciSystem ) || StringUtils.isNotEmpty( ciUrl ) )
998 projectVersionMetadata.setCiManagement( new CiManagement( ciSystem, ciUrl ) );
1002 String issueUrl = columnFamilyResult.getString( "issueManagement.url" );
1003 String issueSystem = columnFamilyResult.getString( "issueManagement.system" );
1004 if ( StringUtils.isNotEmpty( issueSystem ) || StringUtils.isNotEmpty( issueUrl ) )
1006 projectVersionMetadata.setIssueManagement( new IssueManagement( issueSystem, issueUrl ) );
1010 String organizationUrl = columnFamilyResult.getString( "organization.url" );
1011 String organizationName = columnFamilyResult.getString( "organization.name" );
1012 if ( StringUtils.isNotEmpty( organizationUrl ) || StringUtils.isNotEmpty( organizationName ) )
1014 projectVersionMetadata.setOrganization( new Organization( organizationName, organizationUrl ) );
1018 String devConn = columnFamilyResult.getString( "scm.developerConnection" );
1019 String conn = columnFamilyResult.getString( "scm.connection" );
1020 String url = columnFamilyResult.getString( "scm.url" );
1021 if ( StringUtils.isNotEmpty( devConn ) || StringUtils.isNotEmpty( conn ) || StringUtils.isNotEmpty( url ) )
1023 projectVersionMetadata.setScm( new Scm( conn, devConn, url ) );
1026 projectVersionMetadata.setMailingLists( getMailingLists( key ) );
1027 projectVersionMetadata.setLicenses( getLicenses( key ) );
1028 projectVersionMetadata.setDependencies( getDependencies( key ) );
1031 result = HFactory //
1032 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1033 .setColumnFamily( cassandraArchivaManager.getMetadataFacetFamilyName() ) //
1034 .setColumnNames( FACET_ID.toString(), KEY.toString(), VALUE.toString(), NAME.toString() ) //
1035 .addEqualsExpression( REPOSITORY_NAME.toString(), repoId ) //
1036 .addEqualsExpression( NAMESPACE_ID.toString(), namespace ) //
1037 .addEqualsExpression( PROJECT_ID.toString(), projectId ) //
1038 .addEqualsExpression( PROJECT_VERSION.toString(), projectVersion ) //
1041 Map<String, Map<String, String>> metadataFacetsPerFacetIds = new HashMap<>();
1043 for ( Row<String, String, String> row : result.get() )
1045 ColumnSlice<String, String> columnSlice = row.getColumnSlice();
1046 String facetId = getStringValue( columnSlice, FACET_ID.toString() );
1047 Map<String, String> metaValues = metadataFacetsPerFacetIds.get( facetId );
1048 if ( metaValues == null )
1050 metaValues = new HashMap<>();
1051 metadataFacetsPerFacetIds.put( facetId, metaValues );
1053 metaValues.put( getStringValue( columnSlice, KEY.toString() ), getStringValue( columnSlice, VALUE.toString() ) );
1056 if ( !metadataFacetsPerFacetIds.isEmpty() )
1058 for ( Map.Entry<String, Map<String, String>> entry : metadataFacetsPerFacetIds.entrySet() )
1060 MetadataFacetFactory metadataFacetFactory = metadataFacetFactories.get( entry.getKey() );
1061 if ( metadataFacetFactory != null )
1063 MetadataFacet metadataFacet = metadataFacetFactory.createMetadataFacet();
1064 metadataFacet.fromProperties( entry.getValue() );
1065 projectVersionMetadata.addFacet( metadataFacet );
1070 return projectVersionMetadata;
1073 protected void recordMailingList( String projectVersionMetadataKey, List<MailingList> mailingLists )
1075 if ( mailingLists == null || mailingLists.isEmpty() )
1079 Mutator<String> mailingMutator = this.mailingListTemplate.createMutator();
1080 for ( MailingList mailingList : mailingLists )
1082 // we don't care about the key as the real used one with the projectVersionMetadata
1083 String keyMailingList = UUID.randomUUID().toString();
1084 String cfMailingList = cassandraArchivaManager.getMailingListFamilyName();
1086 addInsertion( mailingMutator, keyMailingList, cfMailingList, "projectVersionMetadataModel.key",
1087 projectVersionMetadataKey );
1088 addInsertion( mailingMutator, keyMailingList, cfMailingList, NAME.toString(), mailingList.getName() );
1089 addInsertion( mailingMutator, keyMailingList, cfMailingList, "mainArchiveUrl",
1090 mailingList.getMainArchiveUrl() );
1091 addInsertion( mailingMutator, keyMailingList, cfMailingList, "postAddress", mailingList.getPostAddress() );
1092 addInsertion( mailingMutator, keyMailingList, cfMailingList, "subscribeAddress",
1093 mailingList.getSubscribeAddress() );
1094 addInsertion( mailingMutator, keyMailingList, cfMailingList, "unsubscribeAddress",
1095 mailingList.getUnsubscribeAddress() );
1097 for ( String otherArchive : mailingList.getOtherArchives() )
1099 addInsertion( mailingMutator, keyMailingList, cfMailingList, "otherArchive." + idx, otherArchive );
1104 mailingMutator.execute();
1107 protected void removeMailingList( String projectVersionMetadataKey )
1110 QueryResult<OrderedRows<String, String, String>> result =
1111 HFactory.createRangeSlicesQuery( cassandraArchivaManager.getKeyspace(), ss, ss, ss ) //
1112 .setColumnFamily( cassandraArchivaManager.getMailingListFamilyName() ) //
1113 .setColumnNames( NAME.toString() ) //
1114 .setRowCount( Integer.MAX_VALUE ) //
1115 .addEqualsExpression( "projectVersionMetadataModel.key", projectVersionMetadataKey ) //
1118 if ( result.get().getCount() < 1 )
1123 for ( Row<String, String, String> row : result.get() )
1125 this.mailingListTemplate.deleteRow( row.getKey() );
1130 protected List<MailingList> getMailingLists( String projectVersionMetadataKey )
1132 List<MailingList> mailingLists = new ArrayList<>();
1134 QueryResult<OrderedRows<String, String, String>> result =
1135 HFactory.createRangeSlicesQuery( cassandraArchivaManager.getKeyspace(), ss, ss, ss ) //
1136 .setColumnFamily( cassandraArchivaManager.getMailingListFamilyName() ) //
1137 .setColumnNames( NAME.toString() ) //
1138 .setRowCount( Integer.MAX_VALUE ) //
1139 .addEqualsExpression( "projectVersionMetadataModel.key", projectVersionMetadataKey ) //
1141 for ( Row<String, String, String> row : result.get() )
1143 ColumnFamilyResult<String, String> columnFamilyResult =
1144 this.mailingListTemplate.queryColumns( row.getKey() );
1146 MailingList mailingList = new MailingList();
1147 mailingList.setName( columnFamilyResult.getString( NAME.toString() ) );
1148 mailingList.setMainArchiveUrl( columnFamilyResult.getString( "mainArchiveUrl" ) );
1149 mailingList.setPostAddress( columnFamilyResult.getString( "postAddress" ) );
1150 mailingList.setSubscribeAddress( columnFamilyResult.getString( "subscribeAddress" ) );
1151 mailingList.setUnsubscribeAddress( columnFamilyResult.getString( "unsubscribeAddress" ) );
1153 List<String> otherArchives = new ArrayList<>();
1155 for ( String columnName : columnFamilyResult.getColumnNames() )
1157 if ( StringUtils.startsWith( columnName, "otherArchive." ) )
1159 otherArchives.add( columnFamilyResult.getString( columnName ) );
1163 mailingList.setOtherArchives( otherArchives );
1164 mailingLists.add( mailingList );
1167 return mailingLists;
1170 protected void recordLicenses( String projectVersionMetadataKey, List<License> licenses )
1173 if ( licenses == null || licenses.isEmpty() )
1177 Mutator<String> licenseMutator = this.licenseTemplate.createMutator();
1179 for ( License license : licenses )
1181 // we don't care about the key as the real used one with the projectVersionMetadata
1182 String keyLicense = UUID.randomUUID().toString();
1183 String cfLicense = cassandraArchivaManager.getLicenseFamilyName();
1185 addInsertion( licenseMutator, keyLicense, cfLicense, "projectVersionMetadataModel.key",
1186 projectVersionMetadataKey );
1188 addInsertion( licenseMutator, keyLicense, cfLicense, NAME.toString(), license.getName() );
1190 addInsertion( licenseMutator, keyLicense, cfLicense, URL.toString(), license.getUrl() );
1193 licenseMutator.execute();
1196 protected void removeLicenses( String projectVersionMetadataKey )
1199 QueryResult<OrderedRows<String, String, String>> result =
1200 HFactory.createRangeSlicesQuery( cassandraArchivaManager.getKeyspace(), ss, ss, ss ) //
1201 .setColumnFamily( cassandraArchivaManager.getLicenseFamilyName() ) //
1202 .setColumnNames( NAME.toString() ) //
1203 .setRowCount( Integer.MAX_VALUE ) //
1204 .addEqualsExpression( "projectVersionMetadataModel.key", projectVersionMetadataKey ) //
1206 for ( Row<String, String, String> row : result.get() )
1208 this.licenseTemplate.deleteRow( row.getKey() );
1212 protected List<License> getLicenses( String projectVersionMetadataKey )
1214 List<License> licenses = new ArrayList<>();
1216 QueryResult<OrderedRows<String, String, String>> result =
1217 HFactory.createRangeSlicesQuery( cassandraArchivaManager.getKeyspace(), ss, ss, ss ) //
1218 .setColumnFamily( cassandraArchivaManager.getLicenseFamilyName() ) //
1219 .setColumnNames( "projectVersionMetadataModel.key" ) //
1220 .setRowCount( Integer.MAX_VALUE ) //
1221 .addEqualsExpression( "projectVersionMetadataModel.key", projectVersionMetadataKey ) //
1224 for ( Row<String, String, String> row : result.get() )
1226 ColumnFamilyResult<String, String> columnFamilyResult = this.licenseTemplate.queryColumns( row.getKey() );
1229 new License( columnFamilyResult.getString( NAME.toString() ), columnFamilyResult.getString( URL.toString() ) ) );
1236 protected void recordDependencies( String projectVersionMetadataKey, List<Dependency> dependencies,
1237 String repositoryId )
1240 if ( dependencies == null || dependencies.isEmpty() )
1244 Mutator<String> dependencyMutator = this.dependencyTemplate.createMutator();
1246 for ( Dependency dependency : dependencies )
1248 // we don't care about the key as the real used one with the projectVersionMetadata
1249 String keyDependency = UUID.randomUUID().toString();
1250 String cfDependency = cassandraArchivaManager.getDependencyFamilyName();
1252 addInsertion( dependencyMutator, keyDependency, cfDependency, "projectVersionMetadataModel.key",
1253 projectVersionMetadataKey );
1255 addInsertion( dependencyMutator, keyDependency, cfDependency, REPOSITORY_NAME.toString(), repositoryId );
1257 addInsertion( dependencyMutator, keyDependency, cfDependency, "classifier", dependency.getClassifier() );
1259 addInsertion( dependencyMutator, keyDependency, cfDependency, "optional",
1260 Boolean.toString( dependency.isOptional() ) );
1262 addInsertion( dependencyMutator, keyDependency, cfDependency, "scope", dependency.getScope() );
1264 addInsertion( dependencyMutator, keyDependency, cfDependency, "systemPath", dependency.getSystemPath() );
1266 addInsertion( dependencyMutator, keyDependency, cfDependency, "type", dependency.getType() );
1268 addInsertion( dependencyMutator, keyDependency, cfDependency, ARTIFACT_ID.toString(), dependency.getArtifactId() );
1270 addInsertion( dependencyMutator, keyDependency, cfDependency, GROUP_ID.toString(), dependency.getGroupId() );
1272 addInsertion( dependencyMutator, keyDependency, cfDependency, VERSION.toString(), dependency.getVersion() );
1275 dependencyMutator.execute();
1278 protected void removeDependencies( String projectVersionMetadataKey )
1281 QueryResult<OrderedRows<String, String, String>> result =
1282 HFactory.createRangeSlicesQuery( cassandraArchivaManager.getKeyspace(), ss, ss, ss ) //
1283 .setColumnFamily( cassandraArchivaManager.getDependencyFamilyName() ) //
1284 .setColumnNames( GROUP_ID.toString() ) //
1285 .setRowCount( Integer.MAX_VALUE ) //
1286 .addEqualsExpression( "projectVersionMetadataModel.key", projectVersionMetadataKey ) //
1288 for ( Row<String, String, String> row : result.get() )
1290 this.dependencyTemplate.deleteRow( row.getKey() );
1294 protected List<Dependency> getDependencies( String projectVersionMetadataKey )
1296 List<Dependency> dependencies = new ArrayList<>();
1298 QueryResult<OrderedRows<String, String, String>> result =
1299 HFactory.createRangeSlicesQuery( cassandraArchivaManager.getKeyspace(), ss, ss, ss ) //
1300 .setColumnFamily( cassandraArchivaManager.getDependencyFamilyName() ) //
1301 .setColumnNames( "projectVersionMetadataModel.key" ) //
1302 .setRowCount( Integer.MAX_VALUE ) //
1303 .addEqualsExpression( "projectVersionMetadataModel.key", projectVersionMetadataKey ) //
1306 for ( Row<String, String, String> row : result.get() )
1308 ColumnFamilyResult<String, String> columnFamilyResult =
1309 this.dependencyTemplate.queryColumns( row.getKey() );
1311 Dependency dependency = new Dependency();
1312 dependency.setClassifier( columnFamilyResult.getString( "classifier" ) );
1314 dependency.setOptional( Boolean.parseBoolean( columnFamilyResult.getString( "optional" ) ) );
1316 dependency.setScope( columnFamilyResult.getString( "scope" ) );
1318 dependency.setSystemPath( columnFamilyResult.getString( "systemPath" ) );
1320 dependency.setType( columnFamilyResult.getString( "type" ) );
1322 dependency.setArtifactId( columnFamilyResult.getString( ARTIFACT_ID.toString() ) );
1324 dependency.setGroupId( columnFamilyResult.getString( GROUP_ID.toString() ) );
1326 dependency.setVersion( columnFamilyResult.getString( VERSION.toString() ) );
1328 dependencies.add( dependency );
1331 return dependencies;
1335 public void updateArtifact( String repositoryId, String namespaceId, String projectId, String projectVersion,
1336 ArtifactMetadata artifactMeta )
1337 throws MetadataRepositoryException
1340 Namespace namespace = getNamespace( repositoryId, namespaceId );
1341 if ( namespace == null )
1343 namespace = updateOrAddNamespace( repositoryId, namespaceId );
1346 ProjectMetadata projectMetadata = new ProjectMetadata();
1347 projectMetadata.setId( projectId );
1348 projectMetadata.setNamespace( namespaceId );
1349 updateProject( repositoryId, projectMetadata );
1351 String key = new ArtifactMetadataModel.KeyBuilder().withNamespace( namespace ).withProject( projectId ).withId(
1352 artifactMeta.getId() ).withProjectVersion( projectVersion ).build();
1356 boolean exists = this.artifactMetadataTemplate.isColumnsExist( key );
1361 ColumnFamilyUpdater<String, String> updater = this.artifactMetadataTemplate.createUpdater( key );
1362 updater.setLong( FILE_LAST_MODIFIED.toString(), artifactMeta.getFileLastModified().getTime() );
1363 updater.setLong( WHEN_GATHERED.toString(), artifactMeta.getWhenGathered().getTime() );
1364 updater.setLong( SIZE.toString(), artifactMeta.getSize() );
1365 addUpdateStringValue( updater, MD5.toString(), artifactMeta.getMd5() );
1366 addUpdateStringValue( updater, SHA1.toString(), artifactMeta.getSha1() );
1367 addUpdateStringValue( updater, VERSION.toString(), artifactMeta.getVersion() );
1368 this.artifactMetadataTemplate.update( updater );
1372 String cf = this.cassandraArchivaManager.getArtifactMetadataFamilyName();
1374 this.artifactMetadataTemplate.createMutator() //
1375 .addInsertion( key, cf, column( ID.toString(), artifactMeta.getId() ) )//
1376 .addInsertion( key, cf, column( REPOSITORY_NAME.toString(), repositoryId ) ) //
1377 .addInsertion( key, cf, column( NAMESPACE_ID.toString(), namespaceId ) ) //
1378 .addInsertion( key, cf, column( PROJECT.toString(), artifactMeta.getProject() ) ) //
1379 .addInsertion( key, cf, column( PROJECT_VERSION.toString(), projectVersion ) ) //
1380 .addInsertion( key, cf, column( VERSION.toString(), artifactMeta.getVersion() ) ) //
1381 .addInsertion( key, cf, column( FILE_LAST_MODIFIED.toString(), artifactMeta.getFileLastModified().getTime() ) ) //
1382 .addInsertion( key, cf, column( SIZE.toString(), artifactMeta.getSize() ) ) //
1383 .addInsertion( key, cf, column( MD5.toString(), artifactMeta.getMd5() ) ) //
1384 .addInsertion( key, cf, column( SHA1.toString(), artifactMeta.getSha1() ) ) //
1385 .addInsertion( key, cf, column( WHEN_GATHERED.toString(), artifactMeta.getWhenGathered().getTime() ) )//
1389 key = new ProjectVersionMetadataModel.KeyBuilder() //
1390 .withRepository( repositoryId ) //
1391 .withNamespace( namespace ) //
1392 .withProjectId( projectId ) //
1393 .withProjectVersion( projectVersion ) //
1394 .withId( artifactMeta.getId() ) //
1397 QueryResult<OrderedRows<String, String, String>> result = HFactory //
1398 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1399 .setColumnFamily( cassandraArchivaManager.getProjectVersionMetadataFamilyName() ) //
1400 .setColumnNames( VERSION.toString() ) //
1401 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
1402 .addEqualsExpression( NAMESPACE_ID.toString(), namespaceId ) //
1403 .addEqualsExpression( PROJECT_ID.toString(), projectId ) //
1404 .addEqualsExpression( PROJECT_VERSION.toString(), projectVersion ) //
1405 .addEqualsExpression( VERSION.toString(), artifactMeta.getVersion() ) //
1408 exists = result.get().getCount() > 0;
1412 String cf = this.cassandraArchivaManager.getProjectVersionMetadataFamilyName();
1414 projectVersionMetadataTemplate.createMutator() //
1415 .addInsertion( key, cf, column( NAMESPACE_ID.toString(), namespace.getName() ) ) //
1416 .addInsertion( key, cf, column( REPOSITORY_NAME.toString(), repositoryId ) ) //
1417 .addInsertion( key, cf, column( PROJECT_VERSION.toString(), projectVersion ) ) //
1418 .addInsertion( key, cf, column( PROJECT_ID.toString(), projectId ) ) //
1419 .addInsertion( key, cf, column( VERSION.toString(), artifactMeta.getVersion() ) ) //
1424 ArtifactMetadataModel artifactMetadataModel = new ArtifactMetadataModel();
1426 artifactMetadataModel.setRepositoryId( repositoryId );
1427 artifactMetadataModel.setNamespace( namespaceId );
1428 artifactMetadataModel.setProject( projectId );
1429 artifactMetadataModel.setProjectVersion( projectVersion );
1430 artifactMetadataModel.setVersion( artifactMeta.getVersion() );
1431 artifactMetadataModel.setFileLastModified( artifactMeta.getFileLastModified() == null
1432 ? new Date().getTime()
1433 : artifactMeta.getFileLastModified().getTime() );
1436 updateFacets( artifactMeta, artifactMetadataModel );
1441 public Collection<String> getArtifactVersions( final String repoId, final String namespace, final String projectId,
1442 final String projectVersion )
1443 throws MetadataResolutionException
1446 QueryResult<OrderedRows<String, String, String>> result = HFactory //
1447 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1448 .setColumnFamily( cassandraArchivaManager.getProjectVersionMetadataFamilyName() ) //
1449 .setColumnNames( VERSION.toString() ) //
1450 .addEqualsExpression( REPOSITORY_NAME.toString(), repoId ) //
1451 .addEqualsExpression( NAMESPACE_ID.toString(), namespace ) //
1452 .addEqualsExpression( PROJECT_ID.toString(), projectId ) //
1453 .addEqualsExpression( PROJECT_VERSION.toString(), projectVersion ) //
1456 final Set<String> versions = new HashSet<String>();
1458 for ( Row<String, String, String> row : result.get() )
1460 versions.add( getStringValue( row.getColumnSlice(), VERSION.toString() ) );
1468 * iterate over available facets to remove/add from the artifactMetadata
1470 * @param facetedMetadata
1471 * @param artifactMetadataModel only use for the key
1473 private void updateFacets( final FacetedMetadata facetedMetadata,
1474 final ArtifactMetadataModel artifactMetadataModel )
1477 String cf = cassandraArchivaManager.getMetadataFacetFamilyName();
1479 for ( final String facetId : metadataFacetFactories.keySet() )
1481 MetadataFacet metadataFacet = facetedMetadata.getFacet( facetId );
1482 if ( metadataFacet == null )
1488 QueryResult<OrderedRows<String, String, String>> result =
1489 HFactory.createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1490 .setColumnFamily( cf ) //
1491 .setColumnNames( REPOSITORY_NAME.toString() ) //
1492 .addEqualsExpression( REPOSITORY_NAME.toString(), artifactMetadataModel.getRepositoryId() ) //
1493 .addEqualsExpression( NAMESPACE_ID.toString(), artifactMetadataModel.getNamespace() ) //
1494 .addEqualsExpression( PROJECT_ID.toString(), artifactMetadataModel.getProject() ) //
1495 .addEqualsExpression( PROJECT_VERSION.toString(), artifactMetadataModel.getProjectVersion() ) //
1496 .addEqualsExpression( FACET_ID.toString(), facetId ) //
1499 for ( Row<String, String, String> row : result.get().getList() )
1501 this.metadataFacetTemplate.deleteRow( row.getKey() );
1504 Map<String, String> properties = metadataFacet.toProperties();
1506 for ( Map.Entry<String, String> entry : properties.entrySet() )
1508 String key = new MetadataFacetModel.KeyBuilder().withKey( entry.getKey() ).withArtifactMetadataModel(
1509 artifactMetadataModel ).withFacetId( facetId ).withName( metadataFacet.getName() ).build();
1510 Mutator<String> mutator = metadataFacetTemplate.createMutator() //
1511 .addInsertion( key, cf, column( REPOSITORY_NAME.toString(), artifactMetadataModel.getRepositoryId() ) ) //
1512 .addInsertion( key, cf, column( NAMESPACE_ID.toString(), artifactMetadataModel.getNamespace() ) ) //
1513 .addInsertion( key, cf, column( PROJECT_ID.toString(), artifactMetadataModel.getProject() ) ) //
1514 .addInsertion( key, cf, column( PROJECT_VERSION.toString(), artifactMetadataModel.getProjectVersion() ) ) //
1515 .addInsertion( key, cf, column( FACET_ID.toString(), facetId ) ) //
1516 .addInsertion( key, cf, column( KEY.toString(), entry.getKey() ) ) //
1517 .addInsertion( key, cf, column( VALUE.toString(), entry.getValue() ) );
1519 if ( metadataFacet.getName() != null )
1521 mutator.addInsertion( key, cf, column( NAME.toString(), metadataFacet.getName() ) );
1531 public List<String> getMetadataFacets( final String repositoryId, final String facetId )
1532 throws MetadataRepositoryException
1535 QueryResult<OrderedRows<String, String, String>> result = HFactory //
1536 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1537 .setColumnFamily( cassandraArchivaManager.getMetadataFacetFamilyName() ) //
1538 .setColumnNames( NAME.toString() ) //
1539 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
1540 .addEqualsExpression( FACET_ID.toString(), facetId ) //
1543 final List<String> facets = new ArrayList<>();
1545 for ( Row<String, String, String> row : result.get() )
1547 facets.add( getStringValue( row.getColumnSlice(), NAME.toString() ) );
1553 public boolean hasMetadataFacet( String repositoryId, String facetId )
1554 throws MetadataRepositoryException
1556 return !getMetadataFacets( repositoryId, facetId ).isEmpty();
1560 public MetadataFacet getMetadataFacet( final String repositoryId, final String facetId, final String name )
1561 throws MetadataRepositoryException
1564 MetadataFacetFactory metadataFacetFactory = metadataFacetFactories.get( facetId );
1565 if ( metadataFacetFactory == null )
1570 QueryResult<OrderedRows<String, String, String>> result = HFactory //
1571 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1572 .setColumnFamily( cassandraArchivaManager.getMetadataFacetFamilyName() ) //
1573 .setColumnNames( KEY.toString(), VALUE.toString() ) //
1574 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
1575 .addEqualsExpression( FACET_ID.toString(), facetId ) //
1576 .addEqualsExpression( NAME.toString(), name ) //
1579 MetadataFacet metadataFacet = metadataFacetFactory.createMetadataFacet( repositoryId, name );
1580 int size = result.get().getCount();
1585 Map<String, String> map = new HashMap<>( size );
1586 for ( Row<String, String, String> row : result.get() )
1588 ColumnSlice<String, String> columnSlice = row.getColumnSlice();
1589 map.put( getStringValue( columnSlice, KEY.toString() ), getStringValue( columnSlice, VALUE.toString() ) );
1591 metadataFacet.fromProperties( map );
1592 return metadataFacet;
1596 public void addMetadataFacet( String repositoryId, MetadataFacet metadataFacet )
1597 throws MetadataRepositoryException
1600 if ( metadataFacet == null )
1605 if ( metadataFacet.toProperties().isEmpty() )
1607 String key = new MetadataFacetModel.KeyBuilder().withRepositoryId( repositoryId ).withFacetId(
1608 metadataFacet.getFacetId() ).withName( metadataFacet.getName() ).build();
1610 boolean exists = this.metadataFacetTemplate.isColumnsExist( key );
1614 ColumnFamilyUpdater<String, String> updater = this.metadataFacetTemplate.createUpdater( key );
1615 addUpdateStringValue( updater, FACET_ID.toString(), metadataFacet.getFacetId() );
1616 addUpdateStringValue( updater, NAME.toString(), metadataFacet.getName() );
1617 this.metadataFacetTemplate.update( updater );
1621 String cf = this.cassandraArchivaManager.getMetadataFacetFamilyName();
1622 this.metadataFacetTemplate.createMutator() //
1623 .addInsertion( key, cf, column( REPOSITORY_NAME.toString(), repositoryId ) ) //
1624 .addInsertion( key, cf, column( FACET_ID.toString(), metadataFacet.getFacetId() ) ) //
1625 .addInsertion( key, cf, column( NAME.toString(), metadataFacet.getName() ) ) //
1632 for ( Map.Entry<String, String> entry : metadataFacet.toProperties().entrySet() )
1634 String key = new MetadataFacetModel.KeyBuilder().withRepositoryId( repositoryId ).withFacetId(
1635 metadataFacet.getFacetId() ).withName( metadataFacet.getName() ).withKey( entry.getKey() ).build();
1637 boolean exists = this.metadataFacetTemplate.isColumnsExist( key );
1640 String cf = this.cassandraArchivaManager.getMetadataFacetFamilyName();
1641 this.metadataFacetTemplate.createMutator() //
1642 .addInsertion( key, cf, column( REPOSITORY_NAME.toString(), repositoryId ) ) //
1643 .addInsertion( key, cf, column( FACET_ID.toString(), metadataFacet.getFacetId() ) ) //
1644 .addInsertion( key, cf, column( NAME.toString(), metadataFacet.getName() ) ) //
1645 .addInsertion( key, cf, column( KEY.toString(), entry.getKey() ) ) //
1646 .addInsertion( key, cf, column( VALUE.toString(), entry.getValue() ) ) //
1651 ColumnFamilyUpdater<String, String> updater = this.metadataFacetTemplate.createUpdater( key );
1652 addUpdateStringValue( updater, VALUE.toString(), entry.getValue() );
1653 this.metadataFacetTemplate.update( updater );
1660 public void removeMetadataFacets( final String repositoryId, final String facetId )
1661 throws MetadataRepositoryException
1664 QueryResult<OrderedRows<String, String, String>> result = HFactory //
1665 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1666 .setColumnFamily( cassandraArchivaManager.getMetadataFacetFamilyName() ) //
1667 .setColumnNames( KEY.toString(), VALUE.toString() ) //
1668 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
1669 .addEqualsExpression( FACET_ID.toString(), facetId ) //
1672 for ( Row<String, String, String> row : result.get() )
1674 this.metadataFacetTemplate.deleteRow( row.getKey() );
1680 public void removeMetadataFacet( final String repositoryId, final String facetId, final String name )
1681 throws MetadataRepositoryException
1684 QueryResult<OrderedRows<String, String, String>> result = HFactory //
1685 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1686 .setColumnFamily( cassandraArchivaManager.getMetadataFacetFamilyName() ) //
1687 .setColumnNames( KEY.toString(), VALUE.toString() ) //
1688 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
1689 .addEqualsExpression( FACET_ID.toString(), facetId ) //
1690 .addEqualsExpression( NAME.toString(), name ) //
1693 for ( Row<String, String, String> row : result.get() )
1695 this.metadataFacetTemplate.deleteRow( row.getKey() );
1700 public List<ArtifactMetadata> getArtifactsByDateRange( final String repositoryId, final Date startTime,
1701 final Date endTime )
1702 throws MetadataRepositoryException
1705 LongSerializer ls = LongSerializer.get();
1706 RangeSlicesQuery<String, String, Long> query = HFactory //
1707 .createRangeSlicesQuery( keyspace, ss, ss, ls ) //
1708 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataFamilyName() ) //
1709 .setColumnNames( ArtifactMetadataModel.COLUMNS ); //
1711 if ( startTime != null )
1713 query = query.addGteExpression( WHEN_GATHERED.toString(), startTime.getTime() );
1715 if ( endTime != null )
1717 query = query.addLteExpression( WHEN_GATHERED.toString(), endTime.getTime() );
1719 QueryResult<OrderedRows<String, String, Long>> result = query.execute();
1721 List<ArtifactMetadata> artifactMetadatas = new ArrayList<>( result.get().getCount() );
1723 for ( Row<String, String, Long> row : result.get() )
1725 ColumnSlice<String, Long> columnSlice = row.getColumnSlice();
1726 String repositoryName = getAsStringValue( columnSlice, REPOSITORY_NAME.toString() );
1727 if ( StringUtils.equals( repositoryName, repositoryId ) )
1730 artifactMetadatas.add( mapArtifactMetadataLongColumnSlice( columnSlice ) );
1734 return artifactMetadatas;
1738 protected ArtifactMetadata mapArtifactMetadataLongColumnSlice( ColumnSlice<String, Long> columnSlice )
1740 ArtifactMetadata artifactMetadata = new ArtifactMetadata();
1741 artifactMetadata.setNamespace( getAsStringValue( columnSlice, NAMESPACE_ID.toString() ) );
1742 artifactMetadata.setSize( getLongValue( columnSlice, SIZE.toString() ) );
1743 artifactMetadata.setId( getAsStringValue( columnSlice, ID.toString() ) );
1744 artifactMetadata.setFileLastModified( getLongValue( columnSlice, FILE_LAST_MODIFIED.toString() ) );
1745 artifactMetadata.setMd5( getAsStringValue( columnSlice, MD5.toString() ) );
1746 artifactMetadata.setProject( getAsStringValue( columnSlice, PROJECT.toString() ) );
1747 artifactMetadata.setProjectVersion( getAsStringValue( columnSlice, PROJECT_VERSION.toString() ) );
1748 artifactMetadata.setRepositoryId( getAsStringValue( columnSlice, REPOSITORY_NAME.toString() ) );
1749 artifactMetadata.setSha1( getAsStringValue( columnSlice, SHA1.toString() ) );
1750 artifactMetadata.setVersion( getAsStringValue( columnSlice, VERSION.toString() ) );
1751 Long whenGathered = getLongValue( columnSlice, WHEN_GATHERED.toString() );
1752 if ( whenGathered != null )
1754 artifactMetadata.setWhenGathered( new Date( whenGathered ) );
1756 return artifactMetadata;
1759 protected ArtifactMetadata mapArtifactMetadataStringColumnSlice( ColumnSlice<String, String> columnSlice )
1761 ArtifactMetadata artifactMetadata = new ArtifactMetadata();
1762 artifactMetadata.setNamespace( getStringValue( columnSlice, NAMESPACE_ID.toString() ) );
1763 artifactMetadata.setSize( getAsLongValue( columnSlice, SIZE.toString() ) );
1764 artifactMetadata.setId( getStringValue( columnSlice, ID.toString() ) );
1765 artifactMetadata.setFileLastModified( getAsLongValue( columnSlice, FILE_LAST_MODIFIED.toString() ) );
1766 artifactMetadata.setMd5( getStringValue( columnSlice, MD5.toString() ) );
1767 artifactMetadata.setProject( getStringValue( columnSlice, PROJECT.toString() ) );
1768 artifactMetadata.setProjectVersion( getStringValue( columnSlice, PROJECT_VERSION.toString() ) );
1769 artifactMetadata.setRepositoryId( getStringValue( columnSlice, REPOSITORY_NAME.toString() ) );
1770 artifactMetadata.setSha1( getStringValue( columnSlice, SHA1.toString() ) );
1771 artifactMetadata.setVersion( getStringValue( columnSlice, VERSION.toString() ) );
1772 Long whenGathered = getAsLongValue( columnSlice, WHEN_GATHERED.toString() );
1773 if ( whenGathered != null )
1775 artifactMetadata.setWhenGathered( new Date( whenGathered ) );
1777 return artifactMetadata;
1781 public Collection<ArtifactMetadata> getArtifactsByChecksum( final String repositoryId, final String checksum )
1782 throws MetadataRepositoryException
1785 // cql cannot run or in queries so running twice the query
1786 Map<String, ArtifactMetadata> artifactMetadataMap = new HashMap<>();
1788 RangeSlicesQuery<String, String, String> query = HFactory //
1789 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1790 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataFamilyName() ) //
1791 .setColumnNames( ArtifactMetadataModel.COLUMNS ); //
1793 query = query.addEqualsExpression( SHA1.toString(), checksum ).addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId );
1795 QueryResult<OrderedRows<String, String, String>> result = query.execute();
1797 for ( Row<String, String, String> row : result.get() )
1799 ColumnSlice<String, String> columnSlice = row.getColumnSlice();
1801 artifactMetadataMap.put( row.getKey(), mapArtifactMetadataStringColumnSlice( columnSlice ) );
1806 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1807 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataFamilyName() ) //
1808 .setColumnNames( NAMESPACE_ID.toString(), SIZE.toString(), ID.toString(), FILE_LAST_MODIFIED.toString(), MD5.toString(), PROJECT.toString(), PROJECT_VERSION.toString(),
1809 REPOSITORY_NAME.toString(), VERSION.toString(), WHEN_GATHERED.toString(), SHA1.toString() ); //
1811 query = query.addEqualsExpression( MD5.toString(), checksum ).addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId );
1813 result = query.execute();
1815 for ( Row<String, String, String> row : result.get() )
1817 ColumnSlice<String, String> columnSlice = row.getColumnSlice();
1819 artifactMetadataMap.put( row.getKey(), mapArtifactMetadataStringColumnSlice( columnSlice ) );
1823 return artifactMetadataMap.values();
1828 public void removeArtifact( final String repositoryId, final String namespace, final String project,
1829 final String version, final String id )
1830 throws MetadataRepositoryException
1832 logger.debug( "removeArtifact repositoryId: '{}', namespace: '{}', project: '{}', version: '{}', id: '{}'",
1833 repositoryId, namespace, project, version, id );
1835 new ArtifactMetadataModel.KeyBuilder().withRepositoryId( repositoryId ).withNamespace( namespace ).withId(
1836 id ).withProjectVersion( version ).withProject( project ).build();
1838 this.artifactMetadataTemplate.deleteRow( key );
1840 key = new ProjectVersionMetadataModel.KeyBuilder() //
1841 .withRepository( repositoryId ) //
1842 .withNamespace( namespace ) //
1843 .withProjectId( project ) //
1844 .withProjectVersion( version ) //
1848 this.projectVersionMetadataTemplate.deleteRow( key );
1852 public void removeArtifact( ArtifactMetadata artifactMetadata, String baseVersion )
1853 throws MetadataRepositoryException
1855 logger.debug( "removeArtifact repositoryId: '{}', namespace: '{}', project: '{}', version: '{}', id: '{}'",
1856 artifactMetadata.getRepositoryId(), artifactMetadata.getNamespace(),
1857 artifactMetadata.getProject(), baseVersion, artifactMetadata.getId() );
1859 new ArtifactMetadataModel.KeyBuilder().withRepositoryId( artifactMetadata.getRepositoryId() ).withNamespace(
1860 artifactMetadata.getNamespace() ).withId( artifactMetadata.getId() ).withProjectVersion(
1861 baseVersion ).withProject( artifactMetadata.getProject() ).build();
1863 this.artifactMetadataTemplate.deleteRow( key );
1868 public void removeArtifact( final String repositoryId, final String namespace, final String project,
1869 final String version, final MetadataFacet metadataFacet )
1870 throws MetadataRepositoryException
1873 RangeSlicesQuery<String, String, String> query = HFactory //
1874 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1875 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataFamilyName() ) //
1876 .setColumnNames( NAMESPACE_ID.toString() ); //
1878 query = query.addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
1879 .addEqualsExpression( NAMESPACE_ID.toString(), namespace ) //
1880 .addEqualsExpression( PROJECT.toString(), project ) //
1881 .addEqualsExpression( VERSION.toString(), version );
1883 QueryResult<OrderedRows<String, String, String>> result = query.execute();
1885 for ( Row<String, String, String> row : result.get() )
1887 this.artifactMetadataTemplate.deleteRow( row.getKey() );
1893 public List<ArtifactMetadata> getArtifacts( final String repositoryId )
1894 throws MetadataRepositoryException
1897 RangeSlicesQuery<String, String, String> query = HFactory //
1898 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1899 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataFamilyName() ) //
1900 .setColumnNames( ArtifactMetadataModel.COLUMNS ); //
1902 query = query.addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId );
1904 QueryResult<OrderedRows<String, String, String>> result = query.execute();
1906 List<ArtifactMetadata> artifactMetadatas = new ArrayList<>( result.get().getCount() );
1908 for ( Row<String, String, String> row : result.get() )
1910 ColumnSlice<String, String> columnSlice = row.getColumnSlice();
1912 artifactMetadatas.add( mapArtifactMetadataStringColumnSlice( columnSlice ) );
1916 return artifactMetadatas;
1921 public Collection<ProjectVersionReference> getProjectReferences( String repoId, String namespace, String projectId,
1922 String projectVersion )
1923 throws MetadataResolutionException
1925 QueryResult<OrderedRows<String, String, String>> result = HFactory //
1926 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1927 .setColumnFamily( cassandraArchivaManager.getDependencyFamilyName() ) //
1928 .setColumnNames( "projectVersionMetadataModel.key" ) //
1929 .addEqualsExpression( REPOSITORY_NAME.toString(), repoId ) //
1930 .addEqualsExpression( GROUP_ID.toString(), namespace ) //
1931 .addEqualsExpression( ARTIFACT_ID.toString(), projectId ) //
1932 .addEqualsExpression( VERSION.toString(), projectVersion ) //
1935 List<String> dependenciesIds = new ArrayList<>( result.get().getCount() );
1937 for ( Row<String, String, String> row : result.get().getList() )
1939 dependenciesIds.add( getStringValue( row.getColumnSlice(), "projectVersionMetadataModel.key" ) );
1942 List<ProjectVersionReference> references = new ArrayList<>( result.get().getCount() );
1944 for ( String key : dependenciesIds )
1946 ColumnFamilyResult<String, String> columnFamilyResult =
1947 this.projectVersionMetadataTemplate.queryColumns( key );
1948 references.add( new ProjectVersionReference( ProjectVersionReference.ReferenceType.DEPENDENCY, //
1949 columnFamilyResult.getString( PROJECT_ID.toString() ), //
1950 columnFamilyResult.getString( NAMESPACE_ID.toString() ), //
1951 columnFamilyResult.getString( PROJECT_VERSION.toString() ) ) );
1958 public void removeProjectVersion( final String repoId, final String namespace, final String projectId,
1959 final String projectVersion )
1960 throws MetadataRepositoryException
1963 QueryResult<OrderedRows<String, String, String>> result = HFactory //
1964 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1965 .setColumnFamily( cassandraArchivaManager.getProjectVersionMetadataFamilyName() ) //
1966 .setColumnNames( VERSION.toString() ) //
1967 .addEqualsExpression( REPOSITORY_NAME.toString(), repoId ) //
1968 .addEqualsExpression( NAMESPACE_ID.toString(), namespace ) //
1969 .addEqualsExpression( PROJECT_ID.toString(), projectId ) //
1970 .addEqualsExpression( PROJECT_VERSION.toString(), projectVersion ) //
1973 for ( Row<String, String, String> row : result.get().getList() )
1975 this.projectVersionMetadataTemplate.deleteRow( row.getKey() );
1976 removeMailingList( row.getKey() );
1977 removeLicenses( row.getKey() );
1978 removeDependencies( row.getKey() );
1981 RangeSlicesQuery<String, String, String> query = HFactory //
1982 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1983 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataFamilyName() ) //
1984 .setColumnNames( NAMESPACE_ID.toString() ); //
1986 query = query.addEqualsExpression( REPOSITORY_NAME.toString(), repoId ) //
1987 .addEqualsExpression( NAMESPACE_ID.toString(), namespace ) //
1988 .addEqualsExpression( PROJECT.toString(), projectId ) //
1989 .addEqualsExpression( PROJECT_VERSION.toString(), projectVersion );
1991 result = query.execute();
1993 for ( Row<String, String, String> row : result.get() )
1995 this.artifactMetadataTemplate.deleteRow( row.getKey() );
2001 public Collection<ArtifactMetadata> getArtifacts( final String repoId, final String namespace,
2002 final String projectId, final String projectVersion )
2003 throws MetadataResolutionException
2006 QueryResult<OrderedRows<String, String, String>> result =
2007 HFactory.createRangeSlicesQuery( keyspace, ss, ss, ss ) //
2008 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataFamilyName() ) //
2009 .setColumnNames( ArtifactMetadataModel.COLUMNS )//
2010 .setRowCount( Integer.MAX_VALUE ) //
2011 .addEqualsExpression( REPOSITORY_NAME.toString(), repoId ) //
2012 .addEqualsExpression( NAMESPACE_ID.toString(), namespace ) //
2013 .addEqualsExpression( PROJECT.toString(), projectId ) //
2014 .addEqualsExpression( PROJECT_VERSION.toString(), projectVersion ) //
2017 if ( result.get() == null || result.get().getCount() < 1 )
2019 return Collections.emptyList();
2022 List<ArtifactMetadata> artifactMetadatas = new ArrayList<>( result.get().getCount() );
2024 for ( Row<String, String, String> row : result.get() )
2026 ColumnSlice<String, String> columnSlice = row.getColumnSlice();
2027 ArtifactMetadata artifactMetadata = new ArtifactMetadata();
2028 artifactMetadata.setNamespace( getStringValue( columnSlice, NAMESPACE_ID.toString() ) );
2029 artifactMetadata.setSize( getAsLongValue( columnSlice, SIZE.toString() ) );
2030 artifactMetadata.setId( getStringValue( columnSlice, ID.toString() ) );
2031 artifactMetadata.setFileLastModified( getAsLongValue( columnSlice, FILE_LAST_MODIFIED.toString() ) );
2032 artifactMetadata.setMd5( getStringValue( columnSlice, MD5.toString() ) );
2033 artifactMetadata.setProject( getStringValue( columnSlice, PROJECT.toString() ) );
2034 artifactMetadata.setProjectVersion( getStringValue( columnSlice, PROJECT_VERSION.toString() ) );
2035 artifactMetadata.setRepositoryId( repoId );
2036 artifactMetadata.setSha1( getStringValue( columnSlice, SHA1.toString() ) );
2037 artifactMetadata.setVersion( getStringValue( columnSlice, VERSION.toString() ) );
2038 Long whenGathered = getAsLongValue( columnSlice, WHEN_GATHERED.toString() );
2039 if ( whenGathered != null )
2041 artifactMetadata.setWhenGathered( new Date( whenGathered ) );
2043 artifactMetadatas.add( artifactMetadata );
2046 result = HFactory.createRangeSlicesQuery( keyspace, ss, ss, ss ) //
2047 .setColumnFamily( cassandraArchivaManager.getMetadataFacetFamilyName() ) //
2048 .setColumnNames( FACET_ID.toString(), NAME.toString(), VALUE.toString(), KEY.toString(), PROJECT_VERSION.toString() ) //
2049 .setRowCount( Integer.MAX_VALUE ) //
2050 .addEqualsExpression( REPOSITORY_NAME.toString(), repoId ) //
2051 .addEqualsExpression( NAMESPACE_ID.toString(), namespace ) //
2052 .addEqualsExpression( PROJECT_ID.toString(), projectId ) //
2053 .addEqualsExpression( PROJECT_VERSION.toString(), projectVersion ) //
2056 if ( result.get() == null || result.get().getCount() < 1 )
2058 return artifactMetadatas;
2061 final List<MetadataFacetModel> metadataFacetModels = new ArrayList<>( result.get().getCount() );
2063 for ( Row<String, String, String> row : result.get() )
2065 ColumnSlice<String, String> columnSlice = row.getColumnSlice();
2066 MetadataFacetModel metadataFacetModel = new MetadataFacetModel();
2067 metadataFacetModel.setFacetId( getStringValue( columnSlice, FACET_ID.toString() ) );
2068 metadataFacetModel.setName( getStringValue( columnSlice, NAME.toString() ) );
2069 metadataFacetModel.setValue( getStringValue( columnSlice, VALUE.toString() ) );
2070 metadataFacetModel.setKey( getStringValue( columnSlice, KEY.toString() ) );
2071 metadataFacetModel.setProjectVersion( getStringValue( columnSlice, PROJECT_VERSION.toString() ) );
2072 metadataFacetModels.add( metadataFacetModel );
2075 // rebuild MetadataFacet for artifacts
2077 for ( final ArtifactMetadata artifactMetadata : artifactMetadatas )
2079 Iterable<MetadataFacetModel> metadataFacetModelIterable =
2080 Iterables.filter( metadataFacetModels, new Predicate<MetadataFacetModel>()
2083 public boolean apply( MetadataFacetModel metadataFacetModel )
2085 if ( metadataFacetModel != null )
2087 return StringUtils.equals( artifactMetadata.getVersion(),
2088 metadataFacetModel.getProjectVersion() );
2093 Iterator<MetadataFacetModel> iterator = metadataFacetModelIterable.iterator();
2094 Map<String, List<MetadataFacetModel>> metadataFacetValuesPerFacetId = new HashMap<>();
2095 while ( iterator.hasNext() )
2097 MetadataFacetModel metadataFacetModel = iterator.next();
2098 List<MetadataFacetModel> values = metadataFacetValuesPerFacetId.get( metadataFacetModel.getName() );
2099 if ( values == null )
2101 values = new ArrayList<>();
2102 metadataFacetValuesPerFacetId.put( metadataFacetModel.getFacetId(), values );
2104 values.add( metadataFacetModel );
2108 for ( Map.Entry<String, List<MetadataFacetModel>> entry : metadataFacetValuesPerFacetId.entrySet() )
2110 MetadataFacetFactory metadataFacetFactory = metadataFacetFactories.get( entry.getKey() );
2111 if ( metadataFacetFactory != null )
2113 List<MetadataFacetModel> facetModels = entry.getValue();
2114 if ( !facetModels.isEmpty() )
2116 MetadataFacet metadataFacet = metadataFacetFactory.createMetadataFacet();
2117 Map<String, String> props = new HashMap<>( facetModels.size() );
2118 for ( MetadataFacetModel metadataFacetModel : facetModels )
2120 props.put( metadataFacetModel.getKey(), metadataFacetModel.getValue() );
2122 metadataFacet.fromProperties( props );
2123 artifactMetadata.addFacet( metadataFacet );
2131 return artifactMetadatas;
2137 logger.trace( "save" );
2142 throws MetadataRepositoryException
2144 logger.trace( "close" );
2148 public void revert()
2150 logger.warn( "CassandraMetadataRepository cannot revert" );
2154 public boolean canObtainAccess( Class<?> aClass )
2160 public <T> T obtainAccess( Class<T> aClass )
2161 throws MetadataRepositoryException
2163 throw new IllegalArgumentException(
2164 "Access using " + aClass + " is not supported on the cassandra metadata storage" );
2168 private static class ModelMapperHolder
2170 private static ModelMapper MODEL_MAPPER = new ModelMapper();
2173 protected ModelMapper getModelMapper()
2175 return ModelMapperHolder.MODEL_MAPPER;