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.directory.mavibot.btree.BTree;
33  import org.apache.directory.mavibot.btree.RecordManager;
34  import org.apache.directory.mavibot.btree.exception.BTreeAlreadyManagedException;
35  import org.apache.directory.mavibot.btree.exception.KeyNotFoundException;
36  import org.apache.directory.mavibot.btree.serializer.LongSerializer;
37  import org.apache.directory.mavibot.btree.serializer.StringSerializer;
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   * @author <a href="mailto:labs@labs.apache.org">Mavibot labs Project</a>
47   */
48  //@Ignore("ignoring till RM functionality is standardized")
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()
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", new LongSerializer(), new StringSerializer(), true );
72          }
73          catch ( Exception e )
74          {
75              throw new RuntimeException( e );
76          }
77      }
78  
79  
80      private void openRecordManagerAndBtree()
81      {
82          try
83          {
84              if ( recordManager != null )
85              {
86                  recordManager.close();
87              }
88  
89              // Now, try to reload the file back
90              recordManager = new RecordManager( dataDir.getAbsolutePath() );
91  
92              // load the last created btree
93              btree = recordManager.getManagedTree( "test" );
94          }
95          catch ( Exception e )
96          {
97              throw new RuntimeException( e );
98          }
99      }
100 
101 
102     /**
103      * Test the creation of a RecordManager, and that we can read it back.  
104      */
105     @Test
106     public void testRecordManager() throws IOException, BTreeAlreadyManagedException
107     {
108         assertEquals( 1, recordManager.getNbManagedTrees() );
109 
110         Set<String> managedBTrees = recordManager.getManagedTrees();
111 
112         assertEquals( 1, managedBTrees.size() );
113         assertTrue( managedBTrees.contains( "test" ) );
114 
115         BTree btree1 = recordManager.getManagedTree( "test" );
116 
117         assertNotNull( btree1 );
118         assertEquals( btree.getComparator().getClass().getName(), btree1.getComparator().getClass().getName() );
119         assertEquals( btree.getFile(), btree1.getFile() );
120         assertEquals( btree.getKeySerializer().getClass().getName(), btree1.getKeySerializer().getClass().getName() );
121         assertEquals( btree.getName(), btree1.getName() );
122         assertEquals( btree.getNbElems(), btree1.getNbElems() );
123         assertEquals( btree.getPageSize(), btree1.getPageSize() );
124         assertEquals( btree.getRevision(), btree1.getRevision() );
125         assertEquals( btree.getValueSerializer().getClass().getName(), btree1.getValueSerializer().getClass().getName() );
126         assertTrue( btree.isAllowDuplicates() );
127     }
128 
129 
130     /**
131      * Test the creation of a RecordManager with a BTree containing data.
132      */
133     @Test
134     public void testRecordManagerWithBTreeSameValue() throws IOException, BTreeAlreadyManagedException,
135         KeyNotFoundException
136     {
137         // Now, add some elements in the BTree
138         btree.insert( 3L, "V3" );
139         btree.insert( 3L, "V5" );
140 
141         assertTrue( btree.contains( 3L, "V3" ) );
142         assertTrue( btree.contains( 3L, "V5" ) );
143 
144         // Now, try to reload the file back
145         openRecordManagerAndBtree();
146         assertNotNull( btree );
147 
148         assertTrue( btree.contains( 3L, "V3" ) );
149         assertTrue( btree.contains( 3L, "V5" ) );
150     }
151 
152 
153     /**
154      * Test the creation of a RecordManager with a BTree containing data.
155      */
156     @Test
157     public void testRecordManagerWithBTreeVariousValues() throws IOException, BTreeAlreadyManagedException,
158         KeyNotFoundException
159     {
160         // Now, add some elements in the BTree
161         for ( long i = 1; i < 128; i++ )
162         {
163             String v1 = "V" + i;
164             btree.insert( i, v1 );
165 
166             String v2 = "V" + i + 1;
167             btree.insert( i, v2 );
168         }
169 
170         for ( long i = 1; i < 128; i++ )
171         {
172             String v1 = "V" + i;
173             String v2 = "V" + i + 1;
174             assertTrue( btree.contains( i, v1 ) );
175             assertTrue( btree.contains( i, v2 ) );
176 
177         }
178 
179         // Now, try to reload the file back
180         openRecordManagerAndBtree();
181         assertNotNull( btree );
182 
183         for ( long i = 1; i < 128; i++ )
184         {
185             String v1 = "V" + i;
186             String v2 = "V" + i + 1;
187             assertTrue( btree.contains( i, v1 ) );
188             assertTrue( btree.contains( i, v2 ) );
189 
190         }
191     }
192 }