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