-
Notifications
You must be signed in to change notification settings - Fork 211
/
postgres-synctable.coffee
1087 lines (998 loc) · 41.7 KB
/
postgres-synctable.coffee
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
##############################################################################
#
# CoCalc: Collaborative Calculation in the Cloud
#
# Copyright (C) 2017, Sagemath Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################
###
Server side synchronized tables built on PostgreSQL, and basic support
for user get query updates.
###
EventEmitter = require('events')
immutable = require('immutable')
async = require('async')
underscore = require('underscore')
{defaults} = misc = require('smc-util/misc')
required = defaults.required
misc_node = require('smc-util-node/misc_node')
{pg_type, one_result, all_results, quote_field} = require('./postgres-base')
{SCHEMA} = require('smc-util/schema')
exports.extend_PostgreSQL = (ext) -> class PostgreSQL extends ext
_ensure_trigger_exists: (table, select, watch, cb) =>
dbg = @_dbg("_ensure_trigger_exists(#{table})")
dbg("select=#{misc.to_json(select)}")
if misc.len(select) == 0
cb('there must be at least one column selected')
return
tgname = trigger_name(table, select, watch)
trigger_exists = undefined
async.series([
(cb) =>
dbg("checking whether or not trigger exists")
@_query
query : "SELECT count(*) FROM pg_trigger WHERE tgname = '#{tgname}'"
cb : (err, result) =>
if err
cb(err)
else
trigger_exists = parseInt(result.rows[0].count) > 0
cb()
(cb) =>
if trigger_exists
dbg("trigger #{tgname} already exists")
cb()
return
dbg("creating trigger #{tgname}")
code = trigger_code(table, select, watch)
async.series([
(cb) =>
@_query
query : code.function
cb : cb
(cb) =>
@_query
query : code.trigger
cb : cb
], cb)
], cb)
_listen: (table, select, watch, cb) =>
dbg = @_dbg("_listen(#{table})")
dbg("select = #{misc.to_json(select)}")
if not misc.is_object(select)
cb('select must be an object')
return
if misc.len(select) == 0
cb('there must be at least one column')
return
if not misc.is_array(watch)
cb('watch must be an array')
return
@_listening ?= {}
tgname = trigger_name(table, select, watch)
if @_listening[tgname] > 0
dbg("already listening")
@_listening[tgname] += 1
cb?(undefined, tgname)
return
async.series([
(cb) =>
dbg("ensure trigger exists")
@_ensure_trigger_exists(table, select, watch, cb)
(cb) =>
dbg("add listener")
@_query
query : "LISTEN #{tgname}"
cb : cb
], (err) =>
if err
dbg("fail: err = #{err}")
cb?(err)
else
@_listening[tgname] ?= 0
@_listening[tgname] += 1
dbg("success")
cb?(undefined, tgname)
)
_notification: (mesg) =>
#@_dbg('notification')(misc.to_json(mesg)) # this is way too verbose...
@emit(mesg.channel, JSON.parse(mesg.payload))
_clear_listening_state: =>
@_listening = {}
_stop_listening: (table, select, watch, cb) =>
@_listening ?= {}
tgname = trigger_name(table, select, watch)
if not @_listening[tgname]? or @_listening[tgname] == 0
cb?()
return
if @_listening[tgname] > 0
@_listening[tgname] -= 1
if @_listening[tgname] == 0
@_query
query : "UNLISTEN #{tgname}"
cb : cb
# Server-side changefeed-updated table, which automatically restart changefeed
# on error, etc. See SyncTable docs where the class is defined.
synctable: (opts) =>
opts = defaults opts,
table : required
columns : undefined
where : undefined
limit : undefined
order_by : undefined
where_function : undefined # if given; a function of the *primary* key that returns true if and only if it matches the changefeed
idle_timeout_s : undefined # TODO: currently ignored
cb : undefined
if @is_standby
opts.cb?("synctable against standby database not allowed")
return
return new SyncTable(@, opts.table, opts.columns, opts.where, opts.where_function, opts.limit, opts.order_by, opts.cb)
changefeed: (opts) =>
opts = defaults opts,
table : required # Name of the table
select : required # Map from field names to postgres data types. These must
# determine entries of table (e.g., primary key).
watch : required # Array of field names we watch for changes
where : required # Condition involving only the fields in select; or function taking obj with select and returning true or false
cb : required
if @is_standby
opts.cb?("changefeed against standby database not allowed")
return
new Changes(@, opts.table, opts.select, opts.watch, opts.where, opts.cb)
return
# Event emitter that
project_and_user_tracker: (opts) =>
opts = defaults opts,
cb : required
if @_project_and_user_tracker?
opts.cb(undefined, @_project_and_user_tracker)
return
@_project_and_user_tracker_cbs ?= []
@_project_and_user_tracker_cbs.push(opts.cb)
if @_project_and_user_tracker_cbs.length == 1
x = new ProjectAndUserTracker @, (err) =>
if not err
@_project_and_user_tracker = x
x.on 'error', =>
delete @_project_and_user_tracker
else
x = undefined
for cb in @_project_and_user_tracker_cbs
cb?(err, x)
delete @_project_and_user_tracker_cbs
class ProjectAndUserTracker extends EventEmitter
constructor: (_db, cb) ->
super()
@_db = _db
dbg = @_dbg('constructor')
dbg("Initializing Project and user tracker...")
@setMaxListeners(10000) # every changefeed might result in a listener on this one object.
# by a "set" we mean map to bool
@_accounts = {} # set of accounts we care about
@_users = {} # map from from project_id to set of users of a given project
@_projects = {} # map from account_id to set of projects of a given user
@_collabs = {} # map from account_id to map from account_ids to *number* of projects you have in common
# create changefeed listening on changes to projects table
@_db.changefeed
table : 'projects'
select : {project_id:'UUID'}
watch : ['users']
where : {}
cb : (err, feed) =>
if err
dbg("Error = #{err}")
cb(err)
else
dbg("Done")
@_feed = feed
@_feed.on 'change', @_handle_change
@_feed.on 'error', @_handle_error
@_feed.on 'close', (=> @_handle_error("changefeed closed"))
cb()
_dbg: (f) =>
return @_db._dbg("Tracker.#{f}")
_handle_error: (err) =>
if @_closed
return
# There was an error in the changefeed.
# Error is totally fatal, so we close up shop.
dbg = @_dbg("_handle_error")
dbg("err='#{err}'")
@emit('error', err)
@close()
close: =>
if @_closed
return
@_closed = true
@emit('close')
@removeAllListeners()
@_feed?.close()
delete @_feed
_handle_change: (x) =>
if x.action == 'delete'
project_id = x.old_val.project_id
if not @_users[project_id]?
# no users
return
for account_id of @_users[project_id]
@_remove_user_from_project(account_id, project_id)
return
# users on a project changed or project created
project_id = x.new_val.project_id
@_db._query
query : "SELECT jsonb_object_keys(users) AS account_id FROM projects"
where : "project_id = $::UUID":project_id
cb : all_results 'account_id', (err, users) =>
if err
# TODO! -- will have to try again... or make a version of _query that can't fail...?
return
if not @_users[project_id]?
# we are not already watching this project
any = false
for account_id in users
if @_accounts[account_id]
any = true
break
if not any
# *and* none of our tracked users are on this project... so don't care
return
# first add any users who got added, and record which accounts are relevant
users_now = {}
for account_id in users
users_now[account_id] = true
users_before = @_users[project_id] ? {}
for account_id of users_now
if not users_before[account_id]
@_add_user_to_project(account_id, project_id)
for account_id of users_before
if not users_now[account_id]
@_remove_user_from_project(account_id, project_id)
# add and remove user from a project, maintaining our data structures (@_accounts, @_projects, @_collabs)
_add_user_to_project: (account_id, project_id) =>
dbg = @_dbg('_add_user_to_project')
if account_id?.length != 36 or project_id?.length != 36
# nothing to do -- better than crashing the server...
dbg("WARNING: invalid account_id (='#{account_id}') or project_id (='#{project_id}')")
return
if @_projects[account_id]?[project_id]
return
@emit 'add_user_to_project', {account_id:account_id, project_id:project_id}
users = @_users[project_id] ?= {}
users[account_id] = true
projects = @_projects[account_id] ?= {}
projects[project_id] = true
collabs = @_collabs[account_id] ?= {}
for other_account_id of users
if collabs[other_account_id]?
collabs[other_account_id] += 1
else
collabs[other_account_id] = 1
@emit 'add_collaborator', {account_id:account_id, collab_id:other_account_id}
other_collabs = @_collabs[other_account_id]
if other_collabs[account_id]?
other_collabs[account_id] += 1
else
other_collabs[account_id] = 1
@emit 'add_collaborator', {account_id:other_account_id, collab_id:account_id}
_remove_user_from_project: (account_id, project_id, no_emit) =>
if account_id?.length != 36 or project_id?.length != 36
throw Error("invalid account_id or project_id")
if not @_projects[account_id]?[project_id]
return
if not no_emit
@emit 'remove_user_from_project', {account_id:account_id, project_id:project_id}
collabs = @_collabs[account_id] ?= {}
for other_account_id of @_users[project_id]
@_collabs[account_id][other_account_id] -= 1
if @_collabs[account_id][other_account_id] == 0
delete @_collabs[account_id][other_account_id]
if not no_emit
@emit 'remove_collaborator', {account_id:account_id, collab_id:other_account_id}
@_collabs[other_account_id][account_id] -= 1
if @_collabs[other_account_id][account_id] == 0
delete @_collabs[other_account_id][account_id]
if not no_emit
@emit 'remove_collaborator', {account_id:other_account_id, collab_id:account_id}
delete @_users[project_id][account_id]
delete @_projects[account_id][project_id]
# Register the given account so that this client watches the database
# in order to be aware of all projects and collaborators of the
# given account.
register: (opts) =>
opts = defaults opts,
account_id : required
cb : required
if @_accounts[opts.account_id]?
# already registered
opts.cb()
return
@_register_todo ?= {}
if misc.len(@_register_todo) == 0
# no registration is currently happening
@_register_todo[opts.account_id] = [opts.cb]
# kick things off -- this will keep registering accounts
# until everything is done, then set @_register_todo to undefined
@_register()
else
# Accounts are being registered right now. Add to the todo list.
v = @_register_todo[opts.account_id]
if v?
v.push(opts.cb)
else
@_register_todo[opts.account_id] = [opts.cb]
# Call _register to completely clear the work @_register_todo work queue.
# NOTE: _register does each account, *one after another*, rather than doing
# everything in parallel. WARNING: DO NOT rewrite this to do everything in parallel,
# unless you think you thoroughly understand the algorithm, since I think
# doing things in parallel would horribly break!
_register: =>
account_id = misc.keys(@_register_todo)?[0]
if not account_id?
# no work
return
# Register this account
dbg = @_dbg("_register")
dbg("registering account='#{account_id}'...")
@_db._query
query : "SELECT project_id, json_agg(o) as users FROM (select project_id, jsonb_object_keys(users) AS o FROM projects WHERE users ? $1::TEXT) s group by s.project_id"
params : [account_id]
cb : all_results (err, x) =>
if not err
@_accounts[account_id] = true
for a in x
if @_users[a.project_id]?
# already have data about this project
continue
else
for collab_account_id in a.users
# NOTE: Very rarely, sometimes collab_account_id is not defined
if collab_account_id?
@_add_user_to_project(collab_account_id, a.project_id)
# call the callbacks
if err
dbg("error registering '#{account_id}' -- err=#{err}")
else
dbg("successfully registered '#{account_id}'")
for cb in @_register_todo[account_id]
cb?(err)
# We are done (trying to) register account_id, for good or ill.
delete @_register_todo[account_id]
if misc.len(@_register_todo) > 0
# Deal with next account that needs to be registered
@_register()
unregister: (opts) =>
opts = defaults opts,
account_id : required
if not @_accounts[opts.account_id]?
return
v = []
for project_id of @_projects[opts.account_id]
v.push(project_id)
delete @_accounts[opts.account_id]
# Forget about any projects they we are on that are no longer
# necessary to watch...
for project_id in v
need = false
for account_id of @_users[project_id]
if @_accounts[account_id]?
need = true
break
if not need
for account_id of @_users[project_id]
@_remove_user_from_project(account_id, project_id, true)
delete @_users[project_id]
return
# return *set* of projects that this user is a collaborator on
projects: (account_id) =>
if not @_accounts[account_id]?
# This should never happen, but very rarely it DOES. I do not know why, having studied the
# code. But when it does, just raising an exception blows up the server really badly.
# So for now we just async register the account, return that it is not a collaborator
# on anything. Then some query will fail, get tried again, and work since registration will
# have finished.
#throw Error("account (='#{account_id}') must be registered")
@register(account_id:account_id, cb:->)
return {}
return @_projects[account_id] ? {}
# map from collabs of account_id to number of projects they collab on (account_id itself counted twice)
collabs: (account_id) =>
return @_collabs[account_id]
###
The Changes class is a useful building block
for making changefeeds. It lets you watch when given
columns change in a given table, and be notified
when a where condition is satisfied.
IMPORTANT: If an error event is emitted then then
Changes object will close and not work any further!
You must recreate it.
###
class Changes extends EventEmitter
constructor: (_db, _table, _select, _watch, _where, cb) ->
super()
@_db = _db
@_table = _table
@_select = _select
@_watch = _watch
@_where = _where
@dbg = @_dbg("constructor")
@dbg("select=#{misc.to_json(@_select)}, watch=#{misc.to_json(@_watch)}, @_where=#{misc.to_json(@_where)}")
try
@_init_where()
catch e
cb?("error initializing where conditions -- #{e}")
return
@_db._listen @_table, @_select, @_watch, (err, tgname) =>
if err
cb?(err); return
@_tgname = tgname
@_db.on(@_tgname, @_handle_change)
# NOTE: we close on *connect*, not on disconnect, since then clients
# that try to reconnect will only try to do so when we have an actual
# connection to the database. No point in worrying them while trying
# to reconnect, which only makes matters worse (as they panic and
# requests pile up!).
@_db.once('connect', @close)
cb?(undefined, @)
_dbg: (f) =>
return @_db._dbg("Changes(table='#{@_table}').#{f}")
# this breaks the changefeed -- client must recreate it; nothing further will work at all.
_fail: (err) =>
if @_closed
return
dbg = @_dbg("_fail")
dbg("err='#{err}'")
@emit('error', new Error(err))
@close()
close: (cb) =>
if @_closed
cb?()
return
@_closed = true
@emit('close', {action:'close'})
@removeAllListeners()
@_db.removeListener(@_tgname, @_handle_change)
@_db.removeListener('connect', @close)
@_db._stop_listening(@_table, @_select, @_watch, cb)
delete @_tgname
delete @_condition
cb?()
_old_val: (result, action, mesg) =>
# include only changed fields if action is 'update'
if action == 'update'
old_val = {}
for field, val of mesg[1]
old = mesg[2][field]
if val != old
old_val[field] = old
if misc.len(old_val) > 0
result.old_val = old_val
_handle_change: (mesg) =>
#console.log '_handle_change', mesg
if mesg[0] == 'DELETE'
if not @_match_condition(mesg[2])
return
@emit('change', {action:'delete', old_val:mesg[2]})
else
action = "#{mesg[0].toLowerCase()}"
if not @_match_condition(mesg[1])
if action != 'update'
return
for k, v of mesg[1]
if not mesg[2][k]?
mesg[2][k] = v
if @_match_condition(mesg[2])
@emit('change', {action:'delete', old_val:mesg[2]})
return
if @_watch.length == 0
r = {action:action, new_val:mesg[1]}
@_old_val(r, action, mesg)
@emit('change', r)
return
where = {}
for k, v of mesg[1]
where["#{k} = $"] = v
@_db._query
select: @_watch
table : @_table
where : where
cb : one_result (err, result) =>
if err
@_fail(err)
return
if not result?
# This happens when record isn't deleted, but some
# update results in the object being removed from our
# selection criterion... which we view as "delete".
@emit('change', {action:'delete', old_val:mesg[1]})
return
r = {action:action, new_val:misc.merge(result, mesg[1])}
@_old_val(r, action, mesg)
@emit('change', r)
insert: (where) =>
where0 = {}
for k, v of where
where0["#{k} = $"] = v
@_db._query
select : @_watch.concat(misc.keys(@_select))
table : @_table
where : where0
cb : all_results (err, results) =>
## Useful for testing that the @_fail thing below actually works.
##if Math.random() < .7
## err = "simulated error"
if err
@_dbg("insert")("FAKE ERROR!")
@_fail(err) # this is game over
return
else
for x in results
if @_match_condition(x)
misc.map_mutate_out_undefined(x)
@emit('change', {action:'insert', new_val:x})
delete: (where) =>
# listener is meant to delete everything that *matches* the where, so
# there is no need to actually do a query.
@emit('change', {action:'delete', old_val:where})
_init_where: =>
if typeof(@_where) == 'function'
# user provided function
@_match_condition = @_where
return
if misc.is_object(@_where)
w = [@_where]
else
w = @_where
@_condition = {}
add_condition = (field, op, val) =>
field = field.trim()
if field[0] == '"' # de-quote
field = field.slice(1,field.length-1)
if not @_select[field]?
throw Error("'#{field}' must be in select")
if misc.is_object(val)
throw Error("val (=#{misc.to_json(val)}) must not be an object")
if misc.is_array(val)
if op == '=' or op == '=='
# containment
f = (x) ->
for v in val
if x == v
return true
return false
else if op == '!=' or op == '<>'
# not contained in
f = (x) ->
for v in val
if x == v
return false
return true
else
throw Error("if val is an array, then op must be = or !=")
else if misc.is_date(val)
# Inputs to condition come back as JSON, which doesn't know
# about timestamps, so we convert them to date objects.
if op in ['=', '==']
f = (x) -> (new Date(x) - val == 0)
else if op in ['!=', '<>']
f = (x) -> (new Date(x) - val != 0)
else
g = misc.op_to_function(op)
f = (x) -> g(new Date(x), val)
else
g = misc.op_to_function(op)
f = (x) -> g(x, val)
@_condition[field] = f
for obj in w
if misc.is_object(obj)
for k, val of obj
###
k should be of one of the following forms
- "field op $::TYPE"
- "field op $" or
- "field op any($)"
- 'field' (defaults to =)
where op is one of =, <, >, <=, >=, !=
val must be:
- something where javascript === and comparisons works as you expect!
- or an array, in which case op must be = or !=, and we ALWAYS do inclusion (analogue of any).
###
found = false
for op in misc.operators
i = k.indexOf(op)
if i != -1
add_condition(k.slice(0, i).trim(), op, val)
found = true
break
if not found
throw Error("unable to parse '#{k}'")
else if typeof(obj) == 'string'
found = false
for op in misc.operators
i = obj.indexOf(op)
if i != -1
add_condition(obj.slice(0, i), op, eval(obj.slice(i+op.length).trim()))
found = true
break
if not found
throw Error("unable to parse '#{obj}'")
else
throw Error("NotImplementedError")
if misc.len(@_condition) == 0
delete @_condition
@_match_condition = (obj) =>
#console.log '_match_condition', obj
if not @_condition?
return true
for field, f of @_condition
if not f(obj[field])
#console.log 'failed due to field ', field
return false
return true
class SyncTable extends EventEmitter
constructor: (_db, _table, _columns, _where, _where_function, _limit, _order_by, cb) ->
super()
@_db = _db
@_table = _table
@_columns = _columns
@_where = _where
@_where_function = _where_function
@_limit = _limit
@_order_by = _order_by
t = SCHEMA[@_table]
if not t?
@_state = 'error'
cb?("unknown table #{@_table}")
return
try
@_primary_key = @_db._primary_key(@_table)
catch e
cb?(e)
return
@_listen_columns = {"#{@_primary_key}" : pg_type(t.fields[@_primary_key], @_primary_key)}
# We only trigger an update when one of the columns we care about actually changes.
if @_columns
@_watch_columns = misc.copy(@_columns) # don't include primary key since it can't change.
if @_primary_key not in @_columns
@_columns = @_columns.concat([@_primary_key]) # required
@_select_columns = @_columns
else
@_watch_columns = [] # means all of them
@_select_columns = misc.keys(SCHEMA[@_table].fields)
@_select_query = "SELECT #{(quote_field(x) for x in @_select_columns)} FROM #{@_table}"
#@_update = underscore.throttle(@_update, 500)
@_init (err) =>
if err and not cb?
@emit("error", err)
return
@emit('init')
cb?(err, @)
_dbg: (f) =>
return @_db._dbg("SyncTable(table='#{@_table}').#{f}")
_query_opts: () =>
opts = {}
opts.query = @_select_query
opts.where = @_where
opts.limit = @_limit
opts.order_by = @_order_by
return opts
close: (cb) =>
@removeAllListeners()
@_db.removeListener(@_tgname, @_notification)
@_db.removeListener('connect', @_reconnect)
delete @_value
@_state = 'closed'
@_db._stop_listening(@_table, @_listen_columns, @_watch_columns, cb)
connect: (opts) =>
opts?.cb?() # NO-OP -- only needed for backward compatibility
_notification: (obj) =>
#console.log 'notification', obj
[action, new_val, old_val] = obj
if action == 'DELETE' or not new_val?
k = old_val[@_primary_key]
if @_value.has(k)
@_value = @_value.delete(k)
process.nextTick(=>@emit('change', k))
else
k = new_val[@_primary_key]
if @_where_function? and not @_where_function(k)
# doesn't match -- nothing to do -- ignore
return
@_changed[k] = true
@_update()
_init: (cb) =>
misc.retry_until_success
f : @_do_init
start_delay : 3000
max_delay : 10000
log : @_dbg("_init")
cb : cb
_do_init: (cb) =>
@_state = 'init' # 'init' -> ['error', 'ready'] -> 'closed'
@_value = immutable.Map()
@_changed = {}
async.series([
(cb) =>
# ensure database client is listening for primary keys changes to our table
@_db._listen @_table, @_listen_columns, @_watch_columns, (err, tgname) =>
@_tgname = tgname
@_db.on(@_tgname, @_notification)
cb(err)
(cb) =>
opts = @_query_opts()
opts.cb = (err, result) =>
if err
cb(err)
else
@_process_results(result.rows)
@_db.once('connect', @_reconnect)
cb()
@_db._query(opts)
(cb) =>
@_update(cb)
], (err) =>
if err
@_state = 'error'
cb(err)
else
@_state = 'ready'
cb()
)
_reconnect: (cb) =>
dbg = @_dbg("_reconnect")
if @_state != 'ready'
dbg("only attempt reconnect if we were already successfully connected at some point.")
return
# Everything was already initialized, but then the connection to the
# database was dropped... and then successfully re-connected. Now
# we need to (1) setup everything again, and (2) send out notifications
# about anything in the table that changed.
dbg("Save state from before disconnect")
before = @_value
dbg("Clean up everything.")
@_db.removeListener(@_tgname, @_notification)
@_db.removeListener('connect', @_reconnect)
delete @_value
dbg("connect and initialize")
@_init (err) =>
if err
cb?(err)
return
if @_value? and before?
# It's highly unlikely that before or @_value would not be defined, but it could happen (see #2527)
dbg("notify about anything that changed when we were disconnected")
before.map (v, k) =>
if not v.equals(@_value.get(k))
@emit('change', k)
@_value.map (v, k) =>
if not before.has(k)
@emit('change', k)
cb?()
_process_results: (rows) =>
if @_state == 'closed'
return
for x in rows
k = x[@_primary_key]
v = immutable.fromJS(misc.map_without_undefined(x))
if not v.equals(@_value.get(k))
@_value = @_value.set(k, v)
if @_state == 'ready' # only send out change notifications after ready.
process.nextTick(=>@emit('change', k))
# Remove from synctable anything that no longer matches the where criterion.
_process_deleted: (rows, changed) =>
kept = {}
for x in rows
kept[x[@_primary_key]] = true
for k of changed
if not kept[k] and @_value.has(k)
# The record with primary_key k no longer matches the where criterion
# so we delete it from our synctable.
@_value = @_value.delete(k)
if @_state == 'ready'
process.nextTick(=>@emit('change', k))
# Grab any entries from table about which we have been notified of changes.
_update: (cb) =>
if misc.len(@_changed) == 0 # nothing to do
cb?()
return
changed = @_changed
@_changed = {} # reset changed set -- could get modified during query below, which is fine.
if @_select_columns.length == 1 # special case where we don't have to query for more info
@_process_results((("#{@_primary_key}" : x) for x in misc.keys(changed)))
cb?()
return
# Have to query to get actual changed data.
@_db._query
query : @_select_query
where : [{"#{@_primary_key} = ANY($)" : misc.keys(changed)}, @_where]
cb : (err, result) =>
if err
@_dbg("update")("error #{err}")
for k of changed
@_changed[k] = true # will try again later
else
@_process_results(result.rows)
@_process_deleted(result.rows, changed)
cb?()
get: (key) =>
return if key? then @_value?.get(key) else @_value
getIn: (x) =>
return @_value?.getIn(x)
has: (key) =>
return @_value?.has(key)
# wait until some function of this synctable is truthy
wait: (opts) =>
opts = defaults opts,
until : required # waits until "until(@)" evaluates to something truthy
timeout : 30 # in *seconds* -- set to 0 to disable (sort of DANGEROUS if 0, obviously.)
cb : required # cb(undefined, until(@)) on success and cb('timeout') on failure due to timeout
x = opts.until(@)
if x
opts.cb(undefined, x) # already true
return
fail_timer = undefined
f = =>
x = opts.until(@)
if x
@removeListener('change', f)
if fail_timer?
clearTimeout(fail_timer)
fail_timer = undefined
opts.cb(undefined, x)
@on('change', f)
if opts.timeout
fail = =>
@removeListener('change', f)
opts.cb('timeout')
fail_timer = setTimeout(fail, 1000*opts.timeout)
return
###
Trigger functions
###
trigger_name = (table, select, watch) ->
if not misc.is_object(select)
throw Error("trigger_name -- columns must be a map of colname:type")
c = misc.keys(select)
c.sort()
watch = misc.copy(watch)
watch.sort()
if watch.length > 0
c.push('|')
c = c.concat(watch)
return 'change_' + misc_node.sha1("#{table} #{c.join(' ')}").slice(0,16)
###
INPUT:
table -- name of a table
select -- map from field names (of table) to their postgres types
change -- array of field names (of table)
Creates a trigger function that fires whenever any of the given
columns changes, and sends the columns in select out as a notification.
###
trigger_code = (table, select, watch) ->
tgname = trigger_name(table, select, watch)
column_decl_old = ("#{field}_old #{type ? 'text'};" for field, type of select)
column_decl_new = ("#{field}_new #{type ? 'text'};" for field, type of select)
assign_old = ("#{field}_old = OLD.#{field};" for field, _ of select)
assign_new = ("#{field}_new = NEW.#{field};" for field, _ of select)
build_obj_old = ("'#{field}', #{field}_old" for field, _ of select)
build_obj_new = ("'#{field}', #{field}_new" for field, _ of select)
if watch.length > 0
no_change = ("OLD.#{field} = NEW.#{field}" for field in watch.concat(misc.keys(select))).join(' AND ')
else
no_change = 'FALSE'
if watch.length > 0
x = {}
for k in watch
x[k] = true
for k in misc.keys(select)
x[k] = true
update_of = "OF #{(quote_field(field) for field in misc.keys(x)).join(',')}"
else
update_of = ""
code = {}
code.function = """
CREATE OR REPLACE FUNCTION #{tgname}() RETURNS TRIGGER AS $$
DECLARE
notification json;
obj_old json;
obj_new json;
#{column_decl_old.join('\n')}
#{column_decl_new.join('\n')}
BEGIN
-- TG_OP is 'DELETE', 'INSERT' or 'UPDATE'
IF TG_OP = 'DELETE' THEN
#{assign_old.join('\n')}
obj_old = json_build_object(#{build_obj_old.join(',')});
END IF;
IF TG_OP = 'INSERT' THEN
#{assign_new.join('\n')}
obj_new = json_build_object(#{build_obj_new.join(',')});
END IF;
IF TG_OP = 'UPDATE' THEN
IF #{no_change} THEN
RETURN NULL;
END IF;
#{assign_old.join('\n')}