xref: /haiku/src/add-ons/kernel/file_systems/xfs/BPlusTree.cpp (revision 9295c1f645806eca5d7699c985f7b509528c9eaa)
1 /*
2  * Copyright 2020, Shubham Bhagat, shubhambhagat111@yahoo.com
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "BPlusTree.h"
8 
9 
10 void
11 bplustree_short_block::SwapEndian()
12 {
13 	bb_magic = B_BENDIAN_TO_HOST_INT32(bb_magic);
14 	bb_level = B_BENDIAN_TO_HOST_INT16(bb_level);
15 	bb_numrecs = B_BENDIAN_TO_HOST_INT16(bb_numrecs);
16 	bb_leftsib = B_BENDIAN_TO_HOST_INT32(bb_leftsib);
17 	bb_rightsib = B_BENDIAN_TO_HOST_INT32(bb_rightsib);
18 }
19 
20 
21 void
22 bplustree_long_block::SwapEndian()
23 {
24 	bb_magic = B_BENDIAN_TO_HOST_INT32(bb_magic);
25 	bb_level = B_BENDIAN_TO_HOST_INT16(bb_level);
26 	bb_numrecs = B_BENDIAN_TO_HOST_INT16(bb_numrecs);
27 	bb_leftsib = B_BENDIAN_TO_HOST_INT64(bb_leftsib);
28 	bb_rightsib = B_BENDIAN_TO_HOST_INT64(bb_rightsib);
29 }
30 
31 
32 void
33 xfs_alloc_rec::SwapEndian()
34 {
35 	ar_startblock = B_BENDIAN_TO_HOST_INT32(ar_startblock);
36 	ar_blockcount =  B_BENDIAN_TO_HOST_INT32(ar_blockcount);
37 }
38 
39 
40 uint32
41 BPlusTree::BlockSize()
42 {
43 	return fVolume.SuperBlock().BlockSize();
44 }
45 
46 
47 int
48 BPlusTree::RecordSize()
49 {
50 	if (fRecType == ALLOC_FLAG)
51 		return XFS_ALLOC_REC_SIZE;
52 
53 	return B_BAD_VALUE;
54 }
55 
56 
57 int
58 BPlusTree::MaxRecords(bool leaf)
59 {
60 	int blockLen = BlockSize();
61 
62 	if (fPtrType == SHORT_BLOCK_FLAG)
63 		blockLen - XFS_BTREE_SBLOCK_SIZE;
64 
65 	if (leaf) {
66 		if (fRecType == ALLOC_FLAG)
67 			return blockLen / sizeof(xfs_alloc_rec_t);
68 	} else {
69 		if (fKeyType == ALLOC_FLAG) {
70 			return blockLen / (sizeof(xfs_alloc_key_t)
71 							+ sizeof(xfs_alloc_ptr_t));
72 		}
73 	}
74 
75 	return B_BAD_VALUE;
76 }
77 
78 
79 int
80 BPlusTree::KeyLen()
81 {
82 	if (fKeyType == ALLOC_FLAG)
83 		return XFS_ALLOC_REC_SIZE;
84 	return B_BAD_VALUE;
85 }
86 
87 
88 int
89 BPlusTree::BlockLen()
90 {
91 	if (fPtrType == LONG_BLOCK_FLAG)
92 		return XFS_BTREE_LBLOCK_SIZE;
93 	if (fPtrType == SHORT_BLOCK_FLAG)
94 		return XFS_BTREE_SBLOCK_SIZE;
95 	return B_BAD_VALUE;
96 }
97 
98 
99 int
100 BPlusTree::PtrLen()
101 {
102 	if (fPtrType == LONG_BLOCK_FLAG)
103 		return sizeof(uint64);
104 	else
105 		return sizeof(uint32);
106 }
107 
108 
109 int
110 BPlusTree::RecordOffset(int pos)
111 {
112 	return BlockLen() + (pos - 1) * RecordSize();
113 }
114 
115 
116 int
117 BPlusTree::KeyOffset(int pos)
118 {
119 	return BlockLen() + (pos - 1) * KeyLen();
120 }
121 
122 
123 int
124 BPlusTree::PtrOffset(int pos, int level)
125 {
126 	return BlockLen() + MaxRecords(level > 0) * KeyLen()
127 		+ (pos - 1) * PtrLen();
128 }
129