protected static void checkSupportLinkedClass(OType type) { if (type != OType.LINK && type != OType.LINKSET && type != OType.LINKLIST && type != OType.LINKMAP && type != OType.EMBEDDED && type != OType.EMBEDDEDSET && type != OType.EMBEDDEDLIST && type != OType.EMBEDDEDMAP && type != OType.LINKBAG) throw new OSchemaException("Linked class is not supported for type: " + type); }
protected static void checkLinkTypeSupport(OType type) { if (type != OType.EMBEDDEDSET && type != OType.EMBEDDEDLIST && type != OType.EMBEDDEDMAP) throw new OSchemaException("Linked type is not supported for type: " + type); }
private void checkClustersAreAbsent(final int[] iClusterIds) { if (iClusterIds == null) return; for (int clusterId : iClusterIds) { if (clusterId < 0) continue; if (clustersToClasses.containsKey(clusterId)) throw new OSchemaException( "Cluster with id " + clusterId + " already belongs to class " + clustersToClasses.get(clusterId)); } }
void checkClusterCanBeAdded(int clusterId, OClass cls) { acquireSchemaReadLock(); try { if (clusterId < 0) return; if (blobClusters.contains(clusterId)) throw new OSchemaException("Cluster with id " + clusterId + " already belongs to Blob"); final OClass existingCls = clustersToClasses.get(clusterId); if (existingCls != null && (cls == null || !cls.equals(existingCls))) throw new OSchemaException( "Cluster with id " + clusterId + " already belongs to the class '" + clustersToClasses.get(clusterId) + "'"); } finally { releaseSchemaReadLock(); } }
void addClusterForClass(ODatabaseDocumentInternal database, final int clusterId, final OClass cls) { acquireSchemaWriteLock(database); try { if (clusterId < 0) return; checkEmbedded(); final OClass existingCls = clustersToClasses.get(clusterId); if (existingCls != null && !cls.equals(existingCls)) throw new OSchemaException( "Cluster with id " + clusterId + " already belongs to class " + clustersToClasses.get(clusterId)); clustersToClasses.put(clusterId, cls); } finally { releaseSchemaWriteLock(database); } }
public OClass createClass(ODatabaseDocumentInternal database, final String className, int clusters, OClass... superClasses) { final Character wrongCharacter = OSchemaShared.checkClassNameIfValid(className); if (wrongCharacter != null) throw new OSchemaException( "Invalid class name found. Character '" + wrongCharacter + "' cannot be used in class name '" + className + "'"); return doCreateClass(database, className, clusters, superClasses); }
private void checkRecursion(final OClass baseClass) { if (isSubClassOf(baseClass)) { throw new OSchemaException("Cannot add base class '" + baseClass.getName() + "', because of recursion"); } }
protected static void checkParametersConflict(List<OClass> classes) { final Map<String, OProperty> comulative = new HashMap<String, OProperty>(); final Map<String, OProperty> properties = new HashMap<String, OProperty>(); for (OClass superClass : classes) { if (superClass == null) continue; OClassImpl impl; if (superClass instanceof OClassAbstractDelegate) impl = (OClassImpl) ((OClassAbstractDelegate) superClass).delegate; else impl = (OClassImpl) superClass; impl.propertiesMap(properties); for (Map.Entry<String, OProperty> entry : properties.entrySet()) { if (comulative.containsKey(entry.getKey())) { final String property = entry.getKey(); final OProperty existingProperty = comulative.get(property); if (!existingProperty.getType().equals(entry.getValue().getType())) { throw new OSchemaException("Properties conflict detected: '" + existingProperty + "] vs [" + entry.getValue() + "]"); } } } comulative.putAll(properties); properties.clear(); } }
public byte[] toStream(final ORecord iRecord, boolean iOnlyDelta) { final long timer = PROFILER.startChrono(); try { return toString(iRecord, new StringBuilder(2048), null, iOnlyDelta, true).toString().getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { throw OException.wrapException(new OSchemaException("error encoding string"), e); } finally { PROFILER.stopChrono(PROFILER.getProcessMetric("serializer.record.string.toStream"), "Serialize record to stream", timer); } }
public OClass createClass(ODatabaseDocumentInternal database, final String className, int[] clusterIds, OClass... superClasses) { final Character wrongCharacter = OSchemaShared.checkClassNameIfValid(className); if (wrongCharacter != null) throw new OSchemaException( "Invalid class name found. Character '" + wrongCharacter + "' cannot be used in class name '" + className + "'"); OClass result; int retry = 0; while (true) try { result = doCreateClass(database, className, clusterIds, retry, superClasses); break; } catch (ClusterIdsAreEmptyException ignore) { classes.remove(className.toLowerCase(Locale.ENGLISH)); clusterIds = createClusters(database, className); retry++; } return result; }
protected void setClass(final OClass iClass) { if (iClass != null && iClass.isAbstract()) throw new OSchemaException("Cannot create a document of the abstract class '" + iClass + "'"); if (iClass == null) _className = null; else _className = iClass.getName(); _immutableClazz = null; _immutableSchemaVersion = -1; if (iClass != null) convertFieldsToClass(iClass); }
public OGlobalProperty createGlobalProperty(final String name, final OType type, final Integer id) { OGlobalProperty global; if (id < properties.size() && (global = properties.get(id)) != null) { if (!global.getName().equals(name) || !global.getType().equals(type)) throw new OSchemaException("A property with id " + id + " already exist "); return global; } global = new OGlobalPropertyImpl(name, type, id); ensurePropertiesSize(id); properties.set(id, global); propertiesByNameType.put(global.getName() + "|" + global.getType().name(), global); return global; }
public void checkEmbedded() { if (!(getDatabase().getStorage().getUnderlying() instanceof OAbstractPaginatedStorage)) throw new OSchemaException("'Internal' schema modification methods can be used only inside of embedded database"); }
@Override public ORecord fromStream(final byte[] iSource, final ORecord iRecord, final String[] iFields) { final long timer = PROFILER.startChrono(); try { return fromString(new String(iSource,"UTF-8"), iRecord, iFields); } catch (UnsupportedEncodingException e) { throw OException.wrapException(new OSchemaException("Error reading record"),e); } finally { PROFILER .stopChrono(PROFILER.getProcessMetric("serializer.record.string.fromStream"), "Deserialize record from stream", timer); } }
protected void dropPropertyInternal(ODatabaseDocumentInternal database, final String iPropertyName) { if (database.getTransaction().isActive()) throw new IllegalStateException("Cannot drop a property inside a transaction"); database.checkSecurity(ORule.ResourceGeneric.SCHEMA, ORole.PERMISSION_DELETE); acquireSchemaWriteLock(); try { checkEmbedded(); final OProperty prop = properties.remove(iPropertyName); if (prop == null) throw new OSchemaException("Property '" + iPropertyName + "' not found in class " + name + "'"); } finally { releaseSchemaWriteLock(); } }
protected void checkForDateFormat(final String iDateAsString) { if (iDateAsString != null) if (globalRef.getType() == OType.DATE) { try { getDatabase().getStorage().getConfiguration().getDateFormatInstance().parse(iDateAsString); } catch (ParseException e) { throw OException .wrapException(new OSchemaException("Invalid date format while formatting date '" + iDateAsString + "'"), e); } } else if (globalRef.getType() == OType.DATETIME) { try { getDatabase().getStorage().getConfiguration().getDateTimeFormatInstance().parse(iDateAsString); } catch (ParseException e) { throw OException .wrapException(new OSchemaException("Invalid datetime format while formatting date '" + iDateAsString + "'"), e); } } }
protected void addSuperClassInternal(ODatabaseDocumentInternal database, final OClass superClass) { acquireSchemaWriteLock(); try { final OClassImpl cls; if (superClass instanceof OClassAbstractDelegate) cls = (OClassImpl) ((OClassAbstractDelegate) superClass).delegate; else cls = (OClassImpl) superClass; if (cls != null) { // CHECK THE USER HAS UPDATE PRIVILEGE AGAINST EXTENDING CLASS final OSecurityUser user = database.getUser(); if (user != null) user.allow(ORule.ResourceGeneric.CLASS, cls.getName(), ORole.PERMISSION_UPDATE); if (superClasses.contains(superClass)) { throw new OSchemaException( "Class: '" + this.getName() + "' already has the class '" + superClass.getName() + "' as superclass"); } cls.addBaseClass(this); superClasses.add(cls); } } finally { releaseSchemaWriteLock(); } }
protected void checkParametersConflict(final OClass baseClass) { final Collection<OProperty> baseClassProperties = baseClass.properties(); for (OProperty property : baseClassProperties) { OProperty thisProperty = getProperty(property.getName()); if (thisProperty != null && !thisProperty.getType().equals(property.getType())) { throw new OSchemaException( "Cannot add base class '" + baseClass.getName() + "', because of property conflict: '" + thisProperty + "' vs '" + property + "'"); } } }
public OClass addClusterId(final int clusterId) { final ODatabaseDocumentInternal database = getDatabase(); database.checkSecurity(ORule.ResourceGeneric.SCHEMA, ORole.PERMISSION_UPDATE); if (isAbstract()) { throw new OSchemaException("Impossible to associate a cluster to an abstract class class"); } acquireSchemaWriteLock(); try { final OStorage storage = database.getStorage(); if (isDistributedCommand(database)) { final String cmd = String.format("alter class `%s` addcluster %d", name, clusterId); final OCommandSQL commandSQL = new OCommandSQL(cmd); commandSQL.addExcludedNode(((OAutoshardedStorage) storage).getNodeId()); database.command(commandSQL).execute(); addClusterIdInternal(database, clusterId); } else addClusterIdInternal(database, clusterId); } finally { releaseSchemaWriteLock(); } return this; }
public String getClusterName(final ORecord record) { if (ODatabaseRecordThreadLocal.instance().get().getStorage().isRemote()) // DON'T ASSIGN CLUSTER WITH REMOTE: SERVER KNOWS THE RIGHT CLUSTER BASED ON LOCALITY return null; int clusterId = record.getIdentity().getClusterId(); if (clusterId == ORID.CLUSTER_ID_INVALID) { // COMPUTE THE CLUSTER ID OClass schemaClass = null; if (record instanceof ODocument) schemaClass = ODocumentInternal.getImmutableSchemaClass(getDatabase(), (ODocument) record); if (schemaClass != null) { // FIND THE RIGHT CLUSTER AS CONFIGURED IN CLASS if (schemaClass.isAbstract()) throw new OSchemaException("Document belongs to abstract class '" + schemaClass.getName() + "' and cannot be saved"); clusterId = schemaClass.getClusterForNewInstance((ODocument) record); return database.getClusterNameById(clusterId); } else { return database.getClusterNameById(database.getStorage().getDefaultClusterId()); } } else { return database.getClusterNameById(clusterId); } }