1/* Query - query parsing and evaluation
2 *
3 * Copyright 2001-2004, Axel D��rfler, axeld@pinc-software.de.
4 * This file may be used under the terms of the MIT License.
5 *
6 * Adjusted by Ingo Weinhold <bonefish@cs.tu-berlin.de> for usage in RAM FS.
7 */
8#ifndef QUERY_H
9#define QUERY_H
10
11
12#include <OS.h>
13#include <SupportDefs.h>
14
15#include <util/DoublyLinkedList.h>
16
17#include "Index.h"
18#include "Stack.h"
19#include "ramfs.h"
20
21class Entry;
22class Equation;
23class IndexIterator;
24class Node;
25class Query;
26class Term;
27class Volume;
28
29
30#define B_QUERY_NON_INDEXED	0x00000002
31
32
33// Wraps the RAM FS Index to provide the interface required by the Query
34// implementation. At least most of it.
35//
36// IndexWrapper
37class IndexWrapper {
38public:
39	IndexWrapper(Volume *volume);
40
41	status_t SetTo(const char *name);
42	void Unset();
43
44	uint32 Type() const;
45	off_t GetSize() const;
46	int32 KeySize() const;
47
48private:
49	friend class IndexIterator;
50
51	Volume	*fVolume;
52	Index	*fIndex;
53};
54
55// IndexIterator
56class IndexIterator {
57public:
58	IndexIterator(IndexWrapper *indexWrapper);
59
60	status_t Find(const uint8 *const key, size_t keyLength);
61	status_t GetNextEntry(uint8 *buffer, uint16 *keyLength, size_t bufferSize,
62						  Entry **entry);
63
64private:
65	IndexWrapper		*fIndexWrapper;
66	IndexEntryIterator	fIterator;
67	bool				fInitialized;
68};
69
70
71class Expression {
72	public:
73		Expression(char *expr);
74		~Expression();
75
76		status_t InitCheck();
77		const char *Position() const { return fPosition; }
78		Term *Root() const { return fTerm; }
79
80	protected:
81		Term *ParseOr(char **expr);
82		Term *ParseAnd(char **expr);
83		Term *ParseEquation(char **expr);
84
85		bool IsOperator(char **expr,char op);
86
87	private:
88		Expression(const Expression &);
89		Expression &operator=(const Expression &);
90			// no implementation
91
92		char *fPosition;
93		Term *fTerm;
94};
95
96class Query : public DoublyLinkedListLinkImpl<Query> {
97	public:
98		Query(Volume *volume, Expression *expression, uint32 flags);
99		~Query();
100
101		status_t Rewind();
102		status_t GetNextEntry(struct dirent *, size_t size);
103
104		void SetLiveMode(port_id port, int32 token);
105		void LiveUpdate(Entry *entry, Node* node, const char *attribute,
106			int32 type, const uint8 *oldKey, size_t oldLength,
107			const uint8 *newKey, size_t newLength);
108
109		Expression *GetExpression() const { return fExpression; }
110
111	private:
112//		void SendNotification(Entry* entry)
113
114	private:
115		Volume			*fVolume;
116		Expression		*fExpression;
117		Equation		*fCurrent;
118		IndexIterator	*fIterator;
119		IndexWrapper	fIndex;
120		Stack<Equation *> fStack;
121
122		uint32			fFlags;
123		port_id			fPort;
124		int32			fToken;
125		bool			fNeedsEntry;
126};
127
128#endif	/* QUERY_H */
129