forked from livegrep/livegrep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_planner.h
141 lines (114 loc) · 3.58 KB
/
query_planner.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/********************************************************************
* livegrep -- query_planner.h
* Copyright (c) 2011-2013 Nelson Elhage
*
* This program is free software. You may use, redistribute, and/or
* modify it under the terms listed in the COPYING file.
********************************************************************/
#ifndef CODESEARCH_INDEXER_H
#define CODESEARCH_INDEXER_H
#include <vector>
#include <list>
#include <set>
#include <string>
#include <atomic>
#include <map>
#include <boost/intrusive_ptr.hpp>
#include "re2/re2.h"
#include "re2/walker-inl.h"
#include "src/common.h"
using std::string;
using std::vector;
using std::list;
using std::set;
using boost::intrusive_ptr;
enum {
kAnchorNone = 0x00,
kAnchorLeft = 0x01,
kAnchorRight = 0x02,
kAnchorBoth = 0x03,
kAnchorRepeat = 0x04
};
class QueryPlan {
public:
typedef std::map<std::pair<uchar, uchar>, intrusive_ptr<QueryPlan> >::iterator iterator;
typedef std::map<std::pair<uchar, uchar>, intrusive_ptr<QueryPlan> >::const_iterator const_iterator;
typedef std::pair<std::pair<uchar, uchar>, intrusive_ptr<QueryPlan> > value_type;
iterator begin() {
return edges_.begin();
}
iterator end() {
return edges_.end();
}
QueryPlan(int anchor = kAnchorNone)
: anchor(anchor), refs_(0) { }
QueryPlan(std::pair<uchar, uchar> p,
intrusive_ptr<QueryPlan> next,
int anchor = kAnchorNone)
: anchor(anchor), refs_(0) {
insert(value_type(p, next));
}
void insert(const value_type& v);
void concat(intrusive_ptr<QueryPlan> rhs);
bool empty() {
return edges_.empty();
}
size_t size() {
return edges_.size();
}
class Stats {
public:
double selectivity_;
int depth_;
long nodes_;
long tail_paths_;
Stats();
Stats insert(const value_type& v) const;
Stats concat(const Stats& rhs) const;
};
const Stats& stats() {
return stats_;
}
/*
* Returns an approximation of the fraction of the input corpus
* that this index key will reduce the search space to.
*
* e.g. selectivity() == 1.0 implies that this index key includes
* the entire input.
*
* selectivity() == 0.1 means that using this index key will
* only require searching 1/10th of the corpus.
*
* This value is computed without any reference to the actual
* characteristics of any particular corpus, and so is a rough
* approximation at best.
*/
double selectivity();
/*
* Returns a value approximating the "goodness" of this index key,
* in arbitrary units. Higher is better. The weight incorporates
* both the selectivity, above, and the cost of using this index
* key.
*/
unsigned weight();
int depth();
long nodes();
string ToString();
void check_rep();
int anchor;
void collect_tails(list<QueryPlan::const_iterator>& tails);
protected:
std::map<std::pair<uchar, uchar>, intrusive_ptr<QueryPlan> > edges_;
Stats stats_;
std::atomic_int refs_;
void collect_tails(list<QueryPlan::iterator>& tails);
void collect_tails(list<QueryPlan::iterator>& tails,
set<QueryPlan*> &seen);
private:
QueryPlan(const QueryPlan&);
void operator=(const QueryPlan&);
friend void intrusive_ptr_add_ref(QueryPlan *key);
friend void intrusive_ptr_release(QueryPlan *key);
};
intrusive_ptr<QueryPlan> constructQueryPlan(const re2::RE2 &pat);
#endif /* CODESEARCH_INDEXER_H */