Skip to content

Commit b6fe726

Browse files
author
Vitaly Baranov
committed
Rename row policy's 'name' to 'short_name', 'full_name' to 'name'.
This change simplifies the interface of IAccesEntity.
1 parent 6f15a0d commit b6fe726

25 files changed

+153
-174
lines changed

src/Access/DiskAccessStorage.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ bool DiskAccessStorage::rebuildLists()
525525

526526
auto type = entity->getType();
527527
auto & name_to_id_map = name_to_id_maps.at(type);
528-
auto it_by_name = name_to_id_map.emplace(entity->getFullName(), id).first;
528+
auto it_by_name = name_to_id_map.emplace(entity->getName(), id).first;
529529
id_to_entry_map.emplace(id, Entry{it_by_name->first, type});
530530
}
531531

@@ -609,7 +609,7 @@ UUID DiskAccessStorage::insertImpl(const AccessEntityPtr & new_entity, bool repl
609609

610610
void DiskAccessStorage::insertNoLock(const UUID & id, const AccessEntityPtr & new_entity, bool replace_if_exists, Notifications & notifications)
611611
{
612-
const String & name = new_entity->getFullName();
612+
const String & name = new_entity->getName();
613613
std::type_index type = new_entity->getType();
614614
if (!initialized)
615615
throw Exception(
@@ -622,7 +622,7 @@ void DiskAccessStorage::insertNoLock(const UUID & id, const AccessEntityPtr & ne
622622
if (it_by_id != id_to_entry_map.end())
623623
{
624624
const auto & existing_entry = it_by_id->second;
625-
throwIDCollisionCannotInsert(id, type, name, existing_entry.entity->getType(), existing_entry.entity->getFullName());
625+
throwIDCollisionCannotInsert(id, type, name, existing_entry.entity->getType(), existing_entry.entity->getName());
626626
}
627627

628628
auto & name_to_id_map = name_to_id_maps.at(type);
@@ -703,7 +703,7 @@ void DiskAccessStorage::updateNoLock(const UUID & id, const UpdateFunc & update_
703703
if (*new_entity == *old_entity)
704704
return;
705705

706-
String new_name = new_entity->getFullName();
706+
String new_name = new_entity->getName();
707707
auto old_name = entry.name;
708708
const std::type_index type = entry.type;
709709
bool name_changed = (new_name != old_name);

src/Access/IAccessEntity.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ const char * IAccessEntity::getKeyword(std::type_index type)
4343

4444
bool IAccessEntity::equal(const IAccessEntity & other) const
4545
{
46-
return (full_name == other.full_name) && (getType() == other.getType());
46+
return (name == other.name) && (getType() == other.getType());
4747
}
4848
}

src/Access/IAccessEntity.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,14 @@ struct IAccessEntity
2727
bool isTypeOf() const { return isTypeOf(typeid(EntityType)); }
2828
bool isTypeOf(std::type_index type) const { return type == getType(); }
2929

30-
virtual void setName(const String & name_) { full_name = name_; }
31-
virtual String getName() const { return full_name; }
32-
String getFullName() const { return full_name; }
30+
virtual void setName(const String & name_) { name = name_; }
31+
const String & getName() const { return name; }
3332

3433
friend bool operator ==(const IAccessEntity & lhs, const IAccessEntity & rhs) { return lhs.equal(rhs); }
3534
friend bool operator !=(const IAccessEntity & lhs, const IAccessEntity & rhs) { return !(lhs == rhs); }
3635

3736
protected:
38-
String full_name;
37+
String name;
3938

4039
virtual bool equal(const IAccessEntity & other) const;
4140

src/Access/IAccessStorage.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ std::shared_ptr<const EntityType> IAccessStorage::read(const UUID & id) const
179179
auto ptr = typeid_cast<std::shared_ptr<const EntityType>>(entity);
180180
if (ptr)
181181
return ptr;
182-
throwBadCast(id, entity->getType(), entity->getFullName(), typeid(EntityType));
182+
throwBadCast(id, entity->getType(), entity->getName(), typeid(EntityType));
183183
}
184184

185185

src/Access/MemoryAccessStorage.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ AccessEntityPtr MemoryAccessStorage::readImpl(const UUID & id) const
5555

5656
String MemoryAccessStorage::readNameImpl(const UUID & id) const
5757
{
58-
return readImpl(id)->getFullName();
58+
return readImpl(id)->getName();
5959
}
6060

6161

@@ -73,15 +73,15 @@ UUID MemoryAccessStorage::insertImpl(const AccessEntityPtr & new_entity, bool re
7373

7474
void MemoryAccessStorage::insertNoLock(const UUID & id, const AccessEntityPtr & new_entity, bool replace_if_exists, Notifications & notifications)
7575
{
76-
const String & name = new_entity->getFullName();
76+
const String & name = new_entity->getName();
7777
std::type_index type = new_entity->getType();
7878

7979
/// Check that we can insert.
8080
auto it = entries.find(id);
8181
if (it != entries.end())
8282
{
8383
const auto & existing_entry = it->second;
84-
throwIDCollisionCannotInsert(id, type, name, existing_entry.entity->getType(), existing_entry.entity->getFullName());
84+
throwIDCollisionCannotInsert(id, type, name, existing_entry.entity->getType(), existing_entry.entity->getName());
8585
}
8686

8787
auto it2 = names.find({name, type});
@@ -120,7 +120,7 @@ void MemoryAccessStorage::removeNoLock(const UUID & id, Notifications & notifica
120120
throwNotFound(id);
121121

122122
Entry & entry = it->second;
123-
const String & name = entry.entity->getFullName();
123+
const String & name = entry.entity->getName();
124124
std::type_index type = entry.entity->getType();
125125

126126
prepareNotifications(entry, true, notifications);
@@ -156,14 +156,14 @@ void MemoryAccessStorage::updateNoLock(const UUID & id, const UpdateFunc & updat
156156

157157
entry.entity = new_entity;
158158

159-
if (new_entity->getFullName() != old_entity->getFullName())
159+
if (new_entity->getName() != old_entity->getName())
160160
{
161-
auto it2 = names.find({new_entity->getFullName(), new_entity->getType()});
161+
auto it2 = names.find({new_entity->getName(), new_entity->getType()});
162162
if (it2 != names.end())
163-
throwNameCollisionCannotRename(old_entity->getType(), old_entity->getFullName(), new_entity->getFullName());
163+
throwNameCollisionCannotRename(old_entity->getType(), old_entity->getName(), new_entity->getName());
164164

165-
names.erase({old_entity->getFullName(), old_entity->getType()});
166-
names[std::pair{new_entity->getFullName(), new_entity->getType()}] = &entry;
165+
names.erase({old_entity->getName(), old_entity->getType()});
166+
names[std::pair{new_entity->getName(), new_entity->getType()}] = &entry;
167167
}
168168

169169
prepareNotifications(entry, false, notifications);
@@ -211,7 +211,7 @@ void MemoryAccessStorage::setAllNoLock(const std::vector<std::pair<UUID, AccessE
211211
continue;
212212
}
213213
}
214-
auto it2 = names.find({entity->getFullName(), entity->getType()});
214+
auto it2 = names.find({entity->getName(), entity->getType()});
215215
if (it2 != names.end())
216216
{
217217
Entry & entry = *(it2->second);

src/Access/RowPolicy.cpp

Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include <Access/RowPolicy.h>
2-
#include <Interpreters/Context.h>
32
#include <Common/quoteString.h>
43
#include <boost/range/algorithm/equal.hpp>
54

@@ -8,71 +7,62 @@ namespace DB
87
{
98
namespace ErrorCodes
109
{
10+
extern const int NOT_IMPLEMENTED;
1111
extern const int LOGICAL_ERROR;
1212
}
1313

1414

15-
namespace
15+
String RowPolicy::NameParts::getName() const
1616
{
17-
void generateFullNameImpl(const String & database_, const String & table_name_, const String & policy_name_, String & full_name_)
17+
String name;
18+
name.reserve(database.length() + table_name.length() + short_name.length() + 6);
19+
name += backQuoteIfNeed(short_name);
20+
name += " ON ";
21+
if (!name.empty())
1822
{
19-
full_name_.clear();
20-
full_name_.reserve(database_.length() + table_name_.length() + policy_name_.length() + 6);
21-
full_name_ += backQuoteIfNeed(policy_name_);
22-
full_name_ += " ON ";
23-
if (!database_.empty())
24-
{
25-
full_name_ += backQuoteIfNeed(database_);
26-
full_name_ += '.';
27-
}
28-
full_name_ += backQuoteIfNeed(table_name_);
23+
name += backQuoteIfNeed(database);
24+
name += '.';
2925
}
26+
name += backQuoteIfNeed(table_name);
27+
return name;
3028
}
3129

3230

33-
String RowPolicy::FullNameParts::getFullName() const
31+
void RowPolicy::setDatabase(const String & database)
3432
{
35-
String full_name;
36-
generateFullNameImpl(database, table_name, policy_name, full_name);
37-
return full_name;
33+
name_parts.database = database;
34+
IAccessEntity::setName(name_parts.getName());
3835
}
3936

40-
41-
String RowPolicy::FullNameParts::getFullName(const Context & context) const
37+
void RowPolicy::setTableName(const String & table_name)
4238
{
43-
String full_name;
44-
generateFullNameImpl(database.empty() ? context.getCurrentDatabase() : database, table_name, policy_name, full_name);
45-
return full_name;
39+
name_parts.table_name = table_name;
40+
IAccessEntity::setName(name_parts.getName());
4641
}
4742

48-
49-
void RowPolicy::setDatabase(const String & database_)
43+
void RowPolicy::setShortName(const String & short_name)
5044
{
51-
database = database_;
52-
generateFullNameImpl(database, table_name, policy_name, full_name);
45+
name_parts.short_name = short_name;
46+
IAccessEntity::setName(name_parts.getName());
5347
}
5448

55-
56-
void RowPolicy::setTableName(const String & table_name_)
49+
void RowPolicy::setNameParts(const String & short_name, const String & database, const String & table_name)
5750
{
58-
table_name = table_name_;
59-
generateFullNameImpl(database, table_name, policy_name, full_name);
51+
name_parts.short_name = short_name;
52+
name_parts.database = database;
53+
name_parts.table_name = table_name;
54+
IAccessEntity::setName(name_parts.getName());
6055
}
6156

62-
63-
void RowPolicy::setName(const String & policy_name_)
57+
void RowPolicy::setNameParts(const NameParts & name_parts_)
6458
{
65-
policy_name = policy_name_;
66-
generateFullNameImpl(database, table_name, policy_name, full_name);
59+
name_parts = name_parts_;
60+
IAccessEntity::setName(name_parts.getName());
6761
}
6862

69-
70-
void RowPolicy::setFullName(const String & database_, const String & table_name_, const String & policy_name_)
63+
void RowPolicy::setName(const String &)
7164
{
72-
database = database_;
73-
table_name = table_name_;
74-
policy_name = policy_name_;
75-
generateFullNameImpl(database, table_name, policy_name, full_name);
65+
throw Exception("RowPolicy::setName() is not implemented", ErrorCodes::NOT_IMPLEMENTED);
7666
}
7767

7868

@@ -81,9 +71,8 @@ bool RowPolicy::equal(const IAccessEntity & other) const
8171
if (!IAccessEntity::equal(other))
8272
return false;
8373
const auto & other_policy = typeid_cast<const RowPolicy &>(other);
84-
return (database == other_policy.database) && (table_name == other_policy.table_name) && (policy_name == other_policy.policy_name)
85-
&& boost::range::equal(conditions, other_policy.conditions) && restrictive == other_policy.restrictive
86-
&& (to_roles == other_policy.to_roles);
74+
return (name_parts == other_policy.name_parts) && boost::range::equal(conditions, other_policy.conditions)
75+
&& restrictive == other_policy.restrictive && (to_roles == other_policy.to_roles);
8776
}
8877

8978

src/Access/RowPolicy.h

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,34 @@
66

77
namespace DB
88
{
9-
class Context;
10-
119

1210
/** Represents a row level security policy for a table.
1311
*/
1412
struct RowPolicy : public IAccessEntity
1513
{
16-
void setDatabase(const String & database_);
17-
void setTableName(const String & table_name_);
18-
void setName(const String & policy_name_) override;
19-
void setFullName(const String & database_, const String & table_name_, const String & policy_name_);
20-
21-
String getDatabase() const { return database; }
22-
String getTableName() const { return table_name; }
23-
String getName() const override { return policy_name; }
24-
25-
struct FullNameParts
14+
struct NameParts
2615
{
16+
String short_name;
2717
String database;
2818
String table_name;
29-
String policy_name;
30-
String getFullName() const;
31-
String getFullName(const Context & context) const;
19+
20+
String getName() const;
21+
auto toTuple() const { return std::tie(short_name, database, table_name); }
22+
friend bool operator ==(const NameParts & left, const NameParts & right) { return left.toTuple() == right.toTuple(); }
23+
friend bool operator !=(const NameParts & left, const NameParts & right) { return left.toTuple() != right.toTuple(); }
3224
};
3325

26+
void setShortName(const String & short_name);
27+
void setDatabase(const String & database);
28+
void setTableName(const String & table_name);
29+
void setNameParts(const String & short_name, const String & database, const String & table_name);
30+
void setNameParts(const NameParts & name_parts);
31+
32+
const String & getDatabase() const { return name_parts.database; }
33+
const String & getTableName() const { return name_parts.table_name; }
34+
const String & getShortName() const { return name_parts.short_name; }
35+
const NameParts & getNameParts() const { return name_parts; }
36+
3437
/// Filter is a SQL conditional expression used to figure out which rows should be visible
3538
/// for user or available for modification. If the expression returns NULL or false for some rows
3639
/// those rows are silently suppressed.
@@ -71,9 +74,9 @@ struct RowPolicy : public IAccessEntity
7174
ExtendedRoleSet to_roles;
7275

7376
private:
74-
String database;
75-
String table_name;
76-
String policy_name;
77+
void setName(const String & name_) override;
78+
79+
NameParts name_parts;
7780
bool restrictive = false;
7881
};
7982

src/Access/RowPolicyCache.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ void RowPolicyCache::PolicyInfo::setPolicy(const RowPolicyPtr & policy_)
8585
tryLogCurrentException(
8686
&Poco::Logger::get("RowPolicy"),
8787
String("Could not parse the condition ") + RowPolicy::conditionTypeToString(type) + " of row policy "
88-
+ backQuote(policy->getFullName()));
88+
+ backQuote(policy->getName()));
8989
}
9090
}
9191
}

src/Access/UsersConfigAccessStorage.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ namespace
5454
}
5555

5656

57-
UUID generateID(const IAccessEntity & entity) { return generateID(entity.getType(), entity.getFullName()); }
57+
UUID generateID(const IAccessEntity & entity) { return generateID(entity.getType(), entity.getName()); }
5858

5959
UserPtr parseUser(const Poco::Util::AbstractConfiguration & config, const String & user_name)
6060
{
@@ -344,7 +344,7 @@ namespace
344344
String filter = (it != user_to_filters.end()) ? it->second : "1";
345345

346346
auto policy = std::make_shared<RowPolicy>();
347-
policy->setFullName(database, table_name, user_name);
347+
policy->setNameParts(user_name, database, table_name);
348348
policy->conditions[RowPolicy::SELECT_FILTER] = filter;
349349
policy->to_roles.add(generateID(typeid(User), user_name));
350350
policies.push_back(policy);
@@ -494,21 +494,21 @@ String UsersConfigAccessStorage::readNameImpl(const UUID & id) const
494494

495495
UUID UsersConfigAccessStorage::insertImpl(const AccessEntityPtr & entity, bool)
496496
{
497-
throwReadonlyCannotInsert(entity->getType(), entity->getFullName());
497+
throwReadonlyCannotInsert(entity->getType(), entity->getName());
498498
}
499499

500500

501501
void UsersConfigAccessStorage::removeImpl(const UUID & id)
502502
{
503503
auto entity = read(id);
504-
throwReadonlyCannotRemove(entity->getType(), entity->getFullName());
504+
throwReadonlyCannotRemove(entity->getType(), entity->getName());
505505
}
506506

507507

508508
void UsersConfigAccessStorage::updateImpl(const UUID & id, const UpdateFunc &)
509509
{
510510
auto entity = read(id);
511-
throwReadonlyCannotUpdate(entity->getType(), entity->getFullName());
511+
throwReadonlyCannotUpdate(entity->getType(), entity->getName());
512512
}
513513

514514

src/Functions/currentRowPolicies.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class FunctionCurrentRowPolicies : public IFunction
7474
{
7575
const String database = policy->getDatabase();
7676
const String table_name = policy->getTableName();
77-
const String policy_name = policy->getName();
77+
const String policy_name = policy->getShortName();
7878
database_column->insertData(database.data(), database.length());
7979
table_name_column->insertData(table_name.data(), table_name.length());
8080
policy_name_column->insertData(policy_name.data(), policy_name.length());
@@ -123,7 +123,7 @@ class FunctionCurrentRowPolicies : public IFunction
123123
const auto policy = context.getAccessControlManager().tryRead<RowPolicy>(policy_id);
124124
if (policy)
125125
{
126-
const String policy_name = policy->getName();
126+
const String policy_name = policy->getShortName();
127127
policy_name_column->insertData(policy_name.data(), policy_name.length());
128128
}
129129
}

0 commit comments

Comments
 (0)