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
18Index::Index()
19	:
20	fVolume(NULL),
21	fName(),
22	fType(0),
23	fKeyLength(0),
24	fFixedKeyLength(true)
25{
26}
27
28
29Index::~Index()
30{
31}
32
33
34status_t
35Index::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
50bool
51Index::GetIterator(IndexIterator& iterator)
52{
53	AbstractIndexIterator* actualIterator = InternalGetIterator();
54	iterator.SetIterator(actualIterator);
55
56	return actualIterator != NULL;
57}
58
59
60bool
61Index::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
71void
72Index::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->Parent()->ID());
81			}
82		}
83	)
84}
85
86
87// #pragma mark - IndexIterator
88
89
90IndexIterator::IndexIterator()
91	:
92	fIterator(NULL)
93{
94}
95
96
97IndexIterator::~IndexIterator()
98{
99	SetIterator(NULL);
100}
101
102
103bool
104IndexIterator::HasNext() const
105{
106	return fIterator != NULL && fIterator->HasNext();
107}
108
109
110Node*
111IndexIterator::Next()
112{
113	return fIterator != NULL ? fIterator->Next(NULL, NULL) : NULL;
114}
115
116
117Node*
118IndexIterator::Next(void* buffer, size_t* _keyLength)
119{
120	return fIterator != NULL ? fIterator->Next(buffer, _keyLength) : NULL;
121}
122
123
124status_t
125IndexIterator::Suspend()
126{
127	return fIterator != NULL ? fIterator->Suspend() : B_BAD_VALUE;
128}
129
130
131status_t
132IndexIterator::Resume()
133{
134	return fIterator != NULL ? fIterator->Resume() : B_BAD_VALUE;
135}
136
137
138void
139IndexIterator::SetIterator(AbstractIndexIterator* iterator)
140{
141	delete fIterator;
142	fIterator = iterator;
143}
144
145
146// #pragma mark - AbstractIndexIterator
147
148
149AbstractIndexIterator::AbstractIndexIterator()
150{
151}
152
153
154AbstractIndexIterator::~AbstractIndexIterator()
155{
156}
157
158
159status_t
160AbstractIndexIterator::Suspend()
161{
162	return B_OK;
163}
164
165
166status_t
167AbstractIndexIterator::Resume()
168{
169	return B_OK;
170}
171
172