move cassandra to simply use hector client: start with repositories and namespaces

git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1579819 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Olivier Lamy 2014-03-20 23:22:20 +00:00
parent 44880ea190
commit 50cb8970ef
14 changed files with 536 additions and 1008 deletions

View File

@ -73,13 +73,14 @@
<artifactId>modelmapper</artifactId> <artifactId>modelmapper</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.geronimo.specs</groupId> <groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jpa_2.0_spec</artifactId> <artifactId>geronimo-jpa_2.0_spec</artifactId>
</dependency> </dependency>
<!-- FIXME still need old jackson --> <!-- FIXME still need old jackson -->
<!--
<dependency> <dependency>
<groupId>org.codehaus.jackson</groupId> <groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId> <artifactId>jackson-mapper-asl</artifactId>
@ -145,7 +146,7 @@
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
-->
<dependency> <dependency>
<groupId>org.apache.cassandra</groupId> <groupId>org.apache.cassandra</groupId>
<artifactId>cassandra-all</artifactId> <artifactId>cassandra-all</artifactId>

View File

@ -19,14 +19,8 @@ package org.apache.archiva.metadata.repository.cassandra;
* under the License. * under the License.
*/ */
import com.netflix.astyanax.Keyspace; import me.prettyprint.hector.api.Cluster;
import com.netflix.astyanax.entitystore.EntityManager; import me.prettyprint.hector.api.Keyspace;
import org.apache.archiva.metadata.repository.cassandra.model.ArtifactMetadataModel;
import org.apache.archiva.metadata.repository.cassandra.model.MetadataFacetModel;
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.ProjectVersionMetadataModel;
import org.apache.archiva.metadata.repository.cassandra.model.Repository;
/** /**
* @author Olivier Lamy * @author Olivier Lamy
@ -34,25 +28,14 @@ import org.apache.archiva.metadata.repository.cassandra.model.Repository;
*/ */
public interface CassandraArchivaManager public interface CassandraArchivaManager
{ {
Keyspace getKeyspace();
void start(); void start();
void shutdown(); void shutdown();
boolean started(); boolean started();
EntityManager<Repository, String> getRepositoryEntityManager(); Keyspace getKeyspace();
EntityManager<Namespace, String> getNamespaceEntityManager();
EntityManager<Project, String> getProjectEntityManager();
EntityManager<ArtifactMetadataModel, String> getArtifactMetadataModelEntityManager();
EntityManager<MetadataFacetModel, String> getMetadataFacetModelEntityManager();
EntityManager<ProjectVersionMetadataModel, String> getProjectVersionMetadataModelEntityManager();
Cluster getCluster();
} }

View File

@ -19,6 +19,11 @@ package org.apache.archiva.metadata.repository.cassandra;
* under the License. * under the License.
*/ */
import me.prettyprint.cassandra.serializers.SerializerTypeInferer;
import me.prettyprint.hector.api.Serializer;
import me.prettyprint.hector.api.beans.HColumn;
import me.prettyprint.hector.api.factory.HFactory;
/** /**
* @author Olivier Lamy * @author Olivier Lamy
* @since 2.0.0 * @since 2.0.0
@ -57,6 +62,14 @@ public class CassandraUtils
return builder.toString(); return builder.toString();
} }
public static <A, B> HColumn<A, B> column( final A name, final B value )
{
return HFactory.createColumn( name, //
value, //
(Serializer<A>) SerializerTypeInferer.getSerializer( name ), //
(Serializer<B>) SerializerTypeInferer.getSerializer( value ) );
}
private CassandraUtils() private CassandraUtils()
{ {
// no-op // no-op

View File

@ -19,26 +19,18 @@ package org.apache.archiva.metadata.repository.cassandra;
* under the License. * under the License.
*/ */
import com.google.common.collect.ImmutableMap; import me.prettyprint.cassandra.model.BasicColumnDefinition;
import com.netflix.astyanax.AstyanaxContext; import me.prettyprint.cassandra.model.ConfigurableConsistencyLevel;
import com.netflix.astyanax.Keyspace; import me.prettyprint.cassandra.serializers.StringSerializer;
import com.netflix.astyanax.connectionpool.NodeDiscoveryType; import me.prettyprint.cassandra.service.CassandraHostConfigurator;
import com.netflix.astyanax.connectionpool.exceptions.ConnectionException; import me.prettyprint.cassandra.service.ThriftKsDef;
import com.netflix.astyanax.connectionpool.exceptions.NotFoundException; import me.prettyprint.hector.api.Cluster;
import com.netflix.astyanax.connectionpool.impl.ConnectionPoolConfigurationImpl; import me.prettyprint.hector.api.HConsistencyLevel;
import com.netflix.astyanax.connectionpool.impl.ConnectionPoolType; import me.prettyprint.hector.api.Keyspace;
import com.netflix.astyanax.connectionpool.impl.Slf4jConnectionPoolMonitorImpl; import me.prettyprint.hector.api.ddl.ColumnFamilyDefinition;
import com.netflix.astyanax.ddl.KeyspaceDefinition; import me.prettyprint.hector.api.ddl.ColumnIndexType;
import com.netflix.astyanax.entitystore.DefaultEntityManager; import me.prettyprint.hector.api.ddl.ComparatorType;
import com.netflix.astyanax.entitystore.EntityManager; import me.prettyprint.hector.api.factory.HFactory;
import com.netflix.astyanax.impl.AstyanaxConfigurationImpl;
import com.netflix.astyanax.thrift.ThriftFamilyFactory;
import org.apache.archiva.metadata.repository.cassandra.model.ArtifactMetadataModel;
import org.apache.archiva.metadata.repository.cassandra.model.MetadataFacetModel;
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.ProjectVersionMetadataModel;
import org.apache.archiva.metadata.repository.cassandra.model.Repository;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
@ -47,8 +39,8 @@ import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy; import javax.annotation.PreDestroy;
import javax.inject.Inject; import javax.inject.Inject;
import javax.persistence.PersistenceException; import java.util.ArrayList;
import java.util.Properties; import java.util.List;
/** /**
* FIXME make all configuration not hardcoded :-) * FIXME make all configuration not hardcoded :-)
@ -70,215 +62,104 @@ public class DefaultCassandraArchivaManager
private static final String KEYSPACE_NAME = "ArchivaKeySpace"; private static final String KEYSPACE_NAME = "ArchivaKeySpace";
private AstyanaxContext<Keyspace> keyspaceContext; private boolean started;
private Cluster cluster;
private Keyspace keyspace; private Keyspace keyspace;
private boolean started = false;
private EntityManager<Repository, String> repositoryEntityManager;
private EntityManager<Namespace, String> namespaceEntityManager;
private EntityManager<Project, String> projectEntityManager;
private EntityManager<ArtifactMetadataModel, String> artifactMetadataModelEntityManager;
private EntityManager<MetadataFacetModel, String> metadataFacetModelEntityManager;
private EntityManager<ProjectVersionMetadataModel, String> projectVersionMetadataModelEntityManager;
@PostConstruct @PostConstruct
public void initialize() public void initialize()
throws ConnectionException
{ {
// FIXME must come from configuration not sys props
String cassandraHost = System.getProperty( "cassandraHost", "localhost" ); String cassandraHost = System.getProperty( "cassandraHost", "localhost" );
String cassandraPort = System.getProperty( "cassandraPort" ); String cassandraPort = System.getProperty( "cassandraPort" );
String cqlVersion = System.getProperty( "cassandra.cqlversion", "3.0.0" ); int maxActive = Integer.getInteger( "cassandra.maxActive", 20 );
keyspaceContext = new AstyanaxContext.Builder().forCluster( CLUSTER_NAME ).forKeyspace( String readConsistencyLevel =
KEYSPACE_NAME ).withAstyanaxConfiguration( System.getProperty( "cassandra.readConsistencyLevel", HConsistencyLevel.QUORUM.name() );
new AstyanaxConfigurationImpl() String writeConsistencyLevel =
//.setCqlVersion( cqlVersion ) System.getProperty( "cassandra.readConsistencyLevel", HConsistencyLevel.QUORUM.name() );
.setDiscoveryType( NodeDiscoveryType.RING_DESCRIBE )
.setConnectionPoolType( ConnectionPoolType.TOKEN_AWARE ) )
.withConnectionPoolConfiguration(
new ConnectionPoolConfigurationImpl( CLUSTER_NAME + "_" + KEYSPACE_NAME )
.setSocketTimeout( 30000 )
.setMaxTimeoutWhenExhausted( 2000 )
.setMaxConnsPerHost( 20 )
.setInitConnsPerHost( 10 )
.setSeeds( cassandraHost + ":" + cassandraPort ) )
.withConnectionPoolMonitor( new Slf4jConnectionPoolMonitorImpl() )
.buildKeyspace( ThriftFamilyFactory.getInstance() );
this.start(); int replicationFactor = Integer.getInteger( "cassandra.replicationFactor", 1 );
keyspace = keyspaceContext.getClient(); String keyspaceName = System.getProperty( "cassandra.keyspace.name", KEYSPACE_NAME );
//Partitioner partitioner = keyspace.getPartitioner(); String clusterName = System.getProperty( "cassandra.cluster.name", CLUSTER_NAME );
ImmutableMap<String, Object> options = ImmutableMap.<String, Object>builder().put( "strategy_options", final CassandraHostConfigurator configurator =
ImmutableMap.<String, Object>builder().put( new CassandraHostConfigurator( cassandraHost + ":" + cassandraPort );
"replication_factor", configurator.setMaxActive( maxActive );
"1" ).build() ).put(
"strategy_class", "SimpleStrategy" ).build();
// test if the namespace already exists if exception or null create it cluster = HFactory.getOrCreateCluster( clusterName, configurator );
boolean keyspaceExists = false;
try final ConfigurableConsistencyLevel consistencyLevelPolicy = new ConfigurableConsistencyLevel();
consistencyLevelPolicy.setDefaultReadConsistencyLevel( HConsistencyLevel.valueOf( readConsistencyLevel ) );
consistencyLevelPolicy.setDefaultWriteConsistencyLevel( HConsistencyLevel.valueOf( writeConsistencyLevel ) );
keyspace = HFactory.createKeyspace( keyspaceName, cluster, consistencyLevelPolicy );
List<ColumnFamilyDefinition> cfds = new ArrayList<ColumnFamilyDefinition>();
// namespace table
{ {
KeyspaceDefinition keyspaceDefinition = keyspace.describeKeyspace();
if ( keyspaceDefinition != null ) final ColumnFamilyDefinition namespaces =
HFactory.createColumnFamilyDefinition( keyspace.getKeyspaceName(), "namespace",
ComparatorType.UTF8TYPE );
cfds.add( namespaces );
// creating indexes for cql query
BasicColumnDefinition nameColumn = new BasicColumnDefinition();
nameColumn.setName( StringSerializer.get().toByteBuffer( "name" ) );
nameColumn.setIndexName( "name" );
nameColumn.setIndexType( ColumnIndexType.KEYS );
nameColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
namespaces.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 );
}
{
final ColumnFamilyDefinition repository =
HFactory.createColumnFamilyDefinition( keyspace.getKeyspaceName(), "repository",
ComparatorType.UTF8TYPE );
cfds.add( repository );
BasicColumnDefinition nameColumn = new BasicColumnDefinition();
nameColumn.setName( StringSerializer.get().toByteBuffer( "name" ) );
nameColumn.setIndexName( "name" );
nameColumn.setIndexType( ColumnIndexType.KEYS );
nameColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
repository.addColumnDefinition( nameColumn );
}
{ // ensure keyspace exists, here if the keyspace doesn't exist we suppose nothing exist
if ( cluster.describeKeyspace( keyspaceName ) == null )
{ {
keyspaceExists = true; logger.info( "Creating Archiva Cassandra '" + keyspaceName + "' keyspace." );
cluster.addKeyspace( HFactory.createKeyspaceDefinition( keyspaceName, //
ThriftKsDef.DEF_STRATEGY_CLASS, //
replicationFactor, //
cfds )
);
} }
}
catch ( ConnectionException e )
{
} }
if ( !keyspaceExists )
{
keyspace.createKeyspace( options );
}
try
{
Properties properties = keyspace.getKeyspaceProperties();
logger.info( "keyspace properties: {}", properties );
}
catch ( ConnectionException e )
{
// FIXME better logging !
logger.warn( e.getMessage(), e );
}
try
{
repositoryEntityManager =
DefaultEntityManager.<Repository, String>builder()
.withEntityType( Repository.class )
.withKeyspace( keyspace )
.withAutoCommit( true )
.withColumnFamily( "repository" )
.build();
boolean exists = columnFamilyExists( "repository" );
// TODO very basic test we must test model change too
if ( !exists )
{
repositoryEntityManager.createStorage( null );
}
namespaceEntityManager =
DefaultEntityManager.<Namespace, String>builder()
.withEntityType( Namespace.class )
.withKeyspace( keyspace )
.withAutoCommit( true )
.withColumnFamily( "namespace" )
.build();
exists = columnFamilyExists( "namespace" );
if ( !exists )
{
// create secondary index
options =
ImmutableMap.<String, Object>builder()
.put("repositoryid", ImmutableMap.<String, Object>builder()
.put("validation_class", "UTF8Type")
.put("index_name", "Indexrepositoryid")
.put("index_type", "KEYS")
.build()).build();
namespaceEntityManager.createStorage( options );
}
projectEntityManager =
DefaultEntityManager.<Project, String>builder()
.withEntityType( Project.class )
.withKeyspace( keyspace )
.withAutoCommit( true )
.withColumnFamily( "project" )
.build();
exists = columnFamilyExists( "project" );
if ( !exists )
{
projectEntityManager.createStorage( null );
}
artifactMetadataModelEntityManager =
DefaultEntityManager.<ArtifactMetadataModel, String>builder()
.withEntityType( ArtifactMetadataModel.class )
.withAutoCommit( true )
.withKeyspace( keyspace )
.withColumnFamily( "artifactmetadatamodel" )
.build();
exists = columnFamilyExists( "artifactmetadatamodel" );
if ( !exists )
{
artifactMetadataModelEntityManager.createStorage( null );
}
metadataFacetModelEntityManager =
DefaultEntityManager.<MetadataFacetModel, String>builder()
.withEntityType( MetadataFacetModel.class )
.withAutoCommit( true )
.withKeyspace( keyspace )
.withColumnFamily( "metadatafacetmodel" )
.build();
exists = columnFamilyExists( "metadatafacetmodel" );
if ( !exists )
{
metadataFacetModelEntityManager.createStorage( null );
}
projectVersionMetadataModelEntityManager =
DefaultEntityManager.<ProjectVersionMetadataModel, String>builder()
.withEntityType( ProjectVersionMetadataModel.class )
.withAutoCommit( true )
.withKeyspace( keyspace )
.withColumnFamily( "projectversionmetadatamodel" )
.build();
exists = columnFamilyExists( "projectversionmetadatamodel" );
if ( !exists )
{
projectVersionMetadataModelEntityManager.createStorage( null );
}
}
catch ( PersistenceException e )
{
// FIXME report exception
logger.error( e.getMessage(), e );
}
catch ( ConnectionException e )
{
// FIXME report exception
logger.error( e.getMessage(), e );
}
} }
public void start() public void start()
{ {
keyspaceContext.start();
started = true;
} }
@PreDestroy @PreDestroy
public void shutdown() public void shutdown()
{ {
if ( keyspaceContext != null )
{
keyspaceContext.shutdown();
started = false;
}
} }
@ -288,21 +169,6 @@ public class DefaultCassandraArchivaManager
return started; return started;
} }
private boolean columnFamilyExists( String columnFamilyName )
throws ConnectionException
{
try
{
Properties properties = keyspace.getColumnFamilyProperties( columnFamilyName );
logger.debug( "getColumnFamilyProperties for {}: {}", columnFamilyName, properties );
return true;
}
catch ( NotFoundException e )
{
return false;
}
}
@Override @Override
public Keyspace getKeyspace() public Keyspace getKeyspace()
@ -310,66 +176,8 @@ public class DefaultCassandraArchivaManager
return keyspace; return keyspace;
} }
public EntityManager<Repository, String> getRepositoryEntityManager() public Cluster getCluster()
{ {
return repositoryEntityManager; return cluster;
}
public void setRepositoryEntityManager( EntityManager<Repository, String> repositoryEntityManager )
{
this.repositoryEntityManager = repositoryEntityManager;
}
public EntityManager<Namespace, String> getNamespaceEntityManager()
{
return namespaceEntityManager;
}
public void setNamespaceEntityManager( EntityManager<Namespace, String> namespaceEntityManager )
{
this.namespaceEntityManager = namespaceEntityManager;
}
public EntityManager<Project, String> getProjectEntityManager()
{
return projectEntityManager;
}
public void setProjectEntityManager( EntityManager<Project, String> projectEntityManager )
{
this.projectEntityManager = projectEntityManager;
}
public EntityManager<ArtifactMetadataModel, String> getArtifactMetadataModelEntityManager()
{
return artifactMetadataModelEntityManager;
}
public void setArtifactMetadataModelEntityManager(
EntityManager<ArtifactMetadataModel, String> artifactMetadataModelEntityManager )
{
this.artifactMetadataModelEntityManager = artifactMetadataModelEntityManager;
}
public EntityManager<MetadataFacetModel, String> getMetadataFacetModelEntityManager()
{
return metadataFacetModelEntityManager;
}
public void setMetadataFacetModelEntityManager(
EntityManager<MetadataFacetModel, String> metadataFacetModelEntityManager )
{
this.metadataFacetModelEntityManager = metadataFacetModelEntityManager;
}
public EntityManager<ProjectVersionMetadataModel, String> getProjectVersionMetadataModelEntityManager()
{
return projectVersionMetadataModelEntityManager;
}
public void setProjectVersionMetadataModelEntityManager(
EntityManager<ProjectVersionMetadataModel, String> projectVersionMetadataModelEntityManager )
{
this.projectVersionMetadataModelEntityManager = projectVersionMetadataModelEntityManager;
} }
} }

View File

@ -19,12 +19,9 @@ package org.apache.archiva.metadata.repository.cassandra.model;
* under the License. * under the License.
*/ */
import com.netflix.astyanax.entitystore.Serializer;
import com.netflix.astyanax.serializers.LongSerializer;
import org.apache.archiva.metadata.repository.cassandra.CassandraUtils; import org.apache.archiva.metadata.repository.cassandra.CassandraUtils;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id; import javax.persistence.Id;
import java.io.Serializable; import java.io.Serializable;
import java.util.Date; import java.util.Date;
@ -35,58 +32,45 @@ import java.util.Date;
* @author Olivier Lamy * @author Olivier Lamy
* @since 2.0.0 * @since 2.0.0
*/ */
@Entity
public class ArtifactMetadataModel public class ArtifactMetadataModel
implements Serializable implements Serializable
{ {
// repositoryId + namespaceId + project + projectVersion + id // repositoryId + namespaceId + project + projectVersion + id
@Id @Id
@Serializer( DeflateStringSerializer.class )
private String artifactMetadataModelId; private String artifactMetadataModelId;
@Column( name = "id" ) @Column(name = "id")
@Serializer( DeflateStringSerializer.class )
private String id; private String id;
@Column( name = "repositoryId" ) @Column(name = "repositoryId")
@Serializer( DeflateStringSerializer.class )
private String repositoryId; private String repositoryId;
@Column( name = "namespace" ) @Column(name = "namespace")
@Serializer( DeflateStringSerializer.class )
private String namespace; private String namespace;
@Column( name = "project" ) @Column(name = "project")
@Serializer( DeflateStringSerializer.class )
private String project; private String project;
@Column( name = "projectVersion" ) @Column(name = "projectVersion")
@Serializer( DeflateStringSerializer.class )
private String projectVersion; private String projectVersion;
@Column( name = "version" ) @Column(name = "version")
@Serializer( DeflateStringSerializer.class )
private String version; private String version;
@Column( name = "fileLastModified" ) @Column(name = "fileLastModified")
@Serializer( LongSerializer.class )
private long fileLastModified; private long fileLastModified;
@Column( name = "size" ) @Column(name = "size")
@Serializer( LongSerializer.class )
private long size; private long size;
@Column( name = "md5" ) @Column(name = "md5")
@Serializer( DeflateStringSerializer.class )
private String md5; private String md5;
@Column( name = "sha1" ) @Column(name = "sha1")
@Serializer( DeflateStringSerializer.class )
private String sha1; private String sha1;
@Column( name = "whenGathered" ) @Column(name = "whenGathered")
@Serializer( LongSerializer.class )
private long whenGathered; private long whenGathered;
public ArtifactMetadataModel() public ArtifactMetadataModel()

View File

@ -1,149 +0,0 @@
package org.apache.archiva.metadata.repository.cassandra.model;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import com.netflix.astyanax.serializers.AbstractSerializer;
import com.netflix.astyanax.serializers.ComparatorType;
import org.apache.commons.codec.binary.StringUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream;
/**
* For Huge String we use a deflate compression
* @author Olivier Lamy
* @since 2.0.0
*/
public class DeflateStringSerializer
extends AbstractSerializer<String>
{
private Logger logger = LoggerFactory.getLogger( getClass() );
private static final String UTF_8 = "UTF-8";
private static final DeflateStringSerializer instance = new DeflateStringSerializer();
private static final Charset charset = Charset.forName( UTF_8 );
public static DeflateStringSerializer get()
{
return instance;
}
@Override
public ByteBuffer toByteBuffer( String obj )
{
if ( obj == null )
{
return null;
}
try
{
byte[] bytes = compressWithDeflate( StringUtils.getBytesUtf8( obj ) );
return ByteBuffer.wrap( bytes );
}
catch ( IOException e )
{
throw new RuntimeException( "Fail to compress column data", e );
}
}
@Override
public String fromByteBuffer( ByteBuffer byteBuffer )
{
if ( byteBuffer == null )
{
return null;
}
ByteBuffer dup = byteBuffer.duplicate();
try
{
String str = getFromDeflateBytes( dup.array() );
return str;
}
catch ( IOException e )
{
throw new RuntimeException( "Fail to decompress column data", e );
}
}
public String getFromDeflateBytes( byte[] bytes )
throws IOException
{
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( bytes );
InflaterInputStream inflaterInputStream = new InflaterInputStream( byteArrayInputStream );
return IOUtils.toString( inflaterInputStream );
}
public byte[] compressWithDeflate( byte[] unCompress )
throws IOException
{
try
{
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
DeflaterOutputStream out = new DeflaterOutputStream( buffer, new Deflater( Deflater.BEST_COMPRESSION ) );
out.write( unCompress );
out.finish();
ByteArrayInputStream bais = new ByteArrayInputStream( buffer.toByteArray() );
byte[] res = IOUtils.toByteArray( bais );
return res;
}
catch ( IOException e )
{
logger.debug( "IOException in compressStringWithDeflate", e );
throw e;
}
}
@Override
public ComparatorType getComparatorType()
{
return ComparatorType.BYTESTYPE;
}
@Override
public ByteBuffer fromString( String str )
{
return instance.fromString( str );
}
@Override
public String getString( ByteBuffer byteBuffer )
{
return instance.getString( byteBuffer );
}
}

View File

@ -19,11 +19,9 @@ package org.apache.archiva.metadata.repository.cassandra.model;
* under the License. * under the License.
*/ */
import com.netflix.astyanax.entitystore.Serializer;
import org.apache.archiva.metadata.repository.cassandra.CassandraUtils; import org.apache.archiva.metadata.repository.cassandra.CassandraUtils;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id; import javax.persistence.Id;
/** /**
@ -32,32 +30,26 @@ import javax.persistence.Id;
* @author Olivier Lamy * @author Olivier Lamy
* @since 2.0.0 * @since 2.0.0
*/ */
@Entity
public class MetadataFacetModel public class MetadataFacetModel
{ {
// id is repositoryId + namespaceId + projectId + facetId + name + mapKey // id is repositoryId + namespaceId + projectId + facetId + name + mapKey
@Id @Id
@Column( name = "id" ) @Column(name = "id")
@Serializer( DeflateStringSerializer.class )
private String id; private String id;
@Column( name = "artifactMetadataModel" ) @Column(name = "artifactMetadataModel")
private ArtifactMetadataModel artifactMetadataModel; private ArtifactMetadataModel artifactMetadataModel;
@Column( name = "facetId" ) @Column(name = "facetId")
@Serializer( DeflateStringSerializer.class )
private String facetId; private String facetId;
@Column( name = "key" ) @Column(name = "key")
@Serializer( DeflateStringSerializer.class )
private String key; private String key;
@Column( name = "name" ) @Column(name = "name")
@Serializer( DeflateStringSerializer.class )
private String name; private String name;
@Column( name = "value" ) @Column(name = "value")
@Serializer( DeflateStringSerializer.class )
private String value; private String value;
public MetadataFacetModel() public MetadataFacetModel()
@ -232,7 +224,8 @@ public class MetadataFacetModel
String str = CassandraUtils.generateKey( this.artifactMetadataModel == null String str = CassandraUtils.generateKey( this.artifactMetadataModel == null
? this.repositoryId ? this.repositoryId
: this.artifactMetadataModel.getArtifactMetadataModelId(), : this.artifactMetadataModel.getArtifactMetadataModelId(),
this.facetId, this.name, this.key ); this.facetId, this.name, this.key
);
//return Long.toString( str.hashCode() ); //return Long.toString( str.hashCode() );
return str; return str;

View File

@ -19,12 +19,9 @@ package org.apache.archiva.metadata.repository.cassandra.model;
* under the License. * under the License.
*/ */
import com.netflix.astyanax.entitystore.Serializer;
import org.apache.archiva.metadata.repository.cassandra.CassandraUtils; import org.apache.archiva.metadata.repository.cassandra.CassandraUtils;
import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Id;
import java.io.Serializable; import java.io.Serializable;
@ -32,32 +29,18 @@ import java.io.Serializable;
* @author Olivier Lamy * @author Olivier Lamy
* @since 2.0.0 * @since 2.0.0
*/ */
@Entity
public class Namespace public class Namespace
implements Serializable implements Serializable
{ {
@Id
@Serializer( DeflateStringSerializer.class )
private String id; private String id;
@Column( name = "name" )
@Serializer( DeflateStringSerializer.class )
private String name; private String name;
@Column( name = "repositoryid" )
@Serializer( DeflateStringSerializer.class )
private String repositoryId; private String repositoryId;
@Column( name = "repositoryname" )
@Serializer( DeflateStringSerializer.class )
private String repositoryName; private String repositoryName;
//@ManyToOne(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
//@JoinColumn(name = "repository_id")
//private transient Repository repository;
public Namespace() public Namespace()
{ {
// no op // no op

View File

@ -19,11 +19,9 @@ package org.apache.archiva.metadata.repository.cassandra.model;
* under the License. * under the License.
*/ */
import com.netflix.astyanax.entitystore.Serializer;
import org.apache.archiva.metadata.repository.cassandra.CassandraUtils; import org.apache.archiva.metadata.repository.cassandra.CassandraUtils;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id; import javax.persistence.Id;
import java.io.Serializable; import java.io.Serializable;
@ -31,21 +29,18 @@ import java.io.Serializable;
* @author Olivier Lamy * @author Olivier Lamy
* @since 2.0.0 * @since 2.0.0
*/ */
@Entity
public class Project public class Project
implements Serializable implements Serializable
{ {
@Id @Id
@Column( name = "projectKey" ) @Column(name = "projectKey")
@Serializer( DeflateStringSerializer.class )
private String projectKey; private String projectKey;
@Column( name = "projectId" ) @Column(name = "projectId")
@Serializer( DeflateStringSerializer.class )
private String projectId; private String projectId;
@Column( name = "repository" ) @Column(name = "repository")
private Namespace namespace; private Namespace namespace;
public Project() public Project()

View File

@ -19,7 +19,6 @@ package org.apache.archiva.metadata.repository.cassandra.model;
* under the License. * under the License.
*/ */
import com.netflix.astyanax.entitystore.Serializer;
import org.apache.archiva.metadata.model.CiManagement; import org.apache.archiva.metadata.model.CiManagement;
import org.apache.archiva.metadata.model.Dependency; import org.apache.archiva.metadata.model.Dependency;
import org.apache.archiva.metadata.model.IssueManagement; import org.apache.archiva.metadata.model.IssueManagement;
@ -30,7 +29,6 @@ import org.apache.archiva.metadata.model.Scm;
import org.apache.archiva.metadata.repository.cassandra.CassandraUtils; import org.apache.archiva.metadata.repository.cassandra.CassandraUtils;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id; import javax.persistence.Id;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -39,63 +37,56 @@ import java.util.List;
* @author Olivier Lamy * @author Olivier Lamy
* @since 2.0.0 * @since 2.0.0
*/ */
@Entity
public class ProjectVersionMetadataModel public class ProjectVersionMetadataModel
{ {
// repositoryId + namespace + projectId + id (version) // repositoryId + namespace + projectId + id (version)
@Id @Id
@Serializer( DeflateStringSerializer.class )
private String rowId; private String rowId;
@Column( name = "namespace" ) @Column(name = "namespace")
private Namespace namespace; private Namespace namespace;
/** /**
* id is the version * id is the version
*/ */
@Column( name = "id" ) @Column(name = "id")
@Serializer( DeflateStringSerializer.class )
private String id; private String id;
@Column( name = "projectId" ) @Column(name = "projectId")
@Serializer( DeflateStringSerializer.class )
private String projectId; private String projectId;
@Column( name = "url" ) @Column(name = "url")
@Serializer( DeflateStringSerializer.class )
private String url; private String url;
@Column( name = "name" ) @Column(name = "name")
@Serializer( DeflateStringSerializer.class )
private String name; private String name;
@Column( name = "description" ) @Column(name = "description")
@Serializer( DeflateStringSerializer.class )
private String description; private String description;
@Column( name = "organization" ) @Column(name = "organization")
private Organization organization; private Organization organization;
@Column( name = "issueManagement" ) @Column(name = "issueManagement")
private IssueManagement issueManagement; private IssueManagement issueManagement;
@Column( name = "scm" ) @Column(name = "scm")
private Scm scm; private Scm scm;
@Column( name = "ciManagement" ) @Column(name = "ciManagement")
private CiManagement ciManagement; private CiManagement ciManagement;
// FIXME store those values in a separate table // FIXME store those values in a separate table
@Column( name = "licenses" ) @Column(name = "licenses")
private List<License> licenses = new ArrayList<License>(); private List<License> licenses = new ArrayList<License>();
@Column( name = "mailingLists" ) @Column(name = "mailingLists")
private List<MailingList> mailingLists = new ArrayList<MailingList>(); private List<MailingList> mailingLists = new ArrayList<MailingList>();
@Column( name = "dependencies" ) @Column(name = "dependencies")
private List<Dependency> dependencies = new ArrayList<Dependency>(); private List<Dependency> dependencies = new ArrayList<Dependency>();
@Column( name = "incomplete" ) @Column(name = "incomplete")
private boolean incomplete; private boolean incomplete;
public String getProjectId() public String getProjectId()

View File

@ -19,12 +19,6 @@ package org.apache.archiva.metadata.repository.cassandra.model;
* under the License. * under the License.
*/ */
import com.netflix.astyanax.entitystore.Serializer;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable; import java.io.Serializable;
@ -32,22 +26,14 @@ import java.io.Serializable;
* @author Olivier Lamy * @author Olivier Lamy
* @since 2.0.0 * @since 2.0.0
*/ */
@Entity
public class Repository public class Repository
implements Serializable implements Serializable
{ {
@Id
@Column( name = "id" )
@Serializer( DeflateStringSerializer.class )
private String id; private String id;
@Column(name = "name")
@Serializer( DeflateStringSerializer.class )
private String name; private String name;
//private transient List<Namespace> namespaces = new ArrayList<Namespace>();
public Repository() public Repository()
{ {
// no op // no op
@ -79,22 +65,6 @@ public class Repository
this.name = name; this.name = name;
} }
/*
public List<Namespace> getNamespaces()
{
if ( this.namespaces == null )
{
this.namespaces = new ArrayList<Namespace>();
}
return namespaces;
}
public void setNamespaces( List<Namespace> namespaces )
{
this.namespaces = namespaces;
}
*/
@Override @Override
public boolean equals( Object o ) public boolean equals( Object o )
{ {

View File

@ -41,7 +41,7 @@ public class CassandraMetadataRepositoryTest
private Logger logger = LoggerFactory.getLogger( getClass() ); private Logger logger = LoggerFactory.getLogger( getClass() );
@Inject @Inject
@Named( value = "archivaEntityManagerFactory#cassandra" ) @Named(value = "archivaEntityManagerFactory#cassandra")
CassandraArchivaManager cassandraArchivaManager; CassandraArchivaManager cassandraArchivaManager;
CassandraMetadataRepository cmr; CassandraMetadataRepository cmr;
@ -78,45 +78,12 @@ public class CassandraMetadataRepositoryTest
protected void clearReposAndNamespace() protected void clearReposAndNamespace()
throws Exception throws Exception
{ {
/* cassandraArchivaManager.getCluster().truncate( cassandraArchivaManager.getKeyspace().getKeyspaceName(),
List<Project> projects = cmr.getProjectEntityManager().getAll(); "project" );
cassandraArchivaManager.getCluster().truncate( cassandraArchivaManager.getKeyspace().getKeyspaceName(),
cmr.getProjectEntityManager().remove( projects ); "namespace" );
*/ cassandraArchivaManager.getCluster().truncate( cassandraArchivaManager.getKeyspace().getKeyspaceName(),
cmr.getProjectEntityManager().truncate(); "repository" );
/*
List<Namespace> namespaces = cmr.getNamespaceEntityManager().getAll();
cmr.getNamespaceEntityManager().remove( namespaces );
*/
cmr.getNamespaceEntityManager().truncate();
/*
List<Repository> repositories = cmr.getRepositoryEntityManager().getAll();
cmr.getRepositoryEntityManager().remove( repositories );
*/
cmr.getRepositoryEntityManager().truncate();
/*
List<ArtifactMetadataModel> artifactMetadataModels = cmr.getArtifactMetadataModelEntityManager().getAll();
cmr.getArtifactMetadataModelEntityManager().remove( artifactMetadataModels );
*/
cmr.getArtifactMetadataModelEntityManager().truncate();
/*
List<MetadataFacetModel> metadataFacetModels = cmr.getMetadataFacetModelEntityManager().getAll();
cmr.getMetadataFacetModelEntityManager().remove( metadataFacetModels );
*/
cmr.getMetadataFacetModelEntityManager().truncate();
/*
List<ProjectVersionMetadataModel> projectVersionMetadataModels =
cmr.getProjectVersionMetadataModelEntityManager().getAll();
cmr.getProjectVersionMetadataModelEntityManager().remove( projectVersionMetadataModels );
*/
cmr.getProjectVersionMetadataModelEntityManager().truncate();
} }

View File

@ -37,15 +37,15 @@ import javax.inject.Named;
/** /**
* @author Olivier Lamy * @author Olivier Lamy
*/ */
@RunWith(ArchivaSpringJUnit4ClassRunner.class) @RunWith( ArchivaSpringJUnit4ClassRunner.class )
@ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" }) @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
public class RepositoriesNamespaceTest public class RepositoriesNamespaceTest
{ {
private Logger logger = LoggerFactory.getLogger( getClass() ); private Logger logger = LoggerFactory.getLogger( getClass() );
@Inject @Inject
@Named(value = "archivaEntityManagerFactory#cassandra") @Named( value = "archivaEntityManagerFactory#cassandra" )
CassandraArchivaManager cassandraArchivaManager; CassandraArchivaManager cassandraArchivaManager;
@ -85,24 +85,35 @@ public class RepositoriesNamespaceTest
cmr.updateNamespace( "release", "org" ); cmr.updateNamespace( "release", "org" );
r = cmr.getRepositoryEntityManager().get( "release" ); r = cmr.getRepository( "release" );
Assertions.assertThat( r ).isNotNull(); Assertions.assertThat( r ).isNotNull();
Assertions.assertThat( cmr.getRepositories() ).isNotEmpty().hasSize( 1 ); Assertions.assertThat( cmr.getRepositories() ).isNotEmpty().hasSize( 1 );
Assertions.assertThat( cmr.getNamespaces( "release" ) ).isNotEmpty().hasSize( 1 ); Assertions.assertThat( cmr.getNamespaces( "release" ) ).isNotEmpty().hasSize( 1 );
n = cmr.getNamespaceEntityManager().get( "release" + CassandraUtils.SEPARATOR + "org" ); n = cmr.getNamespace( "release", "org" );
Assertions.assertThat( n ).isNotNull(); Assertions.assertThat( n ).isNotNull();
Assertions.assertThat( n.getRepository() ).isNotNull(); Assertions.assertThat( n.getRepository() ).isNotNull();
cmr.updateNamespace( "release", "org.apache" ); cmr.updateNamespace( "release", "org.apache" );
r = cmr.getRepositoryEntityManager().get( "release" ); r = cmr.getRepository( "release" );
Assertions.assertThat( r ).isNotNull(); Assertions.assertThat( r ).isNotNull();
Assertions.assertThat( cmr.getNamespaces( "release" ) ).isNotEmpty().hasSize( 2 ); Assertions.assertThat( cmr.getNamespaces( "release" ) ).isNotEmpty().hasSize( 2 );
cmr.removeNamespace( "release", "org.apache" );
Assertions.assertThat( cmr.getNamespaces( "release" ) ).isNotEmpty().hasSize( 1 );
Assertions.assertThat( cmr.getNamespaces( "release" ) ).containsExactly( "org" );
cmr.removeRepository( "release" );
r = cmr.getRepository( "release" );
Assertions.assertThat( r ).isNull();
} }
catch ( Exception e ) catch ( Exception e )
{ {
@ -118,22 +129,11 @@ public class RepositoriesNamespaceTest
protected void clearReposAndNamespace() protected void clearReposAndNamespace()
throws Exception throws Exception
{ {
/* cassandraArchivaManager.getCluster().truncate( cassandraArchivaManager.getKeyspace().getKeyspaceName(),
List<Project> projects = cmr.getProjectEntityManager().getAll(); "project" );
cmr.getProjectEntityManager().remove( projects ); cassandraArchivaManager.getCluster().truncate( cassandraArchivaManager.getKeyspace().getKeyspaceName(),
*/ "namespace" );
cmr.getProjectEntityManager().truncate(); cassandraArchivaManager.getCluster().truncate( cassandraArchivaManager.getKeyspace().getKeyspaceName(),
"repository" );
/*
List<Namespace> namespaces = cmr.getNamespaceEntityManager().getAll();
cmr.getNamespaceEntityManager().remove( namespaces );
*/
cmr.getNamespaceEntityManager().truncate();
/*
List<Repository> repositories = cmr.getRepositoryEntityManager().getAll();
cmr.getRepositoryEntityManager().remove( repositories );
*/
cmr.getRepositoryEntityManager().truncate();
} }
} }