Search in sources :

Example 36 with CqlSession

use of com.datastax.oss.driver.api.core.CqlSession in project archiva by apache.

the class CassandraMetadataRepository method removeProject.

@Override
public void removeProject(RepositorySession repositorySession, final String repositoryId, final String namespaceId, final String projectId) throws MetadataRepositoryException {
    String key = // 
    new Project.KeyBuilder().withProjectId(// 
    projectId).withNamespace(// 
    new Namespace(namespaceId, new Repository(repositoryId))).build();
    CqlSession session = cassandraArchivaManager.getSession();
    {
        String table = cassandraArchivaManager.getProjectFamilyName();
        Delete delete = deleteFrom(table).whereColumn(DEFAULT_PRIMARY_KEY).isEqualTo(literal(key));
        session.execute(delete.build());
        table = cassandraArchivaManager.getProjectVersionMetadataFamilyName();
        Select query = selectFrom(table).columns(DEFAULT_PRIMARY_KEY, PROJECT_ID.toString()).whereColumn(REPOSITORY_NAME.toString()).isEqualTo(literal(repositoryId)).whereColumn(NAMESPACE_ID.toString()).isEqualTo(literal(namespaceId)).whereColumn(PROJECT_ID.toString()).isEqualTo(literal(projectId)).allowFiltering();
        ResultSet result = session.execute(query.build());
        result.forEach(row -> removeMailingList(row.get(DEFAULT_PRIMARY_KEY, String.class)));
        List<String> tables = Arrays.asList(cassandraArchivaManager.getProjectVersionMetadataFamilyName(), cassandraArchivaManager.getArtifactMetadataFamilyName());
        for (String dTable : tables) {
            Select deleteRows = selectFrom(dTable).column(DEFAULT_PRIMARY_KEY).whereColumn(REPOSITORY_NAME.toString()).isEqualTo(literal(repositoryId)).whereColumn(NAMESPACE_ID.toString()).isEqualTo(literal(namespaceId)).whereColumn(PROJECT_ID.toString()).isEqualTo(literal(projectId)).allowFiltering();
            result = session.execute(deleteRows.build());
            StreamSupport.stream(result.spliterator(), false).map(row -> row.getString(DEFAULT_PRIMARY_KEY)).forEach(delKey -> session.execute(deleteFrom(dTable).column(PROJECT_ID.toString()).whereColumn(DEFAULT_PRIMARY_KEY).isEqualTo(literal(delKey)).build()));
        }
    }
}
Also used : Delete(com.datastax.oss.driver.api.querybuilder.delete.Delete) QueryBuilder(com.datastax.oss.driver.api.querybuilder.QueryBuilder) MailingList(org.apache.archiva.metadata.model.MailingList) Arrays(java.util.Arrays) ZonedDateTime(java.time.ZonedDateTime) BiFunction(java.util.function.BiFunction) Insert(com.datastax.oss.driver.api.querybuilder.insert.Insert) RepositorySession(org.apache.archiva.metadata.repository.RepositorySession) LoggerFactory(org.slf4j.LoggerFactory) Delete(com.datastax.oss.driver.api.querybuilder.delete.Delete) UpdateWithAssignments(com.datastax.oss.driver.api.querybuilder.update.UpdateWithAssignments) StringUtils(org.apache.commons.lang3.StringUtils) STORAGE_TZ(org.apache.archiva.metadata.model.ModelInfo.STORAGE_TZ) MetadataRepository(org.apache.archiva.metadata.repository.MetadataRepository) Map(java.util.Map) ProjectMetadata(org.apache.archiva.metadata.model.ProjectMetadata) Select(com.datastax.oss.driver.api.querybuilder.select.Select) DEFAULT_PRIMARY_KEY(org.apache.archiva.metadata.repository.cassandra.CassandraArchivaManager.DEFAULT_PRIMARY_KEY) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata) ColumnDefinition(com.datastax.oss.driver.api.core.cql.ColumnDefinition) ProjectVersionReference(org.apache.archiva.metadata.model.ProjectVersionReference) ColumnNames(org.apache.archiva.metadata.repository.cassandra.model.ColumnNames) Update(com.datastax.oss.driver.api.querybuilder.update.Update) CiManagement(org.apache.archiva.metadata.model.CiManagement) IssueManagement(org.apache.archiva.metadata.model.IssueManagement) UUID(java.util.UUID) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) MetadataService(org.apache.archiva.metadata.repository.MetadataService) List(java.util.List) Stream(java.util.stream.Stream) License(org.apache.archiva.metadata.model.License) Dependency(org.apache.archiva.metadata.model.Dependency) MetadataFacetFactory(org.apache.archiva.metadata.model.MetadataFacetFactory) UpdateStart(com.datastax.oss.driver.api.querybuilder.update.UpdateStart) Spliterator(java.util.Spliterator) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) Repository(org.apache.archiva.metadata.repository.cassandra.model.Repository) AbstractMetadataRepository(org.apache.archiva.metadata.repository.AbstractMetadataRepository) MetadataFacet(org.apache.archiva.metadata.model.MetadataFacet) ClusteringOrder(com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder) HashMap(java.util.HashMap) Namespace(org.apache.archiva.metadata.repository.cassandra.model.Namespace) ArtifactMetadataModel(org.apache.archiva.metadata.repository.cassandra.model.ArtifactMetadataModel) ArrayList(java.util.ArrayList) ModelMapper(org.modelmapper.ModelMapper) ProjectVersionMetadata(org.apache.archiva.metadata.model.ProjectVersionMetadata) CqlSession(com.datastax.oss.driver.api.core.CqlSession) QueryParameter(org.apache.archiva.metadata.QueryParameter) ProjectVersionMetadataModel(org.apache.archiva.metadata.repository.cassandra.model.ProjectVersionMetadataModel) ChecksumAlgorithm(org.apache.archiva.checksum.ChecksumAlgorithm) Project(org.apache.archiva.metadata.repository.cassandra.model.Project) StreamSupport(java.util.stream.StreamSupport) Organization(org.apache.archiva.metadata.model.Organization) FacetedMetadata(org.apache.archiva.metadata.model.FacetedMetadata) Scm(org.apache.archiva.metadata.model.Scm) LinkedList(java.util.LinkedList) Row(com.datastax.oss.driver.api.core.cql.Row) RegularInsert(com.datastax.oss.driver.api.querybuilder.insert.RegularInsert) Logger(org.slf4j.Logger) Properties(java.util.Properties) Iterator(java.util.Iterator) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) Consumer(java.util.function.Consumer) MetadataResolutionException(org.apache.archiva.metadata.repository.MetadataResolutionException) MetadataFacetModel(org.apache.archiva.metadata.repository.cassandra.model.MetadataFacetModel) Comparator(java.util.Comparator) Project(org.apache.archiva.metadata.repository.cassandra.model.Project) MetadataRepository(org.apache.archiva.metadata.repository.MetadataRepository) Repository(org.apache.archiva.metadata.repository.cassandra.model.Repository) AbstractMetadataRepository(org.apache.archiva.metadata.repository.AbstractMetadataRepository) Select(com.datastax.oss.driver.api.querybuilder.select.Select) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) MailingList(org.apache.archiva.metadata.model.MailingList) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Namespace(org.apache.archiva.metadata.repository.cassandra.model.Namespace)

Example 37 with CqlSession

use of com.datastax.oss.driver.api.core.CqlSession in project archiva by apache.

the class CassandraMetadataRepository method recordLicenses.

protected void recordLicenses(String projectVersionMetadataKey, List<License> licenses) {
    if (licenses == null || licenses.isEmpty()) {
        return;
    }
    String table = cassandraArchivaManager.getLicenseFamilyName();
    CqlSession session = cassandraArchivaManager.getSession();
    {
        for (License license : licenses) {
            // we don't care about the key as the real used one with the projectVersionMetadata
            String keyLicense = UUID.randomUUID().toString();
            RegularInsert insert = insertInto(table).value(DEFAULT_PRIMARY_KEY, literal(keyLicense)).value("\"projectVersionMetadataModel.key\"", literal(projectVersionMetadataKey)).value(NAME.toString(), literal(license.getName())).value(URL.toString(), literal(license.getUrl()));
            session.execute(insert.build());
        }
    }
}
Also used : License(org.apache.archiva.metadata.model.License) RegularInsert(com.datastax.oss.driver.api.querybuilder.insert.RegularInsert) CqlSession(com.datastax.oss.driver.api.core.CqlSession)

Example 38 with CqlSession

use of com.datastax.oss.driver.api.core.CqlSession in project archiva by apache.

the class CassandraMetadataRepository method getArtifactsByDateRange.

@Override
public List<ArtifactMetadata> getArtifactsByDateRange(RepositorySession repositorySession, final String repositoryId, final ZonedDateTime startTime, final ZonedDateTime endTime, QueryParameter queryParameter) throws MetadataRepositoryException {
    final String table = cassandraArchivaManager.getArtifactMetadataFamilyName();
    CqlSession session = cassandraArchivaManager.getSession();
    {
        long start = startTime == null ? Long.MIN_VALUE : startTime.toInstant().toEpochMilli();
        long end = endTime == null ? Long.MAX_VALUE : endTime.toInstant().toEpochMilli();
        Select query = selectFrom(table).all().whereColumn(REPOSITORY_NAME.toString()).isEqualTo(literal(repositoryId)).whereColumn(WHEN_GATHERED.toString()).isGreaterThanOrEqualTo(literal(start)).whereColumn(WHEN_GATHERED.toString()).isLessThanOrEqualTo(literal(end)).allowFiltering();
        ResultSet result = session.execute(query.build());
        return StreamSupport.stream(result.spliterator(), false).map(this::mapArtifactMetadata).collect(Collectors.toList());
    }
}
Also used : Select(com.datastax.oss.driver.api.querybuilder.select.Select) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) CqlSession(com.datastax.oss.driver.api.core.CqlSession)

Example 39 with CqlSession

use of com.datastax.oss.driver.api.core.CqlSession in project archiva by apache.

the class CassandraMetadataRepository method removeMetadataFacets.

@Override
public void removeMetadataFacets(RepositorySession repositorySession, final String repositoryId, final String facetId) throws MetadataRepositoryException {
    final String table = cassandraArchivaManager.getMetadataFacetFamilyName();
    CqlSession session = cassandraArchivaManager.getSession();
    {
        Select deleteRows = selectFrom(table).column(DEFAULT_PRIMARY_KEY).whereColumn(REPOSITORY_NAME.toString()).isEqualTo(literal(repositoryId)).whereColumn(FACET_ID.toString()).isEqualTo(literal(facetId)).allowFiltering();
        ResultSet result = session.execute(deleteRows.build());
        StreamSupport.stream(result.spliterator(), false).map(row -> row.getString(DEFAULT_PRIMARY_KEY)).distinct().forEach(delKey -> session.execute(deleteFrom(table).whereColumn(DEFAULT_PRIMARY_KEY).isEqualTo(literal(delKey)).build()));
    }
}
Also used : QueryBuilder(com.datastax.oss.driver.api.querybuilder.QueryBuilder) MailingList(org.apache.archiva.metadata.model.MailingList) Arrays(java.util.Arrays) ZonedDateTime(java.time.ZonedDateTime) BiFunction(java.util.function.BiFunction) Insert(com.datastax.oss.driver.api.querybuilder.insert.Insert) RepositorySession(org.apache.archiva.metadata.repository.RepositorySession) LoggerFactory(org.slf4j.LoggerFactory) Delete(com.datastax.oss.driver.api.querybuilder.delete.Delete) UpdateWithAssignments(com.datastax.oss.driver.api.querybuilder.update.UpdateWithAssignments) StringUtils(org.apache.commons.lang3.StringUtils) STORAGE_TZ(org.apache.archiva.metadata.model.ModelInfo.STORAGE_TZ) MetadataRepository(org.apache.archiva.metadata.repository.MetadataRepository) Map(java.util.Map) ProjectMetadata(org.apache.archiva.metadata.model.ProjectMetadata) Select(com.datastax.oss.driver.api.querybuilder.select.Select) DEFAULT_PRIMARY_KEY(org.apache.archiva.metadata.repository.cassandra.CassandraArchivaManager.DEFAULT_PRIMARY_KEY) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata) ColumnDefinition(com.datastax.oss.driver.api.core.cql.ColumnDefinition) ProjectVersionReference(org.apache.archiva.metadata.model.ProjectVersionReference) ColumnNames(org.apache.archiva.metadata.repository.cassandra.model.ColumnNames) Update(com.datastax.oss.driver.api.querybuilder.update.Update) CiManagement(org.apache.archiva.metadata.model.CiManagement) IssueManagement(org.apache.archiva.metadata.model.IssueManagement) UUID(java.util.UUID) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) MetadataService(org.apache.archiva.metadata.repository.MetadataService) List(java.util.List) Stream(java.util.stream.Stream) License(org.apache.archiva.metadata.model.License) Dependency(org.apache.archiva.metadata.model.Dependency) MetadataFacetFactory(org.apache.archiva.metadata.model.MetadataFacetFactory) UpdateStart(com.datastax.oss.driver.api.querybuilder.update.UpdateStart) Spliterator(java.util.Spliterator) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) Repository(org.apache.archiva.metadata.repository.cassandra.model.Repository) AbstractMetadataRepository(org.apache.archiva.metadata.repository.AbstractMetadataRepository) MetadataFacet(org.apache.archiva.metadata.model.MetadataFacet) ClusteringOrder(com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder) HashMap(java.util.HashMap) Namespace(org.apache.archiva.metadata.repository.cassandra.model.Namespace) ArtifactMetadataModel(org.apache.archiva.metadata.repository.cassandra.model.ArtifactMetadataModel) ArrayList(java.util.ArrayList) ModelMapper(org.modelmapper.ModelMapper) ProjectVersionMetadata(org.apache.archiva.metadata.model.ProjectVersionMetadata) CqlSession(com.datastax.oss.driver.api.core.CqlSession) QueryParameter(org.apache.archiva.metadata.QueryParameter) ProjectVersionMetadataModel(org.apache.archiva.metadata.repository.cassandra.model.ProjectVersionMetadataModel) ChecksumAlgorithm(org.apache.archiva.checksum.ChecksumAlgorithm) Project(org.apache.archiva.metadata.repository.cassandra.model.Project) StreamSupport(java.util.stream.StreamSupport) Organization(org.apache.archiva.metadata.model.Organization) FacetedMetadata(org.apache.archiva.metadata.model.FacetedMetadata) Scm(org.apache.archiva.metadata.model.Scm) LinkedList(java.util.LinkedList) Row(com.datastax.oss.driver.api.core.cql.Row) RegularInsert(com.datastax.oss.driver.api.querybuilder.insert.RegularInsert) Logger(org.slf4j.Logger) Properties(java.util.Properties) Iterator(java.util.Iterator) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) Consumer(java.util.function.Consumer) MetadataResolutionException(org.apache.archiva.metadata.repository.MetadataResolutionException) MetadataFacetModel(org.apache.archiva.metadata.repository.cassandra.model.MetadataFacetModel) Comparator(java.util.Comparator) Select(com.datastax.oss.driver.api.querybuilder.select.Select) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) CqlSession(com.datastax.oss.driver.api.core.CqlSession)

Example 40 with CqlSession

use of com.datastax.oss.driver.api.core.CqlSession in project archiva by apache.

the class CassandraMetadataRepository method getMetadataFacet.

@Override
public <T extends MetadataFacet> T getMetadataFacet(RepositorySession repositorySession, final String repositoryId, final Class<T> facetClazz, final String name) throws MetadataRepositoryException {
    final MetadataFacetFactory<T> metadataFacetFactory = getFacetFactory(facetClazz);
    if (metadataFacetFactory == null) {
        return null;
    }
    final String facetId = metadataFacetFactory.getFacetId();
    final String table = cassandraArchivaManager.getMetadataFacetFamilyName();
    T metadataFacet = metadataFacetFactory.createMetadataFacet(repositoryId, name);
    CqlSession session = cassandraArchivaManager.getSession();
    {
        Select query = selectFrom(table).column(KEY.toString()).column(VALUE.toString()).whereColumn(REPOSITORY_NAME.toString()).isEqualTo(literal(repositoryId)).whereColumn(FACET_ID.toString()).isEqualTo(literal(facetId)).whereColumn(NAME.toString()).isEqualTo(literal(name)).allowFiltering();
        ResultSet result = session.execute(query.build());
        if (result.getAvailableWithoutFetching() == 0) {
            return null;
        }
        Map<String, String> props = StreamSupport.stream(result.spliterator(), false).filter(row -> !row.isNull(KEY.toString())).collect(Collectors.toMap(row -> row.getString(KEY.toString()), row -> row.getString(VALUE.toString())));
        metadataFacet.fromProperties(props);
        return metadataFacet;
    }
}
Also used : Select(com.datastax.oss.driver.api.querybuilder.select.Select) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

CqlSession (com.datastax.oss.driver.api.core.CqlSession)523 Test (org.junit.Test)284 ResultSet (com.datastax.oss.driver.api.core.cql.ResultSet)111 Row (com.datastax.oss.driver.api.core.cql.Row)95 PreparedStatement (com.datastax.oss.driver.api.core.cql.PreparedStatement)73 DriverConfigLoader (com.datastax.oss.driver.api.core.config.DriverConfigLoader)60 Test (org.junit.jupiter.api.Test)51 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)49 List (java.util.List)45 BeforeClass (org.junit.BeforeClass)43 Node (com.datastax.oss.driver.api.core.metadata.Node)42 ArrayList (java.util.ArrayList)42 Map (java.util.Map)40 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)32 UUID (java.util.UUID)31 Metadata (com.datastax.oss.driver.api.core.metadata.Metadata)30 HashMap (java.util.HashMap)30 BoundStatement (com.datastax.oss.driver.api.core.cql.BoundStatement)27 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)25 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)24