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.checksum.ChecksumAlgorithm;
41 import org.apache.archiva.configuration.ArchivaConfiguration;
42 import org.apache.archiva.metadata.QueryParameter;
43 import org.apache.archiva.metadata.model.ArtifactMetadata;
44 import org.apache.archiva.metadata.model.CiManagement;
45 import org.apache.archiva.metadata.model.Dependency;
46 import org.apache.archiva.metadata.model.FacetedMetadata;
47 import org.apache.archiva.metadata.model.IssueManagement;
48 import org.apache.archiva.metadata.model.License;
49 import org.apache.archiva.metadata.model.MailingList;
50 import org.apache.archiva.metadata.model.MetadataFacet;
51 import org.apache.archiva.metadata.model.MetadataFacetFactory;
52 import org.apache.archiva.metadata.model.Organization;
53 import org.apache.archiva.metadata.model.ProjectMetadata;
54 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
55 import org.apache.archiva.metadata.model.ProjectVersionReference;
56 import org.apache.archiva.metadata.model.Scm;
57 import org.apache.archiva.metadata.repository.AbstractMetadataRepository;
58 import org.apache.archiva.metadata.repository.MetadataRepository;
59 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
60 import org.apache.archiva.metadata.repository.MetadataResolutionException;
61 import org.apache.archiva.metadata.repository.MetadataService;
62 import org.apache.archiva.metadata.repository.RepositorySession;
63 import org.apache.archiva.metadata.repository.cassandra.model.ArtifactMetadataModel;
64 import org.apache.archiva.metadata.repository.cassandra.model.MetadataFacetModel;
65 import org.apache.archiva.metadata.repository.cassandra.model.Namespace;
66 import org.apache.archiva.metadata.repository.cassandra.model.Project;
67 import org.apache.archiva.metadata.repository.cassandra.model.ProjectVersionMetadataModel;
68 import org.apache.archiva.metadata.repository.cassandra.model.Repository;
69 import org.apache.commons.lang.StringUtils;
70 import org.modelmapper.ModelMapper;
71 import org.slf4j.Logger;
72 import org.slf4j.LoggerFactory;
74 import javax.annotation.ParametersAreNonnullByDefault;
75 import java.time.Instant;
76 import java.time.ZonedDateTime;
78 import java.util.function.BiFunction;
79 import java.util.function.Consumer;
80 import java.util.stream.Collectors;
81 import java.util.stream.Stream;
82 import java.util.stream.StreamSupport;
84 import static org.apache.archiva.metadata.model.ModelInfo.STORAGE_TZ;
85 import static org.apache.archiva.metadata.repository.cassandra.CassandraUtils.*;
86 import static org.apache.archiva.metadata.repository.cassandra.model.ColumnNames.*;
89 * @author Olivier Lamy
92 @ParametersAreNonnullByDefault
93 public class CassandraMetadataRepository
94 extends AbstractMetadataRepository implements MetadataRepository
97 private static final String ARTIFACT_METADATA_MODEL_KEY = "artifactMetadataModel.key";
98 private Logger logger = LoggerFactory.getLogger( getClass() );
100 private ArchivaConfiguration configuration;
102 private final CassandraArchivaManager cassandraArchivaManager;
104 private final ColumnFamilyTemplate<String, String> projectVersionMetadataTemplate;
106 private final ColumnFamilyTemplate<String, String> projectTemplate;
108 private final ColumnFamilyTemplate<String, String> artifactMetadataTemplate;
110 private final ColumnFamilyTemplate<String, String> metadataFacetTemplate;
112 private final ColumnFamilyTemplate<String, String> mailingListTemplate;
114 private final ColumnFamilyTemplate<String, String> licenseTemplate;
116 private final ColumnFamilyTemplate<String, String> dependencyTemplate;
118 private final ColumnFamilyTemplate<String, String> checksumTemplate;
120 private final Keyspace keyspace;
122 private final StringSerializer ss = StringSerializer.get();
124 public CassandraMetadataRepository( MetadataService metadataService,
125 ArchivaConfiguration configuration,
126 CassandraArchivaManager cassandraArchivaManager )
128 super( metadataService );
129 this.configuration = configuration;
130 this.cassandraArchivaManager = cassandraArchivaManager;
131 this.keyspace = cassandraArchivaManager.getKeyspace();
133 this.projectVersionMetadataTemplate =
134 new ThriftColumnFamilyTemplate<>( cassandraArchivaManager.getKeyspace(), //
135 cassandraArchivaManager.getProjectVersionMetadataFamilyName(), //
136 StringSerializer.get(), //
137 StringSerializer.get() );
139 this.projectTemplate = new ThriftColumnFamilyTemplate<>( cassandraArchivaManager.getKeyspace(), //
140 cassandraArchivaManager.getProjectFamilyName(), //
142 StringSerializer.get(), //
143 StringSerializer.get() );
145 this.artifactMetadataTemplate = new ThriftColumnFamilyTemplate<>( cassandraArchivaManager.getKeyspace(), //
146 cassandraArchivaManager.getArtifactMetadataFamilyName(),
147 StringSerializer.get(), //
148 StringSerializer.get() );
150 this.metadataFacetTemplate = new ThriftColumnFamilyTemplate<>( cassandraArchivaManager.getKeyspace(), //
151 cassandraArchivaManager.getMetadataFacetFamilyName(),
153 StringSerializer.get(), //
154 StringSerializer.get() );
156 this.mailingListTemplate = new ThriftColumnFamilyTemplate<>( cassandraArchivaManager.getKeyspace(), //
157 cassandraArchivaManager.getMailingListFamilyName(),
159 StringSerializer.get(), //
160 StringSerializer.get() );
162 this.licenseTemplate = new ThriftColumnFamilyTemplate<>( cassandraArchivaManager.getKeyspace(), //
163 cassandraArchivaManager.getLicenseFamilyName(),
165 StringSerializer.get(), //
166 StringSerializer.get() );
168 this.dependencyTemplate = new ThriftColumnFamilyTemplate<>( cassandraArchivaManager.getKeyspace(), //
169 cassandraArchivaManager.getDependencyFamilyName(),
171 StringSerializer.get(), //
172 StringSerializer.get() );
174 this.checksumTemplate = new ThriftColumnFamilyTemplate<>( cassandraArchivaManager.getKeyspace(), //
175 cassandraArchivaManager.getChecksumFamilyName(),
177 StringSerializer.get(), //
178 StringSerializer.get() );
183 * if the repository doesn't exist it will be created
185 * @param repositoryId
188 public Repository getOrCreateRepository( String repositoryId )
189 throws MetadataRepositoryException
191 String cf = cassandraArchivaManager.getRepositoryFamilyName();
193 QueryResult<OrderedRows<String, String, String>> result = HFactory //
194 .createRangeSlicesQuery( keyspace, StringSerializer.get(), StringSerializer.get(),
195 StringSerializer.get() ) //
196 .setColumnFamily( cf ) //
197 .setColumnNames( REPOSITORY_NAME.toString() ) //
198 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
201 if ( result.get().getCount() < 1 )
203 // we need to create the repository
204 Repository repository = new Repository( repositoryId );
208 MutationResult mutationResult = HFactory.createMutator( keyspace, StringSerializer.get() ) //
209 .addInsertion( repositoryId, cf,
210 CassandraUtils.column( REPOSITORY_NAME.toString(), repository.getName() ) ) //
212 logger.debug( "time to insert repository: {}", mutationResult.getExecutionTimeMicro() );
215 catch ( HInvalidRequestException e )
217 logger.error( e.getMessage(), e );
218 throw new MetadataRepositoryException( e.getMessage(), e );
223 return new Repository(
224 result.get().getList().get( 0 ).getColumnSlice().getColumnByName( REPOSITORY_NAME.toString() ).getValue() );
228 protected Repository getRepository( String repositoryId )
229 throws MetadataRepositoryException
232 QueryResult<OrderedRows<String, String, String>> result = HFactory //
233 .createRangeSlicesQuery( keyspace, StringSerializer.get(), StringSerializer.get(),
234 StringSerializer.get() ) //
235 .setColumnFamily( cassandraArchivaManager.getRepositoryFamilyName() ) //
236 .setColumnNames( REPOSITORY_NAME.toString() ) //
237 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
239 return ( result.get().getCount() > 0 ) ? new Repository( repositoryId ) : null;
243 public void updateNamespace( RepositorySession session, String repositoryId, String namespaceId )
244 throws MetadataRepositoryException
246 updateOrAddNamespace( repositoryId, namespaceId );
249 private Namespace updateOrAddNamespace( String repositoryId, String namespaceId )
250 throws MetadataRepositoryException
254 Repository repository = getOrCreateRepository( repositoryId );
257 new Namespace.KeyBuilder().withNamespace( namespaceId ).withRepositoryId( repositoryId ).build();
259 Namespace namespace = getNamespace( repositoryId, namespaceId );
260 if ( namespace == null )
262 String cf = cassandraArchivaManager.getNamespaceFamilyName();
263 namespace = new Namespace( namespaceId, repository );
264 HFactory.createMutator( keyspace, StringSerializer.get() )
266 .addInsertion( key, cf, CassandraUtils.column( NAME.toString(), namespace.getName() ) ) //
267 .addInsertion( key, cf, CassandraUtils.column( REPOSITORY_NAME.toString(), repository.getName() ) ) //
273 catch ( HInvalidRequestException e )
275 logger.error( e.getMessage(), e );
276 throw new MetadataRepositoryException( e.getMessage(), e );
280 protected Namespace getNamespace( String repositoryId, String namespaceId )
283 QueryResult<OrderedRows<String, String, String>> result = HFactory //
284 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
285 .setColumnFamily( cassandraArchivaManager.getNamespaceFamilyName() ) //
286 .setColumnNames( REPOSITORY_NAME.toString(), NAME.toString() ) //
287 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
288 .addEqualsExpression( NAME.toString(), namespaceId ) //
290 if ( result.get().getCount() > 0 )
292 ColumnSlice<String, String> columnSlice = result.get().getList().get( 0 ).getColumnSlice();
293 return new Namespace( getStringValue( columnSlice, NAME.toString() ), //
294 new Repository( getStringValue( columnSlice, REPOSITORY_NAME.toString() ) ) );
302 public void removeNamespace( RepositorySession session, String repositoryId, String namespaceId )
303 throws MetadataRepositoryException
308 String key = new Namespace.KeyBuilder() //
309 .withNamespace( namespaceId ) //
310 .withRepositoryId( repositoryId ) //
313 HFactory.createMutator( cassandraArchivaManager.getKeyspace(), new StringSerializer() ) //
314 .addDeletion( key, cassandraArchivaManager.getNamespaceFamilyName() ) //
317 QueryResult<OrderedRows<String, String, String>> result = HFactory //
318 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
319 .setColumnFamily( cassandraArchivaManager.getProjectFamilyName() ) //
320 .setColumnNames( REPOSITORY_NAME.toString() ) //
321 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
322 .addEqualsExpression( NAMESPACE_ID.toString(), namespaceId ) //
325 for ( Row<String, String, String> row : result.get() )
327 this.projectTemplate.deleteRow( row.getKey() );
331 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
332 .setColumnFamily( cassandraArchivaManager.getProjectVersionMetadataFamilyName() ) //
333 .setColumnNames( REPOSITORY_NAME.toString() ) //
334 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
335 .addEqualsExpression( NAMESPACE_ID.toString(), namespaceId ) //
338 for ( Row<String, String, String> row : result.get() )
340 this.projectVersionMetadataTemplate.deleteRow( row.getKey() );
341 removeMailingList( row.getKey() );
345 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
346 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataFamilyName() ) //
347 .setColumnNames( REPOSITORY_NAME.toString() ) //
348 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
349 .addEqualsExpression( NAMESPACE_ID.toString(), namespaceId ) //
352 for ( Row<String, String, String> row : result.get() )
354 this.artifactMetadataTemplate.deleteRow( row.getKey() );
358 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
359 .setColumnFamily( cassandraArchivaManager.getMetadataFacetFamilyName() ) //
360 .setColumnNames( REPOSITORY_NAME.toString() ) //
361 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
362 .addEqualsExpression( NAMESPACE_ID.toString(), namespaceId ) //
365 for ( Row<String, String, String> row : result.get() )
367 this.metadataFacetTemplate.deleteRow( row.getKey() );
371 catch ( HInvalidRequestException e )
373 logger.error( e.getMessage(), e );
374 throw new MetadataRepositoryException( e.getMessage(), e );
380 public void removeRepository( RepositorySession session, final String repositoryId )
381 throws MetadataRepositoryException
384 // TODO use cql queries to delete all
385 List<String> namespacesKey = new ArrayList<>();
387 QueryResult<OrderedRows<String, String, String>> result = HFactory //
388 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
389 .setColumnFamily( cassandraArchivaManager.getNamespaceFamilyName() ) //
390 .setColumnNames( REPOSITORY_NAME.toString() ) //
391 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
394 for ( Row<String, String, String> row : result.get().getList() )
396 namespacesKey.add( row.getKey() );
399 HFactory.createMutator( cassandraArchivaManager.getKeyspace(), ss ) //
400 .addDeletion( namespacesKey, cassandraArchivaManager.getNamespaceFamilyName() ) //
403 //delete repositoryId
404 HFactory.createMutator( cassandraArchivaManager.getKeyspace(), ss ) //
405 .addDeletion( repositoryId, cassandraArchivaManager.getRepositoryFamilyName() ) //
409 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
410 .setColumnFamily( cassandraArchivaManager.getProjectFamilyName() ) //
411 .setColumnNames( REPOSITORY_NAME.toString() ) //
412 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
415 for ( Row<String, String, String> row : result.get() )
417 this.projectTemplate.deleteRow( row.getKey() );
421 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
422 .setColumnFamily( cassandraArchivaManager.getProjectVersionMetadataFamilyName() ) //
423 .setColumnNames( REPOSITORY_NAME.toString() ) //
424 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
427 for ( Row<String, String, String> row : result.get() )
429 this.projectVersionMetadataTemplate.deleteRow( row.getKey() );
430 removeMailingList( row.getKey() );
434 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
435 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataFamilyName() ) //
436 .setColumnNames( REPOSITORY_NAME.toString() ) //
437 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
440 for ( Row<String, String, String> row : result.get() )
442 this.artifactMetadataTemplate.deleteRow( row.getKey() );
446 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
447 .setColumnFamily( cassandraArchivaManager.getMetadataFacetFamilyName() ) //
448 .setColumnNames( REPOSITORY_NAME.toString() ) //
449 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
452 for ( Row<String, String, String> row : result.get() )
454 this.metadataFacetTemplate.deleteRow( row.getKey() );
460 // FIXME this one need peformance improvement maybe a cache?
462 public Collection<String> getRootNamespaces( RepositorySession session, final String repoId )
463 throws MetadataResolutionException
466 QueryResult<OrderedRows<String, String, String>> result = HFactory //
467 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
468 .setColumnFamily( cassandraArchivaManager.getNamespaceFamilyName() ) //
469 .setColumnNames( NAME.toString() ) //
470 .addEqualsExpression( REPOSITORY_NAME.toString(), repoId ) //
473 Set<String> namespaces = new HashSet<String>( result.get().getCount() );
475 for ( Row<String, String, String> row : result.get() )
477 namespaces.add( StringUtils.substringBefore( getStringValue( row.getColumnSlice(), NAME.toString() ), "." ) );
483 // FIXME this one need peformance improvement maybe a cache?
485 public Collection<String> getChildNamespaces( RepositorySession session, final String repoId, final String namespaceId )
486 throws MetadataResolutionException
489 QueryResult<OrderedRows<String, String, String>> result = HFactory //
490 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
491 .setColumnFamily( cassandraArchivaManager.getNamespaceFamilyName() ) //
492 .setColumnNames( NAME.toString() ) //
493 .addEqualsExpression( REPOSITORY_NAME.toString(), repoId ) //
496 List<String> namespaces = new ArrayList<>( result.get().getCount() );
498 for ( Row<String, String, String> row : result.get() )
500 String currentNamespace = getStringValue( row.getColumnSlice(), NAME.toString() );
501 if ( StringUtils.startsWith( currentNamespace, namespaceId ) //
502 && ( StringUtils.length( currentNamespace ) > StringUtils.length( namespaceId ) ) )
504 // store after namespaceId '.' but before next '.'
505 // call org namespace org.apache.maven.shared -> stored apache
507 String calledNamespace = StringUtils.endsWith( namespaceId, "." ) ? namespaceId : namespaceId + ".";
508 String storedNamespace = StringUtils.substringAfter( currentNamespace, calledNamespace );
510 storedNamespace = StringUtils.substringBefore( storedNamespace, "." );
512 namespaces.add( storedNamespace );
520 // only use for testing purpose
521 protected List<String> getNamespaces( final String repoId )
522 throws MetadataResolutionException
525 QueryResult<OrderedRows<String, String, String>> result = HFactory //
526 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
527 .setColumnFamily( cassandraArchivaManager.getNamespaceFamilyName() ) //
528 .setColumnNames( NAME.toString() ) //
529 .addEqualsExpression( REPOSITORY_NAME.toString(), repoId ) //
532 List<String> namespaces = new ArrayList<>( result.get().getCount() );
534 for ( Row<String, String, String> row : result.get() )
536 namespaces.add( getStringValue( row.getColumnSlice(), NAME.toString() ) );
544 public void updateProject( RepositorySession session, String repositoryId, ProjectMetadata projectMetadata )
545 throws MetadataRepositoryException
548 QueryResult<OrderedRows<String, String, String>> result = HFactory //
549 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
550 .setColumnFamily( cassandraArchivaManager.getProjectFamilyName() ) //
551 .setColumnNames( PROJECT_ID.toString() ) //
552 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
553 .addEqualsExpression( NAMESPACE_ID.toString(), projectMetadata.getNamespace() ) //
554 .addEqualsExpression( PROJECT_ID.toString(), projectMetadata.getId() ) //
557 // project exists ? if yes return nothing to update here
558 if ( result.get().getCount() > 0 )
564 Namespace namespace = updateOrAddNamespace( repositoryId, projectMetadata.getNamespace() );
567 new Project.KeyBuilder().withProjectId( projectMetadata.getId() ).withNamespace( namespace ).build();
569 String cf = cassandraArchivaManager.getProjectFamilyName();
570 projectTemplate.createMutator()
572 .addInsertion( key, cf, CassandraUtils.column( PROJECT_ID.toString(), projectMetadata.getId() ) ) //
573 .addInsertion( key, cf, CassandraUtils.column( REPOSITORY_NAME.toString(), repositoryId ) ) //
574 .addInsertion( key, cf, CassandraUtils.column( NAMESPACE_ID.toString(), projectMetadata.getNamespace() ) )//
580 public Collection<String> getProjects( RepositorySession session, final String repoId, final String namespace )
581 throws MetadataResolutionException
584 QueryResult<OrderedRows<String, String, String>> result = HFactory //
585 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
586 .setColumnFamily( cassandraArchivaManager.getProjectFamilyName() ) //
587 .setColumnNames( PROJECT_ID.toString() ) //
588 .addEqualsExpression( REPOSITORY_NAME.toString(), repoId ) //
589 .addEqualsExpression( NAMESPACE_ID.toString(), namespace ) //
592 final Set<String> projects = new HashSet<String>( result.get().getCount() );
594 for ( Row<String, String, String> row : result.get() )
596 projects.add( getStringValue( row.getColumnSlice(), PROJECT_ID.toString() ) );
603 public void removeProject( RepositorySession session, final String repositoryId, final String namespaceId, final String projectId )
604 throws MetadataRepositoryException
607 String key = new Project.KeyBuilder() //
608 .withProjectId( projectId ) //
609 .withNamespace( new Namespace( namespaceId, new Repository( repositoryId ) ) ) //
612 this.projectTemplate.deleteRow( key );
614 QueryResult<OrderedRows<String, String, String>> result = HFactory //
615 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
616 .setColumnFamily( cassandraArchivaManager.getProjectVersionMetadataFamilyName() ) //
617 .setColumnNames( ID.toString() ) //
618 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
619 .addEqualsExpression( NAMESPACE_ID.toString(), namespaceId ) //
620 .addEqualsExpression( PROJECT_ID.toString(), projectId ) //
623 for ( Row<String, String, String> row : result.get() )
625 this.projectVersionMetadataTemplate.deleteRow( row.getKey() );
626 removeMailingList( row.getKey() );
630 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
631 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataFamilyName() ) //
632 .setColumnNames( PROJECT_ID.toString() ) //
633 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
634 .addEqualsExpression( NAMESPACE_ID.toString(), namespaceId ) //
635 .addEqualsExpression( PROJECT_ID.toString(), projectId ) //
638 for ( Row<String, String, String> row : result.get() )
640 this.artifactMetadataTemplate.deleteRow( row.getKey() );
645 public Collection<String> getProjectVersions( RepositorySession session, final String repoId, final String namespace, final String projectId )
646 throws MetadataResolutionException
649 QueryResult<OrderedRows<String, String, String>> result = HFactory //
650 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
651 .setColumnFamily( cassandraArchivaManager.getProjectVersionMetadataFamilyName() ) //
652 .setColumnNames( PROJECT_VERSION.toString() ) //
653 .addEqualsExpression( REPOSITORY_NAME.toString(), repoId ) //
654 .addEqualsExpression( NAMESPACE_ID.toString(), namespace ) //
655 .addEqualsExpression( PROJECT_ID.toString(), projectId ) //
658 int count = result.get().getCount();
662 return Collections.emptyList();
665 Set<String> versions = new HashSet<String>( count );
667 for ( Row<String, String, String> orderedRows : result.get() )
669 versions.add( getStringValue( orderedRows.getColumnSlice(), PROJECT_VERSION.toString() ) );
677 public ProjectMetadata getProject( RepositorySession session, final String repoId, final String namespace, final String id )
678 throws MetadataResolutionException
681 QueryResult<OrderedRows<String, String, String>> result = HFactory //
682 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
683 .setColumnFamily( cassandraArchivaManager.getProjectFamilyName() ) //
684 .setColumnNames( PROJECT_ID.toString() ) //
685 .addEqualsExpression( REPOSITORY_NAME.toString(), repoId ) //
686 .addEqualsExpression( NAMESPACE_ID.toString(), namespace ) //
687 .addEqualsExpression( PROJECT_ID.toString(), id ) //
690 int count = result.get().getCount();
697 ProjectMetadata projectMetadata = new ProjectMetadata();
698 projectMetadata.setId( id );
699 projectMetadata.setNamespace( namespace );
701 logger.debug( "getProject repoId: {}, namespace: {}, projectId: {} -> {}", repoId, namespace, id,
704 return projectMetadata;
707 protected ProjectVersionMetadataModel mapProjectVersionMetadataModel( ColumnSlice<String, String> columnSlice )
709 ProjectVersionMetadataModel projectVersionMetadataModel = new ProjectVersionMetadataModel();
710 projectVersionMetadataModel.setId( getStringValue( columnSlice, ID.toString() ) );
711 projectVersionMetadataModel.setDescription( getStringValue( columnSlice, DESCRIPTION.toString() ) );
712 projectVersionMetadataModel.setName( getStringValue( columnSlice, NAME.toString() ) );
713 Namespace namespace = new Namespace( getStringValue( columnSlice, NAMESPACE_ID.toString() ), //
714 new Repository( getStringValue( columnSlice, REPOSITORY_NAME.toString() ) ) );
715 projectVersionMetadataModel.setNamespace( namespace );
716 projectVersionMetadataModel.setIncomplete(
717 Boolean.parseBoolean( getStringValue( columnSlice, "incomplete" ) ) );
718 projectVersionMetadataModel.setProjectId( getStringValue( columnSlice, PROJECT_ID.toString() ) );
719 projectVersionMetadataModel.setUrl( getStringValue( columnSlice, URL.toString() ) );
720 return projectVersionMetadataModel;
725 public void updateProjectVersion( RepositorySession session, String repositoryId, String namespaceId, String projectId,
726 ProjectVersionMetadata versionMetadata )
727 throws MetadataRepositoryException
731 Namespace namespace = getNamespace( repositoryId, namespaceId );
733 if ( namespace == null )
735 updateOrAddNamespace( repositoryId, namespaceId );
738 if ( getProject( session, repositoryId, namespaceId, projectId ) == null )
740 ProjectMetadata projectMetadata = new ProjectMetadata();
741 projectMetadata.setNamespace( namespaceId );
742 projectMetadata.setId( projectId );
743 updateProject( session, repositoryId, projectMetadata );
747 catch ( MetadataResolutionException e )
749 throw new MetadataRepositoryException( e.getMessage(), e );
752 QueryResult<OrderedRows<String, String, String>> result = HFactory //
753 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
754 .setColumnFamily( cassandraArchivaManager.getProjectVersionMetadataFamilyName() ) //
755 .setColumnNames( PROJECT_VERSION.toString() ) //
756 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
757 .addEqualsExpression( NAMESPACE_ID.toString(), namespaceId ) //
758 .addEqualsExpression( PROJECT_ID.toString(), projectId ) //
759 .addEqualsExpression( PROJECT_VERSION.toString(), versionMetadata.getId() ) //
762 ProjectVersionMetadataModel projectVersionMetadataModel = null;
763 boolean creation = true;
764 if ( result.get().getCount() > 0 )
766 projectVersionMetadataModel =
767 mapProjectVersionMetadataModel( result.get().getList().get( 0 ).getColumnSlice() );
772 projectVersionMetadataModel = getModelMapper().map( versionMetadata, ProjectVersionMetadataModel.class );
775 projectVersionMetadataModel.setProjectId( projectId );
776 projectVersionMetadataModel.setNamespace( new Namespace( namespaceId, new Repository( repositoryId ) ) );
778 projectVersionMetadataModel.setCiManagement( versionMetadata.getCiManagement() );
779 projectVersionMetadataModel.setIssueManagement( versionMetadata.getIssueManagement() );
780 projectVersionMetadataModel.setOrganization( versionMetadata.getOrganization() );
781 projectVersionMetadataModel.setScm( versionMetadata.getScm() );
783 projectVersionMetadataModel.setMailingLists( versionMetadata.getMailingLists() );
784 projectVersionMetadataModel.setDependencies( versionMetadata.getDependencies() );
785 projectVersionMetadataModel.setLicenses( versionMetadata.getLicenses() );
787 // we don't test of repository and namespace really exist !
788 String key = new ProjectVersionMetadataModel.KeyBuilder() //
789 .withRepository( repositoryId ) //
790 .withNamespace( namespaceId ) //
791 .withProjectId( projectId ) //
792 .withProjectVersion( versionMetadata.getVersion() ) //
793 .withId( versionMetadata.getId() ) //
796 // FIXME nested objects to store!!!
799 String cf = cassandraArchivaManager.getProjectVersionMetadataFamilyName();
800 Mutator<String> mutator = projectVersionMetadataTemplate.createMutator()
802 .addInsertion( key, cf, column( PROJECT_ID.toString(), projectId ) ) //
803 .addInsertion( key, cf, column( REPOSITORY_NAME.toString(), repositoryId ) ) //
804 .addInsertion( key, cf, column( NAMESPACE_ID.toString(), namespaceId ) )//
805 .addInsertion( key, cf, column( PROJECT_VERSION.toString(), versionMetadata.getVersion() ) ); //
807 addInsertion( mutator, key, cf, DESCRIPTION.toString(), versionMetadata.getDescription() );
809 addInsertion( mutator, key, cf, NAME.toString(), versionMetadata.getName() );
811 addInsertion( mutator, key, cf, "incomplete", Boolean.toString( versionMetadata.isIncomplete() ) );
813 addInsertion( mutator, key, cf, URL.toString(), versionMetadata.getUrl() );
815 CiManagement ci = versionMetadata.getCiManagement();
818 addInsertion( mutator, key, cf, "ciManagement.system", ci.getSystem() );
819 addInsertion( mutator, key, cf, "ciManagement.url", ci.getUrl() );
824 IssueManagement issueManagement = versionMetadata.getIssueManagement();
826 if ( issueManagement != null )
828 addInsertion( mutator, key, cf, "issueManagement.system", issueManagement.getSystem() );
829 addInsertion( mutator, key, cf, "issueManagement.url", issueManagement.getUrl() );
834 Organization organization = versionMetadata.getOrganization();
835 if ( organization != null )
837 addInsertion( mutator, key, cf, "organization.name", organization.getName() );
838 addInsertion( mutator, key, cf, "organization.url", organization.getUrl() );
843 Scm scm = versionMetadata.getScm();
846 addInsertion( mutator, key, cf, "scm.url", scm.getUrl() );
847 addInsertion( mutator, key, cf, "scm.connection", scm.getConnection() );
848 addInsertion( mutator, key, cf, "scm.developerConnection", scm.getDeveloperConnection() );
852 recordMailingList( key, versionMetadata.getMailingLists() );
854 recordLicenses( key, versionMetadata.getLicenses() );
856 recordDependencies( key, versionMetadata.getDependencies(), repositoryId );
858 MutationResult mutationResult = mutator.execute();
862 ColumnFamilyUpdater<String, String> updater = projectVersionMetadataTemplate.createUpdater( key );
863 addUpdateStringValue( updater, PROJECT_ID.toString(), projectId );
864 addUpdateStringValue( updater, REPOSITORY_NAME.toString(), repositoryId );
865 addUpdateStringValue( updater, NAMESPACE_ID.toString(), namespaceId );
866 addUpdateStringValue( updater, PROJECT_VERSION.toString(), versionMetadata.getVersion() );
867 addUpdateStringValue( updater, DESCRIPTION.toString(), versionMetadata.getDescription() );
869 addUpdateStringValue( updater, NAME.toString(), versionMetadata.getName() );
871 updater.setString( "incomplete", Boolean.toString( versionMetadata.isIncomplete() ) );
872 addUpdateStringValue( updater, URL.toString(), versionMetadata.getUrl() );
875 CiManagement ci = versionMetadata.getCiManagement();
878 addUpdateStringValue( updater, "ciManagement.system", ci.getSystem() );
879 addUpdateStringValue( updater, "ciManagement.url", ci.getUrl() );
883 IssueManagement issueManagement = versionMetadata.getIssueManagement();
884 if ( issueManagement != null )
886 addUpdateStringValue( updater, "issueManagement.system", issueManagement.getSystem() );
887 addUpdateStringValue( updater, "issueManagement.url", issueManagement.getUrl() );
891 Organization organization = versionMetadata.getOrganization();
892 if ( organization != null )
894 addUpdateStringValue( updater, "organization.name", organization.getName() );
895 addUpdateStringValue( updater, "organization.url", organization.getUrl() );
899 Scm scm = versionMetadata.getScm();
902 addUpdateStringValue( updater, "scm.url", scm.getUrl() );
903 addUpdateStringValue( updater, "scm.connection", scm.getConnection() );
904 addUpdateStringValue( updater, "scm.developerConnection", scm.getDeveloperConnection() );
908 // update is a delete record
909 removeMailingList( key );
910 recordMailingList( key, versionMetadata.getMailingLists() );
912 removeLicenses( key );
913 recordLicenses( key, versionMetadata.getLicenses() );
915 removeDependencies( key );
916 recordDependencies( key, versionMetadata.getDependencies(), repositoryId );
918 projectVersionMetadataTemplate.update( updater );
922 ArtifactMetadataModel artifactMetadataModel = new ArtifactMetadataModel();
923 artifactMetadataModel.setRepositoryId( repositoryId );
924 artifactMetadataModel.setNamespace( namespaceId );
925 artifactMetadataModel.setProject( projectId );
926 artifactMetadataModel.setProjectVersion( versionMetadata.getVersion() );
927 artifactMetadataModel.setVersion( versionMetadata.getVersion() );
928 updateFacets( versionMetadata, artifactMetadataModel );
934 public ProjectVersionMetadata getProjectVersion( RepositorySession session, final String repoId, final String namespace,
935 final String projectId, final String projectVersion )
936 throws MetadataResolutionException
939 QueryResult<OrderedRows<String, String, String>> result = HFactory //
940 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
941 .setColumnFamily( cassandraArchivaManager.getProjectVersionMetadataFamilyName() ) //
942 .setColumnNames( PROJECT_VERSION.toString() ) //
943 .addEqualsExpression( REPOSITORY_NAME.toString(), repoId ) //
944 .addEqualsExpression( NAMESPACE_ID.toString(), namespace ) //
945 .addEqualsExpression( PROJECT_ID.toString(), projectId ) //
946 .addEqualsExpression( PROJECT_VERSION.toString(), projectVersion ) //
949 if ( result.get().getCount() < 1 )
954 String key = result.get().iterator().next().getKey();
956 ColumnFamilyResult<String, String> columnFamilyResult = this.projectVersionMetadataTemplate.queryColumns( key );
958 if ( !columnFamilyResult.hasResults() )
963 ProjectVersionMetadata projectVersionMetadata = new ProjectVersionMetadata();
964 projectVersionMetadata.setId( columnFamilyResult.getString( PROJECT_VERSION.toString() ) );
965 projectVersionMetadata.setDescription( columnFamilyResult.getString( DESCRIPTION.toString() ) );
966 projectVersionMetadata.setName( columnFamilyResult.getString( NAME.toString() ) );
968 projectVersionMetadata.setIncomplete( Boolean.parseBoolean( columnFamilyResult.getString( "incomplete" ) ) );
970 projectVersionMetadata.setUrl( columnFamilyResult.getString( URL.toString() ) );
972 String ciUrl = columnFamilyResult.getString( "ciManagement.url" );
973 String ciSystem = columnFamilyResult.getString( "ciManagement.system" );
975 if ( StringUtils.isNotEmpty( ciSystem ) || StringUtils.isNotEmpty( ciUrl ) )
977 projectVersionMetadata.setCiManagement( new CiManagement( ciSystem, ciUrl ) );
981 String issueUrl = columnFamilyResult.getString( "issueManagement.url" );
982 String issueSystem = columnFamilyResult.getString( "issueManagement.system" );
983 if ( StringUtils.isNotEmpty( issueSystem ) || StringUtils.isNotEmpty( issueUrl ) )
985 projectVersionMetadata.setIssueManagement( new IssueManagement( issueSystem, issueUrl ) );
989 String organizationUrl = columnFamilyResult.getString( "organization.url" );
990 String organizationName = columnFamilyResult.getString( "organization.name" );
991 if ( StringUtils.isNotEmpty( organizationUrl ) || StringUtils.isNotEmpty( organizationName ) )
993 projectVersionMetadata.setOrganization( new Organization( organizationName, organizationUrl ) );
997 String devConn = columnFamilyResult.getString( "scm.developerConnection" );
998 String conn = columnFamilyResult.getString( "scm.connection" );
999 String url = columnFamilyResult.getString( "scm.url" );
1000 if ( StringUtils.isNotEmpty( devConn ) || StringUtils.isNotEmpty( conn ) || StringUtils.isNotEmpty( url ) )
1002 projectVersionMetadata.setScm( new Scm( conn, devConn, url ) );
1005 projectVersionMetadata.setMailingLists( getMailingLists( key ) );
1006 projectVersionMetadata.setLicenses( getLicenses( key ) );
1007 projectVersionMetadata.setDependencies( getDependencies( key ) );
1010 result = HFactory //
1011 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1012 .setColumnFamily( cassandraArchivaManager.getMetadataFacetFamilyName() ) //
1013 .setColumnNames( FACET_ID.toString(), KEY.toString(), VALUE.toString(), NAME.toString() ) //
1014 .addEqualsExpression( REPOSITORY_NAME.toString(), repoId ) //
1015 .addEqualsExpression( NAMESPACE_ID.toString(), namespace ) //
1016 .addEqualsExpression( PROJECT_ID.toString(), projectId ) //
1017 .addEqualsExpression( PROJECT_VERSION.toString(), projectVersion ) //
1020 Map<String, Map<String, String>> metadataFacetsPerFacetIds = new HashMap<>();
1022 for ( Row<String, String, String> row : result.get() )
1024 ColumnSlice<String, String> columnSlice = row.getColumnSlice();
1025 String facetId = getStringValue( columnSlice, FACET_ID.toString() );
1026 Map<String, String> metaValues = metadataFacetsPerFacetIds.get( facetId );
1027 if ( metaValues == null )
1029 metaValues = new HashMap<>();
1030 metadataFacetsPerFacetIds.put( facetId, metaValues );
1032 metaValues.put( getStringValue( columnSlice, KEY.toString() ), getStringValue( columnSlice, VALUE.toString() ) );
1035 if ( !metadataFacetsPerFacetIds.isEmpty() )
1037 for ( Map.Entry<String, Map<String, String>> entry : metadataFacetsPerFacetIds.entrySet() )
1039 MetadataFacetFactory metadataFacetFactory = getFacetFactory( entry.getKey() );
1040 if ( metadataFacetFactory != null )
1042 MetadataFacet metadataFacet = metadataFacetFactory.createMetadataFacet();
1043 metadataFacet.fromProperties( entry.getValue() );
1044 projectVersionMetadata.addFacet( metadataFacet );
1049 return projectVersionMetadata;
1052 protected void recordChecksums( String repositoryId, String artifactMetadataKey, Map<String, String> checksums)
1054 if ( checksums == null || checksums.isEmpty() )
1058 Mutator<String> checksumMutator = this.checksumTemplate.createMutator();
1059 for ( Map.Entry<String, String> entry : checksums.entrySet())
1061 // we don't care about the key as the real used one with the projectVersionMetadata
1062 String keyChecksums = UUID.randomUUID().toString();
1063 String cfChecksums = cassandraArchivaManager.getChecksumFamilyName();
1065 addInsertion( checksumMutator, keyChecksums, cfChecksums, ARTIFACT_METADATA_MODEL_KEY,
1066 artifactMetadataKey );
1067 addInsertion( checksumMutator, keyChecksums, cfChecksums, CHECKSUM_ALG.toString(), entry.getKey());
1068 addInsertion( checksumMutator, keyChecksums, cfChecksums, CHECKSUM_VALUE.toString(),
1070 addInsertion(checksumMutator, keyChecksums, cfChecksums, REPOSITORY_NAME.toString(), repositoryId);
1073 checksumMutator.execute();
1076 protected void removeChecksums( String artifactMetadataKey )
1079 QueryResult<OrderedRows<String, String, String>> result =
1080 HFactory.createRangeSlicesQuery( cassandraArchivaManager.getKeyspace(), ss, ss, ss ) //
1081 .setColumnFamily( cassandraArchivaManager.getChecksumFamilyName() ) //
1082 .setColumnNames( CHECKSUM_ALG.toString() ) //
1083 .setRowCount( Integer.MAX_VALUE ) //
1084 .addEqualsExpression(ARTIFACT_METADATA_MODEL_KEY, artifactMetadataKey ) //
1087 if ( result.get().getCount() < 1 )
1092 for ( Row<String, String, String> row : result.get() )
1094 this.checksumTemplate.deleteRow( row.getKey() );
1099 protected Map<String, String> getChecksums( String artifactMetadataKey )
1101 Map<String, String> checksums = new HashMap<>();
1103 QueryResult<OrderedRows<String, String, String>> result =
1104 HFactory.createRangeSlicesQuery( cassandraArchivaManager.getKeyspace(), ss, ss, ss ) //
1105 .setColumnFamily( cassandraArchivaManager.getChecksumFamilyName() ) //
1106 .setColumnNames( ARTIFACT_METADATA_MODEL_KEY, REPOSITORY_NAME.toString(),
1107 CHECKSUM_ALG.toString(), CHECKSUM_VALUE.toString() ) //
1108 .setRowCount( Integer.MAX_VALUE ) //
1109 .addEqualsExpression(ARTIFACT_METADATA_MODEL_KEY, artifactMetadataKey) //
1111 for ( Row<String, String, String> row : result.get() )
1113 ColumnFamilyResult<String, String> columnFamilyResult =
1114 this.checksumTemplate.queryColumns( row.getKey() );
1116 checksums.put(columnFamilyResult.getString(CHECKSUM_ALG.toString()),
1117 columnFamilyResult.getString(CHECKSUM_VALUE.toString()));
1123 protected void recordMailingList( String projectVersionMetadataKey, List<MailingList> mailingLists )
1125 if ( mailingLists == null || mailingLists.isEmpty() )
1129 Mutator<String> mailingMutator = this.mailingListTemplate.createMutator();
1130 for ( MailingList mailingList : mailingLists )
1132 // we don't care about the key as the real used one with the projectVersionMetadata
1133 String keyMailingList = UUID.randomUUID().toString();
1134 String cfMailingList = cassandraArchivaManager.getMailingListFamilyName();
1136 addInsertion( mailingMutator, keyMailingList, cfMailingList, "projectVersionMetadataModel.key",
1137 projectVersionMetadataKey );
1138 addInsertion( mailingMutator, keyMailingList, cfMailingList, NAME.toString(), mailingList.getName() );
1139 addInsertion( mailingMutator, keyMailingList, cfMailingList, "mainArchiveUrl",
1140 mailingList.getMainArchiveUrl() );
1141 addInsertion( mailingMutator, keyMailingList, cfMailingList, "postAddress", mailingList.getPostAddress() );
1142 addInsertion( mailingMutator, keyMailingList, cfMailingList, "subscribeAddress",
1143 mailingList.getSubscribeAddress() );
1144 addInsertion( mailingMutator, keyMailingList, cfMailingList, "unsubscribeAddress",
1145 mailingList.getUnsubscribeAddress() );
1147 for ( String otherArchive : mailingList.getOtherArchives() )
1149 addInsertion( mailingMutator, keyMailingList, cfMailingList, "otherArchive." + idx, otherArchive );
1154 mailingMutator.execute();
1157 protected void removeMailingList( String projectVersionMetadataKey )
1160 QueryResult<OrderedRows<String, String, String>> result =
1161 HFactory.createRangeSlicesQuery( cassandraArchivaManager.getKeyspace(), ss, ss, ss ) //
1162 .setColumnFamily( cassandraArchivaManager.getMailingListFamilyName() ) //
1163 .setColumnNames( NAME.toString() ) //
1164 .setRowCount( Integer.MAX_VALUE ) //
1165 .addEqualsExpression( "projectVersionMetadataModel.key", projectVersionMetadataKey ) //
1168 if ( result.get().getCount() < 1 )
1173 for ( Row<String, String, String> row : result.get() )
1175 this.mailingListTemplate.deleteRow( row.getKey() );
1180 protected List<MailingList> getMailingLists( String projectVersionMetadataKey )
1182 List<MailingList> mailingLists = new ArrayList<>();
1184 QueryResult<OrderedRows<String, String, String>> result =
1185 HFactory.createRangeSlicesQuery( cassandraArchivaManager.getKeyspace(), ss, ss, ss ) //
1186 .setColumnFamily( cassandraArchivaManager.getMailingListFamilyName() ) //
1187 .setColumnNames( NAME.toString() ) //
1188 .setRowCount( Integer.MAX_VALUE ) //
1189 .addEqualsExpression( "projectVersionMetadataModel.key", projectVersionMetadataKey ) //
1191 for ( Row<String, String, String> row : result.get() )
1193 ColumnFamilyResult<String, String> columnFamilyResult =
1194 this.mailingListTemplate.queryColumns( row.getKey() );
1196 MailingList mailingList = new MailingList();
1197 mailingList.setName( columnFamilyResult.getString( NAME.toString() ) );
1198 mailingList.setMainArchiveUrl( columnFamilyResult.getString( "mainArchiveUrl" ) );
1199 mailingList.setPostAddress( columnFamilyResult.getString( "postAddress" ) );
1200 mailingList.setSubscribeAddress( columnFamilyResult.getString( "subscribeAddress" ) );
1201 mailingList.setUnsubscribeAddress( columnFamilyResult.getString( "unsubscribeAddress" ) );
1203 List<String> otherArchives = new ArrayList<>();
1205 for ( String columnName : columnFamilyResult.getColumnNames() )
1207 if ( StringUtils.startsWith( columnName, "otherArchive." ) )
1209 otherArchives.add( columnFamilyResult.getString( columnName ) );
1213 mailingList.setOtherArchives( otherArchives );
1214 mailingLists.add( mailingList );
1217 return mailingLists;
1220 protected void recordLicenses( String projectVersionMetadataKey, List<License> licenses )
1223 if ( licenses == null || licenses.isEmpty() )
1227 Mutator<String> licenseMutator = this.licenseTemplate.createMutator();
1229 for ( License license : licenses )
1231 // we don't care about the key as the real used one with the projectVersionMetadata
1232 String keyLicense = UUID.randomUUID().toString();
1233 String cfLicense = cassandraArchivaManager.getLicenseFamilyName();
1235 addInsertion( licenseMutator, keyLicense, cfLicense, "projectVersionMetadataModel.key",
1236 projectVersionMetadataKey );
1238 addInsertion( licenseMutator, keyLicense, cfLicense, NAME.toString(), license.getName() );
1240 addInsertion( licenseMutator, keyLicense, cfLicense, URL.toString(), license.getUrl() );
1243 licenseMutator.execute();
1246 protected void removeLicenses( String projectVersionMetadataKey )
1249 QueryResult<OrderedRows<String, String, String>> result =
1250 HFactory.createRangeSlicesQuery( cassandraArchivaManager.getKeyspace(), ss, ss, ss ) //
1251 .setColumnFamily( cassandraArchivaManager.getLicenseFamilyName() ) //
1252 .setColumnNames( NAME.toString() ) //
1253 .setRowCount( Integer.MAX_VALUE ) //
1254 .addEqualsExpression( "projectVersionMetadataModel.key", projectVersionMetadataKey ) //
1256 for ( Row<String, String, String> row : result.get() )
1258 this.licenseTemplate.deleteRow( row.getKey() );
1262 protected List<License> getLicenses( String projectVersionMetadataKey )
1264 List<License> licenses = new ArrayList<>();
1266 QueryResult<OrderedRows<String, String, String>> result =
1267 HFactory.createRangeSlicesQuery( cassandraArchivaManager.getKeyspace(), ss, ss, ss ) //
1268 .setColumnFamily( cassandraArchivaManager.getLicenseFamilyName() ) //
1269 .setColumnNames( "projectVersionMetadataModel.key" ) //
1270 .setRowCount( Integer.MAX_VALUE ) //
1271 .addEqualsExpression( "projectVersionMetadataModel.key", projectVersionMetadataKey ) //
1274 for ( Row<String, String, String> row : result.get() )
1276 ColumnFamilyResult<String, String> columnFamilyResult = this.licenseTemplate.queryColumns( row.getKey() );
1279 new License( columnFamilyResult.getString( NAME.toString() ), columnFamilyResult.getString( URL.toString() ) ) );
1286 protected void recordDependencies( String projectVersionMetadataKey, List<Dependency> dependencies,
1287 String repositoryId )
1290 if ( dependencies == null || dependencies.isEmpty() )
1294 Mutator<String> dependencyMutator = this.dependencyTemplate.createMutator();
1296 for ( Dependency dependency : dependencies )
1298 // we don't care about the key as the real used one with the projectVersionMetadata
1299 String keyDependency = UUID.randomUUID().toString();
1300 String cfDependency = cassandraArchivaManager.getDependencyFamilyName();
1302 addInsertion( dependencyMutator, keyDependency, cfDependency, "projectVersionMetadataModel.key",
1303 projectVersionMetadataKey );
1305 addInsertion( dependencyMutator, keyDependency, cfDependency, REPOSITORY_NAME.toString(), repositoryId );
1307 addInsertion( dependencyMutator, keyDependency, cfDependency, "classifier", dependency.getClassifier() );
1309 addInsertion( dependencyMutator, keyDependency, cfDependency, "optional",
1310 Boolean.toString( dependency.isOptional() ) );
1312 addInsertion( dependencyMutator, keyDependency, cfDependency, "scope", dependency.getScope() );
1314 addInsertion( dependencyMutator, keyDependency, cfDependency, "systemPath", dependency.getSystemPath() );
1316 addInsertion( dependencyMutator, keyDependency, cfDependency, "type", dependency.getType() );
1318 addInsertion( dependencyMutator, keyDependency, cfDependency, ARTIFACT_ID.toString(), dependency.getArtifactId() );
1320 addInsertion( dependencyMutator, keyDependency, cfDependency, GROUP_ID.toString(), dependency.getGroupId() );
1322 addInsertion( dependencyMutator, keyDependency, cfDependency, VERSION.toString(), dependency.getVersion() );
1325 dependencyMutator.execute();
1328 protected void removeDependencies( String projectVersionMetadataKey )
1331 QueryResult<OrderedRows<String, String, String>> result =
1332 HFactory.createRangeSlicesQuery( cassandraArchivaManager.getKeyspace(), ss, ss, ss ) //
1333 .setColumnFamily( cassandraArchivaManager.getDependencyFamilyName() ) //
1334 .setColumnNames( GROUP_ID.toString() ) //
1335 .setRowCount( Integer.MAX_VALUE ) //
1336 .addEqualsExpression( "projectVersionMetadataModel.key", projectVersionMetadataKey ) //
1338 for ( Row<String, String, String> row : result.get() )
1340 this.dependencyTemplate.deleteRow( row.getKey() );
1344 protected List<Dependency> getDependencies( String projectVersionMetadataKey )
1346 List<Dependency> dependencies = new ArrayList<>();
1348 QueryResult<OrderedRows<String, String, String>> result =
1349 HFactory.createRangeSlicesQuery( cassandraArchivaManager.getKeyspace(), ss, ss, ss ) //
1350 .setColumnFamily( cassandraArchivaManager.getDependencyFamilyName() ) //
1351 .setColumnNames( "projectVersionMetadataModel.key" ) //
1352 .setRowCount( Integer.MAX_VALUE ) //
1353 .addEqualsExpression( "projectVersionMetadataModel.key", projectVersionMetadataKey ) //
1356 for ( Row<String, String, String> row : result.get() )
1358 ColumnFamilyResult<String, String> columnFamilyResult =
1359 this.dependencyTemplate.queryColumns( row.getKey() );
1361 Dependency dependency = new Dependency();
1362 dependency.setClassifier( columnFamilyResult.getString( "classifier" ) );
1364 dependency.setOptional( Boolean.parseBoolean( columnFamilyResult.getString( "optional" ) ) );
1366 dependency.setScope( columnFamilyResult.getString( "scope" ) );
1368 dependency.setSystemPath( columnFamilyResult.getString( "systemPath" ) );
1370 dependency.setType( columnFamilyResult.getString( "type" ) );
1372 dependency.setArtifactId( columnFamilyResult.getString( ARTIFACT_ID.toString() ) );
1374 dependency.setGroupId( columnFamilyResult.getString( GROUP_ID.toString() ) );
1376 dependency.setVersion( columnFamilyResult.getString( VERSION.toString() ) );
1378 dependencies.add( dependency );
1381 return dependencies;
1384 private Map<String, String> mapChecksums(Map<ChecksumAlgorithm,String> checksums) {
1385 return checksums.entrySet().stream().collect(Collectors.toMap(
1386 e -> e.getKey().name(), e -> e.getValue()
1390 private Map<ChecksumAlgorithm, String> mapChecksumsReverse(Map<String,String> checksums) {
1391 return checksums.entrySet().stream().collect(Collectors.toMap(
1392 e -> ChecksumAlgorithm.valueOf(e.getKey()), e -> e.getValue()
1397 public void updateArtifact( RepositorySession session, String repositoryId, String namespaceId, String projectId, String projectVersion,
1398 ArtifactMetadata artifactMeta )
1399 throws MetadataRepositoryException
1402 Namespace namespace = getNamespace( repositoryId, namespaceId );
1403 if ( namespace == null )
1405 namespace = updateOrAddNamespace( repositoryId, namespaceId );
1408 ProjectMetadata projectMetadata = new ProjectMetadata();
1409 projectMetadata.setId( projectId );
1410 projectMetadata.setNamespace( namespaceId );
1411 updateProject( session, repositoryId, projectMetadata );
1413 String key = new ArtifactMetadataModel.KeyBuilder().withNamespace( namespace ).withProject( projectId ).withId(
1414 artifactMeta.getId() ).withProjectVersion( projectVersion ).build();
1418 boolean exists = this.artifactMetadataTemplate.isColumnsExist( key );
1423 ColumnFamilyUpdater<String, String> updater = this.artifactMetadataTemplate.createUpdater( key );
1424 updater.setLong( FILE_LAST_MODIFIED.toString(), artifactMeta.getFileLastModified().toInstant().toEpochMilli());
1425 updater.setLong( WHEN_GATHERED.toString(), artifactMeta.getWhenGathered().toInstant().toEpochMilli() );
1426 updater.setLong( SIZE.toString(), artifactMeta.getSize() );
1427 addUpdateStringValue( updater, VERSION.toString(), artifactMeta.getVersion() );
1428 removeChecksums(key);
1429 recordChecksums(repositoryId, key, mapChecksums(artifactMeta.getChecksums()));
1430 this.artifactMetadataTemplate.update( updater );
1434 String cf = this.cassandraArchivaManager.getArtifactMetadataFamilyName();
1436 this.artifactMetadataTemplate.createMutator() //
1437 .addInsertion( key, cf, column( ID.toString(), artifactMeta.getId() ) )//
1438 .addInsertion( key, cf, column( REPOSITORY_NAME.toString(), repositoryId ) ) //
1439 .addInsertion( key, cf, column( NAMESPACE_ID.toString(), namespaceId ) ) //
1440 .addInsertion( key, cf, column( PROJECT.toString(), artifactMeta.getProject() ) ) //
1441 .addInsertion( key, cf, column( PROJECT_VERSION.toString(), projectVersion ) ) //
1442 .addInsertion( key, cf, column( VERSION.toString(), artifactMeta.getVersion() ) ) //
1443 .addInsertion( key, cf, column( FILE_LAST_MODIFIED.toString(), artifactMeta.getFileLastModified().toInstant().toEpochMilli() ) ) //
1444 .addInsertion( key, cf, column( SIZE.toString(), artifactMeta.getSize() ) ) //
1445 .addInsertion( key, cf, column( WHEN_GATHERED.toString(), artifactMeta.getWhenGathered().toInstant().toEpochMilli() ) )//
1447 recordChecksums(repositoryId, key, mapChecksums(artifactMeta.getChecksums()));
1450 key = new ProjectVersionMetadataModel.KeyBuilder() //
1451 .withRepository( repositoryId ) //
1452 .withNamespace( namespace ) //
1453 .withProjectId( projectId ) //
1454 .withProjectVersion( projectVersion ) //
1455 .withId( artifactMeta.getId() ) //
1458 QueryResult<OrderedRows<String, String, String>> result = HFactory //
1459 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1460 .setColumnFamily( cassandraArchivaManager.getProjectVersionMetadataFamilyName() ) //
1461 .setColumnNames( VERSION.toString() ) //
1462 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
1463 .addEqualsExpression( NAMESPACE_ID.toString(), namespaceId ) //
1464 .addEqualsExpression( PROJECT_ID.toString(), projectId ) //
1465 .addEqualsExpression( PROJECT_VERSION.toString(), projectVersion ) //
1466 .addEqualsExpression( VERSION.toString(), artifactMeta.getVersion() ) //
1469 exists = result.get().getCount() > 0;
1473 String cf = this.cassandraArchivaManager.getProjectVersionMetadataFamilyName();
1475 projectVersionMetadataTemplate.createMutator() //
1476 .addInsertion( key, cf, column( NAMESPACE_ID.toString(), namespace.getName() ) ) //
1477 .addInsertion( key, cf, column( REPOSITORY_NAME.toString(), repositoryId ) ) //
1478 .addInsertion( key, cf, column( PROJECT_VERSION.toString(), projectVersion ) ) //
1479 .addInsertion( key, cf, column( PROJECT_ID.toString(), projectId ) ) //
1480 .addInsertion( key, cf, column( VERSION.toString(), artifactMeta.getVersion() ) ) //
1485 ArtifactMetadataModel artifactMetadataModel = new ArtifactMetadataModel();
1487 artifactMetadataModel.setRepositoryId( repositoryId );
1488 artifactMetadataModel.setNamespace( namespaceId );
1489 artifactMetadataModel.setProject( projectId );
1490 artifactMetadataModel.setProjectVersion( projectVersion );
1491 artifactMetadataModel.setVersion( artifactMeta.getVersion() );
1492 artifactMetadataModel.setFileLastModified( artifactMeta.getFileLastModified() == null
1493 ? ZonedDateTime.now().toInstant().toEpochMilli()
1494 : artifactMeta.getFileLastModified().toInstant().toEpochMilli() );
1495 artifactMetadataModel.setChecksums(mapChecksums(artifactMeta.getChecksums()));
1498 updateFacets( artifactMeta, artifactMetadataModel );
1503 public Collection<String> getArtifactVersions( RepositorySession session, final String repoId, final String namespace, final String projectId,
1504 final String projectVersion )
1505 throws MetadataResolutionException
1508 QueryResult<OrderedRows<String, String, String>> result = HFactory //
1509 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1510 .setColumnFamily( cassandraArchivaManager.getProjectVersionMetadataFamilyName() ) //
1511 .setColumnNames( VERSION.toString() ) //
1512 .addEqualsExpression( REPOSITORY_NAME.toString(), repoId ) //
1513 .addEqualsExpression( NAMESPACE_ID.toString(), namespace ) //
1514 .addEqualsExpression( PROJECT_ID.toString(), projectId ) //
1515 .addEqualsExpression( PROJECT_VERSION.toString(), projectVersion ) //
1518 final Set<String> versions = new HashSet<>();
1520 for ( Row<String, String, String> row : result.get() )
1522 versions.add( getStringValue( row.getColumnSlice(), VERSION.toString() ) );
1530 * iterate over available facets to remove/add from the artifactMetadata
1532 * @param facetedMetadata
1533 * @param artifactMetadataModel only use for the key
1535 private void updateFacets( final FacetedMetadata facetedMetadata,
1536 final ArtifactMetadataModel artifactMetadataModel )
1539 String cf = cassandraArchivaManager.getMetadataFacetFamilyName();
1541 for ( final String facetId : getSupportedFacets() )
1543 MetadataFacet metadataFacet = facetedMetadata.getFacet( facetId );
1544 if ( metadataFacet == null )
1550 QueryResult<OrderedRows<String, String, String>> result =
1551 HFactory.createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1552 .setColumnFamily( cf ) //
1553 .setColumnNames( REPOSITORY_NAME.toString() ) //
1554 .addEqualsExpression( REPOSITORY_NAME.toString(), artifactMetadataModel.getRepositoryId() ) //
1555 .addEqualsExpression( NAMESPACE_ID.toString(), artifactMetadataModel.getNamespace() ) //
1556 .addEqualsExpression( PROJECT_ID.toString(), artifactMetadataModel.getProject() ) //
1557 .addEqualsExpression( PROJECT_VERSION.toString(), artifactMetadataModel.getProjectVersion() ) //
1558 .addEqualsExpression( FACET_ID.toString(), facetId ) //
1561 for ( Row<String, String, String> row : result.get().getList() )
1563 this.metadataFacetTemplate.deleteRow( row.getKey() );
1566 Map<String, String> properties = metadataFacet.toProperties();
1568 for ( Map.Entry<String, String> entry : properties.entrySet() )
1570 String key = new MetadataFacetModel.KeyBuilder().withKey( entry.getKey() ).withArtifactMetadataModel(
1571 artifactMetadataModel ).withFacetId( facetId ).withName( metadataFacet.getName() ).build();
1572 Mutator<String> mutator = metadataFacetTemplate.createMutator() //
1573 .addInsertion( key, cf, column( REPOSITORY_NAME.toString(), artifactMetadataModel.getRepositoryId() ) ) //
1574 .addInsertion( key, cf, column( NAMESPACE_ID.toString(), artifactMetadataModel.getNamespace() ) ) //
1575 .addInsertion( key, cf, column( PROJECT_ID.toString(), artifactMetadataModel.getProject() ) ) //
1576 .addInsertion( key, cf, column( PROJECT_VERSION.toString(), artifactMetadataModel.getProjectVersion() ) ) //
1577 .addInsertion( key, cf, column( FACET_ID.toString(), facetId ) ) //
1578 .addInsertion( key, cf, column( KEY.toString(), entry.getKey() ) ) //
1579 .addInsertion( key, cf, column( VALUE.toString(), entry.getValue() ) );
1581 if ( metadataFacet.getName() != null )
1583 mutator.addInsertion( key, cf, column( NAME.toString(), metadataFacet.getName() ) );
1593 public List<String> getMetadataFacets( RepositorySession session, final String repositoryId, final String facetId )
1594 throws MetadataRepositoryException
1597 QueryResult<OrderedRows<String, String, String>> result = HFactory //
1598 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1599 .setColumnFamily( cassandraArchivaManager.getMetadataFacetFamilyName() ) //
1600 .setColumnNames( NAME.toString() ) //
1601 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
1602 .addEqualsExpression( FACET_ID.toString(), facetId ) //
1605 final List<String> facets = new ArrayList<>();
1607 for ( Row<String, String, String> row : result.get() )
1609 facets.add( getStringValue( row.getColumnSlice(), NAME.toString() ) );
1614 private <T> Spliterator<T> createResultSpliterator( QueryResult<OrderedRows<String, String, String>> result, BiFunction<Row<String, String, String>, T, T> converter) throws MetadataRepositoryException
1616 final int size = result.get().getCount();
1617 final Iterator<Row<String, String, String>> it = result.get( ).iterator( );
1619 return new Spliterator<T>( )
1621 private T lastItem = null;
1624 public boolean tryAdvance( Consumer<? super T> action )
1630 while ( it.hasNext( ) )
1632 Row<String, String, String> row = it.next( );
1633 T item = converter.apply( row, lastItem );
1634 if ( item != null && lastItem !=null && item != lastItem )
1636 action.accept( lastItem );
1642 action.accept( lastItem );
1652 public Spliterator<T> trySplit( )
1658 public long estimateSize( )
1664 public int characteristics( )
1666 return ORDERED+NONNULL+SIZED;
1673 * Implementation is not very performant, because sorting is part of the stream. I do not know how to specify the sort
1678 * @param repositoryId
1680 * @param queryParameter
1682 * @throws MetadataRepositoryException
1685 public <T extends MetadataFacet> Stream<T> getMetadataFacetStream(RepositorySession session, String repositoryId, Class<T> facetClazz, QueryParameter queryParameter) throws MetadataRepositoryException
1687 final MetadataFacetFactory<T> metadataFacetFactory = getFacetFactory( facetClazz );
1688 final String facetId = metadataFacetFactory.getFacetId( );
1690 QueryResult<OrderedRows<String, String, String>> result = HFactory //
1691 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1692 .setColumnFamily( cassandraArchivaManager.getMetadataFacetFamilyName( ) ) //
1693 .setColumnNames( NAME.toString( ), KEY.toString( ), VALUE.toString( ) ) //
1694 .addEqualsExpression( REPOSITORY_NAME.toString( ), repositoryId ) //
1695 .addEqualsExpression( FACET_ID.toString( ), facetId ) //
1696 .setRange( null, null, false, Integer.MAX_VALUE )
1697 .setRowCount( Integer.MAX_VALUE )
1702 return StreamSupport.stream( createResultSpliterator( result, ( Row<String, String, String> row, T lastItem)-> {
1703 ColumnSlice<String, String> columnSlice = row.getColumnSlice();
1704 String name = getStringValue( columnSlice, NAME.toString( ) );
1706 if (lastItem!=null && lastItem.getName().equals(name))
1708 updateItem = lastItem;
1711 updateItem = metadataFacetFactory.createMetadataFacet( repositoryId, name );
1713 String key = getStringValue( columnSlice, KEY.toString() );
1714 if (StringUtils.isNotEmpty( key ))
1716 Map<String, String> map = new HashMap<>( );
1717 map.put( key , getStringValue( columnSlice, VALUE.toString( ) ) );
1718 updateItem.fromProperties( map );
1722 }), false ).sorted( (f1, f2) -> f1.getName()!=null ? f1.getName().compareTo( f2.getName() ) : 1 ).skip( queryParameter.getOffset()).limit( queryParameter.getLimit());
1726 public boolean hasMetadataFacet( RepositorySession session, String repositoryId, String facetId )
1727 throws MetadataRepositoryException
1729 return !getMetadataFacets( session, repositoryId, facetId ).isEmpty();
1733 public <T extends MetadataFacet> T getMetadataFacet( RepositorySession session, final String repositoryId, final Class<T> facetClazz, final String name )
1734 throws MetadataRepositoryException
1736 final MetadataFacetFactory<T> metadataFacetFactory = getFacetFactory( facetClazz );
1737 if (metadataFacetFactory==null) {
1740 final String facetId = metadataFacetFactory.getFacetId( );
1741 if ( metadataFacetFactory == null )
1746 QueryResult<OrderedRows<String, String, String>> result = HFactory //
1747 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1748 .setColumnFamily( cassandraArchivaManager.getMetadataFacetFamilyName() ) //
1749 .setColumnNames( KEY.toString(), VALUE.toString() ) //
1750 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
1751 .addEqualsExpression( FACET_ID.toString(), facetId ) //
1752 .addEqualsExpression( NAME.toString(), name ) //
1755 T metadataFacet = metadataFacetFactory.createMetadataFacet( repositoryId, name );
1756 int size = result.get().getCount();
1761 Map<String, String> map = new HashMap<>( size );
1762 for ( Row<String, String, String> row : result.get() )
1764 ColumnSlice<String, String> columnSlice = row.getColumnSlice();
1765 map.put( getStringValue( columnSlice, KEY.toString() ), getStringValue( columnSlice, VALUE.toString() ) );
1767 metadataFacet.fromProperties( map );
1768 return metadataFacet;
1772 public MetadataFacet getMetadataFacet( RepositorySession session, String repositoryId, String facetId, String name ) throws MetadataRepositoryException
1774 return getMetadataFacet( session, repositoryId, getFactoryClassForId( facetId ), name );
1778 public void addMetadataFacet( RepositorySession session, String repositoryId, MetadataFacet metadataFacet )
1779 throws MetadataRepositoryException
1782 if ( metadataFacet == null )
1787 if ( metadataFacet.toProperties().isEmpty() )
1789 String key = new MetadataFacetModel.KeyBuilder().withRepositoryId( repositoryId ).withFacetId(
1790 metadataFacet.getFacetId() ).withName( metadataFacet.getName() ).build();
1792 boolean exists = this.metadataFacetTemplate.isColumnsExist( key );
1796 ColumnFamilyUpdater<String, String> updater = this.metadataFacetTemplate.createUpdater( key );
1797 addUpdateStringValue( updater, FACET_ID.toString(), metadataFacet.getFacetId() );
1798 addUpdateStringValue( updater, NAME.toString(), metadataFacet.getName() );
1799 this.metadataFacetTemplate.update( updater );
1803 String cf = this.cassandraArchivaManager.getMetadataFacetFamilyName();
1804 this.metadataFacetTemplate.createMutator() //
1805 .addInsertion( key, cf, column( REPOSITORY_NAME.toString(), repositoryId ) ) //
1806 .addInsertion( key, cf, column( FACET_ID.toString(), metadataFacet.getFacetId() ) ) //
1807 .addInsertion( key, cf, column( NAME.toString(), metadataFacet.getName() ) ) //
1814 for ( Map.Entry<String, String> entry : metadataFacet.toProperties().entrySet() )
1816 String key = new MetadataFacetModel.KeyBuilder().withRepositoryId( repositoryId ).withFacetId(
1817 metadataFacet.getFacetId() ).withName( metadataFacet.getName() ).withKey( entry.getKey() ).build();
1819 boolean exists = this.metadataFacetTemplate.isColumnsExist( key );
1822 String cf = this.cassandraArchivaManager.getMetadataFacetFamilyName();
1823 this.metadataFacetTemplate.createMutator() //
1824 .addInsertion( key, cf, column( REPOSITORY_NAME.toString(), repositoryId ) ) //
1825 .addInsertion( key, cf, column( FACET_ID.toString(), metadataFacet.getFacetId() ) ) //
1826 .addInsertion( key, cf, column( NAME.toString(), metadataFacet.getName() ) ) //
1827 .addInsertion( key, cf, column( KEY.toString(), entry.getKey() ) ) //
1828 .addInsertion( key, cf, column( VALUE.toString(), entry.getValue() ) ) //
1833 ColumnFamilyUpdater<String, String> updater = this.metadataFacetTemplate.createUpdater( key );
1834 addUpdateStringValue( updater, VALUE.toString(), entry.getValue() );
1835 this.metadataFacetTemplate.update( updater );
1842 public void removeMetadataFacets( RepositorySession session, final String repositoryId, final String facetId )
1843 throws MetadataRepositoryException
1846 QueryResult<OrderedRows<String, String, String>> result = HFactory //
1847 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1848 .setColumnFamily( cassandraArchivaManager.getMetadataFacetFamilyName() ) //
1849 .setColumnNames( KEY.toString(), VALUE.toString() ) //
1850 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
1851 .addEqualsExpression( FACET_ID.toString(), facetId ) //
1854 for ( Row<String, String, String> row : result.get() )
1856 this.metadataFacetTemplate.deleteRow( row.getKey() );
1862 public void removeMetadataFacet( RepositorySession session, final String repositoryId, final String facetId, final String name )
1863 throws MetadataRepositoryException
1866 QueryResult<OrderedRows<String, String, String>> result = HFactory //
1867 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1868 .setColumnFamily( cassandraArchivaManager.getMetadataFacetFamilyName() ) //
1869 .setColumnNames( KEY.toString(), VALUE.toString() ) //
1870 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
1871 .addEqualsExpression( FACET_ID.toString(), facetId ) //
1872 .addEqualsExpression( NAME.toString(), name ) //
1875 for ( Row<String, String, String> row : result.get() )
1877 this.metadataFacetTemplate.deleteRow( row.getKey() );
1882 public List<ArtifactMetadata> getArtifactsByDateRange( RepositorySession session, final String repositoryId, final ZonedDateTime startTime,
1883 final ZonedDateTime endTime, QueryParameter queryParameter )
1884 throws MetadataRepositoryException
1887 LongSerializer ls = LongSerializer.get();
1888 RangeSlicesQuery<String, String, Long> query = HFactory //
1889 .createRangeSlicesQuery( keyspace, ss, ss, ls ) //
1890 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataFamilyName() ) //
1891 .setColumnNames( ArtifactMetadataModel.COLUMNS ); //
1894 if ( startTime != null )
1896 query = query.addGteExpression( WHEN_GATHERED.toString(), startTime.toInstant().toEpochMilli() );
1898 if ( endTime != null )
1900 query = query.addLteExpression( WHEN_GATHERED.toString(), endTime.toInstant().toEpochMilli() );
1902 QueryResult<OrderedRows<String, String, Long>> result = query.execute();
1904 List<ArtifactMetadata> artifactMetadatas = new ArrayList<>( result.get().getCount() );
1905 Iterator<Row<String, String, Long>> keyIter = result.get().iterator();
1906 if (keyIter.hasNext()) {
1907 String key = keyIter.next().getKey();
1908 for (Row<String, String, Long> row : result.get()) {
1909 ColumnSlice<String, Long> columnSlice = row.getColumnSlice();
1910 String repositoryName = getAsStringValue(columnSlice, REPOSITORY_NAME.toString());
1911 if (StringUtils.equals(repositoryName, repositoryId)) {
1913 artifactMetadatas.add(mapArtifactMetadataLongColumnSlice(key, columnSlice));
1918 return artifactMetadatas;
1922 * For documentation see {@link MetadataRepository#getArtifactByDateRangeStream(RepositorySession, String, ZonedDateTime, ZonedDateTime, QueryParameter)}
1924 * This implementation orders the stream. It does not order the query in the backend.
1926 * @param session The repository session
1927 * @param repositoryId The repository id
1928 * @param startTime The start time, can be <code>null</code>
1929 * @param endTime The end time, can be <code>null</code>
1930 * @param queryParameter Additional parameters for the query that affect ordering and number of returned results.
1932 * @throws MetadataRepositoryException
1933 * @see MetadataRepository#getArtifactByDateRangeStream
1936 public Stream<ArtifactMetadata> getArtifactByDateRangeStream( RepositorySession session, String repositoryId, ZonedDateTime startTime, ZonedDateTime endTime, QueryParameter queryParameter) throws MetadataRepositoryException
1938 Comparator<ArtifactMetadata> comp = getArtifactMetadataComparator(queryParameter, "whenGathered");
1939 return getArtifactsByDateRange(session, repositoryId, startTime, endTime, queryParameter).stream().sorted(comp).skip(queryParameter.getOffset()).limit(queryParameter.getLimit());
1943 protected ArtifactMetadata mapArtifactMetadataLongColumnSlice( String key, ColumnSlice<String, Long> columnSlice )
1945 ArtifactMetadata artifactMetadata = new ArtifactMetadata();
1946 artifactMetadata.setNamespace( getAsStringValue( columnSlice, NAMESPACE_ID.toString() ) );
1947 artifactMetadata.setSize( getLongValue( columnSlice, SIZE.toString() ) );
1948 artifactMetadata.setId( getAsStringValue( columnSlice, ID.toString() ) );
1949 artifactMetadata.setFileLastModified( getLongValue( columnSlice, FILE_LAST_MODIFIED.toString() ) );
1950 artifactMetadata.setMd5( getAsStringValue( columnSlice, MD5.toString() ) );
1951 artifactMetadata.setProject( getAsStringValue( columnSlice, PROJECT.toString() ) );
1952 artifactMetadata.setProjectVersion( getAsStringValue( columnSlice, PROJECT_VERSION.toString() ) );
1953 artifactMetadata.setRepositoryId( getAsStringValue( columnSlice, REPOSITORY_NAME.toString() ) );
1954 artifactMetadata.setSha1( getAsStringValue( columnSlice, SHA1.toString() ) );
1955 artifactMetadata.setVersion( getAsStringValue( columnSlice, VERSION.toString() ) );
1956 Long whenGathered = getLongValue( columnSlice, WHEN_GATHERED.toString() );
1957 if ( whenGathered != null )
1959 artifactMetadata.setWhenGathered(ZonedDateTime.ofInstant(Instant.ofEpochMilli(whenGathered), STORAGE_TZ));
1961 artifactMetadata.setChecksums(mapChecksumsReverse(getChecksums(key)));
1962 return artifactMetadata;
1965 protected ArtifactMetadata mapArtifactMetadataStringColumnSlice( String key, ColumnSlice<String, String> columnSlice )
1967 ArtifactMetadata artifactMetadata = new ArtifactMetadata();
1968 artifactMetadata.setNamespace( getStringValue( columnSlice, NAMESPACE_ID.toString() ) );
1969 artifactMetadata.setSize( getAsLongValue( columnSlice, SIZE.toString() ) );
1970 artifactMetadata.setId( getStringValue( columnSlice, ID.toString() ) );
1971 artifactMetadata.setFileLastModified( getAsLongValue( columnSlice, FILE_LAST_MODIFIED.toString() ) );
1972 artifactMetadata.setMd5( getStringValue( columnSlice, MD5.toString() ) );
1973 artifactMetadata.setProject( getStringValue( columnSlice, PROJECT.toString() ) );
1974 artifactMetadata.setProjectVersion( getStringValue( columnSlice, PROJECT_VERSION.toString() ) );
1975 artifactMetadata.setRepositoryId( getStringValue( columnSlice, REPOSITORY_NAME.toString() ) );
1976 artifactMetadata.setSha1( getStringValue( columnSlice, SHA1.toString() ) );
1977 artifactMetadata.setVersion( getStringValue( columnSlice, VERSION.toString() ) );
1978 Long whenGathered = getAsLongValue( columnSlice, WHEN_GATHERED.toString() );
1979 if ( whenGathered != null )
1981 artifactMetadata.setWhenGathered(ZonedDateTime.ofInstant(Instant.ofEpochMilli(whenGathered), STORAGE_TZ));
1983 artifactMetadata.setChecksums(mapChecksumsReverse(getChecksums(key)));
1984 return artifactMetadata;
1988 public List<ArtifactMetadata> getArtifactsByChecksum(RepositorySession session, final String repositoryId, final String checksum )
1989 throws MetadataRepositoryException
1992 // cql cannot run or in queries so running twice the query
1993 Map<String, ArtifactMetadata> artifactMetadataMap = new HashMap<>();
1995 RangeSlicesQuery<String, String, String> query = HFactory //
1996 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1997 .setColumnFamily( cassandraArchivaManager.getChecksumFamilyName()) //
1998 .setColumnNames(ARTIFACT_METADATA_MODEL_KEY); //
2000 query = query.addEqualsExpression( CHECKSUM_VALUE.toString(), checksum )
2001 .addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId );
2003 QueryResult<OrderedRows<String, String, String>> result = query.execute();
2005 List<String> artifactKeys = new ArrayList<>();
2006 for ( Row<String, String, String> row : result.get() )
2008 ColumnSlice<String, String> columnSlice = row.getColumnSlice();
2010 artifactKeys.add(columnSlice.getColumnByName(ARTIFACT_METADATA_MODEL_KEY).getValue());
2014 for (String key : artifactKeys) {
2016 .createRangeSlicesQuery(keyspace, ss, ss, ss) //
2017 .setColumnFamily(cassandraArchivaManager.getArtifactMetadataFamilyName()) //
2018 .setColumnNames(NAMESPACE_ID.toString(), SIZE.toString(), ID.toString(), FILE_LAST_MODIFIED.toString(), MD5.toString(), PROJECT.toString(), PROJECT_VERSION.toString(),
2019 REPOSITORY_NAME.toString(), VERSION.toString(), WHEN_GATHERED.toString(), SHA1.toString())
2021 result = query.execute();
2023 for (Row<String, String, String> row : result.get()) {
2024 ColumnSlice<String, String> columnSlice = row.getColumnSlice();
2026 artifactMetadataMap.put(row.getKey(), mapArtifactMetadataStringColumnSlice(key, columnSlice));
2030 return new ArrayList(artifactMetadataMap.values());
2034 * Project version and artifact level metadata are stored in the same place, no distinctions in Cassandra
2035 * implementation, just calls {@link MetadataRepository#getArtifactsByAttribute(RepositorySession, String, String, String)}
2038 public List<ArtifactMetadata> getArtifactsByProjectVersionFacet( RepositorySession session, String key, String value, String repositoryId )
2039 throws MetadataRepositoryException
2041 return this.getArtifactsByAttribute( session, key, value, repositoryId );
2045 public List<ArtifactMetadata> getArtifactsByAttribute( RepositorySession session, String key, String value, String repositoryId )
2046 throws MetadataRepositoryException
2048 RangeSlicesQuery<String, String, String> query =
2049 HFactory.createRangeSlicesQuery( keyspace, ss, ss, ss ) //
2050 .setColumnFamily( cassandraArchivaManager.getMetadataFacetFamilyName() ) //
2051 .setColumnNames( MetadataFacetModel.COLUMNS ) //
2052 .addEqualsExpression( VALUE.toString(), value );
2056 query.addEqualsExpression( KEY.toString(), key ); //
2058 if ( repositoryId != null )
2060 query.addEqualsExpression( "repositoryName", repositoryId );
2063 QueryResult<OrderedRows<String, String, String>> metadataFacetResult = query.execute();
2064 if ( metadataFacetResult.get() == null || metadataFacetResult.get().getCount() < 1 )
2066 return Collections.emptyList();
2069 List<ArtifactMetadata> artifactMetadatas = new LinkedList<ArtifactMetadata>();
2071 // TODO doing multiple queries, there should be a way to get all the artifactMetadatas for any number of
2073 for ( Row<String, String, String> row : metadataFacetResult.get() )
2075 QueryResult<OrderedRows<String, String, String>> artifactMetadataResult =
2076 HFactory.createRangeSlicesQuery( keyspace, ss, ss, ss ) //
2077 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataFamilyName() ) //
2078 .setColumnNames( ArtifactMetadataModel.COLUMNS ) //
2079 .setRowCount( Integer.MAX_VALUE ) //
2080 .addEqualsExpression( REPOSITORY_NAME.toString(),
2081 getStringValue( row.getColumnSlice(), REPOSITORY_NAME ) ) //
2082 .addEqualsExpression( NAMESPACE_ID.toString(), getStringValue( row.getColumnSlice(), NAMESPACE_ID ) ) //
2083 .addEqualsExpression( PROJECT.toString(), getStringValue( row.getColumnSlice(), PROJECT_ID ) ) //
2084 .addEqualsExpression( PROJECT_VERSION.toString(),
2085 getStringValue( row.getColumnSlice(), PROJECT_VERSION ) ) //
2088 if ( artifactMetadataResult.get() == null || artifactMetadataResult.get().getCount() < 1 )
2090 return Collections.emptyList();
2093 for ( Row<String, String, String> artifactMetadataRow : artifactMetadataResult.get() )
2095 String artifactKey = artifactMetadataRow.getKey();
2096 artifactMetadatas.add( mapArtifactMetadataStringColumnSlice( artifactKey, artifactMetadataRow.getColumnSlice() ) );
2100 return mapArtifactFacetToArtifact( metadataFacetResult, artifactMetadatas );
2104 public List<ArtifactMetadata> getArtifactsByProjectVersionAttribute( RepositorySession session, String key, String value, String repositoryId )
2105 throws MetadataRepositoryException
2107 QueryResult<OrderedRows<String, String, String>> result =
2108 HFactory.createRangeSlicesQuery( keyspace, ss, ss, ss ) //
2109 .setColumnFamily( cassandraArchivaManager.getProjectVersionMetadataFamilyName() ) //
2110 .setColumnNames( PROJECT_ID.toString(), REPOSITORY_NAME.toString(), NAMESPACE_ID.toString(),
2111 PROJECT_VERSION.toString() ) //
2112 .addEqualsExpression( key, value ) //
2115 int count = result.get().getCount();
2119 return Collections.emptyList();
2122 List<ArtifactMetadata> artifacts = new LinkedList<ArtifactMetadata>();
2124 for ( Row<String, String, String> row : result.get() )
2126 // TODO doing multiple queries, there should be a way to get all the artifactMetadatas for any number of
2130 artifacts.addAll( getArtifacts( session,
2131 getStringValue( row.getColumnSlice(), REPOSITORY_NAME ),
2132 getStringValue( row.getColumnSlice(), NAMESPACE_ID ),
2133 getStringValue( row.getColumnSlice(), PROJECT_ID ), getStringValue( row.getColumnSlice(), PROJECT_VERSION ) ) );
2135 catch ( MetadataResolutionException e )
2138 throw new IllegalStateException( e );
2145 public void removeArtifact( RepositorySession session, final String repositoryId, final String namespace, final String project,
2146 final String version, final String id )
2147 throws MetadataRepositoryException
2149 logger.debug( "removeTimestampedArtifact repositoryId: '{}', namespace: '{}', project: '{}', version: '{}', id: '{}'",
2150 repositoryId, namespace, project, version, id );
2152 new ArtifactMetadataModel.KeyBuilder().withRepositoryId( repositoryId ).withNamespace( namespace ).withId(
2153 id ).withProjectVersion( version ).withProject( project ).build();
2155 this.artifactMetadataTemplate.deleteRow( key );
2157 key = new ProjectVersionMetadataModel.KeyBuilder() //
2158 .withRepository( repositoryId ) //
2159 .withNamespace( namespace ) //
2160 .withProjectId( project ) //
2161 .withProjectVersion( version ) //
2165 this.projectVersionMetadataTemplate.deleteRow( key );
2169 public void removeTimestampedArtifact( RepositorySession session, ArtifactMetadata artifactMetadata, String baseVersion )
2170 throws MetadataRepositoryException
2172 logger.debug( "removeTimestampedArtifact repositoryId: '{}', namespace: '{}', project: '{}', version: '{}', id: '{}'",
2173 artifactMetadata.getRepositoryId(), artifactMetadata.getNamespace(),
2174 artifactMetadata.getProject(), baseVersion, artifactMetadata.getId() );
2176 new ArtifactMetadataModel.KeyBuilder().withRepositoryId( artifactMetadata.getRepositoryId() ).withNamespace(
2177 artifactMetadata.getNamespace() ).withId( artifactMetadata.getId() ).withProjectVersion(
2178 baseVersion ).withProject( artifactMetadata.getProject() ).build();
2180 this.artifactMetadataTemplate.deleteRow( key );
2185 public void removeFacetFromArtifact( RepositorySession session, final String repositoryId, final String namespace, final String project,
2186 final String version, final MetadataFacet metadataFacet )
2187 throws MetadataRepositoryException
2190 RangeSlicesQuery<String, String, String> query = HFactory //
2191 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
2192 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataFamilyName() ) //
2193 .setColumnNames( NAMESPACE_ID.toString() ); //
2195 query = query.addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId ) //
2196 .addEqualsExpression( NAMESPACE_ID.toString(), namespace ) //
2197 .addEqualsExpression( PROJECT.toString(), project ) //
2198 .addEqualsExpression( VERSION.toString(), version );
2200 QueryResult<OrderedRows<String, String, String>> result = query.execute();
2202 for ( Row<String, String, String> row : result.get() )
2204 this.artifactMetadataTemplate.deleteRow( row.getKey() );
2210 public List<ArtifactMetadata> getArtifacts( RepositorySession session, final String repositoryId )
2211 throws MetadataRepositoryException
2214 RangeSlicesQuery<String, String, String> query = HFactory //
2215 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
2216 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataFamilyName() ) //
2217 .setColumnNames( ArtifactMetadataModel.COLUMNS ); //
2219 query = query.addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId );
2221 QueryResult<OrderedRows<String, String, String>> result = query.execute();
2225 List<ArtifactMetadata> artifactMetadatas = new ArrayList<>( result.get().getCount() );
2227 for ( Row<String, String, String> row : result.get() )
2229 String key = row.getKey();
2230 ColumnSlice<String, String> columnSlice = row.getColumnSlice();
2231 artifactMetadatas.add( mapArtifactMetadataStringColumnSlice( key, columnSlice ) );
2235 return artifactMetadatas;
2240 public Collection<ProjectVersionReference> getProjectReferences( RepositorySession session, String repoId, String namespace, String projectId,
2241 String projectVersion )
2242 throws MetadataResolutionException
2244 QueryResult<OrderedRows<String, String, String>> result = HFactory //
2245 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
2246 .setColumnFamily( cassandraArchivaManager.getDependencyFamilyName() ) //
2247 .setColumnNames( "projectVersionMetadataModel.key" ) //
2248 .addEqualsExpression( REPOSITORY_NAME.toString(), repoId ) //
2249 .addEqualsExpression( GROUP_ID.toString(), namespace ) //
2250 .addEqualsExpression( ARTIFACT_ID.toString(), projectId ) //
2251 .addEqualsExpression( VERSION.toString(), projectVersion ) //
2254 List<String> dependenciesIds = new ArrayList<>( result.get().getCount() );
2256 for ( Row<String, String, String> row : result.get().getList() )
2258 dependenciesIds.add( getStringValue( row.getColumnSlice(), "projectVersionMetadataModel.key" ) );
2261 List<ProjectVersionReference> references = new ArrayList<>( result.get().getCount() );
2263 for ( String key : dependenciesIds )
2265 ColumnFamilyResult<String, String> columnFamilyResult =
2266 this.projectVersionMetadataTemplate.queryColumns( key );
2267 references.add( new ProjectVersionReference( ProjectVersionReference.ReferenceType.DEPENDENCY, //
2268 columnFamilyResult.getString( PROJECT_ID.toString() ), //
2269 columnFamilyResult.getString( NAMESPACE_ID.toString() ), //
2270 columnFamilyResult.getString( PROJECT_VERSION.toString() ) ) );
2277 public void removeProjectVersion( RepositorySession session, final String repoId, final String namespace, final String projectId,
2278 final String projectVersion )
2279 throws MetadataRepositoryException
2282 QueryResult<OrderedRows<String, String, String>> result = HFactory //
2283 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
2284 .setColumnFamily( cassandraArchivaManager.getProjectVersionMetadataFamilyName() ) //
2285 .setColumnNames( VERSION.toString() ) //
2286 .addEqualsExpression( REPOSITORY_NAME.toString(), repoId ) //
2287 .addEqualsExpression( NAMESPACE_ID.toString(), namespace ) //
2288 .addEqualsExpression( PROJECT_ID.toString(), projectId ) //
2289 .addEqualsExpression( PROJECT_VERSION.toString(), projectVersion ) //
2292 for ( Row<String, String, String> row : result.get().getList() )
2294 this.projectVersionMetadataTemplate.deleteRow( row.getKey() );
2295 removeMailingList( row.getKey() );
2296 removeLicenses( row.getKey() );
2297 removeDependencies( row.getKey() );
2300 RangeSlicesQuery<String, String, String> query = HFactory //
2301 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
2302 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataFamilyName() ) //
2303 .setColumnNames( NAMESPACE_ID.toString() ); //
2305 query = query.addEqualsExpression( REPOSITORY_NAME.toString(), repoId ) //
2306 .addEqualsExpression( NAMESPACE_ID.toString(), namespace ) //
2307 .addEqualsExpression( PROJECT.toString(), projectId ) //
2308 .addEqualsExpression( PROJECT_VERSION.toString(), projectVersion );
2310 result = query.execute();
2312 for ( Row<String, String, String> row : result.get() )
2314 this.artifactMetadataTemplate.deleteRow( row.getKey() );
2320 public Collection<ArtifactMetadata> getArtifacts( RepositorySession session, final String repoId, final String namespace,
2321 final String projectId, final String projectVersion )
2322 throws MetadataResolutionException
2325 QueryResult<OrderedRows<String, String, String>> result =
2326 HFactory.createRangeSlicesQuery( keyspace, ss, ss, ss ) //
2327 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataFamilyName() ) //
2328 .setColumnNames( ArtifactMetadataModel.COLUMNS )//
2329 .setRowCount( Integer.MAX_VALUE ) //
2330 .addEqualsExpression( REPOSITORY_NAME.toString(), repoId ) //
2331 .addEqualsExpression( NAMESPACE_ID.toString(), namespace ) //
2332 .addEqualsExpression( PROJECT.toString(), projectId ) //
2333 .addEqualsExpression( PROJECT_VERSION.toString(), projectVersion ) //
2336 if ( result.get() == null || result.get().getCount() < 1 )
2338 return Collections.emptyList();
2341 List<ArtifactMetadata> artifactMetadatas = new ArrayList<>( result.get().getCount() );
2343 for ( Row<String, String, String> row : result.get() )
2345 String key = row.getKey();
2346 artifactMetadatas.add( mapArtifactMetadataStringColumnSlice( key, row.getColumnSlice() ) );
2349 result = HFactory.createRangeSlicesQuery( keyspace, ss, ss, ss ) //
2350 .setColumnFamily( cassandraArchivaManager.getMetadataFacetFamilyName() ) //
2351 .setColumnNames( MetadataFacetModel.COLUMNS ) //
2352 .setRowCount( Integer.MAX_VALUE ) //
2353 .addEqualsExpression( REPOSITORY_NAME.toString(), repoId ) //
2354 .addEqualsExpression( NAMESPACE_ID.toString(), namespace ) //
2355 .addEqualsExpression( PROJECT_ID.toString(), projectId ) //
2356 .addEqualsExpression( PROJECT_VERSION.toString(), projectVersion ) //
2359 return mapArtifactFacetToArtifact(result, artifactMetadatas);
2363 * Attach metadata to each of the ArtifactMetadata objects
2365 private List<ArtifactMetadata> mapArtifactFacetToArtifact( QueryResult<OrderedRows<String, String, String>> result, List<ArtifactMetadata> artifactMetadatas) {
2366 if ( result.get() == null || result.get().getCount() < 1 )
2368 return artifactMetadatas;
2371 final List<MetadataFacetModel> metadataFacetModels = new ArrayList<>( result.get().getCount() );
2373 for ( Row<String, String, String> row : result.get() )
2375 ColumnSlice<String, String> columnSlice = row.getColumnSlice();
2376 MetadataFacetModel metadataFacetModel = new MetadataFacetModel();
2377 metadataFacetModel.setFacetId( getStringValue( columnSlice, FACET_ID.toString() ) );
2378 metadataFacetModel.setName( getStringValue( columnSlice, NAME.toString() ) );
2379 metadataFacetModel.setValue( getStringValue( columnSlice, VALUE.toString() ) );
2380 metadataFacetModel.setKey( getStringValue( columnSlice, KEY.toString() ) );
2381 metadataFacetModel.setProjectVersion( getStringValue( columnSlice, PROJECT_VERSION.toString() ) );
2382 metadataFacetModels.add( metadataFacetModel );
2385 // rebuild MetadataFacet for artifacts
2387 for ( final ArtifactMetadata artifactMetadata : artifactMetadatas )
2389 Iterable<MetadataFacetModel> metadataFacetModelIterable =
2390 Iterables.filter( metadataFacetModels, new Predicate<MetadataFacetModel>()
2393 public boolean apply( MetadataFacetModel metadataFacetModel )
2395 if ( metadataFacetModel != null )
2397 return StringUtils.equals( artifactMetadata.getVersion(),
2398 metadataFacetModel.getProjectVersion() );
2403 Iterator<MetadataFacetModel> iterator = metadataFacetModelIterable.iterator();
2404 Map<String, List<MetadataFacetModel>> metadataFacetValuesPerFacetId = new HashMap<>();
2405 while ( iterator.hasNext() )
2407 MetadataFacetModel metadataFacetModel = iterator.next();
2408 List<MetadataFacetModel> values = metadataFacetValuesPerFacetId.get( metadataFacetModel.getName() );
2409 if ( values == null )
2411 values = new ArrayList<>();
2412 metadataFacetValuesPerFacetId.put( metadataFacetModel.getFacetId(), values );
2414 values.add( metadataFacetModel );
2418 for ( Map.Entry<String, List<MetadataFacetModel>> entry : metadataFacetValuesPerFacetId.entrySet() )
2420 MetadataFacetFactory metadataFacetFactory = getFacetFactory( entry.getKey() );
2421 if ( metadataFacetFactory != null )
2423 List<MetadataFacetModel> facetModels = entry.getValue();
2424 if ( !facetModels.isEmpty() )
2426 MetadataFacet metadataFacet = metadataFacetFactory.createMetadataFacet();
2427 Map<String, String> props = new HashMap<>( facetModels.size() );
2428 for ( MetadataFacetModel metadataFacetModel : facetModels )
2430 props.put( metadataFacetModel.getKey(), metadataFacetModel.getValue() );
2432 metadataFacet.fromProperties( props );
2433 artifactMetadata.addFacet( metadataFacet );
2439 return artifactMetadatas;
2444 throws MetadataRepositoryException
2446 logger.trace( "close" );
2450 private static class ModelMapperHolder
2452 private static ModelMapper MODEL_MAPPER = new ModelMapper();
2455 protected ModelMapper getModelMapper()
2457 return ModelMapperHolder.MODEL_MAPPER;
2461 * This implementation just calls getArtifactsByAttribute( null, text, repositoryId ). We can't search artifacts by
2465 public List<ArtifactMetadata> searchArtifacts( final RepositorySession session, final String repositoryId,
2466 final String text, final boolean exact )
2467 throws MetadataRepositoryException
2469 return this.getArtifactsByAttribute( session, null, text, repositoryId );
2473 * The exact parameter is ignored as we can't do non exact searches in Cassandra
2476 public List<ArtifactMetadata> searchArtifacts( final RepositorySession session, final String repositoryId,
2477 final String key, final String text, final boolean exact )
2478 throws MetadataRepositoryException
2481 List<ArtifactMetadata> artifacts = new LinkedList<ArtifactMetadata>();
2482 artifacts.addAll( this.getArtifactsByAttribute( session, key, text, repositoryId ) );
2483 artifacts.addAll( this.getArtifactsByProjectVersionAttribute( session, key, text, repositoryId ) );
2488 public Stream<ArtifactMetadata> getArtifactStream( final RepositorySession session, final String repositoryId,
2489 final QueryParameter queryParameter ) throws MetadataResolutionException
2491 RangeSlicesQuery<String, String, String> query = HFactory //
2492 .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
2493 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataFamilyName( ) ) //
2494 .setColumnNames( ArtifactMetadataModel.COLUMNS ); //
2496 query = query.addEqualsExpression( REPOSITORY_NAME.toString(), repositoryId );
2498 QueryResult<OrderedRows<String, String, String>> result = query.execute();
2502 return StreamSupport.stream( createResultSpliterator( result, ( Row<String, String, String> row, ArtifactMetadata last ) ->
2503 mapArtifactMetadataStringColumnSlice( row.getKey( ), row.getColumnSlice( ) ) ), false )
2504 .skip( queryParameter.getOffset( ) ).limit( queryParameter.getLimit( ) );
2506 catch ( MetadataRepositoryException e )
2508 throw new MetadataResolutionException( e.getMessage( ), e );
2513 public Stream<ArtifactMetadata> getArtifactStream( final RepositorySession session, final String repoId,
2514 final String namespace, final String projectId, final String projectVersion,
2515 final QueryParameter queryParameter ) throws MetadataResolutionException
2517 // Currently we have to align the facets with the artifacts, which means querying artifacts, querying facets and combining them.
2518 // I so no stream friendly way to do this, so we just use the collection based method and return the stream.
2519 // TODO: Maybe we can query the facets for each artifact separately, but not sure, if this affects performance significantly
2520 // We need some data to verify this.
2521 return getArtifacts( session, repoId, namespace, projectId, projectVersion ).stream( ).skip( queryParameter.getOffset( ) ).limit( queryParameter.getLimit( ) );