]> git.ipfire.org Git - thirdparty/squid.git/blob - src/store/LocalSearch.cc
C++11: Remove GnuRegex and all -lregex related code
[thirdparty/squid.git] / src / store / LocalSearch.cc
1 /*
2 * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 /* DEBUG: section 47 Store Search */
10
11 #include "squid.h"
12 #include "Debug.h"
13 #include "globals.h"
14 #include "store/LocalSearch.h"
15 #include "StoreSearch.h"
16
17 namespace Store {
18
19 /// iterates local store_table
20 class LocalSearch : public StoreSearch
21 {
22 CBDATA_CLASS(LocalSearch);
23
24 public:
25 LocalSearch();
26
27 /* StoreSearch API */
28 virtual void next(void (callback)(void *cbdata), void *cbdata) override;
29 virtual bool next() override;
30 virtual bool error() const override;
31 virtual bool isDone() const override;
32 virtual StoreEntry *currentItem() override;
33
34 private:
35 void copyBucket();
36 void (*callback)(void *cbdata);
37 void *cbdata;
38 bool _done;
39 int bucket;
40 std::vector<StoreEntry *> entries;
41 };
42
43 } // namespace Store
44
45 CBDATA_NAMESPACED_CLASS_INIT(Store, LocalSearch);
46
47 StoreSearch *
48 Store::NewLocalSearch()
49 {
50 return new LocalSearch;
51 }
52
53 Store::LocalSearch::LocalSearch() :
54 callback(NULL),
55 cbdata(NULL),
56 _done(false),
57 bucket(0)
58 {}
59
60 void
61 Store::LocalSearch::next(void (aCallback)(void *), void *aCallbackData)
62 {
63 next();
64 aCallback (aCallbackData);
65 }
66
67 bool
68 Store::LocalSearch::next()
69 {
70 if (!entries.empty())
71 entries.pop_back();
72
73 while (!isDone() && !entries.size())
74 copyBucket();
75
76 return currentItem() != NULL;
77 }
78
79 bool
80 Store::LocalSearch::error() const
81 {
82 return false;
83 }
84
85 bool
86 Store::LocalSearch::isDone() const
87 {
88 return bucket >= store_hash_buckets || _done;
89 }
90
91 StoreEntry *
92 Store::LocalSearch::currentItem()
93 {
94 if (!entries.size())
95 return NULL;
96
97 return entries.back();
98 }
99
100 void
101 Store::LocalSearch::copyBucket()
102 {
103 /* probably need to lock the store entries...
104 * we copy them all to prevent races on the links. */
105 debugs(47, 3, "Store::LocalSearch::copyBucket #" << bucket);
106 assert (!entries.size());
107 hash_link *link_ptr = NULL;
108 hash_link *link_next = NULL;
109 link_next = hash_get_bucket(store_table, bucket);
110
111 while (NULL != (link_ptr = link_next)) {
112 link_next = link_ptr->next;
113 StoreEntry *e = (StoreEntry *) link_ptr;
114
115 entries.push_back(e);
116 }
117
118 ++bucket;
119 debugs(47,3, "got entries: " << entries.size());
120 }
121