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