]> git.ipfire.org Git - thirdparty/squid.git/blame - src/store/LocalSearch.cc
Docs: Copyright updates for 2018 (#114)
[thirdparty/squid.git] / src / store / LocalSearch.cc
CommitLineData
2745fea5 1/*
5b74111a 2 * Copyright (C) 1996-2018 The Squid Software Foundation and contributors
2745fea5
AR
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
17namespace Store {
18
19/// iterates local store_table
20class LocalSearch : public StoreSearch
21{
22 CBDATA_CLASS(LocalSearch);
23
24public:
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
34private:
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
45CBDATA_NAMESPACED_CLASS_INIT(Store, LocalSearch);
46
47StoreSearch *
48Store::NewLocalSearch()
49{
50 return new LocalSearch;
51}
52
53Store::LocalSearch::LocalSearch() :
54 callback(NULL),
55 cbdata(NULL),
56 _done(false),
57 bucket(0)
58{}
59
60void
61Store::LocalSearch::next(void (aCallback)(void *), void *aCallbackData)
62{
63 next();
64 aCallback (aCallbackData);
65}
66
67bool
68Store::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
79bool
80Store::LocalSearch::error() const
81{
82 return false;
83}
84
85bool
86Store::LocalSearch::isDone() const
87{
88 return bucket >= store_hash_buckets || _done;
89}
90
91StoreEntry *
92Store::LocalSearch::currentItem()
93{
94 if (!entries.size())
95 return NULL;
96
97 return entries.back();
98}
99
100void
101Store::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}
7d84d4ca 121