-
Notifications
You must be signed in to change notification settings - Fork 29
/
LockOps.Logical.cs
159 lines (135 loc) · 6.61 KB
/
LockOps.Logical.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*<FILE_LICENSE>
* Azos (A to Z Application Operating System) Framework
* The A to Z Foundation (a.k.a. Azist) licenses this file to you under the MIT license.
* See the LICENSE file in the project root for more information.
</FILE_LICENSE>*/
using System;
using Azos.Sky.Locking.Server;
namespace Azos.Sky.Locking
{
/// <summary>
/// Represents operations of locking transaction
/// </summary>
public static partial class LockOp
{
public static AndOp And(OperatorOp lop, OperatorOp rop) { return new AndOp(lop, rop);}
[Serializable]
public sealed class AndOp : BinaryOperatorOp
{
internal AndOp(OperatorOp lop, OperatorOp rop): base(lop, rop){}
public override bool GetValue(EvalContext ctx)
{ //common code not refactored to parent class for speed (less virt calls)
var lv = LeftOperand.GetValue(ctx);
if (ctx.Aborted) return false;
var rv = RightOperand.GetValue(ctx);
if (ctx.Aborted) return false;
return lv && rv;
}
}
public static OrOp Or(OperatorOp lop, OperatorOp rop) { return new OrOp(lop, rop);}
[Serializable]
public sealed class OrOp : BinaryOperatorOp
{
internal OrOp(OperatorOp lop, OperatorOp rop) : base(lop, rop){}
public override bool GetValue(EvalContext ctx)
{ //common code not refactored to parent class for speed (less virt calls)
var lv = LeftOperand.GetValue(ctx);
if (ctx.Aborted) return false;
var rv = RightOperand.GetValue(ctx);
if (ctx.Aborted) return false;
return lv || rv;
}
}
public static XorOp Xor(OperatorOp lop, OperatorOp rop) { return new XorOp(lop, rop);}
[Serializable]
public sealed class XorOp : BinaryOperatorOp
{
internal XorOp(OperatorOp lop, OperatorOp rop) : base(lop, rop){}
public override bool GetValue(EvalContext ctx)
{ //common code not refactored to parent class for speed (less virt calls)
var lv = LeftOperand.GetValue(ctx);
if (ctx.Aborted) return false;
var rv = RightOperand.GetValue(ctx);
if (ctx.Aborted) return false;
return lv ^ rv;
}
}
public static NotOp Not(OperatorOp operand) { return new NotOp(operand);}
/// <summary>
/// Reverses the boolean value of the operand
/// </summary>
public sealed class NotOp : UnaryOperatorOp
{
internal NotOp(OperatorOp operand)
{
if (operand==null)
throw new LockingException(ServerStringConsts.ARGUMENT_ERROR+"Not(operand=null)");
Operand = operand;
}
public readonly OperatorOp Operand;
public override bool GetValue(EvalContext ctx)
{
var v = Operand.GetValue(ctx);
if (ctx.Aborted) return false;
return !v;
}
public override void Prepare(EvalContext ctx, string path)
{
base.Prepare(ctx, path);
Operand.Prepare(ctx, m_Path);
}
}
public static ExistsOp Exists(string table, string var, object value = null, bool ignoreThisSession=true)
{
return new ExistsOp(table, var, value, ignoreThisSession);
}
/// <summary>
/// Returns true if the named variable exists.
/// </summary>
[Serializable]
public sealed class ExistsOp : UnaryOperatorOp
{
internal ExistsOp(string table, string var, object value, bool ignoreThisSession)
{
if (table.IsNullOrWhiteSpace() || var.IsNullOrWhiteSpace())
throw new LockingException(ServerStringConsts.ARGUMENT_ERROR+"Exists(table|var=null|empty)");
Table = table;
Var = var;
IgnoreThisSession = ignoreThisSession;
}
public readonly string Table; [NonSerialized]internal Server.Table _Table;
public readonly string Var;
public readonly object Value;
public readonly bool IgnoreThisSession;
public override void Prepare(EvalContext ctx, string path)
{
base.Prepare(ctx, path);
_Table = ctx.GetExistingOrMakeTableByName(Table);
}
public override bool GetValue(EvalContext ctx)
{
return _Table.Exists(ctx, Var, Value, IgnoreThisSession);
}
}
public static TrueOp True{ get{ return new TrueOp();}}
/// <summary>
/// Dummy operator that always returns true
/// </summary>
[Serializable]
public sealed class TrueOp : UnaryOperatorOp
{
internal TrueOp() {}
public override bool GetValue(EvalContext ctx){ return true;}
}
public static FalseOp False{ get{ return new FalseOp();}}
/// <summary>
/// Dummy operator that always returns false
/// </summary>
[Serializable]
public sealed class FalseOp : UnaryOperatorOp
{
internal FalseOp() {}
public override bool GetValue(EvalContext ctx){ return false;}
}
}
}