xref: /haiku/src/add-ons/kernel/file_systems/ramfs/Query.h (revision 99d027cd0238c1d86da86d7c3f4200509ccc61a6)
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 
21 class Entry;
22 class Equation;
23 class IndexIterator;
24 class Node;
25 class Query;
26 class Term;
27 class 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
37 class IndexWrapper {
38 public:
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 
48 private:
49 	friend class IndexIterator;
50 
51 	Volume	*fVolume;
52 	Index	*fIndex;
53 };
54 
55 // IndexIterator
56 class IndexIterator {
57 public:
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 
65 private:
66 	IndexWrapper		*fIndexWrapper;
67 	IndexEntryIterator	fIterator;
68 	bool				fInitialized;
69 };
70 
71 
72 class 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 
97 class 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