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