summaryrefslogtreecommitdiffstats
path: root/src/crimson/os/cyan_collection.cc
blob: 82403dbc948c225eb67cb76c1eab9a40c1c0bdb4 (plain)
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
#include "cyan_collection.h"

#include "cyan_object.h"

namespace ceph::os
{

Collection::Collection(const coll_t& c)
  : cid{c}
{}

Collection::~Collection() = default;

Collection::ObjectRef Collection::create_object() const
{
  return new ceph::os::Object{};
}

Collection::ObjectRef Collection::get_object(ghobject_t oid)
{
  auto o = object_hash.find(oid);
  if (o == object_hash.end())
    return ObjectRef();
  return o->second;
}

Collection::ObjectRef Collection::get_or_create_object(ghobject_t oid)
{
  auto result = object_hash.emplace(oid, ObjectRef{});
  if (result.second)
    object_map[oid] = result.first->second = create_object();
  return result.first->second;
}

uint64_t Collection::used_bytes() const
{
  uint64_t result = 0;
  for (auto& obj : object_map) {
    result += obj.second->get_size();
  }
  return result;
}

void Collection::encode(bufferlist& bl) const
{
  ENCODE_START(1, 1, bl);
  encode(xattr, bl);
  encode(use_page_set, bl);
  uint32_t s = object_map.size();
  encode(s, bl);
  for (auto& [oid, obj] : object_map) {
    encode(oid, bl);
    obj->encode(bl);
  }
  ENCODE_FINISH(bl);
}

void Collection::decode(bufferlist::const_iterator& p)
{
  DECODE_START(1, p);
  decode(xattr, p);
  decode(use_page_set, p);
  uint32_t s;
  decode(s, p);
  while (s--) {
    ghobject_t k;
    decode(k, p);
    auto o = create_object();
    o->decode(p);
    object_map.insert(make_pair(k, o));
    object_hash.insert(make_pair(k, o));
  }
  DECODE_FINISH(p);
}

}