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