xref: /haiku/src/add-ons/kernel/file_systems/ramfs/Index.cpp (revision e1c4049fed1047bdb957b0529e1921e97ef94770)
1 /*
2  * Copyright 2007, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * All rights reserved. Distributed under the terms of the MIT license.
4  */
5 
6 #include "DebugSupport.h"
7 #include "Directory.h"
8 #include "Entry.h"
9 #include "Index.h"
10 #include "IndexImpl.h"
11 
12 // Index
13 
14 // constructor
15 Index::Index(Volume *volume, const char *name, uint32 type,
16 			 bool fixedKeyLength, size_t keyLength)
17 	: fVolume(volume),
18 	  fInitStatus(B_OK),
19 	  fName(name),
20 	  fType(type),
21 	  fKeyLength(keyLength),
22 	  fFixedKeyLength(fixedKeyLength)
23 {
24 	if (!fVolume)
25 		fInitStatus = B_BAD_VALUE;
26 	else if (!fName.GetString())
27 		fInitStatus = B_NO_MEMORY;
28 }
29 
30 // destructor
31 Index::~Index()
32 {
33 }
34 
35 // InitCheck
36 status_t
37 Index::InitCheck() const
38 {
39 	return fInitStatus;
40 }
41 
42 // GetIterator
43 bool
44 Index::GetIterator(IndexEntryIterator *iterator)
45 {
46 	bool result = false;
47 	if (iterator) {
48 		AbstractIndexEntryIterator *actualIterator = InternalGetIterator();
49 		if (actualIterator) {
50 			iterator->SetIterator(actualIterator);
51 			result = true;
52 		}
53 	}
54 	return result;
55 }
56 
57 // Find
58 bool
59 Index::Find(const uint8 *key, size_t length, IndexEntryIterator *iterator)
60 {
61 	bool result = false;
62 	if (key && iterator) {
63 		AbstractIndexEntryIterator *actualIterator
64 			= InternalFind(key, length);
65 		if (actualIterator) {
66 			iterator->SetIterator(actualIterator);
67 			result = true;
68 		}
69 	}
70 	return result;
71 }
72 
73 // Dump
74 void
75 Index::Dump()
76 {
77 	D(
78 		PRINT("Index: `%s', type: %lx\n", GetName(), GetType());
79 		for (IndexEntryIterator it(this); it.GetCurrent(); it.GetNext()) {
80 			Entry *entry = it.GetCurrent();
81 			PRINT("  entry: `%s', dir: %lld\n", entry->GetName(),
82 												entry->GetParent()->GetID());
83 		}
84 	)
85 }
86 
87 
88 // IndexEntryIterator
89 
90 // constructor
91 IndexEntryIterator::IndexEntryIterator()
92 	: fIterator(NULL)
93 {
94 }
95 
96 // constructor
97 IndexEntryIterator::IndexEntryIterator(Index *index)
98 	: fIterator(NULL)
99 {
100 	if (index)
101 		index->GetIterator(this);
102 }
103 
104 // destructor
105 IndexEntryIterator::~IndexEntryIterator()
106 {
107 	SetIterator(NULL);
108 }
109 
110 // GetCurrent
111 Entry *
112 IndexEntryIterator::GetCurrent()
113 {
114 	return (fIterator ? fIterator->GetCurrent() : NULL);
115 }
116 
117 // GetCurrent
118 Entry *
119 IndexEntryIterator::GetCurrent(uint8 *buffer, size_t *keyLength)
120 {
121 	return (fIterator ? fIterator->GetCurrent(buffer, keyLength) : NULL);
122 }
123 
124 // GetPrevious
125 Entry *
126 IndexEntryIterator::GetPrevious()
127 {
128 	return (fIterator ? fIterator->GetPrevious() : NULL);
129 }
130 
131 // GetNext
132 Entry *
133 IndexEntryIterator::GetNext()
134 {
135 	return (fIterator ? fIterator->GetNext() : NULL);
136 }
137 
138 // GetNext
139 Entry *
140 IndexEntryIterator::GetNext(uint8 *buffer, size_t *keyLength)
141 {
142 	Entry *entry = NULL;
143 	if (fIterator && fIterator->GetNext())
144 		entry = GetCurrent(buffer, keyLength);
145 	return entry;
146 }
147 
148 // Suspend
149 status_t
150 IndexEntryIterator::Suspend()
151 {
152 	return (fIterator ? fIterator->Suspend() : B_BAD_VALUE);
153 }
154 
155 // Resume
156 status_t
157 IndexEntryIterator::Resume()
158 {
159 	return (fIterator ? fIterator->Resume() : B_BAD_VALUE);
160 }
161 
162 // SetIterator
163 void
164 IndexEntryIterator::SetIterator(AbstractIndexEntryIterator *iterator)
165 {
166 	if (fIterator)
167 		delete fIterator;
168 	fIterator = iterator;
169 }
170 
171 
172 // AbstractIndexEntryIterator
173 
174 // constructor
175 AbstractIndexEntryIterator::AbstractIndexEntryIterator()
176 {
177 }
178 
179 // destructor
180 AbstractIndexEntryIterator::~AbstractIndexEntryIterator()
181 {
182 }
183 
184 // Suspend
185 status_t
186 AbstractIndexEntryIterator::Suspend()
187 {
188 	return B_OK;
189 }
190 
191 // Resume
192 status_t
193 AbstractIndexEntryIterator::Resume()
194 {
195 	return B_OK;
196 }
197 
198