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.assertNotNull;
25  import static org.junit.Assert.assertTrue;
26  
27  import java.io.File;
28  import java.io.IOException;
29  import java.util.Set;
30  import java.util.UUID;
31  
32  import org.apache.commons.io.FileUtils;
33  import org.apache.directory.mavibot.btree.BTree;
34  import org.apache.directory.mavibot.btree.RecordManager;
35  import org.apache.directory.mavibot.btree.exception.BTreeAlreadyManagedException;
36  import org.apache.directory.mavibot.btree.exception.KeyNotFoundException;
37  import org.apache.directory.mavibot.btree.serializer.LongSerializer;
38  import org.apache.directory.mavibot.btree.serializer.StringSerializer;
39  import org.junit.After;
40  import org.junit.Before;
41  import org.junit.Rule;
42  import org.junit.Test;
43  import org.junit.rules.TemporaryFolder;
44  
45  
46  /**
47   * test the RecordManager whith duplicate values
48   * 
49   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
50   */
51  public class RecordManagerWithDuplicatesTest
52  {
53      private BTree<Long, String> btree = null;
54  
55      private RecordManager recordManager = null;
56  
57      @Rule
58      public TemporaryFolder tempFolder = new TemporaryFolder();
59  
60      private File dataDir = null;
61  
62  
63      @Before
64      public void createBTree()
65      {
66          dataDir = tempFolder.newFolder( UUID.randomUUID().toString() );
67  
68          openRecordManagerAndBtree();
69  
70          try
71          {
72              // Create a new BTree which allows duplicate values
73              btree = recordManager.addBTree( "test", new LongSerializer(), new StringSerializer(), true );
74          }
75          catch ( Exception e )
76          {
77              throw new RuntimeException( e );
78          }
79      }
80  
81  
82      @After
83      public void cleanup() throws IOException
84      {
85          btree.close();
86  
87          if ( dataDir.exists() )
88          {
89              FileUtils.deleteDirectory( dataDir );
90          }
91      }
92  
93  
94      private void openRecordManagerAndBtree()
95      {
96          try
97          {
98              if ( recordManager != null )
99              {
100                 recordManager.close();
101             }
102 
103             // Now, try to reload the file back
104             recordManager = new RecordManager( dataDir.getAbsolutePath() );
105 
106             // load the last created btree
107             btree = recordManager.getManagedTree( "test" );
108         }
109         catch ( Exception e )
110         {
111             throw new RuntimeException( e );
112         }
113     }
114 
115 
116     /**
117      * Test the creation of a RecordManager, and that we can read it back.  
118      */
119     @Test
120     public void testRecordManager() throws IOException, BTreeAlreadyManagedException
121     {
122         assertEquals( 1, recordManager.getNbManagedTrees() );
123 
124         Set<String> managedBTrees = recordManager.getManagedTrees();
125 
126         assertEquals( 1, managedBTrees.size() );
127         assertTrue( managedBTrees.contains( "test" ) );
128 
129         BTree<Long, String> btree1 = recordManager.getManagedTree( "test" );
130 
131         assertNotNull( btree1 );
132         assertEquals( btree.getComparator().getClass().getName(), btree1.getComparator().getClass().getName() );
133         assertEquals( btree.getKeySerializer().getClass().getName(), btree1.getKeySerializer().getClass().getName() );
134         assertEquals( btree.getName(), btree1.getName() );
135         assertEquals( btree.getNbElems(), btree1.getNbElems() );
136         assertEquals( btree.getPageSize(), btree1.getPageSize() );
137         assertEquals( btree.getRevision(), btree1.getRevision() );
138         assertEquals( btree.getValueSerializer().getClass().getName(), btree1.getValueSerializer().getClass().getName() );
139         assertTrue( btree.isAllowDuplicates() );
140     }
141 
142 
143     /**
144      * Test the creation of a RecordManager with a BTree containing data.
145      */
146     @Test
147     public void testRecordManagerWithBTreeSameValue() throws IOException, BTreeAlreadyManagedException,
148         KeyNotFoundException
149     {
150         // Now, add some elements in the BTree
151         btree.insert( 3L, "V3" );
152         btree.insert( 3L, "V5" );
153 
154         assertTrue( btree.contains( 3L, "V3" ) );
155         assertTrue( btree.contains( 3L, "V5" ) );
156 
157         // Now, try to reload the file back
158         openRecordManagerAndBtree();
159         assertNotNull( btree );
160 
161         assertTrue( btree.contains( 3L, "V3" ) );
162         assertTrue( btree.contains( 3L, "V5" ) );
163     }
164 
165 
166     /**
167      * Test the creation of a RecordManager with a BTree containing data.
168      */
169     @Test
170     public void testRecordManagerWithBTreeVariousValues() throws IOException, BTreeAlreadyManagedException,
171         KeyNotFoundException
172     {
173         // Now, add some elements in the BTree
174         for ( long i = 1; i < 128; i++ )
175         {
176             String v1 = "V" + i;
177             btree.insert( i, v1 );
178 
179             String v2 = "V" + i + 1;
180             btree.insert( i, v2 );
181         }
182 
183         // Check that the elements are present
184         for ( long i = 1; i < 128; i++ )
185         {
186             String v1 = "V" + i;
187             String v2 = "V" + i + 1;
188             assertTrue( btree.contains( i, v1 ) );
189             assertTrue( btree.contains( i, v2 ) );
190 
191         }
192 
193         // Now, try to reload the file back
194         openRecordManagerAndBtree();
195         assertNotNull( btree );
196 
197         for ( long i = 1; i < 128; i++ )
198         {
199             String v1 = "V" + i;
200             String v2 = "V" + i + 1;
201             assertTrue( btree.contains( i, v1 ) );
202             assertTrue( btree.contains( i, v2 ) );
203 
204         }
205     }
206 }