Return-Path: X-Original-To: apmail-directory-commits-archive@www.apache.org Delivered-To: apmail-directory-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 14DCD10A16 for ; Sat, 28 Dec 2013 19:44:48 +0000 (UTC) Received: (qmail 39135 invoked by uid 500); 28 Dec 2013 19:44:48 -0000 Delivered-To: apmail-directory-commits-archive@directory.apache.org Received: (qmail 39103 invoked by uid 500); 28 Dec 2013 19:44:48 -0000 Mailing-List: contact commits-help@directory.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@directory.apache.org Delivered-To: mailing list commits@directory.apache.org Received: (qmail 39096 invoked by uid 99); 28 Dec 2013 19:44:48 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 28 Dec 2013 19:44:48 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 28 Dec 2013 19:44:44 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 0A1CF238890B; Sat, 28 Dec 2013 19:44:22 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1553897 - in /directory/mavibot/trunk/mavibot/src/main/java/org/apache/directory/mavibot/btree: NameRevision.java NameRevisionComparator.java NameRevisionSerializer.java RevisionName.java Date: Sat, 28 Dec 2013 19:44:21 -0000 To: commits@directory.apache.org From: elecharny@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20131228194422.0A1CF238890B@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 Apache Directory Project + */ +/* no qualifier*/class NameRevision extends Tuple +{ + /** + * 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 Apache Directory Project + */ +/* no qualifier*/class NameRevisionComparator implements Comparator +{ + /** + * {@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 Apache Directory Project + */ +/* no qualifier*/class NameRevisionSerializer extends AbstractElementSerializer +{ + /** + * 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 Apache Directory Project */ -/* no qualifier*/class RevisionName +/* no qualifier*/class RevisionName extends Tuple { - /** 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() + "]"; } }