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