/
couch_mgr.erl
executable file
·793 lines (694 loc) · 33.2 KB
/
couch_mgr.erl
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
%%%-------------------------------------------------------------------
%%% @author James Aimonetti <james@2600hz.org>
%%% @copyright (C) 2010, James Aimonetti
%%% @doc
%%% Manage CouchDB connections
%%% @end
%%% Created : 16 Sep 2010 by James Aimonetti <james@2600hz.org>
%%%-------------------------------------------------------------------
-module(couch_mgr).
-behaviour(gen_server).
%% API
-export([start_link/0, set_host/1, set_host/2, set_host/3, set_host/4, set_host/5, get_host/0, get_port/0, get_creds/0, get_url/0, get_uuid/0, get_uuids/1]).
-export([get_admin_port/0, get_admin_conn/0, get_admin_url/0, get_node_cookie/0, set_node_cookie/1]).
%% System manipulation
-export([db_exists/1, db_info/0, db_info/1, db_create/1, db_create/2, db_compact/1, db_view_cleanup/1, db_delete/1, db_replicate/1]).
-export([admin_db_info/0, admin_db_info/1, admin_db_compact/1, admin_db_view_cleanup/1]).
-export([design_info/2, admin_design_info/2, design_compact/2, admin_design_compact/2]).
%% Document manipulation
-export([save_doc/2, save_doc/3, save_docs/2, save_docs/3, open_doc/2, open_doc/3, del_doc/2, del_docs/2, lookup_doc_rev/2]).
-export([add_change_handler/2, add_change_handler/3, rm_change_handler/2]).
-export([load_doc_from_file/3, update_doc_from_file/3]).
-export([revise_doc_from_file/3]).
-export([revise_docs_from_folder/3, revise_docs_from_folder/4]).
-export([revise_views_from_folder/2, ensure_saved/2, ensure_saved/3]).
-export([all_docs/1, all_design_docs/1, admin_all_docs/1]).
-export([all_docs/2, all_design_docs/2, admin_all_docs/2]).
%% attachments
-export([fetch_attachment/3, put_attachment/4, put_attachment/5, delete_attachment/3, delete_attachment/4]).
%% Views
-export([get_all_results/2, get_results/3]).
-export([get_result_keys/1]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-include("wh_couch.hrl").
-define(SERVER, ?MODULE).
%% Host = IP Address or FQDN
%% Connection = {Host, #server{}}
%% Change handler {DBName :: string(), {Srv :: pid(), SrvRef :: reference()}
-record(state, {
host = {"", ?DEFAULT_PORT, ?DEFAULT_ADMIN_PORT} :: {nonempty_string(), pos_integer(), pos_integer()}
,connection = #server{} :: #server{}
,admin_connection = #server{} :: #server{}
,creds = {"", ""} :: {string(), string()} % {User, Pass}
,change_handlers = dict:new() :: dict()
}).
%%%===================================================================
%%% Couch Functions
%%%===================================================================
%%--------------------------------------------------------------------
%% @public
%% @doc
%% Load a file into couch as a document (not an attachement)
%% @end
%%--------------------------------------------------------------------
-spec load_doc_from_file/3 :: (ne_binary(), atom(), nonempty_string() | ne_binary()) -> {'ok', wh_json:json_object()} | {'error', atom()}.
load_doc_from_file(DbName, App, File) ->
Path = list_to_binary([code:priv_dir(App), "/couchdb/", wh_util:to_list(File)]),
?LOG_SYS("read into db ~s from CouchDB JSON file: ~s", [DbName, Path]),
try
{ok, Bin} = file:read_file(Path),
?MODULE:save_doc(DbName, wh_json:decode(Bin)) %% if it crashes on the match, the catch will let us know
catch
_Type:{badmatch,{error,Reason}} ->
?LOG_SYS("badmatch error: ~p", [Reason]),
{error, Reason};
_Type:Reason ->
?LOG_SYS("exception: ~p", [Reason]),
{error, Reason}
end.
%%--------------------------------------------------------------------
%% @public
%% @doc
%% Overwrite the existing contents of a document with the contents of
%% a file
%% @end
%%--------------------------------------------------------------------
-spec update_doc_from_file/3 :: (ne_binary(), atom(), nonempty_string() | ne_binary()) -> {'ok', wh_json:json_object()} | {'error', atom()}.
update_doc_from_file(DbName, App, File) ->
Path = list_to_binary([code:priv_dir(App), "/couchdb/", File]),
?LOG_SYS("update db ~s from CouchDB file: ~s", [DbName, Path]),
try
{ok, Bin} = file:read_file(Path),
JObj = wh_json:decode(Bin),
{ok, Rev} = ?MODULE:lookup_doc_rev(DbName, wh_json:get_value(<<"_id">>, JObj)),
?MODULE:save_doc(DbName, wh_json:set_value(<<"_rev">>, Rev, JObj))
catch
_Type:{badmatch,{error,Reason}} ->
?LOG_SYS("bad match: ~p", [Reason]),
{error, Reason};
_Type:Reason ->
?LOG_SYS("exception: ~p", [Reason]),
{error, Reason}
end.
%%--------------------------------------------------------------------
%% @public
%% @doc
%% Create or overwrite the existing contents of a document with the
%% contents of a file
%% @end
%%--------------------------------------------------------------------
-spec revise_doc_from_file/3 :: (ne_binary(), atom(), ne_binary() | nonempty_string()) -> {'ok', wh_json:json_object()} | {'error', atom()}.
revise_doc_from_file(DbName, App, File) ->
case ?MODULE:update_doc_from_file(DbName, App, File) of
{error, _E} ->
?LOG_SYS("failed to update doc: ~p", [_E]),
?MODULE:load_doc_from_file(DbName, App, File);
{ok, _}=Resp ->
?LOG_SYS("revised ~s", [File]),
Resp
end.
%%--------------------------------------------------------------------
%% @public
%% @doc
%% Loads all .json files in an applications priv/couchdb/views/ folder
%% into a given database
%% @end
%%--------------------------------------------------------------------
-spec revise_views_from_folder/2 :: (ne_binary(), atom()) -> 'ok'.
revise_views_from_folder(DbName, App) ->
revise_docs_from_folder(DbName, App, "views").
%%--------------------------------------------------------------------
%% @public
%% @doc
%% Loads all .json files in an applications folder, relative to
%% priv/couchdb/ into a given database
%% @end
%%--------------------------------------------------------------------
-spec revise_docs_from_folder/3 :: (ne_binary(), atom(), ne_binary() | nonempty_string()) -> 'ok'.
-spec revise_docs_from_folder/4 :: (ne_binary(), atom(), ne_binary() | nonempty_string(), boolean()) -> 'ok'.
revise_docs_from_folder(DbName, App, Folder) ->
revise_docs_from_folder(DbName, App, Folder, true).
revise_docs_from_folder(DbName, App, Folder, Sleep) ->
Files = filelib:wildcard(lists:flatten([code:priv_dir(App), "/couchdb/", Folder, "/*.json"])),
do_revise_docs_from_folder(DbName, Sleep, Files).
-spec do_revise_docs_from_folder/3 :: (ne_binary(), boolean(), [ne_binary() | nonempty_string(),...]) -> 'ok'.
do_revise_docs_from_folder(_, _, []) -> ok;
do_revise_docs_from_folder(DbName, Sleep, [H|T]) ->
try
{ok, Bin} = file:read_file(H),
JObj = wh_json:decode(Bin),
Sleep andalso timer:sleep(250),
case lookup_doc_rev(DbName, wh_json:get_value(<<"_id">>, JObj)) of
{ok, Rev} ->
?LOG("update doc from file ~s in ~s", [H, DbName]),
save_doc(DbName, wh_json:set_value(<<"_rev">>, Rev, JObj));
{error, not_found} ->
?LOG("import doc from file ~s in ~s", [H, DbName]),
save_doc(DbName, JObj);
{error, Reason} ->
?LOG("failed to load doc ~s into ~s, ~p", [H, DbName, Reason])
end,
do_revise_docs_from_folder(DbName, Sleep, T)
catch
_:_ ->
do_revise_docs_from_folder(DbName, Sleep, T)
end.
%%--------------------------------------------------------------------
%% @public
%% @doc
%% Detemine if a database exists
%% @end
%%--------------------------------------------------------------------
-spec db_exists/1 :: (ne_binary()) -> boolean().
db_exists(DbName) ->
couch_util:db_exists(get_conn(), DbName).
%%--------------------------------------------------------------------
%% @public
%% @doc
%% Retrieve information regarding all databases
%% @end
%%--------------------------------------------------------------------
-spec db_info/0 :: () -> {'ok', [ne_binary(),...] | []} | {'error', atom()}.
-spec admin_db_info/0 :: () -> {'ok', [ne_binary(),...] | []} | {'error', atom()}.
db_info() ->
couch_util:db_info(get_conn()).
admin_db_info() ->
couch_util:db_info(get_admin_conn()).
%%--------------------------------------------------------------------
%% @public
%% @doc
%% Retrieve information regarding a database
%% @end
%%--------------------------------------------------------------------
-spec db_info/1 :: (ne_binary()) -> {'ok', wh_json:json_object()} | {'error', atom()}.
-spec admin_db_info/1 :: (ne_binary()) -> {'ok', wh_json:json_object()} | {'error', atom()}.
db_info(DbName) ->
couch_util:db_info(get_conn(), DbName).
admin_db_info(DbName) ->
couch_util:db_info(get_admin_conn(), DbName).
%%--------------------------------------------------------------------
%% @public
%% @doc
%% Retrieve information regarding a database design doc
%% @end
%%--------------------------------------------------------------------
-spec design_info/2 :: (ne_binary(), ne_binary()) -> {'ok', wh_json:json_object()} | {'error', atom()}.
-spec admin_design_info/2 :: (ne_binary(), ne_binary()) -> {'ok', wh_json:json_object()} | {'error', atom()}.
design_info(DbName, DesignName) ->
couch_util:design_info(get_conn(), DbName, DesignName).
admin_design_info(DbName, DesignName) ->
couch_util:design_info(get_admin_conn(), DbName, DesignName).
-spec design_compact/2 :: (ne_binary(), ne_binary()) -> boolean().
-spec admin_design_compact/2 :: (ne_binary(), ne_binary()) -> boolean().
design_compact(DbName, Design) ->
couch_util:design_compact(get_conn(), DbName, Design).
admin_design_compact(DbName, Design) ->
couch_util:design_compact(get_admin_conn(), DbName, Design).
-spec db_view_cleanup/1 :: (ne_binary()) -> boolean().
-spec admin_db_view_cleanup/1 :: (ne_binary()) -> boolean().
db_view_cleanup(DbName) ->
couch_util:db_view_cleanup(get_conn(), DbName).
admin_db_view_cleanup(DbName) ->
couch_util:db_view_cleanup(get_admin_conn(), DbName).
%%--------------------------------------------------------------------
%% @public
%% @doc
%% Replicate a DB from one host to another
%%
%% Proplist:
%% [{<<"source">>, <<"http://some.couch.server:5984/source_db">>}
%% ,{<<"target">>, <<"target_db">>}
%%
%% IMPORTANT: Use the atom true, not binary <<"true">> (though it may be changing in couch to allow <<"true">>)
%% ,{<<"create_target">>, true} % optional, creates the DB on target if non-existent
%% ,{<<"continuous">>, true} % optional, continuously update target from source
%% ,{<<"cancel">>, true} % optional, will cancel a replication (one-time or continuous)
%%
%% ,{<<"filter">>, <<"source_design_doc/source_filter_name">>} % optional, filter what documents are sent from source to target
%% ,{<<"query_params">>, {struct, [{<<"key1">>, <<"value1">>}, {<<"key2">>, <<"value2">>}]} } % optional, send params to filter function
%% filter_fun: function(doc, req) -> boolean(); passed K/V pairs in query_params are in req in filter function
%%
%% ,{<<"doc_ids">>, [<<"source_doc_id_1">>, <<"source_doc_id_2">>]} % optional, if you only want specific docs, no need for a filter
%%
%% ,{<<"proxy">>, <<"http://some.proxy.server:12345">>} % optional, if you need to pass the replication via proxy to target
%% https support for proxying is suspect
%% ].
%%
%% If authentication is needed at the source's end:
%% {<<"source">>, <<"http://user:password@some.couch.server:5984/source_db">>}
%%
%% If source or target DB is on the current connection, you can just put the DB name, e.g:
%% [{<<"source">>, <<"source_db">>}, {<<"target">>, <<"target_db">>}, ...]
%% Then you don't have to specify the auth creds (if any) for the connection
%%
%% @end
%%--------------------------------------------------------------------
-spec db_replicate/1 :: (proplist() | wh_json:json_object()) -> {'ok', wh_json:json_object()} | {'error', atom()}.
db_replicate(Prop) when is_list(Prop) ->
db_replicate(wh_json:from_list(Prop));
db_replicate(JObj) ->
couch_util:db_replicate(get_conn(), JObj).
%%--------------------------------------------------------------------
%% @public
%% @doc
%% Detemine if a database exists
%% @end
%%--------------------------------------------------------------------
-spec db_create/1 :: (ne_binary()) -> boolean().
db_create(DbName) ->
db_create(DbName, []).
-spec db_create/2 :: (ne_binary(), couch_util:db_create_options()) -> boolean().
db_create(DbName, Options) ->
couch_util:db_create(get_conn(), DbName, Options).
%%--------------------------------------------------------------------
%% @public
%% @doc
%% Compact a database
%% @end
%%--------------------------------------------------------------------
-spec db_compact/1 :: (ne_binary()) -> boolean().
-spec admin_db_compact/1 :: (ne_binary()) -> boolean().
db_compact(DbName) ->
couch_util:db_compact(get_conn(), DbName).
admin_db_compact(DbName) ->
couch_util:db_compact(get_admin_conn(), DbName).
%%--------------------------------------------------------------------
%% @public
%% @doc
%% Delete a database
%% @end
%%--------------------------------------------------------------------
-spec db_delete/1 :: (ne_binary()) -> boolean().
db_delete(DbName) ->
couch_util:db_delete(get_conn(), DbName).
%%%===================================================================
%%% Document Functions
%%%===================================================================
%%--------------------------------------------------------------------
%% @public
%% @doc
%% open a document given a doc id returns an error tuple or the json
%% @end
%%--------------------------------------------------------------------
-spec open_doc/2 :: (ne_binary(), ne_binary()) -> {'ok', wh_json:json_object()} | {'error', 'not_found' | 'db_not_reachable'}.
-spec open_doc/3 :: (ne_binary(), ne_binary(), proplist()) -> {'ok', wh_json:json_object()} | {'error', 'not_found' | 'db_not_reachable'}.
open_doc(DbName, DocId) ->
open_doc(DbName, DocId, []).
open_doc(DbName, DocId, Options) ->
couch_util:open_doc(get_conn(), DbName, DocId, Options).
-spec all_docs/1 :: (ne_binary()) -> {'ok', wh_json:json_objects()} | {'error', atom()}.
-spec admin_all_docs/1 :: (ne_binary()) -> {'ok', wh_json:json_objects()} | {'error', atom()}.
all_docs(DbName) ->
couch_util:all_docs(get_conn(), DbName, []).
admin_all_docs(DbName) ->
couch_util:all_docs(get_admin_conn(), DbName, []).
-spec all_docs/2 :: (ne_binary(), proplist()) -> {'ok', wh_json:json_objects()} | {'error', atom()}.
-spec admin_all_docs/2 :: (ne_binary(), proplist()) -> {'ok', wh_json:json_objects()} | {'error', atom()}.
all_docs(DbName, Options) ->
couch_util:all_docs(get_conn(), DbName, Options).
admin_all_docs(DbName, Options) ->
couch_util:all_docs(get_admin_conn(), DbName, Options).
-spec all_design_docs/1 :: (ne_binary()) -> {'ok', wh_json:json_objects()} | {'error', atom()}.
all_design_docs(DbName) ->
couch_util:all_design_docs(get_conn(), DbName, []).
-spec all_design_docs/2 :: (ne_binary(), proplist()) -> {'ok', wh_json:json_objects()} | {'error', atom()}.
all_design_docs(DbName, Options) ->
couch_util:all_design_docs(get_conn(), DbName, Options).
%%--------------------------------------------------------------------
%% @public
%% @doc
%% get the revision of a document (much faster than requesting the whole document)
%% @end
%%--------------------------------------------------------------------
-spec lookup_doc_rev/2 :: (ne_binary(), ne_binary()) -> {'error', atom()} | {'ok', ne_binary()}.
lookup_doc_rev(DbName, DocId) ->
couch_util:lookup_doc_rev(get_conn(), DbName, DocId).
%%--------------------------------------------------------------------
%% @public
%% @doc
%% save document to the db
%% @end
%%--------------------------------------------------------------------
-spec save_doc/2 :: (ne_binary(), wh_json:json_object() | wh_json:json_objects()) -> {'ok', wh_json:json_object() | wh_json:json_objects()} | {'error', atom()}.
save_doc(DbName, Docs) when is_list(Docs) ->
save_docs(DbName, Docs, []);
save_doc(DbName, Doc) ->
save_doc(DbName, Doc, []).
%% save a document; if it fails to save because of conflict, pull the latest revision and try saving again.
%% any other error is returned
-spec ensure_saved/2 :: (ne_binary(), wh_json:json_object()) -> {'ok', wh_json:json_object()} | {'error', atom()}.
-spec ensure_saved/3 :: (ne_binary(), wh_json:json_object(), proplist()) -> {'ok', wh_json:json_object()} | {'error', atom()}.
ensure_saved(DbName, Doc) ->
ensure_saved(DbName, Doc, []).
ensure_saved(DbName, Doc, Options) ->
couch_util:ensure_saved(get_conn(), DbName, Doc, Options).
-spec save_doc/3 :: (ne_binary(), wh_json:json_object(), proplist()) -> {'ok', wh_json:json_object()} | {'error', atom()}.
save_doc(DbName, Doc, Opts) ->
couch_util:save_doc(get_conn(), DbName, Doc, Opts).
-spec save_docs/2 :: (ne_binary(), wh_json:json_objects()) -> {'ok', wh_json:json_objects()}.
-spec save_docs/3 :: (ne_binary(), wh_json:json_objects(), proplist()) -> {'ok', wh_json:json_objects()}.
save_docs(DbName, Docs) when is_list(Docs) ->
save_docs(DbName, Docs, []).
save_docs(DbName, Docs, Opts) when is_list(Docs) ->
couch_util:save_docs(get_conn(), DbName, Docs, Opts).
%%--------------------------------------------------------------------
%% @public
%% @doc
%% remove document from the db
%% @end
%%--------------------------------------------------------------------
-spec del_doc/2 :: (ne_binary(), wh_json:json_object() | ne_binary()) -> {'ok', wh_json:json_object()} | {'error', atom()}.
del_doc(DbName, Doc) ->
couch_util:del_doc(get_conn(), DbName, Doc).
%%--------------------------------------------------------------------
%% @public
%% @doc
%% remove documents from the db
%% @end
%%--------------------------------------------------------------------
-spec del_docs/2 :: (ne_binary(), wh_json:json_objects()) -> {'ok', wh_json:json_objects()} | {'error', atom()}.
del_docs(DbName, Docs) ->
couch_util:del_docs(get_conn(), DbName, Docs).
%%%===================================================================
%%% Attachment Functions
%%%===================================================================
-spec fetch_attachment/3 :: (ne_binary(), ne_binary(), ne_binary()) -> {'ok', ne_binary()} | {'error', atom()}.
fetch_attachment(DbName, DocId, AName) ->
couch_util:fetch_attachment(get_conn(), DbName, DocId, AName).
-spec put_attachment/4 :: (ne_binary(), ne_binary(), ne_binary(), ne_binary()) -> {'ok', wh_json:json_object()} | {'error', atom()}.
%% Options = [ {'content_type', Type}, {'content_length', Len}, {'rev', Rev}] <- note atoms as keys in proplist
-spec put_attachment/5 :: (ne_binary(), ne_binary(), ne_binary(), ne_binary(), proplist()) -> {'ok', wh_json:json_object()} | {'error', atom()}.
put_attachment(DbName, DocId, AName, Contents) ->
couch_util:put_attachment(get_conn(), DbName, DocId, AName, Contents).
put_attachment(DbName, DocId, AName, Contents, Options) ->
couch_util:put_attachment(get_conn(), DbName, DocId, AName, Contents, Options).
-spec delete_attachment/3 :: (ne_binary(), ne_binary(), ne_binary()) -> {'ok', wh_json:json_object()} | {'error', atom()}.
-spec delete_attachment/4 :: (ne_binary(), ne_binary(), ne_binary(), proplist()) -> {'ok', wh_json:json_object()} | {'error', atom()}.
delete_attachment(DbName, DocId, AName) ->
couch_util:delete_attachment(get_conn(), DbName, DocId, AName).
delete_attachment(DbName, DocId, AName, Options) ->
couch_util:delete_attachment(get_conn(), DbName, DocId, AName, Options).
%%%===================================================================
%%% View Functions
%%%===================================================================
%%--------------------------------------------------------------------
%% @public
%% @doc
%% get the results of the view
%% {Total, Offset, Meta, Rows}
%% @end
%%--------------------------------------------------------------------
-spec get_all_results/2 :: (ne_binary(), ne_binary()) -> {'ok', wh_json:json_objects() | [ne_binary(),...]} | {'error', atom()}.
-spec get_results/3 :: (ne_binary(), ne_binary(), proplist()) -> {'ok', wh_json:json_objects() | [ne_binary(),...]} | {'error', atom()}.
get_all_results(DbName, DesignDoc) ->
get_results(DbName, DesignDoc, []).
get_results(DbName, DesignDoc, ViewOptions) ->
couch_util:get_results(get_conn(), DbName, DesignDoc, ViewOptions).
-spec get_result_keys/1 :: (wh_json:json_objects()) -> [wh_json:json_string(),...] | [].
get_result_keys(JObjs) ->
lists:map(fun get_keys/1, JObjs).
-spec get_keys/1 :: (wh_json:json_object()) -> wh_json:json_string().
get_keys(JObj) ->
wh_json:get_value(<<"key">>, JObj).
%%%===================================================================
%%% API
%%%===================================================================
%%--------------------------------------------------------------------
%% @doc
%% Starts the server
%%
%% @spec start_link() -> {ok, Pid} | ignore | {error, Error}
%% @end
%%--------------------------------------------------------------------
-spec start_link/0 :: () -> startlink_ret().
start_link() ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
%% set the host to connect to
-spec set_host/1 :: (string()) -> 'ok' | {'error', term()}.
set_host(HostName) ->
set_host(HostName, ?DEFAULT_PORT, "", "", ?DEFAULT_ADMIN_PORT).
-spec set_host/2 :: (string(), non_neg_integer()) -> 'ok' | {'error', term()}.
set_host(HostName, Port) ->
set_host(HostName, Port, "", "", ?DEFAULT_ADMIN_PORT).
-spec set_host/3 :: (string(), string(), string()) -> 'ok' | {'error', term()}.
set_host(HostName, UserName, Password) ->
set_host(HostName, ?DEFAULT_PORT, UserName, Password, ?DEFAULT_ADMIN_PORT).
-spec set_host/4 :: (string(), non_neg_integer(), string(), string()) -> 'ok' | {'error', term()}.
set_host(HostName, Port, UserName, Password) ->
set_host(HostName, Port, UserName, Password, ?DEFAULT_ADMIN_PORT).
-spec set_host/5 :: (string(), non_neg_integer(), string(), string(), non_neg_integer()) -> 'ok' | {'error', term()}.
set_host(HostName, Port, UserName, Password, AdminPort) ->
gen_server:call(?SERVER, {set_host, HostName, Port, UserName, Password, AdminPort}, infinity).
get_host() ->
gen_server:call(?SERVER, get_host).
get_port() ->
gen_server:call(?SERVER, get_port).
get_admin_port() ->
gen_server:call(?SERVER, get_admin_port).
get_creds() ->
gen_server:call(?SERVER, get_creds).
get_conn() ->
case whereis(?SERVER) of
Srv when is_pid(Srv) ->
gen_server:call(?SERVER, get_conn);
_E ->
?LOG("no server by the name of ~s", [?SERVER]),
ST = erlang:get_stacktrace(),
?LOG_STACKTRACE(ST)
end.
get_admin_conn() ->
gen_server:call(?SERVER, get_admin_conn).
-spec get_uuid/0 :: () -> binary().
get_uuid() ->
[UUID] = couchbeam:get_uuid(get_conn()),
wh_util:to_binary(UUID).
-spec get_uuids/1 :: (pos_integer()) -> [ne_binary(),...].
get_uuids(Count) ->
Conn = get_conn(),
[wh_util:to_binary(UUID) || UUID <- couchbeam:get_uuids(Conn, Count)].
-spec get_node_cookie/0 :: () -> atom().
get_node_cookie() ->
couch_config:fetch(bigcouch_cookie, monster).
-spec set_node_cookie/1 :: (atom()) -> 'ok'.
set_node_cookie(Cookie) when is_atom(Cookie) ->
couch_config:store(bigcouch_cookie, Cookie).
-spec get_url/0 :: () -> ne_binary() | 'undefined'.
-spec get_admin_url/0 :: () -> ne_binary() | 'undefined'.
get_url() ->
get_url(wh_util:to_binary(get_host()), get_creds(), get_port()).
get_admin_url() ->
get_url(wh_util:to_binary(get_host()), get_creds(), get_admin_port()).
-spec get_url/3 :: (binary(), {string(), string()}, pos_integer()) -> ne_binary() | 'undefined'.
get_url(<<>>, _, _) -> 'undefined';
get_url(H, {[], []}, P) ->
list_to_binary(["http://", H, ":", wh_util:to_binary(P), "/"]);
get_url(H, {User, Pwd}, P) ->
list_to_binary(["http://", wh_util:to_binary(User), ":", wh_util:to_binary(Pwd)
,"@", H, ":", wh_util:to_binary(P), "/"
]).
add_change_handler(DBName, DocID) ->
?LOG_SYS("add change handler for DB: ~s and Doc: ~s", [DBName, DocID]),
gen_server:cast(?SERVER, {add_change_handler, wh_util:to_binary(DBName), wh_util:to_binary(DocID), self()}).
add_change_handler(DBName, DocID, Pid) ->
?LOG_SYS("add change handler for Pid: ~p for DB: ~s and Doc: ~s", [Pid, DBName, DocID]),
gen_server:cast(?SERVER, {add_change_handler, wh_util:to_binary(DBName), wh_util:to_binary(DocID), Pid}).
rm_change_handler(DBName, DocID) ->
?LOG_SYS("rm change handler for DB: ~s and Doc: ~s", [DBName, DocID]),
gen_server:call(?SERVER, {rm_change_handler, wh_util:to_binary(DBName), wh_util:to_binary(DocID)}).
%%%===================================================================
%%% gen_server callbacks
%%%===================================================================
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Initializes the server
%%
%% @spec init(Args) -> {ok, State} |
%% {ok, State, Timeout} |
%% ignore |
%% {stop, Reason}
%% @end
%%--------------------------------------------------------------------
-spec init/1 :: ([]) -> {'ok', #state{}}.
init([]) ->
process_flag(trap_exit, true),
?LOG("starting couch_mgr"),
{ok, init_state()}.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling call messages
%%
%% @spec handle_call(Request, From, State) ->
%% {reply, Reply, State} |
%% {reply, Reply, State, Timeout} |
%% {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, Reply, State} |
%% {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
handle_call(get_host, _From, #state{host={H,_,_}}=State) ->
{reply, H, State};
handle_call(get_port, _From, #state{host={_,P,_}}=State) ->
{reply, P, State};
handle_call(get_admin_port, _From, #state{host={_,_,P}}=State) ->
{reply, P, State};
handle_call({set_host, Host, Port, User, Pass, AdminPort}, _From, #state{host={OldHost,_,_}}=State) ->
?LOG_SYS("updating host from ~p to ~p", [OldHost, Host]),
Conn = couch_util:get_new_connection(Host, Port, User, Pass),
AdminConn = couch_util:get_new_connection(Host, AdminPort, User, Pass),
couch_config:store(couch_config, {Host, Port, User, Pass, AdminPort}),
spawn(fun() -> save_config() end),
{reply, ok, State#state{host={Host, Port, AdminPort}
,connection=Conn
,admin_connection=AdminConn
,change_handlers=dict:new()
,creds={User,Pass}
}};
handle_call({set_host, Host, Port, User, Pass, AdminPort}, _From, State) ->
?LOG_SYS("setting host for the first time to ~p", [Host]),
Conn = couch_util:get_new_connection(Host, Port, User, Pass),
AdminConn = couch_util:get_new_connection(Host, AdminPort, User, Pass),
couch_config:store(couch_config, {Host, Port, User, Pass, AdminPort}),
spawn(fun() -> save_config() end),
{reply, ok, State#state{host={Host,Port,AdminPort}
,connection=Conn
,admin_connection=AdminConn
,change_handlers=dict:new()
,creds={User,Pass}
}};
handle_call(get_conn, _, #state{connection=S}=State) ->
{reply, S, State};
handle_call(get_admin_conn, _, #state{admin_connection=ACon}=State) ->
{reply, ACon, State};
handle_call(get_creds, _, #state{creds=Cred}=State) ->
{reply, Cred, State};
handle_call({rm_change_handler, DBName, DocID}, {Pid, _Ref}, #state{change_handlers=CH}=State) ->
spawn(fun() ->
{ok, {Srv, _}} = dict:find(DBName, CH),
?LOG_SYS("found CH(~p): Rm listener(~p) for db:doc ~s:~s", [Srv, Pid, DBName, DocID]),
change_handler:rm_listener(Srv, Pid, DocID)
end),
{reply, ok, State}.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling cast messages
%%
%% @spec handle_cast(Msg, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
handle_cast({add_change_handler, DBName, DocID, Pid}, #state{change_handlers=CH, connection=S}=State) ->
case dict:find(DBName, CH) of
{ok, {Srv, _}} ->
?LOG_SYS("found CH(~p): Adding listener(~p) for db:doc ~s:~s", [Srv, Pid, DBName, DocID]),
change_handler:add_listener(Srv, Pid, DocID),
{noreply, State};
error ->
{ok, Srv} = change_handler:start_link(couch_util:get_db(S, DBName), []),
?LOG_SYS("started CH(~p): added listener(~p) for db:doc ~s:~s", [Srv, Pid, DBName, DocID]),
SrvRef = erlang:monitor(process, Srv),
change_handler:add_listener(Srv, Pid, DocID),
{noreply, State#state{change_handlers=dict:store(DBName, {Srv, SrvRef}, CH)}}
end.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling all non call/cast messages
%%
%% @spec handle_info(Info, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
handle_info({'DOWN', Ref, process, Srv, complete}, #state{change_handlers=CH}=State) ->
?LOG_SYS("srv ~p down after complete", [Srv]),
erlang:demonitor(Ref, [flush]),
{noreply, State#state{change_handlers=remove_ref(Ref, CH)}};
handle_info({'DOWN', Ref, process, Srv, {error,connection_closed}}, #state{change_handlers=CH}=State) ->
?LOG_SYS("srv ~p down after conn closed", [Srv]),
erlang:demonitor(Ref, [flush]),
{noreply, State#state{change_handlers=remove_ref(Ref, CH)}};
handle_info(_Info, State) ->
?LOG_SYS("unexpected message ~p", [_Info]),
{noreply, State}.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% This function is called by a gen_server when it is about to
%% terminate. It should be the opposite of Module:init/1 and do any
%% necessary cleaning up. When it returns, the gen_server terminates
%% with Reason. The return value is ignored.
%%
%% @spec terminate(Reason, State) -> void()
%% @end
%%--------------------------------------------------------------------
terminate(_Reason, _State) ->
ok.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Convert process state when code is changed
%%
%% @spec code_change(OldVsn, State, Extra) -> {ok, NewState}
%% @end
%%--------------------------------------------------------------------
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%%%===================================================================
%%% Internal functions
%%%===================================================================
%%--------------------------------------------------------------------
%% @private
%% @doc
%% @end
%%--------------------------------------------------------------------
-spec init_state/0 :: () -> #state{}.
init_state() ->
case couch_config:fetch(couch_host) of
undefined ->
?LOG("starting conns with default_couch_host"),
init_state_from_config(couch_config:fetch(default_couch_host));
HostData ->
?LOG("starting conns with couch_host"),
init_state_from_config(HostData)
end.
init_state_from_config(undefined) ->
init_state_from_config({"localhost", ?DEFAULT_PORT, "", "", ?DEFAULT_ADMIN_PORT});
init_state_from_config(null) ->
init_state_from_config({"localhost", ?DEFAULT_PORT, "", "", ?DEFAULT_ADMIN_PORT});
init_state_from_config(H) when not is_tuple(H) ->
init_state_from_config({H, ?DEFAULT_PORT, "", "", ?DEFAULT_ADMIN_PORT});
init_state_from_config({H, Port}) ->
init_state_from_config({H, Port, "", "", ?DEFAULT_ADMIN_PORT});
init_state_from_config({H, User, Pass}) ->
init_state_from_config({H, ?DEFAULT_PORT, User, Pass, ?DEFAULT_ADMIN_PORT});
init_state_from_config({H, Port, User, Pass}) ->
init_state_from_config({H, Port, User, Pass, ?DEFAULT_ADMIN_PORT});
init_state_from_config({H, Port, User, Pass, AdminPort}) ->
Conn = couch_util:get_new_connection(H, wh_util:to_integer(Port), User, Pass),
AdminConn = couch_util:get_new_connection(H, wh_util:to_integer(AdminPort), User, Pass),
?LOG_SYS("returning state record"),
couch_config:ready(),
#state{connection=Conn
,admin_connection=AdminConn
,host={H, wh_util:to_integer(Port), wh_util:to_integer(AdminPort)}
,creds={User, Pass}
}.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% @end
%%--------------------------------------------------------------------
-spec save_config/0 :: () -> 'ok'.
save_config() ->
couch_config:write_config(?CONFIG_FILE_PATH).
-spec remove_ref/2 :: (reference(), dict()) -> dict().
remove_ref(Ref, CH) ->
dict:filter(fun(_, {_, Ref1}) when Ref1 =:= Ref -> false;
(_, _) -> true end, CH).