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