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.FacetedMetadata;
44 import org.apache.archiva.metadata.model.IssueManagement;
45 import org.apache.archiva.metadata.model.MetadataFacet;
46 import org.apache.archiva.metadata.model.MetadataFacetFactory;
47 import org.apache.archiva.metadata.model.Organization;
48 import org.apache.archiva.metadata.model.ProjectMetadata;
49 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
50 import org.apache.archiva.metadata.model.ProjectVersionReference;
51 import org.apache.archiva.metadata.model.Scm;
52 import org.apache.archiva.metadata.repository.MetadataRepository;
53 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
54 import org.apache.archiva.metadata.repository.MetadataResolutionException;
55 import org.apache.archiva.metadata.repository.cassandra.model.ArtifactMetadataModel;
56 import org.apache.archiva.metadata.repository.cassandra.model.MetadataFacetModel;
57 import org.apache.archiva.metadata.repository.cassandra.model.Namespace;
58 import org.apache.archiva.metadata.repository.cassandra.model.Project;
59 import org.apache.archiva.metadata.repository.cassandra.model.ProjectVersionMetadataModel;
60 import org.apache.archiva.metadata.repository.cassandra.model.Repository;
61 import org.apache.commons.lang.StringUtils;
62 import org.modelmapper.ModelMapper;
63 import org.slf4j.Logger;
64 import org.slf4j.LoggerFactory;
66 import javax.persistence.PersistenceException;
67 import java.util.ArrayList;
68 import java.util.Collection;
69 import java.util.Collections;
70 import java.util.Date;
71 import java.util.HashMap;
72 import java.util.HashSet;
73 import java.util.Iterator;
74 import java.util.List;
78 import static org.apache.archiva.metadata.repository.cassandra.CassandraUtils.*;
81 * @author Olivier Lamy
84 public class CassandraMetadataRepository
85 implements MetadataRepository
88 private Logger logger = LoggerFactory.getLogger( getClass() );
90 private ArchivaConfiguration configuration;
92 private final Map<String, MetadataFacetFactory> metadataFacetFactories;
94 private final CassandraArchivaManager cassandraArchivaManager;
96 private final ColumnFamilyTemplate<String, String> projectVersionMetadataModelTemplate;
98 private final ColumnFamilyTemplate<String, String> projectTemplate;
100 private final ColumnFamilyTemplate<String, String> artifactMetadataTemplate;
102 private final ColumnFamilyTemplate<String, String> metadataFacetTemplate;
104 public CassandraMetadataRepository( Map<String, MetadataFacetFactory> metadataFacetFactories,
105 ArchivaConfiguration configuration,
106 CassandraArchivaManager cassandraArchivaManager )
108 this.metadataFacetFactories = metadataFacetFactories;
109 this.configuration = configuration;
110 this.cassandraArchivaManager = cassandraArchivaManager;
112 this.projectVersionMetadataModelTemplate =
113 new ThriftColumnFamilyTemplate<String, String>( cassandraArchivaManager.getKeyspace(), //
114 cassandraArchivaManager.getProjectVersionMetadataModelFamilyName(),
115 StringSerializer.get(), //
116 StringSerializer.get() );
118 this.projectTemplate = new ThriftColumnFamilyTemplate<String, String>( cassandraArchivaManager.getKeyspace(), //
119 cassandraArchivaManager.getProjectFamilyName(),
121 StringSerializer.get(), //
122 StringSerializer.get() );
124 this.artifactMetadataTemplate =
125 new ThriftColumnFamilyTemplate<String, String>( cassandraArchivaManager.getKeyspace(), //
126 cassandraArchivaManager.getArtifactMetadataModelFamilyName(),
128 StringSerializer.get(), //
129 StringSerializer.get() );
131 this.metadataFacetTemplate =
132 new ThriftColumnFamilyTemplate<String, String>( cassandraArchivaManager.getKeyspace(), //
133 cassandraArchivaManager.getMetadataFacetModelFamilyName(),
135 StringSerializer.get(), //
136 StringSerializer.get() );
141 * if the repository doesn't exist it will be created
143 * @param repositoryId
146 public Repository getOrCreateRepository( String repositoryId )
147 throws MetadataRepositoryException
149 String cf = cassandraArchivaManager.getRepositoryFamilyName();
150 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
151 QueryResult<OrderedRows<String, String, String>> result = HFactory //
152 .createRangeSlicesQuery( keyspace, StringSerializer.get(), StringSerializer.get(),
153 StringSerializer.get() ) //
154 .setColumnFamily( cf ) //
155 .setColumnNames( "repositoryName" ) //
156 .addEqualsExpression( "repositoryName", repositoryId ) //
159 if ( result.get().getCount() < 1 )
161 // we need to create the repository
162 Repository repository = new Repository( repositoryId );
166 MutationResult mutationResult = HFactory.createMutator( keyspace, StringSerializer.get() ) //
167 .addInsertion( repositoryId, cf,
168 CassandraUtils.column( "repositoryName", repository.getName() ) ) //
172 catch ( HInvalidRequestException e )
174 logger.error( e.getMessage(), e );
175 throw new MetadataRepositoryException( e.getMessage(), e );
180 return new Repository(
181 result.get().getList().get( 0 ).getColumnSlice().getColumnByName( "repositoryName" ).getValue() );
185 protected Repository getRepository( String repositoryId )
186 throws MetadataRepositoryException
188 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
189 QueryResult<OrderedRows<String, String, String>> result = HFactory //
190 .createRangeSlicesQuery( keyspace, StringSerializer.get(), StringSerializer.get(),
191 StringSerializer.get() ) //
192 .setColumnFamily( cassandraArchivaManager.getRepositoryFamilyName() ) //
193 .setColumnNames( "repositoryName" ) //
194 .addEqualsExpression( "repositoryName", repositoryId ) //
196 return ( result.get().getCount() > 0 ) ? new Repository( repositoryId ) : null;
200 public void updateNamespace( String repositoryId, String namespaceId )
201 throws MetadataRepositoryException
203 updateOrAddNamespace( repositoryId, namespaceId );
206 public Namespace updateOrAddNamespace( String repositoryId, String namespaceId )
207 throws MetadataRepositoryException
211 Repository repository = getOrCreateRepository( repositoryId );
213 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
216 new Namespace.KeyBuilder().withNamespace( namespaceId ).withRepositoryId( repositoryId ).build();
218 Namespace namespace = getNamespace( repositoryId, namespaceId );
219 if ( namespace == null )
221 String cf = cassandraArchivaManager.getNamespaceFamilyName();
222 namespace = new Namespace( namespaceId, repository );
223 HFactory.createMutator( keyspace, StringSerializer.get() )
225 .addInsertion( key, cf, CassandraUtils.column( "name", namespace.getName() ) ) //
226 .addInsertion( key, cf, CassandraUtils.column( "repositoryName", repository.getName() ) ) //
232 catch ( HInvalidRequestException e )
234 logger.error( e.getMessage(), e );
235 throw new MetadataRepositoryException( e.getMessage(), e );
239 protected Namespace getNamespace( String repositoryId, String namespaceId )
241 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
242 QueryResult<OrderedRows<String, String, String>> result = HFactory //
243 .createRangeSlicesQuery( keyspace, //
244 StringSerializer.get(), //
245 StringSerializer.get(), //
246 StringSerializer.get() ) //
247 .setColumnFamily( cassandraArchivaManager.getNamespaceFamilyName() ) //
248 .setColumnNames( "repositoryName", "name" ) //
249 .addEqualsExpression( "repositoryName", repositoryId ) //
250 .addEqualsExpression( "name", namespaceId ) //
252 if ( result.get().getCount() > 0 )
254 ColumnSlice<String, String> columnSlice = result.get().getList().get( 0 ).getColumnSlice();
255 return new Namespace( getStringValue( columnSlice, "name" ), //
256 new Repository( getStringValue( columnSlice, "repositoryName" ) ) );
264 public void removeNamespace( String repositoryId, String namespaceId )
265 throws MetadataRepositoryException
267 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
271 new Namespace.KeyBuilder().withNamespace( namespaceId ).withRepositoryId( repositoryId ).build();
273 HFactory.createMutator( cassandraArchivaManager.getKeyspace(), new StringSerializer() ) //
274 .addDeletion( key, cassandraArchivaManager.getNamespaceFamilyName() ) //
277 QueryResult<OrderedRows<String, String, String>> result = HFactory //
278 .createRangeSlicesQuery( keyspace, //
279 StringSerializer.get(), //
280 StringSerializer.get(), //
281 StringSerializer.get() ) //
282 .setColumnFamily( cassandraArchivaManager.getProjectFamilyName() ) //
283 .setColumnNames( "repositoryName" ) //
284 .addEqualsExpression( "repositoryName", repositoryId ) //
285 .addEqualsExpression( "namespaceId", namespaceId ) //
288 for ( Row<String, String, String> row : result.get() )
290 this.projectTemplate.deleteRow( row.getKey() );
294 .createRangeSlicesQuery( keyspace, //
295 StringSerializer.get(), //
296 StringSerializer.get(), //
297 StringSerializer.get() ) //
298 .setColumnFamily( cassandraArchivaManager.getProjectVersionMetadataModelFamilyName() ) //
299 .setColumnNames( "repositoryName" ) //
300 .addEqualsExpression( "repositoryName", repositoryId ) //
301 .addEqualsExpression( "namespaceId", namespaceId ) //
304 for ( Row<String, String, String> row : result.get() )
306 this.projectVersionMetadataModelTemplate.deleteRow( row.getKey() );
310 .createRangeSlicesQuery( keyspace, //
311 StringSerializer.get(), //
312 StringSerializer.get(), //
313 StringSerializer.get() ) //
314 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataModelFamilyName() ) //
315 .setColumnNames( "repositoryName" ) //
316 .addEqualsExpression( "repositoryName", repositoryId ) //
317 .addEqualsExpression( "namespaceId", namespaceId ) //
320 for ( Row<String, String, String> row : result.get() )
322 this.artifactMetadataTemplate.deleteRow( row.getKey() );
326 .createRangeSlicesQuery( keyspace, //
327 StringSerializer.get(), //
328 StringSerializer.get(), //
329 StringSerializer.get() ) //
330 .setColumnFamily( cassandraArchivaManager.getMetadataFacetModelFamilyName() ) //
331 .setColumnNames( "repositoryName" ) //
332 .addEqualsExpression( "repositoryName", repositoryId ) //
333 .addEqualsExpression( "namespaceId", namespaceId ) //
336 for ( Row<String, String, String> row : result.get() )
338 this.metadataFacetTemplate.deleteRow( row.getKey() );
342 catch ( HInvalidRequestException e )
344 logger.error( e.getMessage(), e );
345 throw new MetadataRepositoryException( e.getMessage(), e );
351 public void removeRepository( final String repositoryId )
352 throws MetadataRepositoryException
355 StringSerializer ss = StringSerializer.get();
357 // TODO use cql queries to delete all
358 List<String> namespacesKey = new ArrayList<String>();
360 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
361 QueryResult<OrderedRows<String, String, String>> result = HFactory //
362 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
363 .setColumnFamily( cassandraArchivaManager.getNamespaceFamilyName() ) //
364 .setColumnNames( "repositoryName" ) //
365 .addEqualsExpression( "repositoryName", repositoryId ) //
368 for ( Row<String, String, String> row : result.get().getList() )
370 namespacesKey.add( row.getKey() );
373 HFactory.createMutator( cassandraArchivaManager.getKeyspace(), ss ) //
374 .addDeletion( namespacesKey, cassandraArchivaManager.getNamespaceFamilyName() ) //
377 //delete repositoryId
378 HFactory.createMutator( cassandraArchivaManager.getKeyspace(), ss ) //
379 .addDeletion( repositoryId, cassandraArchivaManager.getRepositoryFamilyName() ) //
383 .createRangeSlicesQuery( keyspace, //
384 StringSerializer.get(), //
385 StringSerializer.get(), //
386 StringSerializer.get() ) //
387 .setColumnFamily( cassandraArchivaManager.getProjectFamilyName() ) //
388 .setColumnNames( "repositoryName" ) //
389 .addEqualsExpression( "repositoryName", repositoryId ) //
392 for ( Row<String, String, String> row : result.get() )
394 this.projectTemplate.deleteRow( row.getKey() );
398 .createRangeSlicesQuery( keyspace, //
399 StringSerializer.get(), //
400 StringSerializer.get(), //
401 StringSerializer.get() ) //
402 .setColumnFamily( cassandraArchivaManager.getProjectVersionMetadataModelFamilyName() ) //
403 .setColumnNames( "repositoryName" ) //
404 .addEqualsExpression( "repositoryName", repositoryId ) //
407 for ( Row<String, String, String> row : result.get() )
409 this.projectVersionMetadataModelTemplate.deleteRow( row.getKey() );
413 .createRangeSlicesQuery( keyspace, //
414 StringSerializer.get(), //
415 StringSerializer.get(), //
416 StringSerializer.get() ) //
417 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataModelFamilyName() ) //
418 .setColumnNames( "repositoryName" ) //
419 .addEqualsExpression( "repositoryName", repositoryId ) //
422 for ( Row<String, String, String> row : result.get() )
424 this.artifactMetadataTemplate.deleteRow( row.getKey() );
428 .createRangeSlicesQuery( keyspace, //
429 StringSerializer.get(), //
430 StringSerializer.get(), //
431 StringSerializer.get() ) //
432 .setColumnFamily( cassandraArchivaManager.getMetadataFacetModelFamilyName() ) //
433 .setColumnNames( "repositoryName" ) //
434 .addEqualsExpression( "repositoryName", repositoryId ) //
437 for ( Row<String, String, String> row : result.get() )
439 this.metadataFacetTemplate.deleteRow( row.getKey() );
446 public Collection<String> getRepositories()
447 throws MetadataRepositoryException
451 logger.debug( "getRepositories" );
453 final QueryResult<OrderedRows<String, String, String>> cResult = //
454 HFactory.createRangeSlicesQuery( cassandraArchivaManager.getKeyspace(), //
455 StringSerializer.get(), //
456 StringSerializer.get(), //
457 StringSerializer.get() ) //
458 .setColumnFamily( cassandraArchivaManager.getRepositoryFamilyName() ) //
459 .setColumnNames( "repositoryName" ) //
460 .setRange( null, null, false, Integer.MAX_VALUE ) //
463 List<String> repoIds = new ArrayList<String>( cResult.get().getCount() );
465 for ( Row<String, String, String> row : cResult.get() )
467 repoIds.add( getStringValue( row.getColumnSlice(), "repositoryName" ) );
472 catch ( PersistenceException e )
474 throw new MetadataRepositoryException( e.getMessage(), e );
481 public Collection<String> getRootNamespaces( final String repoId )
482 throws MetadataResolutionException
484 StringSerializer ss = StringSerializer.get();
485 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
486 QueryResult<OrderedRows<String, String, String>> result = HFactory //
487 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
488 .setColumnFamily( cassandraArchivaManager.getNamespaceFamilyName() ) //
489 .setColumnNames( "name" ) //
490 .addEqualsExpression( "repositoryName", repoId ) //
493 Set<String> namespaces = new HashSet<String>( result.get().getCount() );
495 for ( Row<String, String, String> row : result.get() )
497 namespaces.add( StringUtils.substringBefore( getStringValue( row.getColumnSlice(), "name" ), "." ) );
505 public Collection<String> getNamespaces( final String repoId, final String namespaceId )
506 throws MetadataResolutionException
508 StringSerializer ss = StringSerializer.get();
509 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
510 QueryResult<OrderedRows<String, String, String>> result = HFactory //
511 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
512 .setColumnFamily( cassandraArchivaManager.getNamespaceFamilyName() ) //
513 .setColumnNames( "name" ) //
514 .addEqualsExpression( "repositoryName", repoId ) //
517 List<String> namespaces = new ArrayList<String>( result.get().getCount() );
519 for ( Row<String, String, String> row : result.get() )
521 String currentNamespace = getStringValue( row.getColumnSlice(), "name" );
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 );
542 public List<String> getNamespaces( final String repoId )
543 throws MetadataResolutionException
545 StringSerializer ss = StringSerializer.get();
546 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
547 QueryResult<OrderedRows<String, String, String>> result = HFactory //
548 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
549 .setColumnFamily( cassandraArchivaManager.getNamespaceFamilyName() ) //
550 .setColumnNames( "name" ) //
551 .addEqualsExpression( "repositoryName", repoId ) //
554 List<String> namespaces = new ArrayList<String>( result.get().getCount() );
556 for ( Row<String, String, String> row : result.get() )
558 namespaces.add( getStringValue( row.getColumnSlice(), "name" ) );
566 public void updateProject( String repositoryId, ProjectMetadata projectMetadata )
567 throws MetadataRepositoryException
569 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
570 StringSerializer ss = StringSerializer.get();
572 QueryResult<OrderedRows<String, String, String>> result = HFactory //
573 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
574 .setColumnFamily( cassandraArchivaManager.getProjectFamilyName() ) //
575 .setColumnNames( "projectId" ) //
576 .addEqualsExpression( "repositoryName", repositoryId ) //
577 .addEqualsExpression( "namespaceId", projectMetadata.getNamespace() ) //
578 .addEqualsExpression( "projectId", projectMetadata.getId() ) //
581 // project exists ? if yes return nothing to update here
582 if ( result.get().getCount() > 0 )
588 Namespace namespace = updateOrAddNamespace( repositoryId, projectMetadata.getNamespace() );
591 new Project.KeyBuilder().withProjectId( projectMetadata.getId() ).withNamespace( namespace ).build();
593 String cf = cassandraArchivaManager.getProjectFamilyName();
594 projectTemplate.createMutator()
596 .addInsertion( key, cf, CassandraUtils.column( "projectId", projectMetadata.getId() ) ) //
597 .addInsertion( key, cf, CassandraUtils.column( "repositoryName", repositoryId ) ) //
598 .addInsertion( key, cf, CassandraUtils.column( "namespaceId", projectMetadata.getNamespace() ) )//
604 public Collection<String> getProjects( final String repoId, final String namespace )
605 throws MetadataResolutionException
608 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
609 StringSerializer ss = StringSerializer.get();
610 QueryResult<OrderedRows<String, String, String>> result = HFactory //
611 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
612 .setColumnFamily( cassandraArchivaManager.getProjectFamilyName() ) //
613 .setColumnNames( "projectId" ) //
614 .addEqualsExpression( "repositoryName", repoId ) //
615 .addEqualsExpression( "namespaceId", namespace ) //
618 final Set<String> projects = new HashSet<String>( result.get().getCount() );
620 for ( Row<String, String, String> row : result.get() )
622 projects.add( getStringValue( row.getColumnSlice(), "projectId" ) );
629 public void removeProject( final String repositoryId, final String namespaceId, final String projectId )
630 throws MetadataRepositoryException
632 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
633 StringSerializer ss = StringSerializer.get();
634 String key = new Project.KeyBuilder() //
635 .withProjectId( projectId ) //
636 .withNamespace( new Namespace( namespaceId, new Repository( repositoryId ) ) ) //
639 this.projectTemplate.deleteRow( key );
641 QueryResult<OrderedRows<String, String, String>> result = HFactory //
642 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
643 .setColumnFamily( cassandraArchivaManager.getProjectVersionMetadataModelFamilyName() ) //
644 .setColumnNames( "id" ) //
645 .addEqualsExpression( "repositoryName", repositoryId ) //
646 .addEqualsExpression( "namespaceId", namespaceId ) //
647 .addEqualsExpression( "projectId", projectId ) //
650 for ( Row<String, String, String> row : result.get() )
652 this.projectVersionMetadataModelTemplate.deleteRow( row.getKey() );
656 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
657 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataModelFamilyName() ) //
658 .setColumnNames( "projectId" ) //
659 .addEqualsExpression( "repositoryName", repositoryId ) //
660 .addEqualsExpression( "namespaceId", namespaceId ) //
661 .addEqualsExpression( "projectId", projectId ) //
664 for ( Row<String, String, String> row : result.get() )
666 this.artifactMetadataTemplate.deleteRow( row.getKey() );
671 public Collection<String> getProjectVersions( final String repoId, final String namespace, final String projectId )
672 throws MetadataResolutionException
675 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
676 StringSerializer ss = StringSerializer.get();
677 QueryResult<OrderedRows<String, String, String>> result = HFactory //
678 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
679 .setColumnFamily( cassandraArchivaManager.getProjectVersionMetadataModelFamilyName() ) //
680 .setColumnNames( "projectVersion" ) //
681 .addEqualsExpression( "repositoryName", repoId ) //
682 .addEqualsExpression( "namespaceId", namespace ) //
683 .addEqualsExpression( "projectId", projectId ) //
686 int count = result.get().getCount();
690 return Collections.emptyList();
693 Set<String> versions = new HashSet<String>( count );
695 for ( Row<String, String, String> orderedRows : result.get() )
697 versions.add( getStringValue( orderedRows.getColumnSlice(), "projectVersion" ) );
705 public ProjectMetadata getProject( final String repoId, final String namespace, final String id )
706 throws MetadataResolutionException
709 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
710 StringSerializer ss = StringSerializer.get();
711 QueryResult<OrderedRows<String, String, String>> result = HFactory //
712 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
713 .setColumnFamily( cassandraArchivaManager.getProjectFamilyName() ) //
714 .setColumnNames( "projectId" ) //
715 .addEqualsExpression( "repositoryName", repoId ) //
716 .addEqualsExpression( "namespaceId", namespace ) //
717 .addEqualsExpression( "projectId", id ) //
720 int count = result.get().getCount();
727 ProjectMetadata projectMetadata = new ProjectMetadata();
728 projectMetadata.setId( id );
729 projectMetadata.setNamespace( namespace );
731 logger.debug( "getProject repoId: {}, namespace: {}, projectId: {} -> {}", repoId, namespace, id,
734 return projectMetadata;
737 protected ProjectVersionMetadataModel mapProjectVersionMetadataModel( ColumnSlice<String, String> columnSlice )
739 ProjectVersionMetadataModel projectVersionMetadataModel = new ProjectVersionMetadataModel();
740 projectVersionMetadataModel.setId( getStringValue( columnSlice, "id" ) );
741 projectVersionMetadataModel.setDescription( getStringValue( columnSlice, "description" ) );
742 projectVersionMetadataModel.setName( getStringValue( columnSlice, "name" ) );
743 projectVersionMetadataModel.setNamespace( new Namespace( getStringValue( columnSlice, "namespaceId" ), //
745 getStringValue( columnSlice, "repositoryName" ) )
748 projectVersionMetadataModel.setIncomplete(
749 Boolean.parseBoolean( getStringValue( columnSlice, "incomplete" ) ) );
750 projectVersionMetadataModel.setProjectId( getStringValue( columnSlice, "projectId" ) );
751 projectVersionMetadataModel.setUrl( getStringValue( columnSlice, "url" ) );
752 return projectVersionMetadataModel;
757 public void updateProjectVersion( String repositoryId, String namespaceId, String projectId,
758 ProjectVersionMetadata versionMetadata )
759 throws MetadataRepositoryException
763 Namespace namespace = getNamespace( repositoryId, namespaceId );
765 if ( namespace == null )
767 updateOrAddNamespace( repositoryId, namespaceId );
770 if ( getProject( repositoryId, namespaceId, projectId ) == null )
772 ProjectMetadata projectMetadata = new ProjectMetadata();
773 projectMetadata.setNamespace( namespaceId );
774 projectMetadata.setId( projectId );
775 updateProject( repositoryId, projectMetadata );
779 catch ( MetadataResolutionException e )
781 throw new MetadataRepositoryException( e.getMessage(), e );
784 StringSerializer ss = StringSerializer.get();
786 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
787 QueryResult<OrderedRows<String, String, String>> result = HFactory //
788 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
789 .setColumnFamily( cassandraArchivaManager.getProjectVersionMetadataModelFamilyName() ) //
790 .setColumnNames( "projectVersion" ) //
791 .addEqualsExpression( "repositoryName", repositoryId ) //
792 .addEqualsExpression( "namespaceId", namespaceId ) //
793 .addEqualsExpression( "projectId", projectId ) //
794 .addEqualsExpression( "projectVersion", versionMetadata.getId() ) //
797 ProjectVersionMetadataModel projectVersionMetadataModel = null;
798 boolean creation = true;
799 if ( result.get().getCount() > 0 )
801 projectVersionMetadataModel =
802 mapProjectVersionMetadataModel( result.get().getList().get( 0 ).getColumnSlice() );
807 projectVersionMetadataModel = getModelMapper().map( versionMetadata, ProjectVersionMetadataModel.class );
810 projectVersionMetadataModel.setProjectId( projectId );
811 projectVersionMetadataModel.setNamespace( new Namespace( namespaceId, new Repository( repositoryId ) ) );
813 projectVersionMetadataModel.setCiManagement( versionMetadata.getCiManagement() );
814 projectVersionMetadataModel.setIssueManagement( versionMetadata.getIssueManagement() );
815 projectVersionMetadataModel.setOrganization( versionMetadata.getOrganization() );
816 projectVersionMetadataModel.setScm( versionMetadata.getScm() );
818 projectVersionMetadataModel.setMailingLists( versionMetadata.getMailingLists() );
819 projectVersionMetadataModel.setDependencies( versionMetadata.getDependencies() );
820 projectVersionMetadataModel.setLicenses( versionMetadata.getLicenses() );
822 // we don't test of repository and namespace really exist !
823 String key = new ProjectVersionMetadataModel.KeyBuilder().withRepository( repositoryId ).withNamespace(
824 namespaceId ).withProjectId( projectId ).withId( versionMetadata.getId() ).build();
826 // FIXME nested objects to store!!!
829 String cf = cassandraArchivaManager.getProjectVersionMetadataModelFamilyName();
830 Mutator<String> mutator = projectVersionMetadataModelTemplate.createMutator()
832 .addInsertion( key, cf, column( "projectId", projectId ) ) //
833 .addInsertion( key, cf, column( "repositoryName", repositoryId ) ) //
834 .addInsertion( key, cf, column( "namespaceId", namespaceId ) )//
835 .addInsertion( key, cf, column( "projectVersion", versionMetadata.getVersion() ) ); //
837 addInsertion( mutator, key, cf, "description", versionMetadata.getDescription() );
839 addInsertion( mutator, key, cf, "name", versionMetadata.getName() );
841 addInsertion( mutator, key, cf, "incomplete", Boolean.toString( versionMetadata.isIncomplete() ) );
843 addInsertion( mutator, key, cf, "url", versionMetadata.getUrl() );
845 CiManagement ci = versionMetadata.getCiManagement();
848 addInsertion( mutator, key, cf, "ciManagement.system", ci.getSystem() );
849 addInsertion( mutator, key, cf, "ciManagement.url", ci.getUrl() );
854 IssueManagement issueManagement = versionMetadata.getIssueManagement();
856 if ( issueManagement != null )
858 addInsertion( mutator, key, cf, "issueManagement.system", issueManagement.getSystem() );
859 addInsertion( mutator, key, cf, "issueManagement.url", issueManagement.getUrl() );
864 Organization organization = versionMetadata.getOrganization();
865 if ( organization != null )
867 addInsertion( mutator, key, cf, "organization.name", organization.getName() );
868 addInsertion( mutator, key, cf, "organization.url", organization.getUrl() );
873 Scm scm = versionMetadata.getScm();
876 addInsertion( mutator, key, cf, "scm.url", scm.getUrl() );
877 addInsertion( mutator, key, cf, "scm.connection", scm.getConnection() );
878 addInsertion( mutator, key, cf, "scm.developerConnection", scm.getDeveloperConnection() );
882 MutationResult mutationResult = mutator.execute();
886 ColumnFamilyUpdater<String, String> updater = projectVersionMetadataModelTemplate.createUpdater( key );
887 updater.setString( "projectId", projectId );
888 updater.setString( "repositoryName", repositoryId );
889 updater.setString( "namespaceId", namespaceId );
890 updater.setString( "projectVersion", versionMetadata.getVersion() );
891 if ( StringUtils.isNotEmpty( versionMetadata.getDescription() ) )
893 updater.setString( "description", versionMetadata.getDescription() );
895 if ( StringUtils.isNotEmpty( versionMetadata.getName() ) )
897 updater.setString( "name", versionMetadata.getName() );
899 updater.setString( "incomplete", Boolean.toString( versionMetadata.isIncomplete() ) );
900 if ( StringUtils.isNotEmpty( versionMetadata.getUrl() ) )
902 updater.setString( "url", versionMetadata.getUrl() );
906 CiManagement ci = versionMetadata.getCiManagement();
909 updater.setString( "ciManagement.system", ci.getSystem() );
910 updater.setString( "ciManagement.url", ci.getUrl() );
914 IssueManagement issueManagement = versionMetadata.getIssueManagement();
915 if ( issueManagement != null )
917 updater.setString( "issueManagement.system", issueManagement.getSystem() );
918 updater.setString( "issueManagement.url", issueManagement.getUrl() );
922 Organization organization = versionMetadata.getOrganization();
923 if ( organization != null )
925 updater.setString( "organization.name", organization.getName() );
926 updater.setString( "organization.url", organization.getUrl() );
930 Scm scm = versionMetadata.getScm();
933 updater.setString( "scm.url", scm.getUrl() );
934 updater.setString( "scm.connection", scm.getConnection() );
935 updater.setString( "scm.developerConnection", scm.getDeveloperConnection() );
939 projectVersionMetadataModelTemplate.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
959 String key = new ProjectVersionMetadataModel.KeyBuilder().withRepository( repoId ).withNamespace(
960 namespace ).withProjectId( projectId ).withId( projectVersion ).build();
962 ColumnFamilyResult<String, String> columnFamilyResult =
963 this.projectVersionMetadataModelTemplate.queryColumns( key );
964 if ( !columnFamilyResult.hasResults() )
969 ProjectVersionMetadata projectVersionMetadata = new ProjectVersionMetadata();
970 projectVersionMetadata.setId( columnFamilyResult.getString( "projectVersion" ) );
971 projectVersionMetadata.setDescription( columnFamilyResult.getString( "description" ) );
972 projectVersionMetadata.setName( columnFamilyResult.getString( "name" ) );
974 projectVersionMetadata.setIncomplete( Boolean.parseBoolean( columnFamilyResult.getString( "incomplete" ) ) );
976 projectVersionMetadata.setUrl( columnFamilyResult.getString( "url" ) );
978 String ciUrl = columnFamilyResult.getString( "ciManagement.url" );
979 String ciSystem = columnFamilyResult.getString( "ciManagement.system" );
981 if ( StringUtils.isNotEmpty( ciSystem ) || StringUtils.isNotEmpty( ciUrl ) )
983 projectVersionMetadata.setCiManagement( new CiManagement( ciSystem, ciUrl ) );
987 String issueUrl = columnFamilyResult.getString( "issueManagement.url" );
988 String issueSystem = columnFamilyResult.getString( "issueManagement.system" );
989 if ( StringUtils.isNotEmpty( issueSystem ) || StringUtils.isNotEmpty( issueUrl ) )
991 projectVersionMetadata.setIssueManagement( new IssueManagement( issueSystem, issueUrl ) );
995 String organizationUrl = columnFamilyResult.getString( "organization.url" );
996 String organizationName = columnFamilyResult.getString( "organization.name" );
997 if ( StringUtils.isNotEmpty( organizationUrl ) || StringUtils.isNotEmpty( organizationName ) )
999 projectVersionMetadata.setOrganization( new Organization( organizationName, organizationUrl ) );
1003 String devConn = columnFamilyResult.getString( "scm.developerConnection" );
1004 String conn = columnFamilyResult.getString( "scm.connection" );
1005 String url = columnFamilyResult.getString( "scm.url" );
1006 if ( StringUtils.isNotEmpty( devConn ) || StringUtils.isNotEmpty( conn ) || StringUtils.isNotEmpty( url ) )
1008 projectVersionMetadata.setScm( new Scm( conn, devConn, url ) );
1011 // FIXME complete collections !!
1014 StringSerializer ss = StringSerializer.get();
1016 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
1017 QueryResult<OrderedRows<String, String, String>> result = HFactory //
1018 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1019 .setColumnFamily( cassandraArchivaManager.getMetadataFacetModelFamilyName() ) //
1020 .setColumnNames( "facetId", "key", "value", "name" ) //
1021 .addEqualsExpression( "repositoryName", repoId ) //
1022 .addEqualsExpression( "namespaceId", namespace ) //
1023 .addEqualsExpression( "projectId", projectId ) //
1024 .addEqualsExpression( "projectVersion", projectVersion ) //
1027 Map<String, Map<String, String>> metadataFacetsPerFacetIds = new HashMap<String, Map<String, String>>();
1029 for ( Row<String, String, String> row : result.get() )
1031 ColumnSlice<String, String> columnSlice = row.getColumnSlice();
1032 String facetId = getStringValue( columnSlice, "facetId" );
1033 Map<String, String> metaValues = metadataFacetsPerFacetIds.get( facetId );
1034 if ( metaValues == null )
1036 metaValues = new HashMap<String, String>();
1037 metadataFacetsPerFacetIds.put( facetId, metaValues );
1039 metaValues.put( getStringValue( columnSlice, "key" ), getStringValue( columnSlice, "value" ) );
1042 if ( !metadataFacetsPerFacetIds.isEmpty() )
1044 for ( Map.Entry<String, Map<String, String>> entry : metadataFacetsPerFacetIds.entrySet() )
1046 MetadataFacetFactory metadataFacetFactory = metadataFacetFactories.get( entry.getKey() );
1047 if ( metadataFacetFactory != null )
1049 MetadataFacet metadataFacet = metadataFacetFactory.createMetadataFacet( repoId, entry.getKey() );
1050 metadataFacet.fromProperties( entry.getValue() );
1051 projectVersionMetadata.addFacet( metadataFacet );
1056 return projectVersionMetadata;
1061 public void updateArtifact( String repositoryId, String namespaceId, String projectId, String projectVersion,
1062 ArtifactMetadata artifactMeta )
1063 throws MetadataRepositoryException
1066 Namespace namespace = getNamespace( repositoryId, namespaceId );
1067 if ( namespace == null )
1069 namespace = updateOrAddNamespace( repositoryId, namespaceId );
1072 ProjectMetadata projectMetadata = new ProjectMetadata();
1073 projectMetadata.setId( projectId );
1074 projectMetadata.setNamespace( namespaceId );
1075 updateProject( repositoryId, projectMetadata );
1077 String key = new ArtifactMetadataModel.KeyBuilder().withNamespace( namespace ).withProject( projectId ).withId(
1078 artifactMeta.getId() ).withProjectVersion( projectVersion ).build();
1082 boolean exists = this.artifactMetadataTemplate.isColumnsExist( key );
1087 ColumnFamilyUpdater<String, String> updater = this.artifactMetadataTemplate.createUpdater( key );
1088 updater.setLong( "fileLastModified", artifactMeta.getFileLastModified().getTime() );
1089 updater.setLong( "whenGathered", artifactMeta.getWhenGathered().getTime() );
1090 updater.setLong( "size", artifactMeta.getSize() );
1091 updater.setString( "md5", artifactMeta.getMd5() );
1092 updater.setString( "sha1", artifactMeta.getSha1() );
1093 updater.setString( "version", artifactMeta.getVersion() );
1094 this.artifactMetadataTemplate.update( updater );
1098 String cf = this.cassandraArchivaManager.getArtifactMetadataModelFamilyName();
1100 this.artifactMetadataTemplate.createMutator() //
1101 .addInsertion( key, cf, column( "id", artifactMeta.getId() ) )//
1102 .addInsertion( key, cf, column( "repositoryName", repositoryId ) ) //
1103 .addInsertion( key, cf, column( "namespaceId", namespaceId ) ) //
1104 .addInsertion( key, cf, column( "project", artifactMeta.getProject() ) ) //
1105 .addInsertion( key, cf, column( "projectVersion", projectVersion ) ) //
1106 .addInsertion( key, cf, column( "version", artifactMeta.getVersion() ) ) //
1107 .addInsertion( key, cf, column( "fileLastModified", artifactMeta.getFileLastModified().getTime() ) ) //
1108 .addInsertion( key, cf, column( "size", artifactMeta.getSize() ) ) //
1109 .addInsertion( key, cf, column( "md5", artifactMeta.getMd5() ) ) //
1110 .addInsertion( key, cf, column( "sha1", artifactMeta.getSha1() ) ) //
1111 .addInsertion( key, cf, column( "whenGathered", artifactMeta.getWhenGathered().getTime() ) )//
1115 key = new ProjectVersionMetadataModel.KeyBuilder().withRepository( repositoryId ).withNamespace(
1116 namespace ).withProjectId( projectId ).withId( projectVersion ).build();
1118 exists = this.projectVersionMetadataModelTemplate.isColumnsExist( key );
1122 ProjectVersionMetadataModel projectVersionMetadataModel = new ProjectVersionMetadataModel();
1123 projectVersionMetadataModel.setProjectId( projectId );
1124 projectVersionMetadataModel.setId( projectVersion );
1125 projectVersionMetadataModel.setNamespace( namespace );
1127 String cf = this.cassandraArchivaManager.getProjectVersionMetadataModelFamilyName();
1129 projectVersionMetadataModelTemplate.createMutator() //
1130 .addInsertion( key, cf, column( "namespaceId", namespace.getName() ) ) //
1131 .addInsertion( key, cf, column( "repositoryName", repositoryId ) ) //
1132 .addInsertion( key, cf, column( "projectVersion", projectVersion ) ) //
1133 .addInsertion( key, cf, column( "projectId", projectId ) ) //
1138 ArtifactMetadataModel artifactMetadataModel = new ArtifactMetadataModel();
1140 artifactMetadataModel.setRepositoryId( repositoryId );
1141 artifactMetadataModel.setNamespace( namespaceId );
1142 artifactMetadataModel.setProject( projectId );
1143 artifactMetadataModel.setProjectVersion( projectVersion );
1144 artifactMetadataModel.setVersion( artifactMeta.getVersion() );
1145 artifactMetadataModel.setFileLastModified( artifactMeta.getFileLastModified() == null
1146 ? new Date().getTime()
1147 : artifactMeta.getFileLastModified().getTime() );
1150 updateFacets( artifactMeta, artifactMetadataModel );
1155 public Collection<String> getArtifactVersions( final String repoId, final String namespace, final String projectId,
1156 final String projectVersion )
1157 throws MetadataResolutionException
1159 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
1160 StringSerializer ss = StringSerializer.get();
1161 QueryResult<OrderedRows<String, String, String>> result = HFactory //
1162 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1163 .setColumnFamily( cassandraArchivaManager.getProjectVersionMetadataModelFamilyName() ) //
1164 .setColumnNames( "projectVersion" ) //
1165 .addEqualsExpression( "repositoryName", repoId ) //
1166 .addEqualsExpression( "namespaceId", namespace ) //
1167 .addEqualsExpression( "projectId", projectId ) //
1168 .addEqualsExpression( "projectVersion", projectVersion ) //
1171 final Set<String> versions = new HashSet<String>();
1173 for ( Row<String, String, String> row : result.get() )
1175 versions.add( getStringValue( row.getColumnSlice(), "projectVersion" ) );
1183 * iterate over available facets to remove/add from the artifactMetadata
1185 * @param facetedMetadata
1186 * @param artifactMetadataModel only use for the key
1188 private void updateFacets( final FacetedMetadata facetedMetadata,
1189 final ArtifactMetadataModel artifactMetadataModel )
1191 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
1192 StringSerializer ss = StringSerializer.get();
1193 String cf = cassandraArchivaManager.getMetadataFacetModelFamilyName();
1195 for ( final String facetId : metadataFacetFactories.keySet() )
1197 MetadataFacet metadataFacet = facetedMetadata.getFacet( facetId );
1198 if ( metadataFacet == null )
1204 QueryResult<OrderedRows<String, String, String>> result =
1205 HFactory.createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1206 .setColumnFamily( cf ) //
1207 .setColumnNames( "repositoryName" ) //
1208 .addEqualsExpression( "repositoryName", artifactMetadataModel.getRepositoryId() ) //
1209 .addEqualsExpression( "namespaceId", artifactMetadataModel.getNamespace() ) //
1210 .addEqualsExpression( "projectId", artifactMetadataModel.getProject() ) //
1211 .addEqualsExpression( "projectVersion",
1212 artifactMetadataModel.getProjectVersion() ).addEqualsExpression( "facetId",
1216 for ( Row<String, String, String> row : result.get().getList() )
1218 this.metadataFacetTemplate.deleteRow( row.getKey() );
1221 Map<String, String> properties = metadataFacet.toProperties();
1223 for ( Map.Entry<String, String> entry : properties.entrySet() )
1225 String key = new MetadataFacetModel.KeyBuilder().withKey( entry.getKey() ).withArtifactMetadataModel(
1226 artifactMetadataModel ).withFacetId( facetId ).withName( metadataFacet.getName() ).build();
1227 metadataFacetTemplate.createMutator() //
1228 .addInsertion( key, cf, column( "repositoryName", artifactMetadataModel.getRepositoryId() ) ) //
1229 .addInsertion( key, cf, column( "namespaceId", artifactMetadataModel.getNamespace() ) ) //
1230 .addInsertion( key, cf, column( "projectId", artifactMetadataModel.getProject() ) ) //
1231 .addInsertion( key, cf, column( "projectVersion", artifactMetadataModel.getProjectVersion() ) ) //
1232 .addInsertion( key, cf, column( "facetId", facetId ) ) //
1233 .addInsertion( key, cf, column( "key", entry.getKey() ) ) //
1234 .addInsertion( key, cf, column( "value", entry.getValue() ) ) //
1235 .addInsertion( key, cf, column( "name", metadataFacet.getName() ) ) //
1243 public List<String> getMetadataFacets( final String repositoryId, final String facetId )
1244 throws MetadataRepositoryException
1247 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
1248 StringSerializer ss = StringSerializer.get();
1249 QueryResult<OrderedRows<String, String, String>> result = HFactory //
1250 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1251 .setColumnFamily( cassandraArchivaManager.getMetadataFacetModelFamilyName() ) //
1252 .setColumnNames( "name" ) //
1253 .addEqualsExpression( "repositoryName", repositoryId ) //
1254 .addEqualsExpression( "facetId", facetId ) //
1257 final List<String> facets = new ArrayList<String>();
1259 for ( Row<String, String, String> row : result.get() )
1261 facets.add( getStringValue( row.getColumnSlice(), "name" ) );
1267 public boolean hasMetadataFacet( String repositoryId, String facetId )
1268 throws MetadataRepositoryException
1270 return !getMetadataFacets( repositoryId, facetId ).isEmpty();
1274 public MetadataFacet getMetadataFacet( final String repositoryId, final String facetId, final String name )
1275 throws MetadataRepositoryException
1278 MetadataFacetFactory metadataFacetFactory = metadataFacetFactories.get( facetId );
1279 if ( metadataFacetFactory == null )
1284 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
1285 StringSerializer ss = StringSerializer.get();
1286 QueryResult<OrderedRows<String, String, String>> result = HFactory //
1287 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1288 .setColumnFamily( cassandraArchivaManager.getMetadataFacetModelFamilyName() ) //
1289 .setColumnNames( "key", "value" ) //
1290 .addEqualsExpression( "repositoryName", repositoryId ) //
1291 .addEqualsExpression( "facetId", facetId ) //
1292 .addEqualsExpression( "name", name ) //
1295 MetadataFacet metadataFacet = metadataFacetFactory.createMetadataFacet( repositoryId, name );
1296 Map<String, String> map = new HashMap<String, String>( result.get().getCount() );
1297 for ( Row<String, String, String> row : result.get() )
1299 ColumnSlice<String, String> columnSlice = row.getColumnSlice();
1300 map.put( getStringValue( columnSlice, "key" ), getStringValue( columnSlice, "value" ) );
1302 metadataFacet.fromProperties( map );
1303 return metadataFacet;
1307 public void addMetadataFacet( String repositoryId, MetadataFacet metadataFacet )
1308 throws MetadataRepositoryException
1311 if ( metadataFacet == null )
1316 if ( metadataFacet.toProperties().isEmpty() )
1318 String key = new MetadataFacetModel.KeyBuilder().withRepositoryId( repositoryId ).withFacetId(
1319 metadataFacet.getFacetId() ).withName( metadataFacet.getName() ).build();
1321 boolean exists = this.metadataFacetTemplate.isColumnsExist( key );
1325 ColumnFamilyUpdater<String, String> updater = this.metadataFacetTemplate.createUpdater( key );
1326 updater.setString( "facetId", metadataFacet.getFacetId() );
1327 updater.setString( "name", metadataFacet.getName() );
1328 this.metadataFacetTemplate.update( updater );
1332 String cf = this.cassandraArchivaManager.getMetadataFacetModelFamilyName();
1333 this.metadataFacetTemplate.createMutator() //
1334 .addInsertion( key, cf, column( "repositoryName", repositoryId ) ) //
1335 .addInsertion( key, cf, column( "facetId", metadataFacet.getFacetId() ) ) //
1336 .addInsertion( key, cf, column( "name", metadataFacet.getName() ) ) //
1343 for ( Map.Entry<String, String> entry : metadataFacet.toProperties().entrySet() )
1346 String key = new MetadataFacetModel.KeyBuilder().withRepositoryId( repositoryId ).withFacetId(
1347 metadataFacet.getFacetId() ).withName( metadataFacet.getName() ).withKey( entry.getKey() ).build();
1349 boolean exists = this.metadataFacetTemplate.isColumnsExist( key );
1352 //metadataFacetModel = new MetadataFacetModel();
1353 // we need to store the repositoryId
1354 //ArtifactMetadataModel artifactMetadataModel = new ArtifactMetadataModel();
1355 //artifactMetadataModel.setRepositoryId( repositoryId );
1356 //metadataFacetModel.setArtifactMetadataModel( artifactMetadataModel );
1357 //metadataFacetModel.setId( key );
1358 //metadataFacetModel.setKey( entry.getKey() );
1359 //metadataFacetModel.setFacetId( metadataFacet.getFacetId() );
1360 //metadataFacetModel.setName( metadataFacet.getName() );
1362 String cf = this.cassandraArchivaManager.getMetadataFacetModelFamilyName();
1363 this.metadataFacetTemplate.createMutator() //
1364 .addInsertion( key, cf, column( "repositoryName", repositoryId ) ) //
1365 .addInsertion( key, cf, column( "facetId", metadataFacet.getFacetId() ) ) //
1366 .addInsertion( key, cf, column( "name", metadataFacet.getName() ) ) //
1367 .addInsertion( key, cf, column( "key", entry.getKey() ) ) //
1368 .addInsertion( key, cf, column( "value", entry.getValue() ) ) //
1374 ColumnFamilyUpdater<String, String> updater = this.metadataFacetTemplate.createUpdater( key );
1375 updater.setString( "value", entry.getValue() );
1376 this.metadataFacetTemplate.update( updater );
1383 public void removeMetadataFacets( final String repositoryId, final String facetId )
1384 throws MetadataRepositoryException
1386 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
1387 StringSerializer ss = StringSerializer.get();
1388 QueryResult<OrderedRows<String, String, String>> result = HFactory //
1389 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1390 .setColumnFamily( cassandraArchivaManager.getMetadataFacetModelFamilyName() ) //
1391 .setColumnNames( "key", "value" ) //
1392 .addEqualsExpression( "repositoryName", repositoryId ) //
1393 .addEqualsExpression( "facetId", facetId ) //
1396 for ( Row<String, String, String> row : result.get() )
1398 this.metadataFacetTemplate.deleteRow( row.getKey() );
1404 public void removeMetadataFacet( final String repositoryId, final String facetId, final String name )
1405 throws MetadataRepositoryException
1407 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
1408 StringSerializer ss = StringSerializer.get();
1409 QueryResult<OrderedRows<String, String, String>> result = HFactory //
1410 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1411 .setColumnFamily( cassandraArchivaManager.getMetadataFacetModelFamilyName() ) //
1412 .setColumnNames( "key", "value" ) //
1413 .addEqualsExpression( "repositoryName", repositoryId ) //
1414 .addEqualsExpression( "facetId", facetId ) //
1415 .addEqualsExpression( "name", name ) //
1418 for ( Row<String, String, String> row : result.get() )
1420 this.metadataFacetTemplate.deleteRow( row.getKey() );
1425 public List<ArtifactMetadata> getArtifactsByDateRange( final String repositoryId, final Date startTime,
1426 final Date endTime )
1427 throws MetadataRepositoryException
1430 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
1431 StringSerializer ss = StringSerializer.get();
1432 LongSerializer ls = LongSerializer.get();
1433 RangeSlicesQuery<String, String, Long> query = HFactory //
1434 .createRangeSlicesQuery( keyspace, ss, ss, ls ) //
1435 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataModelFamilyName() ) //
1436 .setColumnNames( "namespaceId", "size", "id", "fileLastModified", "md5", "project", "projectVersion",
1437 "repositoryName", "version", "whenGathered", "sha1" ); //
1439 if ( startTime != null )
1441 query = query.addGteExpression( "whenGathered", startTime.getTime() );
1443 if ( endTime != null )
1445 query = query.addLteExpression( "whenGathered", endTime.getTime() );
1447 QueryResult<OrderedRows<String, String, Long>> result = query.execute();
1449 List<ArtifactMetadata> artifactMetadatas = new ArrayList<ArtifactMetadata>( result.get().getCount() );
1451 for ( Row<String, String, Long> row : result.get() )
1453 ColumnSlice<String, Long> columnSlice = row.getColumnSlice();
1454 String repositoryName = getAsStringValue( columnSlice, "repositoryName" );
1455 if ( StringUtils.equals( repositoryName, repositoryId ) )
1458 artifactMetadatas.add( mapArtifactMetadataLongColumnSlice( columnSlice ) );
1462 return artifactMetadatas;
1466 protected ArtifactMetadata mapArtifactMetadataLongColumnSlice( ColumnSlice<String, Long> columnSlice )
1468 ArtifactMetadata artifactMetadata = new ArtifactMetadata();
1469 artifactMetadata.setNamespace( getAsStringValue( columnSlice, "namespaceId" ) );
1470 artifactMetadata.setSize( getLongValue( columnSlice, "size" ) );
1471 artifactMetadata.setId( getAsStringValue( columnSlice, "id" ) );
1472 artifactMetadata.setFileLastModified( getLongValue( columnSlice, "fileLastModified" ) );
1473 artifactMetadata.setMd5( getAsStringValue( columnSlice, "md5" ) );
1474 artifactMetadata.setProject( getAsStringValue( columnSlice, "project" ) );
1475 artifactMetadata.setProjectVersion( getAsStringValue( columnSlice, "projectVersion" ) );
1476 artifactMetadata.setRepositoryId( getAsStringValue( columnSlice, "repositoryName" ) );
1477 artifactMetadata.setSha1( getAsStringValue( columnSlice, "sha1" ) );
1478 artifactMetadata.setVersion( getAsStringValue( columnSlice, "version" ) );
1479 Long whenGathered = getLongValue( columnSlice, "whenGathered" );
1480 if ( whenGathered != null )
1482 artifactMetadata.setWhenGathered( new Date( whenGathered ) );
1484 return artifactMetadata;
1487 protected ArtifactMetadata mapArtifactMetadataStringColumnSlice( ColumnSlice<String, String> columnSlice )
1489 ArtifactMetadata artifactMetadata = new ArtifactMetadata();
1490 artifactMetadata.setNamespace( getStringValue( columnSlice, "namespaceId" ) );
1491 artifactMetadata.setSize( getAsLongValue( columnSlice, "size" ) );
1492 artifactMetadata.setId( getStringValue( columnSlice, "id" ) );
1493 artifactMetadata.setFileLastModified( getAsLongValue( columnSlice, "fileLastModified" ) );
1494 artifactMetadata.setMd5( getStringValue( columnSlice, "md5" ) );
1495 artifactMetadata.setProject( getStringValue( columnSlice, "project" ) );
1496 artifactMetadata.setProjectVersion( getStringValue( columnSlice, "projectVersion" ) );
1497 artifactMetadata.setRepositoryId( getStringValue( columnSlice, "repositoryName" ) );
1498 artifactMetadata.setSha1( getStringValue( columnSlice, "sha1" ) );
1499 artifactMetadata.setVersion( getStringValue( columnSlice, "version" ) );
1500 Long whenGathered = getAsLongValue( columnSlice, "whenGathered" );
1501 if ( whenGathered != null )
1503 artifactMetadata.setWhenGathered( new Date( whenGathered ) );
1505 return artifactMetadata;
1509 public Collection<ArtifactMetadata> getArtifactsByChecksum( final String repositoryId, final String checksum )
1510 throws MetadataRepositoryException
1512 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
1513 StringSerializer ss = StringSerializer.get();
1515 // cql cannot run or in queries so running twice the query
1516 Map<String, ArtifactMetadata> artifactMetadataMap = new HashMap<String, ArtifactMetadata>();
1518 RangeSlicesQuery<String, String, String> query = HFactory //
1519 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1520 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataModelFamilyName() ) //
1521 .setColumnNames( "namespaceId", "size", "id", "fileLastModified", "md5", "project", "projectVersion",
1522 "repositoryName", "version", "whenGathered", "sha1" ); //
1524 query = query.addEqualsExpression( "sha1", checksum ).addEqualsExpression( "repositoryName", repositoryId );
1526 QueryResult<OrderedRows<String, String, String>> result = query.execute();
1528 for ( Row<String, String, String> row : result.get() )
1530 ColumnSlice<String, String> columnSlice = row.getColumnSlice();
1532 artifactMetadataMap.put( row.getKey(), mapArtifactMetadataStringColumnSlice( columnSlice ) );
1537 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1538 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataModelFamilyName() ) //
1539 .setColumnNames( "namespaceId", "size", "id", "fileLastModified", "md5", "project", "projectVersion",
1540 "repositoryName", "version", "whenGathered", "sha1" ); //
1542 query = query.addEqualsExpression( "md5", checksum ).addEqualsExpression( "repositoryName", repositoryId );
1544 result = query.execute();
1546 for ( Row<String, String, String> row : result.get() )
1548 ColumnSlice<String, String> columnSlice = row.getColumnSlice();
1550 artifactMetadataMap.put( row.getKey(), mapArtifactMetadataStringColumnSlice( columnSlice ) );
1554 return artifactMetadataMap.values();
1559 public void removeArtifact( final String repositoryId, final String namespace, final String project,
1560 final String version, final String id )
1561 throws MetadataRepositoryException
1563 logger.debug( "removeArtifact repositoryId: '{}', namespace: '{}', project: '{}', version: '{}', id: '{}'",
1564 repositoryId, namespace, project, version, id );
1566 new ArtifactMetadataModel.KeyBuilder().withRepositoryId( repositoryId ).withNamespace( namespace ).withId(
1567 id ).withProjectVersion( version ).withProject( project ).build();
1569 this.artifactMetadataTemplate.deleteRow( key );
1572 new ProjectVersionMetadataModel.KeyBuilder().withId( version ).withRepository( repositoryId ).withNamespace(
1573 namespace ).withProjectId( project ).build();
1575 this.projectVersionMetadataModelTemplate.deleteRow( key );
1581 public void removeArtifact( ArtifactMetadata artifactMetadata, String baseVersion )
1582 throws MetadataRepositoryException
1584 logger.debug( "removeArtifact repositoryId: '{}', namespace: '{}', project: '{}', version: '{}', id: '{}'",
1585 artifactMetadata.getRepositoryId(), artifactMetadata.getNamespace(),
1586 artifactMetadata.getProject(), baseVersion, artifactMetadata.getId() );
1588 new ArtifactMetadataModel.KeyBuilder().withRepositoryId( artifactMetadata.getRepositoryId() ).withNamespace(
1589 artifactMetadata.getNamespace() ).withId( artifactMetadata.getId() ).withProjectVersion(
1590 baseVersion ).withProject( artifactMetadata.getProject() ).build();
1592 this.artifactMetadataTemplate.deleteRow( key );
1597 public void removeArtifact( final String repositoryId, final String namespace, final String project,
1598 final String version, final MetadataFacet metadataFacet )
1599 throws MetadataRepositoryException
1602 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
1604 StringSerializer ss = StringSerializer.get();
1606 RangeSlicesQuery<String, String, String> query = HFactory //
1607 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1608 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataModelFamilyName() ) //
1609 .setColumnNames( "namespaceId" ); //
1611 query = query.addEqualsExpression( "repositoryName", repositoryId ) //
1612 .addEqualsExpression( "namespaceId", namespace ) //
1613 .addEqualsExpression( "project", project ) //
1614 .addEqualsExpression( "version", version );
1616 QueryResult<OrderedRows<String, String, String>> result = query.execute();
1618 for ( Row<String, String, String> row : result.get() )
1620 this.artifactMetadataTemplate.deleteRow( row.getKey() );
1628 public List<ArtifactMetadata> getArtifacts( final String repositoryId )
1629 throws MetadataRepositoryException
1631 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
1632 StringSerializer ss = StringSerializer.get();
1634 RangeSlicesQuery<String, String, String> query = HFactory //
1635 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1636 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataModelFamilyName() ) //
1637 .setColumnNames( "namespaceId", "size", "id", "fileLastModified", "md5", "project", "projectVersion",
1638 "repositoryName", "version", "whenGathered", "sha1" ); //
1640 query = query.addEqualsExpression( "repositoryName", repositoryId );
1642 QueryResult<OrderedRows<String, String, String>> result = query.execute();
1644 List<ArtifactMetadata> artifactMetadatas = new ArrayList<ArtifactMetadata>( result.get().getCount() );
1646 for ( Row<String, String, String> row : result.get() )
1648 ColumnSlice<String, String> columnSlice = row.getColumnSlice();
1650 artifactMetadatas.add( mapArtifactMetadataStringColumnSlice( columnSlice ) );
1654 return artifactMetadatas;
1659 public Collection<ProjectVersionReference> getProjectReferences( String repoId, String namespace, String projectId,
1660 String projectVersion )
1661 throws MetadataResolutionException
1663 // FIXME implement this
1664 return Collections.emptyList();
1668 public void removeProjectVersion( final String repoId, final String namespace, final String projectId,
1669 final String projectVersion )
1670 throws MetadataRepositoryException
1673 String key = new ProjectVersionMetadataModel.KeyBuilder().withRepository( repoId ).withNamespace(
1674 namespace ).withProjectId( projectId ).withId( projectVersion ).build();
1676 this.projectVersionMetadataModelTemplate.deleteRow( key );
1678 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
1680 StringSerializer ss = StringSerializer.get();
1682 RangeSlicesQuery<String, String, String> query = HFactory //
1683 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1684 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataModelFamilyName() ) //
1685 .setColumnNames( "namespaceId" ); //
1687 query = query.addEqualsExpression( "repositoryName", repoId ) //
1688 .addEqualsExpression( "namespaceId", namespace ) //
1689 .addEqualsExpression( "project", projectId ) //
1690 .addEqualsExpression( "projectVersion", projectVersion );
1692 QueryResult<OrderedRows<String, String, String>> result = query.execute();
1694 for ( Row<String, String, String> row : result.get() )
1696 this.artifactMetadataTemplate.deleteRow( row.getKey() );
1702 public Collection<ArtifactMetadata> getArtifacts( final String repoId, final String namespace,
1703 final String projectId, final String projectVersion )
1704 throws MetadataResolutionException
1707 Keyspace keyspace = cassandraArchivaManager.getKeyspace();
1709 StringSerializer ss = StringSerializer.get();
1711 QueryResult<OrderedRows<String, String, String>> result =
1712 HFactory.createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1713 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataModelFamilyName() ) //
1714 .setColumnNames( "id", "repositoryName", "namespaceId", "project", "projectVersion", "version",
1715 "fileLastModified", "size", "md5", "sha1", "whenGathered" )//
1716 .setRowCount( Integer.MAX_VALUE ) //
1717 .addEqualsExpression( "repositoryName", repoId ) //
1718 .addEqualsExpression( "namespaceId", namespace ) //
1719 .addEqualsExpression( "project", projectId ) //
1720 .addEqualsExpression( "projectVersion", projectVersion ) //
1723 if ( result.get() == null || result.get().getCount() < 1 )
1725 return Collections.emptyList();
1728 List<ArtifactMetadata> artifactMetadatas = new ArrayList<ArtifactMetadata>( result.get().getCount() );
1730 for ( Row<String, String, String> row : result.get() )
1732 ColumnSlice<String, String> columnSlice = row.getColumnSlice();
1733 ArtifactMetadata artifactMetadata = new ArtifactMetadata();
1734 artifactMetadata.setNamespace( getStringValue( columnSlice, "namespaceId" ) );
1735 artifactMetadata.setSize( getAsLongValue( columnSlice, "size" ) );
1736 artifactMetadata.setId( getStringValue( columnSlice, "id" ) );
1737 artifactMetadata.setFileLastModified( getAsLongValue( columnSlice, "fileLastModified" ) );
1738 artifactMetadata.setMd5( getStringValue( columnSlice, "md5" ) );
1739 artifactMetadata.setProject( getStringValue( columnSlice, "project" ) );
1740 artifactMetadata.setProjectVersion( getStringValue( columnSlice, "projectVersion" ) );
1741 artifactMetadata.setRepositoryId( repoId );
1742 artifactMetadata.setSha1( getStringValue( columnSlice, "sha1" ) );
1743 artifactMetadata.setVersion( getStringValue( columnSlice, "version" ) );
1744 Long whenGathered = getAsLongValue( columnSlice, "whenGathered" );
1745 if ( whenGathered != null )
1747 artifactMetadata.setWhenGathered( new Date( whenGathered ) );
1749 artifactMetadatas.add( artifactMetadata );
1752 result = HFactory.createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1753 .setColumnFamily( cassandraArchivaManager.getMetadataFacetModelFamilyName() ) //
1754 .setColumnNames( "facetId", "name", "value", "key", "projectVersion" ) //
1755 .setRowCount( Integer.MAX_VALUE ) //
1756 .addEqualsExpression( "repositoryName", repoId ) //
1757 .addEqualsExpression( "namespaceId", namespace ) //
1758 .addEqualsExpression( "projectId", projectId ) //
1759 .addEqualsExpression( "projectVersion", projectVersion ) //
1762 if ( result.get() == null || result.get().getCount() < 1 )
1764 return artifactMetadatas;
1767 final List<MetadataFacetModel> metadataFacetModels =
1768 new ArrayList<MetadataFacetModel>( result.get().getCount() );
1770 for ( Row<String, String, String> row : result.get() )
1772 ColumnSlice<String, String> columnSlice = row.getColumnSlice();
1773 MetadataFacetModel metadataFacetModel = new MetadataFacetModel();
1774 metadataFacetModel.setFacetId( getStringValue( columnSlice, "facetId" ) );
1775 metadataFacetModel.setName( getStringValue( columnSlice, "name" ) );
1776 metadataFacetModel.setValue( getStringValue( columnSlice, "value" ) );
1777 metadataFacetModel.setKey( getStringValue( columnSlice, "key" ) );
1778 metadataFacetModel.setProjectVersion( getStringValue( columnSlice, "projectVersion" ) );
1779 metadataFacetModels.add( metadataFacetModel );
1782 // rebuild MetadataFacet for artifacts
1784 for ( final ArtifactMetadata artifactMetadata : artifactMetadatas )
1786 Iterable<MetadataFacetModel> metadataFacetModelIterable =
1787 Iterables.filter( metadataFacetModels, new Predicate<MetadataFacetModel>()
1790 public boolean apply( MetadataFacetModel metadataFacetModel )
1792 if ( metadataFacetModel != null )
1794 return StringUtils.equals( artifactMetadata.getVersion(),
1795 metadataFacetModel.getProjectVersion() );
1800 Iterator<MetadataFacetModel> iterator = metadataFacetModelIterable.iterator();
1801 Map<String, List<MetadataFacetModel>> metadataFacetValuesPerFacetId =
1802 new HashMap<String, List<MetadataFacetModel>>();
1803 while ( iterator.hasNext() )
1805 MetadataFacetModel metadataFacetModel = iterator.next();
1806 List<MetadataFacetModel> values = metadataFacetValuesPerFacetId.get( metadataFacetModel.getName() );
1807 if ( values == null )
1809 values = new ArrayList<MetadataFacetModel>();
1810 metadataFacetValuesPerFacetId.put( metadataFacetModel.getFacetId(), values );
1812 values.add( metadataFacetModel );
1816 for ( Map.Entry<String, List<MetadataFacetModel>> entry : metadataFacetValuesPerFacetId.entrySet() )
1818 MetadataFacetFactory metadataFacetFactory = metadataFacetFactories.get( entry.getKey() );
1819 if ( metadataFacetFactory != null )
1821 List<MetadataFacetModel> facetModels = entry.getValue();
1822 if ( !facetModels.isEmpty() )
1824 MetadataFacet metadataFacet =
1825 metadataFacetFactory.createMetadataFacet( repoId, facetModels.get( 0 ).getName() );
1826 Map<String, String> props = new HashMap<String, String>( facetModels.size() );
1827 for ( MetadataFacetModel metadataFacetModel : facetModels )
1829 props.put( metadataFacetModel.getKey(), metadataFacetModel.getValue() );
1831 metadataFacet.fromProperties( props );
1832 artifactMetadata.addFacet( metadataFacet );
1840 return artifactMetadatas;
1846 logger.trace( "save" );
1851 throws MetadataRepositoryException
1853 logger.trace( "close" );
1857 public void revert()
1859 logger.warn( "CassandraMetadataRepository cannot revert" );
1863 public boolean canObtainAccess( Class<?> aClass )
1869 public <T> T obtainAccess( Class<T> aClass )
1870 throws MetadataRepositoryException
1872 throw new IllegalArgumentException(
1873 "Access using " + aClass + " is not supported on the cassandra metadata storage" );
1877 private static class ModelMapperHolder
1879 private static ModelMapper MODEL_MAPPER = new ModelMapper();
1882 protected ModelMapper getModelMapper()
1884 return ModelMapperHolder.MODEL_MAPPER;