1 /*
2  * Copyright 2006-2008 The FLWOR Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #ifndef ZORBA_API_AUDIT_IMPL_H
19 #define ZORBA_API_AUDIT_IMPL_H
20 
21 #include <sys/types.h>
22 #include <zorba/audit.h>
23 #include <zorba/audit_scoped.h>
24 #include <map>
25 #include <set>
26 #include "zorbatypes/zstring.h"
27 
28 namespace zorba {
29 namespace audit {
30 
31   class Provider;
32   class EventImpl;
33 
34   class PropertyGroupImpl : public PropertyGroup {
35   public:
36 
37     PropertyGroupImpl(const size_t pathLength, const char** path);
38 
39     virtual size_t      pathLength()       const;
40     virtual const char* getSegment(size_t) const;
41 
42   private:
43     const size_t m_pathLength;
44     const char** m_path;
45   };
46 
47   extern const PropertyGroupImpl XQUERY_COMPILATION;
48 
49   class PropertyImpl : public Property {
50   public:
51 
52     /**
53      * constructor for "static" audit properties (properties defined in zorba)
54      */
55     PropertyImpl(const PropertyGroup& g, const char n[], long i, Type t);
56     /**
57      * constructor for "dynamic" audit properties (properties defined by users
58      * of the audit facility)
59      */
60     PropertyImpl(const String& n);
61     virtual ~PropertyImpl();
62 
63     const PropertyGroup& group() const;
64     const char*          name()  const;
65     long                 id()    const;
66     Type                 type()  const;
67 
68   private:
69     PropertyImpl(const PropertyImpl&); // not implemented
70     PropertyImpl& operator=(const PropertyImpl&);  // not implemented
71 
72     const PropertyGroup& m_group;
73     char*                m_name;
74     const long           m_id;
75     const Type           m_type;
76   };
77 
78   extern const PropertyImpl XQUERY_COMPILATION_FILENAME;
79   extern const PropertyImpl XQUERY_COMPILATION_PARSE_DURATION;
80   extern const PropertyImpl XQUERY_COMPILATION_TRANSLATION_DURATION;
81   extern const PropertyImpl XQUERY_COMPILATION_OPTIMIZATION_DURATION;
82   extern const PropertyImpl XQUERY_COMPILATION_CODEGENERATION_DURATION;
83 
84   class ObservationImpl : public Observation {
85   public:
86     union Value {
87       long long l;
88       size_t    s;
89     };
90 
91     ObservationImpl(const Property& property, long long value);
92     ObservationImpl(const Property& property, size_t value, const std::vector<String>*);
93 
94     virtual const Property&    property()    const;
95     virtual const String& stringValue() const;
96     virtual long long          longValue()   const;
97 
98   private:
99     const Property*                 m_property;
100     union Value                     m_value;
101     const std::vector<String>* m_strings;
102   };
103 
104   class ConfigurationImpl : public Configuration {
105   public:
106 
107     virtual size_t size() const;
108     virtual void   enableAudit(size_t i);
109     virtual void   enableAudit(const String& aPropertyName);
110     virtual bool   auditEnabled(size_t i) const;
111     virtual bool   auditEnabled(const String& aPropertyName) const;
112 
113     virtual const Property* getDynamicProperty(const String& prop_name) const;
114 
115     virtual std::ostream& write(std::ostream&) const;
116   private:
117     typedef std::map<const String, const Property*> DynPropsMap;
118 
119     friend class EventImpl;
120     friend class ProviderImpl;
121 
122     ConfigurationImpl(size_t size);
123     ConfigurationImpl(const ConfigurationImpl&); // not implemented
124     ConfigurationImpl& operator=(const ConfigurationImpl&);  // not implemented
125     virtual ~ConfigurationImpl();
126 
127     size_t m_size;
128     bool* m_enabled;
129     DynPropsMap m_dyn_props;
130   };
131 
132   class RecordImpl : public Record {
133   public:
134     virtual const PropertyGroup& group() const;
135 
136     virtual size_t             size()       const;
137     virtual const Observation& at(size_t i) const;
138 
139     virtual void add(const Property& prop, long long val);
140     virtual void add(const Property& prop, const String& val);
141 
142   private:
143     friend class EventImpl;
144 
RecordImpl(EventImpl * event)145     RecordImpl(EventImpl* event) : m_event(event) {}
~RecordImpl()146     virtual ~RecordImpl() {}
147 
148     EventImpl* m_event;
149     std::vector<ObservationImpl> m_parameters;
150   };
151 
152   class EventImpl : public Event {
153   public:
154     EventImpl(const Configuration* config);
155     virtual ~EventImpl();
156 
157     virtual bool audit(const Property& prop) const;
158     virtual bool audit(const String& prop_name) const;
159 
160     virtual const Property* getDynamicProperty(const String& prop_name) const;
161 
162     virtual Record* createRecord();
163     virtual void    submitRecord(Record*);
164 
165     virtual size_t        size()       const;
166     virtual const Record* at(size_t i) const;
167 
168     virtual std::ostream& write(std::ostream&) const;
169 
170   private:
171     friend class RecordImpl;
172 
173     EventImpl(const Event&); // not implemented
174     Event& operator=(const Event&); // not implemented
175 
176     Provider*                m_provider;
177     const Configuration*     m_config;
178     std::set<RecordImpl*>    m_recordsActive;
179     std::vector<RecordImpl*> m_recordsSubmitted;
180     std::vector<String> m_strings;
181   };
182 
183   class NOPEventImpl : public Event {
184   public:
185     NOPEventImpl();
186     virtual ~NOPEventImpl();
187 
188     virtual bool audit(const Property& prop) const;
189     virtual bool audit(const String& prop_name) const;
190 
191     virtual const Property* getDynamicProperty(const String&) const;
192 
193     virtual Record* createRecord();
194     virtual void    submitRecord(Record*);
195 
196     virtual size_t        size()       const;
197     virtual const Record* at(size_t i) const;
198 
199     virtual std::ostream& write(std::ostream&) const;
200   };
201 
202   extern NOPEventImpl NOP_EVENT_IMPL;
203 
204   class ProviderImpl : public Provider {
205   public:
206     virtual Configuration* createConfiguration(size_t);
207     virtual void           destroyConfiguration(Configuration*);
208 
209     virtual Event* createEvent(const Configuration*);
210     virtual void   submitEvent(Event*);
211   };
212 
213   extern ProviderImpl PROVIDER_IMPL;
214 }
215 }
216 
217 #endif
218 
219 /*
220  * Local variables:
221  * mode: c++
222  * End:
223  */
224 /* vim:set et sw=2 ts=2: */
225