public override Exceptional Execute(DBContext dbContext, GraphDBType graphDBType) { var listOfTypeAttributes = new Dictionary<TypeAttribute, GraphDBType>(); var retExcept = new Exceptional(); var existingTypeAttributes = graphDBType.GetAllAttributes(dbContext); foreach (var attr in _ListOfAttributes) { var createExcept = attr.CreateTypeAttribute(dbContext); if (!createExcept.Success()) { retExcept.PushIExceptional(createExcept); } if (existingTypeAttributes.Exists(item => item.Name == createExcept.Value.Name)) { retExcept.PushIExceptional(new Exceptional(new Error_AttributeAlreadyExists(createExcept.Value.Name))); } var attrType = dbContext.DBTypeManager.GetTypeByName(attr.AttributeType.Name); if (attrType == null) { retExcept.PushIExceptional(new Exceptional(new Error_TypeDoesNotExist(attr.AttributeType.Name))); return retExcept; } if (attrType.IsUserDefined) { retExcept.PushIExceptional(new Exceptional(new Error_InvalidReferenceAssignmentOfUndefAttr())); return retExcept; } createExcept.Value.DBTypeUUID = attrType.UUID; createExcept.Value.RelatedGraphDBTypeUUID = graphDBType.UUID; graphDBType.AddAttribute(createExcept.Value, dbContext.DBTypeManager, true); var flushExcept = dbContext.DBTypeManager.FlushType(graphDBType); if (!flushExcept.Success()) { retExcept.PushIExceptional(flushExcept); } listOfTypeAttributes.Add(createExcept.Value, attrType); } var dbobjects = dbContext.DBObjectCache.SelectDBObjectsForLevelKey(new LevelKey(graphDBType, dbContext.DBTypeManager), dbContext); foreach (var item in dbobjects) { if (!item.Success()) { retExcept.PushIExceptional(item); } else { var undefAttrExcept = item.Value.GetUndefinedAttributePayload(dbContext.DBObjectManager); if (!undefAttrExcept.Success()) { retExcept.PushIExceptional(undefAttrExcept); } foreach (var attr in listOfTypeAttributes) { IObject value; if (undefAttrExcept.Value.TryGetValue(attr.Key.Name, out value)) { var typeOfOperator = GraphDBTypeMapper.ConvertGraph2CSharp(attr.Value.Name); if (GraphDBTypeMapper.IsAValidAttributeType(attr.Value, typeOfOperator, dbContext, value)) { item.Value.AddAttribute(attr.Key.UUID, value); var removeExcept = item.Value.RemoveUndefinedAttribute(attr.Key.Name, dbContext.DBObjectManager); if (!removeExcept.Success()) { retExcept.PushIExceptional(removeExcept); } var flushExcept = dbContext.DBObjectManager.FlushDBObject(item.Value); if (!flushExcept.Success()) { retExcept.PushIExceptional(flushExcept); } } else { retExcept.PushIExceptional(new Exceptional(new Error_InvalidUndefAttrType(attr.Key.Name, attr.Value.Name))); } } } } } return Exceptional.OK; }
/// <summary> /// Adds an attribute with given name and type to the class with the given name /// </summary> /// <param name="targetClass">The class, we want to add the new attribute to.</param> /// <param name="myAttributeName">The name of the attribute.</param> /// <param name="attributeType">The type of the attribute.</param> /// <returns>Ture, if the attribute could be added to the target class. Else, false. (attribute contained in superclass)</returns> public Exceptional<ResultType> AddAttributeToType(GraphDBType mytype, TypeAttribute myTypeAttribute) { #region check if already initialized if (GetTypeByUUID(myTypeAttribute.DBTypeUUID) == null) { return new Exceptional<ResultType>(new Error_TypeDoesNotExist(myTypeAttribute.DBTypeUUID.ToString())); } #endregion #region Check if any ParentType already have an attribute with this name foreach (var aType in GetAllParentTypes(mytype, true, true)) { if (aType.GetTypeAttributeByName(myTypeAttribute.Name) != null) { return new Exceptional<ResultType>(new Error_AttributeExistsInSupertype(myTypeAttribute.Name, aType.Name)); } } #endregion #region adapt type //if we reach this code, no other superclass contains an attribute with this name, so add it! myTypeAttribute.RelatedGraphDBTypeUUID = mytype.UUID; if (myTypeAttribute.DefaultValue != null) { mytype.AddMandatoryAttribute(myTypeAttribute.UUID, this); } mytype.AddAttribute(myTypeAttribute, this, true); var FlushExcept = FlushType(mytype); if (FlushExcept.Failed()) return new Exceptional<ResultType>(FlushExcept); #endregion #region update lookup tables ob sub-classes foreach (var aSubType in GetAllSubtypes(mytype, false)) { aSubType.AttributeLookupTable.Add(myTypeAttribute.UUID, myTypeAttribute); } #endregion return new Exceptional<ResultType>(ResultType.Successful); }
/// <summary> /// Removes an attribute of the given type. /// </summary> /// <param name="aUserType">The target type.</param> /// <param name="attributeUUID">The attribute uuid, referencing the deprecated attribute.</param> /// <returns></returns> private Exceptional<ResultType> RemoveAttributeOfType(GraphDBType aUserType, AttributeUUID attributeUUID) { #region INPUT EXCEPTIONS if (aUserType == null) { return new Exceptional<ResultType>(new Error_ArgumentNullOrEmpty("aUserType")); } #endregion #region remove attribute from type TypeAttribute typeAttribute = aUserType.Attributes[attributeUUID]; if (typeAttribute != null) { aUserType.RemoveAttribute(typeAttribute.UUID); var FlushExcept = FlushType(aUserType); if (FlushExcept.Failed()) { aUserType.AddAttribute(typeAttribute, this, false); return new Exceptional<ResultType>(FlushExcept); } #region update lookup tables ob sub-classes foreach (var aSubType in GetAllSubtypes(aUserType).Where(aType => aType != aUserType)) { //delete from lookuptable aSubType.RemoveAttributeFromLookupTable(typeAttribute.UUID); } #endregion } else { return new Exceptional<ResultType>(new Error_AttributeIsNotDefined(aUserType.Name, typeAttribute.Name)); } #endregion return new Exceptional<ResultType>(ResultType.Successful); }
/// <summary> /// Initializes the type manager. This method may only be called once, else it throws an TypeInitializationException. /// </summary> /// <param name="myIGraphFSSession">The myIGraphFS, on which the database is stored.</param> /// <param name="myDatabaseLocation">The databases root path in the myIGraphFS.</param> public Exceptional Init(IGraphFSSession myIGraphFSSession, ObjectLocation myDatabaseLocation, Boolean myRebuildIndices) { #region Input validation if (myIGraphFSSession == null) return new Exceptional<bool>(new Error_ArgumentNullOrEmpty("The parameter myIGraphFS must not be null!")); if (myDatabaseLocation == null) return new Exceptional<bool>(new Error_ArgumentNullOrEmpty("The parameter myDatabaseLocation must not be null!")); //ToDo: Find a better way to check this! if (_BasicTypes.ContainsKey(DBBaseObject.UUID)) return new Exceptional<bool>(new Error_UnknownDBError("The TypeManager had already been initialized!")); #endregion var objectDirectoryShards = UInt16.Parse(_DBContext.GraphAppSettings.Get<ObjectsDirectoryShardsSetting>()); #region DBObject - The base of all database types // DBObject is a child of DBBaseObject var typeDBBaseObject = new GraphDBType(DBBaseObject.UUID, myDatabaseLocation, DBBaseObject.Name, null, new Dictionary<AttributeUUID, TypeAttribute>(), false, false, "The base of all database types", DBConstants.ObjectDirectoryShards); _SystemTypes.Add(typeDBBaseObject.UUID, typeDBBaseObject); #endregion #region DBReference - The base of all user defined database types // DBObject is a child of DBBaseObject // == DBObject! var typeDBReference = new GraphDBType(DBReference.UUID, myDatabaseLocation, DBReference.Name, DBBaseObject.UUID, new Dictionary<AttributeUUID, TypeAttribute>(), false, false, "The base of all user defined database types", DBConstants.ObjectDirectoryShards); #region DBVertex var typeDBVertex = new GraphDBType(DBVertex.UUID, myDatabaseLocation, DBVertex.Name, DBReference.UUID, new Dictionary<AttributeUUID, TypeAttribute>(), false, false, "The base of all user defined database vertices", DBConstants.ObjectDirectoryShards); _SystemTypes.Add(typeDBVertex.UUID, typeDBVertex); #endregion #region UUID special Attribute var specialTypeAttribute_UUID = new SpecialTypeAttribute_UUID() { DBTypeUUID = DBReference.UUID, RelatedGraphDBTypeUUID = typeDBReference.UUID, KindOfType = KindsOfType.SpecialAttribute }; typeDBReference.AddAttribute(specialTypeAttribute_UUID, this, false); _GUIDTypeAttribute = specialTypeAttribute_UUID; #endregion #region CreationTime special attribute var specialTypeAttribute_CrTime = new SpecialTypeAttribute_CREATIONTIME() { DBTypeUUID = DBUInt64.UUID, RelatedGraphDBTypeUUID = typeDBReference.UUID, KindOfType = KindsOfType.SpecialAttribute }; typeDBReference.AddAttribute(specialTypeAttribute_CrTime, this, false); #endregion #region DeletionTime special attribute var specialTypeAttribute_DelTime = new SpecialTypeAttribute_DELETIONTIME() { DBTypeUUID = DBUInt64.UUID, RelatedGraphDBTypeUUID = typeDBReference.UUID, KindOfType = KindsOfType.SpecialAttribute }; typeDBReference.AddAttribute(specialTypeAttribute_DelTime, this, false); #endregion #region Edition special attribute var specialTypeAttribute_Edition = new SpecialTypeAttribute_EDITION() { DBTypeUUID = DBString.UUID, RelatedGraphDBTypeUUID = typeDBReference.UUID, KindOfType = KindsOfType.SpecialAttribute }; typeDBReference.AddAttribute(specialTypeAttribute_Edition, this, false); #endregion #region Editions special attribute var specialTypeAttribute_Editions = new SpecialTypeAttribute_EDITIONS() { DBTypeUUID = DBString.UUID, RelatedGraphDBTypeUUID = typeDBReference.UUID, KindOfType = KindsOfType.SpecialAttribute }; typeDBReference.AddAttribute(specialTypeAttribute_Editions, this, false); #endregion #region LastAccessTime special attribute var specialTypeAttribute_AcTime = new SpecialTypeAttribute_LASTACCESSTIME() { DBTypeUUID = DBUInt64.UUID, RelatedGraphDBTypeUUID = typeDBReference.UUID, KindOfType = KindsOfType.SpecialAttribute }; typeDBReference.AddAttribute(specialTypeAttribute_AcTime, this, false); #endregion #region LastModificationTime special attribute var specialTypeAttribute_LastModTime = new SpecialTypeAttribute_LASTMODIFICATIONTIME() { DBTypeUUID = DBUInt64.UUID, RelatedGraphDBTypeUUID = typeDBReference.UUID, KindOfType = KindsOfType.SpecialAttribute }; typeDBReference.AddAttribute(specialTypeAttribute_LastModTime, this, false); #endregion #region TypeName special Attribute var specialTypeAttribute_TYPE = new SpecialTypeAttribute_TYPE() { DBTypeUUID = DBString.UUID, RelatedGraphDBTypeUUID = typeDBReference.UUID, KindOfType = KindsOfType.SpecialAttribute }; typeDBReference.AddAttribute(specialTypeAttribute_TYPE, this, false); #endregion #region REVISION special Attribute var specialTypeAttribute_REVISION = new SpecialTypeAttribute_REVISION() { DBTypeUUID = DBString.UUID, RelatedGraphDBTypeUUID = typeDBReference.UUID, KindOfType = KindsOfType.SpecialAttribute }; typeDBReference.AddAttribute(specialTypeAttribute_REVISION, this, false); #endregion #region REVISIONS special Attribute var specialTypeAttribute_REVISIONS = new SpecialTypeAttribute_REVISIONS() { DBTypeUUID = DBString.UUID, RelatedGraphDBTypeUUID = typeDBReference.UUID, KindOfType = KindsOfType.SpecialAttribute }; typeDBReference.AddAttribute(specialTypeAttribute_REVISIONS, this, false); #endregion #region STREAMS special Attribute var specialTypeAttribute_STREAMS = new SpecialTypeAttribute_STREAMS() { DBTypeUUID = DBString.UUID, RelatedGraphDBTypeUUID = typeDBReference.UUID, KindOfType = KindsOfType.SpecialAttribute }; typeDBReference.AddAttribute(specialTypeAttribute_STREAMS, this, false); #endregion #region NUMBER OF REVISIONS Attribute var specialTypeAttribute_NUMBEROFREVISIONS = new SpecialTypeAttribute_NUMBEROFREVISIONS() { DBTypeUUID = DBUInt64.UUID, RelatedGraphDBTypeUUID = typeDBReference.UUID, KindOfType = KindsOfType.SpecialAttribute }; typeDBReference.AddAttribute(specialTypeAttribute_NUMBEROFREVISIONS, this, false); #endregion #region NUMBER OF COPIES var specialTypeAttribute_NUMBEROFCOPIES = new SpecialTypeAttribute_NUMBEROFCOPIES() { DBTypeUUID = DBUInt64.UUID, RelatedGraphDBTypeUUID = typeDBReference.UUID, KindOfType = KindsOfType.SpecialAttribute }; typeDBReference.AddAttribute(specialTypeAttribute_NUMBEROFCOPIES, this, false); #endregion #region PARENT REVISION IDs var specialTypeAttribute_PARENTREVISIONIDs = new SpecialTypeAttribute_PARENTREVISIONS() { DBTypeUUID = DBString.UUID, RelatedGraphDBTypeUUID = typeDBReference.UUID, KindOfType = KindsOfType.SpecialAttribute }; typeDBReference.AddAttribute(specialTypeAttribute_PARENTREVISIONIDs, this, false); #endregion #region MAX REVISION AGE var specialTypeAttribute_MAXREVISIONAGE = new SpecialTypeAttribute_MAXREVISIONAGE() { DBTypeUUID = DBUInt64.UUID, RelatedGraphDBTypeUUID = typeDBReference.UUID, KindOfType = KindsOfType.SpecialAttribute }; typeDBReference.AddAttribute(specialTypeAttribute_MAXREVISIONAGE, this, false); #endregion #region MIN NUMBER OF REVISIONS var specialTypeAttribute_MINNUMBEROFREVISIONS = new SpecialTypeAttribute_MINNUMBEROFREVISIONS() { DBTypeUUID = DBUInt64.UUID, RelatedGraphDBTypeUUID = typeDBReference.UUID, KindOfType = KindsOfType.SpecialAttribute }; typeDBReference.AddAttribute(specialTypeAttribute_MINNUMBEROFREVISIONS, this, false); #endregion #region MAX NUMBER OF REVISIONS var specialTypeAttribute_MAXNUMBEROFREVISIONS = new SpecialTypeAttribute_MAXNUMBEROFREVISIONS() { DBTypeUUID = DBUInt64.UUID, RelatedGraphDBTypeUUID = typeDBReference.UUID, KindOfType = KindsOfType.SpecialAttribute }; typeDBReference.AddAttribute(specialTypeAttribute_MAXNUMBEROFREVISIONS, this, false); #endregion #region MAX NUMBER OF COPIES var specialTypeAttribute_MAXNUMBEROFCOPIES = new SpecialTypeAttribute_MAXNUMBEROFCOPIES() { DBTypeUUID = DBUInt64.UUID, RelatedGraphDBTypeUUID = typeDBReference.UUID, KindOfType = KindsOfType.SpecialAttribute }; typeDBReference.AddAttribute(specialTypeAttribute_MAXNUMBEROFCOPIES, this, false); #endregion #region MIN NUMBER OF COPIES var specialTypeAttribute_MINNUMBEROFCOPIES = new SpecialTypeAttribute_MINNUMBEROFCOPIES() { DBTypeUUID = DBUInt64.UUID, RelatedGraphDBTypeUUID = typeDBReference.UUID, KindOfType = KindsOfType.SpecialAttribute }; typeDBReference.AddAttribute(specialTypeAttribute_MINNUMBEROFCOPIES, this, false); #endregion foreach (var attr in typeDBReference.Attributes) { typeDBVertex.AddAttribute(attr.Value, this, false); } _SystemTypes.Add(typeDBReference.UUID, typeDBReference); #endregion #region DBEdge var typeDBEdge = new GraphDBType(new TypeUUID(DBConstants.DBEdgeID), myDatabaseLocation, DBConstants.DBEdgeName, DBReference.UUID, new Dictionary<AttributeUUID, TypeAttribute>(), false, false, "The base of all user defined database edges", DBConstants.ObjectDirectoryShards); _SystemTypes.Add(typeDBEdge.UUID, typeDBEdge); #endregion #region Build-in basic database types // These are children of DBBaseObject _BasicTypes.Add(DBBoolean.UUID, new GraphDBType(DBBoolean.UUID, new ObjectLocation(myDatabaseLocation), DBBoolean.Name, DBBaseObject.UUID, new Dictionary<AttributeUUID, TypeAttribute>(), false, false, "", DBConstants.ObjectDirectoryShards)); _BasicTypes.Add(DBDateTime.UUID, new GraphDBType(DBDateTime.UUID, new ObjectLocation(myDatabaseLocation), DBDateTime.Name, DBBaseObject.UUID, new Dictionary<AttributeUUID, TypeAttribute>(), false, false, "", DBConstants.ObjectDirectoryShards)); _BasicTypes.Add(DBDouble.UUID, new GraphDBType(DBDouble.UUID, new ObjectLocation(myDatabaseLocation), DBDouble.Name, DBBaseObject.UUID, new Dictionary<AttributeUUID, TypeAttribute>(), false, false, "", DBConstants.ObjectDirectoryShards)); _BasicTypes.Add(DBInt64.UUID, new GraphDBType(DBInt64.UUID, new ObjectLocation(myDatabaseLocation), DBInt64.Name, DBBaseObject.UUID, new Dictionary<AttributeUUID, TypeAttribute>(), false, false, "", DBConstants.ObjectDirectoryShards)); _BasicTypes.Add(DBInt32.UUID, new GraphDBType(DBInt32.UUID, new ObjectLocation(myDatabaseLocation), DBInt32.Name, DBBaseObject.UUID, new Dictionary<AttributeUUID, TypeAttribute>(), false, false, "", DBConstants.ObjectDirectoryShards)); _BasicTypes.Add(DBUInt64.UUID, new GraphDBType(DBUInt64.UUID, new ObjectLocation(myDatabaseLocation), DBUInt64.Name, DBBaseObject.UUID, new Dictionary<AttributeUUID, TypeAttribute>(), false, false, "", DBConstants.ObjectDirectoryShards)); _BasicTypes.Add(DBString.UUID, new GraphDBType(DBString.UUID, new ObjectLocation(myDatabaseLocation), DBString.Name, DBBaseObject.UUID, new Dictionary<AttributeUUID, TypeAttribute>(), false, false, "", DBConstants.ObjectDirectoryShards)); _BasicTypes.Add(DBBackwardEdgeType.UUID, new GraphDBType(DBBackwardEdgeType.UUID, new ObjectLocation(myDatabaseLocation), DBBackwardEdgeType.Name, DBBaseObject.UUID, new Dictionary<AttributeUUID, TypeAttribute>(), false, false, "", DBConstants.ObjectDirectoryShards)); #endregion foreach (var _GraphDBType in _SystemTypes.Values) _TypesNameLookUpTable.Add(_GraphDBType.Name, _GraphDBType); foreach (var _GraphDBType in _BasicTypes.Values) _TypesNameLookUpTable.Add(_GraphDBType.Name, _GraphDBType); return LoadUserDefinedDatabaseTypes(myRebuildIndices); }