xref: /haiku/src/tests/add-ons/kernel/file_systems/bfs/btree/Journal.h (revision 5e7964b0a929555415798dea3373db9ac4611caa)
1 #ifndef JOURNAL_H
2 #define JOURNAL_H
3 /* Journal - emulation for the B+Tree torture test
4 **
5 ** Initial version by Axel Dörfler, axeld@pinc-software.de
6 ** This file may be used under the terms of the MIT License.
7 */
8 
9 
10 #include <stdio.h>
11 
12 #include "Volume.h"
13 #include "Debug.h"
14 #include "Utility.h"
15 
16 #include "cache.h"
17 
18 
19 class TransactionListener
20 	: public DoublyLinkedListLinkImpl<TransactionListener> {
21 public:
22 								TransactionListener()
23 								{
24 								}
25 
26 	virtual						~TransactionListener()
27 								{
28 								}
29 
30 	virtual void				TransactionDone(bool success) = 0;
31 	virtual void				RemovedFromTransaction() = 0;
32 };
33 
34 typedef DoublyLinkedList<TransactionListener> TransactionListeners;
35 
36 
37 class Transaction {
38 	public:
39 		Transaction(Volume *volume, off_t refBlock)
40 			:
41 			fVolume(volume),
42 			fID(volume->GenerateTransactionID())
43 		{
44 		}
45 
46 		~Transaction()
47 		{
48 		}
49 
50 		int32 ID() const
51 		{
52 			return fID;
53 		}
54 
55 		Volume* GetVolume()
56 		{
57 			return fVolume;
58 		}
59 
60 		status_t WriteBlocks(off_t blockNumber, const uint8* buffer,
61 			size_t numBlocks = 1)
62 		{
63 			return cached_write(fVolume->Device(), blockNumber, buffer,
64 				numBlocks);
65 		}
66 
67 
68 		status_t Start(Volume* volume, off_t refBlock)
69 		{
70 			return B_OK;
71 		}
72 
73 
74 		status_t Done()
75 		{
76 			NotifyListeners(true);
77 			return B_OK;
78 		}
79 
80 		void
81 		AddListener(TransactionListener* listener)
82 		{
83 			fListeners.Add(listener);
84 		}
85 
86 		void
87 		RemoveListener(TransactionListener* listener)
88 		{
89 			fListeners.Remove(listener);
90 			listener->RemovedFromTransaction();
91 		}
92 
93 		void
94 		NotifyListeners(bool success)
95 		{
96 			while (TransactionListener* listener = fListeners.RemoveHead()) {
97 				listener->TransactionDone(success);
98 				listener->RemovedFromTransaction();
99 			}
100 		}
101 
102 	protected:
103 		Volume*	fVolume;
104 		int32	fID;
105 		TransactionListeners	fListeners;
106 };
107 
108 
109 #endif	/* JOURNAL_H */
110