/// <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); }
/// <summary> /// Creates a new DBObject of a given GraphType inserts its UUID into all indices of the given GraphType. /// </summary> /// <param name="myGraphDBType">The GraphType of the DBObject to create</param> /// <param name="myDBObjectAttributes">A dictionary of attributes for the new DBObject</param> /// <param name="mySpecialTypeAttributes">Special values which should be set to a object</param> /// <param name="myCheckUniqueness">check for unique constraints</param> /// <returns>The UUID of the new DBObject</returns> public Exceptional<DBObjectStream> CreateNewDBObjectStream(GraphDBType myGraphDBType, Dictionary<AttributeUUID, IObject> myDBObjectAttributes, Dictionary<String, IObject> myUndefAttributes, Dictionary<ASpecialTypeAttribute, Object> mySpecialTypeAttributes, SessionSettings mySessionSettings, Boolean myCheckUniqueness) { #region Input validation if (myGraphDBType == null) return new Exceptional<DBObjectStream>(new Error_ArgumentNullOrEmpty("The parameter myGraphType must not be null!")); if (myDBObjectAttributes == null) return new Exceptional<DBObjectStream>(new Error_ArgumentNullOrEmpty("The parameter myDBObjectAttributes must not be null!")); #endregion #region Check uniqueness if (myCheckUniqueness) { var parentTypes = _DBContext.DBTypeManager.GetAllParentTypes(myGraphDBType, true, false); var CheckVal = _DBContext.DBIndexManager.CheckUniqueConstraint(myGraphDBType, parentTypes, myDBObjectAttributes); if (CheckVal.Failed()) return new Exceptional<DBObjectStream>(CheckVal); } #endregion #region Create a new DBObjectStream DBObjectStream _NewDBObjectStream = null; ObjectUUID _NewObjectUUID = null; ASpecialTypeAttribute _SpecialTypeAttribute_UUID_Key = new SpecialTypeAttribute_UUID(); Object _SpecialTypeAttribute_UUID_Value = null; #region Search for an user-defined ObjectUUID if (mySpecialTypeAttributes != null) { if (mySpecialTypeAttributes.TryGetValue(_SpecialTypeAttribute_UUID_Key, out _SpecialTypeAttribute_UUID_Value)) { // User-defined ObjectUUID of type UInt64 var _ValueAsUInt64 = _SpecialTypeAttribute_UUID_Value as UInt64?; if (_ValueAsUInt64 != null) _NewObjectUUID = new ObjectUUID(_ValueAsUInt64.Value); // User-defined ObjectUUID of type String or anything else... else { var _String = _SpecialTypeAttribute_UUID_Value.ToString(); if (_String == null || _String == "") return new Exceptional<DBObjectStream>(new Error_InvalidAttributeValue(SpecialTypeAttribute_UUID.AttributeName, _String)); _NewObjectUUID = new ObjectUUID(_SpecialTypeAttribute_UUID_Value.ToString()); } mySpecialTypeAttributes[_SpecialTypeAttribute_UUID_Key] = _NewObjectUUID; } } #endregion // If _NewObjectUUID == null a new one will be generated! _NewDBObjectStream = new DBObjectStream(_NewObjectUUID, myGraphDBType, myDBObjectAttributes); _NewDBObjectStream.ObjectLocation = new ObjectLocation(_NewDBObjectStream.ObjectPath, _NewDBObjectStream.ObjectUUID.ToString()); #endregion #region Check for duplicate ObjectUUIDs... maybe resolve duplicates! var _DBObjectStreamAlreadyExistsResult = ObjectExistsOnFS(_NewDBObjectStream); if (_DBObjectStreamAlreadyExistsResult.Failed()) return new Exceptional<DBObjectStream>(_DBObjectStreamAlreadyExistsResult); while (_DBObjectStreamAlreadyExistsResult.Value == Trinary.TRUE) { if (_NewObjectUUID != null) return new Exceptional<DBObjectStream>(new Error_DBObjectCollision(_NewDBObjectStream)); _NewDBObjectStream = new DBObjectStream(ObjectUUID.NewUUID, myGraphDBType, myDBObjectAttributes); _NewDBObjectStream.ObjectLocation = new ObjectLocation(_NewDBObjectStream.ObjectPath, _NewDBObjectStream.ObjectUUID.ToString()); _DBObjectStreamAlreadyExistsResult = ObjectExistsOnFS(_NewDBObjectStream); if (_DBObjectStreamAlreadyExistsResult.Failed()) return new Exceptional<DBObjectStream>(_DBObjectStreamAlreadyExistsResult); } #endregion #region Check for ExtractSetting attributes unlike UUID if (mySpecialTypeAttributes != null && mySpecialTypeAttributes.Any()) { foreach (var _SpecialAttribute in mySpecialTypeAttributes) { // Skip SpecialTypeAttribute_UUID! if (!(_SpecialAttribute.Key is SpecialTypeAttribute_UUID)) { var result = _SpecialAttribute.Key.ApplyTo(_NewDBObjectStream, _SpecialAttribute.Value); if (result.Failed()) { return new Exceptional<DBObjectStream>(result); } } } } #endregion // Flush new DBObject var flushResult = FlushDBObject(_NewDBObjectStream); if (flushResult.Failed()) return new Exceptional<DBObjectStream>(flushResult); #region Check for existing object - might be removed at some time //var exists = ObjectExistsOnFS(_NewDBObjectStream); //if (exists.Failed()) // return new Exceptional<DBObjectStream>(exists); //if (exists.Value != Trinary.TRUE) //{ // return new Exceptional<DBObjectStream>(new Error_UnknownDBError("DBObject with path " + _NewDBObjectStream.ObjectLocation + " does not exist.")); //} #endregion #region Add UUID of the new DBObject to all indices of myGraphType foreach (var _GraphDBType in _DBContext.DBTypeManager.GetAllParentTypes(myGraphDBType, true, false)) { foreach (var _AAttributeIndex in _GraphDBType.GetAllAttributeIndices(false)) { //Find out if the dbobject carries all necessary attributes if (_NewDBObjectStream.HasAtLeastOneAttribute(_AAttributeIndex.IndexKeyDefinition.IndexKeyAttributeUUIDs, _GraphDBType, mySessionSettings)) { //valid dbo for idx _AAttributeIndex.Insert(_NewDBObjectStream, _GraphDBType, _DBContext); } } } #endregion #region add undefined attributes to the object if (!myUndefAttributes.IsNullOrEmpty()) { foreach (var item in myUndefAttributes) { var addExcept = _NewDBObjectStream.AddUndefinedAttribute(item.Key, item.Value, this); if (addExcept.Failed()) { return new Exceptional<DBObjectStream>(addExcept); } } } #endregion return new Exceptional<DBObjectStream>(_NewDBObjectStream); }
/// <summary> /// Create a readout based on the passed <paramref name="attributes"/>, <paramref name="undefinedAttributes"/>, <paramref name="specialTypeAttributes"/> which are all optional /// </summary> /// <param name="myDBObjectStreamExceptional"></param> /// <param name="attributes"></param> /// <param name="undefinedAttributes"></param> /// <param name="specialTypeAttributes"></param> /// <returns></returns> private Exceptional<Vertex> GetManipulationResultSet(DBContext myDBContext, Exceptional<DBObjectStream> myDBObjectStreamExceptional, GraphDBType myGraphDBType, Dictionary<TypeAndAttributeDefinition, IObject> attributes = null, Dictionary<String, IObject> undefinedAttributes = null, Dictionary<ASpecialTypeAttribute, Object> specialTypeAttributes = null) { Vertex _Vertex = null; #region Return inserted attributes #region attributes if (!attributes.IsNullOrEmpty()) { _Vertex = new Vertex(attributes.ToDictionary(key => key.Key.Definition.Name, value => value.Value.GetReadoutValue())); } else { _Vertex = new Vertex(); } #endregion #region UndefinedAttributes if (!undefinedAttributes.IsNullOrEmpty()) { foreach (var undefAttr in undefinedAttributes) { _Vertex.AddAttribute(undefAttr.Key, undefAttr.Value.GetReadoutValue()); } } #endregion #region SpecialTypeAttributes if (!specialTypeAttributes.IsNullOrEmpty()) { foreach (var specAttr in specialTypeAttributes) { _Vertex.AddAttribute(specAttr.Key.Name, specAttr.Value); } } #endregion #region UUID if (!_Vertex.HasAttribute(SpecialTypeAttribute_UUID.AttributeName)) { var extractedValue = new SpecialTypeAttribute_UUID().ExtractValue(myDBObjectStreamExceptional.Value, myGraphDBType, myDBContext); if (extractedValue.Failed()) { return new Exceptional<Vertex>(extractedValue); } _Vertex.AddAttribute(SpecialTypeAttribute_UUID.AttributeName, extractedValue.Value.GetReadoutValue()); } #endregion #region REVISION if (!_Vertex.HasAttribute(SpecialTypeAttribute_REVISION.AttributeName)) // If it was updated by SpecialTypeAttributes we do not need to add them again { var extractedValue = new SpecialTypeAttribute_REVISION().ExtractValue(myDBObjectStreamExceptional.Value, myGraphDBType, myDBContext); if (extractedValue.Failed()) { return new Exceptional<Vertex>(extractedValue); } _Vertex.AddAttribute(SpecialTypeAttribute_REVISION.AttributeName, extractedValue.Value.GetReadoutValue()); } #endregion #endregion return new Exceptional<Vertex>(_Vertex); }