-
Notifications
You must be signed in to change notification settings - Fork 868
/
OClassRemote.java
263 lines (222 loc) · 8.21 KB
/
OClassRemote.java
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package com.orientechnologies.orient.core.metadata.schema;
import com.orientechnologies.orient.core.db.ODatabaseDocumentInternal;
import com.orientechnologies.orient.core.db.OScenarioThreadLocal;
import com.orientechnologies.orient.core.exception.OSchemaException;
import com.orientechnologies.orient.core.metadata.security.ORole;
import com.orientechnologies.orient.core.metadata.security.ORule;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.orientechnologies.orient.core.storage.OAutoshardedStorage;
import com.orientechnologies.orient.core.storage.OStorage;
import com.orientechnologies.orient.core.storage.OStorageProxy;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
/**
* Created by tglman on 14/06/17.
*/
public class OClassRemote extends OClassImpl {
protected OClassRemote(OSchemaShared iOwner, String iName) {
super(iOwner, iName);
}
protected OClassRemote(OSchemaShared iOwner, String iName, int[] iClusterIds) {
super(iOwner, iName, iClusterIds);
}
protected OClassRemote(OSchemaShared iOwner, ODocument iDocument, String iName) {
super(iOwner, iDocument, iName);
}
protected OProperty addProperty(final String propertyName, final OType type, final OType linkedType, final OClass linkedClass,
final boolean unsafe) {
if (type == null)
throw new OSchemaException("Property type not defined.");
if (propertyName == null || propertyName.length() == 0)
throw new OSchemaException("Property name is null or empty");
final ODatabaseDocumentInternal database = getDatabase();
validatePropertyName(propertyName);
if (database.getTransaction().isActive()) {
throw new OSchemaException("Cannot create property '" + propertyName + "' inside a transaction");
}
database.checkSecurity(ORule.ResourceGeneric.SCHEMA, ORole.PERMISSION_UPDATE);
if (linkedType != null)
OPropertyImpl.checkLinkTypeSupport(type);
if (linkedClass != null)
OPropertyImpl.checkSupportLinkedClass(type);
acquireSchemaWriteLock();
try {
final StringBuilder cmd = new StringBuilder("create property ");
// CLASS.PROPERTY NAME
cmd.append('`');
cmd.append(name);
cmd.append('`');
cmd.append('.');
cmd.append('`');
cmd.append(propertyName);
cmd.append('`');
// TYPE
cmd.append(' ');
cmd.append(type.name);
if (linkedType != null) {
// TYPE
cmd.append(' ');
cmd.append(linkedType.name);
} else if (linkedClass != null) {
// TYPE
cmd.append(' ');
cmd.append('`');
cmd.append(linkedClass.getName());
cmd.append('`');
}
if (unsafe)
cmd.append(" unsafe ");
database.command(cmd.toString());
reload();
return getProperty(propertyName);
} finally {
releaseSchemaWriteLock();
}
}
public OClassImpl setEncryption(final String iValue) {
getDatabase().checkSecurity(ORule.ResourceGeneric.SCHEMA, ORole.PERMISSION_UPDATE);
acquireSchemaWriteLock();
try {
final ODatabaseDocumentInternal database = getDatabase();
final String cmd = String.format("alter class `%s` encryption %s", name, iValue);
database.command(cmd);
} finally {
releaseSchemaWriteLock();
}
return this;
}
@Override
public OClass setClusterSelection(final String value) {
getDatabase().checkSecurity(ORule.ResourceGeneric.SCHEMA, ORole.PERMISSION_UPDATE);
acquireSchemaWriteLock();
try {
final ODatabaseDocumentInternal database = getDatabase();
final String cmd = String.format("alter class `%s` clusterselection '%s'", name, value);
database.command(cmd);
return this;
} finally {
releaseSchemaWriteLock();
}
}
public OClassImpl setCustom(final String name, final String value) {
getDatabase().checkSecurity(ORule.ResourceGeneric.SCHEMA, ORole.PERMISSION_UPDATE);
acquireSchemaWriteLock();
try {
final ODatabaseDocumentInternal database = getDatabase();
final String cmd = String.format("alter class `%s` custom %s=%s", getName(), name, value);
database.command(cmd);
return this;
} finally {
releaseSchemaWriteLock();
}
}
public void clearCustom() {
getDatabase().checkSecurity(ORule.ResourceGeneric.SCHEMA, ORole.PERMISSION_UPDATE);
acquireSchemaWriteLock();
try {
final ODatabaseDocumentInternal database = getDatabase();
final String cmd = String.format("alter class `%s` custom clear", getName());
database.command(cmd);
} finally {
releaseSchemaWriteLock();
}
}
@Override
public OClass setSuperClasses(final List<? extends OClass> classes) {
getDatabase().checkSecurity(ORule.ResourceGeneric.SCHEMA, ORole.PERMISSION_UPDATE);
if (classes != null) {
List<OClass> toCheck = new ArrayList<OClass>(classes);
toCheck.add(this);
checkParametersConflict(toCheck);
}
acquireSchemaWriteLock();
try {
final ODatabaseDocumentInternal database = getDatabase();
final StringBuilder sb = new StringBuilder();
if (classes != null && classes.size() > 0) {
for (OClass superClass : classes) {
sb.append('`').append(superClass.getName()).append("`,");
}
sb.deleteCharAt(sb.length() - 1);
} else
sb.append("null");
final String cmd = String.format("alter class `%s` superclasses %s", name, sb);
database.command(cmd);
} finally {
releaseSchemaWriteLock();
}
return this;
}
@Override
public OClass addSuperClass(final OClass superClass) {
final ODatabaseDocumentInternal database = getDatabase();
database.checkSecurity(ORule.ResourceGeneric.SCHEMA, ORole.PERMISSION_UPDATE);
checkParametersConflict(superClass);
acquireSchemaWriteLock();
try {
final String cmd = String.format("alter class `%s` superclass +`%s`", name, superClass != null ? superClass.getName() : null);
database.command(cmd);
} finally {
releaseSchemaWriteLock();
}
return this;
}
@Override
public OClass removeSuperClass(OClass superClass) {
final ODatabaseDocumentInternal database = getDatabase();
database.checkSecurity(ORule.ResourceGeneric.SCHEMA, ORole.PERMISSION_UPDATE);
acquireSchemaWriteLock();
try {
final String cmd = String.format("alter class `%s` superclass -`%s`", name, superClass != null ? superClass.getName() : null);
database.command(cmd);
} finally {
releaseSchemaWriteLock();
}
return this;
}
public OClass setName(final String name) {
if (getName().equals(name))
return this;
final ODatabaseDocumentInternal database = getDatabase();
database.checkSecurity(ORule.ResourceGeneric.SCHEMA, ORole.PERMISSION_UPDATE);
final Character wrongCharacter = OSchemaShared.checkClassNameIfValid(name);
OClass oClass = database.getMetadata().getSchema().getClass(name);
if (oClass != null) {
String error = String.format("Cannot rename class %s to %s. A Class with name %s exists", this.name, name, name);
throw new OSchemaException(error);
}
if (wrongCharacter != null)
throw new OSchemaException(
"Invalid class name found. Character '" + wrongCharacter + "' cannot be used in class name '" + name + "'");
acquireSchemaWriteLock();
try {
final String cmd = String.format("alter class `%s` name `%s`", this.name, name);
database.command(cmd);
} finally {
releaseSchemaWriteLock();
}
return this;
}
public OClass setShortName(String shortName) {
if (shortName != null) {
shortName = shortName.trim();
if (shortName.isEmpty())
shortName = null;
}
final ODatabaseDocumentInternal database = getDatabase();
database.checkSecurity(ORule.ResourceGeneric.SCHEMA, ORole.PERMISSION_UPDATE);
acquireSchemaWriteLock();
try {
final String cmd = String.format("alter class `%s` shortname `%s`", name, shortName);
database.command(cmd);
} finally {
releaseSchemaWriteLock();
}
return this;
}
protected OPropertyImpl createPropertyInstance(ODocument p) {
return new OPropertyRemote(this, p);
}
}