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 */ 020package org.apache.directory.server.core.partition.impl.btree.jdbm; 021 022 023import java.io.ByteArrayOutputStream; 024import java.io.IOException; 025import java.io.ObjectOutputStream; 026 027import org.apache.directory.server.core.avltree.ArrayTree; 028import org.slf4j.Logger; 029import org.slf4j.LoggerFactory; 030 031import jdbm.btree.BTree; 032import jdbm.helper.Serializer; 033 034 035/** 036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 037 */ 038public class IndexValueSerializer implements Serializer 039{ 040 private static final long serialVersionUID = 1L; 041 042 /** the flag for a Long value*/ 043 private static final byte LONG_VALUE = 0; 044 045 /** the flag for a AvlTree value*/ 046 private static final byte AVL_TREE_VALUE = 0; 047 048 /** the flag for a BTree value*/ 049 private static final byte BTREE_VALUE = 0; 050 051 /** the logger for this class */ 052 private static final Logger LOG = LoggerFactory.getLogger( IndexValueSerializer.class ); 053 054 055 public Object deserialize( byte[] serialized ) throws IOException 056 { 057 return null; 058 } 059 060 061 /** 062 * Serialize the object. It can be a long, a BTree or an AvlTree 063 * 064 * @param obj The object to serialize 065 * @return a byte[] containing the serialized value 066 * @throws IOException If the serialization failed 067 */ 068 public byte[] serialize( Object obj ) throws IOException 069 { 070 if ( obj instanceof ArrayTree ) 071 { 072 LOG.debug( "Serializing an AvlTree" ); 073 return serialize( ( ArrayTree<?> ) obj ); 074 } 075 else if ( obj instanceof BTree ) 076 { 077 LOG.debug( "Serializing a BTree" ); 078 return serialize( ( BTree ) obj ); 079 } 080 else 081 { 082 LOG.debug( "Serializing a long [{}]", obj ); 083 return serialize( ( Long ) obj ); 084 } 085 } 086 087 088 /** 089 * Serialize a Long value 090 */ 091 private byte[] serialize( Long value ) throws IOException 092 { 093 try ( ByteArrayOutputStream baos = new ByteArrayOutputStream(); 094 ObjectOutputStream out = new ObjectOutputStream( baos ) ) 095 { 096 097 // First, write the type 098 out.write( LONG_VALUE ); 099 100 // Now, flush the Long 101 out.writeLong( value ); 102 103 // And return the result 104 out.flush(); 105 106 if ( LOG.isDebugEnabled() ) 107 { 108 LOG.debug( ">------------------------------------------------" ); 109 LOG.debug( "Serializes a LONG value" ); 110 } 111 112 return baos.toByteArray(); 113 } 114 } 115 116 117 /** 118 * Serialize a BTree value 119 */ 120 private byte[] serialize( BTree bTree ) throws IOException 121 { 122 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 123 ObjectOutputStream out = new ObjectOutputStream( baos ); 124 125 // First, write the type 126 out.write( BTREE_VALUE ); 127 128 // Marshal the BTree here. 129 // TODO : add the code 130 131 out.flush(); 132 133 if ( LOG.isDebugEnabled() ) 134 { 135 LOG.debug( ">------------------------------------------------" ); 136 LOG.debug( "Serializes an BTree" ); 137 } 138 139 return baos.toByteArray(); 140 } 141 142 143 /** 144 * Serialize a AvlTree value 145 */ 146 private byte[] serialize( ArrayTree<?> arrayTree ) throws IOException 147 { 148 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 149 ObjectOutputStream out = new ObjectOutputStream( baos ); 150 151 // First, write the type 152 out.write( AVL_TREE_VALUE ); 153 154 // Marshal the AvlTree here. 155 // TODO : add the code 156 157 out.flush(); 158 159 if ( LOG.isDebugEnabled() ) 160 { 161 LOG.debug( ">------------------------------------------------" ); 162 LOG.debug( "Serializes an AVL tree" ); 163 } 164 165 return baos.toByteArray(); 166 } 167}