-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCreateTopic.sql
193 lines (175 loc) · 7.55 KB
/
CreateTopic.sql
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
--------------------------------------------------------------------------------------------------------------------------------
-- CREATE TOPIC
--------------------------------------------------------------------------------------------------------------------------------
-- Creates a new topic.
--------------------------------------------------------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[CreateTopic]
@Key VARCHAR(128) ,
@ContentType VARCHAR(128) ,
@ParentID INT = NULL,
@Attributes AttributeValues READONLY,
@ExtendedAttributes XML = NULL,
@References TopicReferences READONLY,
@Version DATETIME2(7) = NULL
AS
--------------------------------------------------------------------------------------------------------------------------------
-- DECLARE VARIABLES
--------------------------------------------------------------------------------------------------------------------------------
DECLARE @IsNestedTransaction BIT;
DECLARE @TopicID INT;
BEGIN TRY
--------------------------------------------------------------------------------------------------------------------------------
-- BEGIN TRANSACTION
--------------------------------------------------------------------------------------------------------------------------------
-- ### NOTE JJC20210218: By necessity, this procedure potentially makes a massive number of changes to the Topics table's nested
-- set. During the execution, the nested set hierarchy WILL be in an inconsistent state. Read operations during that time are
-- very likely to be corrupted. As such, it's critical that the updates made as part of this procedure be isolated from other
-- reads being performed on the system. Further, we don't want any writes being made to the Topics table during this time—see
-- notes below regarding TABLOCK. By combining SERIALIZABLE with TABLOCK, we ensure that a) readers get a stable state, while b)
-- writers are prevented from concurrently modifying the table. Fortunately, these types of operations should be pretty
-- uncommon! The nested set model is very much optimized for read performance and presumes a relatively stable data set.
--------------------------------------------------------------------------------------------------------------------------------
IF (@@TRANCOUNT = 0)
BEGIN
SET @IsNestedTransaction = 0;
BEGIN TRANSACTION;
END
ELSE
BEGIN
SET @IsNestedTransaction = 1;
END
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
--------------------------------------------------------------------------------------------------------------------------------
-- SET DEFAULT VERSION DATETIME
--------------------------------------------------------------------------------------------------------------------------------
IF @Version IS NULL
SET @Version = SYSUTCDATETIME()
--------------------------------------------------------------------------------------------------------------------------------
-- DECLARE AND SET VARIABLES
--------------------------------------------------------------------------------------------------------------------------------
DECLARE @RangeRight INT --Right Most Sibling
SET @RangeRight = 0
--------------------------------------------------------------------------------------------------------------------------------
-- RESERVE SPACE FOR NEW CHILD.
--------------------------------------------------------------------------------------------------------------------------------
-- ### NOTE JJC20210218: We usually avoid broad hints like TABLOCK. That said, the create operation requires multiple operations
-- against the topics table which will fail if the topic range shifts. Locking the table helps ensure that data integrity issues
-- aren't introduced by concurrent modification of the nested set. Because this is being done within a SERIALIZABLE isolation
-- level, this lock will be maintained for the duration of the transaction.
--------------------------------------------------------------------------------------------------------------------------------
IF (@ParentID IS NOT NULL)
BEGIN
SELECT @RangeRight = RangeRight
FROM Topics
WITH ( TABLOCK
)
WHERE TopicID = @ParentID
UPDATE Topics
SET RangeLeft =
CASE
WHEN RangeLeft > @RangeRight
THEN RangeLeft + 2
ELSE RangeLeft
END,
RangeRight =
CASE
WHEN RangeRight >= @RangeRight
THEN RangeRight + 2
ELSE RangeRight
END
WHERE RangeRight >= @RangeRight
END
ELSE
BEGIN
SELECT @RangeRight = ISNULL(MAX(RangeRight), 0) + 1
FROM Topics
WITH ( TABLOCK
)
END
--------------------------------------------------------------------------------------------------------------------------------
-- CREATE NEW TOPIC
--------------------------------------------------------------------------------------------------------------------------------
INSERT INTO Topics (
RangeLeft,
RangeRight,
TopicKey,
ContentType,
ParentID,
LastModified
)
Values (
@RangeRight,
@RangeRight + 1,
@Key,
@ContentType,
@ParentID,
@Version
)
SELECT @TopicID = SCOPE_IDENTITY()
--------------------------------------------------------------------------------------------------------------------------------
-- ADD INDEXED ATTRIBUTES
--------------------------------------------------------------------------------------------------------------------------------
INSERT INTO Attributes (
TopicID ,
AttributeKey ,
AttributeValue ,
Version
)
SELECT @TopicID,
AttributeKey,
AttributeValue,
@Version
FROM @Attributes
WHERE ISNULL(AttributeValue, '') != ''
--------------------------------------------------------------------------------------------------------------------------------
-- ADD EXTENDED ATTRIBUTES (XML)
--------------------------------------------------------------------------------------------------------------------------------
IF @ExtendedAttributes IS NOT NULL
BEGIN
INSERT
INTO ExtendedAttributes (
TopicID ,
AttributesXml ,
Version
)
VALUES ( @TopicID ,
@ExtendedAttributes ,
@Version
)
END
--------------------------------------------------------------------------------------------------------------------------------
-- ADD REFERENCES
--------------------------------------------------------------------------------------------------------------------------------
DECLARE @ReferenceCount INT
SELECT @ReferenceCount = COUNT(ReferenceKey)
FROM @References
IF @ReferenceCount > 0
BEGIN
EXEC UpdateReferences @TopicID,
@References,
@Version,
1
END
--------------------------------------------------------------------------------------------------------------------------------
-- COMMIT TRANSACTION
--------------------------------------------------------------------------------------------------------------------------------
IF (@@TRANCOUNT > 0 AND @IsNestedTransaction = 0)
BEGIN
COMMIT
END
END TRY
--------------------------------------------------------------------------------------------------------------------------------
-- HANDLE ERRORS
--------------------------------------------------------------------------------------------------------------------------------
BEGIN CATCH
IF (@@TRANCOUNT > 0 AND @IsNestedTransaction = 0)
BEGIN
ROLLBACK;
END;
THROW
RETURN;
END CATCH
--------------------------------------------------------------------------------------------------------------------------------
-- RETURN TOPIC ID
--------------------------------------------------------------------------------------------------------------------------------
RETURN @TopicID