You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@archiva.apache.org by ol...@apache.org on 2013/11/18 06:47:59 UTC

svn commit: r1542900 - in /archiva/trunk/archiva-modules/plugins/metadata-store-cassandra/src/main/java/org/apache/archiva/metadata/repository/cassandra: CassandraUtils.java model/HugeStringSerializer.java

Author: olamy
Date: Mon Nov 18 05:47:58 2013
New Revision: 1542900

URL: http://svn.apache.org/r1542900
Log:
add new classes

Added:
    archiva/trunk/archiva-modules/plugins/metadata-store-cassandra/src/main/java/org/apache/archiva/metadata/repository/cassandra/CassandraUtils.java   (with props)
    archiva/trunk/archiva-modules/plugins/metadata-store-cassandra/src/main/java/org/apache/archiva/metadata/repository/cassandra/model/HugeStringSerializer.java   (with props)

Added: archiva/trunk/archiva-modules/plugins/metadata-store-cassandra/src/main/java/org/apache/archiva/metadata/repository/cassandra/CassandraUtils.java
URL: http://svn.apache.org/viewvc/archiva/trunk/archiva-modules/plugins/metadata-store-cassandra/src/main/java/org/apache/archiva/metadata/repository/cassandra/CassandraUtils.java?rev=1542900&view=auto
==============================================================================
--- archiva/trunk/archiva-modules/plugins/metadata-store-cassandra/src/main/java/org/apache/archiva/metadata/repository/cassandra/CassandraUtils.java (added)
+++ archiva/trunk/archiva-modules/plugins/metadata-store-cassandra/src/main/java/org/apache/archiva/metadata/repository/cassandra/CassandraUtils.java Mon Nov 18 05:47:58 2013
@@ -0,0 +1,64 @@
+package org.apache.archiva.metadata.repository.cassandra;
+
+/*
+ * 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.
+ */
+
+/**
+ * @author Olivier Lamy
+ */
+public class CassandraUtils
+{
+
+    private static final String EMPTY_VALUE = "";
+
+    public static final String SEPARATOR = "->";
+
+    public static String generateKey( final String... bases )
+    {
+        final StringBuilder builder = new StringBuilder();
+        if ( bases == null || bases.length == 0 )
+        {
+            return builder.toString();
+        }
+
+        for ( final String s : bases )
+        {
+            if ( s != null )
+            {
+                builder.append( s );
+            }
+            else
+            {
+                builder.append( EMPTY_VALUE );
+            }
+            builder.append( SEPARATOR );
+        }
+        if ( builder.length() > 0 )
+        {
+            builder.setLength( builder.length() - SEPARATOR.length() );
+        }
+        return builder.toString();
+    }
+
+    private CassandraUtils()
+    {
+        // no-op
+    }
+
+}

Propchange: archiva/trunk/archiva-modules/plugins/metadata-store-cassandra/src/main/java/org/apache/archiva/metadata/repository/cassandra/CassandraUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: archiva/trunk/archiva-modules/plugins/metadata-store-cassandra/src/main/java/org/apache/archiva/metadata/repository/cassandra/CassandraUtils.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: archiva/trunk/archiva-modules/plugins/metadata-store-cassandra/src/main/java/org/apache/archiva/metadata/repository/cassandra/model/HugeStringSerializer.java
URL: http://svn.apache.org/viewvc/archiva/trunk/archiva-modules/plugins/metadata-store-cassandra/src/main/java/org/apache/archiva/metadata/repository/cassandra/model/HugeStringSerializer.java?rev=1542900&view=auto
==============================================================================
--- archiva/trunk/archiva-modules/plugins/metadata-store-cassandra/src/main/java/org/apache/archiva/metadata/repository/cassandra/model/HugeStringSerializer.java (added)
+++ archiva/trunk/archiva-modules/plugins/metadata-store-cassandra/src/main/java/org/apache/archiva/metadata/repository/cassandra/model/HugeStringSerializer.java Mon Nov 18 05:47:58 2013
@@ -0,0 +1,191 @@
+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.cassandra.db.marshal.UTF8Type;
+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.DeflaterInputStream;
+import java.util.zip.DeflaterOutputStream;
+import java.util.zip.InflaterInputStream;
+
+
+/**
+ * For Huge String we use a  compression
+ * @author Olivier Lamy
+ */
+public class HugeStringSerializer
+    extends AbstractSerializer<String>
+{
+
+    private Logger logger = LoggerFactory.getLogger( getClass() );
+
+    private static final String UTF_8 = "UTF-8";
+
+    private static final HugeStringSerializer instance = new HugeStringSerializer();
+
+    private static final Charset charset = Charset.forName( UTF_8 );
+
+    public static HugeStringSerializer 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 );
+    }
+
+    /*
+    private static final String UTF_8 = "UTF-8";
+    private static final HugeStringSerializer instance = new HugeStringSerializer();
+    private static final Charset charset = Charset.forName(UTF_8);
+
+    public static HugeStringSerializer get() {
+        return instance;
+    }
+
+    @Override
+    public ByteBuffer toByteBuffer(String obj) {
+        if (obj == null) {
+            return null;
+        }
+        return ByteBuffer.wrap(obj.getBytes(charset));
+    }
+
+    @Override
+    public String fromByteBuffer(ByteBuffer byteBuffer) {
+        if (byteBuffer == null) {
+            return null;
+        }
+        final ByteBuffer dup = byteBuffer.duplicate();
+        return charset.decode(dup).toString();
+    }
+
+    @Override
+    public ComparatorType getComparatorType() {
+        return ComparatorType.UTF8TYPE;
+    }
+
+    @Override
+    public ByteBuffer fromString(String str) {
+        return UTF8Type.instance.fromString(str);
+    }
+
+    @Override
+    public String getString(ByteBuffer byteBuffer) {
+        return UTF8Type.instance.getString(byteBuffer);
+    }
+    */
+}

Propchange: archiva/trunk/archiva-modules/plugins/metadata-store-cassandra/src/main/java/org/apache/archiva/metadata/repository/cassandra/model/HugeStringSerializer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: archiva/trunk/archiva-modules/plugins/metadata-store-cassandra/src/main/java/org/apache/archiva/metadata/repository/cassandra/model/HugeStringSerializer.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision