directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r1301557 - in /directory/apacheds/branches/apacheds-txns: core-api/src/main/java/org/apache/directory/server/core/api/partition/index/ core-shared/src/main/java/org/apache/directory/server/core/shared/txn/logedit/ core-shared/src/test/java/...
Date Fri, 16 Mar 2012 14:44:59 GMT
Author: elecharny
Date: Fri Mar 16 14:44:58 2012
New Revision: 1301557

URL: http://svn.apache.org/viewvc?rev=1301557&view=rev
Log:
o Removed all the calls to readObject/writeObject in the various read/writeExternal calls
o Added a type for the DataChange being serialized so that we can deserialize them
o Added a test for the UserRecord serialization

Added:
    directory/apacheds/branches/apacheds-txns/core-shared/src/test/java/org/apache/directory/server/core/shared/log/UserLogRecordTest.java
Modified:
    directory/apacheds/branches/apacheds-txns/core-api/src/main/java/org/apache/directory/server/core/api/partition/index/ParentIdAndRdn.java
    directory/apacheds/branches/apacheds-txns/core-shared/src/main/java/org/apache/directory/server/core/shared/txn/logedit/DataChangeContainer.java
    directory/apacheds/branches/apacheds-txns/core-shared/src/main/java/org/apache/directory/server/core/shared/txn/logedit/IndexChange.java
    directory/apacheds/branches/apacheds-txns/core-shared/src/main/java/org/apache/directory/server/core/shared/txn/logedit/TxnStateChange.java

Modified: directory/apacheds/branches/apacheds-txns/core-api/src/main/java/org/apache/directory/server/core/api/partition/index/ParentIdAndRdn.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-txns/core-api/src/main/java/org/apache/directory/server/core/api/partition/index/ParentIdAndRdn.java?rev=1301557&r1=1301556&r2=1301557&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-txns/core-api/src/main/java/org/apache/directory/server/core/api/partition/index/ParentIdAndRdn.java
(original)
+++ directory/apacheds/branches/apacheds-txns/core-api/src/main/java/org/apache/directory/server/core/api/partition/index/ParentIdAndRdn.java
Fri Mar 16 14:44:58 2012
@@ -209,7 +209,8 @@ public class ParentIdAndRdn implements E
 
     public void writeExternal( ObjectOutput out ) throws IOException
     {
-        out.writeObject( parentId );
+        out.writeLong( parentId.getMostSignificantBits() );
+        out.writeLong( parentId.getLeastSignificantBits() );
         out.writeInt( rdns.length );
 
         for ( Rdn rdn : rdns )
@@ -222,7 +223,10 @@ public class ParentIdAndRdn implements E
     @SuppressWarnings("unchecked")
     public void readExternal( ObjectInput in ) throws IOException, ClassNotFoundException
     {
-        parentId = ( UUID ) in.readObject();
+        long mostSignificantBits = in.readLong();
+        long leastSignificantBits = in.readLong();
+
+        parentId = new UUID( mostSignificantBits, leastSignificantBits );
         int size = in.readInt();
         rdns = new Rdn[size];
 

Modified: directory/apacheds/branches/apacheds-txns/core-shared/src/main/java/org/apache/directory/server/core/shared/txn/logedit/DataChangeContainer.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-txns/core-shared/src/main/java/org/apache/directory/server/core/shared/txn/logedit/DataChangeContainer.java?rev=1301557&r1=1301556&r2=1301557&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-txns/core-shared/src/main/java/org/apache/directory/server/core/shared/txn/logedit/DataChangeContainer.java
(original)
+++ directory/apacheds/branches/apacheds-txns/core-shared/src/main/java/org/apache/directory/server/core/shared/txn/logedit/DataChangeContainer.java
Fri Mar 16 14:44:58 2012
@@ -61,6 +61,15 @@ public class DataChangeContainer extends
     /** List of data changes */
     private List<DataChange> changes = new LinkedList<DataChange>();
 
+    /** The type of change */
+    private enum ChangeType
+    {
+        INDEX_MODIFICATION,
+        ENTRY_ADD_DELETE,
+        ENTRY_CHANGE,
+        ENTRY_REPLACE
+    }
+
 
     //For externalizable
     public DataChangeContainer()
@@ -266,12 +275,33 @@ public class DataChangeContainer extends
 
         // Here, we must find a way to find the Partition from its Dn
 
-        DataChange change;
+        DataChange change = null;
         int numChanges = in.readInt();
 
         for ( int idx = 0; idx < numChanges; idx++ )
         {
-            change = ( DataChange ) in.readObject();
+            ChangeType changeType = ChangeType.values()[in.read()];
+
+            switch ( changeType )
+            {
+                case ENTRY_ADD_DELETE:
+                    change = new EntryAddDelete();
+                    break;
+
+                case ENTRY_CHANGE:
+                    change = new EntryChange();
+                    break;
+
+                case ENTRY_REPLACE:
+                    change = new EntryReplace();
+                    break;
+
+                case INDEX_MODIFICATION:
+                    change = new IndexChange();
+                    break;
+            }
+
+            change.readExternal( in );
             changes.add( change );
         }
     }
@@ -303,6 +333,24 @@ public class DataChangeContainer extends
         while ( it.hasNext() )
         {
             change = it.next();
+
+            if ( change instanceof IndexModification )
+            {
+                out.write( ChangeType.INDEX_MODIFICATION.ordinal() );
+            }
+            else if ( change instanceof EntryAddDelete )
+            {
+                out.write( ChangeType.ENTRY_ADD_DELETE.ordinal() );
+            }
+            else if ( change instanceof EntryChange )
+            {
+                out.write( ChangeType.ENTRY_CHANGE.ordinal() );
+            }
+            else if ( change instanceof EntryReplace )
+            {
+                out.write( ChangeType.ENTRY_REPLACE.ordinal() );
+            }
+
             change.writeExternal( out );
         }
     }

Modified: directory/apacheds/branches/apacheds-txns/core-shared/src/main/java/org/apache/directory/server/core/shared/txn/logedit/IndexChange.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-txns/core-shared/src/main/java/org/apache/directory/server/core/shared/txn/logedit/IndexChange.java?rev=1301557&r1=1301556&r2=1301557&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-txns/core-shared/src/main/java/org/apache/directory/server/core/shared/txn/logedit/IndexChange.java
(original)
+++ directory/apacheds/branches/apacheds-txns/core-shared/src/main/java/org/apache/directory/server/core/shared/txn/logedit/IndexChange.java
Fri Mar 16 14:44:58 2012
@@ -27,6 +27,7 @@ import java.util.UUID;
 
 import org.apache.directory.server.core.api.partition.Partition;
 import org.apache.directory.server.core.api.partition.index.Index;
+import org.apache.directory.server.core.api.partition.index.ParentIdAndRdn;
 import org.apache.directory.server.core.api.txn.logedit.IndexModification;
 
 
@@ -164,7 +165,7 @@ public class IndexChange implements Inde
     public void readExternal( ObjectInput in ) throws IOException, ClassNotFoundException
     {
         oid = in.readUTF();
-        KeyType keyType = KeyType.getType( in.readByte() );
+        KeyType keyType = KeyType.values()[in.readByte()];
 
         switch ( keyType )
         {
@@ -182,6 +183,19 @@ public class IndexChange implements Inde
                 in.read( ( byte[] ) key );
                 break;
 
+            case RDN:
+                ParentIdAndRdn parentIdAndRdn = new ParentIdAndRdn();
+                parentIdAndRdn.readExternal( in );
+                key = parentIdAndRdn;
+                break;
+
+            case UUID:
+                long mostSignificantBits = in.readLong();
+                long leastSignificantBits = in.readLong();
+                UUID uuid = new UUID( mostSignificantBits, leastSignificantBits );
+                key = uuid;
+                break;
+
             case OBJECT:
                 key = in.readObject();
                 break;
@@ -199,23 +213,35 @@ public class IndexChange implements Inde
 
         if ( key instanceof String )
         {
-            out.write( KeyType.STRING.getOrdinal() );
+            out.write( KeyType.STRING.ordinal() );
             out.writeUTF( ( String ) key );
         }
         else if ( key instanceof byte[] )
         {
-            out.write( KeyType.BYTES.getOrdinal() );
+            out.write( KeyType.BYTES.ordinal() );
             out.writeInt( ( ( byte[] ) key ).length );
             out.write( ( byte[] ) key );
         }
         else if ( key instanceof Long )
         {
-            out.write( KeyType.LONG.getOrdinal() );
+            out.write( KeyType.LONG.ordinal() );
             out.writeLong( ( Long ) key );
         }
+        else if ( key instanceof ParentIdAndRdn )
+        {
+            out.write( KeyType.RDN.ordinal() );
+            ( ( ParentIdAndRdn ) key ).writeExternal( out );
+        }
+        else if ( key instanceof UUID )
+        {
+            out.write( KeyType.UUID.ordinal() );
+            UUID uuid = ( UUID ) key;
+            out.writeLong( uuid.getMostSignificantBits() );
+            out.writeLong( uuid.getLeastSignificantBits() );
+        }
         else
         {
-            out.write( KeyType.OBJECT.getOrdinal() );
+            out.write( KeyType.OBJECT.ordinal() );
             out.writeObject( key );
         }
 
@@ -258,39 +284,11 @@ public class IndexChange implements Inde
 
     private enum KeyType
     {
-        STRING(0),
-        LONG(1),
-        BYTES(2),
-        OBJECT(3);
-
-        private int value;
-
-
-        private KeyType( int value )
-        {
-            this.value = value;
-        }
-
-
-        private byte getOrdinal()
-        {
-            return ( byte ) value;
-        }
-
-
-        private static KeyType getType( byte value )
-        {
-            switch ( value )
-            {
-                case 0:
-                    return STRING;
-                case 1:
-                    return LONG;
-                case 2:
-                    return BYTES;
-                default:
-                    return OBJECT;
-            }
-        }
+        STRING,
+        LONG,
+        BYTES,
+        RDN,
+        UUID,
+        OBJECT;
     }
 }

Modified: directory/apacheds/branches/apacheds-txns/core-shared/src/main/java/org/apache/directory/server/core/shared/txn/logedit/TxnStateChange.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-txns/core-shared/src/main/java/org/apache/directory/server/core/shared/txn/logedit/TxnStateChange.java?rev=1301557&r1=1301556&r2=1301557&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-txns/core-shared/src/main/java/org/apache/directory/server/core/shared/txn/logedit/TxnStateChange.java
(original)
+++ directory/apacheds/branches/apacheds-txns/core-shared/src/main/java/org/apache/directory/server/core/shared/txn/logedit/TxnStateChange.java
Fri Mar 16 14:44:58 2012
@@ -92,7 +92,7 @@ public class TxnStateChange extends Abst
     public void readExternal( ObjectInput in ) throws IOException, ClassNotFoundException
     {
         txnID = in.readLong();
-        txnState = ChangeState.values()[in.readInt()];
+        txnState = ChangeState.values()[in.read()];
     }
 
 
@@ -100,7 +100,7 @@ public class TxnStateChange extends Abst
     public void writeExternal( ObjectOutput out ) throws IOException
     {
         out.writeLong( txnID );
-        out.writeInt( txnState.ordinal() );
+        out.write( txnState.ordinal() );
     }
 
 

Added: directory/apacheds/branches/apacheds-txns/core-shared/src/test/java/org/apache/directory/server/core/shared/log/UserLogRecordTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-txns/core-shared/src/test/java/org/apache/directory/server/core/shared/log/UserLogRecordTest.java?rev=1301557&view=auto
==============================================================================
--- directory/apacheds/branches/apacheds-txns/core-shared/src/test/java/org/apache/directory/server/core/shared/log/UserLogRecordTest.java
(added)
+++ directory/apacheds/branches/apacheds-txns/core-shared/src/test/java/org/apache/directory/server/core/shared/log/UserLogRecordTest.java
Fri Mar 16 14:44:58 2012
@@ -0,0 +1,197 @@
+/*
+ *   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.server.core.shared.log;
+
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.util.UUID;
+
+import org.apache.directory.server.core.api.log.UserLogRecord;
+import org.apache.directory.server.core.api.log.UserLogRecord.LogEditType;
+import org.apache.directory.server.core.api.partition.Partition;
+import org.apache.directory.server.core.api.partition.index.GenericIndex;
+import org.apache.directory.server.core.api.partition.index.Index;
+import org.apache.directory.server.core.api.txn.logedit.DataChange;
+import org.apache.directory.server.core.shared.txn.logedit.DataChangeContainer;
+import org.apache.directory.server.core.shared.txn.logedit.EntryAddDelete;
+import org.apache.directory.server.core.shared.txn.logedit.EntryChange;
+import org.apache.directory.server.core.shared.txn.logedit.EntryReplace;
+import org.apache.directory.server.core.shared.txn.logedit.IndexChange;
+import org.apache.directory.server.core.shared.txn.logedit.IndexChange.Type;
+import org.apache.directory.server.core.shared.txn.logedit.TxnStateChange;
+import org.apache.directory.server.core.shared.txn.logedit.TxnStateChange.ChangeState;
+import org.apache.directory.server.core.shared.txn.utils.MockPartition;
+import org.apache.directory.shared.ldap.model.entry.DefaultEntry;
+import org.apache.directory.shared.ldap.model.entry.Entry;
+import org.apache.directory.shared.ldap.model.name.Dn;
+import org.junit.Test;
+
+
+/**
+ * A test class for the UserLogRecord class
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class UserLogRecordTest
+{
+    private ObjectInputStream buildStream( byte[] buffer ) throws IOException
+    {
+        ObjectInputStream oIn = null;
+        ByteArrayInputStream in = new ByteArrayInputStream( buffer );
+
+        try
+        {
+            oIn = new ObjectInputStream( in );
+
+            return oIn;
+        }
+        catch ( IOException ioe )
+        {
+            throw ioe;
+        }
+    }
+
+
+    @Test
+    public void testTxnStateChangeSerializationBegin() throws Exception
+    {
+        TxnStateChange txnStateChange = new TxnStateChange( 1L, ChangeState.TXN_BEGIN );
+
+        UserLogRecord userLogRecord = new UserLogRecord();
+        txnStateChange.injectData( userLogRecord, LogEditType.TXN );
+
+        TxnStateChange txnDeser = new TxnStateChange();
+        txnDeser.readExternal( buildStream( userLogRecord.getDataBuffer() ) );
+
+        assertEquals( txnStateChange.getTxnID(), txnDeser.getTxnID() );
+        assertEquals( txnStateChange.getTxnState(), txnDeser.getTxnState() );
+    }
+
+
+    @Test
+    public void testTxnStateChangeSerializationCommit() throws Exception
+    {
+        TxnStateChange txnStateChange = new TxnStateChange( 1L, ChangeState.TXN_COMMIT );
+
+        UserLogRecord userLogRecord = new UserLogRecord();
+        txnStateChange.injectData( userLogRecord, LogEditType.TXN );
+
+        TxnStateChange txnDeser = new TxnStateChange();
+        txnDeser.readExternal( buildStream( userLogRecord.getDataBuffer() ) );
+
+        assertEquals( txnStateChange.getTxnID(), txnDeser.getTxnID() );
+        assertEquals( txnStateChange.getTxnState(), txnDeser.getTxnState() );
+    }
+
+
+    @Test
+    public void testTxnStateChangeSerializationAbort() throws Exception
+    {
+        TxnStateChange txnStateChange = new TxnStateChange( 1L, ChangeState.TXN_ABORT );
+
+        UserLogRecord userLogRecord = new UserLogRecord();
+        txnStateChange.injectData( userLogRecord, LogEditType.TXN );
+
+        TxnStateChange txnDeser = new TxnStateChange();
+        txnDeser.readExternal( buildStream( userLogRecord.getDataBuffer() ) );
+
+        assertEquals( txnStateChange.getTxnID(), txnDeser.getTxnID() );
+        assertEquals( txnStateChange.getTxnState(), txnDeser.getTxnState() );
+    }
+
+
+    @Test
+    public void testDataChangeContainerSerialization() throws Exception
+    {
+        Partition partition = new MockPartition( new Dn( "dc=test" ) );
+        DataChangeContainer dataChangeContainer = new DataChangeContainer( partition );
+
+        dataChangeContainer.setEntryID( UUID.randomUUID() );
+        dataChangeContainer.setTxnID( 1L );
+
+        Index<UUID> index = new GenericIndex<UUID>( "entryUUID" );
+
+        DataChange indexChange1 = new IndexChange( index, UUID.randomUUID(), UUID.randomUUID(),
Type.ADD, true );
+        DataChange indexChange2 = new IndexChange( index, UUID.randomUUID(), UUID.randomUUID(),
Type.ADD, true );
+        Entry entry = new DefaultEntry(
+            "dc=example,dc=com",
+            "objectClass:top",
+            "objectClass:person",
+            "cn: test",
+            "sn:test"
+            );
+
+        DataChange entryAddDelete = new EntryAddDelete( entry, EntryAddDelete.Type.DELETE
);
+
+        dataChangeContainer.addChange( indexChange1 );
+        dataChangeContainer.addChange( indexChange2 );
+        dataChangeContainer.addChange( entryAddDelete );
+
+        UserLogRecord userLogRecord = new UserLogRecord();
+        dataChangeContainer.injectData( userLogRecord, LogEditType.DATA );
+
+        DataChangeContainer dataDeser = new DataChangeContainer();
+        dataDeser.readExternal( buildStream( userLogRecord.getDataBuffer() ) );
+
+        assertEquals( dataChangeContainer.getTxnID(), dataDeser.getTxnID() );
+        assertEquals( dataChangeContainer.getEntryID(), dataDeser.getEntryID() );
+        assertEquals( dataChangeContainer.getPartitionDn(), dataDeser.getPartitionDn() );
+        assertEquals( dataChangeContainer.getChanges().size(), dataDeser.getChanges().size()
);
+
+        for ( int i = 0; i < dataChangeContainer.getChanges().size(); i++ )
+        {
+            DataChange originalChange = dataChangeContainer.getChanges().get( i );
+            DataChange deserChange = dataDeser.getChanges().get( i );
+
+            if ( originalChange instanceof IndexChange )
+            {
+                assertEquals( ( ( IndexChange ) originalChange ).getID(), ( ( IndexChange
) deserChange ).getID() );
+                assertEquals( ( ( IndexChange ) originalChange ).getKey(), ( ( IndexChange
) deserChange ).getKey() );
+                assertEquals( ( ( IndexChange ) originalChange ).getOID(), ( ( IndexChange
) deserChange ).getOID() );
+                assertEquals( ( ( IndexChange ) originalChange ).getType(), ( ( IndexChange
) deserChange ).getType() );
+            }
+            else if ( originalChange instanceof EntryAddDelete )
+            {
+                assertEquals( ( ( EntryAddDelete ) originalChange ).getType(),
+                    ( ( EntryAddDelete ) deserChange ).getType() );
+                assertEquals( ( ( EntryAddDelete ) originalChange ).getChangedEntry(),
+                    ( ( EntryAddDelete ) deserChange ).getChangedEntry() );
+            }
+            else if ( originalChange instanceof EntryChange )
+            {
+                assertEquals( ( ( EntryChange ) originalChange ).getRedoChange(),
+                    ( ( EntryChange ) deserChange ).getRedoChange() );
+                assertEquals( ( ( EntryChange ) originalChange ).getUndoChange(),
+                    ( ( EntryChange ) deserChange ).getUndoChange() );
+            }
+            else if ( originalChange instanceof EntryReplace )
+            {
+                assertEquals( ( ( EntryReplace ) originalChange ).getNewEntry(),
+                    ( ( EntryReplace ) deserChange ).getNewEntry() );
+                assertEquals( ( ( EntryReplace ) originalChange ).getOldEntry(),
+                    ( ( EntryReplace ) deserChange ).getOldEntry() );
+            }
+        }
+    }
+}



Mime
View raw message