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 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
40
41
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
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
115 recordManagerTxn = new RecordManager( dataDirWithTxn.getAbsolutePath() );
116 recordManagerNoTxn = new RecordManager( dataDirNoTxn.getAbsolutePath() );
117
118
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
162 btreeWithTransactions.insert( i, Long.toString( i ) );
163
164 }
165
166 long t1 = System.currentTimeMillis();
167
168 System.out.println( "Delta with transaction for 100K elements = " + ( t1 - t0 ) );
169 }
170 }