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.memory;
21  
22  
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertTrue;
25  
26  import java.io.DataOutputStream;
27  import java.io.File;
28  import java.io.FileOutputStream;
29  import java.io.IOException;
30  import java.lang.reflect.Array;
31  import java.util.Comparator;
32  import java.util.Iterator;
33  import java.util.Random;
34  
35  import org.apache.directory.mavibot.btree.Tuple;
36  import org.apache.directory.mavibot.btree.util.IntTupleReaderWriter;
37  import org.junit.Rule;
38  import org.junit.Test;
39  import org.junit.rules.TemporaryFolder;
40  
41  
42  /**
43   * Test cases for BulkDataSorter.
44   *
45   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
46   */
47  public class BulkDataSorterTest
48  {
49      @Rule
50      public TemporaryFolder tempFolder = new TemporaryFolder();
51  
52      private Comparator<Tuple<Integer, Integer>> tupleComp = new Comparator<Tuple<Integer, Integer>>()
53      {
54  
55          @Override
56          public int compare( Tuple<Integer, Integer> o1, Tuple<Integer, Integer> o2 )
57          {
58              return o1.getKey().compareTo( o2.getKey() );
59          }
60      };
61  
62  
63      @Test
64      public void testSortedFileCount() throws IOException
65      {
66          int count = 7;
67          IntTupleReaderWriter itrw = new IntTupleReaderWriter();
68          Random random = new Random();
69  
70          File dataFile = tempFolder.newFile( "tuple.data" );
71          DataOutputStream out = new DataOutputStream( new FileOutputStream( dataFile ) );
72  
73          Tuple<Integer, Integer>[] arr = ( Tuple<Integer, Integer>[] ) Array.newInstance( Tuple.class, count );
74  
75          for ( int i = 0; i < count; i++ )
76          {
77              int x = random.nextInt( 100 );
78  
79              Tuple<Integer, Integer> t = new Tuple<Integer, Integer>( x, x );
80  
81              arr[i] = t;
82  
83              itrw.storeSortedTuple( t, out );
84          }
85  
86          out.close();
87  
88          BulkDataSorter<Integer, Integer> bds = new BulkDataSorter<Integer, Integer>( itrw, tupleComp, 4 );
89          bds.sort( dataFile );
90  
91          assertEquals( 2, bds.getWorkDir().list().length );
92  
93          deleteDir( bds.getWorkDir() );
94      }
95  
96  
97      @Test
98      public void testSortedFileMerge() throws IOException
99      {
100         testSortedFileMerge( 10, 2 );
101         testSortedFileMerge( 100, 7 );
102         testSortedFileMerge( 1000, 25 );
103         testSortedFileMerge( 10000, 100 );
104         testSortedFileMerge( 10000, 101 );
105         testSortedFileMerge( 100000, 501 );
106     }
107 
108 
109     public void testSortedFileMerge( int count, int splitAfter ) throws IOException
110     {
111         IntTupleReaderWriter itrw = new IntTupleReaderWriter();
112         Random random = new Random();
113 
114         File dataFile = tempFolder.newFile( "tuple.data" );
115 
116         DataOutputStream out = new DataOutputStream( new FileOutputStream( dataFile ) );
117 
118         Tuple<Integer, Integer>[] arr = ( Tuple<Integer, Integer>[] ) Array.newInstance( Tuple.class, count );
119 
120         int randUpper = count;
121         if ( count < 100 )
122         {
123             randUpper = 100;
124         }
125 
126         for ( int i = 0; i < count; i++ )
127         {
128             int x = random.nextInt( randUpper );
129 
130             Tuple<Integer, Integer> t = new Tuple<Integer, Integer>( x, x );
131 
132             arr[i] = t;
133 
134             itrw.storeSortedTuple( t, out );
135         }
136 
137         out.close();
138 
139         BulkDataSorter<Integer, Integer> bds = new BulkDataSorter<Integer, Integer>( itrw, tupleComp, splitAfter );
140         bds.sort( dataFile );
141 
142         Iterator<Tuple<Integer, Integer>> itr = bds.getMergeSortedTuples();
143 
144         Integer prev = null;
145         while ( itr.hasNext() )
146         {
147             Tuple<Integer, Integer> t = itr.next();
148 
149             if ( prev == null )
150             {
151                 prev = t.getKey();
152             }
153             else
154             {
155                 assertTrue( prev <= t.getKey() );
156             }
157         }
158 
159         deleteDir( bds.getWorkDir() );
160     }
161 
162 
163     private void deleteDir( File dir )
164     {
165         if ( dir.isFile() )
166         {
167             dir.delete();
168         }
169 
170         File[] files = dir.listFiles();
171 
172         for ( File f : files )
173         {
174             f.delete();
175         }
176 
177         dir.delete();
178     }
179 }