001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.server.core.partition.impl.btree.jdbm;
021    
022    import java.io.ByteArrayOutputStream;
023    import java.io.IOException;
024    import java.io.ObjectOutputStream;
025    
026    import org.apache.directory.server.core.avltree.ArrayTree;
027    import org.slf4j.Logger;
028    import org.slf4j.LoggerFactory;
029    
030    import jdbm.btree.BTree;
031    import jdbm.helper.Serializer;
032    
033    /**
034     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035     * @version $Rev$, $Date$
036     */
037    public class IndexValueSerializer implements Serializer
038    {
039        private static final long serialVersionUID = 1L;
040        
041        /** the flag for a Long value*/
042        private static byte LONG_VALUE = 0;
043    
044        /** the flag for a AvlTree value*/
045        private static byte AVL_TREE_VALUE = 0;
046    
047        /** the flag for a BTree value*/
048        private static byte BTREE_VALUE = 0;
049    
050        /** the logger for this class */
051        private static final Logger LOG = LoggerFactory.getLogger( IndexValueSerializer.class );
052    
053        public Object deserialize( byte[] serialized ) throws IOException
054        {
055            return null;
056        }
057    
058        /**
059         * Serialize the object. It can be a long, a BTree or an AvlTree
060         * 
061         * @param obj The object to serialize
062         * @return a byte[] containing the serialized value
063         * @throws IOException If the serialization failed
064         */
065        public byte[] serialize( Object obj ) throws IOException
066        {
067            if ( obj instanceof ArrayTree )
068            {
069                LOG.debug( "Serializing an AvlTree" );
070                return serialize( (ArrayTree<?>)obj );
071            }
072            else if ( obj instanceof BTree )
073            {
074                LOG.debug( "Serializing a BTree" );
075                return serialize( (BTree)obj );
076            }
077            else
078            {
079                LOG.debug( "Serializing a long [{}]", obj );
080                return serialize( (Long)obj );
081            }
082        }
083    
084        
085        /**
086         * Serialize a Long value
087         */
088        private byte[] serialize( Long value ) throws IOException
089        {
090            ByteArrayOutputStream baos = new ByteArrayOutputStream();
091            ObjectOutputStream out = new ObjectOutputStream( baos );
092    
093            // First, write the type
094            out.write( LONG_VALUE );
095            
096            // Now, flush the Long 
097            out.writeLong( value );
098            
099            // And return the result
100            out.flush();
101    
102            if ( LOG.isDebugEnabled() )
103            {
104                LOG.debug( ">------------------------------------------------" );
105                LOG.debug( "Serializes a LONG value" );
106            }
107    
108            return baos.toByteArray();
109        }
110    
111        
112        /**
113         * Serialize an AvlTree value
114         */
115        private byte[] serialize( BTree bTree ) throws IOException
116        {
117            ByteArrayOutputStream baos = new ByteArrayOutputStream();
118            ObjectOutputStream out = new ObjectOutputStream( baos );
119    
120            // First, write the type
121            out.write( AVL_TREE_VALUE );
122            
123            // Marshal the AvlTree here. 
124            // TODO : add the code
125    
126            out.flush();
127            
128            if ( LOG.isDebugEnabled() )
129            {
130                LOG.debug( ">------------------------------------------------" );
131                LOG.debug( "Serializes an AVL tree" );
132            }
133    
134            return baos.toByteArray();
135        }
136    
137        
138        /**
139         * Serialize a AvlTree value
140         */
141        private byte[] serialize( ArrayTree<?> arrayTree ) throws IOException
142        {
143            ByteArrayOutputStream baos = new ByteArrayOutputStream();
144            ObjectOutputStream out = new ObjectOutputStream( baos );
145    
146            // First, write the type
147            out.write( BTREE_VALUE );
148            
149            out.flush();
150    
151            if ( LOG.isDebugEnabled() )
152            {
153                LOG.debug( ">------------------------------------------------" );
154                LOG.debug( "Serializes an AVL tree" );
155            }
156    
157            return baos.toByteArray();
158        }
159    }