1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2013 Inktank <info@inktank.com>
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
16 #include "common/Formatter.h"
20 HitSet::HitSet(const HitSet::Params& params)
23 switch (params.get_type()) {
26 BloomHitSet::Params *p =
27 static_cast<BloomHitSet::Params*>(params.impl.get());
28 impl.reset(new BloomHitSet(p));
32 case TYPE_EXPLICIT_HASH:
33 impl.reset(new ExplicitHashHitSet(static_cast<ExplicitHashHitSet::Params*>(params.impl.get())));
36 case TYPE_EXPLICIT_OBJECT:
37 impl.reset(new ExplicitObjectHitSet(static_cast<ExplicitObjectHitSet::Params*>(params.impl.get())));
41 assert (0 == "unknown HitSet type");
45 void HitSet::encode(bufferlist &bl) const
47 ENCODE_START(1, 1, bl);
50 ::encode((__u8)impl->get_type(), bl);
53 ::encode((__u8)TYPE_NONE, bl);
58 void HitSet::decode(bufferlist::iterator &bl)
64 switch ((impl_type_t)type) {
65 case TYPE_EXPLICIT_HASH:
66 impl.reset(new ExplicitHashHitSet);
68 case TYPE_EXPLICIT_OBJECT:
69 impl.reset(new ExplicitObjectHitSet);
72 impl.reset(new BloomHitSet);
78 throw buffer::malformed_input("unrecognized HitMap type");
85 void HitSet::dump(Formatter *f) const
87 f->dump_string("type", get_type_name());
88 f->dump_string("sealed", sealed ? "yes" : "no");
93 void HitSet::generate_test_instances(list<HitSet*>& o)
95 o.push_back(new HitSet);
96 o.push_back(new HitSet(new BloomHitSet(10, .1, 1)));
97 o.back()->insert(hobject_t());
98 o.back()->insert(hobject_t("asdf", "", CEPH_NOSNAP, 123, 1, ""));
99 o.back()->insert(hobject_t("qwer", "", CEPH_NOSNAP, 456, 1, ""));
100 o.push_back(new HitSet(new ExplicitHashHitSet));
101 o.back()->insert(hobject_t());
102 o.back()->insert(hobject_t("asdf", "", CEPH_NOSNAP, 123, 1, ""));
103 o.back()->insert(hobject_t("qwer", "", CEPH_NOSNAP, 456, 1, ""));
104 o.push_back(new HitSet(new ExplicitObjectHitSet));
105 o.back()->insert(hobject_t());
106 o.back()->insert(hobject_t("asdf", "", CEPH_NOSNAP, 123, 1, ""));
107 o.back()->insert(hobject_t("qwer", "", CEPH_NOSNAP, 456, 1, ""));
110 HitSet::Params::Params(const Params& o)
112 if (o.get_type() != TYPE_NONE) {
113 create_impl(o.get_type());
114 // it's annoying to write virtual operator= methods; use encode/decode
118 bufferlist::iterator p = bl.begin();
120 } // else we don't need to do anything
123 const HitSet::Params& HitSet::Params::operator=(const Params& o)
125 create_impl(o.get_type());
127 // it's annoying to write virtual operator= methods; use encode/decode
131 bufferlist::iterator p = bl.begin();
137 void HitSet::Params::encode(bufferlist &bl) const
139 ENCODE_START(1, 1, bl);
141 ::encode((__u8)impl->get_type(), bl);
144 ::encode((__u8)TYPE_NONE, bl);
149 bool HitSet::Params::create_impl(impl_type_t type)
151 switch ((impl_type_t)type) {
152 case TYPE_EXPLICIT_HASH:
153 impl.reset(new ExplicitHashHitSet::Params);
155 case TYPE_EXPLICIT_OBJECT:
156 impl.reset(new ExplicitObjectHitSet::Params);
159 impl.reset(new BloomHitSet::Params);
170 void HitSet::Params::decode(bufferlist::iterator &bl)
175 if (!create_impl((impl_type_t)type))
176 throw buffer::malformed_input("unrecognized HitMap type");
182 void HitSet::Params::dump(Formatter *f) const
184 f->dump_string("type", HitSet::get_type_name(get_type()));
189 void HitSet::Params::generate_test_instances(list<HitSet::Params*>& o)
191 #define loop_hitset_params(kind) \
193 list<kind::Params*> params; \
194 kind::Params::generate_test_instances(params); \
195 for (list<kind::Params*>::iterator i = params.begin(); \
196 i != params.end(); ++i) \
197 o.push_back(new Params(*i)); \
199 o.push_back(new Params);
200 o.push_back(new Params(new BloomHitSet::Params));
201 loop_hitset_params(BloomHitSet);
202 o.push_back(new Params(new ExplicitHashHitSet::Params));
203 loop_hitset_params(ExplicitHashHitSet);
204 o.push_back(new Params(new ExplicitObjectHitSet::Params));
205 loop_hitset_params(ExplicitObjectHitSet);
208 ostream& operator<<(ostream& out, const HitSet::Params& p) {
209 out << HitSet::get_type_name(p.get_type());
212 p.impl->dump_stream(out);
219 void ExplicitHashHitSet::dump(Formatter *f) const {
220 f->dump_unsigned("insert_count", count);
221 f->open_array_section("hash_set");
222 for (ceph::unordered_set<uint32_t>::const_iterator p = hits.begin();
225 f->dump_unsigned("hash", *p);
229 void ExplicitObjectHitSet::dump(Formatter *f) const {
230 f->dump_unsigned("insert_count", count);
231 f->open_array_section("set");
232 for (ceph::unordered_set<hobject_t>::const_iterator p = hits.begin();
235 f->open_object_section("object");
242 void BloomHitSet::Params::dump(Formatter *f) const {
243 f->dump_float("false_positive_probability", get_fpp());
244 f->dump_int("target_size", target_size);
245 f->dump_int("seed", seed);
248 void BloomHitSet::dump(Formatter *f) const {
249 f->open_object_section("bloom_filter");