/
OImmutableClass.java
678 lines (534 loc) · 17.3 KB
/
OImmutableClass.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
package com.orientechnologies.orient.core.metadata.schema;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.orientechnologies.common.listener.OProgressListener;
import com.orientechnologies.orient.core.db.ODatabaseDocumentInternal;
import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal;
import com.orientechnologies.orient.core.index.OIndex;
import com.orientechnologies.orient.core.index.OIndexManager;
import com.orientechnologies.orient.core.metadata.schema.clusterselection.OClusterSelectionStrategy;
import com.orientechnologies.orient.core.metadata.security.OSecurityShared;
import com.orientechnologies.orient.core.record.impl.ODocument;
/**
* @author Andrey Lomakin (a.lomakin-at-orientechnologies.com)
* @since 10/21/14
*/
public class OImmutableClass implements OClass {
private final boolean isAbstract;
private final boolean strictMode;
private final String superClassName;
private final String name;
private final String streamAbleName;
private final Map<String, OProperty> properties;
private Map<String, OProperty> allPropertiesMap;
private Collection<OProperty> allProperties;
private final Class<?> javaClass;
private final OClusterSelectionStrategy clusterSelection;
private final int defaultClusterId;
private final int[] clusterIds;
private final int[] polymorphicClusterIds;
private final Collection<String> baseClassesNames;
private final float overSize;
private final float classOverSize;
private final String shortName;
private final Map<String, String> customFields;
private final OImmutableSchema schema;
// do not do it volatile it is already SAFE TO USE IT in MT mode.
private OImmutableClass superClass;
// do not do it volatile it is already SAFE TO USE IT in MT mode.
private Collection<OImmutableClass> baseClasses;
private boolean restricted;
public OImmutableClass(OClass oClass, OImmutableSchema schema) {
isAbstract = oClass.isAbstract();
strictMode = oClass.isStrictMode();
this.schema = schema;
if (oClass.getSuperClass() != null)
superClassName = oClass.getSuperClass().getName();
else
superClassName = null;
name = oClass.getName();
streamAbleName = oClass.getStreamableName();
clusterSelection = oClass.getClusterSelection();
defaultClusterId = oClass.getDefaultClusterId();
clusterIds = oClass.getClusterIds();
polymorphicClusterIds = oClass.getPolymorphicClusterIds();
baseClassesNames = new ArrayList<String>();
for (OClass baseClass : oClass.getBaseClasses())
baseClassesNames.add(baseClass.getName());
overSize = oClass.getOverSize();
classOverSize = oClass.getClassOverSize();
shortName = oClass.getShortName();
javaClass = oClass.getJavaClass();
properties = new HashMap<String, OProperty>();
for (OProperty p : oClass.declaredProperties())
properties.put(p.getName().toLowerCase(), new OImmutableProperty(p, this));
Map<String, String> customFields = new HashMap<String, String>();
for (String key : oClass.getCustomKeys())
customFields.put(key, oClass.getCustom(key));
this.customFields = Collections.unmodifiableMap(customFields);
}
public void init() {
initSuperClass();
final Collection<OProperty> allProperties = new ArrayList<OProperty>();
OImmutableClass currentClass = this;
do {
allProperties.addAll(currentClass.properties.values());
currentClass.initSuperClass();
currentClass = currentClass.superClass;
} while (currentClass != null);
this.allProperties = Collections.unmodifiableCollection(allProperties);
final Map<String, OProperty> allPropsMap = new HashMap<String, OProperty>(20);
OImmutableClass currentMapClass = this;
do {
for (OProperty p : currentMapClass.properties.values()) {
final String propName = p.getName();
if (!allPropsMap.containsKey(propName))
allPropsMap.put(propName, p);
}
currentMapClass.initSuperClass();
currentMapClass = currentMapClass.superClass;
} while (currentMapClass != null);
allPropertiesMap = Collections.unmodifiableMap(allPropsMap);
this.restricted = isSubClassOf(OSecurityShared.RESTRICTED_CLASSNAME);
}
@Override
public <T> T newInstance() throws InstantiationException, IllegalAccessException {
throw new UnsupportedOperationException();
}
@Override
public boolean isAbstract() {
return isAbstract;
}
@Override
public OClass setAbstract(boolean iAbstract) {
throw new UnsupportedOperationException();
}
@Override
public boolean isStrictMode() {
return strictMode;
}
@Override
public OClass setStrictMode(boolean iMode) {
throw new UnsupportedOperationException();
}
@Override
public OClass getSuperClass() {
initSuperClass();
return superClass;
}
@Override
public OClass setSuperClass(OClass iSuperClass) {
throw new UnsupportedOperationException();
}
@Override
public String getName() {
return name;
}
@Override
public OClass setName(String iName) {
throw new UnsupportedOperationException();
}
@Override
public String getStreamableName() {
return streamAbleName;
}
@Override
public Collection<OProperty> declaredProperties() {
return Collections.unmodifiableCollection(properties.values());
}
@Override
public Collection<OProperty> properties() {
return allProperties;
}
@Override
public Map<String, OProperty> propertiesMap() {
return allPropertiesMap;
}
@Override
public Collection<OProperty> getIndexedProperties() {
Collection<OProperty> indexedProps = null;
OImmutableClass currentClass = this;
initSuperClass();
do {
for (OProperty p : currentClass.properties.values())
if (areIndexed(p.getName())) {
if (indexedProps == null)
indexedProps = new ArrayList<OProperty>();
indexedProps.add(p);
}
currentClass.initSuperClass();
currentClass = currentClass.superClass;
} while (currentClass != null);
return (Collection<OProperty>) (indexedProps != null ? Collections.unmodifiableCollection(indexedProps) : Collections
.emptyList());
}
@Override
public OProperty getProperty(String propertyName) {
initSuperClass();
propertyName = propertyName.toLowerCase();
OImmutableClass currentClass = this;
do {
final OProperty p = currentClass.properties.get(propertyName);
if (p != null)
return p;
currentClass.initSuperClass();
currentClass = currentClass.superClass;
} while (currentClass != null);
return null;
}
@Override
public OProperty createProperty(String iPropertyName, OType iType) {
throw new UnsupportedOperationException();
}
@Override
public OProperty createProperty(String iPropertyName, OType iType, OClass iLinkedClass) {
throw new UnsupportedOperationException();
}
@Override
public OProperty createProperty(String iPropertyName, OType iType, OType iLinkedType) {
throw new UnsupportedOperationException();
}
@Override
public void dropProperty(String iPropertyName) {
throw new UnsupportedOperationException();
}
@Override
public boolean existsProperty(String propertyName) {
propertyName = propertyName.toLowerCase();
OImmutableClass currentClass = this;
do {
final boolean result = currentClass.properties.containsKey(propertyName);
if (result)
return true;
currentClass = (OImmutableClass) currentClass.getSuperClass();
} while (currentClass != null);
return false;
}
@Override
public Class<?> getJavaClass() {
return javaClass;
}
@Override
public int getClusterForNewInstance(final ODocument doc) {
return clusterSelection.getCluster(this, doc);
}
@Override
public int getDefaultClusterId() {
return defaultClusterId;
}
@Override
public void setDefaultClusterId(int iDefaultClusterId) {
throw new UnsupportedOperationException();
}
@Override
public int[] getClusterIds() {
return clusterIds;
}
@Override
public OClass addClusterId(int iId) {
throw new UnsupportedOperationException();
}
@Override
public OClusterSelectionStrategy getClusterSelection() {
return clusterSelection;
}
@Override
public OClass setClusterSelection(OClusterSelectionStrategy clusterSelection) {
throw new UnsupportedOperationException();
}
@Override
public OClass setClusterSelection(String iStrategyName) {
throw new UnsupportedOperationException();
}
@Override
public OClass addCluster(String iClusterName) {
throw new UnsupportedOperationException();
}
@Override
public OClass removeClusterId(int iId) {
throw new UnsupportedOperationException();
}
@Override
public int[] getPolymorphicClusterIds() {
return polymorphicClusterIds;
}
public OImmutableSchema getSchema() {
return schema;
}
@Override
public Collection<OClass> getBaseClasses() {
initBaseClasses();
ArrayList<OClass> result = new ArrayList<OClass>();
for (OClass c : baseClasses)
result.add(c);
return result;
}
@Override
public Collection<OClass> getAllBaseClasses() {
initBaseClasses();
final Set<OClass> set = new HashSet<OClass>();
set.addAll(getBaseClasses());
for (OImmutableClass c : baseClasses)
set.addAll(c.getAllBaseClasses());
return set;
}
@Override
public long getSize() {
long size = 0;
for (int clusterId : clusterIds)
size += getDatabase().getClusterRecordSizeById(clusterId);
return size;
}
@Override
public float getOverSize() {
return overSize;
}
@Override
public float getClassOverSize() {
return classOverSize;
}
@Override
public OClass setOverSize(float overSize) {
throw new UnsupportedOperationException();
}
@Override
public long count() {
return count(true);
}
@Override
public long count(boolean isPolymorphic) {
if (isPolymorphic)
return getDatabase().countClusterElements(OClassImpl.readableClusters(getDatabase(), polymorphicClusterIds));
return getDatabase().countClusterElements(OClassImpl.readableClusters(getDatabase(), clusterIds));
}
@Override
public void truncate() throws IOException {
throw new UnsupportedOperationException();
}
@Override
public boolean isSubClassOf(String iClassName) {
if (iClassName == null)
return false;
OClass cls = this;
do {
if (iClassName.equalsIgnoreCase(cls.getName()) || iClassName.equalsIgnoreCase(cls.getShortName()))
return true;
cls = cls.getSuperClass();
} while (cls != null);
return false;
}
@Override
public boolean isSubClassOf(OClass clazz) {
if (clazz == null)
return false;
OClass cls = this;
while (cls != null) {
if (cls.equals(clazz))
return true;
cls = cls.getSuperClass();
}
return false;
}
@Override
public boolean isSuperClassOf(OClass clazz) {
return clazz != null && clazz.isSubClassOf(this);
}
@Override
public String getShortName() {
return shortName;
}
@Override
public OClass setShortName(String shortName) {
throw new UnsupportedOperationException();
}
@Override
public Object get(ATTRIBUTES iAttribute) {
if (iAttribute == null)
throw new IllegalArgumentException("attribute is null");
switch (iAttribute) {
case NAME:
return getName();
case SHORTNAME:
return getShortName();
case SUPERCLASS:
return getSuperClass();
case OVERSIZE:
return getOverSize();
case STRICTMODE:
return isStrictMode();
case ABSTRACT:
return isAbstract();
case CLUSTERSELECTION:
return getClusterSelection();
case CUSTOM:
return getCustomInternal();
}
throw new IllegalArgumentException("Cannot find attribute '" + iAttribute + "'");
}
@Override
public OClass set(ATTRIBUTES attribute, Object iValue) {
throw new UnsupportedOperationException();
}
@Override
public OIndex<?> createIndex(String iName, INDEX_TYPE iType, String... fields) {
throw new UnsupportedOperationException();
}
@Override
public OIndex<?> createIndex(String iName, String iType, String... fields) {
throw new UnsupportedOperationException();
}
@Override
public OIndex<?> createIndex(String iName, INDEX_TYPE iType, OProgressListener iProgressListener, String... fields) {
throw new UnsupportedOperationException();
}
@Override
public OIndex<?> createIndex(String iName, String iType, OProgressListener iProgressListener, ODocument metadata,
String algorithm, String... fields) {
throw new UnsupportedOperationException();
}
@Override
public OIndex<?> createIndex(String iName, String iType, OProgressListener iProgressListener, ODocument metadata,
String... fields) {
throw new UnsupportedOperationException();
}
@Override
public Set<OIndex<?>> getInvolvedIndexes(Collection<String> fields) {
initSuperClass();
final Set<OIndex<?>> result = new HashSet<OIndex<?>>(getClassInvolvedIndexes(fields));
if (superClass != null)
result.addAll(superClass.getInvolvedIndexes(fields));
return result;
}
@Override
public Set<OIndex<?>> getInvolvedIndexes(String... fields) {
return getInvolvedIndexes(Arrays.asList(fields));
}
@Override
public Set<OIndex<?>> getClassInvolvedIndexes(Collection<String> fields) {
final OIndexManager indexManager = getDatabase().getMetadata().getIndexManager();
return indexManager.getClassInvolvedIndexes(name, fields);
}
@Override
public Set<OIndex<?>> getClassInvolvedIndexes(String... fields) {
return getClassInvolvedIndexes(Arrays.asList(fields));
}
@Override
public boolean areIndexed(Collection<String> fields) {
final OIndexManager indexManager = getDatabase().getMetadata().getIndexManager();
final boolean currentClassResult = indexManager.areIndexed(name, fields);
initSuperClass();
if (superClass != null)
return currentClassResult || superClass.areIndexed(fields);
return currentClassResult;
}
@Override
public boolean areIndexed(String... fields) {
return areIndexed(Arrays.asList(fields));
}
@Override
public OIndex<?> getClassIndex(String iName) {
return getDatabase().getMetadata().getIndexManager().getClassIndex(this.name, name);
}
@Override
public Set<OIndex<?>> getClassIndexes() {
return getDatabase().getMetadata().getIndexManager().getClassIndexes(name);
}
@Override
public void getClassIndexes(Collection<OIndex<?>> indexes) {
getDatabase().getMetadata().getIndexManager().getClassIndexes(name, indexes);
}
@Override
public Set<OIndex<?>> getIndexes() {
initSuperClass();
final Set<OIndex<?>> indexes = getClassIndexes();
for (OClass s = superClass; s != null; s = s.getSuperClass()) {
s.getClassIndexes(indexes);
}
return indexes;
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result;
return result;
}
@Override
public boolean equals(final Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!OClass.class.isAssignableFrom(obj.getClass()))
return false;
final OClass other = (OClass) obj;
if (name == null) {
if (other.getName() != null)
return false;
} else if (!name.equals(other.getName()))
return false;
return true;
}
@Override
public String toString() {
return name;
}
@Override
public String getCustom(String iName) {
return customFields.get(iName);
}
@Override
public OClass setCustom(String iName, String iValue) {
throw new UnsupportedOperationException();
}
@Override
public void removeCustom(String iName) {
throw new UnsupportedOperationException();
}
@Override
public void clearCustom() {
throw new UnsupportedOperationException();
}
@Override
public Set<String> getCustomKeys() {
return Collections.unmodifiableSet(customFields.keySet());
}
@Override
public boolean hasClusterId(int clusterId) {
return Arrays.binarySearch(clusterIds, clusterId) >= 0;
}
@Override
public int compareTo(OClass other) {
return name.compareTo(other.getName());
}
private ODatabaseDocumentInternal getDatabase() {
return ODatabaseRecordThreadLocal.INSTANCE.get();
}
private Map<String, String> getCustomInternal() {
return customFields;
}
private void initSuperClass() {
if (superClassName != null && superClass == null) {
superClass = (OImmutableClass) schema.getClass(superClassName);
}
}
private void initBaseClasses() {
if (baseClasses == null) {
final List<OImmutableClass> result = new ArrayList<OImmutableClass>(baseClassesNames.size());
for (String clsName : baseClassesNames)
result.add((OImmutableClass) schema.getClass(clsName));
baseClasses = result;
}
}
public boolean isRestricted() {
return restricted;
}
}