1 /* Copyright (C) 2004 - 2009  Versant Inc.  http://www.db4o.com */
2 
3 using Db4objects.Db4o.Diagnostic;
4 using Db4objects.Db4o.Internal;
5 using Db4objects.Db4o.Internal.Delete;
6 using Db4objects.Db4o.Internal.Diagnostic;
7 using Db4objects.Db4o.Internal.Marshall;
8 using Db4objects.Db4o.Internal.Slots;
9 using Db4objects.Db4o.Reflect;
10 using Db4objects.Db4o.Typehandlers;
11 
12 namespace Db4objects.Db4o.Internal.Delete
13 {
14 	/// <exclude></exclude>
15 	public class DeleteContextImpl : ObjectHeaderContext, IDeleteContext, IObjectIdContext
16 	{
17 		private readonly IReflectClass _fieldClass;
18 
19 		private readonly Config4Field _fieldConfig;
20 
DeleteContextImpl(Db4objects.Db4o.Internal.StatefulBuffer buffer, ObjectHeader objectHeader, IReflectClass fieldClass, Config4Field fieldConfig)21 		public DeleteContextImpl(Db4objects.Db4o.Internal.StatefulBuffer buffer, ObjectHeader
22 			 objectHeader, IReflectClass fieldClass, Config4Field fieldConfig) : base(buffer
23 			.Transaction(), buffer, objectHeader)
24 		{
25 			_fieldClass = fieldClass;
26 			_fieldConfig = fieldConfig;
27 		}
28 
DeleteContextImpl(Db4objects.Db4o.Internal.Delete.DeleteContextImpl parentContext , IReflectClass fieldClass, Config4Field fieldConfig)29 		public DeleteContextImpl(Db4objects.Db4o.Internal.Delete.DeleteContextImpl parentContext
30 			, IReflectClass fieldClass, Config4Field fieldConfig) : this(parentContext.StatefulBuffer
31 			(), parentContext._objectHeader, fieldClass, fieldConfig)
32 		{
33 		}
34 
CascadeDeleteDepth(int depth)35 		public virtual void CascadeDeleteDepth(int depth)
36 		{
37 			StatefulBuffer().SetCascadeDeletes(depth);
38 		}
39 
StatefulBuffer()40 		private Db4objects.Db4o.Internal.StatefulBuffer StatefulBuffer()
41 		{
42 			return ((Db4objects.Db4o.Internal.StatefulBuffer)Buffer());
43 		}
44 
CascadeDeleteDepth()45 		public virtual int CascadeDeleteDepth()
46 		{
47 			return StatefulBuffer().CascadeDeletes();
48 		}
49 
CascadeDelete()50 		public virtual bool CascadeDelete()
51 		{
52 			return CascadeDeleteDepth() > 0;
53 		}
54 
DefragmentRecommended()55 		public virtual void DefragmentRecommended()
56 		{
57 			DiagnosticProcessor dp = Container()._handlers.DiagnosticProcessor();
58 			if (dp.Enabled())
59 			{
60 				dp.DefragmentRecommended(DefragmentRecommendation.DefragmentRecommendationReason.
61 					DeleteEmbeded);
62 			}
63 		}
64 
ReadSlot()65 		public virtual Slot ReadSlot()
66 		{
67 			return new Slot(Buffer().ReadInt(), Buffer().ReadInt());
68 		}
69 
Delete(ITypeHandler4 handler)70 		public virtual void Delete(ITypeHandler4 handler)
71 		{
72 			ITypeHandler4 correctHandlerVersion = HandlerRegistry.CorrectHandlerVersion(this,
73 				handler);
74 			int preservedCascadeDepth = CascadeDeleteDepth();
75 			CascadeDeleteDepth(AdjustedDepth());
76 			if (Handlers4.HandleAsObject(correctHandlerVersion))
77 			{
78 				DeleteObject();
79 			}
80 			else
81 			{
82 				correctHandlerVersion.Delete(this);
83 			}
84 			CascadeDeleteDepth(preservedCascadeDepth);
85 		}
86 
DeleteObject()87 		public virtual void DeleteObject()
88 		{
89 			int id = Buffer().ReadInt();
90 			if (CascadeDelete())
91 			{
92 				Container().DeleteByID(Transaction(), id, CascadeDeleteDepth());
93 			}
94 		}
95 
AdjustedDepth()96 		private int AdjustedDepth()
97 		{
98 			if (Platform4.IsStruct(_fieldClass))
99 			{
100 				return 1;
101 			}
102 			if (_fieldConfig == null)
103 			{
104 				return CascadeDeleteDepth();
105 			}
106 			if (_fieldConfig.CascadeOnDelete().DefiniteYes())
107 			{
108 				return 1;
109 			}
110 			if (_fieldConfig.CascadeOnDelete().DefiniteNo())
111 			{
112 				return 0;
113 			}
114 			return CascadeDeleteDepth();
115 		}
116 
ObjectId()117 		public virtual int ObjectId()
118 		{
119 			return StatefulBuffer().GetID();
120 		}
121 	}
122 }
123