summaryrefslogtreecommitdiffstats
path: root/src/rgw/rgw_user_types.h
blob: c9a1a46ade1e9b0317667aa1d9c32cdeb6c72285 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab ft=cpp

/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2019 Red Hat, Inc.
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation. See file COPYING.
 *
 */

/* N.B., this header defines fundamental serialized types.  Do not
 * include files which can only be compiled in radosgw or OSD
 * contexts (e.g., rgw_sal.h, rgw_common.h) */

#pragma once

#include <string_view>
#include <fmt/format.h>

#include "common/dout.h"
#include "common/Formatter.h"

struct rgw_user {
  std::string tenant;
  std::string id;
  std::string ns;

  rgw_user() {}
  explicit rgw_user(const std::string& s) {
    from_str(s);
  }
  rgw_user(const std::string& tenant, const std::string& id, const std::string& ns="")
    : tenant(tenant),
      id(id),
      ns(ns) {
  }
  rgw_user(std::string&& tenant, std::string&& id, std::string&& ns="")
    : tenant(std::move(tenant)),
      id(std::move(id)),
      ns(std::move(ns)) {
  }

  void encode(ceph::buffer::list& bl) const {
    ENCODE_START(2, 1, bl);
    encode(tenant, bl);
    encode(id, bl);
    encode(ns, bl);
    ENCODE_FINISH(bl);
  }
  void decode(ceph::buffer::list::const_iterator& bl) {
    DECODE_START(2, bl);
    decode(tenant, bl);
    decode(id, bl);
    if (struct_v >= 2) {
      decode(ns, bl);
    }
    DECODE_FINISH(bl);
  }

  void to_str(std::string& str) const {
    if (!tenant.empty()) {
      if (!ns.empty()) {
        str = tenant + '$' + ns + '$' + id;
      } else {
        str = tenant + '$' + id;
      }
    } else if (!ns.empty()) {
      str = '$' + ns + '$' + id;
    } else {
      str = id;
    }
  }

  void clear() {
    tenant.clear();
    id.clear();
    ns.clear();
  }

  bool empty() const {
    return id.empty();
  }

  std::string to_str() const {
    std::string s;
    to_str(s);
    return s;
  }

  void from_str(const std::string& str) {
    size_t pos = str.find('$');
    if (pos != std::string::npos) {
      tenant = str.substr(0, pos);
      std::string_view sv = str;
      std::string_view ns_id = sv.substr(pos + 1);
      size_t ns_pos = ns_id.find('$');
      if (ns_pos != std::string::npos) {
        ns = std::string(ns_id.substr(0, ns_pos));
        id = std::string(ns_id.substr(ns_pos + 1));
      } else {
        ns.clear();
        id = std::string(ns_id);
      }
    } else {
      tenant.clear();
      ns.clear();
      id = str;
    }
  }

  rgw_user& operator=(const std::string& str) {
    from_str(str);
    return *this;
  }

  int compare(const rgw_user& u) const {
    int r = tenant.compare(u.tenant);
    if (r != 0)
      return r;
    r = ns.compare(u.ns);
    if (r != 0) {
      return r;
    }
    return id.compare(u.id);
  }
  int compare(const std::string& str) const {
    rgw_user u(str);
    return compare(u);
  }

  bool operator!=(const rgw_user& rhs) const {
    return (compare(rhs) != 0);
  }
  bool operator==(const rgw_user& rhs) const {
    return (compare(rhs) == 0);
  }
  bool operator<(const rgw_user& rhs) const {
    if (tenant < rhs.tenant) {
      return true;
    } else if (tenant > rhs.tenant) {
      return false;
    }
    if (ns < rhs.ns) {
      return true;
    } else if (ns > rhs.ns) {
      return false;
    }
    return (id < rhs.id);
  }
  void dump(ceph::Formatter *f) const;
  static void generate_test_instances(std::list<rgw_user*>& o);
};
WRITE_CLASS_ENCODER(rgw_user)