xref: /haiku/src/add-ons/kernel/file_systems/packagefs/indices/Index.cpp (revision fc7456e9b1ec38c941134ed6d01c438cf289381e)
1 /*
2  * Copyright 2011, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "Index.h"
8 
9 #include "DebugSupport.h"
10 #include "Directory.h"
11 #include "Node.h"
12 #include "IndexImpl.h"
13 
14 
15 // #pragma mark - Index
16 
17 
18 Index::Index()
19 	:
20 	fVolume(NULL),
21 	fName(),
22 	fType(0),
23 	fKeyLength(0),
24 	fFixedKeyLength(true)
25 {
26 }
27 
28 
29 Index::~Index()
30 {
31 }
32 
33 
34 status_t
35 Index::Init(Volume* volume, const char* name, uint32 type, bool fixedKeyLength,
36 	size_t keyLength)
37 {
38 	if (!fName.SetTo(name))
39 		return B_NO_MEMORY;
40 
41 	fVolume = volume;
42 	fType = type;
43 	fKeyLength = keyLength;
44 	fFixedKeyLength = fixedKeyLength;
45 
46 	return B_OK;
47 }
48 
49 
50 bool
51 Index::GetIterator(IndexIterator& iterator)
52 {
53 	AbstractIndexIterator* actualIterator = InternalGetIterator();
54 	iterator.SetIterator(actualIterator);
55 
56 	return actualIterator != NULL;
57 }
58 
59 
60 bool
61 Index::Find(const void* key, size_t length, IndexIterator& iterator)
62 {
63 	AbstractIndexIterator* actualIterator
64 		= key != NULL ? InternalFind(key, length) : NULL;
65 	iterator.SetIterator(actualIterator);
66 
67 	return actualIterator != NULL;
68 }
69 
70 
71 void
72 Index::Dump()
73 {
74 	D(
75 		PRINT("Index: `%s', type: %" B_PRIx32 "\n", Name().Data(), Type());
76 		IndexIterator it;
77 		if (GetIterator(it)) {
78 			while (Node* node = it.Next()) {
79 				PRINT("  node: `%s', dir: %" B_PRIdINO "\n",
80 					node->Name().Data(), node->GetParentUnchecked()->ID());
81 			}
82 		}
83 	)
84 }
85 
86 
87 // #pragma mark - IndexIterator
88 
89 
90 IndexIterator::IndexIterator()
91 	:
92 	fIterator(NULL)
93 {
94 }
95 
96 
97 IndexIterator::~IndexIterator()
98 {
99 	SetIterator(NULL);
100 }
101 
102 
103 bool
104 IndexIterator::HasNext() const
105 {
106 	return fIterator != NULL && fIterator->HasNext();
107 }
108 
109 
110 Node*
111 IndexIterator::Next()
112 {
113 	return fIterator != NULL ? fIterator->Next(NULL, NULL) : NULL;
114 }
115 
116 
117 Node*
118 IndexIterator::Next(void* buffer, size_t* _keyLength)
119 {
120 	return fIterator != NULL ? fIterator->Next(buffer, _keyLength) : NULL;
121 }
122 
123 
124 status_t
125 IndexIterator::Suspend()
126 {
127 	return fIterator != NULL ? fIterator->Suspend() : B_BAD_VALUE;
128 }
129 
130 
131 status_t
132 IndexIterator::Resume()
133 {
134 	return fIterator != NULL ? fIterator->Resume() : B_BAD_VALUE;
135 }
136 
137 
138 void
139 IndexIterator::SetIterator(AbstractIndexIterator* iterator)
140 {
141 	delete fIterator;
142 	fIterator = iterator;
143 }
144 
145 
146 // #pragma mark - AbstractIndexIterator
147 
148 
149 AbstractIndexIterator::AbstractIndexIterator()
150 {
151 }
152 
153 
154 AbstractIndexIterator::~AbstractIndexIterator()
155 {
156 }
157 
158 
159 status_t
160 AbstractIndexIterator::Suspend()
161 {
162 	return B_OK;
163 }
164 
165 
166 status_t
167 AbstractIndexIterator::Resume()
168 {
169 	return B_OK;
170 }
171 
172