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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
|
% Licensed under the Apache License, Version 2.0 (the "License"); you may not
% use this file except in compliance with the License. You may obtain a copy of
% the License at
%
% http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
% License for the specific language governing permissions and limitations under
% the License.
-module(fabric2_db).
-export([
create/2,
open/2,
delete/2,
list_dbs/0,
list_dbs/1,
list_dbs/3,
list_dbs_info/0,
list_dbs_info/1,
list_dbs_info/3,
check_is_admin/1,
check_is_member/1,
name/1,
get_after_doc_read_fun/1,
get_before_doc_update_fun/1,
get_committed_update_seq/1,
get_compacted_seq/1,
get_compactor_pid/1,
get_db_info/1,
%% get_partition_info/2,
get_del_doc_count/1,
get_doc_count/1,
get_doc_count/2,
%% get_epochs/1,
%% get_filepath/1,
get_instance_start_time/1,
get_pid/1,
get_revs_limit/1,
get_security/1,
get_update_seq/1,
get_user_ctx/1,
get_uuid/1,
%% get_purge_seq/1,
%% get_oldest_purge_seq/1,
%% get_purge_infos_limit/1,
is_clustered/1,
is_db/1,
is_partitioned/1,
is_system_db/1,
is_system_db_name/1,
is_replicator_db/1,
is_users_db/1,
set_revs_limit/2,
%% set_purge_infos_limit/2,
set_security/2,
set_user_ctx/2,
ensure_full_commit/1,
ensure_full_commit/2,
%% load_validation_funs/1,
%% reload_validation_funs/1,
open_doc/2,
open_doc/3,
open_doc_revs/4,
%% open_doc_int/3,
get_doc_info/2,
get_full_doc_info/2,
get_full_doc_infos/2,
get_missing_revs/2,
%% get_design_doc/2,
%% get_design_docs/1,
%% get_design_doc_count/1,
%% get_purge_infos/2,
%% get_minimum_purge_seq/1,
%% purge_client_exists/3,
validate_docid/1,
%% doc_from_json_obj_validate/2,
update_doc/2,
update_doc/3,
update_docs/2,
update_docs/3,
%% delete_doc/3,
%% purge_docs/2,
%% purge_docs/3,
read_attachment/3,
write_attachment/3,
fold_docs/3,
fold_docs/4,
fold_design_docs/4,
fold_local_docs/4,
fold_changes/4,
fold_changes/5,
%% count_changes_since/2,
%% fold_purge_infos/4,
%% fold_purge_infos/5,
%% calculate_start_seq/3,
%% owner_of/2,
%% start_compact/1,
%% cancel_compact/1,
%% wait_for_compaction/1,
%% wait_for_compaction/2,
dbname_suffix/1,
normalize_dbname/1,
validate_dbname/1,
%% make_doc/5,
new_revid/2
]).
-include_lib("couch/include/couch_db.hrl").
-include("fabric2.hrl").
% Default max database name length is based on CouchDb < 4.x compatibility. See
% default.ini entry for additional information.
-define(DEFAULT_MAX_DATABASE_NAME_LENGTH, 238).
-define(DBNAME_REGEX,
"^[a-z][a-z0-9\\_\\$()\\+\\-\\/]*" % use the stock CouchDB regex
"(\\.[0-9]{10,})?$" % but allow an optional shard timestamp at the end
).
-define(FIRST_DDOC_KEY, <<"_design/">>).
-define(LAST_DDOC_KEY, <<"_design0">>).
-define(RETURN(Term), throw({?MODULE, Term})).
create(DbName, Options) ->
case validate_dbname(DbName) of
ok ->
Result = fabric2_fdb:transactional(DbName, Options, fun(TxDb) ->
case fabric2_fdb:exists(TxDb) of
true ->
{error, file_exists};
false ->
fabric2_fdb:create(TxDb, Options)
end
end),
% We cache outside of the transaction so that we're sure
% that the transaction was committed.
case Result of
#{} = Db0 ->
Db1 = maybe_add_sys_db_callbacks(Db0),
ok = fabric2_server:store(Db1),
{ok, Db1#{tx := undefined}};
Error ->
Error
end;
Error ->
Error
end.
open(DbName, Options) ->
case fabric2_server:fetch(DbName) of
#{} = Db ->
Db1 = maybe_set_user_ctx(Db, Options),
{ok, require_member_check(Db1)};
undefined ->
Result = fabric2_fdb:transactional(DbName, Options, fun(TxDb) ->
fabric2_fdb:open(TxDb, Options)
end),
% Cache outside the transaction retry loop
case Result of
#{} = Db0 ->
Db1 = maybe_add_sys_db_callbacks(Db0),
ok = fabric2_server:store(Db1),
Db2 = Db1#{tx := undefined},
{ok, require_member_check(Db2)};
Error ->
Error
end
end.
delete(DbName, Options) ->
% Delete doesn't check user_ctx, that's done at the HTTP API level
% here we just care to get the `database_does_not_exist` error thrown
Options1 = lists:keystore(user_ctx, 1, Options, ?ADMIN_CTX),
{ok, Db} = open(DbName, Options1),
Resp = fabric2_fdb:transactional(Db, fun(TxDb) ->
fabric2_fdb:delete(TxDb)
end),
if Resp /= ok -> Resp; true ->
fabric2_server:remove(DbName)
end.
list_dbs() ->
list_dbs([]).
list_dbs(Options) ->
Callback = fun(DbName, Acc) -> [DbName | Acc] end,
DbNames = fabric2_fdb:transactional(fun(Tx) ->
fabric2_fdb:list_dbs(Tx, Callback, [], Options)
end),
lists:reverse(DbNames).
list_dbs(UserFun, UserAcc0, Options) ->
FoldFun = fun
(DbName, Acc) -> maybe_stop(UserFun({row, [{id, DbName}]}, Acc))
end,
fabric2_fdb:transactional(fun(Tx) ->
try
UserAcc1 = maybe_stop(UserFun({meta, []}, UserAcc0)),
UserAcc2 = fabric2_fdb:list_dbs(
Tx,
FoldFun,
UserAcc1,
Options
),
{ok, maybe_stop(UserFun(complete, UserAcc2))}
catch throw:{stop, FinalUserAcc} ->
{ok, FinalUserAcc}
end
end).
list_dbs_info() ->
list_dbs_info([]).
list_dbs_info(Options) ->
Callback = fun(Value, Acc) ->
NewAcc = case Value of
{meta, _} -> Acc;
{row, DbInfo} -> [DbInfo | Acc];
complete -> Acc
end,
{ok, NewAcc}
end,
{ok, DbInfos} = list_dbs_info(Callback, [], Options),
{ok, lists:reverse(DbInfos)}.
list_dbs_info(UserFun, UserAcc0, Options) ->
FoldFun = fun(DbName, InfoFuture, {FutureQ, Count, Acc}) ->
NewFutureQ = queue:in({DbName, InfoFuture}, FutureQ),
drain_info_futures(NewFutureQ, Count + 1, UserFun, Acc)
end,
fabric2_fdb:transactional(fun(Tx) ->
try
UserAcc1 = maybe_stop(UserFun({meta, []}, UserAcc0)),
InitAcc = {queue:new(), 0, UserAcc1},
{FinalFutureQ, _, UserAcc2} = fabric2_fdb:list_dbs_info(
Tx,
FoldFun,
InitAcc,
Options
),
UserAcc3 = drain_all_info_futures(FinalFutureQ, UserFun, UserAcc2),
{ok, maybe_stop(UserFun(complete, UserAcc3))}
catch throw:{stop, FinalUserAcc} ->
{ok, FinalUserAcc}
end
end).
is_admin(Db, {SecProps}) when is_list(SecProps) ->
case fabric2_db_plugin:check_is_admin(Db) of
true ->
true;
false ->
UserCtx = get_user_ctx(Db),
{Admins} = get_admins(SecProps),
is_authorized(Admins, UserCtx)
end.
check_is_admin(Db) ->
check_is_admin(Db, get_security(Db)).
check_is_admin(Db, SecDoc) ->
case is_admin(Db, SecDoc) of
true ->
ok;
false ->
UserCtx = get_user_ctx(Db),
Reason = <<"You are not a db or server admin.">>,
throw_security_error(UserCtx, Reason)
end.
check_is_member(Db) ->
check_is_member(Db, get_security(Db)).
check_is_member(Db, SecDoc) ->
case is_member(Db, SecDoc) of
true ->
ok;
false ->
UserCtx = get_user_ctx(Db),
throw_security_error(UserCtx)
end.
require_admin_check(#{} = Db) ->
Db#{security_fun := fun check_is_admin/2}.
require_member_check(#{} = Db) ->
Db#{security_fun := fun check_is_member/2}.
name(#{name := DbName}) ->
DbName.
get_after_doc_read_fun(#{after_doc_read := AfterDocRead}) ->
AfterDocRead.
get_before_doc_update_fun(#{before_doc_update := BeforeDocUpdate}) ->
BeforeDocUpdate.
get_committed_update_seq(#{} = Db) ->
get_update_seq(Db).
get_compacted_seq(#{} = Db) ->
get_update_seq(Db).
get_compactor_pid(#{} = _Db) ->
nil.
get_db_info(#{} = Db) ->
DbProps = fabric2_fdb:transactional(Db, fun(TxDb) ->
fabric2_fdb:get_info(TxDb)
end),
{ok, make_db_info(name(Db), DbProps)}.
get_del_doc_count(#{} = Db) ->
get_doc_count(Db, <<"doc_del_count">>).
get_doc_count(Db) ->
get_doc_count(Db, <<"doc_count">>).
get_doc_count(Db, undefined) ->
get_doc_count(Db, <<"doc_count">>);
get_doc_count(Db, <<"_all_docs">>) ->
get_doc_count(Db, <<"doc_count">>);
get_doc_count(DbName, <<"_design">>) ->
get_doc_count(DbName, <<"doc_design_count">>);
get_doc_count(DbName, <<"_local">>) ->
get_doc_count(DbName, <<"doc_local_count">>);
get_doc_count(Db, Key) ->
fabric2_fdb:transactional(Db, fun(TxDb) ->
fabric2_fdb:get_stat(TxDb, Key)
end).
get_instance_start_time(#{}) ->
0.
get_pid(#{}) ->
nil.
get_revs_limit(#{} = Db) ->
#{revs_limit := RevsLimit} = fabric2_fdb:transactional(Db, fun(TxDb) ->
fabric2_fdb:ensure_current(TxDb)
end),
RevsLimit.
get_security(#{} = Db) ->
#{security_doc := SecDoc} = fabric2_fdb:transactional(Db, fun(TxDb) ->
fabric2_fdb:ensure_current(TxDb)
end),
SecDoc.
get_update_seq(#{} = Db) ->
fabric2_fdb:transactional(Db, fun(TxDb) ->
fabric2_fdb:get_last_change(TxDb)
end).
get_user_ctx(#{user_ctx := UserCtx}) ->
UserCtx.
get_uuid(#{uuid := UUID}) ->
UUID.
is_clustered(#{}) ->
false.
is_db(#{name := _}) ->
true;
is_db(_) ->
false.
is_partitioned(#{}) ->
false.
is_system_db(#{name := DbName}) ->
is_system_db_name(DbName).
is_system_db_name(DbName) when is_list(DbName) ->
is_system_db_name(?l2b(DbName));
is_system_db_name(DbName) when is_binary(DbName) ->
Suffix = filename:basename(DbName),
case {filename:dirname(DbName), lists:member(Suffix, ?SYSTEM_DATABASES)} of
{<<".">>, Result} -> Result;
{_Prefix, false} -> false;
{Prefix, true} ->
ReOpts = [{capture,none}, dollar_endonly],
re:run(Prefix, ?DBNAME_REGEX, ReOpts) == match
end.
is_replicator_db(#{name := DbName}) ->
is_replicator_db(DbName);
is_replicator_db(DbName) when is_binary(DbName) ->
fabric2_util:dbname_ends_with(DbName, <<"_replicator">>).
is_users_db(#{name := DbName}) ->
is_users_db(DbName);
is_users_db(DbName) when is_binary(DbName) ->
AuthenticationDb = config:get("chttpd_auth", "authentication_db"),
CfgUsersSuffix = config:get("couchdb", "users_db_suffix", "_users"),
IsAuthCache = if AuthenticationDb == undefined -> false; true ->
DbName == ?l2b(AuthenticationDb)
end,
IsCfgUsersDb = fabric2_util:dbname_ends_with(DbName, ?l2b(CfgUsersSuffix)),
IsGlobalUsersDb = fabric2_util:dbname_ends_with(DbName, <<"_users">>),
IsAuthCache orelse IsCfgUsersDb orelse IsGlobalUsersDb.
set_revs_limit(#{} = Db0, RevsLimit) when is_integer(RevsLimit) ->
Db1 = require_admin_check(Db0),
Resp = fabric2_fdb:transactional(Db1, fun(TxDb) ->
fabric2_fdb:set_config(TxDb, revs_limit, RevsLimit)
end),
case Resp of
{ok, #{} = Db2} -> fabric2_server:store(Db2);
Err -> Err
end.
set_security(#{} = Db0, Security) ->
Db1 = require_admin_check(Db0),
ok = fabric2_util:validate_security_object(Security),
Resp = fabric2_fdb:transactional(Db1, fun(TxDb) ->
fabric2_fdb:set_config(TxDb, security_doc, Security)
end),
case Resp of
{ok, #{} = Db2} -> fabric2_server:store(Db2);
Err -> Err
end.
set_user_ctx(#{} = Db, UserCtx) ->
Db#{user_ctx := UserCtx}.
ensure_full_commit(#{}) ->
{ok, 0}.
ensure_full_commit(#{}, _Timeout) ->
{ok, 0}.
open_doc(#{} = Db, DocId) ->
open_doc(Db, DocId, []).
open_doc(#{} = Db, <<?LOCAL_DOC_PREFIX, _/binary>> = DocId, _Options) ->
fabric2_fdb:transactional(Db, fun(TxDb) ->
case fabric2_fdb:get_local_doc(TxDb, DocId) of
#doc{} = Doc -> {ok, Doc};
Else -> Else
end
end);
open_doc(#{} = Db, DocId, Options) ->
NeedsTreeOpts = [revs_info, conflicts, deleted_conflicts],
NeedsTree = (Options -- NeedsTreeOpts /= Options),
OpenDeleted = lists:member(deleted, Options),
fabric2_fdb:transactional(Db, fun(TxDb) ->
Revs = case NeedsTree of
true -> fabric2_fdb:get_all_revs(TxDb, DocId);
false -> fabric2_fdb:get_winning_revs(TxDb, DocId, 1)
end,
if Revs == [] -> {not_found, missing}; true ->
#{winner := true} = RI = lists:last(Revs),
case fabric2_fdb:get_doc_body(TxDb, DocId, RI) of
#doc{deleted = true} when not OpenDeleted ->
{not_found, deleted};
#doc{} = Doc ->
apply_open_doc_opts(Doc, Revs, Options);
Else ->
Else
end
end
end).
open_doc_revs(Db, DocId, Revs, Options) ->
Latest = lists:member(latest, Options),
fabric2_fdb:transactional(Db, fun(TxDb) ->
AllRevInfos = fabric2_fdb:get_all_revs(TxDb, DocId),
RevTree = lists:foldl(fun(RI, TreeAcc) ->
RIPath = fabric2_util:revinfo_to_path(RI),
{Merged, _} = couch_key_tree:merge(TreeAcc, RIPath),
Merged
end, [], AllRevInfos),
{Found, Missing} = case Revs of
all ->
{couch_key_tree:get_all_leafs(RevTree), []};
_ when Latest ->
couch_key_tree:get_key_leafs(RevTree, Revs);
_ ->
couch_key_tree:get(RevTree, Revs)
end,
Docs = lists:map(fun({Value, {Pos, [Rev | RevPath]}}) ->
case Value of
?REV_MISSING ->
% We have the rev in our list but know nothing about it
{{not_found, missing}, {Pos, Rev}};
_ ->
RevInfo = #{
rev_id => {Pos, Rev},
rev_path => RevPath
},
case fabric2_fdb:get_doc_body(TxDb, DocId, RevInfo) of
#doc{} = Doc ->
apply_open_doc_opts(Doc, AllRevInfos, Options);
Else ->
{Else, {Pos, Rev}}
end
end
end, Found),
MissingDocs = [{{not_found, missing}, MRev} || MRev <- Missing],
{ok, Docs ++ MissingDocs}
end).
get_doc_info(Db, DocId) ->
case get_full_doc_info(Db, DocId) of
not_found -> not_found;
FDI -> couch_doc:to_doc_info(FDI)
end.
get_full_doc_info(Db, DocId) ->
RevInfos = fabric2_fdb:transactional(Db, fun(TxDb) ->
fabric2_fdb:get_all_revs(TxDb, DocId)
end),
if RevInfos == [] -> not_found; true ->
#{winner := true} = Winner = lists:last(RevInfos),
RevTree = lists:foldl(fun(RI, TreeAcc) ->
RIPath = fabric2_util:revinfo_to_path(RI),
{Merged, _} = couch_key_tree:merge(TreeAcc, RIPath),
Merged
end, [], RevInfos),
#full_doc_info{
id = DocId,
update_seq = fabric2_fdb:vs_to_seq(maps:get(sequence, Winner)),
deleted = maps:get(deleted, Winner),
rev_tree = RevTree
}
end.
get_full_doc_infos(Db, DocIds) ->
fabric2_fdb:transactional(Db, fun(TxDb) ->
lists:map(fun(DocId) ->
get_full_doc_info(TxDb, DocId)
end, DocIds)
end).
get_missing_revs(Db, JsonIdRevs) ->
IdRevs = [idrevs(IdR) || IdR <- JsonIdRevs],
AllRevInfos = fabric2_fdb:transactional(Db, fun(TxDb) ->
lists:foldl(fun({Id, _Revs}, Acc) ->
case maps:is_key(Id, Acc) of
true ->
Acc;
false ->
RevInfos = fabric2_fdb:get_all_revs(TxDb, Id),
Acc#{Id => RevInfos}
end
end, #{}, IdRevs)
end),
AllMissing = lists:flatmap(fun({Id, Revs}) ->
#{Id := RevInfos} = AllRevInfos,
Missing = try
lists:foldl(fun(RevInfo, RevAcc) ->
if RevAcc /= [] -> ok; true ->
throw(all_found)
end,
filter_found_revs(RevInfo, RevAcc)
end, Revs, RevInfos)
catch throw:all_found ->
[]
end,
if Missing == [] -> []; true ->
PossibleAncestors = find_possible_ancestors(RevInfos, Missing),
[{Id, Missing, PossibleAncestors}]
end
end, IdRevs),
{ok, AllMissing}.
validate_docid(<<"">>) ->
throw({illegal_docid, <<"Document id must not be empty">>});
validate_docid(<<"_design/">>) ->
throw({illegal_docid, <<"Illegal document id `_design/`">>});
validate_docid(<<"_local/">>) ->
throw({illegal_docid, <<"Illegal document id `_local/`">>});
validate_docid(Id) when is_binary(Id) ->
MaxLen = case config:get("couchdb", "max_document_id_length", "infinity") of
"infinity" -> infinity;
IntegerVal -> list_to_integer(IntegerVal)
end,
case MaxLen > 0 andalso byte_size(Id) > MaxLen of
true -> throw({illegal_docid, <<"Document id is too long">>});
false -> ok
end,
case couch_util:validate_utf8(Id) of
false -> throw({illegal_docid, <<"Document id must be valid UTF-8">>});
true -> ok
end,
case Id of
<<?DESIGN_DOC_PREFIX, _/binary>> -> ok;
<<?LOCAL_DOC_PREFIX, _/binary>> -> ok;
<<"_", _/binary>> ->
case fabric2_db_plugin:validate_docid(Id) of
true ->
ok;
false ->
throw(
{illegal_docid,
<<"Only reserved document ids may start with underscore.">>})
end;
_Else -> ok
end;
validate_docid(Id) ->
couch_log:debug("Document id is not a string: ~p", [Id]),
throw({illegal_docid, <<"Document id must be a string">>}).
update_doc(Db, Doc) ->
update_doc(Db, Doc, []).
update_doc(Db, Doc, Options) ->
case update_docs(Db, [Doc], Options) of
{ok, [{ok, NewRev}]} ->
{ok, NewRev};
{ok, [{{_Id, _Rev}, Error}]} ->
throw(Error);
{error, [{{_Id, _Rev}, Error}]} ->
throw(Error);
{error, [Error]} ->
throw(Error);
{ok, []} ->
% replication success
{Pos, [RevId | _]} = Doc#doc.revs,
{ok, {Pos, RevId}}
end.
update_docs(Db, Docs) ->
update_docs(Db, Docs, []).
update_docs(Db, Docs0, Options) ->
Docs1 = apply_before_doc_update(Db, Docs0, Options),
try
validate_atomic_update(Docs0, lists:member(all_or_nothing, Options)),
Resps0 = case lists:member(replicated_changes, Options) of
false ->
fabric2_fdb:transactional(Db, fun(TxDb) ->
update_docs_interactive(TxDb, Docs1, Options)
end);
true ->
lists:map(fun(Doc) ->
fabric2_fdb:transactional(Db, fun(TxDb) ->
update_doc_int(TxDb, Doc, Options)
end)
end, Docs1)
end,
% Notify index builder
fabric2_index:db_updated(name(Db)),
% Convert errors
Resps1 = lists:map(fun(Resp) ->
case Resp of
{#doc{} = Doc, Error} ->
#doc{
id = DocId,
revs = Revs
} = Doc,
RevId = case Revs of
{RevPos, [Rev | _]} -> {RevPos, Rev};
{0, []} -> {0, <<>>};
Else -> Else
end,
{{DocId, RevId}, Error};
Else ->
Else
end
end, Resps0),
case lists:member(replicated_changes, Options) of
true ->
{ok, lists:flatmap(fun(R) ->
case R of
{ok, []} -> [];
{{_, _}, {ok, []}} -> [];
Else -> [Else]
end
end, Resps1)};
false ->
{ok, Resps1}
end
catch throw:{aborted, Errors} ->
{aborted, Errors}
end.
read_attachment(Db, DocId, AttId) ->
fabric2_fdb:transactional(Db, fun(TxDb) ->
fabric2_fdb:read_attachment(TxDb, DocId, AttId)
end).
write_attachment(Db, DocId, Att) ->
Data = couch_att:fetch(data, Att),
{ok, AttId} = fabric2_fdb:write_attachment(Db, DocId, Data),
couch_att:store(data, {loc, Db, DocId, AttId}, Att).
fold_docs(Db, UserFun, UserAcc) ->
fold_docs(Db, UserFun, UserAcc, []).
fold_docs(Db, UserFun, UserAcc0, Options) ->
fabric2_fdb:transactional(Db, fun(TxDb) ->
try
#{
db_prefix := DbPrefix
} = TxDb,
Prefix = erlfdb_tuple:pack({?DB_ALL_DOCS}, DbPrefix),
Meta = get_all_docs_meta(TxDb, Options),
UserAcc1 = maybe_stop(UserFun({meta, Meta}, UserAcc0)),
UserAcc2 = fabric2_fdb:fold_range(TxDb, Prefix, fun({K, V}, Acc) ->
{DocId} = erlfdb_tuple:unpack(K, Prefix),
RevId = erlfdb_tuple:unpack(V),
maybe_stop(UserFun({row, [
{id, DocId},
{key, DocId},
{value, {[{rev, couch_doc:rev_to_str(RevId)}]}}
]}, Acc))
end, UserAcc1, Options),
{ok, maybe_stop(UserFun(complete, UserAcc2))}
catch throw:{stop, FinalUserAcc} ->
{ok, FinalUserAcc}
end
end).
fold_design_docs(Db, UserFun, UserAcc0, Options1) ->
Options2 = set_design_doc_keys(Options1),
fold_docs(Db, UserFun, UserAcc0, Options2).
fold_local_docs(Db, UserFun, UserAcc0, Options0) ->
% This is mostly for testing and sanity checking. When calling from a test
% namespace will be automatically set. We also assert when called from the
% API the correct namespace was set
Options = case lists:keyfind(namespace, 1, Options0) of
{namespace, <<"_local">>} -> Options0;
false -> [{namespace, <<"_local">>} | Options0]
end,
fabric2_fdb:transactional(Db, fun(TxDb) ->
try
#{
db_prefix := DbPrefix
} = TxDb,
Prefix = erlfdb_tuple:pack({?DB_LOCAL_DOCS}, DbPrefix),
Meta = get_all_docs_meta(TxDb, Options),
UserAcc1 = maybe_stop(UserFun({meta, Meta}, UserAcc0)),
UserAcc2 = fabric2_fdb:fold_range(TxDb, Prefix, fun({K, V}, Acc) ->
{DocId} = erlfdb_tuple:unpack(K, Prefix),
Rev = fabric2_fdb:get_local_doc_rev(TxDb, DocId, V),
maybe_stop(UserFun({row, [
{id, DocId},
{key, DocId},
{value, {[{rev, couch_doc:rev_to_str({0, Rev})}]}}
]}, Acc))
end, UserAcc1, Options),
{ok, maybe_stop(UserFun(complete, UserAcc2))}
catch throw:{stop, FinalUserAcc} ->
{ok, FinalUserAcc}
end
end).
fold_changes(Db, SinceSeq, UserFun, UserAcc) ->
fold_changes(Db, SinceSeq, UserFun, UserAcc, []).
fold_changes(Db, SinceSeq, UserFun, UserAcc, Options) ->
fabric2_fdb:transactional(Db, fun(TxDb) ->
try
#{
db_prefix := DbPrefix
} = TxDb,
Prefix = erlfdb_tuple:pack({?DB_CHANGES}, DbPrefix),
Dir = case fabric2_util:get_value(dir, Options, fwd) of
rev -> rev;
_ -> fwd
end,
RestartTx = case fabric2_util:get_value(restart_tx, Options) of
undefined -> [{restart_tx, true}];
_AlreadySet -> []
end,
StartKey = get_since_seq(TxDb, Dir, SinceSeq),
EndKey = case Dir of
rev -> fabric2_util:seq_zero_vs();
_ -> fabric2_util:seq_max_vs()
end,
FoldOpts = [
{start_key, StartKey},
{end_key, EndKey}
] ++ RestartTx ++ Options,
{ok, fabric2_fdb:fold_range(TxDb, Prefix, fun({K, V}, Acc) ->
{SeqVS} = erlfdb_tuple:unpack(K, Prefix),
{DocId, Deleted, RevId} = erlfdb_tuple:unpack(V),
Change = #{
id => DocId,
sequence => fabric2_fdb:vs_to_seq(SeqVS),
rev_id => RevId,
deleted => Deleted
},
maybe_stop(UserFun(Change, Acc))
end, UserAcc, FoldOpts)}
catch throw:{stop, FinalUserAcc} ->
{ok, FinalUserAcc}
end
end).
dbname_suffix(DbName) ->
filename:basename(normalize_dbname(DbName)).
normalize_dbname(DbName) ->
% Remove in the final cleanup. We don't need to handle shards prefix or
% remove .couch suffixes anymore. Keep it for now to pass all the existing
% tests.
couch_db:normalize_dbname(DbName).
validate_dbname(DbName) when is_list(DbName) ->
validate_dbname(?l2b(DbName));
validate_dbname(DbName) when is_binary(DbName) ->
Normalized = normalize_dbname(DbName),
fabric2_db_plugin:validate_dbname(
DbName, Normalized, fun validate_dbname_int/2).
validate_dbname_int(DbName, Normalized) when is_binary(DbName) ->
case validate_dbname_length(DbName) of
ok -> validate_dbname_pat(DbName, Normalized);
{error, _} = Error -> Error
end.
validate_dbname_length(DbName) ->
MaxLength = config:get_integer("couchdb", "max_database_name_length",
?DEFAULT_MAX_DATABASE_NAME_LENGTH),
case byte_size(DbName) =< MaxLength of
true -> ok;
false -> {error, {database_name_too_long, DbName}}
end.
validate_dbname_pat(DbName, Normalized) ->
DbNoExt = couch_util:drop_dot_couch_ext(DbName),
case re:run(DbNoExt, ?DBNAME_REGEX, [{capture,none}, dollar_endonly]) of
match ->
ok;
nomatch ->
case is_system_db_name(Normalized) of
true -> ok;
false -> {error, {illegal_database_name, DbName}}
end
end.
maybe_add_sys_db_callbacks(Db) ->
IsReplicatorDb = is_replicator_db(Db),
IsUsersDb = is_users_db(Db),
{BDU, ADR} = if
IsReplicatorDb ->
{
fun couch_replicator_docs:before_doc_update/3,
fun couch_replicator_docs:after_doc_read/2
};
IsUsersDb ->
{
fun fabric2_users_db:before_doc_update/3,
fun fabric2_users_db:after_doc_read/2
};
true ->
{undefined, undefined}
end,
Db#{
before_doc_update := BDU,
after_doc_read := ADR
}.
make_db_info(DbName, Props) ->
BaseProps = [
{cluster, {[{n, 0}, {q, 0}, {r, 0}, {w, 0}]}},
{compact_running, false},
{data_size, 0},
{db_name, DbName},
{disk_format_version, 0},
{disk_size, 0},
{instance_start_time, <<"0">>},
{purge_seq, 0}
],
lists:foldl(fun({Key, Val}, Acc) ->
lists:keystore(Key, 1, Acc, {Key, Val})
end, BaseProps, Props).
drain_info_futures(FutureQ, Count, _UserFun, Acc) when Count < 100 ->
{FutureQ, Count, Acc};
drain_info_futures(FutureQ, Count, UserFun, Acc) when Count >= 100 ->
{{value, {DbName, Future}}, RestQ} = queue:out(FutureQ),
InfoProps = fabric2_fdb:get_info_wait(Future),
DbInfo = make_db_info(DbName, InfoProps),
NewAcc = maybe_stop(UserFun({row, DbInfo}, Acc)),
{RestQ, Count - 1, NewAcc}.
drain_all_info_futures(FutureQ, UserFun, Acc) ->
case queue:out(FutureQ) of
{{value, {DbName, Future}}, RestQ} ->
InfoProps = fabric2_fdb:get_info_wait(Future),
DbInfo = make_db_info(DbName, InfoProps),
NewAcc = maybe_stop(UserFun({row, DbInfo}, Acc)),
drain_all_info_futures(RestQ, UserFun, NewAcc);
{empty, _} ->
Acc
end.
new_revid(Db, Doc) ->
#doc{
id = DocId,
body = Body,
revs = {OldStart, OldRevs},
atts = Atts,
deleted = Deleted
} = Doc,
{NewAtts, AttSigInfo} = lists:mapfoldl(fun(Att, Acc) ->
[Name, Type, Data, Md5] = couch_att:fetch([name, type, data, md5], Att),
case Data of
{loc, _, _, _} ->
{Att, [{Name, Type, Md5} | Acc]};
_ ->
Att1 = couch_att:flush(Db, DocId, Att),
Att2 = couch_att:store(revpos, OldStart + 1, Att1),
{Att2, [{Name, Type, couch_att:fetch(md5, Att2)} | Acc]}
end
end, [], Atts),
Rev = case length(Atts) == length(AttSigInfo) of
true ->
OldRev = case OldRevs of [] -> 0; [OldRev0 | _] -> OldRev0 end,
SigTerm = [Deleted, OldStart, OldRev, Body, AttSigInfo],
couch_hash:md5_hash(term_to_binary(SigTerm, [{minor_version, 1}]));
false ->
erlang:error(missing_att_info)
end,
Doc#doc{
revs = {OldStart + 1, [Rev | OldRevs]},
atts = NewAtts
}.
get_all_docs_meta(TxDb, Options) ->
NS = couch_util:get_value(namespace, Options),
DocCount = get_doc_count(TxDb, NS),
case lists:keyfind(update_seq, 1, Options) of
{_, true} ->
UpdateSeq = fabric2_db:get_update_seq(TxDb),
[{update_seq, UpdateSeq}];
_ ->
[]
end ++ [{total, DocCount}, {offset, null}].
maybe_set_user_ctx(Db, Options) ->
case fabric2_util:get_value(user_ctx, Options) of
#user_ctx{} = UserCtx ->
set_user_ctx(Db, UserCtx);
undefined ->
Db
end.
is_member(Db, {SecProps}) when is_list(SecProps) ->
case is_admin(Db, {SecProps}) of
true ->
true;
false ->
case is_public_db(SecProps) of
true ->
true;
false ->
{Members} = get_members(SecProps),
UserCtx = get_user_ctx(Db),
is_authorized(Members, UserCtx)
end
end.
is_authorized(Group, UserCtx) ->
#user_ctx{
name = UserName,
roles = UserRoles
} = UserCtx,
Names = fabric2_util:get_value(<<"names">>, Group, []),
Roles = fabric2_util:get_value(<<"roles">>, Group, []),
case check_security(roles, UserRoles, [<<"_admin">> | Roles]) of
true ->
true;
false ->
check_security(names, UserName, Names)
end.
check_security(roles, [], _) ->
false;
check_security(roles, UserRoles, Roles) ->
UserRolesSet = ordsets:from_list(UserRoles),
RolesSet = ordsets:from_list(Roles),
not ordsets:is_disjoint(UserRolesSet, RolesSet);
check_security(names, _, []) ->
false;
check_security(names, null, _) ->
false;
check_security(names, UserName, Names) ->
lists:member(UserName, Names).
throw_security_error(#user_ctx{name = null} = UserCtx) ->
Reason = <<"You are not authorized to access this db.">>,
throw_security_error(UserCtx, Reason);
throw_security_error(#user_ctx{name = _} = UserCtx) ->
Reason = <<"You are not allowed to access this db.">>,
throw_security_error(UserCtx, Reason).
throw_security_error(#user_ctx{} = UserCtx, Reason) ->
Error = security_error_type(UserCtx),
throw({Error, Reason}).
security_error_type(#user_ctx{name = null}) ->
unauthorized;
security_error_type(#user_ctx{name = _}) ->
forbidden.
is_public_db(SecProps) ->
{Members} = get_members(SecProps),
Names = fabric2_util:get_value(<<"names">>, Members, []),
Roles = fabric2_util:get_value(<<"roles">>, Members, []),
Names =:= [] andalso Roles =:= [].
get_admins(SecProps) ->
fabric2_util:get_value(<<"admins">>, SecProps, {[]}).
get_members(SecProps) ->
% we fallback to readers here for backwards compatibility
case fabric2_util:get_value(<<"members">>, SecProps) of
undefined ->
fabric2_util:get_value(<<"readers">>, SecProps, {[]});
Members ->
Members
end.
apply_open_doc_opts(Doc0, Revs, Options) ->
IncludeRevsInfo = lists:member(revs_info, Options),
IncludeConflicts = lists:member(conflicts, Options),
IncludeDelConflicts = lists:member(deleted_conflicts, Options),
IncludeLocalSeq = lists:member(local_seq, Options),
% This revs_info becomes fairly useless now that we're
% not keeping old document bodies around...
Meta1 = if not IncludeRevsInfo -> []; true ->
{Pos, [Rev | RevPath]} = Doc0#doc.revs,
RevPathMissing = lists:map(fun(R) -> {R, missing} end, RevPath),
[{revs_info, Pos, [{Rev, available} | RevPathMissing]}]
end,
Meta2 = if not IncludeConflicts -> []; true ->
Conflicts = [RI || RI = #{winner := false, deleted := false} <- Revs],
if Conflicts == [] -> []; true ->
ConflictRevs = [maps:get(rev_id, RI) || RI <- Conflicts],
[{conflicts, ConflictRevs}]
end
end,
Meta3 = if not IncludeDelConflicts -> []; true ->
DelConflicts = [RI || RI = #{winner := false, deleted := true} <- Revs],
if DelConflicts == [] -> []; true ->
DelConflictRevs = [maps:get(rev_id, RI) || RI <- DelConflicts],
[{deleted_conflicts, DelConflictRevs}]
end
end,
Meta4 = if not IncludeLocalSeq -> []; true ->
#{winner := true, sequence := SeqVS} = lists:last(Revs),
[{local_seq, fabric2_fdb:vs_to_seq(SeqVS)}]
end,
Doc1 = case lists:keyfind(atts_since, 1, Options) of
{_, PossibleAncestors} ->
#doc{
revs = DocRevs,
atts = Atts0
} = Doc0,
RevPos = find_ancestor_rev_pos(DocRevs, PossibleAncestors),
Atts1 = lists:map(fun(Att) ->
[AttPos, Data] = couch_att:fetch([revpos, data], Att),
if AttPos > RevPos -> couch_att:store(data, Data, Att);
true -> couch_att:store(data, stub, Att)
end
end, Atts0),
Doc0#doc{atts = Atts1};
false ->
Doc0
end,
{ok, Doc1#doc{meta = Meta1 ++ Meta2 ++ Meta3 ++ Meta4}}.
find_ancestor_rev_pos({_, []}, _PossibleAncestors) ->
0;
find_ancestor_rev_pos(_DocRevs, []) ->
0;
find_ancestor_rev_pos({RevPos, [RevId | Rest]}, AttsSinceRevs) ->
case lists:member({RevPos, RevId}, AttsSinceRevs) of
true -> RevPos;
false -> find_ancestor_rev_pos({RevPos - 1, Rest}, AttsSinceRevs)
end.
filter_found_revs(RevInfo, Revs) ->
#{
rev_id := {Pos, Rev},
rev_path := RevPath
} = RevInfo,
FullRevPath = [Rev | RevPath],
lists:flatmap(fun({FindPos, FindRev} = RevIdToFind) ->
if FindPos > Pos -> [RevIdToFind]; true ->
% Add 1 because lists:nth is 1 based
Idx = Pos - FindPos + 1,
case Idx > length(FullRevPath) of
true ->
[RevIdToFind];
false ->
case lists:nth(Idx, FullRevPath) == FindRev of
true -> [];
false -> [RevIdToFind]
end
end
end
end, Revs).
find_possible_ancestors(RevInfos, MissingRevs) ->
% Find any revinfos that are possible ancestors
% of the missing revs. A possible ancestor is
% any rev that has a start position less than
% any missing revision. Stated alternatively,
% find any revinfo that could theoretically
% extended to be one or more of the missing
% revisions.
%
% Since we are looking at any missing revision
% we can just compare against the maximum missing
% start position.
MaxMissingPos = case MissingRevs of
[] -> 0;
[_ | _] -> lists:max([Start || {Start, _Rev} <- MissingRevs])
end,
lists:flatmap(fun(RevInfo) ->
#{rev_id := {RevPos, _} = RevId} = RevInfo,
case RevPos < MaxMissingPos of
true -> [RevId];
false -> []
end
end, RevInfos).
apply_before_doc_update(Db, Docs, Options) ->
UpdateType = case lists:member(replicated_changes, Options) of
true -> replicated_changes;
false -> interactive_edit
end,
lists:map(fun(Doc) ->
fabric2_db_plugin:before_doc_update(Db, Doc, UpdateType)
end, Docs).
update_doc_int(#{} = Db, #doc{} = Doc, Options) ->
IsLocal = case Doc#doc.id of
<<?LOCAL_DOC_PREFIX, _/binary>> -> true;
_ -> false
end,
IsReplicated = lists:member(replicated_changes, Options),
try
case {IsLocal, IsReplicated} of
{false, false} -> update_doc_interactive(Db, Doc, Options);
{false, true} -> update_doc_replicated(Db, Doc, Options);
{true, _} -> update_local_doc(Db, Doc, Options)
end
catch throw:{?MODULE, Return} ->
Return
end.
update_docs_interactive(Db, Docs0, Options) ->
Docs = tag_docs(Docs0),
Futures = get_winning_rev_futures(Db, Docs),
{Result, _} = lists:mapfoldl(fun(Doc, SeenIds) ->
try
update_docs_interactive(Db, Doc, Options, Futures, SeenIds)
catch throw:{?MODULE, Return} ->
{Return, SeenIds}
end
end, [], Docs),
Result.
update_docs_interactive(Db, #doc{id = <<?LOCAL_DOC_PREFIX, _/binary>>} = Doc,
Options, _Futures, SeenIds) ->
{update_local_doc(Db, Doc, Options), SeenIds};
update_docs_interactive(Db, Doc, Options, Futures, SeenIds) ->
case lists:member(Doc#doc.id, SeenIds) of
true ->
{conflict, SeenIds};
false ->
Future = maps:get(doc_tag(Doc), Futures),
case update_doc_interactive(Db, Doc, Future, Options) of
{ok, _} = Resp ->
{Resp, [Doc#doc.id | SeenIds]};
_ = Resp ->
{Resp, SeenIds}
end
end.
update_doc_interactive(Db, Doc0, Options) ->
% Get the current winning revision. This is needed
% regardless of which branch we're updating. The extra
% revision we're grabbing is an optimization to
% save us a round trip if we end up deleting
% the winning revision branch.
NumRevs = if Doc0#doc.deleted -> 2; true -> 1 end,
Future = fabric2_fdb:get_winning_revs_future(Db, Doc0#doc.id, NumRevs),
update_doc_interactive(Db, Doc0, Future, Options).
update_doc_interactive(Db, Doc0, Future, _Options) ->
RevInfos = fabric2_fdb:get_winning_revs_wait(Db, Future),
{Winner, SecondPlace} = case RevInfos of
[] -> {not_found, not_found};
[WRI] -> {WRI, not_found};
[WRI, SPRI] -> {WRI, SPRI}
end,
WinnerRevId = case Winner of
not_found ->
{0, <<>>};
_ ->
case maps:get(deleted, Winner) of
true -> {0, <<>>};
false -> maps:get(rev_id, Winner)
end
end,
% Check that a revision was specified if required
Doc0RevId = doc_to_revid(Doc0),
if Doc0RevId /= {0, <<>>} orelse WinnerRevId == {0, <<>>} -> ok; true ->
?RETURN({Doc0, conflict})
end,
% Check that we're not trying to create a deleted doc
if Doc0RevId /= {0, <<>>} orelse not Doc0#doc.deleted -> ok; true ->
?RETURN({Doc0, conflict})
end,
% Get the target revision to update
Target = case Doc0RevId == WinnerRevId of
true ->
Winner;
false ->
case fabric2_fdb:get_non_deleted_rev(Db, Doc0#doc.id, Doc0RevId) of
#{deleted := false} = Target0 ->
Target0;
not_found ->
% Either a missing revision or a deleted
% revision. Either way a conflict. Note
% that we get not_found for a deleted revision
% because we only check for the non-deleted
% key in fdb
?RETURN({Doc0, conflict})
end
end,
Doc1 = case Winner of
#{deleted := true} when not Doc0#doc.deleted ->
% When recreating a deleted document we want to extend
% the winning revision branch rather than create a
% new branch. If we did not do this we could be
% recreating into a state that previously existed.
Doc0#doc{revs = fabric2_util:revinfo_to_revs(Winner)};
#{} ->
% Otherwise we're extending the target's revision
% history with this update
Doc0#doc{revs = fabric2_util:revinfo_to_revs(Target)};
not_found ->
% Creating a new doc means our revs start empty
Doc0
end,
% Validate the doc update and create the
% new revinfo map
Doc2 = prep_and_validate(Db, Doc1, Target),
Doc3 = new_revid(Db, Doc2),
#doc{
deleted = NewDeleted,
revs = {NewRevPos, [NewRev | NewRevPath]},
atts = Atts
} = Doc4 = stem_revisions(Db, Doc3),
NewRevInfo = #{
winner => undefined,
exists => false,
deleted => NewDeleted,
rev_id => {NewRevPos, NewRev},
rev_path => NewRevPath,
sequence => undefined,
branch_count => undefined,
att_hash => fabric2_util:hash_atts(Atts),
rev_size => fabric2_util:rev_size(Doc4)
},
% Gather the list of possible winnig revisions
Possible = case Target == Winner of
true when not Doc4#doc.deleted ->
[NewRevInfo];
true when Doc4#doc.deleted ->
case SecondPlace of
#{} -> [NewRevInfo, SecondPlace];
not_found -> [NewRevInfo]
end;
false ->
[NewRevInfo, Winner]
end,
% Sort the rev infos such that the winner is first
{NewWinner0, NonWinner} = case fabric2_util:sort_revinfos(Possible) of
[W] -> {W, not_found};
[W, NW] -> {W, NW}
end,
BranchCount = case Winner of
not_found -> 1;
#{branch_count := BC} -> BC
end,
NewWinner = NewWinner0#{branch_count := BranchCount},
ToUpdate = if NonWinner == not_found -> []; true -> [NonWinner] end,
ToRemove = if Target == not_found -> []; true -> [Target] end,
ok = fabric2_fdb:write_doc(
Db,
Doc4,
NewWinner,
Winner,
ToUpdate,
ToRemove
),
{ok, {NewRevPos, NewRev}}.
update_doc_replicated(Db, Doc0, _Options) ->
#doc{
id = DocId,
deleted = Deleted,
revs = {RevPos, [Rev | RevPath]}
} = Doc0,
DocRevInfo0 = #{
winner => undefined,
exists => false,
deleted => Deleted,
rev_id => {RevPos, Rev},
rev_path => RevPath,
sequence => undefined,
branch_count => undefined,
att_hash => <<>>,
rev_size => null
},
AllRevInfos = fabric2_fdb:get_all_revs(Db, DocId),
RevTree = lists:foldl(fun(RI, TreeAcc) ->
RIPath = fabric2_util:revinfo_to_path(RI),
{Merged, _} = couch_key_tree:merge(TreeAcc, RIPath),
Merged
end, [], AllRevInfos),
DocRevPath = fabric2_util:revinfo_to_path(DocRevInfo0),
{NewTree, Status} = couch_key_tree:merge(RevTree, DocRevPath),
if Status /= internal_node -> ok; true ->
% We already know this revision so nothing
% left to do.
?RETURN({Doc0, {ok, []}})
end,
% Its possible to have a replication with fewer than $revs_limit
% revisions which extends an existing branch. To avoid
% losing revision history we extract the new node from the
% tree and use the combined path after stemming.
{[{_, {RevPos, UnstemmedRevs}}], []}
= couch_key_tree:get(NewTree, [{RevPos, Rev}]),
Doc1 = stem_revisions(Db, Doc0#doc{revs = {RevPos, UnstemmedRevs}}),
{RevPos, [Rev | NewRevPath]} = Doc1#doc.revs,
DocRevInfo1 = DocRevInfo0#{rev_path := NewRevPath},
% Find any previous revision we knew about for
% validation and attachment handling.
AllLeafsFull = couch_key_tree:get_all_leafs_full(NewTree),
LeafPath = get_leaf_path(RevPos, Rev, AllLeafsFull),
PrevRevInfo = find_prev_revinfo(RevPos, LeafPath),
Doc2 = prep_and_validate(Db, Doc1, PrevRevInfo),
Doc3 = flush_doc_atts(Db, Doc2),
DocRevInfo2 = DocRevInfo1#{
atts_hash => fabric2_util:hash_atts(Doc3#doc.atts),
rev_size => fabric2_util:rev_size(Doc3)
},
% Possible winners are the previous winner and
% the new DocRevInfo
Winner = case fabric2_util:sort_revinfos(AllRevInfos) of
[#{winner := true} = WRI | _] -> WRI;
[] -> not_found
end,
{NewWinner0, NonWinner} = case Winner == PrevRevInfo of
true ->
{DocRevInfo2, not_found};
false ->
[W, NW] = fabric2_util:sort_revinfos([Winner, DocRevInfo2]),
{W, NW}
end,
NewWinner = NewWinner0#{branch_count := length(AllLeafsFull)},
ToUpdate = if NonWinner == not_found -> []; true -> [NonWinner] end,
ToRemove = if PrevRevInfo == not_found -> []; true -> [PrevRevInfo] end,
ok = fabric2_fdb:write_doc(
Db,
Doc3,
NewWinner,
Winner,
ToUpdate,
ToRemove
),
{ok, []}.
update_local_doc(Db, Doc0, _Options) ->
Doc1 = case increment_local_doc_rev(Doc0) of
{ok, Updated} -> Updated;
{error, Error} -> ?RETURN({Doc0, Error})
end,
ok = fabric2_fdb:write_local_doc(Db, Doc1),
#doc{revs = {0, [Rev]}} = Doc1,
{ok, {0, integer_to_binary(Rev)}}.
flush_doc_atts(Db, Doc) ->
#doc{
id = DocId,
atts = Atts
} = Doc,
NewAtts = lists:map(fun(Att) ->
case couch_att:fetch(data, Att) of
{loc, _, _, _} ->
Att;
_ ->
couch_att:flush(Db, DocId, Att)
end
end, Atts),
Doc#doc{atts = NewAtts}.
get_winning_rev_futures(Db, Docs) ->
lists:foldl(fun(Doc, Acc) ->
#doc{
id = DocId,
deleted = Deleted
} = Doc,
IsLocal = case DocId of
<<?LOCAL_DOC_PREFIX, _/binary>> -> true;
_ -> false
end,
if IsLocal -> Acc; true ->
NumRevs = if Deleted -> 2; true -> 1 end,
Future = fabric2_fdb:get_winning_revs_future(Db, DocId, NumRevs),
DocTag = doc_tag(Doc),
Acc#{DocTag => Future}
end
end, #{}, Docs).
prep_and_validate(Db, NewDoc, PrevRevInfo) ->
HasStubs = couch_doc:has_stubs(NewDoc),
HasVDUs = [] /= maps:get(validate_doc_update_funs, Db),
IsDDoc = case NewDoc#doc.id of
<<?DESIGN_DOC_PREFIX, _/binary>> -> true;
_ -> false
end,
WasDeleted = case PrevRevInfo of
not_found -> false;
#{deleted := D} -> D
end,
PrevDoc = case HasStubs orelse (HasVDUs and not IsDDoc) of
true when PrevRevInfo /= not_found, not WasDeleted ->
case fabric2_fdb:get_doc_body(Db, NewDoc#doc.id, PrevRevInfo) of
#doc{} = PDoc -> PDoc;
{not_found, _} -> nil
end;
_ ->
nil
end,
MergedDoc = if not HasStubs -> NewDoc; true ->
% This will throw an error if we have any
% attachment stubs missing data
couch_doc:merge_stubs(NewDoc, PrevDoc)
end,
check_duplicate_attachments(MergedDoc),
validate_doc_update(Db, MergedDoc, PrevDoc),
MergedDoc.
validate_doc_update(Db, #doc{id = <<"_design/", _/binary>>} = Doc, _) ->
case catch check_is_admin(Db) of
ok -> validate_ddoc(Db, Doc);
Error -> ?RETURN({Doc, Error})
end;
validate_doc_update(Db, Doc, PrevDoc) ->
#{
security_doc := Security,
validate_doc_update_funs := VDUs
} = Db,
Fun = fun() ->
JsonCtx = fabric2_util:user_ctx_to_json(Db),
lists:map(fun(VDU) ->
try
case VDU(Doc, PrevDoc, JsonCtx, Security) of
ok -> ok;
Error1 -> throw(Error1)
end
catch throw:Error2 ->
?RETURN({Doc, Error2})
end
end, VDUs)
end,
Stat = [couchdb, query_server, vdu_process_time],
if VDUs == [] -> ok; true ->
couch_stats:update_histogram(Stat, Fun)
end.
validate_ddoc(Db, DDoc) ->
try
ok = couch_index_server:validate(Db, couch_doc:with_ejson_body(DDoc))
catch
throw:{invalid_design_doc, Reason} ->
throw({bad_request, invalid_design_doc, Reason});
throw:{compilation_error, Reason} ->
throw({bad_request, compilation_error, Reason});
throw:Error ->
?RETURN({DDoc, Error})
end.
validate_atomic_update(_, false) ->
ok;
validate_atomic_update(AllDocs, true) ->
% TODO actually perform the validation. This requires some hackery, we need
% to basically extract the prep_and_validate_updates function from couch_db
% and only run that, without actually writing in case of a success.
Error = {not_implemented, <<"all_or_nothing is not supported">>},
PreCommitFailures = lists:map(fun(#doc{id=Id, revs = {Pos,Revs}}) ->
case Revs of [] -> RevId = <<>>; [RevId|_] -> ok end,
{{Id, {Pos, RevId}}, Error}
end, AllDocs),
throw({aborted, PreCommitFailures}).
check_duplicate_attachments(#doc{atts = Atts}) ->
lists:foldl(fun(Att, Names) ->
Name = couch_att:fetch(name, Att),
case ordsets:is_element(Name, Names) of
true -> throw({bad_request, <<"Duplicate attachments">>});
false -> ordsets:add_element(Name, Names)
end
end, ordsets:new(), Atts).
get_since_seq(Db, rev, <<>>) ->
get_since_seq(Db, rev, now);
get_since_seq(_Db, _Dir, Seq) when Seq == <<>>; Seq == <<"0">>; Seq == 0->
fabric2_util:seq_zero_vs();
get_since_seq(Db, Dir, Seq) when Seq == now; Seq == <<"now">> ->
CurrSeq = fabric2_fdb:get_last_change(Db),
get_since_seq(Db, Dir, CurrSeq);
get_since_seq(_Db, _Dir, Seq) when is_binary(Seq), size(Seq) == 24 ->
fabric2_fdb:next_vs(fabric2_fdb:seq_to_vs(Seq));
get_since_seq(Db, Dir, List) when is_list(List) ->
get_since_seq(Db, Dir, list_to_binary(List));
get_since_seq(_Db, _Dir, Seq) ->
erlang:error({invalid_since_seq, Seq}).
get_leaf_path(Pos, Rev, [{Pos, [{Rev, _RevInfo} | LeafPath]} | _]) ->
LeafPath;
get_leaf_path(Pos, Rev, [_WrongLeaf | RestLeafs]) ->
get_leaf_path(Pos, Rev, RestLeafs).
find_prev_revinfo(_Pos, []) ->
not_found;
find_prev_revinfo(Pos, [{_Rev, ?REV_MISSING} | RestPath]) ->
find_prev_revinfo(Pos - 1, RestPath);
find_prev_revinfo(_Pos, [{_Rev, #{} = RevInfo} | _]) ->
RevInfo.
increment_local_doc_rev(#doc{deleted = true} = Doc) ->
{ok, Doc#doc{revs = {0, [0]}}};
increment_local_doc_rev(#doc{revs = {0, []}} = Doc) ->
{ok, Doc#doc{revs = {0, [1]}}};
increment_local_doc_rev(#doc{revs = {0, [RevStr | _]}} = Doc) ->
try
PrevRev = binary_to_integer(RevStr),
{ok, Doc#doc{revs = {0, [PrevRev + 1]}}}
catch error:badarg ->
{error, <<"Invalid rev format">>}
end;
increment_local_doc_rev(#doc{}) ->
{error, <<"Invalid rev format">>}.
doc_to_revid(#doc{revs = Revs}) ->
case Revs of
{0, []} -> {0, <<>>};
{RevPos, [Rev | _]} -> {RevPos, Rev}
end.
tag_docs([]) ->
[];
tag_docs([#doc{meta = Meta} = Doc | Rest]) ->
NewDoc = Doc#doc{
meta = [{ref, make_ref()} | Meta]
},
[NewDoc | tag_docs(Rest)].
doc_tag(#doc{meta = Meta}) ->
fabric2_util:get_value(ref, Meta).
idrevs({Id, Revs}) when is_list(Revs) ->
{docid(Id), [rev(R) || R <- Revs]}.
docid(DocId) when is_list(DocId) ->
list_to_binary(DocId);
docid(DocId) ->
DocId.
rev(Rev) when is_list(Rev); is_binary(Rev) ->
couch_doc:parse_rev(Rev);
rev({Seq, Hash} = Rev) when is_integer(Seq), is_binary(Hash) ->
Rev.
maybe_stop({ok, Acc}) ->
Acc;
maybe_stop({stop, Acc}) ->
throw({stop, Acc}).
set_design_doc_keys(Options1) ->
Dir = couch_util:get_value(dir, Options1, fwd),
Options2 = set_design_doc_start_key(Options1, Dir),
set_design_doc_end_key(Options2, Dir).
set_design_doc_start_key(Options, fwd) ->
Key1 = couch_util:get_value(start_key, Options, ?FIRST_DDOC_KEY),
Key2 = max(Key1, ?FIRST_DDOC_KEY),
lists:keystore(start_key, 1, Options, {start_key, Key2});
set_design_doc_start_key(Options, rev) ->
Key1 = couch_util:get_value(start_key, Options, ?LAST_DDOC_KEY),
Key2 = min(Key1, ?LAST_DDOC_KEY),
lists:keystore(start_key, 1, Options, {start_key, Key2}).
set_design_doc_end_key(Options, fwd) ->
case couch_util:get_value(end_key_gt, Options) of
undefined ->
Key1 = couch_util:get_value(end_key, Options, ?LAST_DDOC_KEY),
Key2 = min(Key1, ?LAST_DDOC_KEY),
lists:keystore(end_key, 1, Options, {end_key, Key2});
EKeyGT ->
Key2 = min(EKeyGT, ?LAST_DDOC_KEY),
lists:keystore(end_key_gt, 1, Options, {end_key_gt, Key2})
end;
set_design_doc_end_key(Options, rev) ->
case couch_util:get_value(end_key_gt, Options) of
undefined ->
Key1 = couch_util:get_value(end_key, Options, ?FIRST_DDOC_KEY),
Key2 = max(Key1, ?FIRST_DDOC_KEY),
lists:keystore(end_key, 1, Options, {end_key, Key2});
EKeyGT ->
Key2 = max(EKeyGT, ?FIRST_DDOC_KEY),
lists:keystore(end_key_gt, 1, Options, {end_key_gt, Key2})
end.
stem_revisions(#{} = Db, #doc{} = Doc) ->
#{revs_limit := RevsLimit} = Db,
#doc{revs = {RevPos, Revs}} = Doc,
case RevPos >= RevsLimit of
true -> Doc#doc{revs = {RevPos, lists:sublist(Revs, RevsLimit)}};
false -> Doc
end.
|