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.*;
85 * @author Olivier Lamy
88 public class CassandraMetadataRepository
89 implements MetadataRepository
92 private Logger logger = LoggerFactory.getLogger( getClass() );
94 private ArchivaConfiguration configuration;
96 private final Map<String, MetadataFacetFactory> metadataFacetFactories;
98 private final CassandraArchivaManager cassandraArchivaManager;
100 private final ColumnFamilyTemplate<String, String> projectVersionMetadataModelTemplate;
102 private final ColumnFamilyTemplate<String, String> projectTemplate;
104 private final ColumnFamilyTemplate<String, String> artifactMetadataTemplate;
106 private final ColumnFamilyTemplate<String, String> metadataFacetTemplate;
108 private final ColumnFamilyTemplate<String, String> mailingListTemplate;
110 private final ColumnFamilyTemplate<String, String> licenseTemplate;
112 private final ColumnFamilyTemplate<String, String> dependencyTemplate;
114 public CassandraMetadataRepository( Map<String, MetadataFacetFactory> metadataFacetFactories,
115 ArchivaConfiguration configuration,
116 CassandraArchivaManager cassandraArchivaManager )
118 this.metadataFacetFactories = metadataFacetFactories;
119 this.configuration = configuration;
120 this.cassandraArchivaManager = cassandraArchivaManager;
122 this.projectVersionMetadataModelTemplate =
123 new ThriftColumnFamilyTemplate<String, String>( cassandraArchivaManager.getKeyspace(), //
124 cassandraArchivaManager.getProjectVersionMetadataFamilyName(),
125 StringSerializer.get(), //
126 StringSerializer.get() );
128 this.projectTemplate = new ThriftColumnFamilyTemplate<String, String>( cassandraArchivaManager.getKeyspace(), //
129 cassandraArchivaManager.getProjectFamilyName(),
131 StringSerializer.get(), //
132 StringSerializer.get() );
134 this.artifactMetadataTemplate =
135 new ThriftColumnFamilyTemplate<String, String>( cassandraArchivaManager.getKeyspace(), //
136 cassandraArchivaManager.getArtifactMetadataFamilyName(),
138 StringSerializer.get(), //
139 StringSerializer.get() );
141 this.metadataFacetTemplate =
142 new ThriftColumnFamilyTemplate<String, String>( cassandraArchivaManager.getKeyspace(), //
143 cassandraArchivaManager.getMetadataFacetFamilyName(),
145 StringSerializer.get(), //
146 StringSerializer.get() );
148 this.mailingListTemplate =
149 new ThriftColumnFamilyTemplate<String, String>( cassandraArchivaManager.getKeyspace(), //
150 cassandraArchivaManager.getMailingListFamilyName(),
152 StringSerializer.get(), //
153 StringSerializer.get() );
155 this.licenseTemplate = new ThriftColumnFamilyTemplate<String, String>( cassandraArchivaManager.getKeyspace(), //
156 cassandraArchivaManager.getLicenseFamilyName(),
158 StringSerializer.get(), //
159 StringSerializer.get() );
161 this.dependencyTemplate =
162 new ThriftColumnFamilyTemplate<String, String>( cassandraArchivaManager.getKeyspace(), //
163 cassandraArchivaManager.getDependencyFamilyName(),
165 StringSerializer.get(), //
166 StringSerializer.get() );
171 * if the repository doesn't exist it will be created
173 * @param repositoryId
176 public Repository getOrCreateRepository( String repositoryId )
177 throws MetadataRepositoryException
179 String cf = cassandraArchivaManager.getRepositoryFamilyName();
180 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
181 QueryResult<OrderedRows<String, String, String>> result = HFactory //
182 .createRangeSlicesQuery( keyspace, StringSerializer.get(), StringSerializer.get(),
183 StringSerializer.get() ) //
184 .setColumnFamily( cf ) //
185 .setColumnNames( "repositoryName" ) //
186 .addEqualsExpression( "repositoryName", repositoryId ) //
189 if ( result.get().getCount() < 1 )
191 // we need to create the repository
192 Repository repository = new Repository( repositoryId );
196 MutationResult mutationResult = HFactory.createMutator( keyspace, StringSerializer.get() ) //
197 .addInsertion( repositoryId, cf,
198 CassandraUtils.column( "repositoryName", repository.getName() ) ) //
202 catch ( HInvalidRequestException e )
204 logger.error( e.getMessage(), e );
205 throw new MetadataRepositoryException( e.getMessage(), e );
210 return new Repository(
211 result.get().getList().get( 0 ).getColumnSlice().getColumnByName( "repositoryName" ).getValue() );
215 protected Repository getRepository( String repositoryId )
216 throws MetadataRepositoryException
218 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
219 QueryResult<OrderedRows<String, String, String>> result = HFactory //
220 .createRangeSlicesQuery( keyspace, StringSerializer.get(), StringSerializer.get(),
221 StringSerializer.get() ) //
222 .setColumnFamily( cassandraArchivaManager.getRepositoryFamilyName() ) //
223 .setColumnNames( "repositoryName" ) //
224 .addEqualsExpression( "repositoryName", repositoryId ) //
226 return ( result.get().getCount() > 0 ) ? new Repository( repositoryId ) : null;
230 public void updateNamespace( String repositoryId, String namespaceId )
231 throws MetadataRepositoryException
233 updateOrAddNamespace( repositoryId, namespaceId );
236 public Namespace updateOrAddNamespace( String repositoryId, String namespaceId )
237 throws MetadataRepositoryException
241 Repository repository = getOrCreateRepository( repositoryId );
243 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
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", namespace.getName() ) ) //
256 .addInsertion( key, cf, CassandraUtils.column( "repositoryName", 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 )
271 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
272 QueryResult<OrderedRows<String, String, String>> result = HFactory //
273 .createRangeSlicesQuery( keyspace, //
274 StringSerializer.get(), //
275 StringSerializer.get(), //
276 StringSerializer.get() ) //
277 .setColumnFamily( cassandraArchivaManager.getNamespaceFamilyName() ) //
278 .setColumnNames( "repositoryName", "name" ) //
279 .addEqualsExpression( "repositoryName", repositoryId ) //
280 .addEqualsExpression( "name", namespaceId ) //
282 if ( result.get().getCount() > 0 )
284 ColumnSlice<String, String> columnSlice = result.get().getList().get( 0 ).getColumnSlice();
285 return new Namespace( getStringValue( columnSlice, "name" ), //
286 new Repository( getStringValue( columnSlice, "repositoryName" ) ) );
294 public void removeNamespace( String repositoryId, String namespaceId )
295 throws MetadataRepositoryException
297 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
301 new Namespace.KeyBuilder().withNamespace( namespaceId ).withRepositoryId( repositoryId ).build();
303 HFactory.createMutator( cassandraArchivaManager.getKeyspace(), new StringSerializer() ) //
304 .addDeletion( key, cassandraArchivaManager.getNamespaceFamilyName() ) //
307 QueryResult<OrderedRows<String, String, String>> result = HFactory //
308 .createRangeSlicesQuery( keyspace, //
309 StringSerializer.get(), //
310 StringSerializer.get(), //
311 StringSerializer.get() ) //
312 .setColumnFamily( cassandraArchivaManager.getProjectFamilyName() ) //
313 .setColumnNames( "repositoryName" ) //
314 .addEqualsExpression( "repositoryName", repositoryId ) //
315 .addEqualsExpression( "namespaceId", namespaceId ) //
318 for ( Row<String, String, String> row : result.get() )
320 this.projectTemplate.deleteRow( row.getKey() );
324 .createRangeSlicesQuery( keyspace, //
325 StringSerializer.get(), //
326 StringSerializer.get(), //
327 StringSerializer.get() ) //
328 .setColumnFamily( cassandraArchivaManager.getProjectVersionMetadataFamilyName() ) //
329 .setColumnNames( "repositoryName" ) //
330 .addEqualsExpression( "repositoryName", repositoryId ) //
331 .addEqualsExpression( "namespaceId", namespaceId ) //
334 for ( Row<String, String, String> row : result.get() )
336 this.projectVersionMetadataModelTemplate.deleteRow( row.getKey() );
337 removeMailingList( row.getKey() );
341 .createRangeSlicesQuery( keyspace, //
342 StringSerializer.get(), //
343 StringSerializer.get(), //
344 StringSerializer.get() ) //
345 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataFamilyName() ) //
346 .setColumnNames( "repositoryName" ) //
347 .addEqualsExpression( "repositoryName", repositoryId ) //
348 .addEqualsExpression( "namespaceId", namespaceId ) //
351 for ( Row<String, String, String> row : result.get() )
353 this.artifactMetadataTemplate.deleteRow( row.getKey() );
357 .createRangeSlicesQuery( keyspace, //
358 StringSerializer.get(), //
359 StringSerializer.get(), //
360 StringSerializer.get() ) //
361 .setColumnFamily( cassandraArchivaManager.getMetadataFacetFamilyName() ) //
362 .setColumnNames( "repositoryName" ) //
363 .addEqualsExpression( "repositoryName", repositoryId ) //
364 .addEqualsExpression( "namespaceId", namespaceId ) //
367 for ( Row<String, String, String> row : result.get() )
369 this.metadataFacetTemplate.deleteRow( row.getKey() );
373 catch ( HInvalidRequestException e )
375 logger.error( e.getMessage(), e );
376 throw new MetadataRepositoryException( e.getMessage(), e );
382 public void removeRepository( final String repositoryId )
383 throws MetadataRepositoryException
386 StringSerializer ss = StringSerializer.get();
388 // TODO use cql queries to delete all
389 List<String> namespacesKey = new ArrayList<String>();
391 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
392 QueryResult<OrderedRows<String, String, String>> result = HFactory //
393 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
394 .setColumnFamily( cassandraArchivaManager.getNamespaceFamilyName() ) //
395 .setColumnNames( "repositoryName" ) //
396 .addEqualsExpression( "repositoryName", repositoryId ) //
399 for ( Row<String, String, String> row : result.get().getList() )
401 namespacesKey.add( row.getKey() );
404 HFactory.createMutator( cassandraArchivaManager.getKeyspace(), ss ) //
405 .addDeletion( namespacesKey, cassandraArchivaManager.getNamespaceFamilyName() ) //
408 //delete repositoryId
409 HFactory.createMutator( cassandraArchivaManager.getKeyspace(), ss ) //
410 .addDeletion( repositoryId, cassandraArchivaManager.getRepositoryFamilyName() ) //
414 .createRangeSlicesQuery( keyspace, //
415 StringSerializer.get(), //
416 StringSerializer.get(), //
417 StringSerializer.get() ) //
418 .setColumnFamily( cassandraArchivaManager.getProjectFamilyName() ) //
419 .setColumnNames( "repositoryName" ) //
420 .addEqualsExpression( "repositoryName", repositoryId ) //
423 for ( Row<String, String, String> row : result.get() )
425 this.projectTemplate.deleteRow( row.getKey() );
429 .createRangeSlicesQuery( keyspace, //
430 StringSerializer.get(), //
431 StringSerializer.get(), //
432 StringSerializer.get() ) //
433 .setColumnFamily( cassandraArchivaManager.getProjectVersionMetadataFamilyName() ) //
434 .setColumnNames( "repositoryName" ) //
435 .addEqualsExpression( "repositoryName", repositoryId ) //
438 for ( Row<String, String, String> row : result.get() )
440 this.projectVersionMetadataModelTemplate.deleteRow( row.getKey() );
441 removeMailingList( row.getKey() );
445 .createRangeSlicesQuery( keyspace, //
446 StringSerializer.get(), //
447 StringSerializer.get(), //
448 StringSerializer.get() ) //
449 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataFamilyName() ) //
450 .setColumnNames( "repositoryName" ) //
451 .addEqualsExpression( "repositoryName", repositoryId ) //
454 for ( Row<String, String, String> row : result.get() )
456 this.artifactMetadataTemplate.deleteRow( row.getKey() );
460 .createRangeSlicesQuery( keyspace, //
461 StringSerializer.get(), //
462 StringSerializer.get(), //
463 StringSerializer.get() ) //
464 .setColumnFamily( cassandraArchivaManager.getMetadataFacetFamilyName() ) //
465 .setColumnNames( "repositoryName" ) //
466 .addEqualsExpression( "repositoryName", repositoryId ) //
469 for ( Row<String, String, String> row : result.get() )
471 this.metadataFacetTemplate.deleteRow( row.getKey() );
478 public Collection<String> getRepositories()
479 throws MetadataRepositoryException
483 logger.debug( "getRepositories" );
485 final QueryResult<OrderedRows<String, String, String>> cResult = //
486 HFactory.createRangeSlicesQuery( cassandraArchivaManager.getKeyspace(), //
487 StringSerializer.get(), //
488 StringSerializer.get(), //
489 StringSerializer.get() ) //
490 .setColumnFamily( cassandraArchivaManager.getRepositoryFamilyName() ) //
491 .setColumnNames( "repositoryName" ) //
492 .setRange( null, null, false, Integer.MAX_VALUE ) //
495 List<String> repoIds = new ArrayList<String>( cResult.get().getCount() );
497 for ( Row<String, String, String> row : cResult.get() )
499 repoIds.add( getStringValue( row.getColumnSlice(), "repositoryName" ) );
504 catch ( PersistenceException e )
506 throw new MetadataRepositoryException( e.getMessage(), e );
513 public Collection<String> getRootNamespaces( final String repoId )
514 throws MetadataResolutionException
516 StringSerializer ss = StringSerializer.get();
517 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
518 QueryResult<OrderedRows<String, String, String>> result = HFactory //
519 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
520 .setColumnFamily( cassandraArchivaManager.getNamespaceFamilyName() ) //
521 .setColumnNames( "name" ) //
522 .addEqualsExpression( "repositoryName", repoId ) //
525 Set<String> namespaces = new HashSet<String>( result.get().getCount() );
527 for ( Row<String, String, String> row : result.get() )
529 namespaces.add( StringUtils.substringBefore( getStringValue( row.getColumnSlice(), "name" ), "." ) );
537 public Collection<String> getNamespaces( final String repoId, final String namespaceId )
538 throws MetadataResolutionException
540 StringSerializer ss = StringSerializer.get();
541 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
542 QueryResult<OrderedRows<String, String, String>> result = HFactory //
543 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
544 .setColumnFamily( cassandraArchivaManager.getNamespaceFamilyName() ) //
545 .setColumnNames( "name" ) //
546 .addEqualsExpression( "repositoryName", repoId ) //
549 List<String> namespaces = new ArrayList<String>( result.get().getCount() );
551 for ( Row<String, String, String> row : result.get() )
553 String currentNamespace = getStringValue( row.getColumnSlice(), "name" );
554 if ( StringUtils.startsWith( currentNamespace, namespaceId ) //
555 && ( StringUtils.length( currentNamespace ) > StringUtils.length( namespaceId ) ) )
557 // store after namespaceId '.' but before next '.'
558 // call org namespace org.apache.maven.shared -> stored apache
560 String calledNamespace = StringUtils.endsWith( namespaceId, "." ) ? namespaceId : namespaceId + ".";
561 String storedNamespace = StringUtils.substringAfter( currentNamespace, calledNamespace );
563 storedNamespace = StringUtils.substringBefore( storedNamespace, "." );
565 namespaces.add( storedNamespace );
574 public List<String> getNamespaces( final String repoId )
575 throws MetadataResolutionException
577 StringSerializer ss = StringSerializer.get();
578 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
579 QueryResult<OrderedRows<String, String, String>> result = HFactory //
580 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
581 .setColumnFamily( cassandraArchivaManager.getNamespaceFamilyName() ) //
582 .setColumnNames( "name" ) //
583 .addEqualsExpression( "repositoryName", repoId ) //
586 List<String> namespaces = new ArrayList<String>( result.get().getCount() );
588 for ( Row<String, String, String> row : result.get() )
590 namespaces.add( getStringValue( row.getColumnSlice(), "name" ) );
598 public void updateProject( String repositoryId, ProjectMetadata projectMetadata )
599 throws MetadataRepositoryException
601 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
602 StringSerializer ss = StringSerializer.get();
604 QueryResult<OrderedRows<String, String, String>> result = HFactory //
605 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
606 .setColumnFamily( cassandraArchivaManager.getProjectFamilyName() ) //
607 .setColumnNames( "projectId" ) //
608 .addEqualsExpression( "repositoryName", repositoryId ) //
609 .addEqualsExpression( "namespaceId", projectMetadata.getNamespace() ) //
610 .addEqualsExpression( "projectId", projectMetadata.getId() ) //
613 // project exists ? if yes return nothing to update here
614 if ( result.get().getCount() > 0 )
620 Namespace namespace = updateOrAddNamespace( repositoryId, projectMetadata.getNamespace() );
623 new Project.KeyBuilder().withProjectId( projectMetadata.getId() ).withNamespace( namespace ).build();
625 String cf = cassandraArchivaManager.getProjectFamilyName();
626 projectTemplate.createMutator()
628 .addInsertion( key, cf, CassandraUtils.column( "projectId", projectMetadata.getId() ) ) //
629 .addInsertion( key, cf, CassandraUtils.column( "repositoryName", repositoryId ) ) //
630 .addInsertion( key, cf, CassandraUtils.column( "namespaceId", projectMetadata.getNamespace() ) )//
636 public Collection<String> getProjects( final String repoId, final String namespace )
637 throws MetadataResolutionException
640 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
641 StringSerializer ss = StringSerializer.get();
642 QueryResult<OrderedRows<String, String, String>> result = HFactory //
643 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
644 .setColumnFamily( cassandraArchivaManager.getProjectFamilyName() ) //
645 .setColumnNames( "projectId" ) //
646 .addEqualsExpression( "repositoryName", repoId ) //
647 .addEqualsExpression( "namespaceId", namespace ) //
650 final Set<String> projects = new HashSet<String>( result.get().getCount() );
652 for ( Row<String, String, String> row : result.get() )
654 projects.add( getStringValue( row.getColumnSlice(), "projectId" ) );
661 public void removeProject( final String repositoryId, final String namespaceId, final String projectId )
662 throws MetadataRepositoryException
664 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
665 StringSerializer ss = StringSerializer.get();
666 String key = new Project.KeyBuilder() //
667 .withProjectId( projectId ) //
668 .withNamespace( new Namespace( namespaceId, new Repository( repositoryId ) ) ) //
671 this.projectTemplate.deleteRow( key );
673 QueryResult<OrderedRows<String, String, String>> result = HFactory //
674 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
675 .setColumnFamily( cassandraArchivaManager.getProjectVersionMetadataFamilyName() ) //
676 .setColumnNames( "id" ) //
677 .addEqualsExpression( "repositoryName", repositoryId ) //
678 .addEqualsExpression( "namespaceId", namespaceId ) //
679 .addEqualsExpression( "projectId", projectId ) //
682 for ( Row<String, String, String> row : result.get() )
684 this.projectVersionMetadataModelTemplate.deleteRow( row.getKey() );
685 removeMailingList( row.getKey() );
689 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
690 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataFamilyName() ) //
691 .setColumnNames( "projectId" ) //
692 .addEqualsExpression( "repositoryName", repositoryId ) //
693 .addEqualsExpression( "namespaceId", namespaceId ) //
694 .addEqualsExpression( "projectId", projectId ) //
697 for ( Row<String, String, String> row : result.get() )
699 this.artifactMetadataTemplate.deleteRow( row.getKey() );
704 public Collection<String> getProjectVersions( final String repoId, final String namespace, final String projectId )
705 throws MetadataResolutionException
708 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
709 StringSerializer ss = StringSerializer.get();
710 QueryResult<OrderedRows<String, String, String>> result = HFactory //
711 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
712 .setColumnFamily( cassandraArchivaManager.getProjectVersionMetadataFamilyName() ) //
713 .setColumnNames( "projectVersion" ) //
714 .addEqualsExpression( "repositoryName", repoId ) //
715 .addEqualsExpression( "namespaceId", namespace ) //
716 .addEqualsExpression( "projectId", projectId ) //
719 int count = result.get().getCount();
723 return Collections.emptyList();
726 Set<String> versions = new HashSet<String>( count );
728 for ( Row<String, String, String> orderedRows : result.get() )
730 versions.add( getStringValue( orderedRows.getColumnSlice(), "projectVersion" ) );
738 public ProjectMetadata getProject( final String repoId, final String namespace, final String id )
739 throws MetadataResolutionException
742 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
743 StringSerializer ss = StringSerializer.get();
744 QueryResult<OrderedRows<String, String, String>> result = HFactory //
745 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
746 .setColumnFamily( cassandraArchivaManager.getProjectFamilyName() ) //
747 .setColumnNames( "projectId" ) //
748 .addEqualsExpression( "repositoryName", repoId ) //
749 .addEqualsExpression( "namespaceId", namespace ) //
750 .addEqualsExpression( "projectId", id ) //
753 int count = result.get().getCount();
760 ProjectMetadata projectMetadata = new ProjectMetadata();
761 projectMetadata.setId( id );
762 projectMetadata.setNamespace( namespace );
764 logger.debug( "getProject repoId: {}, namespace: {}, projectId: {} -> {}", repoId, namespace, id,
767 return projectMetadata;
770 protected ProjectVersionMetadataModel mapProjectVersionMetadataModel( ColumnSlice<String, String> columnSlice )
772 ProjectVersionMetadataModel projectVersionMetadataModel = new ProjectVersionMetadataModel();
773 projectVersionMetadataModel.setId( getStringValue( columnSlice, "id" ) );
774 projectVersionMetadataModel.setDescription( getStringValue( columnSlice, "description" ) );
775 projectVersionMetadataModel.setName( getStringValue( columnSlice, "name" ) );
776 projectVersionMetadataModel.setNamespace( new Namespace( getStringValue( columnSlice, "namespaceId" ), //
778 getStringValue( columnSlice, "repositoryName" ) )
781 projectVersionMetadataModel.setIncomplete(
782 Boolean.parseBoolean( getStringValue( columnSlice, "incomplete" ) ) );
783 projectVersionMetadataModel.setProjectId( getStringValue( columnSlice, "projectId" ) );
784 projectVersionMetadataModel.setUrl( getStringValue( columnSlice, "url" ) );
785 return projectVersionMetadataModel;
790 public void updateProjectVersion( String repositoryId, String namespaceId, String projectId,
791 ProjectVersionMetadata versionMetadata )
792 throws MetadataRepositoryException
796 Namespace namespace = getNamespace( repositoryId, namespaceId );
798 if ( namespace == null )
800 updateOrAddNamespace( repositoryId, namespaceId );
803 if ( getProject( repositoryId, namespaceId, projectId ) == null )
805 ProjectMetadata projectMetadata = new ProjectMetadata();
806 projectMetadata.setNamespace( namespaceId );
807 projectMetadata.setId( projectId );
808 updateProject( repositoryId, projectMetadata );
812 catch ( MetadataResolutionException e )
814 throw new MetadataRepositoryException( e.getMessage(), e );
817 StringSerializer ss = StringSerializer.get();
819 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
820 QueryResult<OrderedRows<String, String, String>> result = HFactory //
821 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
822 .setColumnFamily( cassandraArchivaManager.getProjectVersionMetadataFamilyName() ) //
823 .setColumnNames( "projectVersion" ) //
824 .addEqualsExpression( "repositoryName", repositoryId ) //
825 .addEqualsExpression( "namespaceId", namespaceId ) //
826 .addEqualsExpression( "projectId", projectId ) //
827 .addEqualsExpression( "projectVersion", versionMetadata.getId() ) //
830 ProjectVersionMetadataModel projectVersionMetadataModel = null;
831 boolean creation = true;
832 if ( result.get().getCount() > 0 )
834 projectVersionMetadataModel =
835 mapProjectVersionMetadataModel( result.get().getList().get( 0 ).getColumnSlice() );
840 projectVersionMetadataModel = getModelMapper().map( versionMetadata, ProjectVersionMetadataModel.class );
843 projectVersionMetadataModel.setProjectId( projectId );
844 projectVersionMetadataModel.setNamespace( new Namespace( namespaceId, new Repository( repositoryId ) ) );
846 projectVersionMetadataModel.setCiManagement( versionMetadata.getCiManagement() );
847 projectVersionMetadataModel.setIssueManagement( versionMetadata.getIssueManagement() );
848 projectVersionMetadataModel.setOrganization( versionMetadata.getOrganization() );
849 projectVersionMetadataModel.setScm( versionMetadata.getScm() );
851 projectVersionMetadataModel.setMailingLists( versionMetadata.getMailingLists() );
852 projectVersionMetadataModel.setDependencies( versionMetadata.getDependencies() );
853 projectVersionMetadataModel.setLicenses( versionMetadata.getLicenses() );
855 // we don't test of repository and namespace really exist !
856 String key = new ProjectVersionMetadataModel.KeyBuilder().withRepository( repositoryId ).withNamespace(
857 namespaceId ).withProjectId( projectId ).withId( versionMetadata.getId() ).build();
859 // FIXME nested objects to store!!!
862 String cf = cassandraArchivaManager.getProjectVersionMetadataFamilyName();
863 Mutator<String> mutator = projectVersionMetadataModelTemplate.createMutator()
865 .addInsertion( key, cf, column( "projectId", projectId ) ) //
866 .addInsertion( key, cf, column( "repositoryName", repositoryId ) ) //
867 .addInsertion( key, cf, column( "namespaceId", namespaceId ) )//
868 .addInsertion( key, cf, column( "projectVersion", versionMetadata.getVersion() ) ); //
870 addInsertion( mutator, key, cf, "description", versionMetadata.getDescription() );
872 addInsertion( mutator, key, cf, "name", versionMetadata.getName() );
874 addInsertion( mutator, key, cf, "incomplete", Boolean.toString( versionMetadata.isIncomplete() ) );
876 addInsertion( mutator, key, cf, "url", versionMetadata.getUrl() );
878 CiManagement ci = versionMetadata.getCiManagement();
881 addInsertion( mutator, key, cf, "ciManagement.system", ci.getSystem() );
882 addInsertion( mutator, key, cf, "ciManagement.url", ci.getUrl() );
887 IssueManagement issueManagement = versionMetadata.getIssueManagement();
889 if ( issueManagement != null )
891 addInsertion( mutator, key, cf, "issueManagement.system", issueManagement.getSystem() );
892 addInsertion( mutator, key, cf, "issueManagement.url", issueManagement.getUrl() );
897 Organization organization = versionMetadata.getOrganization();
898 if ( organization != null )
900 addInsertion( mutator, key, cf, "organization.name", organization.getName() );
901 addInsertion( mutator, key, cf, "organization.url", organization.getUrl() );
906 Scm scm = versionMetadata.getScm();
909 addInsertion( mutator, key, cf, "scm.url", scm.getUrl() );
910 addInsertion( mutator, key, cf, "scm.connection", scm.getConnection() );
911 addInsertion( mutator, key, cf, "scm.developerConnection", scm.getDeveloperConnection() );
915 recordMailingList( key, versionMetadata.getMailingLists() );
917 recordLicenses( key, versionMetadata.getLicenses() );
919 recordDependencies( key, versionMetadata.getDependencies() );
921 MutationResult mutationResult = mutator.execute();
925 ColumnFamilyUpdater<String, String> updater = projectVersionMetadataModelTemplate.createUpdater( key );
926 updater.setString( "projectId", projectId );
927 updater.setString( "repositoryName", repositoryId );
928 updater.setString( "namespaceId", namespaceId );
929 updater.setString( "projectVersion", versionMetadata.getVersion() );
930 if ( StringUtils.isNotEmpty( versionMetadata.getDescription() ) )
932 updater.setString( "description", versionMetadata.getDescription() );
934 if ( StringUtils.isNotEmpty( versionMetadata.getName() ) )
936 updater.setString( "name", versionMetadata.getName() );
938 updater.setString( "incomplete", Boolean.toString( versionMetadata.isIncomplete() ) );
939 if ( StringUtils.isNotEmpty( versionMetadata.getUrl() ) )
941 updater.setString( "url", versionMetadata.getUrl() );
945 CiManagement ci = versionMetadata.getCiManagement();
948 updater.setString( "ciManagement.system", ci.getSystem() );
949 updater.setString( "ciManagement.url", ci.getUrl() );
953 IssueManagement issueManagement = versionMetadata.getIssueManagement();
954 if ( issueManagement != null )
956 updater.setString( "issueManagement.system", issueManagement.getSystem() );
957 updater.setString( "issueManagement.url", issueManagement.getUrl() );
961 Organization organization = versionMetadata.getOrganization();
962 if ( organization != null )
964 updater.setString( "organization.name", organization.getName() );
965 updater.setString( "organization.url", organization.getUrl() );
969 Scm scm = versionMetadata.getScm();
972 updater.setString( "scm.url", scm.getUrl() );
973 updater.setString( "scm.connection", scm.getConnection() );
974 updater.setString( "scm.developerConnection", scm.getDeveloperConnection() );
978 // update is a delete record
979 removeMailingList( key );
980 recordMailingList( key, versionMetadata.getMailingLists() );
982 removeLicenses( key );
983 recordLicenses( key, versionMetadata.getLicenses() );
985 removeDependencies( key );
986 recordDependencies( key, versionMetadata.getDependencies() );
988 projectVersionMetadataModelTemplate.update( updater );
992 ArtifactMetadataModel artifactMetadataModel = new ArtifactMetadataModel();
993 artifactMetadataModel.setRepositoryId( repositoryId );
994 artifactMetadataModel.setNamespace( namespaceId );
995 artifactMetadataModel.setProject( projectId );
996 artifactMetadataModel.setProjectVersion( versionMetadata.getVersion() );
997 artifactMetadataModel.setVersion( versionMetadata.getVersion() );
998 updateFacets( versionMetadata, artifactMetadataModel );
1004 public ProjectVersionMetadata getProjectVersion( final String repoId, final String namespace,
1005 final String projectId, final String projectVersion )
1006 throws MetadataResolutionException
1009 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
1010 StringSerializer ss = StringSerializer.get();
1011 QueryResult<OrderedRows<String, String, String>> result = HFactory //
1012 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1013 .setColumnFamily( cassandraArchivaManager.getProjectVersionMetadataFamilyName() ) //
1014 .setColumnNames( "projectVersion" ) //
1015 .addEqualsExpression( "repositoryName", repoId ) //
1016 .addEqualsExpression( "namespaceId", namespace ) //
1017 .addEqualsExpression( "projectId", projectId ) //
1018 .addEqualsExpression( "projectVersion", projectVersion ) //
1021 if ( result.get().getCount() < 1 )
1026 String key = result.get().iterator().next().getKey();
1028 ColumnFamilyResult<String, String> columnFamilyResult =
1029 this.projectVersionMetadataModelTemplate.queryColumns( key );
1031 if ( !columnFamilyResult.hasResults() )
1036 ProjectVersionMetadata projectVersionMetadata = new ProjectVersionMetadata();
1037 projectVersionMetadata.setId( columnFamilyResult.getString( "projectVersion" ) );
1038 projectVersionMetadata.setDescription( columnFamilyResult.getString( "description" ) );
1039 projectVersionMetadata.setName( columnFamilyResult.getString( "name" ) );
1041 projectVersionMetadata.setIncomplete( Boolean.parseBoolean( columnFamilyResult.getString( "incomplete" ) ) );
1043 projectVersionMetadata.setUrl( columnFamilyResult.getString( "url" ) );
1045 String ciUrl = columnFamilyResult.getString( "ciManagement.url" );
1046 String ciSystem = columnFamilyResult.getString( "ciManagement.system" );
1048 if ( StringUtils.isNotEmpty( ciSystem ) || StringUtils.isNotEmpty( ciUrl ) )
1050 projectVersionMetadata.setCiManagement( new CiManagement( ciSystem, ciUrl ) );
1054 String issueUrl = columnFamilyResult.getString( "issueManagement.url" );
1055 String issueSystem = columnFamilyResult.getString( "issueManagement.system" );
1056 if ( StringUtils.isNotEmpty( issueSystem ) || StringUtils.isNotEmpty( issueUrl ) )
1058 projectVersionMetadata.setIssueManagement( new IssueManagement( issueSystem, issueUrl ) );
1062 String organizationUrl = columnFamilyResult.getString( "organization.url" );
1063 String organizationName = columnFamilyResult.getString( "organization.name" );
1064 if ( StringUtils.isNotEmpty( organizationUrl ) || StringUtils.isNotEmpty( organizationName ) )
1066 projectVersionMetadata.setOrganization( new Organization( organizationName, organizationUrl ) );
1070 String devConn = columnFamilyResult.getString( "scm.developerConnection" );
1071 String conn = columnFamilyResult.getString( "scm.connection" );
1072 String url = columnFamilyResult.getString( "scm.url" );
1073 if ( StringUtils.isNotEmpty( devConn ) || StringUtils.isNotEmpty( conn ) || StringUtils.isNotEmpty( url ) )
1075 projectVersionMetadata.setScm( new Scm( conn, devConn, url ) );
1078 projectVersionMetadata.setMailingLists( getMailingLists( key ) );
1079 projectVersionMetadata.setLicenses( getLicenses( key ) );
1080 projectVersionMetadata.setDependencies( getDependencies( key ) );
1083 result = HFactory //
1084 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1085 .setColumnFamily( cassandraArchivaManager.getMetadataFacetFamilyName() ) //
1086 .setColumnNames( "facetId", "key", "value", "name" ) //
1087 .addEqualsExpression( "repositoryName", repoId ) //
1088 .addEqualsExpression( "namespaceId", namespace ) //
1089 .addEqualsExpression( "projectId", projectId ) //
1090 .addEqualsExpression( "projectVersion", projectVersion ) //
1093 Map<String, Map<String, String>> metadataFacetsPerFacetIds = new HashMap<String, Map<String, String>>();
1095 for ( Row<String, String, String> row : result.get() )
1097 ColumnSlice<String, String> columnSlice = row.getColumnSlice();
1098 String facetId = getStringValue( columnSlice, "facetId" );
1099 Map<String, String> metaValues = metadataFacetsPerFacetIds.get( facetId );
1100 if ( metaValues == null )
1102 metaValues = new HashMap<String, String>();
1103 metadataFacetsPerFacetIds.put( facetId, metaValues );
1105 metaValues.put( getStringValue( columnSlice, "key" ), getStringValue( columnSlice, "value" ) );
1108 if ( !metadataFacetsPerFacetIds.isEmpty() )
1110 for ( Map.Entry<String, Map<String, String>> entry : metadataFacetsPerFacetIds.entrySet() )
1112 MetadataFacetFactory metadataFacetFactory = metadataFacetFactories.get( entry.getKey() );
1113 if ( metadataFacetFactory != null )
1115 MetadataFacet metadataFacet = metadataFacetFactory.createMetadataFacet( repoId, entry.getKey() );
1116 metadataFacet.fromProperties( entry.getValue() );
1117 projectVersionMetadata.addFacet( metadataFacet );
1122 return projectVersionMetadata;
1125 protected void recordMailingList( String projectVersionMetadataKey, List<MailingList> mailingLists )
1127 if ( mailingLists == null || mailingLists.isEmpty() )
1131 Mutator<String> mailingMutator = this.mailingListTemplate.createMutator();
1132 for ( MailingList mailingList : mailingLists )
1134 // we don't care about the key as the real used one with the projectVersionMetadata
1135 String keyMailingList = UUID.randomUUID().toString();
1136 String cfMailingList = cassandraArchivaManager.getMailingListFamilyName();
1138 addInsertion( mailingMutator, keyMailingList, cfMailingList, "projectVersionMetadataModel.key",
1139 projectVersionMetadataKey );
1140 addInsertion( mailingMutator, keyMailingList, cfMailingList, "name", mailingList.getName() );
1141 addInsertion( mailingMutator, keyMailingList, cfMailingList, "mainArchiveUrl",
1142 mailingList.getMainArchiveUrl() );
1143 addInsertion( mailingMutator, keyMailingList, cfMailingList, "postAddress", mailingList.getPostAddress() );
1144 addInsertion( mailingMutator, keyMailingList, cfMailingList, "subscribeAddress",
1145 mailingList.getSubscribeAddress() );
1146 addInsertion( mailingMutator, keyMailingList, cfMailingList, "unsubscribeAddress",
1147 mailingList.getUnsubscribeAddress() );
1149 for ( String otherArchive : mailingList.getOtherArchives() )
1151 addInsertion( mailingMutator, keyMailingList, cfMailingList, "otherArchive." + idx, otherArchive );
1156 mailingMutator.execute();
1159 protected void removeMailingList( String projectVersionMetadataKey )
1161 StringSerializer ss = StringSerializer.get();
1162 QueryResult<OrderedRows<String, String, String>> result =
1163 HFactory.createRangeSlicesQuery( cassandraArchivaManager.getKeyspace(), ss, ss, ss ) //
1164 .setColumnFamily( cassandraArchivaManager.getMailingListFamilyName() ) //
1165 .setColumnNames( "name" ) //
1166 .setRowCount( Integer.MAX_VALUE ) //
1167 .addEqualsExpression( "projectVersionMetadataModel.key", projectVersionMetadataKey ) //
1169 for ( Row<String, String, String> row : result.get() )
1171 this.mailingListTemplate.deleteRow( row.getKey() );
1175 protected List<MailingList> getMailingLists( String projectVersionMetadataKey )
1177 List<MailingList> mailingLists = new ArrayList<MailingList>();
1179 StringSerializer ss = StringSerializer.get();
1180 QueryResult<OrderedRows<String, String, String>> result =
1181 HFactory.createRangeSlicesQuery( cassandraArchivaManager.getKeyspace(), ss, ss, ss ) //
1182 .setColumnFamily( cassandraArchivaManager.getMailingListFamilyName() ) //
1183 .setColumnNames( "name" ) //
1184 .setRowCount( Integer.MAX_VALUE ) //
1185 .addEqualsExpression( "projectVersionMetadataModel.key", projectVersionMetadataKey ) //
1187 for ( Row<String, String, String> row : result.get() )
1189 ColumnFamilyResult<String, String> columnFamilyResult =
1190 this.mailingListTemplate.queryColumns( row.getKey() );
1192 MailingList mailingList = new MailingList();
1193 mailingList.setName( columnFamilyResult.getString( "name" ) );
1194 mailingList.setMainArchiveUrl( columnFamilyResult.getString( "mainArchiveUrl" ) );
1195 mailingList.setPostAddress( columnFamilyResult.getString( "postAddress" ) );
1196 mailingList.setSubscribeAddress( columnFamilyResult.getString( "subscribeAddress" ) );
1197 mailingList.setUnsubscribeAddress( columnFamilyResult.getString( "unsubscribeAddress" ) );
1199 List<String> otherArchives = new ArrayList<String>();
1201 for ( String columnName : columnFamilyResult.getColumnNames() )
1203 if ( StringUtils.startsWith( columnName, "otherArchive." ) )
1205 otherArchives.add( columnFamilyResult.getString( columnName ) );
1209 mailingList.setOtherArchives( otherArchives );
1210 mailingLists.add( mailingList );
1213 return mailingLists;
1216 protected void recordLicenses( String projectVersionMetadataKey, List<License> licenses )
1219 if ( licenses == null || licenses.isEmpty() )
1223 Mutator<String> licenseMutator = this.licenseTemplate.createMutator();
1225 for ( License license : licenses )
1227 // we don't care about the key as the real used one with the projectVersionMetadata
1228 String keyLicense = UUID.randomUUID().toString();
1229 String cfLicense = cassandraArchivaManager.getLicenseFamilyName();
1231 addInsertion( licenseMutator, keyLicense, cfLicense, "projectVersionMetadataModel.key",
1232 projectVersionMetadataKey );
1234 addInsertion( licenseMutator, keyLicense, cfLicense, "name", license.getName() );
1236 addInsertion( licenseMutator, keyLicense, cfLicense, "url", license.getUrl() );
1239 licenseMutator.execute();
1242 protected void removeLicenses( String projectVersionMetadataKey )
1244 StringSerializer ss = StringSerializer.get();
1245 QueryResult<OrderedRows<String, String, String>> result =
1246 HFactory.createRangeSlicesQuery( cassandraArchivaManager.getKeyspace(), ss, ss, ss ) //
1247 .setColumnFamily( cassandraArchivaManager.getLicenseFamilyName() ) //
1248 .setColumnNames( "name" ) //
1249 .setRowCount( Integer.MAX_VALUE ) //
1250 .addEqualsExpression( "projectVersionMetadataModel.key", projectVersionMetadataKey ) //
1252 for ( Row<String, String, String> row : result.get() )
1254 this.licenseTemplate.deleteRow( row.getKey() );
1258 protected List<License> getLicenses( String projectVersionMetadataKey )
1260 List<License> licenses = new ArrayList<License>();
1262 StringSerializer ss = StringSerializer.get();
1263 QueryResult<OrderedRows<String, String, String>> result =
1264 HFactory.createRangeSlicesQuery( cassandraArchivaManager.getKeyspace(), ss, ss, ss ) //
1265 .setColumnFamily( cassandraArchivaManager.getLicenseFamilyName() ) //
1266 .setColumnNames( "projectVersionMetadataModel.key" ) //
1267 .setRowCount( Integer.MAX_VALUE ) //
1268 .addEqualsExpression( "projectVersionMetadataModel.key", projectVersionMetadataKey ) //
1271 for ( Row<String, String, String> row : result.get() )
1273 ColumnFamilyResult<String, String> columnFamilyResult = this.licenseTemplate.queryColumns( row.getKey() );
1276 new License( columnFamilyResult.getString( "name" ), columnFamilyResult.getString( "url" ) ) );
1283 protected void recordDependencies( String projectVersionMetadataKey, List<Dependency> dependencies )
1286 if ( dependencies == null || dependencies.isEmpty() )
1290 Mutator<String> dependencyMutator = this.dependencyTemplate.createMutator();
1292 for ( Dependency dependency : dependencies )
1294 // we don't care about the key as the real used one with the projectVersionMetadata
1295 String keyDependency = UUID.randomUUID().toString();
1296 String cfDependency = cassandraArchivaManager.getDependencyFamilyName();
1298 addInsertion( dependencyMutator, keyDependency, cfDependency, "projectVersionMetadataModel.key",
1299 projectVersionMetadataKey );
1301 addInsertion( dependencyMutator, keyDependency, cfDependency, "classifier", dependency.getClassifier() );
1303 addInsertion( dependencyMutator, keyDependency, cfDependency, "optional",
1304 Boolean.toString( dependency.isOptional() ) );
1306 addInsertion( dependencyMutator, keyDependency, cfDependency, "scope", dependency.getScope() );
1308 addInsertion( dependencyMutator, keyDependency, cfDependency, "systemPath", dependency.getSystemPath() );
1310 addInsertion( dependencyMutator, keyDependency, cfDependency, "type", dependency.getType() );
1312 addInsertion( dependencyMutator, keyDependency, cfDependency, "artifactId", dependency.getArtifactId() );
1314 addInsertion( dependencyMutator, keyDependency, cfDependency, "groupId", dependency.getGroupId() );
1316 addInsertion( dependencyMutator, keyDependency, cfDependency, "version", dependency.getVersion() );
1319 dependencyMutator.execute();
1322 protected void removeDependencies( String projectVersionMetadataKey )
1324 StringSerializer ss = StringSerializer.get();
1325 QueryResult<OrderedRows<String, String, String>> result =
1326 HFactory.createRangeSlicesQuery( cassandraArchivaManager.getKeyspace(), ss, ss, ss ) //
1327 .setColumnFamily( cassandraArchivaManager.getDependencyFamilyName() ) //
1328 .setColumnNames( "groupId" ) //
1329 .setRowCount( Integer.MAX_VALUE ) //
1330 .addEqualsExpression( "projectVersionMetadataModel.key", projectVersionMetadataKey ) //
1332 for ( Row<String, String, String> row : result.get() )
1334 this.dependencyTemplate.deleteRow( row.getKey() );
1338 protected List<Dependency> getDependencies( String projectVersionMetadataKey )
1340 List<Dependency> dependencies = new ArrayList<Dependency>();
1342 StringSerializer ss = StringSerializer.get();
1343 QueryResult<OrderedRows<String, String, String>> result =
1344 HFactory.createRangeSlicesQuery( cassandraArchivaManager.getKeyspace(), ss, ss, ss ) //
1345 .setColumnFamily( cassandraArchivaManager.getDependencyFamilyName() ) //
1346 .setColumnNames( "projectVersionMetadataModel.key" ) //
1347 .setRowCount( Integer.MAX_VALUE ) //
1348 .addEqualsExpression( "projectVersionMetadataModel.key", projectVersionMetadataKey ) //
1351 for ( Row<String, String, String> row : result.get() )
1353 ColumnFamilyResult<String, String> columnFamilyResult =
1354 this.dependencyTemplate.queryColumns( row.getKey() );
1356 Dependency dependency = new Dependency();
1357 dependency.setClassifier( columnFamilyResult.getString( "classifier" ) );
1359 dependency.setOptional( Boolean.parseBoolean( columnFamilyResult.getString( "optional" ) ) );
1361 dependency.setScope( columnFamilyResult.getString( "scope" ) );
1363 dependency.setSystemPath( columnFamilyResult.getString( "systemPath" ) );
1365 dependency.setType( columnFamilyResult.getString( "type" ) );
1367 dependency.setArtifactId( columnFamilyResult.getString( "artifactId" ) );
1369 dependency.setGroupId( columnFamilyResult.getString( "groupId" ) );
1371 dependency.setVersion( columnFamilyResult.getString( "version" ) );
1373 dependencies.add( dependency );
1376 return dependencies;
1380 public void updateArtifact( String repositoryId, String namespaceId, String projectId, String projectVersion,
1381 ArtifactMetadata artifactMeta )
1382 throws MetadataRepositoryException
1385 Namespace namespace = getNamespace( repositoryId, namespaceId );
1386 if ( namespace == null )
1388 namespace = updateOrAddNamespace( repositoryId, namespaceId );
1391 ProjectMetadata projectMetadata = new ProjectMetadata();
1392 projectMetadata.setId( projectId );
1393 projectMetadata.setNamespace( namespaceId );
1394 updateProject( repositoryId, projectMetadata );
1396 String key = new ArtifactMetadataModel.KeyBuilder().withNamespace( namespace ).withProject( projectId ).withId(
1397 artifactMeta.getId() ).withProjectVersion( projectVersion ).build();
1401 boolean exists = this.artifactMetadataTemplate.isColumnsExist( key );
1406 ColumnFamilyUpdater<String, String> updater = this.artifactMetadataTemplate.createUpdater( key );
1407 updater.setLong( "fileLastModified", artifactMeta.getFileLastModified().getTime() );
1408 updater.setLong( "whenGathered", artifactMeta.getWhenGathered().getTime() );
1409 updater.setLong( "size", artifactMeta.getSize() );
1410 updater.setString( "md5", artifactMeta.getMd5() );
1411 updater.setString( "sha1", artifactMeta.getSha1() );
1412 updater.setString( "version", artifactMeta.getVersion() );
1413 this.artifactMetadataTemplate.update( updater );
1417 String cf = this.cassandraArchivaManager.getArtifactMetadataFamilyName();
1419 this.artifactMetadataTemplate.createMutator() //
1420 .addInsertion( key, cf, column( "id", artifactMeta.getId() ) )//
1421 .addInsertion( key, cf, column( "repositoryName", repositoryId ) ) //
1422 .addInsertion( key, cf, column( "namespaceId", namespaceId ) ) //
1423 .addInsertion( key, cf, column( "project", artifactMeta.getProject() ) ) //
1424 .addInsertion( key, cf, column( "projectVersion", projectVersion ) ) //
1425 .addInsertion( key, cf, column( "version", artifactMeta.getVersion() ) ) //
1426 .addInsertion( key, cf, column( "fileLastModified", artifactMeta.getFileLastModified().getTime() ) ) //
1427 .addInsertion( key, cf, column( "size", artifactMeta.getSize() ) ) //
1428 .addInsertion( key, cf, column( "md5", artifactMeta.getMd5() ) ) //
1429 .addInsertion( key, cf, column( "sha1", artifactMeta.getSha1() ) ) //
1430 .addInsertion( key, cf, column( "whenGathered", artifactMeta.getWhenGathered().getTime() ) )//
1434 key = new ProjectVersionMetadataModel.KeyBuilder() //
1435 .withRepository( repositoryId ) //
1436 .withNamespace( namespace ) //
1437 .withProjectId( projectId ) //
1438 .withId( artifactMeta.getId() ) //
1441 exists = this.projectVersionMetadataModelTemplate.isColumnsExist( key );
1445 String cf = this.cassandraArchivaManager.getProjectVersionMetadataFamilyName();
1447 projectVersionMetadataModelTemplate.createMutator() //
1448 .addInsertion( key, cf, column( "namespaceId", namespace.getName() ) ) //
1449 .addInsertion( key, cf, column( "repositoryName", repositoryId ) ) //
1450 .addInsertion( key, cf, column( "projectVersion", projectVersion ) ) //
1451 .addInsertion( key, cf, column( "projectId", projectId ) ) //
1452 .addInsertion( key, cf, column( "version", artifactMeta.getVersion() ) ) //
1457 ArtifactMetadataModel artifactMetadataModel = new ArtifactMetadataModel();
1459 artifactMetadataModel.setRepositoryId( repositoryId );
1460 artifactMetadataModel.setNamespace( namespaceId );
1461 artifactMetadataModel.setProject( projectId );
1462 artifactMetadataModel.setProjectVersion( projectVersion );
1463 artifactMetadataModel.setVersion( artifactMeta.getVersion() );
1464 artifactMetadataModel.setFileLastModified( artifactMeta.getFileLastModified() == null
1465 ? new Date().getTime()
1466 : artifactMeta.getFileLastModified().getTime() );
1469 updateFacets( artifactMeta, artifactMetadataModel );
1474 public Collection<String> getArtifactVersions( final String repoId, final String namespace, final String projectId,
1475 final String projectVersion )
1476 throws MetadataResolutionException
1478 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
1479 StringSerializer ss = StringSerializer.get();
1480 QueryResult<OrderedRows<String, String, String>> result = HFactory //
1481 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1482 .setColumnFamily( cassandraArchivaManager.getProjectVersionMetadataFamilyName() ) //
1483 .setColumnNames( "version" ) //
1484 .addEqualsExpression( "repositoryName", repoId ) //
1485 .addEqualsExpression( "namespaceId", namespace ) //
1486 .addEqualsExpression( "projectId", projectId ) //
1487 .addEqualsExpression( "projectVersion", projectVersion ) //
1490 final Set<String> versions = new HashSet<String>();
1492 for ( Row<String, String, String> row : result.get() )
1494 versions.add( getStringValue( row.getColumnSlice(), "version" ) );
1502 * iterate over available facets to remove/add from the artifactMetadata
1504 * @param facetedMetadata
1505 * @param artifactMetadataModel only use for the key
1507 private void updateFacets( final FacetedMetadata facetedMetadata,
1508 final ArtifactMetadataModel artifactMetadataModel )
1510 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
1511 StringSerializer ss = StringSerializer.get();
1512 String cf = cassandraArchivaManager.getMetadataFacetFamilyName();
1514 for ( final String facetId : metadataFacetFactories.keySet() )
1516 MetadataFacet metadataFacet = facetedMetadata.getFacet( facetId );
1517 if ( metadataFacet == null )
1523 QueryResult<OrderedRows<String, String, String>> result =
1524 HFactory.createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1525 .setColumnFamily( cf ) //
1526 .setColumnNames( "repositoryName" ) //
1527 .addEqualsExpression( "repositoryName", artifactMetadataModel.getRepositoryId() ) //
1528 .addEqualsExpression( "namespaceId", artifactMetadataModel.getNamespace() ) //
1529 .addEqualsExpression( "projectId", artifactMetadataModel.getProject() ) //
1530 .addEqualsExpression( "projectVersion",
1531 artifactMetadataModel.getProjectVersion() ).addEqualsExpression( "facetId",
1535 for ( Row<String, String, String> row : result.get().getList() )
1537 this.metadataFacetTemplate.deleteRow( row.getKey() );
1540 Map<String, String> properties = metadataFacet.toProperties();
1542 for ( Map.Entry<String, String> entry : properties.entrySet() )
1544 String key = new MetadataFacetModel.KeyBuilder().withKey( entry.getKey() ).withArtifactMetadataModel(
1545 artifactMetadataModel ).withFacetId( facetId ).withName( metadataFacet.getName() ).build();
1546 metadataFacetTemplate.createMutator() //
1547 .addInsertion( key, cf, column( "repositoryName", artifactMetadataModel.getRepositoryId() ) ) //
1548 .addInsertion( key, cf, column( "namespaceId", artifactMetadataModel.getNamespace() ) ) //
1549 .addInsertion( key, cf, column( "projectId", artifactMetadataModel.getProject() ) ) //
1550 .addInsertion( key, cf, column( "projectVersion", artifactMetadataModel.getProjectVersion() ) ) //
1551 .addInsertion( key, cf, column( "facetId", facetId ) ) //
1552 .addInsertion( key, cf, column( "key", entry.getKey() ) ) //
1553 .addInsertion( key, cf, column( "value", entry.getValue() ) ) //
1554 .addInsertion( key, cf, column( "name", metadataFacet.getName() ) ) //
1562 public List<String> getMetadataFacets( final String repositoryId, final String facetId )
1563 throws MetadataRepositoryException
1566 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
1567 StringSerializer ss = StringSerializer.get();
1568 QueryResult<OrderedRows<String, String, String>> result = HFactory //
1569 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1570 .setColumnFamily( cassandraArchivaManager.getMetadataFacetFamilyName() ) //
1571 .setColumnNames( "name" ) //
1572 .addEqualsExpression( "repositoryName", repositoryId ) //
1573 .addEqualsExpression( "facetId", facetId ) //
1576 final List<String> facets = new ArrayList<String>();
1578 for ( Row<String, String, String> row : result.get() )
1580 facets.add( getStringValue( row.getColumnSlice(), "name" ) );
1586 public boolean hasMetadataFacet( String repositoryId, String facetId )
1587 throws MetadataRepositoryException
1589 return !getMetadataFacets( repositoryId, facetId ).isEmpty();
1593 public MetadataFacet getMetadataFacet( final String repositoryId, final String facetId, final String name )
1594 throws MetadataRepositoryException
1597 MetadataFacetFactory metadataFacetFactory = metadataFacetFactories.get( facetId );
1598 if ( metadataFacetFactory == null )
1603 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
1604 StringSerializer ss = StringSerializer.get();
1605 QueryResult<OrderedRows<String, String, String>> result = HFactory //
1606 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1607 .setColumnFamily( cassandraArchivaManager.getMetadataFacetFamilyName() ) //
1608 .setColumnNames( "key", "value" ) //
1609 .addEqualsExpression( "repositoryName", repositoryId ) //
1610 .addEqualsExpression( "facetId", facetId ) //
1611 .addEqualsExpression( "name", name ) //
1614 MetadataFacet metadataFacet = metadataFacetFactory.createMetadataFacet( repositoryId, name );
1615 int size = result.get().getCount();
1620 Map<String, String> map = new HashMap<String, String>( size );
1621 for ( Row<String, String, String> row : result.get() )
1623 ColumnSlice<String, String> columnSlice = row.getColumnSlice();
1624 map.put( getStringValue( columnSlice, "key" ), getStringValue( columnSlice, "value" ) );
1626 metadataFacet.fromProperties( map );
1627 return metadataFacet;
1631 public void addMetadataFacet( String repositoryId, MetadataFacet metadataFacet )
1632 throws MetadataRepositoryException
1635 if ( metadataFacet == null )
1640 if ( metadataFacet.toProperties().isEmpty() )
1642 String key = new MetadataFacetModel.KeyBuilder().withRepositoryId( repositoryId ).withFacetId(
1643 metadataFacet.getFacetId() ).withName( metadataFacet.getName() ).build();
1645 boolean exists = this.metadataFacetTemplate.isColumnsExist( key );
1649 ColumnFamilyUpdater<String, String> updater = this.metadataFacetTemplate.createUpdater( key );
1650 updater.setString( "facetId", metadataFacet.getFacetId() );
1651 updater.setString( "name", metadataFacet.getName() );
1652 this.metadataFacetTemplate.update( updater );
1656 String cf = this.cassandraArchivaManager.getMetadataFacetFamilyName();
1657 this.metadataFacetTemplate.createMutator() //
1658 .addInsertion( key, cf, column( "repositoryName", repositoryId ) ) //
1659 .addInsertion( key, cf, column( "facetId", metadataFacet.getFacetId() ) ) //
1660 .addInsertion( key, cf, column( "name", metadataFacet.getName() ) ) //
1667 for ( Map.Entry<String, String> entry : metadataFacet.toProperties().entrySet() )
1670 String key = new MetadataFacetModel.KeyBuilder().withRepositoryId( repositoryId ).withFacetId(
1671 metadataFacet.getFacetId() ).withName( metadataFacet.getName() ).withKey( entry.getKey() ).build();
1673 boolean exists = this.metadataFacetTemplate.isColumnsExist( key );
1676 //metadataFacetModel = new MetadataFacetModel();
1677 // we need to store the repositoryId
1678 //ArtifactMetadataModel artifactMetadataModel = new ArtifactMetadataModel();
1679 //artifactMetadataModel.setRepositoryId( repositoryId );
1680 //metadataFacetModel.setArtifactMetadataModel( artifactMetadataModel );
1681 //metadataFacetModel.setId( key );
1682 //metadataFacetModel.setKey( entry.getKey() );
1683 //metadataFacetModel.setFacetId( metadataFacet.getFacetId() );
1684 //metadataFacetModel.setName( metadataFacet.getName() );
1686 String cf = this.cassandraArchivaManager.getMetadataFacetFamilyName();
1687 this.metadataFacetTemplate.createMutator() //
1688 .addInsertion( key, cf, column( "repositoryName", repositoryId ) ) //
1689 .addInsertion( key, cf, column( "facetId", metadataFacet.getFacetId() ) ) //
1690 .addInsertion( key, cf, column( "name", metadataFacet.getName() ) ) //
1691 .addInsertion( key, cf, column( "key", entry.getKey() ) ) //
1692 .addInsertion( key, cf, column( "value", entry.getValue() ) ) //
1698 ColumnFamilyUpdater<String, String> updater = this.metadataFacetTemplate.createUpdater( key );
1699 updater.setString( "value", entry.getValue() );
1700 this.metadataFacetTemplate.update( updater );
1707 public void removeMetadataFacets( final String repositoryId, final String facetId )
1708 throws MetadataRepositoryException
1710 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
1711 StringSerializer ss = StringSerializer.get();
1712 QueryResult<OrderedRows<String, String, String>> result = HFactory //
1713 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1714 .setColumnFamily( cassandraArchivaManager.getMetadataFacetFamilyName() ) //
1715 .setColumnNames( "key", "value" ) //
1716 .addEqualsExpression( "repositoryName", repositoryId ) //
1717 .addEqualsExpression( "facetId", facetId ) //
1720 for ( Row<String, String, String> row : result.get() )
1722 this.metadataFacetTemplate.deleteRow( row.getKey() );
1728 public void removeMetadataFacet( final String repositoryId, final String facetId, final String name )
1729 throws MetadataRepositoryException
1731 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
1732 StringSerializer ss = StringSerializer.get();
1733 QueryResult<OrderedRows<String, String, String>> result = HFactory //
1734 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1735 .setColumnFamily( cassandraArchivaManager.getMetadataFacetFamilyName() ) //
1736 .setColumnNames( "key", "value" ) //
1737 .addEqualsExpression( "repositoryName", repositoryId ) //
1738 .addEqualsExpression( "facetId", facetId ) //
1739 .addEqualsExpression( "name", name ) //
1742 for ( Row<String, String, String> row : result.get() )
1744 this.metadataFacetTemplate.deleteRow( row.getKey() );
1749 public List<ArtifactMetadata> getArtifactsByDateRange( final String repositoryId, final Date startTime,
1750 final Date endTime )
1751 throws MetadataRepositoryException
1754 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
1755 StringSerializer ss = StringSerializer.get();
1756 LongSerializer ls = LongSerializer.get();
1757 RangeSlicesQuery<String, String, Long> query = HFactory //
1758 .createRangeSlicesQuery( keyspace, ss, ss, ls ) //
1759 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataFamilyName() ) //
1760 .setColumnNames( "namespaceId", "size", "id", "fileLastModified", "md5", "project", "projectVersion",
1761 "repositoryName", "version", "whenGathered", "sha1" ); //
1763 if ( startTime != null )
1765 query = query.addGteExpression( "whenGathered", startTime.getTime() );
1767 if ( endTime != null )
1769 query = query.addLteExpression( "whenGathered", endTime.getTime() );
1771 QueryResult<OrderedRows<String, String, Long>> result = query.execute();
1773 List<ArtifactMetadata> artifactMetadatas = new ArrayList<ArtifactMetadata>( result.get().getCount() );
1775 for ( Row<String, String, Long> row : result.get() )
1777 ColumnSlice<String, Long> columnSlice = row.getColumnSlice();
1778 String repositoryName = getAsStringValue( columnSlice, "repositoryName" );
1779 if ( StringUtils.equals( repositoryName, repositoryId ) )
1782 artifactMetadatas.add( mapArtifactMetadataLongColumnSlice( columnSlice ) );
1786 return artifactMetadatas;
1790 protected ArtifactMetadata mapArtifactMetadataLongColumnSlice( ColumnSlice<String, Long> columnSlice )
1792 ArtifactMetadata artifactMetadata = new ArtifactMetadata();
1793 artifactMetadata.setNamespace( getAsStringValue( columnSlice, "namespaceId" ) );
1794 artifactMetadata.setSize( getLongValue( columnSlice, "size" ) );
1795 artifactMetadata.setId( getAsStringValue( columnSlice, "id" ) );
1796 artifactMetadata.setFileLastModified( getLongValue( columnSlice, "fileLastModified" ) );
1797 artifactMetadata.setMd5( getAsStringValue( columnSlice, "md5" ) );
1798 artifactMetadata.setProject( getAsStringValue( columnSlice, "project" ) );
1799 artifactMetadata.setProjectVersion( getAsStringValue( columnSlice, "projectVersion" ) );
1800 artifactMetadata.setRepositoryId( getAsStringValue( columnSlice, "repositoryName" ) );
1801 artifactMetadata.setSha1( getAsStringValue( columnSlice, "sha1" ) );
1802 artifactMetadata.setVersion( getAsStringValue( columnSlice, "version" ) );
1803 Long whenGathered = getLongValue( columnSlice, "whenGathered" );
1804 if ( whenGathered != null )
1806 artifactMetadata.setWhenGathered( new Date( whenGathered ) );
1808 return artifactMetadata;
1811 protected ArtifactMetadata mapArtifactMetadataStringColumnSlice( ColumnSlice<String, String> columnSlice )
1813 ArtifactMetadata artifactMetadata = new ArtifactMetadata();
1814 artifactMetadata.setNamespace( getStringValue( columnSlice, "namespaceId" ) );
1815 artifactMetadata.setSize( getAsLongValue( columnSlice, "size" ) );
1816 artifactMetadata.setId( getStringValue( columnSlice, "id" ) );
1817 artifactMetadata.setFileLastModified( getAsLongValue( columnSlice, "fileLastModified" ) );
1818 artifactMetadata.setMd5( getStringValue( columnSlice, "md5" ) );
1819 artifactMetadata.setProject( getStringValue( columnSlice, "project" ) );
1820 artifactMetadata.setProjectVersion( getStringValue( columnSlice, "projectVersion" ) );
1821 artifactMetadata.setRepositoryId( getStringValue( columnSlice, "repositoryName" ) );
1822 artifactMetadata.setSha1( getStringValue( columnSlice, "sha1" ) );
1823 artifactMetadata.setVersion( getStringValue( columnSlice, "version" ) );
1824 Long whenGathered = getAsLongValue( columnSlice, "whenGathered" );
1825 if ( whenGathered != null )
1827 artifactMetadata.setWhenGathered( new Date( whenGathered ) );
1829 return artifactMetadata;
1833 public Collection<ArtifactMetadata> getArtifactsByChecksum( final String repositoryId, final String checksum )
1834 throws MetadataRepositoryException
1836 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
1837 StringSerializer ss = StringSerializer.get();
1839 // cql cannot run or in queries so running twice the query
1840 Map<String, ArtifactMetadata> artifactMetadataMap = new HashMap<String, ArtifactMetadata>();
1842 RangeSlicesQuery<String, String, String> query = HFactory //
1843 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1844 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataFamilyName() ) //
1845 .setColumnNames( "namespaceId", "size", "id", "fileLastModified", "md5", "project", "projectVersion",
1846 "repositoryName", "version", "whenGathered", "sha1" ); //
1848 query = query.addEqualsExpression( "sha1", checksum ).addEqualsExpression( "repositoryName", repositoryId );
1850 QueryResult<OrderedRows<String, String, String>> result = query.execute();
1852 for ( Row<String, String, String> row : result.get() )
1854 ColumnSlice<String, String> columnSlice = row.getColumnSlice();
1856 artifactMetadataMap.put( row.getKey(), mapArtifactMetadataStringColumnSlice( columnSlice ) );
1861 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1862 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataFamilyName() ) //
1863 .setColumnNames( "namespaceId", "size", "id", "fileLastModified", "md5", "project", "projectVersion",
1864 "repositoryName", "version", "whenGathered", "sha1" ); //
1866 query = query.addEqualsExpression( "md5", checksum ).addEqualsExpression( "repositoryName", repositoryId );
1868 result = query.execute();
1870 for ( Row<String, String, String> row : result.get() )
1872 ColumnSlice<String, String> columnSlice = row.getColumnSlice();
1874 artifactMetadataMap.put( row.getKey(), mapArtifactMetadataStringColumnSlice( columnSlice ) );
1878 return artifactMetadataMap.values();
1883 public void removeArtifact( final String repositoryId, final String namespace, final String project,
1884 final String version, final String id )
1885 throws MetadataRepositoryException
1887 logger.debug( "removeArtifact repositoryId: '{}', namespace: '{}', project: '{}', version: '{}', id: '{}'",
1888 repositoryId, namespace, project, version, id );
1890 new ArtifactMetadataModel.KeyBuilder().withRepositoryId( repositoryId ).withNamespace( namespace ).withId(
1891 id ).withProjectVersion( version ).withProject( project ).build();
1893 this.artifactMetadataTemplate.deleteRow( key );
1896 new ProjectVersionMetadataModel.KeyBuilder().withId( version ).withRepository( repositoryId ).withNamespace(
1897 namespace ).withProjectId( project ).build();
1899 this.projectVersionMetadataModelTemplate.deleteRow( key );
1903 public void removeArtifact( ArtifactMetadata artifactMetadata, String baseVersion )
1904 throws MetadataRepositoryException
1906 logger.debug( "removeArtifact repositoryId: '{}', namespace: '{}', project: '{}', version: '{}', id: '{}'",
1907 artifactMetadata.getRepositoryId(), artifactMetadata.getNamespace(),
1908 artifactMetadata.getProject(), baseVersion, artifactMetadata.getId() );
1910 new ArtifactMetadataModel.KeyBuilder().withRepositoryId( artifactMetadata.getRepositoryId() ).withNamespace(
1911 artifactMetadata.getNamespace() ).withId( artifactMetadata.getId() ).withProjectVersion(
1912 baseVersion ).withProject( artifactMetadata.getProject() ).build();
1914 this.artifactMetadataTemplate.deleteRow( key );
1919 public void removeArtifact( final String repositoryId, final String namespace, final String project,
1920 final String version, final MetadataFacet metadataFacet )
1921 throws MetadataRepositoryException
1924 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
1926 StringSerializer ss = StringSerializer.get();
1928 RangeSlicesQuery<String, String, String> query = HFactory //
1929 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1930 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataFamilyName() ) //
1931 .setColumnNames( "namespaceId" ); //
1933 query = query.addEqualsExpression( "repositoryName", repositoryId ) //
1934 .addEqualsExpression( "namespaceId", namespace ) //
1935 .addEqualsExpression( "project", project ) //
1936 .addEqualsExpression( "version", version );
1938 QueryResult<OrderedRows<String, String, String>> result = query.execute();
1940 for ( Row<String, String, String> row : result.get() )
1942 this.artifactMetadataTemplate.deleteRow( row.getKey() );
1950 public List<ArtifactMetadata> getArtifacts( final String repositoryId )
1951 throws MetadataRepositoryException
1953 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
1954 StringSerializer ss = StringSerializer.get();
1956 RangeSlicesQuery<String, String, String> query = HFactory //
1957 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1958 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataFamilyName() ) //
1959 .setColumnNames( "namespaceId", "size", "id", "fileLastModified", "md5", "project", "projectVersion",
1960 "repositoryName", "version", "whenGathered", "sha1" ); //
1962 query = query.addEqualsExpression( "repositoryName", repositoryId );
1964 QueryResult<OrderedRows<String, String, String>> result = query.execute();
1966 List<ArtifactMetadata> artifactMetadatas = new ArrayList<ArtifactMetadata>( result.get().getCount() );
1968 for ( Row<String, String, String> row : result.get() )
1970 ColumnSlice<String, String> columnSlice = row.getColumnSlice();
1972 artifactMetadatas.add( mapArtifactMetadataStringColumnSlice( columnSlice ) );
1976 return artifactMetadatas;
1981 public Collection<ProjectVersionReference> getProjectReferences( String repoId, String namespace, String projectId,
1982 String projectVersion )
1983 throws MetadataResolutionException
1985 // FIXME implement this
1986 return Collections.emptyList();
1990 public void removeProjectVersion( final String repoId, final String namespace, final String projectId,
1991 final String projectVersion )
1992 throws MetadataRepositoryException
1995 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
1997 StringSerializer ss = StringSerializer.get();
1999 QueryResult<OrderedRows<String, String, String>> result = HFactory //
2000 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
2001 .setColumnFamily( cassandraArchivaManager.getProjectVersionMetadataFamilyName() ) //
2002 .setColumnNames( "version" ) //
2003 .addEqualsExpression( "repositoryName", repoId ) //
2004 .addEqualsExpression( "namespaceId", namespace ) //
2005 .addEqualsExpression( "projectId", projectId ) //
2006 .addEqualsExpression( "projectVersion", projectVersion ) //
2009 for ( Row<String, String, String> row : result.get().getList() )
2011 this.projectVersionMetadataModelTemplate.deleteRow( row.getKey() );
2014 RangeSlicesQuery<String, String, String> query = HFactory //
2015 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
2016 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataFamilyName() ) //
2017 .setColumnNames( "namespaceId" ); //
2019 query = query.addEqualsExpression( "repositoryName", repoId ) //
2020 .addEqualsExpression( "namespaceId", namespace ) //
2021 .addEqualsExpression( "project", projectId ) //
2022 .addEqualsExpression( "projectVersion", projectVersion );
2024 result = query.execute();
2026 for ( Row<String, String, String> row : result.get() )
2028 this.artifactMetadataTemplate.deleteRow( row.getKey() );
2034 public Collection<ArtifactMetadata> getArtifacts( final String repoId, final String namespace,
2035 final String projectId, final String projectVersion )
2036 throws MetadataResolutionException
2039 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
2041 StringSerializer ss = StringSerializer.get();
2043 QueryResult<OrderedRows<String, String, String>> result =
2044 HFactory.createRangeSlicesQuery( keyspace, ss, ss, ss ) //
2045 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataFamilyName() ) //
2046 .setColumnNames( "id", "repositoryName", "namespaceId", "project", "projectVersion", "version",
2047 "fileLastModified", "size", "md5", "sha1", "whenGathered" )//
2048 .setRowCount( Integer.MAX_VALUE ) //
2049 .addEqualsExpression( "repositoryName", repoId ) //
2050 .addEqualsExpression( "namespaceId", namespace ) //
2051 .addEqualsExpression( "project", projectId ) //
2052 .addEqualsExpression( "projectVersion", projectVersion ) //
2055 if ( result.get() == null || result.get().getCount() < 1 )
2057 return Collections.emptyList();
2060 List<ArtifactMetadata> artifactMetadatas = new ArrayList<ArtifactMetadata>( result.get().getCount() );
2062 for ( Row<String, String, String> row : result.get() )
2064 ColumnSlice<String, String> columnSlice = row.getColumnSlice();
2065 ArtifactMetadata artifactMetadata = new ArtifactMetadata();
2066 artifactMetadata.setNamespace( getStringValue( columnSlice, "namespaceId" ) );
2067 artifactMetadata.setSize( getAsLongValue( columnSlice, "size" ) );
2068 artifactMetadata.setId( getStringValue( columnSlice, "id" ) );
2069 artifactMetadata.setFileLastModified( getAsLongValue( columnSlice, "fileLastModified" ) );
2070 artifactMetadata.setMd5( getStringValue( columnSlice, "md5" ) );
2071 artifactMetadata.setProject( getStringValue( columnSlice, "project" ) );
2072 artifactMetadata.setProjectVersion( getStringValue( columnSlice, "projectVersion" ) );
2073 artifactMetadata.setRepositoryId( repoId );
2074 artifactMetadata.setSha1( getStringValue( columnSlice, "sha1" ) );
2075 artifactMetadata.setVersion( getStringValue( columnSlice, "version" ) );
2076 Long whenGathered = getAsLongValue( columnSlice, "whenGathered" );
2077 if ( whenGathered != null )
2079 artifactMetadata.setWhenGathered( new Date( whenGathered ) );
2081 artifactMetadatas.add( artifactMetadata );
2084 result = HFactory.createRangeSlicesQuery( keyspace, ss, ss, ss ) //
2085 .setColumnFamily( cassandraArchivaManager.getMetadataFacetFamilyName() ) //
2086 .setColumnNames( "facetId", "name", "value", "key", "projectVersion" ) //
2087 .setRowCount( Integer.MAX_VALUE ) //
2088 .addEqualsExpression( "repositoryName", repoId ) //
2089 .addEqualsExpression( "namespaceId", namespace ) //
2090 .addEqualsExpression( "projectId", projectId ) //
2091 .addEqualsExpression( "projectVersion", projectVersion ) //
2094 if ( result.get() == null || result.get().getCount() < 1 )
2096 return artifactMetadatas;
2099 final List<MetadataFacetModel> metadataFacetModels =
2100 new ArrayList<MetadataFacetModel>( result.get().getCount() );
2102 for ( Row<String, String, String> row : result.get() )
2104 ColumnSlice<String, String> columnSlice = row.getColumnSlice();
2105 MetadataFacetModel metadataFacetModel = new MetadataFacetModel();
2106 metadataFacetModel.setFacetId( getStringValue( columnSlice, "facetId" ) );
2107 metadataFacetModel.setName( getStringValue( columnSlice, "name" ) );
2108 metadataFacetModel.setValue( getStringValue( columnSlice, "value" ) );
2109 metadataFacetModel.setKey( getStringValue( columnSlice, "key" ) );
2110 metadataFacetModel.setProjectVersion( getStringValue( columnSlice, "projectVersion" ) );
2111 metadataFacetModels.add( metadataFacetModel );
2114 // rebuild MetadataFacet for artifacts
2116 for ( final ArtifactMetadata artifactMetadata : artifactMetadatas )
2118 Iterable<MetadataFacetModel> metadataFacetModelIterable =
2119 Iterables.filter( metadataFacetModels, new Predicate<MetadataFacetModel>()
2122 public boolean apply( MetadataFacetModel metadataFacetModel )
2124 if ( metadataFacetModel != null )
2126 return StringUtils.equals( artifactMetadata.getVersion(),
2127 metadataFacetModel.getProjectVersion() );
2132 Iterator<MetadataFacetModel> iterator = metadataFacetModelIterable.iterator();
2133 Map<String, List<MetadataFacetModel>> metadataFacetValuesPerFacetId =
2134 new HashMap<String, List<MetadataFacetModel>>();
2135 while ( iterator.hasNext() )
2137 MetadataFacetModel metadataFacetModel = iterator.next();
2138 List<MetadataFacetModel> values = metadataFacetValuesPerFacetId.get( metadataFacetModel.getName() );
2139 if ( values == null )
2141 values = new ArrayList<MetadataFacetModel>();
2142 metadataFacetValuesPerFacetId.put( metadataFacetModel.getFacetId(), values );
2144 values.add( metadataFacetModel );
2148 for ( Map.Entry<String, List<MetadataFacetModel>> entry : metadataFacetValuesPerFacetId.entrySet() )
2150 MetadataFacetFactory metadataFacetFactory = metadataFacetFactories.get( entry.getKey() );
2151 if ( metadataFacetFactory != null )
2153 List<MetadataFacetModel> facetModels = entry.getValue();
2154 if ( !facetModels.isEmpty() )
2156 MetadataFacet metadataFacet =
2157 metadataFacetFactory.createMetadataFacet( repoId, facetModels.get( 0 ).getName() );
2158 Map<String, String> props = new HashMap<String, String>( facetModels.size() );
2159 for ( MetadataFacetModel metadataFacetModel : facetModels )
2161 props.put( metadataFacetModel.getKey(), metadataFacetModel.getValue() );
2163 metadataFacet.fromProperties( props );
2164 artifactMetadata.addFacet( metadataFacet );
2172 return artifactMetadatas;
2178 logger.trace( "save" );
2183 throws MetadataRepositoryException
2185 logger.trace( "close" );
2189 public void revert()
2191 logger.warn( "CassandraMetadataRepository cannot revert" );
2195 public boolean canObtainAccess( Class<?> aClass )
2201 public <T> T obtainAccess( Class<T> aClass )
2202 throws MetadataRepositoryException
2204 throw new IllegalArgumentException(
2205 "Access using " + aClass + " is not supported on the cassandra metadata storage" );
2209 private static class ModelMapperHolder
2211 private static ModelMapper MODEL_MAPPER = new ModelMapper();
2214 protected ModelMapper getModelMapper()
2216 return ModelMapperHolder.MODEL_MAPPER;