1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
36
37
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
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
87 recordManagerTxn = new RecordManager( dataDirWithTxn.getAbsolutePath() );
88 recordManagerNoTxn = new RecordManager( dataDirNoTxn.getAbsolutePath() );
89
90
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 }