You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2013/12/28 20:44:21 UTC
svn commit: r1553897 - in
/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree:
NameRevision.java NameRevisionComparator.java NameRevisionSerializer.java
RevisionName.java
Author: elecharny
Date: Sat Dec 28 19:44:21 2013
New Revision: 1553897
URL: http://svn.apache.org/r1553897
Log:
o Added the NameRevision class to be used by the BtreeOfBtrees, and the associated comparator and serializer
o The RevisionName class inherits from Tuple now
Added:
directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/NameRevision.java
directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/NameRevisionComparator.java
directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/NameRevisionSerializer.java
Modified:
directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/RevisionName.java
Added: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/NameRevision.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/NameRevision.java?rev=1553897&view=auto
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/NameRevision.java (added)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/NameRevision.java Sat Dec 28 19:44:21 2013
@@ -0,0 +1,128 @@
+/*
+ * 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.
+ *
+ */
+package org.apache.directory.mavibot.btree;
+
+
+/**
+ * A data structure that stores a Btree name associated with a revision. We use
+ * it to manage Btree of Btrees.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+/* no qualifier*/class NameRevision extends Tuple<String, Long>
+{
+ /**
+ * A constructor for the RevisionName class
+ * @param revision The revision
+ * @param name The BTree name
+ */
+ /* no qualifier*/NameRevision( String name, long revision )
+ {
+ super( name, revision );
+ }
+
+
+ /**
+ * @return the revision
+ */
+ /* no qualifier*/long getRevision()
+ {
+ return getValue();
+ }
+
+
+ /**
+ * @param revision the revision to set
+ */
+ /* no qualifier*/void setRevision( long revision )
+ {
+ setValue( revision );
+ }
+
+
+ /**
+ * @return the btree name
+ */
+ /* no qualifier*/String getName()
+ {
+ return getKey();
+ }
+
+
+ /**
+ * @param name the btree name to set
+ */
+ /* no qualifier*/void setName( String name )
+ {
+ setKey( name );
+ }
+
+
+ /**
+ * @see Object#equals(Object)
+ */
+ public boolean equals( Object that )
+ {
+ if ( this == that )
+ {
+ return true;
+ }
+
+ if ( !( that instanceof NameRevision ) )
+ {
+ return false;
+ }
+
+ NameRevision revisionName = ( NameRevision ) that;
+
+ if ( getRevision() != revisionName.getRevision() )
+ {
+ return false;
+ }
+
+ if ( getName() == null )
+ {
+ return revisionName.getName() == null;
+ }
+
+ return ( getName().equals( revisionName.getName() ) );
+
+ }
+
+
+ @Override
+ public int hashCode()
+ {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ( ( getName() == null ) ? 0 : getName().hashCode() );
+ result = prime * result + ( int ) ( getRevision() ^ ( getRevision() >>> 32 ) );
+ return result;
+ }
+
+
+ /**
+ * @see Object#toString()
+ */
+ public String toString()
+ {
+ return "[" + getRevision() + ":" + getName() + "]";
+ }
+}
Added: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/NameRevisionComparator.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/NameRevisionComparator.java?rev=1553897&view=auto
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/NameRevisionComparator.java (added)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/NameRevisionComparator.java Sat Dec 28 19:44:21 2013
@@ -0,0 +1,76 @@
+/*
+ * 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.
+ *
+ */
+package org.apache.directory.mavibot.btree;
+
+
+import java.util.Comparator;
+
+
+/**
+ * A comparator for the RevisionName class
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+/* no qualifier*/class NameRevisionComparator implements Comparator<NameRevision>
+{
+ /**
+ * {@inheritDoc}
+ */
+ public int compare( NameRevision rn1, NameRevision rn2 )
+ {
+ if ( rn1 == rn2 )
+ {
+ return 0;
+ }
+
+ // First compare the name
+ int comp = rn1.getName().compareTo( rn2.getName() );
+
+ if ( comp < 0 )
+ {
+ return -1;
+ }
+ else if ( comp > 0 )
+ {
+ return 1;
+ }
+
+ if ( rn1.getRevision() < rn2.getRevision() )
+ {
+ return -1;
+ }
+ else if ( rn1.getRevision() > rn2.getRevision() )
+ {
+ return 1;
+ }
+
+ // The name are equal : check the revision
+ if ( rn1.getRevision() < rn2.getRevision() )
+ {
+ return -1;
+ }
+ else if ( rn1.getRevision() > rn2.getRevision() )
+ {
+ return 1;
+ }
+
+ return 0;
+ }
+}
Added: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/NameRevisionSerializer.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/NameRevisionSerializer.java?rev=1553897&view=auto
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/NameRevisionSerializer.java (added)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/NameRevisionSerializer.java Sat Dec 28 19:44:21 2013
@@ -0,0 +1,245 @@
+/*
+ * 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.
+ *
+ */
+package org.apache.directory.mavibot.btree;
+
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+
+import org.apache.directory.mavibot.btree.exception.SerializerCreationException;
+import org.apache.directory.mavibot.btree.serializer.AbstractElementSerializer;
+import org.apache.directory.mavibot.btree.serializer.BufferHandler;
+import org.apache.directory.mavibot.btree.serializer.ByteArraySerializer;
+import org.apache.directory.mavibot.btree.serializer.IntSerializer;
+import org.apache.directory.mavibot.btree.serializer.LongSerializer;
+import org.apache.directory.mavibot.btree.serializer.StringSerializer;
+import org.apache.directory.mavibot.btree.util.Strings;
+
+
+/**
+ * A serializer for the NameRevision object. The NameRevision will be serialized
+ * as a String ( the Name) followed by the revision as a Long.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+/* no qualifier*/class NameRevisionSerializer extends AbstractElementSerializer<NameRevision>
+{
+ /**
+ * Create a new instance of a NameRevisionSerializer
+ */
+ /* no qualifier*/NameRevisionSerializer()
+ {
+ super( new NameRevisionComparator() );
+ }
+
+
+ /**
+ * A static method used to deserialize a NameRevision from a byte array.
+ *
+ * @param in The byte array containing the NameRevision
+ * @return A NameRevision instance
+ */
+ /* no qualifier*/static NameRevision deserialize( byte[] in )
+ {
+ return deserialize( in, 0 );
+ }
+
+
+ /**
+ * A static method used to deserialize a NameRevision from a byte array.
+ *
+ * @param in The byte array containing the NameRevision
+ * @param start the position in the byte[] we will deserialize the NameRevision from
+ * @return A NameRevision instance
+ */
+ /* no qualifier*/static NameRevision deserialize( byte[] in, int start )
+ {
+ // The buffer must be 8 bytes plus 4 bytes long (the revision is a long, and the name is a String
+ if ( ( in == null ) || ( in.length < 12 + start ) )
+ {
+ throw new SerializerCreationException( "Cannot extract a NameRevision from a buffer with not enough bytes" );
+ }
+
+ long revision = LongSerializer.deserialize( in, start );
+ String name = StringSerializer.deserialize( in, 8 + start );
+
+ NameRevision revisionName = new NameRevision( name, revision );
+
+ return revisionName;
+ }
+
+
+ /**
+ * A static method used to deserialize a NameRevision from a byte array.
+ *
+ * @param in The byte array containing the NameRevision
+ * @return A NameRevision instance
+ */
+ public NameRevision fromBytes( byte[] in )
+ {
+ return deserialize( in, 0 );
+ }
+
+
+ /**
+ * A static method used to deserialize a NameRevision from a byte array.
+ *
+ * @param in The byte array containing the NameRevision
+ * @param start the position in the byte[] we will deserialize the NameRevision from
+ * @return A NameRevision instance
+ */
+ public NameRevision fromBytes( byte[] in, int start )
+ {
+ // The buffer must be 8 bytes plus 4 bytes long (the revision is a long, and the name is a String
+ if ( ( in == null ) || ( in.length < 12 + start ) )
+ {
+ throw new SerializerCreationException( "Cannot extract a NameRevision from a buffer with not enough bytes" );
+ }
+
+ long revision = LongSerializer.deserialize( in, start );
+ String name = StringSerializer.deserialize( in, 8 + start );
+
+ NameRevision revisionName = new NameRevision( name, revision );
+
+ return revisionName;
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public byte[] serialize( NameRevision revisionName )
+ {
+ if ( revisionName == null )
+ {
+ throw new SerializerCreationException( "The revisionName instance should not be null " );
+ }
+
+ byte[] result = null;
+
+ if ( revisionName.getName() != null )
+ {
+ byte[] stringBytes = Strings.getBytesUtf8( revisionName.getName() );
+ int stringLen = stringBytes.length;
+ result = new byte[8 + 4 + stringBytes.length];
+ LongSerializer.serialize( result, 0, revisionName.getRevision() );
+
+ if ( stringLen > 0 )
+ {
+ ByteArraySerializer.serialize( result, 8, stringBytes );
+ }
+ }
+ else
+ {
+ result = new byte[8 + 4];
+ LongSerializer.serialize( result, 0, revisionName.getRevision() );
+ StringSerializer.serialize( result, 8, null );
+ }
+
+ return result;
+ }
+
+
+ /**
+ * Serialize a NameRevision
+ *
+ * @param buffer the Buffer that will contain the serialized value
+ * @param start the position in the buffer we will store the serialized NameRevision
+ * @param value the value to serialize
+ * @return The byte[] containing the serialized NameRevision
+ */
+ /* no qualifier*/static byte[] serialize( byte[] buffer, int start, NameRevision revisionName )
+ {
+ if ( revisionName.getName() != null )
+ {
+ byte[] stringBytes = Strings.getBytesUtf8( revisionName.getName() );
+ int stringLen = stringBytes.length;
+ LongSerializer.serialize( buffer, start, revisionName.getRevision() );
+ IntSerializer.serialize( buffer, 8 + start, stringLen );
+ ByteArraySerializer.serialize( buffer, 12 + start, stringBytes );
+ }
+ else
+ {
+ LongSerializer.serialize( buffer, start, revisionName.getRevision() );
+ StringSerializer.serialize( buffer, 8, null );
+ }
+
+ return buffer;
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public NameRevision deserialize( BufferHandler bufferHandler ) throws IOException
+ {
+ byte[] revisionBytes = bufferHandler.read( 8 );
+ long revision = LongSerializer.deserialize( revisionBytes );
+
+ byte[] lengthBytes = bufferHandler.read( 4 );
+
+ int len = IntSerializer.deserialize( lengthBytes );
+
+ switch ( len )
+ {
+ case 0:
+ return new NameRevision( "", revision );
+
+ case -1:
+ return new NameRevision( null, revision );
+
+ default:
+ byte[] nameBytes = bufferHandler.read( len );
+
+ return new NameRevision( Strings.utf8ToString( nameBytes ), revision );
+ }
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public NameRevision deserialize( ByteBuffer buffer ) throws IOException
+ {
+ // The revision
+ long revision = buffer.getLong();
+
+ // The name's length
+ int len = buffer.getInt();
+
+ switch ( len )
+ {
+ case 0:
+ return new NameRevision( "", revision );
+
+ case -1:
+ return new NameRevision( null, revision );
+
+ default:
+ byte[] nameBytes = new byte[len];
+ buffer.get( nameBytes );
+
+ return new NameRevision( Strings.utf8ToString( nameBytes ), revision );
+ }
+ }
+}
Modified: directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/RevisionName.java
URL: http://svn.apache.org/viewvc/directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/RevisionName.java?rev=1553897&r1=1553896&r2=1553897&view=diff
==============================================================================
--- directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/RevisionName.java (original)
+++ directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree/RevisionName.java Sat Dec 28 19:44:21 2013
@@ -26,15 +26,8 @@ package org.apache.directory.mavibot.btr
*
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
*/
-/* no qualifier*/class RevisionName
+/* no qualifier*/class RevisionName extends Tuple<Long, String>
{
- /** The revision number on the BTree */
- private long revision;
-
- /** The BTree name */
- private String name;
-
-
/**
* A constructor for the RevisionName class
* @param revision The revision
@@ -42,8 +35,7 @@ package org.apache.directory.mavibot.btr
*/
/* no qualifier*/RevisionName( long revision, String name )
{
- this.revision = revision;
- this.name = name;
+ super( revision, name );
}
@@ -52,7 +44,7 @@ package org.apache.directory.mavibot.btr
*/
/* no qualifier*/long getRevision()
{
- return revision;
+ return getKey();
}
@@ -61,7 +53,7 @@ package org.apache.directory.mavibot.btr
*/
/* no qualifier*/void setRevision( long revision )
{
- this.revision = revision;
+ setKey( revision );
}
@@ -70,7 +62,7 @@ package org.apache.directory.mavibot.btr
*/
/* no qualifier*/String getName()
{
- return name;
+ return getValue();
}
@@ -79,7 +71,7 @@ package org.apache.directory.mavibot.btr
*/
/* no qualifier*/void setName( String name )
{
- this.name = name;
+ setValue( name );
}
@@ -100,17 +92,17 @@ package org.apache.directory.mavibot.btr
RevisionName revisionName = ( RevisionName ) that;
- if ( revision != revisionName.revision )
+ if ( getRevision() != revisionName.getRevision() )
{
return false;
}
- if ( name == null )
+ if ( getName() == null )
{
- return revisionName.name == null;
+ return revisionName.getName() == null;
}
- return ( name.equals( revisionName.name ) );
+ return ( getName().equals( revisionName.getName() ) );
}
@@ -120,8 +112,8 @@ package org.apache.directory.mavibot.btr
{
final int prime = 31;
int result = 1;
- result = prime * result + ( ( name == null ) ? 0 : name.hashCode() );
- result = prime * result + ( int ) ( revision ^ ( revision >>> 32 ) );
+ result = prime * result + ( ( getName() == null ) ? 0 : getName().hashCode() );
+ result = prime * result + ( int ) ( getRevision() ^ ( getRevision() >>> 32 ) );
return result;
}
@@ -131,6 +123,6 @@ package org.apache.directory.mavibot.btr
*/
public String toString()
{
- return "[" + name + ":" + revision + "]";
+ return "[" + getRevision() + ":" + getName() + "]";
}
}