1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 #include "include/interval_set.h"
3 #include "include/buffer_fwd.h"
6 #ifndef COMMON_OBJECT_H
7 #define COMMON_OBJECT_H
15 bool test_object_contents();
17 class ObjectContents {
19 std::map<uint64_t, unsigned int> seeds;
20 interval_set<uint64_t> written;
24 ObjectContents *parent;
25 std::map<uint64_t, unsigned int>::iterator iter;
26 unsigned int current_state;
30 unsigned int get_state(uint64_t pos);
32 explicit Iterator(ObjectContents *parent) :
33 parent(parent), iter(parent->seeds.end()),
34 current_state(0), current_val(0), pos(-1) {
38 return parent->written.contains(pos) ?
39 static_cast<char>(current_val % 256) : '\0';
44 void seek_to(uint64_t _pos) {
46 (iter != parent->seeds.end() && _pos >= iter->first)) {
47 iter = parent->seeds.upper_bound(_pos);
49 current_state = iter->second;
50 current_val = rand_r(¤t_state);
54 while (pos < _pos) ++(*this);
57 void seek_to_first() {
60 Iterator &operator++() {
62 if (iter != parent->seeds.end() && pos >= iter->first) {
63 assert(pos == iter->first);
64 current_state = iter->second;
67 current_val = rand_r(¤t_state);
71 return pos < parent->size();
73 friend class ObjectContents;
76 ObjectContents() : _size(0), _exists(false) {
80 explicit ObjectContents(bufferlist::iterator &bp) {
83 ::decode(written, bp);
84 ::decode(_exists, bp);
87 void clone_range(ObjectContents &other,
88 interval_set<uint64_t> &intervals);
89 void write(unsigned int seed,
92 Iterator get_iterator() {
93 return Iterator(this);
96 uint64_t size() const { return _size; }
98 bool exists() { return _exists; }
100 void debug(std::ostream &out) {
101 out << "_size is " << _size << std::endl;
102 out << "seeds is: (";
103 for (std::map<uint64_t, unsigned int>::iterator i = seeds.begin();
106 out << "[" << i->first << "," << i->second << "], ";
108 out << ")" << std::endl;
109 out << "written is " << written << std::endl;
110 out << "_exists is " << _exists << std::endl;
113 void encode(bufferlist &bl) const {
116 ::encode(written, bl);
117 ::encode(_exists, bl);