/// <summary> /// Removes a DBObject. /// </summary> /// <param name="myGraphType">The Type of the DBObject that is to be removed.</param> /// <param name="myDBObject">The UUID of the DBObject.</param> public Exceptional RemoveDBObject(GraphDBType myTypeOfDBObject, DBObjectStream myDBObject, DBObjectCache myDBObjectCache, SessionSettings myToken) { #region Input exceptions if (myTypeOfDBObject == null) { return new Exceptional(new Error_ArgumentNullOrEmpty("myTypeOfDBObject")); } if (myDBObject == null) { return new Exceptional(new Error_ArgumentNullOrEmpty("myUUID")); } #endregion #region Data ObjectLocation myDBObjectLocation; #endregion // Get DBObject path myDBObjectLocation = myDBObject.ObjectLocation; #region remove from attributeIDX foreach (var anIndex in myTypeOfDBObject.GetAllAttributeIndices(false)) { anIndex.Remove(myDBObject, myTypeOfDBObject, _DBContext); } #endregion #region remove from fs #region Remove DBOBJECTSTREAM var _RemoveObjectExceptional = _IGraphFSSession.RemoveObjectIfExists(myDBObjectLocation, DBConstants.DBOBJECTSTREAM); if (_RemoveObjectExceptional.Failed()) { return _RemoveObjectExceptional; } #endregion #region Remove DBBACKWARDEDGESTREAM _RemoveObjectExceptional = _IGraphFSSession.RemoveObjectIfExists(myDBObjectLocation, DBConstants.DBBACKWARDEDGESTREAM); if (_RemoveObjectExceptional.Failed()) { return _RemoveObjectExceptional; } #endregion #region Remove UNDEFATTRIBUTESSTREAM _RemoveObjectExceptional = _IGraphFSSession.RemoveObjectIfExists(myDBObjectLocation, DBConstants.UNDEFATTRIBUTESSTREAM); if (_RemoveObjectExceptional.Failed()) { return _RemoveObjectExceptional; } #endregion #endregion return Exceptional.OK; }
/// <summary> /// This is the update method which will change some <paramref name="myDBObjects"/> an behalf of the <paramref name="myListOfUpdates"/> /// </summary> /// <param name="myDBObjects">Some dbobjects</param> /// <param name="myListOfUpdates">The list of update tasks (assign, delete, etc)</param> /// <param name="dbObjectCache"></param> /// <returns></returns> public QueryResult Update(IEnumerable<Exceptional<DBObjectStream>> myDBObjects, IEnumerable<AAttributeAssignOrUpdateOrRemove> myListOfUpdates, DBContext myDBContext, GraphDBType myGraphDBType) { #region Data var queryResultContent = new List<Vertex>(); var queryResult = new QueryResult(); Warning_UndefinedAttribute undefAttrWarning = null; #endregion #region check for undefined attributes setting var undefAttrSetting = myDBContext.DBSettingsManager.GetSetting(SettingUndefAttrBehaviour.UUID, myDBContext, TypesSettingScope.DB); if (!undefAttrSetting.Success()) { return new QueryResult(undefAttrSetting); } var undefSettingVal = ((SettingUndefAttrBehaviour)undefAttrSetting.Value).Behaviour; #endregion #region Validate attributes foreach (var updateOrAssign in myListOfUpdates) { System.Diagnostics.Debug.Assert(updateOrAssign != null); if (updateOrAssign is AAttributeAssignOrUpdateOrRemove && !(updateOrAssign is AttributeRemove)) { var result = (updateOrAssign as AAttributeAssignOrUpdateOrRemove).AttributeIDChain.Validate(myDBContext, true); if (updateOrAssign.IsUndefinedAttributeAssign) { switch (undefSettingVal) { case UndefAttributeBehaviour.disallow: return new QueryResult(new Error_UndefinedAttributes()); case UndefAttributeBehaviour.warn: if (undefAttrWarning == null) { undefAttrWarning = new Warning_UndefinedAttribute(); } queryResult.PushIWarning(undefAttrWarning); break; } } if (result.Failed()) { return new QueryResult(result); } } } //var undefinedAttributeAssignments = myListOfUpdates.Where(a => a.IsUndefinedAttributeAssign); var definedAttributeAssignments = myListOfUpdates.Where(a => !a.IsUndefinedAttributeAssign); #endregion #region check unique constraint - refactor!! if (definedAttributeAssignments.CountIsGreater(0)) { Exceptional<Boolean> CheckConstraint = null; IEnumerable<GraphDBType> parentTypes = myDBContext.DBTypeManager.GetAllParentTypes(myGraphDBType, true, false); Dictionary<AttributeUUID, IObject> ChkForUnique = new Dictionary<AttributeUUID, IObject>(); foreach (var entry in myDBObjects) { //all attributes, that are going to be changed var attrsToCheck = entry.Value.GetAttributes().Where(item => definedAttributeAssignments.Select(updAttrs => GetAttributesToCheckForUnique(updAttrs)).Contains(item.Key)); foreach (var attrValue in attrsToCheck) { if (!ChkForUnique.ContainsKey(attrValue.Key)) ChkForUnique.Add(attrValue.Key, attrValue.Value); } CheckConstraint = myDBContext.DBIndexManager.CheckUniqueConstraint(myGraphDBType, parentTypes, ChkForUnique); ChkForUnique.Clear(); if (CheckConstraint.Failed()) return new QueryResult(CheckConstraint.IErrors); } } #endregion #region regular update foreach (var aDBO in myDBObjects) { //key: attribute name //value: TypeAttribute, NewValue Dictionary<String, Tuple<TypeAttribute, IObject>> attrsForResult = new Dictionary<String, Tuple<TypeAttribute, IObject>>(); if (aDBO.Failed()) { return new QueryResult(aDBO); } #region data Boolean sthChanged = false; #endregion #region iterate tasks //Exceptional<Boolean> partialResult; foreach (var attributeUpdateOrAssign in myListOfUpdates) { var updateResult = attributeUpdateOrAssign.Update(myDBContext, aDBO.Value, myGraphDBType); if (updateResult.Failed()) { return new QueryResult(updateResult); } if (updateResult.Value.Count > 0) { sthChanged = true; attrsForResult.AddRange(updateResult.Value); } } #endregion if (sthChanged) { var definedAttributes = ExtractDefinedAttributes(attrsForResult, myDBContext.DBTypeManager); #region update Idx foreach (var _AttributeIndex in myGraphDBType.GetAllAttributeIndices()) { if(definedAttributes.Exists(item => _AttributeIndex.IndexKeyDefinition.IndexKeyAttributeUUIDs.Contains(item.Key.Definition.UUID))) { //execute update _AttributeIndex.Update(aDBO.Value, myGraphDBType, myDBContext); } } #endregion #region update dbobjects on fs var flushResult = myDBContext.DBObjectManager.FlushDBObject(aDBO.Value); if (!flushResult.Success()) { return new QueryResult(flushResult); } #endregion var resultSet = GetManipulationResultSet(myDBContext, aDBO, myGraphDBType, undefinedAttributes: ExtractUndefindedAttributes(attrsForResult), attributes: definedAttributes); if (!resultSet.Success()) { /* what should happen now this should not break the update */ } queryResultContent.Add(resultSet.Value); } } #endregion #region Return all affected dbObjectUUIDs queryResult.Vertices = queryResultContent; return queryResult; #endregion }
/// <summary> /// Removes GraphTypes from filesystem /// </summary> /// <param name="typeDir">The directory of the GraphType that is going to be deleted.</param> /// <returns>True for success or otherwise false.</returns> private Exceptional<Boolean> RemoveTypeFromFs(GraphDBType toBeDeletedType) { using (var _Transaction = _IGraphFSSession.BeginFSTransaction()) { #region remove Objects directory foreach (var aObjectDirectoryShard in GetObjectDirectoryShards(toBeDeletedType)) { #region remove every DBObjectStream var shardsDir = toBeDeletedType.ObjectLocation + DBConstants.DBObjectsLocation + aObjectDirectoryShard; foreach (var aDBObject in _IGraphFSSession.GetDirectoryListing(shardsDir, kv => kv.Value.ObjectStreamsList.Contains(DBConstants.DBOBJECTSTREAM)).Value) { //make shure that BackwardEdgeStream and UndefinedAttributeStream are also deleted var removeIdxShardExcept = _IGraphFSSession.RemoveFSObject(shardsDir + aDBObject, DBConstants.DBOBJECTSTREAM, null, null); if (removeIdxShardExcept.Failed()) { return new Exceptional<bool>(removeIdxShardExcept); // return and rollback transaction } } #endregion #region remove the shard directory var removeObjectShardExcept = _IGraphFSSession.RemoveDirectoryObject(shardsDir, true); if (removeObjectShardExcept.Failed()) { return new Exceptional<bool>(removeObjectShardExcept); // return and rollback transaction } #endregion } #region remove the Objects directory var removeObjectsDirExcept = _IGraphFSSession.RemoveDirectoryObject(toBeDeletedType.ObjectLocation + DBConstants.DBObjectsLocation, true); if (removeObjectsDirExcept.Failed()) { return new Exceptional<bool>(removeObjectsDirExcept); // return and rollback transaction } #endregion #endregion #region remove Indices directory foreach (var aAttributeIDX in toBeDeletedType.GetAllAttributeIndices(false)) { #region remove the shards var shards = _IGraphFSSession.GetDirectoryListing(aAttributeIDX.FileSystemLocation, kv => kv.Value.ObjectStreamsList.Contains(DBConstants.DBINDEXSTREAM)); foreach (var aIdxShard in shards.Value) { var removeIdxShardExcept = _IGraphFSSession.RemoveFSObject(aAttributeIDX.FileSystemLocation + aIdxShard, DBConstants.DBINDEXSTREAM, null, null); if (removeIdxShardExcept.Failed()) { return new Exceptional<bool>(removeIdxShardExcept); // return and rollback transaction } } #endregion #region remove the idx directory itself var removeIDXDirExcept = _IGraphFSSession.RemoveDirectoryObject(aAttributeIDX.FileSystemLocation, true); if (removeIDXDirExcept.Failed()) { return new Exceptional<bool>(removeIDXDirExcept); // return and rollback transaction } #endregion } #endregion #region remove the type var removeObjectExcept = _IGraphFSSession.RemoveFSObject(toBeDeletedType.ObjectLocation, DBConstants.DBTYPESTREAM, null, null); if (removeObjectExcept.Failed()) { return new Exceptional<bool>(removeObjectExcept); // return and rollback transaction } #endregion var removeDirExcept = _IGraphFSSession.RemoveDirectoryObject(toBeDeletedType.ObjectLocation, true); if (removeDirExcept.Failed()) { return new Exceptional<bool>(removeDirExcept); // return and rollback transaction } _Transaction.Commit(); } return new Exceptional<bool>(true); }
public Exceptional Truncate(DBContext myDBContext, GraphDBType myGraphDBType) { #region Remove #region remove dbobjects var listOfAffectedDBObjects = myDBContext.DBObjectCache.SelectDBObjectsForLevelKey(new LevelKey(myGraphDBType, myDBContext.DBTypeManager), myDBContext).ToList(); foreach (var aDBO in listOfAffectedDBObjects) { if (!aDBO.Success()) { return new Exceptional(aDBO); } var dbObjID = aDBO.Value.ObjectUUID; var dbBackwardEdgeLoadExcept = myDBContext.DBObjectManager.LoadBackwardEdge(aDBO.Value.ObjectLocation); if (!dbBackwardEdgeLoadExcept.Success()) return new Exceptional(dbBackwardEdgeLoadExcept); var dbBackwardEdges = dbBackwardEdgeLoadExcept.Value; var result = myDBContext.DBObjectManager.RemoveDBObject(myGraphDBType, aDBO.Value, myDBContext.DBObjectCache, myDBContext.SessionSettings); if (!result.Success()) { return new Exceptional(result); } if (dbBackwardEdges != null) { var deleteReferences = DeleteObjectReferences(dbObjID, dbBackwardEdges, myDBContext); if (!deleteReferences.Success()) return new Exceptional(deleteReferences); } } #endregion #region remove indices Parallel.ForEach(myGraphDBType.GetAllAttributeIndices(false), aIdx => { //it is not necessary to clear the UUID IDX because this is done by deleting the objects in the fs aIdx.ClearAndRemoveFromDisc(myDBContext.DBIndexManager); }); #endregion #endregion return Exceptional.OK; }
public override Exceptional Execute(DBContext dbContext, GraphDBType graphDBType) { var listOfTypeAttrs = new List<TypeAttribute>(); var retExcept = new Exceptional(); foreach (var attr in _ListOfAttributes) { var typeAttr = graphDBType.GetTypeAttributeByName(attr); if (typeAttr == null) { return new Exceptional(new Error_AttributeIsNotDefined(attr)); } var attrType = dbContext.DBTypeManager.GetTypeByUUID(typeAttr.DBTypeUUID); if (attrType == null) return new Exceptional(new Error_TypeDoesNotExist("")); if (attrType.IsUserDefined) return new Exceptional(new Error_InvalidReferenceAssignmentOfUndefAttr()); listOfTypeAttrs.Add(typeAttr); } var dbobjects = dbContext.DBObjectCache.SelectDBObjectsForLevelKey(new LevelKey(graphDBType, dbContext.DBTypeManager), dbContext); foreach (var item in dbobjects) { if (!item.Success()) { retExcept.PushIExceptional(item); } else { foreach (var attr in listOfTypeAttrs) { if (item.Value.HasAttribute(attr.UUID, graphDBType)) { var attrVal = item.Value.GetAttribute(attr.UUID); var addExcept = item.Value.AddUndefinedAttribute(attr.Name, attrVal, dbContext.DBObjectManager); if (!addExcept.Success()) retExcept.PushIExceptional(addExcept); item.Value.RemoveAttribute(attr.UUID); var saveExcept = dbContext.DBObjectManager.FlushDBObject(item.Value); if (!saveExcept.Success()) retExcept.PushIExceptional(saveExcept); } } } } var indices = graphDBType.GetAllAttributeIndices(); List<AAttributeIndex> idxToDelete = new List<AAttributeIndex>(); //remove attributes from type foreach (var attr in listOfTypeAttrs) { foreach (var item in indices) { var index = item.IndexKeyDefinition.IndexKeyAttributeUUIDs.Find(idx => idx == attr.UUID); if (index != null && item.IndexKeyDefinition.IndexKeyAttributeUUIDs.Count == 1) { idxToDelete.Add(item); } } //remove indices foreach (var idx in idxToDelete) { var remExcept = graphDBType.RemoveIndex(idx.IndexName, idx.IndexEdition, dbContext.DBTypeManager); if (!remExcept.Success()) { retExcept.PushIExceptional(remExcept); } } idxToDelete.Clear(); graphDBType.RemoveAttribute(attr.UUID); var flushExcept = dbContext.DBTypeManager.FlushType(graphDBType); if (!flushExcept.Success()) { retExcept.PushIExceptional(flushExcept); } } return retExcept; }
private String CreateGraphDDL(DumpFormats myDumpFormat, GraphDBType myGraphDBType, DBContext myDBContext) { var stringBuilder = new StringBuilder(); stringBuilder.AppendFormat("{0} ", myGraphDBType.Name); if (myGraphDBType.ParentTypeUUID != null) { stringBuilder.AppendFormat("{0} {1} ", S_EXTENDS.ToUpperString(), myGraphDBType.GetParentType(myDBContext.DBTypeManager).Name);//builder.AppendLine(); #region Not backwardEdge attributes if (myGraphDBType.GetFilteredAttributes(ta => !ta.IsBackwardEdge).CountIsGreater(0)) { stringBuilder.Append(S_ATTRIBUTES.ToUpperString() + S_BRACKET_LEFT.ToUpperString() + CreateGraphDDLOfAttributes(myDumpFormat, myGraphDBType.GetFilteredAttributes(ta => !ta.IsBackwardEdge), myDBContext) + S_BRACKET_RIGHT.ToUpperString() + " "); } #endregion #region BackwardEdge attributes if (myGraphDBType.GetFilteredAttributes(ta => ta.IsBackwardEdge).CountIsGreater(0)) { stringBuilder.Append(S_BACKWARDEDGES.ToUpperString() + S_BRACKET_LEFT.ToUpperString() + CreateGraphDDLOfBackwardEdges(myDumpFormat, myGraphDBType.GetFilteredAttributes(ta => ta.IsBackwardEdge), myDBContext) + S_BRACKET_RIGHT.ToUpperString() + " "); } #endregion #region Uniques if (myGraphDBType.GetUniqueAttributes().CountIsGreater(0)) { stringBuilder.Append(S_UNIQUE.ToUpperString() + S_BRACKET_LEFT.Symbol + CreateGraphDDLOfAttributeUUIDs(myDumpFormat, myGraphDBType.GetUniqueAttributes(), myGraphDBType) + S_BRACKET_RIGHT.Symbol + " "); } #endregion #region Mandatory attributes if (myGraphDBType.GetMandatoryAttributes().CountIsGreater(0)) { stringBuilder.Append(S_MANDATORY.ToUpperString() + S_BRACKET_LEFT.Symbol + CreateGraphDDLOfAttributeUUIDs(myDumpFormat, myGraphDBType.GetMandatoryAttributes(), myGraphDBType) + S_BRACKET_RIGHT.Symbol + " "); } #endregion #region Indices if (myGraphDBType.GetAllAttributeIndices(false).CountIsGreater(0)) { stringBuilder.Append(S_INDICES.ToUpperString() + S_BRACKET_LEFT.Symbol + CreateGraphDDLOfIndices(myDumpFormat, myGraphDBType.GetAllAttributeIndices(false), myGraphDBType) + S_BRACKET_RIGHT.Symbol + " "); } #endregion } return stringBuilder.ToString(); }
/// <summary> /// output for the type attributes /// </summary> /// <param name="myGraphDBType">The db type</param> /// <param name="myDBContext">The db context</param> /// <returns>a list of readouts, contains the attributes</returns> private IEnumerable<IVertex> GenerateIndicesOutput(GraphDBType myGraphDBType, DBContext myDBContext) { var _AttributeReadout = new List<Vertex>(); foreach (var idx in myGraphDBType.GetAllAttributeIndices(myDBContext, false)) { var Attributes = new Dictionary<String, Object>(); Attributes.Add("UUID", idx.ObjectUUID); Attributes.Add("TYPE", idx.IndexType); Attributes.Add("Name", idx.IndexName); Attributes.Add("Edition", idx.IndexEdition); _AttributeReadout.Add(new Vertex(Attributes)); } return _AttributeReadout; }
/// <summary> /// Removes GraphTypes from filesystem /// </summary> /// <param name="typeDir">The directory of the GraphType that is going to be deleted.</param> /// <returns>True for success or otherwise false.</returns> private Exceptional<Boolean> RemoveTypeFromFs(DBContext myDBContext, GraphDBType toBeDeletedType) { using (var _Transaction = _IGraphFSSession.BeginFSTransaction()) { #region remove the Objects directory var removeObjectsDirExcept = _IGraphFSSession.RemoveDirectoryObject(toBeDeletedType.ObjectLocation + DBConstants.DBObjectsLocation, true); if (removeObjectsDirExcept.Failed()) { return new Exceptional<bool>(removeObjectsDirExcept); // return and rollback transaction } #endregion #region remove Indices directory foreach (var aAttributeIDX in toBeDeletedType.GetAllAttributeIndices(myDBContext, false)) { aAttributeIDX.ClearAndRemoveFromDisc(_DBContext); } #endregion #region remove the type var removeObjectExcept = _IGraphFSSession.RemoveFSObject(toBeDeletedType.ObjectLocation, DBConstants.DBTYPESTREAM, null, null); if (removeObjectExcept.Failed()) { return new Exceptional<bool>(removeObjectExcept); // return and rollback transaction } #endregion var removeDirExcept = _IGraphFSSession.RemoveDirectoryObject(toBeDeletedType.ObjectLocation, true); if (removeDirExcept.Failed()) { return new Exceptional<bool>(removeDirExcept); // return and rollback transaction } _Transaction.Commit(); } return new Exceptional<bool>(true); }