xref: /haiku/src/add-ons/kernel/file_systems/ramfs/Query.h (revision 1acbe440b8dd798953bec31d18ee589aa3f71b73)
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 "DLList.h"
16 #include "Index.h"
17 #include "Stack.h"
18 #include "ramfs.h"
19 
20 class Entry;
21 class Equation;
22 class IndexIterator;
23 class Node;
24 class Query;
25 class Term;
26 class Volume;
27 
28 
29 #define B_QUERY_NON_INDEXED	0x00000002
30 
31 
32 // Wraps the RAM FS Index to provide the interface required by the Query
33 // implementation. At least most of it.
34 //
35 // IndexWrapper
36 class IndexWrapper {
37 public:
38 	IndexWrapper(Volume *volume);
39 
40 	status_t SetTo(const char *name);
41 	void Unset();
42 
43 	uint32 Type() const;
44 	off_t GetSize() const;
45 	int32 KeySize() const;
46 
47 private:
48 	friend class IndexIterator;
49 
50 	Volume	*fVolume;
51 	Index	*fIndex;
52 };
53 
54 // IndexIterator
55 class IndexIterator {
56 public:
57 	IndexIterator(IndexWrapper *indexWrapper);
58 
59 	status_t Find(const uint8 *const key, size_t keyLength);
60 	status_t GetNextEntry(uint8 *buffer, uint16 *keyLength, size_t bufferSize,
61 						  Entry **entry);
62 
63 private:
64 	IndexWrapper		*fIndexWrapper;
65 	IndexEntryIterator	fIterator;
66 	bool				fInitialized;
67 };
68 
69 
70 class Expression {
71 	public:
72 		Expression(char *expr);
73 		~Expression();
74 
75 		status_t InitCheck();
76 		const char *Position() const { return fPosition; }
77 		Term *Root() const { return fTerm; }
78 
79 	protected:
80 		Term *ParseOr(char **expr);
81 		Term *ParseAnd(char **expr);
82 		Term *ParseEquation(char **expr);
83 
84 		bool IsOperator(char **expr,char op);
85 
86 	private:
87 		Expression(const Expression &);
88 		Expression &operator=(const Expression &);
89 			// no implementation
90 
91 		char *fPosition;
92 		Term *fTerm;
93 };
94 
95 class Query : public DLListLinkImpl<Query> {
96 	public:
97 		Query(Volume *volume, Expression *expression, uint32 flags);
98 		~Query();
99 
100 		status_t Rewind();
101 		status_t GetNextEntry(struct dirent *, size_t size);
102 
103 		void SetLiveMode(port_id port, int32 token);
104 		void LiveUpdate(Entry *entry, Node* node, const char *attribute,
105 			int32 type, const uint8 *oldKey, size_t oldLength,
106 			const uint8 *newKey, size_t newLength);
107 
108 		Expression *GetExpression() const { return fExpression; }
109 
110 	private:
111 //		void SendNotification(Entry* entry)
112 
113 	private:
114 		Volume			*fVolume;
115 		Expression		*fExpression;
116 		Equation		*fCurrent;
117 		IndexIterator	*fIterator;
118 		IndexWrapper	fIndex;
119 		Stack<Equation *> fStack;
120 
121 		uint32			fFlags;
122 		port_id			fPort;
123 		int32			fToken;
124 		bool			fNeedsEntry;
125 };
126 
127 #endif	/* QUERY_H */
128