]> source.dussan.org Git - archiva.git/commitdiff
start moving project data handling to hector
authorOlivier Lamy <olamy@apache.org>
Thu, 20 Mar 2014 23:22:50 +0000 (23:22 +0000)
committerOlivier Lamy <olamy@apache.org>
Thu, 20 Mar 2014 23:22:50 +0000 (23:22 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1579822 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/plugins/metadata-store-cassandra/src/main/java/org/apache/archiva/metadata/repository/cassandra/CassandraArchivaManager.java
archiva-modules/plugins/metadata-store-cassandra/src/main/java/org/apache/archiva/metadata/repository/cassandra/CassandraMetadataRepository.java
archiva-modules/plugins/metadata-store-cassandra/src/main/java/org/apache/archiva/metadata/repository/cassandra/DefaultCassandraArchivaManager.java
archiva-modules/plugins/metadata-store-cassandra/src/test/java/org/apache/archiva/metadata/repository/cassandra/RepositoriesNamespaceTest.java

index 88ed20ff937405d81ad94d76aa9bb47ce6824f39..142e134bc79f69166db05984766fcf4fe35cbd36 100644 (file)
@@ -41,6 +41,7 @@ import org.apache.archiva.metadata.repository.MetadataRepositoryException;
 import org.apache.archiva.metadata.repository.MetadataResolutionException;
 import org.apache.archiva.metadata.repository.cassandra.model.ArtifactMetadataModel;
 import org.apache.archiva.metadata.repository.cassandra.model.Namespace;
+import org.apache.archiva.metadata.repository.cassandra.model.Project;
 import org.apache.archiva.metadata.repository.cassandra.model.Repository;
 import org.apache.commons.lang.StringUtils;
 import org.modelmapper.ModelMapper;
@@ -497,36 +498,71 @@ public class CassandraMetadataRepository
     public void updateProject( String repositoryId, ProjectMetadata projectMetadata )
         throws MetadataRepositoryException
     {
-/*
-        // project exists ? if yes return
-        String projectKey = new Project.KeyBuilder().withProjectId( projectMetadata.getId() ).withNamespace(
-            new Namespace( projectMetadata.getNamespace(), new Repository( repositoryId ) ) ).build();
+        Keyspace keyspace = cassandraArchivaManager.getKeyspace();
 
-        Project project = getProjectEntityManager().get( projectKey );
-        if ( project != null )
+        QueryResult<OrderedRows<String, String, String>> result = HFactory //
+            .createRangeSlicesQuery( keyspace, //
+                                     StringSerializer.get(), //
+                                     StringSerializer.get(), //
+                                     StringSerializer.get() ) //
+            .setColumnFamily( cassandraArchivaManager.getProjectFamilyName() ) //
+            .setColumnNames( "projectId" ) //
+            .addEqualsExpression( "repositoryId", repositoryId ) //
+            .addEqualsExpression( "namespaceId", projectMetadata.getNamespace() ) //
+            .addEqualsExpression( "projectId", projectMetadata.getId() ) //
+            .execute();
+
+        // project exists ? if yes return
+        if ( result.get().getCount() > 0 )
         {
             return;
         }
 
-        String namespaceKey = new Namespace.KeyBuilder().withRepositoryId( repositoryId ).withNamespace(
-            projectMetadata.getNamespace() ).build();
-        Namespace namespace = getNamespaceEntityManager().get( namespaceKey );
-        if ( namespace == null )
-        {
-            namespace = updateOrAddNamespace( repositoryId, projectMetadata.getNamespace() );
-        }
+        Namespace namespace = updateOrAddNamespace( repositoryId, projectMetadata.getNamespace() );
 
-        project = new Project( projectKey, projectMetadata.getId(), namespace );
+        String key =
+            new Project.KeyBuilder().withProjectId( projectMetadata.getId() ).withNamespace( namespace ).build();
+
+        HFactory.createMutator( keyspace, StringSerializer.get() )
+            //  values
+            .addInsertion( key, //
+                           cassandraArchivaManager.getProjectFamilyName(), //
+                           CassandraUtils.column( "projectId", projectMetadata.getId() ) ) //
+            .addInsertion( key, //
+                           cassandraArchivaManager.getProjectFamilyName(), //
+                           CassandraUtils.column( "repositoryId", repositoryId ) ) //
+            .addInsertion( key, //
+                           cassandraArchivaManager.getProjectFamilyName(), //
+                           CassandraUtils.column( "namespaceId", projectMetadata.getNamespace() ) )//
+            .execute();
+    }
 
-        try
+    @Override
+    public Collection<String> getProjects( final String repoId, final String namespace )
+        throws MetadataResolutionException
+    {
+
+        Keyspace keyspace = cassandraArchivaManager.getKeyspace();
+
+        QueryResult<OrderedRows<String, String, String>> result = HFactory //
+            .createRangeSlicesQuery( keyspace, //
+                                     StringSerializer.get(), //
+                                     StringSerializer.get(), //
+                                     StringSerializer.get() ) //
+            .setColumnFamily( cassandraArchivaManager.getProjectFamilyName() ) //
+            .setColumnNames( "projectId" ) //
+            .addEqualsExpression( "repositoryId", repoId ) //
+            .addEqualsExpression( "namespaceId", namespace ) //
+            .execute();
+
+        final Set<String> projects = new HashSet<String>( result.get().getCount() );
+
+        for ( Row<String, String, String> row : result.get() )
         {
-            getProjectEntityManager().put( project );
+            projects.add( row.getColumnSlice().getColumnByName( "projectId" ).getValue() );
         }
-        catch ( PersistenceException e )
-        {
-            throw new MetadataRepositoryException( e.getMessage(), e );
-        }*/
 
+        return projects;
     }
 
     @Override
@@ -1516,34 +1552,6 @@ public class CassandraMetadataRepository
         return Collections.emptyList();
     }
 
-    @Override
-    public Collection<String> getProjects( final String repoId, final String namespace )
-        throws MetadataResolutionException
-    {
-        final Set<String> projects = new HashSet<String>();
-
-/*        // FIXME use cql query
-        getProjectEntityManager().visitAll( new Function<Project, Boolean>()
-        {
-            @Override
-            public Boolean apply( Project project )
-            {
-                if ( project != null )
-                {
-                    if ( StringUtils.equals( repoId, project.getNamespace().getRepository().getName() )
-                        && StringUtils.startsWith( project.getNamespace().getName(), namespace ) )
-                    {
-                        projects.add( project.getProjectId() );
-                    }
-                }
-                return Boolean.TRUE;
-            }
-        } );*/
-
-        return projects;
-    }
-
-
     @Override
     public void removeProjectVersion( final String repoId, final String namespace, final String projectId,
                                       final String projectVersion )
index 4314a071f5cf93f1ffddc62d094b6245ef0f52eb..a5c212e48ede8eb0515d9da551f4ae008c718201 100644 (file)
@@ -73,6 +73,8 @@ public class DefaultCassandraArchivaManager
 
     private String namespaceFamilyName = "namespace";
 
+    private String projectFamilyName = "project";
+
 
     @PostConstruct
     public void initialize()
@@ -107,11 +109,11 @@ public class DefaultCassandraArchivaManager
         // namespace table
         {
 
-            final ColumnFamilyDefinition namespaces =
+            final ColumnFamilyDefinition namespace =
                 HFactory.createColumnFamilyDefinition( keyspace.getKeyspaceName(), //
                                                        getNamespaceFamilyName(), //
                                                        ComparatorType.UTF8TYPE );
-            cfds.add( namespaces );
+            cfds.add( namespace );
 
             // creating indexes for cql query
 
@@ -120,16 +122,17 @@ public class DefaultCassandraArchivaManager
             nameColumn.setIndexName( "name" );
             nameColumn.setIndexType( ColumnIndexType.KEYS );
             nameColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
-            namespaces.addColumnDefinition( nameColumn );
+            namespace.addColumnDefinition( nameColumn );
 
             BasicColumnDefinition repositoryIdColumn = new BasicColumnDefinition();
             repositoryIdColumn.setName( StringSerializer.get().toByteBuffer( "repositoryId" ) );
             repositoryIdColumn.setIndexName( "repositoryId" );
             repositoryIdColumn.setIndexType( ColumnIndexType.KEYS );
             repositoryIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
-            namespaces.addColumnDefinition( repositoryIdColumn );
+            namespace.addColumnDefinition( repositoryIdColumn );
         }
 
+        // repository table
         {
             final ColumnFamilyDefinition repository =
                 HFactory.createColumnFamilyDefinition( keyspace.getKeyspaceName(), //
@@ -146,6 +149,39 @@ public class DefaultCassandraArchivaManager
             repository.addColumnDefinition( nameColumn );
         }
 
+        // project table
+        {
+
+            final ColumnFamilyDefinition project =
+                HFactory.createColumnFamilyDefinition( keyspace.getKeyspaceName(), //
+                                                       getProjectFamilyName(), //
+                                                       ComparatorType.UTF8TYPE );
+            cfds.add( project );
+
+            // creating indexes for cql query
+
+            BasicColumnDefinition projectIdColumn = new BasicColumnDefinition();
+            projectIdColumn.setName( StringSerializer.get().toByteBuffer( "projectId" ) );
+            projectIdColumn.setIndexName( "projectId" );
+            projectIdColumn.setIndexType( ColumnIndexType.KEYS );
+            projectIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
+            project.addColumnDefinition( projectIdColumn );
+
+            BasicColumnDefinition repositoryIdColumn = new BasicColumnDefinition();
+            repositoryIdColumn.setName( StringSerializer.get().toByteBuffer( "repositoryId" ) );
+            repositoryIdColumn.setIndexName( "repositoryId" );
+            repositoryIdColumn.setIndexType( ColumnIndexType.KEYS );
+            repositoryIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
+            project.addColumnDefinition( repositoryIdColumn );
+
+            BasicColumnDefinition namespaceIdColumn = new BasicColumnDefinition();
+            namespaceIdColumn.setName( StringSerializer.get().toByteBuffer( "namespaceId" ) );
+            namespaceIdColumn.setIndexName( "namespaceId" );
+            namespaceIdColumn.setIndexType( ColumnIndexType.KEYS );
+            namespaceIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
+            project.addColumnDefinition( namespaceIdColumn );
+        }
+
         { // ensure keyspace exists, here if the keyspace doesn't exist we suppose nothing exist
             if ( cluster.describeKeyspace( keyspaceName ) == null )
             {
@@ -197,4 +233,9 @@ public class DefaultCassandraArchivaManager
     {
         return namespaceFamilyName;
     }
+
+    public String getProjectFamilyName()
+    {
+        return projectFamilyName;
+    }
 }
index c6cd742f2392a70d250c6a1ff53aaadd70b94275..403a9326eda7ca418b31ea1ef7233cb3324d57a9 100644 (file)
@@ -19,6 +19,7 @@ package org.apache.archiva.metadata.repository.cassandra;
  * under the License.
  */
 
+import org.apache.archiva.metadata.model.ProjectMetadata;
 import org.apache.archiva.metadata.repository.cassandra.model.Namespace;
 import org.apache.archiva.metadata.repository.cassandra.model.Repository;
 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
@@ -108,6 +109,15 @@ public class RepositoriesNamespaceTest
             Assertions.assertThat( cmr.getNamespaces( "release" ) ).isNotEmpty().hasSize( 1 );
             Assertions.assertThat( cmr.getNamespaces( "release" ) ).containsExactly( "org" );
 
+            ProjectMetadata projectMetadata = new ProjectMetadata();
+            projectMetadata.setId( "theproject" );
+            projectMetadata.setNamespace( "org" );
+
+            cmr.updateProject( "release", projectMetadata );
+
+            Assertions.assertThat( cmr.getProjects( "release", "org" ) ).isNotEmpty().hasSize( 1 ).containsExactly(
+                "theproject" );
+
             cmr.removeRepository( "release" );
 
             r = cmr.getRepository( "release" );