View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.directory.mavibot.btree.serializer;
21  
22  
23  import java.io.IOException;
24  import java.nio.ByteBuffer;
25  
26  import org.apache.directory.mavibot.btree.comparator.LongArrayComparator;
27  
28  
29  /**
30   * A serializer for a Long[].
31   * 
32   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
33   */
34  public class LongArraySerializer extends AbstractElementSerializer<long[]>
35  {
36      /**
37       * Create a new instance of LongSerializer
38       */
39      public LongArraySerializer()
40      {
41          super( new LongArrayComparator() );
42      }
43  
44  
45      /**
46       * {@inheritDoc}
47       */
48      public byte[] serialize( long[] element )
49      {
50          int len = -1;
51  
52          if ( element != null )
53          {
54              len = element.length;
55          }
56  
57          byte[] bytes = null;
58  
59          switch ( len )
60          {
61              case 0:
62                  bytes = new byte[4];
63  
64                  bytes[0] = 0x00;
65                  bytes[1] = 0x00;
66                  bytes[2] = 0x00;
67                  bytes[3] = 0x00;
68  
69                  break;
70  
71              case -1:
72                  bytes = new byte[4];
73  
74                  bytes[0] = ( byte ) 0xFF;
75                  bytes[1] = ( byte ) 0xFF;
76                  bytes[2] = ( byte ) 0xFF;
77                  bytes[3] = ( byte ) 0xFF;
78  
79                  break;
80  
81              default:
82                  bytes = new byte[len * 8 + 4];
83                  int pos = 0;
84  
85                  // The number of longs
86                  bytes[pos++] = ( byte ) ( len >>> 24 );
87                  bytes[pos++] = ( byte ) ( len >>> 16 );
88                  bytes[pos++] = ( byte ) ( len >>> 8 );
89                  bytes[pos++] = ( byte ) ( len );
90  
91                  // Serialize the longs now
92                  for ( long value : element )
93                  {
94                      bytes[pos++] = ( byte ) ( value >>> 56 );
95                      bytes[pos++] = ( byte ) ( value >>> 48 );
96                      bytes[pos++] = ( byte ) ( value >>> 40 );
97                      bytes[pos++] = ( byte ) ( value >>> 32 );
98                      bytes[pos++] = ( byte ) ( value >>> 24 );
99                      bytes[pos++] = ( byte ) ( value >>> 16 );
100                     bytes[pos++] = ( byte ) ( value >>> 8 );
101                     bytes[pos++] = ( byte ) ( value );
102                 }
103         }
104 
105         return bytes;
106     }
107 
108 
109     /**
110      * {@inheritDoc}
111      */
112     public long[] deserialize( BufferHandler bufferHandler ) throws IOException
113     {
114         byte[] in = bufferHandler.read( 4 );
115 
116         int len = IntSerializer.deserialize( in );
117 
118         switch ( len )
119         {
120             case 0:
121                 return new long[]
122                     {};
123 
124             case -1:
125                 return null;
126 
127             default:
128                 long[] longs = new long[len];
129 
130                 int pos = 4;
131 
132                 for ( int i = 0; i < len; i++ )
133                 {
134                     longs[i] = ( ( long ) in[pos++] << 56 ) +
135                         ( ( in[pos++] & 0xFFL ) << 48 ) +
136                         ( ( in[pos++] & 0xFFL ) << 40 ) +
137                         ( ( in[pos++] & 0xFFL ) << 32 ) +
138                         ( ( in[pos++] & 0xFFL ) << 24 ) +
139                         ( ( in[pos++] & 0xFFL ) << 16 ) +
140                         ( ( in[pos++] & 0xFFL ) << 8 ) +
141                         ( in[pos++] & 0xFFL );
142                 }
143 
144                 return longs;
145         }
146     }
147 
148 
149     /**
150      * {@inheritDoc}
151      */
152     public long[] deserialize( ByteBuffer buffer ) throws IOException
153     {
154         int len = buffer.getInt();
155 
156         switch ( len )
157         {
158             case 0:
159                 return new long[]
160                     {};
161 
162             case -1:
163                 return null;
164 
165             default:
166                 long[] longs = new long[len];
167 
168                 for ( int i = 0; i < len; i++ )
169                 {
170                     longs[i] = buffer.getLong();
171                 }
172 
173                 return longs;
174         }
175     }
176 
177 
178     /**
179      * {@inheritDoc}
180      */
181     @Override
182     public int compare( long[] type1, long[] type2 )
183     {
184         if ( type1 == type2 )
185         {
186             return 0;
187         }
188 
189         if ( type1 == null )
190         {
191             if ( type2 == null )
192             {
193                 return 0;
194             }
195             else
196             {
197                 return -1;
198             }
199         }
200         else
201         {
202             if ( type2 == null )
203             {
204                 return 1;
205             }
206             else
207             {
208                 if ( type1.length < type2.length )
209                 {
210                     int pos = 0;
211 
212                     for ( long b1 : type1 )
213                     {
214                         long b2 = type2[pos];
215 
216                         if ( b1 == b2 )
217                         {
218                             pos++;
219                         }
220                         else if ( b1 < b2 )
221                         {
222                             return -1;
223                         }
224                         else
225                         {
226                             return 1;
227                         }
228                     }
229 
230                     return 1;
231                 }
232                 else
233                 {
234                     int pos = 0;
235 
236                     for ( long b2 : type2 )
237                     {
238                         long b1 = type1[pos];
239 
240                         if ( b1 == b2 )
241                         {
242                             pos++;
243                         }
244                         else if ( b1 < b2 )
245                         {
246                             return -1;
247                         }
248                         else
249                         {
250                             return 1;
251                         }
252                     }
253 
254                     return -11;
255                 }
256             }
257         }
258     }
259 
260 
261     @Override
262     public long[] fromBytes( byte[] buffer ) throws IOException
263     {
264         int len = IntSerializer.deserialize( buffer );
265         int pos = 4;
266 
267         switch ( len )
268         {
269             case 0:
270                 return new long[]
271                     {};
272 
273             case -1:
274                 return null;
275 
276             default:
277                 long[] longs = new long[len];
278 
279                 for ( int i = 0; i < len; i++ )
280                 {
281                     longs[i] = LongSerializer.deserialize( buffer, pos );
282                     pos += 8;
283                 }
284 
285                 return longs;
286         }
287     }
288 
289 
290     @Override
291     public long[] fromBytes( byte[] buffer, int pos ) throws IOException
292     {
293         int newPos = pos;
294         int len = IntSerializer.deserialize( buffer, newPos );
295 
296         switch ( len )
297         {
298             case 0:
299                 return new long[]
300                     {};
301 
302             case -1:
303                 return null;
304 
305             default:
306                 long[] longs = new long[len];
307 
308                 for ( int i = 0; i < len; i++ )
309                 {
310                     longs[i] = LongSerializer.deserialize( buffer, newPos );
311                     newPos += 8;
312                 }
313 
314                 return longs;
315         }
316     }
317 }