@@ -38,7 +38,88 @@ u4(cc_size=4, cc_update_time=0)
3838
3939另一种常见的思路,是将计算与查询分开。在后台常驻一个进程,用来负责循环更新全图所有节点的 CC 信息,查询则只负责返回缓存结果。
4040
41-
41+ {% code title="update\_ cc\_ size\_ in\_ batch.gsql" %}
42+ ``` sql
43+ CREATE QUERY update_cc_size_in_batch(
44+ INT batch_size,
45+ DATETIME start_date_time,
46+ DATETIME end_date_time
47+ ) FOR GRAPH MyGraph {
48+
49+ OrAccum< BOOL> @visited;
50+ MapAccum< VERTEX, BOOL> @@updated;
51+ MaxAccum< DATETIME> @cc_update_time;
52+ SetAccum< VERTEX> @@batch_accounts;
53+ MinAccum< DATETIME> @@last_update_time;
54+
55+ INT num_cc_updated = 0 ;
56+
57+ /*
58+ 按照 cc 上一次更新时间排序,从距离当前时间最早的账号中
59+ 抽样出一批种子账号
60+ */
61+ all_accounts = {Account.* };
62+ samples =
63+ SELECT t
64+ FROM all_accounts:t
65+ ORDER BY t .cc_update_time ASC
66+ LIMIT batch_size
67+ ;
68+ /*
69+ 将这些账号加入到一个 SetAccum 中
70+ */
71+ samples =
72+ SELECT t
73+ FROM samples:t
74+ POST- ACCUM
75+ @@batch_accounts + = t,
76+ @@updated + = (t - > FALSE),
77+ @@last_update_time + = t .cc_update_time
78+ ;
79+
80+ /* Update cc_size for every vertices introduced by the batch accounts */
81+
82+ FOREACH seed_account IN @@batch_accounts DO
83+ /*
84+ If the seed_account has been updated in the previous iteration,
85+ skip to update it.
86+ */
87+ IF @@updated .get (seed_account) == TRUE THEN
88+ CONTINUE;
89+ END;
90+
91+ /* Do traversing from the seed account */
92+ seed = {seed_account};
93+ comp_vs = seed;
94+ WHILE seed .size () > 0 DO
95+ seed =
96+ SELECT t
97+ FROM seed - (co_ip:e)- > Account:t
98+ WHERE
99+ (t.@visited == FALSE) AND
100+ (e .create_time BETWEEN start_dt AND end_dt)
101+ POST- ACCUM
102+ t.@visited + = TRUE,
103+ @@updated + = (t - > TRUE)
104+ ;
105+ comp_vs = comp_vs UNION seed;
106+ END;
107+
108+ /* Update cc_size info */
109+ UPDATE s FROM comp_vs:s
110+ SET s .cc_size = comp_vs .size (),
111+ s .cc_update_time = now()
112+ ;
113+ num_cc_updated = num_cc_updated + 1 ;
114+ END;
115+
116+ PRINT num_cc_updated,
117+ @@last_update_time AS last_update_time,
118+ now() AS current_time
119+ ;
120+ }
121+ ```
122+ {% endcode %}
42123
43124
44125
0 commit comments