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