2 * Copyright 2011, Jérôme Duval, korli@users.berlios.de.
3 * Copyright 2001-2010, Axel Dörfler, axeld@pinc-software.de.
4 * This file may be used under the terms of the MIT License.
14 #define BTREE_NULL -1LL
15 #define BTREE_FREE -2LL
17 #define BTRFS_MAX_TREE_DEPTH 8
20 enum btree_traversing
{
30 // #pragma mark - in-memory structures
33 template<class T
> class Stack
;
37 // needed for searching (utilizing a stack)
46 BTree(Volume
* volume
);
48 btrfs_stream
* stream
);
52 status_t
FindExact(btrfs_key
& key
, void** value
,
53 uint32
* size
= NULL
, bool read
= true);
54 status_t
FindNext(btrfs_key
& key
, void** value
,
55 uint32
* size
= NULL
, bool read
= true);
56 status_t
FindPrevious(btrfs_key
& key
, void** value
,
57 uint32
* size
= NULL
, bool read
= true);
59 Volume
* SystemVolume() const { return fVolume
; }
61 status_t
SetRoot(off_t logical
, fsblock_t
* block
);
62 fsblock_t
RootBlock() const { return fRootBlock
; }
63 off_t
LogicalRoot() const { return fLogicalRoot
; }
66 BTree(const BTree
& other
);
67 BTree
& operator=(const BTree
& other
);
70 status_t
_Find(btrfs_key
& key
, void** value
, uint32
* size
,
71 bool read
, btree_traversing type
);
72 void _AddIterator(TreeIterator
* iterator
);
73 void _RemoveIterator(TreeIterator
* iterator
);
75 friend class TreeIterator
;
81 SinglyLinkedList
<TreeIterator
> fIterators
;
87 Node(Volume
* volume
, off_t block
);
90 // just return from Header
91 uint64
LogicalAddress() const
92 { return fNode
->header
.LogicalAddress(); }
94 { return fNode
->header
.Flags(); }
95 uint64
Generation() const
96 { return fNode
->header
.Generation(); }
98 { return fNode
->header
.Owner(); }
99 uint32
ItemCount() const
100 { return fNode
->header
.ItemCount(); }
102 { return fNode
->header
.Level(); }
104 btrfs_index
* Index(uint32 i
) const
105 { return &fNode
->index
[i
]; }
107 btrfs_entry
* Item(uint32 i
) const
108 { return &fNode
->entries
[i
]; }
109 uint8
* ItemData(uint32 i
) const
110 { return (uint8
*)Item(0) + Item(i
)->Offset(); }
115 void SetTo(off_t block
);
116 void SetToWritable(off_t block
,
117 int32 transactionId
, bool empty
);
119 off_t
BlockNum() const { return fBlockNumber
;}
120 bool IsWritable() const { return fWritable
; }
122 status_t
SearchSlot(const btrfs_key
& key
, int* slot
,
123 btree_traversing type
) const;
126 Node
& operator=(const Node
&);
142 Path
operator=(const Path
&);
143 Node
* nodes
[BTRFS_MAX_TREE_DEPTH
];
149 class TreeIterator
: public SinglyLinkedListLinkImpl
<TreeIterator
> {
151 TreeIterator(BTree
* tree
, btrfs_key
& key
);
154 status_t
Traverse(btree_traversing direction
,
155 btrfs_key
& key
, void** value
,
156 uint32
* size
= NULL
);
157 status_t
Find(btrfs_key
& key
);
160 status_t
GetNextEntry(btrfs_key
& key
, void** value
,
161 uint32
* size
= NULL
);
162 status_t
GetPreviousEntry(btrfs_key
& key
, void** value
,
163 uint32
* size
= NULL
);
165 BTree
* Tree() const { return fTree
; }
175 btrfs_key fCurrentKey
;
179 // #pragma mark - TreeIterator inline functions
183 TreeIterator::Rewind()
185 fCurrentKey
.SetOffset(BTREE_BEGIN
);
191 TreeIterator::GetNextEntry(btrfs_key
& key
, void** value
, uint32
* size
)
193 return Traverse(BTREE_FORWARD
, key
, value
, size
);
198 TreeIterator::GetPreviousEntry(btrfs_key
& key
, void** value
,
201 return Traverse(BTREE_BACKWARD
, key
, value
, size
);