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 void setSuperClassesInternal(final List<? extends OClass> classes) { List<OClassImpl> newSuperClasses = new ArrayList<OClassImpl>(); OClassImpl cls; for (OClass superClass : classes) { if (superClass instanceof OClassAbstractDelegate) cls = (OClassImpl) ((OClassAbstractDelegate) superClass).delegate; else cls = (OClassImpl) superClass; if (newSuperClasses.contains(cls)) { throw new OSchemaException("Duplicated superclass '" + cls.getName() + "'"); } newSuperClasses.add(cls); } List<OClassImpl> toAddList = new ArrayList<OClassImpl>(newSuperClasses); toAddList.removeAll(superClasses); List<OClassImpl> toRemoveList = new ArrayList<OClassImpl>(superClasses); toRemoveList.removeAll(newSuperClasses); for (OClassImpl toRemove : toRemoveList) { toRemove.removeBaseClassInternal(this); } for (OClassImpl addTo : toAddList) { addTo.addBaseClass(this); } superClasses.clear(); superClasses.addAll(newSuperClasses); }
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"); }
public void checkPersistentPropertyType(final ODatabaseInternal<ORecord> database, final String propertyName, final OType type) { if (OType.ANY.equals(type)) { return; } final boolean strictSQL = database.getStorage().getConfiguration().isStrictSql(); final StringBuilder builder = new StringBuilder(256); builder.append("select count(*) as count from "); builder.append(getEscapedName(name, strictSQL)); builder.append(" where "); builder.append(getEscapedName(propertyName, strictSQL)); builder.append(".type() not in ["); final Iterator<OType> cur = type.getCastable().iterator(); while (cur.hasNext()) { builder.append('"').append(cur.next().name()).append('"'); if (cur.hasNext()) builder.append(","); } builder.append("] and ").append(getEscapedName(propertyName, strictSQL)).append(" is not null "); if (type.isMultiValue()) builder.append(" and ").append(getEscapedName(propertyName, strictSQL)).append(".size() <> 0 limit 1"); try (final OResultSet res = database.command(builder.toString())) { if (((Long) res.next().getProperty("count")) > 0) throw new OSchemaException("The database contains some schema-less data in the property '" + name + "." + propertyName + "' that is not compatible with the type " + type + ". Fix those records and change the schema again"); } }
@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(); } }
public 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."); throw new OSchemaException("Property name is null or empty"); throw new OSchemaException("Cannot create property '" + propertyName + "' inside a transaction");
throw new OSchemaException("Class '" + className + "' already exists in current database"); throw OException.wrapException(new OSchemaException("Cannot create class '" + className + "'"), e); } finally { releaseSchemaWriteLock(database);
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); } } }