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  import static org.junit.Assert.assertTrue;
23  
24  import java.io.File;
25  import java.io.IOException;
26  import java.util.UUID;
27  
28  import org.apache.commons.io.FileUtils;
29  import org.apache.directory.mavibot.btree.serializer.LongSerializer;
30  import org.apache.directory.mavibot.btree.serializer.StringSerializer;
31  import org.junit.After;
32  import org.junit.Before;
33  import org.junit.Ignore;
34  import org.junit.Rule;
35  import org.junit.Test;
36  import org.junit.rules.TemporaryFolder;
37  
38  /**
39   * Test the PersistedBTree with transaction
40   *
41   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
42   */
43  public class PersistedBTreeTransactionTest
44  {
45      @Rule
46      public TemporaryFolder tempFolder = new TemporaryFolder();
47      private File dataDirWithTxn = null;
48      private File dataDirNoTxn = null;
49      private BTree<Long, String> btreeWithTransactions = null;
50      private BTree<Long, String> btreeNoTransactions = null;
51      private RecordManager recordManagerTxn = null;
52      private RecordManager recordManagerNoTxn = null;
53  
54  
55      @Before
56      public void createBTree() throws IOException
57      {
58          dataDirWithTxn = tempFolder.newFolder( UUID.randomUUID().toString() );
59          dataDirNoTxn = tempFolder.newFolder( UUID.randomUUID().toString() );
60  
61          openRecordManagerAndBtrees();
62  
63          try
64          {
65              // Create a new BTree with transaction and another one without
66              btreeWithTransactions = recordManagerTxn.addBTree( "testWithTxn", LongSerializer.INSTANCE, StringSerializer.INSTANCE, false );
67              btreeNoTransactions = recordManagerNoTxn.addBTree( "testNoTxn", LongSerializer.INSTANCE, StringSerializer.INSTANCE, false );
68          }
69          catch ( Exception e )
70          {
71              throw new RuntimeException( e );
72          }
73      }
74  
75  
76      @After
77      public void cleanup() throws IOException
78      {
79          btreeNoTransactions.close();
80          btreeWithTransactions.close();
81  
82          if ( dataDirNoTxn.exists() )
83          {
84              FileUtils.deleteDirectory( dataDirNoTxn );
85          }
86  
87          if ( dataDirWithTxn.exists() )
88          {
89              FileUtils.deleteDirectory( dataDirWithTxn );
90          }
91          
92          recordManagerNoTxn.close();
93          recordManagerTxn.close();
94          
95          assertTrue( recordManagerNoTxn.isContextOk() );
96          assertTrue( recordManagerTxn.isContextOk() );
97      }
98  
99  
100     private void openRecordManagerAndBtrees()
101     {
102         try
103         {
104             if ( recordManagerTxn != null )
105             {
106                 recordManagerTxn.close();
107             }
108 
109             if ( recordManagerNoTxn != null )
110             {
111                 recordManagerNoTxn.close();
112             }
113 
114             // Now, try to reload the file back
115             recordManagerTxn = new RecordManager( dataDirWithTxn.getAbsolutePath() );
116             recordManagerNoTxn = new RecordManager( dataDirNoTxn.getAbsolutePath() );
117 
118             // load the last created btree
119             if ( btreeWithTransactions != null )
120             {
121                 btreeWithTransactions = recordManagerTxn.getManagedTree( btreeWithTransactions.getName() );
122             }
123 
124             if ( btreeNoTransactions != null )
125             {
126                 btreeNoTransactions = recordManagerNoTxn.getManagedTree( btreeNoTransactions.getName() );
127             }
128         }
129         catch ( Exception e )
130         {
131             throw new RuntimeException( e );
132         }
133     }
134 
135 
136     @Test
137     public void testWithoutTransaction() throws IOException
138     {
139         long t0 = System.currentTimeMillis();
140 
141         for ( long i = 0L; i < 1000L; i++ )
142         {
143             btreeNoTransactions.insert( i, Long.toString( i ) );
144         }
145 
146         long t1 = System.currentTimeMillis();
147 
148         System.out.println( "Delta without transaction for 100K elements = " + ( t1 - t0 ) );
149     }
150 
151 
152     @Test
153     @Ignore("Fails atm")
154     public void testWithTransaction() throws IOException
155     {
156         long t0 = System.currentTimeMillis();
157 
158         for ( long i = 0L; i < 1000L; i++ )
159         {
160             System.out.println( i );
161             //btreeWithTransactions.beginTransaction();
162             btreeWithTransactions.insert( i, Long.toString( i ) );
163             //btreeWithTransactions.commit();
164         }
165 
166         long t1 = System.currentTimeMillis();
167 
168         System.out.println( "Delta with transaction for 100K elements = " + ( t1 - t0 ) );
169     }
170 }