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 Rewind();
62	status_t GetNextEntry(uint8 *buffer, uint16 *keyLength, size_t bufferSize,
63						  Entry **entry);
64
65private:
66	IndexWrapper		*fIndexWrapper;
67	IndexEntryIterator	fIterator;
68	bool				fInitialized;
69};
70
71
72class Expression {
73	public:
74		Expression(char *expr);
75		~Expression();
76
77		status_t InitCheck();
78		const char *Position() const { return fPosition; }
79		Term *Root() const { return fTerm; }
80
81	protected:
82		Term *ParseOr(char **expr);
83		Term *ParseAnd(char **expr);
84		Term *ParseEquation(char **expr);
85
86		bool IsOperator(char **expr,char op);
87
88	private:
89		Expression(const Expression &);
90		Expression &operator=(const Expression &);
91			// no implementation
92
93		char *fPosition;
94		Term *fTerm;
95};
96
97class Query : public DoublyLinkedListLinkImpl<Query> {
98	public:
99		Query(Volume *volume, Expression *expression, uint32 flags);
100		~Query();
101
102		status_t Rewind();
103		status_t GetNextEntry(struct dirent *, size_t size);
104
105		void SetLiveMode(port_id port, int32 token);
106		void LiveUpdate(Entry *entry, Node* node, const char *attribute,
107			int32 type, const uint8 *oldKey, size_t oldLength,
108			const uint8 *newKey, size_t newLength);
109
110		Expression *GetExpression() const { return fExpression; }
111
112	private:
113//		void SendNotification(Entry* entry)
114
115	private:
116		Volume			*fVolume;
117		Expression		*fExpression;
118		Equation		*fCurrent;
119		IndexIterator	*fIterator;
120		IndexWrapper	fIndex;
121		Stack<Equation *> fStack;
122
123		uint32			fFlags;
124		port_id			fPort;
125		int32			fToken;
126		bool			fNeedsEntry;
127};
128
129#endif	/* QUERY_H */
130