summaryrefslogtreecommitdiff
path: root/src/libebackend/e-cache.c
blob: 4d89cb25ec6781f181a3199e24290f45925e77d7 (plain)
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
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2017 Red Hat, Inc. (www.redhat.com)
 *
 * This library is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation.
 *
 * This library 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 Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * SECTION: e-cache
 * @include: libebackend/libebackend.h
 * @short_description: An SQLite data cache
 *
 * The #ECache is an abstract class which consists of the common
 * parts which can be used by its descendants. It also allows
 * storing offline state for the stored objects.
 *
 * The API is thread safe, with special considerations to be made
 * around e_cache_lock() and e_cache_unlock() for
 * the sake of isolating transactions across threads.
 **/

#include "evolution-data-server-config.h"

#include <errno.h>
#include <sqlite3.h>

#include <glib.h>
#include <glib/gi18n-lib.h>
#include <glib/gstdio.h>

#include <camel/camel.h>
#include <libedataserver/libedataserver.h>

#include "e-sqlite3-vfs.h"

#include "e-cache.h"

#define E_CACHE_KEY_VERSION	"version"
#define E_CACHE_KEY_REVISION	"revision"

/* The number of SQLite virtual machine instructions that are
 * evaluated at a time, the user passed GCancellable is
 * checked between each batch of evaluated instructions.
 */
#define E_CACHE_CANCEL_BATCH_SIZE	200

/* How many rows to read when e_cache_foreach_update() */
#define E_CACHE_UPDATE_BATCH_SIZE	100

struct _ECachePrivate {
	gchar *filename;
	sqlite3 *db;

	GRecMutex lock;			/* Main API lock */
	guint32 in_transaction;		/* Nested transaction counter */
	ECacheLockType lock_type;	/* The lock type acquired for the current transaction */
	GCancellable *cancellable;	/* User passed GCancellable, we abort an operation if cancelled */

	guint32 revision_change_frozen;
	gint revision_counter;
	gint64 last_revision_time;
	gboolean needs_revision_change;
};

enum {
	BEFORE_PUT,
	BEFORE_REMOVE,
	REVISION_CHANGED,
	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL];

G_DEFINE_QUARK (e-cache-error-quark, e_cache_error)

G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (ECache, e_cache, G_TYPE_OBJECT)

G_DEFINE_BOXED_TYPE (ECacheColumnValues, e_cache_column_values, e_cache_column_values_copy, e_cache_column_values_free)
G_DEFINE_BOXED_TYPE (ECacheOfflineChange, e_cache_offline_change, e_cache_offline_change_copy, e_cache_offline_change_free)
G_DEFINE_BOXED_TYPE (ECacheColumnInfo, e_cache_column_info, e_cache_column_info_copy, e_cache_column_info_free)

/**
 * e_cache_column_values_new:
 *
 * Creates a new #ECacheColumnValues to store values for additional columns.
 * The column names are compared case insensitively.
 *
 * Returns: (transfer full): a new #ECacheColumnValues. Free with e_cache_column_values_free(),
 *    when no longer needed.
 *
 * Since: 3.26
 **/
ECacheColumnValues *
e_cache_column_values_new (void)
{
	return (ECacheColumnValues *) g_hash_table_new_full (camel_strcase_hash, camel_strcase_equal, g_free, g_free);
}

/**
 * e_cache_column_values_copy:
 * @other_columns: (nullable): an #ECacheColumnValues
 *
 * Returns: (transfer full): Copy of the @other_columns. Free with
 *    e_cache_column_values_free(), when no longer needed.
 *
 * Since: 3.26
 **/
ECacheColumnValues *
e_cache_column_values_copy (ECacheColumnValues *other_columns)
{
	GHashTableIter iter;
	gpointer name, value;
	ECacheColumnValues *copy;

	if (!other_columns)
		return NULL;

	copy = e_cache_column_values_new ();

	e_cache_column_values_init_iter (other_columns, &iter);
	while (g_hash_table_iter_next (&iter, &name, &value)) {
		e_cache_column_values_put (copy, name, value);
	}

	return copy;
}

/**
 * e_cache_column_values_free:
 * @other_columns: (nullable): an #ECacheColumnValues
 *
 * Frees previously allocated @other_columns with
 * e_cache_column_values_new() or e_cache_column_values_copy().
 *
 * Since: 3.26
 **/
void
e_cache_column_values_free (ECacheColumnValues *other_columns)
{
	if (other_columns)
		g_hash_table_destroy ((GHashTable *) other_columns);
}

/**
 * e_cache_column_values_put:
 * @other_columns: an #ECacheColumnValues
 * @name: a column name
 * @value: (nullable): a column value
 *
 * Puts the @value for column @name. If contains a value for the same
 * column, then it is replaced. This creates a copy of both @name
 * and @value.
 *
 * Since: 3.26
 **/
void
e_cache_column_values_put (ECacheColumnValues *other_columns,
			   const gchar *name,
			   const gchar *value)
{
	GHashTable *hash_table = (GHashTable *) other_columns;

	g_return_if_fail (other_columns != NULL);
	g_return_if_fail (name != NULL);

	g_hash_table_insert (hash_table, g_strdup (name), g_strdup (value));
}

/**
 * e_cache_column_values_take_value:
 * @other_columns: an #ECacheColumnValues
 * @name: a column name
 * @value: (nullable) (in) (transfer full): a column value
 *
 * Puts the @value for column @name. If contains a value for the same
 * column, then it is replaced. This creates a copy of the @name, but
 * takes owner ship of the @value.
 *
 * Since: 3.26
 **/
void
e_cache_column_values_take_value (ECacheColumnValues *other_columns,
				  const gchar *name,
				  gchar *value)
{
	GHashTable *hash_table = (GHashTable *) other_columns;

	g_return_if_fail (other_columns != NULL);
	g_return_if_fail (name != NULL);

	g_hash_table_insert (hash_table, g_strdup (name), value);
}

/**
 * e_cache_column_values_take:
 * @other_columns: an #ECacheColumnValues
 * @name: (in) (transfer full): a column name
 * @value: (nullable) (in) (transfer full): a column value
 *
 * Puts the @value for column @name. If contains a value for the same
 * column, then it is replaced. This creates takes ownership of both
 * the @name and the @value.
 *
 * Since: 3.26
 **/
void
e_cache_column_values_take (ECacheColumnValues *other_columns,
			    gchar *name,
			    gchar *value)
{
	GHashTable *hash_table = (GHashTable *) other_columns;

	g_return_if_fail (other_columns != NULL);
	g_return_if_fail (name != NULL);

	g_hash_table_insert (hash_table, name, value);
}

/**
 * e_cache_column_values_contains:
 * @other_columns: an #ECacheColumnValues
 * @name: a column name
 *
 * Returns: Whether @other_columns contains column named @name.
 *
 * Since: 3.26
 **/
gboolean
e_cache_column_values_contains (ECacheColumnValues *other_columns,
				const gchar *name)
{
	GHashTable *hash_table = (GHashTable *) other_columns;

	g_return_val_if_fail (other_columns != NULL, FALSE);
	g_return_val_if_fail (name != NULL, FALSE);

	return g_hash_table_contains (hash_table, name);
}

/**
 * e_cache_column_values_remove:
 * @other_columns: an #ECacheColumnValues
 * @name: a column name
 *
 * Removes value for the column named @name from @other_columns.
 *
 * Returns: Whether such column existed and had been removed.
 *
 * Since: 3.26
 **/
gboolean
e_cache_column_values_remove (ECacheColumnValues *other_columns,
			      const gchar *name)
{
	GHashTable *hash_table = (GHashTable *) other_columns;

	g_return_val_if_fail (other_columns != NULL, FALSE);
	g_return_val_if_fail (name != NULL, FALSE);

	return g_hash_table_remove (hash_table, name);
}

/**
 * e_cache_column_values_remove_all:
 * @other_columns: an #ECacheColumnValues
 *
 * Removes all values from the @other_columns, leaving it empty.
 *
 * Since: 3.26
 **/
void
e_cache_column_values_remove_all (ECacheColumnValues *other_columns)
{
	GHashTable *hash_table = (GHashTable *) other_columns;

	g_return_if_fail (other_columns != NULL);

	g_hash_table_remove_all (hash_table);
}

/**
 * e_cache_column_values_lookup:
 * @other_columns: an #ECacheColumnValues
 * @name: a column name
 *
 * Looks up currently stored value for the column named @name.
 * As the values can be %NULL one cannot distinguish between
 * a column which doesn't have stored any value and a column
 * which has stored %NULL value. Use e_cache_column_values_contains()
 * to check whether such column exitst in the @other_columns.
 * The returned pointer is owned by @other_columns and is valid until
 * the value is overwritten of the @other_columns freed.
 *
 * Returns: (nullable): Stored value for the column named @name,
 *    or %NULL, if no such column values is stored.
 *
 * Since: 3.26
 **/
const gchar *
e_cache_column_values_lookup (ECacheColumnValues *other_columns,
			      const gchar *name)
{
	GHashTable *hash_table = (GHashTable *) other_columns;

	g_return_val_if_fail (other_columns != NULL, NULL);
	g_return_val_if_fail (name != NULL, NULL);

	return g_hash_table_lookup (hash_table, name);
}

/**
 * e_cache_column_values_get_size:
 * @other_columns: an #ECacheColumnValues
 *
 * Returns: How many columns are stored in the @other_columns.
 *
 * Since: 3.26
 **/
guint
e_cache_column_values_get_size (ECacheColumnValues *other_columns)
{
	GHashTable *hash_table = (GHashTable *) other_columns;

	g_return_val_if_fail (other_columns != NULL, 0);

	return g_hash_table_size (hash_table);
}

/**
 * e_cache_column_values_init_iter:
 * @other_columns: an #ECacheColumnValues
 * @iter: a #GHashTableIter
 *
 * Initialized the @iter, thus the @other_columns can be traversed
 * with g_hash_table_iter_next(). The key is a column name and
 * the value is the corresponding column value.
 *
 * Since: 3.26
 **/
void
e_cache_column_values_init_iter (ECacheColumnValues *other_columns,
				 GHashTableIter *iter)
{
	GHashTable *hash_table = (GHashTable *) other_columns;

	g_return_if_fail (other_columns != NULL);
	g_return_if_fail (iter != NULL);

	g_hash_table_iter_init (iter, hash_table);
}

/**
 * e_cache_offline_change_new:
 * @uid: a unique object identifier
 * @revision: (nullable): a revision of the object
 * @object: (nullable): object itself
 * @state: an #EOfflineState
 *
 * Creates a new #ECacheOfflineChange with the offline @state
 * information for the given @uid.
 *
 * Returns: (transfer full): A new #ECacheOfflineChange. Free it with
 *    e_cache_offline_change_free() when no longer needed.
 *
 * Since: 3.26
 **/
ECacheOfflineChange *
e_cache_offline_change_new (const gchar *uid,
			    const gchar *revision,
			    const gchar *object,
			    EOfflineState state)
{
	ECacheOfflineChange *change;

	g_return_val_if_fail (uid != NULL, NULL);

	change = g_slice_new0 (ECacheOfflineChange);
	change->uid = g_strdup (uid);
	change->revision = g_strdup (revision);
	change->object = g_strdup (object);
	change->state = state;

	return change;
}

/**
 * e_cache_offline_change_copy:
 * @change: (nullable): a source #ECacheOfflineChange to copy, or %NULL
 *
 * Returns: (transfer full) (nullable): Copy of the given @change.
 *    Free it with e_cache_offline_change_free() when no longer
 *    needed. If the @change is %NULL, then returns %NULL as well.
 *
 * Since: 3.26
 **/
ECacheOfflineChange *
e_cache_offline_change_copy (const ECacheOfflineChange *change)
{
	if (!change)
		return NULL;

	return e_cache_offline_change_new (change->uid, change->revision, change->object, change->state);
}

/**
 * e_cache_offline_change_free:
 * @change: (nullable): an #ECacheOfflineChange
 *
 * Frees the @change structure, previously allocated with e_cache_offline_change_new()
 * or e_cache_offline_change_copy().
 *
 * Since: 3.26
 **/
void
e_cache_offline_change_free (gpointer change)
{
	ECacheOfflineChange *chng = change;

	if (chng) {
		g_free (chng->uid);
		g_free (chng->revision);
		g_free (chng->object);
		g_slice_free (ECacheOfflineChange, chng);
	}
}

/**
 * e_cache_column_info_new:
 * @name: a column name
 * @type: a column type
 * @index_name: (nullable): an index name for this column, or %NULL
 *
 * Returns: (transfer full): A new #ECacheColumnInfo. Free it with
 *    e_cache_column_info_free() when no longer needed.
 *
 * Since: 3.26
 **/
ECacheColumnInfo *
e_cache_column_info_new (const gchar *name,
			 const gchar *type,
			 const gchar *index_name)
{
	ECacheColumnInfo *info;

	g_return_val_if_fail (name != NULL, NULL);
	g_return_val_if_fail (type != NULL, NULL);

	info = g_slice_new0 (ECacheColumnInfo);
	info->name = g_strdup (name);
	info->type = g_strdup (type);
	info->index_name = g_strdup (index_name);

	return info;
}

/**
 * e_cache_column_info_copy:
 * @info: (nullable): a source #ECacheColumnInfo to copy, or %NULL
 *
 * Returns: (transfer full) (nullable): Copy of the given @info.
 *    Free it with e_cache_column_info_free() when no longer needed.
 *    If the @info is %NULL, then returns %NULL as well.
 *
 * Since: 3.26
 **/
ECacheColumnInfo *
e_cache_column_info_copy (const ECacheColumnInfo *info)
{
	if (!info)
		return NULL;

	return e_cache_column_info_new (info->name, info->type, info->index_name);
}

/**
 * e_cache_column_info_free:
 * @info: (nullable): an #ECacheColumnInfo
 *
 * Frees the @info structure, previously allocated with e_cache_column_info_new()
 * or e_cache_column_info_copy().
 *
 * Since: 3.26
 **/
void
e_cache_column_info_free (gpointer info)
{
	ECacheColumnInfo *nfo = info;

	if (nfo) {
		g_free (nfo->name);
		g_free (nfo->type);
		g_free (nfo->index_name);
		g_slice_free (ECacheColumnInfo, nfo);
	}
}

struct CacheSQLiteExecData {
	ECache *cache;
	ECacheSelectFunc callback;
	gpointer user_data;
};

static gint
e_cache_sqlite_exec_cb (gpointer user_data,
			gint ncols,
			gchar **column_values,
			gchar **column_names)
{
	struct CacheSQLiteExecData *cse = user_data;

	g_return_val_if_fail (cse != NULL, SQLITE_MISUSE);
	g_return_val_if_fail (cse->callback != NULL, SQLITE_MISUSE);

	if (!cse->callback (cse->cache, ncols, (const gchar **) column_names, (const gchar **) column_values, cse->user_data))
		return SQLITE_ABORT;

	return SQLITE_OK;
}

static gboolean
e_cache_sqlite_exec_internal (ECache *cache,
			      const gchar *stmt,
			      ECacheSelectFunc callback,
			      gpointer user_data,
			      GCancellable *cancellable,
			      GError **error)
{
	struct CacheSQLiteExecData cse;
	GCancellable *previous_cancellable;
	gchar *errmsg = NULL;
	gint ret = -1, retries = 0;

	g_return_val_if_fail (E_IS_CACHE (cache), FALSE);
	g_return_val_if_fail (stmt != NULL, FALSE);

	g_rec_mutex_lock (&cache->priv->lock);

	previous_cancellable = cache->priv->cancellable;
	if (cancellable)
		cache->priv->cancellable = cancellable;

	cse.cache = cache;
	cse.callback = callback;
	cse.user_data = user_data;

	ret = sqlite3_exec (cache->priv->db, stmt, callback ? e_cache_sqlite_exec_cb : NULL, &cse, &errmsg);

	while (ret == SQLITE_BUSY || ret == SQLITE_LOCKED || ret == -1) {
		/* try for ~15 seconds, then give up */
		if (retries > 150)
			break;
		retries++;

		g_clear_pointer (&errmsg, sqlite3_free);
		g_thread_yield ();
		g_usleep (100 * 1000); /* Sleep for 100 ms */

		ret = sqlite3_exec (cache->priv->db, stmt, callback ? e_cache_sqlite_exec_cb : NULL, &cse, &errmsg);
	}

	cache->priv->cancellable = previous_cancellable;

	g_rec_mutex_unlock (&cache->priv->lock);

	if (ret != SQLITE_OK) {
		if (ret == SQLITE_CONSTRAINT) {
			g_set_error_literal (error, E_CACHE_ERROR, E_CACHE_ERROR_CONSTRAINT, errmsg);
		} else if (ret == SQLITE_ABORT || ret == SQLITE_INTERRUPT) {
			g_set_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED, "Operation cancelled: %s", errmsg);
		} else if (ret == SQLITE_CORRUPT && cache->priv->filename && *cache->priv->filename) {
			g_set_error (error, E_CACHE_ERROR, E_CACHE_ERROR_CORRUPT,
				"%s (%s)", errmsg, cache->priv->filename);
		} else {
			gchar *valid_utf8 = e_util_utf8_make_valid (stmt);
			g_set_error (error, E_CACHE_ERROR, E_CACHE_ERROR_ENGINE,
				"SQLite error code '%d': %s (statement:%s)", ret, errmsg, valid_utf8 ? valid_utf8 : stmt);
			g_free (valid_utf8);
		}
		sqlite3_free (errmsg);

		return FALSE;
	}

	if (errmsg)
		sqlite3_free (errmsg);

	return TRUE;
}

static gboolean
e_cache_sqlite_exec_printf (ECache *cache,
			    const gchar *format,
			    ECacheSelectFunc callback,
			    gpointer user_data,
			    GCancellable *cancellable,
			    GError **error,
			    ...)
{
	gboolean success;
	va_list args;
	gchar *stmt;

	g_return_val_if_fail (E_IS_CACHE (cache), FALSE);
	g_return_val_if_fail (format != NULL, FALSE);

	va_start (args, error);
	stmt = sqlite3_vmprintf (format, args);

	success = e_cache_sqlite_exec_internal (cache, stmt, callback, user_data, cancellable, error);

	sqlite3_free (stmt);
	va_end (args);

	return success;
}

static gboolean
e_cache_read_key_value (ECache *cache,
			gint ncols,
			const gchar **column_names,
			const gchar **column_values,
			gpointer user_data)
{
	gchar **pvalue = user_data;

	g_return_val_if_fail (ncols == 1, FALSE);
	g_return_val_if_fail (column_names != NULL, FALSE);
	g_return_val_if_fail (column_values != NULL, FALSE);
	g_return_val_if_fail (pvalue != NULL, FALSE);

	if (!*pvalue)
		*pvalue = g_strdup (column_values[0]);

	return TRUE;
}

static gchar *
e_cache_build_user_key (const gchar *key)
{
	return g_strconcat ("user::", key, NULL);
}

static gboolean
e_cache_set_key_internal (ECache *cache,
			  gboolean is_user_key,
			  const gchar *key,
			  const gchar *value,
			  GError **error)
{
	gchar *tmp = NULL;
	const gchar *usekey;
	gboolean success;

	if (is_user_key) {
		tmp = e_cache_build_user_key (key);
		usekey = tmp;
	} else {
		usekey = key;
	}

	if (value) {
		success = e_cache_sqlite_exec_printf (cache,
			"INSERT or REPLACE INTO " E_CACHE_TABLE_KEYS " (key, value) VALUES (%Q, %Q)",
			NULL, NULL, NULL, error,
			usekey, value);
	} else {
		success = e_cache_sqlite_exec_printf (cache,
			"DELETE FROM " E_CACHE_TABLE_KEYS " WHERE key = %Q",
			NULL, NULL, NULL, error,
			usekey);
	}

	g_free (tmp);

	return success;
}

static gchar *
e_cache_dup_key_internal (ECache *cache,
			  gboolean is_user_key,
			  const gchar *key,
			  GError **error)
{
	gchar *tmp = NULL;
	const gchar *usekey;
	gchar *value = NULL;

	if (is_user_key) {
		tmp = e_cache_build_user_key (key);
		usekey = tmp;
	} else {
		usekey = key;
	}

	if (!e_cache_sqlite_exec_printf (cache,
		"SELECT value FROM " E_CACHE_TABLE_KEYS " WHERE key = %Q",
		e_cache_read_key_value, &value, NULL, error,
		usekey)) {
		g_warn_if_fail (value == NULL);
	}

	g_free (tmp);

	return value;
}

static gint
e_cache_check_cancelled_cb (gpointer user_data)
{
	ECache *cache = user_data;

	/* Do not use E_IS_CACHE() here, for performance reasons */
	g_return_val_if_fail (cache != NULL, SQLITE_ABORT);

	if (cache->priv->cancellable &&
	    g_cancellable_is_cancelled (cache->priv->cancellable)) {
		return SQLITE_ABORT;
	}

	return SQLITE_OK;
}

static gboolean
e_cache_init_sqlite (ECache *cache,
		     const gchar *filename,
		     GCancellable *cancellable,
		     GError **error)
{
	gint ret;

	g_return_val_if_fail (E_IS_CACHE (cache), FALSE);
	g_return_val_if_fail (filename != NULL, FALSE);
	g_return_val_if_fail (cache->priv->filename == NULL, FALSE);

	cache->priv->filename = g_strdup (filename);

	ret = sqlite3_open (filename, &cache->priv->db);
	if (ret != SQLITE_OK) {
		if (!cache->priv->db) {
			g_set_error_literal (error, E_CACHE_ERROR, E_CACHE_ERROR_LOAD, _("Out of memory"));
		} else {
			const gchar *errmsg = sqlite3_errmsg (cache->priv->db);

			g_set_error (error, E_CACHE_ERROR, E_CACHE_ERROR_ENGINE,
				_("Can’t open database %s: %s"), filename, errmsg);

			sqlite3_close (cache->priv->db);
			cache->priv->db = NULL;
		}

		return FALSE;
	}

	/* Handle GCancellable */
	sqlite3_progress_handler (
		cache->priv->db,
		E_CACHE_CANCEL_BATCH_SIZE,
		e_cache_check_cancelled_cb,
		cache);

	return e_cache_sqlite_exec_internal (cache, "ATTACH DATABASE ':memory:' AS mem", NULL, NULL, cancellable, error) &&
		e_cache_sqlite_exec_internal (cache, "PRAGMA foreign_keys = ON",          NULL, NULL, cancellable, error) &&
		e_cache_sqlite_exec_internal (cache, "PRAGMA case_sensitive_like = ON",   NULL, NULL, cancellable, error);
}

static gboolean
e_cache_garther_column_names_cb (ECache *cache,
				 gint ncols,
				 const gchar *column_names[],
				 const gchar *column_values[],
				 gpointer user_data)
{
	GHashTable *known_columns = user_data;
	gint ii;

	g_return_val_if_fail (known_columns != NULL, FALSE);
	g_return_val_if_fail (column_names != NULL, FALSE);
	g_return_val_if_fail (column_values != NULL, FALSE);

	for (ii = 0; ii < ncols; ii++) {
		if (column_names[ii] && camel_strcase_equal (column_names[ii], "name")) {
			if (column_values[ii])
				g_hash_table_insert (known_columns, g_strdup (column_values[ii]), NULL);
			break;
		}
	}

	return TRUE;
}

static gboolean
e_cache_init_tables (ECache *cache,
		     const GSList *other_columns,
		     GCancellable *cancellable,
		     GError **error)
{
	GHashTable *known_columns;
	GString *objects_stmt;
	const GSList *link;

	g_return_val_if_fail (E_IS_CACHE (cache), FALSE);
	g_return_val_if_fail (cache->priv->db != NULL, FALSE);

	if (!e_cache_sqlite_exec_internal (cache,
		"CREATE TABLE IF NOT EXISTS " E_CACHE_TABLE_KEYS " ("
		"key TEXT PRIMARY KEY,"
		"value TEXT)",
		NULL, NULL, cancellable, error)) {
		return FALSE;
	}

	objects_stmt = g_string_new ("");

	g_string_append (objects_stmt, "CREATE TABLE IF NOT EXISTS " E_CACHE_TABLE_OBJECTS " ("
		E_CACHE_COLUMN_UID " TEXT PRIMARY KEY,"
		E_CACHE_COLUMN_REVISION " TEXT,"
		E_CACHE_COLUMN_OBJECT " TEXT,"
		E_CACHE_COLUMN_STATE " INTEGER");

	for (link = other_columns; link; link = g_slist_next (link)) {
		const ECacheColumnInfo *info = link->data;

		if (!info)
			continue;

		g_string_append_c (objects_stmt, ',');
		g_string_append (objects_stmt, info->name);
		g_string_append_c (objects_stmt, ' ');
		g_string_append (objects_stmt, info->type);
	}

	g_string_append_c (objects_stmt, ')');

	if (!e_cache_sqlite_exec_internal (cache, objects_stmt->str, NULL, NULL, cancellable, error)) {
		g_string_free (objects_stmt, TRUE);

		return FALSE;
	}

	g_string_free (objects_stmt, TRUE);

	/* Verify that all other columns are there and remove those unused */
	known_columns = g_hash_table_new_full (camel_strcase_hash, camel_strcase_equal, g_free, NULL);

	if (!e_cache_sqlite_exec_internal (cache, "PRAGMA table_info (" E_CACHE_TABLE_OBJECTS ")",
		e_cache_garther_column_names_cb, known_columns, cancellable, error)) {
		g_string_free (objects_stmt, TRUE);

		return FALSE;
	}

	g_hash_table_remove (known_columns, E_CACHE_COLUMN_UID);
	g_hash_table_remove (known_columns, E_CACHE_COLUMN_REVISION);
	g_hash_table_remove (known_columns, E_CACHE_COLUMN_OBJECT);
	g_hash_table_remove (known_columns, E_CACHE_COLUMN_STATE);

	for (link = other_columns; link; link = g_slist_next (link)) {
		const ECacheColumnInfo *info = link->data;

		if (!info)
			continue;

		if (g_hash_table_remove (known_columns, info->name))
			continue;

		if (!e_cache_sqlite_exec_printf (cache,
			"ALTER TABLE " E_CACHE_TABLE_OBJECTS " ADD COLUMN %Q %s",
			NULL, NULL, cancellable, error,
			info->name, info->type)) {
			g_hash_table_destroy (known_columns);

			return FALSE;
		}
	}

	g_hash_table_destroy (known_columns);

	for (link = other_columns; link; link = g_slist_next (link)) {
		const ECacheColumnInfo *info = link->data;

		if (!info || !info->index_name)
			continue;

		if (!e_cache_sqlite_exec_printf (cache,
			"CREATE INDEX IF NOT EXISTS %Q ON " E_CACHE_TABLE_OBJECTS " (%s)",
			NULL, NULL, cancellable, error,
			info->index_name, info->name)) {
			return FALSE;
		}
	}

	return TRUE;
}

/**
 * e_cache_initialize_sync:
 * @cache: an #ECache
 * @filename: a filename of an SQLite database to use
 * @other_columns: (element-type ECacheColumnInfo) (nullable): an optional
 *    #GSList with additional columns to add to the objects table
 * @cancellable: optional #GCancellable object, or %NULL
 * @error: return location for a #GError, or %NULL
 *
 * Initializes the @cache and opens the @filename database.
 * This should be called by the descendant.
 *
 * The @other_columns are added to the objects table (@E_CACHE_TABLE_OBJECTS).
 * Values for these columns are returned by e_cache_get()
 * and can be stored with e_cache_put().
 *
 * Returns: Whether succeeded.
 *
 * Since: 3.26
 **/
gboolean
e_cache_initialize_sync (ECache *cache,
			 const gchar *filename,
			 const GSList *other_columns,
			 GCancellable *cancellable,
			 GError **error)
{
	gchar *dirname;
	gboolean success;

	g_return_val_if_fail (E_IS_CACHE (cache), FALSE);
	g_return_val_if_fail (cache->priv->filename == NULL, FALSE);

	/* Ensure existance of the directories leading up to 'filename' */
	dirname = g_path_get_dirname (filename);
	if (g_mkdir_with_parents (dirname, 0777) < 0) {
		g_set_error (error, E_CACHE_ERROR, E_CACHE_ERROR_LOAD,
			_("Can not make parent directory: %s"),
			g_strerror (errno));
		g_free (dirname);

		return FALSE;
	}

	g_free (dirname);

	g_rec_mutex_lock (&cache->priv->lock);

	success = e_cache_init_sqlite (cache, filename, cancellable, error) &&
		e_cache_init_tables (cache, other_columns, cancellable, error);

	g_rec_mutex_unlock (&cache->priv->lock);

	return success;
}

/**
 * e_cache_get_filename:
 * @cache: an #ECache
 *
 * Returns: a filename of the @cache, with which it had been initialized.
 *
 * Since: 3.26
 **/
const gchar *
e_cache_get_filename (ECache *cache)
{
	g_return_val_if_fail (E_IS_CACHE (cache), NULL);

	return cache->priv->filename;
}

/**
 * e_cache_get_version:
 * @cache: an #ECache
 *
 * Returns: A cache data version. This is meant to be used by the descendants.
 *
 * Since: 3.26
 **/
gint
e_cache_get_version (ECache *cache)
{
	gchar *value;
	gint version = -1;

	g_return_val_if_fail (E_IS_CACHE (cache), -1);

	value = e_cache_dup_key_internal (cache, FALSE, E_CACHE_KEY_VERSION, NULL);

	if (value) {
		version = g_ascii_strtoll (value, NULL, 10);
		g_free (value);
	}

	return version;
}

/**
 * e_cache_set_version:
 * @cache: an #ECache
 * @version: a cache data version to set
 *
 * Sets a cache data version. This is meant to be used by the descendants.
 * The @version should be greater than zero.
 *
 * Since: 3.26
 **/
void
e_cache_set_version (ECache *cache,
		     gint version)
{
	gchar *value;

	g_return_if_fail (E_IS_CACHE (cache));
	g_return_if_fail (version > 0);

	value = g_strdup_printf ("%d", version);
	e_cache_set_key_internal (cache, FALSE, E_CACHE_KEY_VERSION, value, NULL);
	g_free (value);
}

/**
 * e_cache_dup_revision:
 * @cache: an #ECache
 *
 * Returns: (transfer full): A revision of the whole @cache. This is meant to be
 *    used by the descendants. Free the returned pointer with g_free(), when no
 *    longer needed.
 *
 * Since: 3.26
 **/
gchar *
e_cache_dup_revision (ECache *cache)
{
	g_return_val_if_fail (E_IS_CACHE (cache), NULL);

	return e_cache_dup_key_internal (cache, FALSE, E_CACHE_KEY_REVISION, NULL);
}

/**
 * e_cache_set_revision:
 * @cache: an #ECache
 * @revision: (nullable): a revision to set; use %NULL to unset it
 *
 * Sets the @revision of the whole @cache. This is not meant to be
 * used by the descendants, because the revision is updated automatically
 * when needed. The descendants can listen to "revision-changed" signal.
 *
 * Since: 3.26
 **/
void
e_cache_set_revision (ECache *cache,
		      const gchar *revision)
{
	g_return_if_fail (E_IS_CACHE (cache));

	e_cache_set_key_internal (cache, FALSE, E_CACHE_KEY_REVISION, revision, NULL);

	g_signal_emit (cache, signals[REVISION_CHANGED], 0, NULL);
}

/**
 * e_cache_change_revision:
 * @cache: an #ECache
 *
 * Instructs the @cache to change its revision. In case the revision
 * change is frozen with e_cache_freeze_revision_change() it notes to
 * change the revision once the revision change is fully thaw.
 *
 * Since: 3.26
 **/
void
e_cache_change_revision (ECache *cache)
{
	g_return_if_fail (E_IS_CACHE (cache));

	g_rec_mutex_lock (&cache->priv->lock);

	if (e_cache_is_revision_change_frozen (cache)) {
		cache->priv->needs_revision_change = TRUE;
	} else {
		gchar time_string[100] = { 0 };
		const struct tm *tm = NULL;
		time_t t;
		gint64 revision_time;
		gchar *revision;

		revision_time = g_get_real_time () / (1000 * 1000);
		t = (time_t) revision_time;

		if (revision_time != cache->priv->last_revision_time) {
			cache->priv->revision_counter = 0;
			cache->priv->last_revision_time = revision_time;
		}

		tm = gmtime (&t);
		if (tm)
			strftime (time_string, 100, "%Y-%m-%dT%H:%M:%SZ", tm);

		revision = g_strdup_printf ("%s(%d)", time_string, cache->priv->revision_counter++);

		e_cache_set_revision (cache, revision);

		g_free (revision);
	}

	g_rec_mutex_unlock (&cache->priv->lock);
}

/**
 * e_cache_freeze_revision_change:
 * @cache: an #ECache
 *
 * Freezes automatic revision change for the @cache. The function
 * can be called multiple times, but each such call requires its
 * pair function e_cache_thaw_revision_change() call. See also
 * e_cache_change_revision().
 *
 * Since: 3.26
 **/
void
e_cache_freeze_revision_change (ECache *cache)
{
	g_return_if_fail (E_IS_CACHE (cache));

	g_rec_mutex_lock (&cache->priv->lock);

	cache->priv->revision_change_frozen++;
	g_warn_if_fail (cache->priv->revision_change_frozen != 0);

	g_rec_mutex_unlock (&cache->priv->lock);
}

/**
 * e_cache_thaw_revision_change:
 * @cache: an #ECache
 *
 * Thaws automatic revision change for the @cache. It's the pair
 * function of e_cache_freeze_revision_change().
 *
 * Since: 3.26
 **/
void
e_cache_thaw_revision_change (ECache *cache)
{
	g_return_if_fail (E_IS_CACHE (cache));

	g_rec_mutex_lock (&cache->priv->lock);

	if (!cache->priv->revision_change_frozen) {
		g_warn_if_fail (cache->priv->revision_change_frozen > 0);
	} else {
		cache->priv->revision_change_frozen--;
		if (!cache->priv->revision_change_frozen &&
		    cache->priv->needs_revision_change) {
			cache->priv->needs_revision_change = FALSE;
			e_cache_change_revision (cache);
		}
	}

	g_rec_mutex_unlock (&cache->priv->lock);
}

/**
 * e_cache_is_revision_change_frozen:
 * @cache: an #ECache
 *
 * Returns: Whether automatic revision change for the @cache
 *    is currently frozen.
 *
 * Since: 3.26
 **/
gboolean
e_cache_is_revision_change_frozen (ECache *cache)
{
	gboolean frozen;

	g_return_val_if_fail (E_IS_CACHE (cache), FALSE);

	g_rec_mutex_lock (&cache->priv->lock);
	frozen = cache->priv->revision_change_frozen > 0;
	g_rec_mutex_unlock (&cache->priv->lock);

	return frozen;
}

/**
 * e_cache_erase:
 * @cache: an #ECache
 *
 * Erases the cache and all of its content from the disk.
 * The only valid operation after this is to free the @cache.
 *
 * Since: 3.26
 **/
void
e_cache_erase (ECache *cache)
{
	ECacheClass *klass;

	g_return_if_fail (E_IS_CACHE (cache));

	if (!cache->priv->db)
		return;

	klass = E_CACHE_GET_CLASS (cache);
	g_return_if_fail (klass != NULL);

	if (klass->erase)
		klass->erase (cache);

	sqlite3_close (cache->priv->db);
	cache->priv->db = NULL;

	g_unlink (cache->priv->filename);

	g_free (cache->priv->filename);
	cache->priv->filename = NULL;
}

static gboolean
e_cache_count_rows_cb (ECache *cache,
		       gint ncols,
		       const gchar **column_names,
		       const gchar **column_values,
		       gpointer user_data)
{
	guint *pnrows = user_data;

	g_return_val_if_fail (pnrows != NULL, FALSE);

	*pnrows = (*pnrows) + 1;

	return TRUE;
}

/**
 * e_cache_contains:
 * @cache: an #ECache
 * @uid: a unique identifier of an object
 * @deleted_flag: one of #ECacheDeletedFlag enum
 *
 * Checkes whether the @cache contains an object with
 * the given @uid.
 *
 * Returns: Whether the object had been found.
 *
 * Since: 3.26
 **/
gboolean
e_cache_contains (ECache *cache,
		  const gchar *uid,
		  ECacheDeletedFlag deleted_flag)
{
	guint nrows = 0;

	g_return_val_if_fail (E_IS_CACHE (cache), FALSE);
	g_return_val_if_fail (uid != NULL, FALSE);

	if (deleted_flag == E_CACHE_INCLUDE_DELETED) {
		e_cache_sqlite_exec_printf (cache,
			"SELECT " E_CACHE_COLUMN_UID " FROM " E_CACHE_TABLE_OBJECTS
			" WHERE " E_CACHE_COLUMN_UID " = %Q"
			" LIMIT 2",
			e_cache_count_rows_cb, &nrows, NULL, NULL,
			uid);
	} else {
		e_cache_sqlite_exec_printf (cache,
			"SELECT " E_CACHE_COLUMN_UID " FROM " E_CACHE_TABLE_OBJECTS
			" WHERE " E_CACHE_COLUMN_UID " = %Q AND " E_CACHE_COLUMN_STATE " != %d"
			" LIMIT 2",
			e_cache_count_rows_cb, &nrows, NULL, NULL,
			uid, E_OFFLINE_STATE_LOCALLY_DELETED);
	}

	g_warn_if_fail (nrows <= 1);

	return nrows > 0;
}

struct GetObjectData {
	gchar *object;
	gchar **out_revision;
	ECacheColumnValues **out_other_columns;
};

static gboolean
e_cache_get_object_cb (ECache *cache,
		       gint ncols,
		       const gchar **column_names,
		       const gchar **column_values,
		       gpointer user_data)
{
	struct GetObjectData *gd = user_data;
	gint ii;

	g_return_val_if_fail (gd != NULL, FALSE);
	g_return_val_if_fail (column_names != NULL, FALSE);
	g_return_val_if_fail (column_values != NULL, FALSE);

	for (ii = 0; ii < ncols; ii++) {
		if (g_ascii_strcasecmp (column_names[ii], E_CACHE_COLUMN_UID) == 0 ||
		    g_ascii_strcasecmp (column_names[ii], E_CACHE_COLUMN_STATE) == 0) {
			/* Skip these two */
		} else if (g_ascii_strcasecmp (column_names[ii], E_CACHE_COLUMN_REVISION) == 0) {
			if (gd->out_revision)
				*gd->out_revision = g_strdup (column_values[ii]);
		} else if (g_ascii_strcasecmp (column_names[ii], E_CACHE_COLUMN_OBJECT) == 0) {
			gd->object = g_strdup (column_values[ii]);
		} else if (gd->out_other_columns) {
			if (!*gd->out_other_columns)
				*gd->out_other_columns = e_cache_column_values_new ();

			e_cache_column_values_put (*gd->out_other_columns, column_names[ii], column_values[ii]);
		} else if (gd->object && (!gd->out_revision || *gd->out_revision)) {
			/* Short-break the cycle when the other columns are not requested and
			   the object/revision values were already read. */
			break;
		}
	}

	return TRUE;
}

static gchar *
e_cache_get_object_internal (ECache *cache,
			     gboolean include_deleted,
			     const gchar *uid,
			     gchar **out_revision,
			     ECacheColumnValues **out_other_columns,
			     GCancellable *cancellable,
			     GError **error)
{
	struct GetObjectData gd;
	gboolean success;

	g_return_val_if_fail (E_IS_CACHE (cache), NULL);
	g_return_val_if_fail (uid != NULL, NULL);

	if (out_revision)
		*out_revision = NULL;

	if (out_other_columns)
		*out_other_columns = NULL;

	gd.object = NULL;
	gd.out_revision = out_revision;
	gd.out_other_columns = out_other_columns;

	if (include_deleted) {
		success = e_cache_sqlite_exec_printf (cache,
			"SELECT * FROM " E_CACHE_TABLE_OBJECTS
			" WHERE " E_CACHE_COLUMN_UID " = %Q",
			e_cache_get_object_cb, &gd, cancellable, error,
			uid);
	} else {
		success = e_cache_sqlite_exec_printf (cache,
			"SELECT * FROM " E_CACHE_TABLE_OBJECTS
			" WHERE " E_CACHE_COLUMN_UID " = %Q AND " E_CACHE_COLUMN_STATE " != %d",
			e_cache_get_object_cb, &gd, cancellable, error,
			uid, E_OFFLINE_STATE_LOCALLY_DELETED);
	}

	if (success && !gd.object)
		g_set_error (error, E_CACHE_ERROR, E_CACHE_ERROR_NOT_FOUND, _("Object “%s” not found"), uid);

	return gd.object;
}

/**
 * e_cache_get:
 * @cache: an #ECache
 * @uid: a unique identifier of an object
 * @out_revision: (out) (nullable) (transfer full): an out variable for a revision
 *    of the object, or %NULL to ignore
 * @out_other_columns: (out) (nullable) (transfer full): an out
 *    variable for #ECacheColumnValues other columns, as defined when creating the @cache, or %NULL to ignore
 * @cancellable: optional #GCancellable object, or %NULL
 * @error: return location for a #GError, or %NULL
 *
 * Returns an object with the given @uid. This function does not consider locally
 * deleted objects. The @out_revision is set to the object revision, if not %NULL.
 * Free it with g_free() when no longer needed. Similarly the @out_other_columns
 * contains a column name to column value strings for additional columns which had
 * been requested when calling e_cache_initialize_sync(), if not %NULL.
 * Free the returned #ECacheColumnValues with e_cache_column_values_free(), when
 * no longer needed.
 *
 * Returns: (nullable) (transfer full): An object with the given @uid. Free it
 *    with g_free(), when no longer needed. Returns %NULL on error, like when
 *    the object could not be found.
 *
 * Since: 3.26
 **/
gchar *
e_cache_get (ECache *cache,
	     const gchar *uid,
	     gchar **out_revision,
	     ECacheColumnValues **out_other_columns,
	     GCancellable *cancellable,
	     GError **error)
{
	g_return_val_if_fail (E_IS_CACHE (cache), NULL);
	g_return_val_if_fail (uid != NULL, NULL);

	return e_cache_get_object_internal (cache, FALSE, uid, out_revision, out_other_columns, cancellable, error);
}

/**
 * e_cache_get_object_include_deleted:
 * @cache: an #ECache
 * @uid: a unique identifier of an object
 * @out_revision: (out) (nullable) (transfer full): an out variable for a revision
 *    of the object, or %NULL to ignore
 * @out_other_columns: (out) (nullable) (transfer full): an out
 *    variable for #ECacheColumnValues other columns, as defined when creating the @cache, or %NULL to ignore
 * @cancellable: optional #GCancellable object, or %NULL
 * @error: return location for a #GError, or %NULL
 *
 * The same as e_cache_get(), only considers also locally deleted objects.
 *
 * Returns: (nullable) (transfer full): An object with the given @uid. Free it
 *    with g_free(), when no longer needed. Returns %NULL on error, like when
 *    the object could not be found.
 *
 * Since: 3.30
 **/
gchar *
e_cache_get_object_include_deleted (ECache *cache,
				    const gchar *uid,
				    gchar **out_revision,
				    ECacheColumnValues **out_other_columns,
				    GCancellable *cancellable,
				    GError **error)
{
	g_return_val_if_fail (E_IS_CACHE (cache), NULL);
	g_return_val_if_fail (uid != NULL, NULL);

	return e_cache_get_object_internal (cache, TRUE, uid, out_revision, out_other_columns, cancellable, error);
}

static gboolean
e_cache_put_locked (ECache *cache,
		    const gchar *uid,
		    const gchar *revision,
		    const gchar *object,
		    ECacheColumnValues *other_columns,
		    EOfflineState offline_state,
		    gboolean is_replace,
		    GCancellable *cancellable,
		    GError **error)
{
	ECacheColumnValues *my_other_columns = NULL;
	gboolean success = TRUE;

	g_return_val_if_fail (E_IS_CACHE (cache), FALSE);
	g_return_val_if_fail (uid != NULL, FALSE);
	g_return_val_if_fail (object != NULL, FALSE);

	if (!other_columns) {
		my_other_columns = e_cache_column_values_new ();
		other_columns = my_other_columns;
	}

	g_signal_emit (cache,
		       signals[BEFORE_PUT],
		       0,
		       uid, revision, object, other_columns,
		       is_replace, cancellable, error,
		       &success);

	if (success) {
		ECacheClass *klass;

		klass = E_CACHE_GET_CLASS (cache);
		g_return_val_if_fail (klass != NULL, FALSE);
		g_return_val_if_fail (klass->put_locked != NULL, FALSE);

		success = klass->put_locked (cache, uid, revision, object, other_columns, offline_state, is_replace, cancellable, error);

		if (success)
			e_cache_change_revision (cache);
	}

	e_cache_column_values_free (my_other_columns);

	return success;
}

/**
 * e_cache_put:
 * @cache: an #ECache
 * @uid: a unique identifier of an object
 * @revision: (nullable): a revision of the object
 * @object: the object itself
 * @other_columns: (nullable): an #ECacheColumnValues with other columns to set; can be %NULL
 * @offline_flag: one of #ECacheOfflineFlag, whether putting this object in offline
 * @cancellable: optional #GCancellable object, or %NULL
 * @error: return location for a #GError, or %NULL
 *
 * Stores an object into the cache. Depending on @offline_flag, this update
 * the object's offline state accordingly. When the @offline_flag is set
 * to %E_CACHE_IS_ONLINE, then it's set to #E_OFFLINE_STATE_SYNCED, like
 * to be fully synchronized with the server, regardless of its previous
 * offline state. Overwriting locally deleted object behaves like an addition
 * of a completely new object.
 *
 * Returns: Whether succeeded.
 *
 * Since: 3.26
 **/
gboolean
e_cache_put (ECache *cache,
	     const gchar *uid,
	     const gchar *revision,
	     const gchar *object,
	     ECacheColumnValues *other_columns,
	     ECacheOfflineFlag offline_flag,
	     GCancellable *cancellable,
	     GError **error)
{
	EOfflineState offline_state;
	gboolean success = TRUE, is_replace;

	g_return_val_if_fail (E_IS_CACHE (cache), FALSE);
	g_return_val_if_fail (uid != NULL, FALSE);
	g_return_val_if_fail (object != NULL, FALSE);

	e_cache_lock (cache, E_CACHE_LOCK_WRITE);

	if (offline_flag == E_CACHE_IS_ONLINE) {
		is_replace = e_cache_contains (cache, uid, E_CACHE_EXCLUDE_DELETED);
		offline_state = E_OFFLINE_STATE_SYNCED;
	} else {
		is_replace = e_cache_contains (cache, uid, E_CACHE_INCLUDE_DELETED);
		if (is_replace) {
			GError *local_error = NULL;

			offline_state = e_cache_get_offline_state (cache, uid, cancellable, &local_error);

			if (local_error) {
				success = FALSE;
				g_propagate_error (error, local_error);
			} else if (offline_state != E_OFFLINE_STATE_LOCALLY_CREATED) {
				offline_state = E_OFFLINE_STATE_LOCALLY_MODIFIED;
			}
		} else {
			offline_state = E_OFFLINE_STATE_LOCALLY_CREATED;
		}
	}

	success = success && e_cache_put_locked (cache, uid, revision, object, other_columns,
		offline_state, is_replace, cancellable, error);

	e_cache_unlock (cache, success ? E_CACHE_UNLOCK_COMMIT : E_CACHE_UNLOCK_ROLLBACK);

	return success;
}

/**
 * e_cache_remove:
 * @cache: an #ECache
 * @uid: a unique identifier of an object
 * @offline_flag: one of #ECacheOfflineFlag, whether removing the object in offline
 * @cancellable: optional #GCancellable object, or %NULL
 * @error: return location for a #GError, or %NULL
 *
 * Removes the object with the given @uid from the @cache. Based on the @offline_flag,
 * it can remove also any information about locally made offline changes. Removing
 * the object with %E_CACHE_IS_OFFLINE will still remember it for later use
 * with e_cache_get_offline_changes().
 *
 * Returns: Whether succeeded.
 *
 * Since: 3.26
 **/
gboolean
e_cache_remove (ECache *cache,
		const gchar *uid,
		ECacheOfflineFlag offline_flag,
		GCancellable *cancellable,
		GError **error)
{
	ECacheClass *klass;
	gboolean success = TRUE;

	g_return_val_if_fail (E_IS_CACHE (cache), FALSE);
	g_return_val_if_fail (uid != NULL, FALSE);

	klass = E_CACHE_GET_CLASS (cache);
	g_return_val_if_fail (klass != NULL, FALSE);
	g_return_val_if_fail (klass->remove_locked != NULL, FALSE);

	e_cache_lock (cache, E_CACHE_LOCK_WRITE);

	if (offline_flag == E_CACHE_IS_ONLINE) {
		success = klass->remove_locked (cache, uid, cancellable, error);
	} else {
		EOfflineState offline_state;

		offline_state = e_cache_get_offline_state (cache, uid, cancellable, error);
		if (offline_state == E_OFFLINE_STATE_UNKNOWN) {
			success = FALSE;
		} else if (offline_state == E_OFFLINE_STATE_LOCALLY_CREATED) {
			success = klass->remove_locked (cache, uid, cancellable, error);
		} else {
			g_signal_emit (cache,
				       signals[BEFORE_REMOVE],
				       0,
				       uid, cancellable, error,
				       &success);

			if (success) {
				success = e_cache_set_offline_state (cache, uid,
					E_OFFLINE_STATE_LOCALLY_DELETED, cancellable, error);
			}
		}
	}

	if (success)
		e_cache_change_revision (cache);

	e_cache_unlock (cache, success ? E_CACHE_UNLOCK_COMMIT : E_CACHE_UNLOCK_ROLLBACK);

	return success;
}

/**
 * e_cache_remove_all:
 * @cache: an #ECache
 * @cancellable: optional #GCancellable object, or %NULL
 * @error: return location for a #GError, or %NULL
 *
 * Removes all objects from the @cache in one call.
 *
 * Returns: Whether succeeded.
 *
 * Since: 3.26
 **/
gboolean
e_cache_remove_all (ECache *cache,
		    GCancellable *cancellable,
		    GError **error)
{
	ECacheClass *klass;
	GSList *uids = NULL;
	gboolean success;

	g_return_val_if_fail (E_IS_CACHE (cache), FALSE);

	klass = E_CACHE_GET_CLASS (cache);
	g_return_val_if_fail (klass != NULL, FALSE);
	g_return_val_if_fail (klass->remove_all_locked != NULL, FALSE);

	e_cache_lock (cache, E_CACHE_LOCK_WRITE);

	success = e_cache_get_uids (cache, E_CACHE_INCLUDE_DELETED, &uids, NULL, cancellable, error);

	if (success && uids)
		success = klass->remove_all_locked (cache, uids, cancellable, error);

	if (success) {
		e_cache_sqlite_maybe_vacuum (cache, cancellable, NULL);
		e_cache_change_revision (cache);
	}

	e_cache_unlock (cache, success ? E_CACHE_UNLOCK_COMMIT : E_CACHE_UNLOCK_ROLLBACK);

	g_slist_free_full (uids, g_free);

	return success;
}

static gboolean
e_cache_get_uint64_cb (ECache *cache,
		       gint ncols,
		       const gchar **column_names,
		       const gchar **column_values,
		       gpointer user_data)
{
	guint64 *pui64 = user_data;

	g_return_val_if_fail (pui64 != NULL, FALSE);

	if (ncols == 1) {
		*pui64 = column_values[0] ? g_ascii_strtoull (column_values[0], NULL, 10) : 0;
	} else {
		*pui64 = 0;
	}

	return TRUE;
}

static gboolean
e_cache_get_int64_cb (ECache *cache,
		      gint ncols,
		      const gchar **column_names,
		      const gchar **column_values,
		      gpointer user_data)
{
	gint64 *pi64 = user_data;

	g_return_val_if_fail (pi64 != NULL, FALSE);

	if (ncols == 1) {
		*pi64 = column_values[0] ? g_ascii_strtoll (column_values[0], NULL, 10) : 0;
	} else {
		*pi64 = 0;
	}

	return TRUE;
}

/**
 * e_cache_get_count:
 * @cache: an #ECache
 * @deleted_flag: one of #ECacheDeletedFlag enum
 * @cancellable: optional #GCancellable object, or %NULL
 * @error: return location for a #GError, or %NULL
 *
 * Returns: Count of objects stored in the @cache.
 *
 * Since: 3.26
 **/
guint
e_cache_get_count (ECache *cache,
		   ECacheDeletedFlag deleted_flag,
		   GCancellable *cancellable,
		   GError **error)
{
	guint64 nobjects = 0;

	g_return_val_if_fail (E_IS_CACHE (cache), 0);

	if (deleted_flag == E_CACHE_INCLUDE_DELETED) {
		e_cache_sqlite_exec_printf (cache,
			"SELECT COUNT(*) FROM " E_CACHE_TABLE_OBJECTS,
			e_cache_get_uint64_cb, &nobjects, cancellable, error);
	} else {
		e_cache_sqlite_exec_printf (cache,
			"SELECT COUNT(*) FROM " E_CACHE_TABLE_OBJECTS
			" WHERE " E_CACHE_COLUMN_STATE " != %d",
			e_cache_get_uint64_cb, &nobjects, NULL, NULL,
			E_OFFLINE_STATE_LOCALLY_DELETED);
	}

	return nobjects;
}

struct GatherRowsData {
	GSList **out_uids;
	GSList **out_revisions;
	GSList **out_objects;
};

static gboolean
e_cache_gather_rows_data_cb (ECache *cache,
			     const gchar *uid,
			     const gchar *revision,
			     const gchar *object,
			     EOfflineState offline_state,
			     gint ncols,
			     const gchar *column_names[],
			     const gchar *column_values[],
			     gpointer user_data)
{
	struct GatherRowsData *gd = user_data;

	g_return_val_if_fail (gd != NULL, FALSE);

	if (gd->out_uids)
		*gd->out_uids = g_slist_prepend (*gd->out_uids, g_strdup (uid));

	if (gd->out_revisions)
		*gd->out_revisions = g_slist_prepend (*gd->out_revisions, g_strdup (revision));

	if (gd->out_objects)
		*gd->out_objects = g_slist_prepend (*gd->out_objects, g_strdup (object));

	return TRUE;
}

/**
 * e_cache_get_uids:
 * @cache: an #ECache
 * @deleted_flag: one of #ECacheDeletedFlag enum
 * @out_uids: (out) (transfer full) (element-type utf8): a pointer to #GSList to store the found uid to
 * @out_revisions: (out) (transfer full) (element-type utf8) (nullable): a pointer to #GSList to store
 *    the found revisions to, or %NULL
 * @cancellable: optional #GCancellable object, or %NULL
 * @error: return location for a #GError, or %NULL
 *
 * Gets a list of unique object identifiers stored in the @cache, optionally
 * together with their revisions. The uids are not returned in any particular
 * order, but the position between @out_uids and @out_revisions matches
 * the same object.
 *
 * Both @out_uids and @out_revisions contain newly allocated #GSList, which
 * should be freed with g_slist_free_full (slist, g_free); when no longer needed.
 *
 * Returns: Whether succeeded. It doesn't necessarily mean that there was
 *    any object stored in the @cache.
 *
 * Since: 3.26
 **/
gboolean
e_cache_get_uids (ECache *cache,
		  ECacheDeletedFlag deleted_flag,
		  GSList **out_uids,
		  GSList **out_revisions,
		  GCancellable *cancellable,
		  GError **error)
{
	struct GatherRowsData gr;

	g_return_val_if_fail (E_IS_CACHE (cache), FALSE);
	g_return_val_if_fail (out_uids, FALSE);

	gr.out_uids = out_uids;
	gr.out_revisions = out_revisions;
	gr.out_objects = NULL;

	return e_cache_foreach (cache, deleted_flag, NULL,
		e_cache_gather_rows_data_cb, &gr, cancellable, error);
}

/**
 * e_cache_get_objects:
 * @cache: an #ECache
 * @deleted_flag: one of #ECacheDeletedFlag enum
 * @out_objects: (out) (transfer full) (element-type utf8): a pointer to #GSList to store the found objects to
 * @out_revisions: (out) (transfer full) (element-type utf8) (nullable): a pointer to #GSList to store
 *    the found revisions to, or %NULL
 * @cancellable: optional #GCancellable object, or %NULL
 * @error: return location for a #GError, or %NULL
 *
 * Gets a list of objects stored in the @cache, optionally together with
 * their revisions. The uids are not returned in any particular order,
 * but the position between @out_objects and @out_revisions matches
 * the same object.
 *
 * Both @out_objects and @out_revisions contain newly allocated #GSList, which
 * should be freed with g_slist_free_full (slist, g_free); when no longer needed.
 *
 * Returns: Whether succeeded. It doesn't necessarily mean that there was
 *    any object stored in the @cache.
 *
 * Since: 3.26
 **/
gboolean
e_cache_get_objects (ECache *cache,
		     ECacheDeletedFlag deleted_flag,
		     GSList **out_objects,
		     GSList **out_revisions,
		     GCancellable *cancellable,
		     GError **error)
{
	struct GatherRowsData gr;

	g_return_val_if_fail (E_IS_CACHE (cache), FALSE);
	g_return_val_if_fail (out_objects, FALSE);

	gr.out_uids = NULL;
	gr.out_revisions = out_revisions;
	gr.out_objects = out_objects;

	return e_cache_foreach (cache, deleted_flag, NULL,
		e_cache_gather_rows_data_cb, &gr, cancellable, error);
}

struct ForeachData {
	gint uid_index;
	gint revision_index;
	gint object_index;
	gint state_index;
	ECacheForeachFunc func;
	gpointer user_data;
};

static gboolean
e_cache_foreach_cb (ECache *cache,
		    gint ncols,
		    const gchar *column_names[],
		    const gchar *column_values[],
		    gpointer user_data)
{
	struct ForeachData *fe = user_data;
	EOfflineState offline_state;

	g_return_val_if_fail (fe != NULL, FALSE);
	g_return_val_if_fail (fe->func != NULL, FALSE);
	g_return_val_if_fail (column_names != NULL, FALSE);
	g_return_val_if_fail (column_values != NULL, FALSE);

	if (fe->uid_index == -1 ||
	    fe->revision_index == -1 ||
	    fe->object_index == -1 ||
	    fe->state_index == -1) {
		gint ii;

		for (ii = 0; ii < ncols && (fe->uid_index == -1 ||
		     fe->revision_index == -1 ||
		     fe->object_index == -1 ||
		     fe->state_index == -1); ii++) {
			if (!column_names[ii])
				continue;

			if (fe->uid_index == -1 && g_ascii_strcasecmp (column_names[ii], E_CACHE_COLUMN_UID) == 0) {
				fe->uid_index = ii;
			} else if (fe->revision_index == -1 && g_ascii_strcasecmp (column_names[ii], E_CACHE_COLUMN_REVISION) == 0) {
				fe->revision_index = ii;
			} else if (fe->object_index == -1 && g_ascii_strcasecmp (column_names[ii], E_CACHE_COLUMN_OBJECT) == 0) {
				fe->object_index = ii;
			} else if (fe->state_index == -1 && g_ascii_strcasecmp (column_names[ii], E_CACHE_COLUMN_STATE) == 0) {
				fe->state_index = ii;
			}
		}
	}

	g_return_val_if_fail (fe->uid_index >= 0 && fe->uid_index < ncols, FALSE);
	g_return_val_if_fail (fe->revision_index >= 0 && fe->revision_index < ncols, FALSE);
	g_return_val_if_fail (fe->object_index >= 0 && fe->object_index < ncols, FALSE);
	g_return_val_if_fail (fe->state_index >= 0 && fe->state_index < ncols, FALSE);

	if (!column_values[fe->state_index])
		offline_state = E_OFFLINE_STATE_UNKNOWN;
	else
		offline_state = g_ascii_strtoull (column_values[fe->state_index], NULL, 10);

	return fe->func (cache, column_values[fe->uid_index], column_values[fe->revision_index], column_values[fe->object_index],
		offline_state, ncols, column_names, column_values, fe->user_data);
}

/**
 * e_cache_foreach:
 * @cache: an #ECache
 * @deleted_flag: one of #ECacheDeletedFlag enum
 * @where_clause: (nullable): an optional SQLite WHERE clause part, or %NULL
 * @func: (scope call): an #ECacheForeachFunc function to call for each object
 * @user_data: user data for the @func
 * @cancellable: optional #GCancellable object, or %NULL
 * @error: return location for a #GError, or %NULL
 *
 * Calls @func for each found object, which satisfies the criteria
 * for both @deleted_flag and @where_clause.
 *
 * Note the @func should not call any SQLite commands, because it's invoked
 * within a SELECT statement execution.
 *
 * Returns: Whether succeeded.
 *
 * Since: 3.26
 **/
gboolean
e_cache_foreach (ECache *cache,
		 ECacheDeletedFlag deleted_flag,
		 const gchar *where_clause,
		 ECacheForeachFunc func,
		 gpointer user_data,
		 GCancellable *cancellable,
		 GError **error)
{
	struct ForeachData fe;
	GString *stmt;
	gboolean success;

	g_return_val_if_fail (E_IS_CACHE (cache), FALSE);
	g_return_val_if_fail (func, FALSE);

	stmt = g_string_new ("SELECT * FROM " E_CACHE_TABLE_OBJECTS);

	if (where_clause) {
		g_string_append (stmt, " WHERE ");

		if (deleted_flag == E_CACHE_INCLUDE_DELETED) {
			g_string_append (stmt, where_clause);
		} else {
			g_string_append_printf (stmt, E_CACHE_COLUMN_STATE "!=%d AND (%s)",
				E_OFFLINE_STATE_LOCALLY_DELETED, where_clause);
		}
	} else if (deleted_flag != E_CACHE_INCLUDE_DELETED) {
		g_string_append_printf (stmt, " WHERE " E_CACHE_COLUMN_STATE "!=%d", E_OFFLINE_STATE_LOCALLY_DELETED);
	}

	fe.func = func;
	fe.user_data = user_data;
	fe.uid_index = -1;
	fe.revision_index = -1;
	fe.object_index = -1;
	fe.state_index = -1;

	success = e_cache_sqlite_exec_internal (cache, stmt->str, e_cache_foreach_cb, &fe, cancellable, error);

	g_string_free (stmt, TRUE);

	return success;
}

struct ForeachUpdateRowData {
	gchar *uid;
	gchar *revision;
	gchar *object;
	EOfflineState offline_state;
	gint ncols;
	GPtrArray *column_values;
};

static void
foreach_update_row_data_free (gpointer ptr)
{
	struct ForeachUpdateRowData *fr = ptr;

	if (fr) {
		g_free (fr->uid);
		g_free (fr->revision);
		g_free (fr->object);
		g_ptr_array_free (fr->column_values, TRUE);
		g_slice_free (struct ForeachUpdateRowData, fr);
	}
}

struct ForeachUpdateData {
	gint uid_index;
	gint revision_index;
	gint object_index;
	gint state_index;
	GSList *rows; /* struct ForeachUpdateRowData * */
	GPtrArray *column_names;
};

static gboolean
e_cache_foreach_update_cb (ECache *cache,
			   gint ncols,
			   const gchar *column_names[],
			   const gchar *column_values[],
			   gpointer user_data)
{
	struct ForeachUpdateData *fu = user_data;
	struct ForeachUpdateRowData *rd;
	EOfflineState offline_state;
	GPtrArray *cnames, *cvalues;
	gint ii;

	g_return_val_if_fail (fu != NULL, FALSE);
	g_return_val_if_fail (column_names != NULL, FALSE);
	g_return_val_if_fail (column_values != NULL, FALSE);

	if (fu->uid_index == -1 ||
	    fu->revision_index == -1 ||
	    fu->object_index == -1 ||
	    fu->state_index == -1) {
		gint ii;

		for (ii = 0; ii < ncols && (fu->uid_index == -1 ||
		     fu->revision_index == -1 ||
		     fu->object_index == -1 ||
		     fu->state_index == -1); ii++) {
			if (!column_names[ii])
				continue;

			if (fu->uid_index == -1 && g_ascii_strcasecmp (column_names[ii], E_CACHE_COLUMN_UID) == 0) {
				fu->uid_index = ii;
			} else if (fu->revision_index == -1 && g_ascii_strcasecmp (column_names[ii], E_CACHE_COLUMN_REVISION) == 0) {
				fu->revision_index = ii;
			} else if (fu->object_index == -1 && g_ascii_strcasecmp (column_names[ii], E_CACHE_COLUMN_OBJECT) == 0) {
				fu->object_index = ii;
			} else if (fu->state_index == -1 && g_ascii_strcasecmp (column_names[ii], E_CACHE_COLUMN_STATE) == 0) {
				fu->state_index = ii;
			}
		}
	}

	g_return_val_if_fail (fu->uid_index >= 0 && fu->uid_index < ncols, FALSE);
	g_return_val_if_fail (fu->revision_index >= 0 && fu->revision_index < ncols, FALSE);
	g_return_val_if_fail (fu->object_index >= 0 && fu->object_index < ncols, FALSE);
	g_return_val_if_fail (fu->state_index >= 0 && fu->state_index < ncols, FALSE);

	if (!column_values[fu->state_index])
		offline_state = E_OFFLINE_STATE_UNKNOWN;
	else
		offline_state = g_ascii_strtoull (column_values[fu->state_index], NULL, 10);

	cnames = fu->column_names ? NULL : g_ptr_array_new_full (ncols, g_free);
	cvalues = g_ptr_array_new_full (ncols, g_free);

	for (ii = 0; ii < ncols; ii++) {
		if (fu->uid_index == ii ||
		    fu->revision_index == ii ||
		    fu->object_index == ii ||
		    fu->state_index == ii) {
			continue;
		}

		if (cnames)
			g_ptr_array_add (cnames, g_strdup (column_names[ii]));

		g_ptr_array_add (cvalues, g_strdup (column_values[ii]));
	}

	rd = g_slice_new0 (struct ForeachUpdateRowData);
	rd->uid = g_strdup (column_values[fu->uid_index]);
	rd->revision = g_strdup (column_values[fu->revision_index]);
	rd->object = g_strdup (column_values[fu->object_index]);
	rd->offline_state = offline_state;
	rd->ncols = cvalues->len;
	rd->column_values = cvalues;

	if (cnames)
		fu->column_names = cnames;

	fu->rows = g_slist_prepend (fu->rows, rd);

	g_return_val_if_fail (fu->column_names && (gint) fu->column_names->len == rd->ncols, FALSE);

	return TRUE;
}

/**
 * e_cache_foreach_update:
 * @cache: an #ECache
 * @deleted_flag: one of #ECacheDeletedFlag enum
 * @where_clause: (nullable): an optional SQLite WHERE clause part, or %NULL
 * @func: (scope call): an #ECacheUpdateFunc function to call for each object
 * @user_data: user data for the @func
 * @cancellable: optional #GCancellable object, or %NULL
 * @error: return location for a #GError, or %NULL
 *
 * Calls @func for each found object, which satisfies the criteria for both
 * @deleted_flag and @where_clause, letting the caller update values where
 * necessary. The return value of @func is used to determine whether the call
 * was successful, not whether there are any changes to be saved. If anything
 * fails during the call then the all changes are reverted.
 *
 * When there are requested any changes by the @func, this function also
 * calls e_cache_copy_missing_to_column_values() to ensure no descendant
 * column data is lost.
 *
 * Returns: Whether succeeded.
 *
 * Since: 3.26
 **/
gboolean
e_cache_foreach_update (ECache *cache,
			ECacheDeletedFlag deleted_flag,
			const gchar *where_clause,
			ECacheUpdateFunc func,
			gpointer user_data,
			GCancellable *cancellable,
			GError **error)
{
	GString *stmt_begin;
	gchar *uid = NULL;
	gint n_results;
	gboolean has_where = TRUE;
	gboolean success;

	g_return_val_if_fail (E_IS_CACHE (cache), FALSE);
	g_return_val_if_fail (func, FALSE);

	e_cache_lock (cache, E_CACHE_LOCK_WRITE);

	stmt_begin = g_string_new ("SELECT * FROM " E_CACHE_TABLE_OBJECTS);

	if (where_clause) {
		g_string_append (stmt_begin, " WHERE ");

		if (deleted_flag == E_CACHE_INCLUDE_DELETED) {
			g_string_append (stmt_begin, where_clause);
		} else {
			g_string_append_printf (stmt_begin, E_CACHE_COLUMN_STATE "!=%d AND (%s)",
				E_OFFLINE_STATE_LOCALLY_DELETED, where_clause);
		}
	} else if (deleted_flag != E_CACHE_INCLUDE_DELETED) {
		g_string_append_printf (stmt_begin, " WHERE " E_CACHE_COLUMN_STATE "!=%d", E_OFFLINE_STATE_LOCALLY_DELETED);
	} else {
		has_where = FALSE;
	}

	do {
		GString *stmt;
		GSList *link;
		struct ForeachUpdateData fu;

		fu.uid_index = -1;
		fu.revision_index = -1;
		fu.object_index = -1;
		fu.state_index = -1;
		fu.rows = NULL;
		fu.column_names = NULL;

		stmt = g_string_new (stmt_begin->str);

		if (uid) {
			if (has_where)
				g_string_append (stmt, " AND ");
			else
				g_string_append (stmt, " WHERE ");

			e_cache_sqlite_stmt_append_printf (stmt, E_CACHE_COLUMN_UID ">%Q", uid);
		}

		g_string_append_printf (stmt, " ORDER BY " E_CACHE_COLUMN_UID " ASC LIMIT %d", E_CACHE_UPDATE_BATCH_SIZE);

		success = e_cache_sqlite_exec_internal (cache, stmt->str, e_cache_foreach_update_cb, &fu, cancellable, error);

		g_string_free (stmt, TRUE);

		if (success) {
			n_results = 0;
			fu.rows = g_slist_reverse (fu.rows);

			for (link = fu.rows; success && link; link = g_slist_next (link), n_results++) {
				struct ForeachUpdateRowData *fr = link->data;

				success = fr && fr->column_values && fu.column_names;
				if (success) {
					gchar *new_revision = NULL;
					gchar *new_object = NULL;
					EOfflineState new_offline_state = fr->offline_state;
					ECacheColumnValues *new_other_columns = NULL;

					success = func (cache, fr->uid, fr->revision, fr->object, fr->offline_state,
						fr->ncols, (const gchar **) fu.column_names->pdata,
						(const gchar **) fr->column_values->pdata,
						&new_revision, &new_object, &new_offline_state, &new_other_columns,
						user_data);

					if (success && (
					    (new_revision && g_strcmp0 (new_revision, fr->revision) != 0) ||
					    (new_object && g_strcmp0 (new_object, fr->object) != 0) ||
					    (new_offline_state != fr->offline_state) ||
					    (new_other_columns && e_cache_column_values_get_size (new_other_columns) > 0))) {
						if (!new_other_columns)
							new_other_columns = e_cache_column_values_new ();

						e_cache_copy_missing_to_column_values (cache, fr->ncols,
							(const gchar **) fu.column_names->pdata,
							(const gchar **) fr->column_values->pdata,
							new_other_columns);

						success = e_cache_put_locked (cache,
							fr->uid,
							new_revision ? new_revision : fr->revision,
							new_object ? new_object : fr->object,
							new_other_columns,
							new_offline_state,
							TRUE, cancellable, error);
					}

					g_free (new_revision);
					g_free (new_object);
					e_cache_column_values_free (new_other_columns);

					if (!g_slist_next (link)) {
						g_free (uid);
						uid = g_strdup (fr->uid);
					}
				}
			}
		}

		g_slist_free_full (fu.rows, foreach_update_row_data_free);
		if (fu.column_names)
			g_ptr_array_free (fu.column_names, TRUE);
	} while (success && n_results == E_CACHE_UPDATE_BATCH_SIZE);

	g_string_free (stmt_begin, TRUE);
	g_free (uid);

	e_cache_unlock (cache, success ? E_CACHE_UNLOCK_COMMIT : E_CACHE_UNLOCK_ROLLBACK);

	return success;
}

/**
 * e_cache_copy_missing_to_column_values:
 * @cache: an #ECache
 * @ncols: count of columns, items in column_names and column_values
 * @column_names: (array length=ncols) (element-type utf8): column names
 * @column_values: (array length=ncols) (element-type utf8): column values
 * @other_columns: (inout): an #ECacheColumnValues to fill
 *
 * Adds every column value which is not part of the @other_columns to it,
 * except of E_CACHE_COLUMN_UID, E_CACHE_COLUMN_REVISION, E_CACHE_COLUMN_OBJECT
 * and E_CACHE_COLUMN_STATE columns.
 *
 * This can be used within the callback of e_cache_foreach_update().
 *
 * Since: 3.32
 **/
void
e_cache_copy_missing_to_column_values (ECache *cache,
				       gint ncols,
				       const gchar *column_names[],
				       const gchar *column_values[],
				       ECacheColumnValues *other_columns)
{
	gint ii;

	g_return_if_fail (E_IS_CACHE (cache));
	g_return_if_fail (column_names != NULL);
	g_return_if_fail (column_values != NULL);
	g_return_if_fail (other_columns != NULL);

	for (ii = 0; ii < ncols; ii++) {
		if (column_names[ii] && column_values[ii] &&
		    !e_cache_column_values_contains (other_columns, column_names[ii]) &&
		    g_ascii_strcasecmp (column_names[ii], E_CACHE_COLUMN_UID) != 0 &&
		    g_ascii_strcasecmp (column_names[ii], E_CACHE_COLUMN_REVISION) != 0 &&
		    g_ascii_strcasecmp (column_names[ii], E_CACHE_COLUMN_OBJECT) != 0 &&
		    g_ascii_strcasecmp (column_names[ii], E_CACHE_COLUMN_STATE) != 0) {
			e_cache_column_values_put (other_columns, column_names[ii], column_values[ii]);
		}
	}
}

/**
 * e_cache_get_offline_state:
 * @cache: an #ECache
 * @uid: a unique identifier of an object
 * @cancellable: optional #GCancellable object, or %NULL
 * @error: return location for a #GError, or %NULL
 *
 * Returns: Current offline state #EOfflineState for the given object.
 *    It returns %E_OFFLINE_STATE_UNKNOWN when the object could not be
 *    found or other error happened.
 *
 * Since: 3.26
 **/
EOfflineState
e_cache_get_offline_state (ECache *cache,
			   const gchar *uid,
			   GCancellable *cancellable,
			   GError **error)
{
	EOfflineState offline_state = E_OFFLINE_STATE_UNKNOWN;
	gint64 value = offline_state;

	g_return_val_if_fail (E_IS_CACHE (cache), E_OFFLINE_STATE_UNKNOWN);
	g_return_val_if_fail (uid != NULL, E_OFFLINE_STATE_UNKNOWN);

	if (!e_cache_contains (cache, uid, E_CACHE_INCLUDE_DELETED)) {
		g_set_error (error, E_CACHE_ERROR, E_CACHE_ERROR_NOT_FOUND, _("Object “%s” not found"), uid);
		return offline_state;
	}

	if (e_cache_sqlite_exec_printf (cache,
		"SELECT " E_CACHE_COLUMN_STATE " FROM " E_CACHE_TABLE_OBJECTS
		" WHERE " E_CACHE_COLUMN_UID " = %Q",
		e_cache_get_int64_cb, &value, cancellable, error,
		uid)) {
		offline_state = value;
	}

	return offline_state;
}

/**
 * e_cache_set_offline_state:
 * @cache: an #ECache
 * @uid: a unique identifier of an object
 * @state: an #EOfflineState to set
 * @cancellable: optional #GCancellable object, or %NULL
 * @error: return location for a #GError, or %NULL
 *
 * Sets an offline @state for the object identified by @uid.
 *
 * Returns: Whether succeeded.
 *
 * Since: 3.26
 **/
gboolean
e_cache_set_offline_state (ECache *cache,
			   const gchar *uid,
			   EOfflineState state,
			   GCancellable *cancellable,
			   GError **error)
{
	g_return_val_if_fail (E_IS_CACHE (cache), FALSE);
	g_return_val_if_fail (uid != NULL, FALSE);

	if (!e_cache_contains (cache, uid, E_CACHE_INCLUDE_DELETED)) {
		g_set_error (error, E_CACHE_ERROR, E_CACHE_ERROR_NOT_FOUND, _("Object “%s” not found"), uid);
		return FALSE;
	}

	return e_cache_sqlite_exec_printf (cache,
		"UPDATE " E_CACHE_TABLE_OBJECTS " SET " E_CACHE_COLUMN_STATE "=%d"
		" WHERE " E_CACHE_COLUMN_UID " = %Q",
		NULL, NULL, cancellable, error,
		state, uid);
}

static gboolean
e_cache_get_offline_changes_cb (ECache *cache,
				const gchar *uid,
				const gchar *revision,
				const gchar *object,
				EOfflineState offline_state,
				gint ncols,
				const gchar *column_names[],
				const gchar *column_values[],
				gpointer user_data)
{
	GSList **pchanges = user_data;

	g_return_val_if_fail (pchanges != NULL, FALSE);

	if (offline_state == E_OFFLINE_STATE_LOCALLY_CREATED ||
	    offline_state == E_OFFLINE_STATE_LOCALLY_MODIFIED ||
	    offline_state == E_OFFLINE_STATE_LOCALLY_DELETED) {
		*pchanges = g_slist_prepend (*pchanges, e_cache_offline_change_new (uid, revision, object, offline_state));
	}

	return TRUE;
}

/**
 * e_cache_get_offline_changes:
 * @cache: an #ECache
 * @cancellable: optional #GCancellable object, or %NULL
 * @error: return location for a #GError, or %NULL
 *
 * Gathers the list of all offline changes being done so far.
 * The returned #GSList contains #ECacheOfflineChange structure.
 * Use e_cache_clear_offline_changes() to clear all offline
 * changes at once.
 *
 * Returns: (transfer full) (element-type ECacheOfflineChange): A newly allocated list of all
 *    offline changes. Free it with g_slist_free_full (slist, e_cache_offline_change_free);
 *    when no longer needed.
 *
 * Since: 3.26
 **/
GSList *
e_cache_get_offline_changes (ECache *cache,
			     GCancellable *cancellable,
			     GError **error)
{
	GSList *changes = NULL;
	gchar *stmt;

	g_return_val_if_fail (E_IS_CACHE (cache), NULL);

	stmt = e_cache_sqlite_stmt_printf (E_CACHE_COLUMN_STATE "!=%d", E_OFFLINE_STATE_SYNCED);

	if (!e_cache_foreach (cache, E_CACHE_INCLUDE_DELETED, stmt, e_cache_get_offline_changes_cb, &changes, cancellable, error)) {
		g_slist_free_full (changes, e_cache_offline_change_free);
		changes = NULL;
	}

	e_cache_sqlite_stmt_free (stmt);

	return changes;
}

/**
 * e_cache_clear_offline_changes:
 * @cache: an #ECache
 * @cancellable: optional #GCancellable object, or %NULL
 * @error: return location for a #GError, or %NULL
 *
 * Marks all objects as being fully synchronized with the server and
 * removes those which are marked as locally deleted.
 *
 * Returns: Whether succeeded.
 *
 * Since: 3.26
 **/
gboolean
e_cache_clear_offline_changes (ECache *cache,
			       GCancellable *cancellable,
			       GError **error)
{
	ECacheClass *klass;
	gboolean success;

	g_return_val_if_fail (E_IS_CACHE (cache), FALSE);

	klass = E_CACHE_GET_CLASS (cache);
	g_return_val_if_fail (klass != NULL, FALSE);
	g_return_val_if_fail (klass->clear_offline_changes_locked != NULL, FALSE);

	e_cache_lock (cache, E_CACHE_LOCK_WRITE);

	success = klass->clear_offline_changes_locked (cache, cancellable, error);

	e_cache_unlock (cache, success ? E_CACHE_UNLOCK_COMMIT : E_CACHE_UNLOCK_ROLLBACK);

	return success;
}

/**
 * e_cache_set_key:
 * @cache: an #ECache
 * @key: a key name
 * @value: (nullable): a value to set, or %NULL to delete the key
 * @error: return location for a #GError, or %NULL
 *
 * Sets a @value of the user @key, or deletes it, if the @value is %NULL.
 *
 * Returns: Whether succeeded.
 *
 * Since: 3.26
 **/
gboolean
e_cache_set_key (ECache *cache,
		 const gchar *key,
		 const gchar *value,
		 GError **error)
{
	g_return_val_if_fail (E_IS_CACHE (cache), FALSE);
	g_return_val_if_fail (key != NULL, FALSE);

	return e_cache_set_key_internal (cache, TRUE, key, value, error);
}

/**
 * e_cache_dup_key:
 * @cache: an #ECache
 * @key: a key name
 * @error: return location for a #GError, or %NULL
 *
 * Returns: (transfer full): a value of the @key. Free the returned string
 *    with g_free(), when no longer needed.
 *
 * Since: 3.26
 **/
gchar *
e_cache_dup_key (ECache *cache,
		 const gchar *key,
		 GError **error)
{
	g_return_val_if_fail (E_IS_CACHE (cache), NULL);
	g_return_val_if_fail (key != NULL, NULL);

	return e_cache_dup_key_internal (cache, TRUE, key, error);
}

/**
 * e_cache_set_key_int:
 * @cache: an #ECache
 * @key: a key name
 * @value: an integer value to set
 * @error: return location for a #GError, or %NULL
 *
 * Sets an integer @value for the user @key.
 *
 * Returns: Whether succeeded.
 *
 * Since: 3.26
 **/
gboolean
e_cache_set_key_int (ECache *cache,
		     const gchar *key,
		     gint value,
		     GError **error)
{
	gchar *str_value;
	gboolean success;

	g_return_val_if_fail (E_IS_CACHE (cache), FALSE);
	g_return_val_if_fail (key != NULL, FALSE);

	str_value = g_strdup_printf ("%d", value);
	success = e_cache_set_key (cache, key, str_value, error);
	g_free (str_value);

	return success;
}

/**
 * e_cache_get_key_int:
 * @cache: an #ECache
 * @key: a key name
 * @error: return location for a #GError, or %NULL
 *
 * Reads the user @key value as an integer.
 *
 * Returns: The user @key value or -1 on error.
 *
 * Since: 3.26
 **/
gint
e_cache_get_key_int (ECache *cache,
		     const gchar *key,
		     GError **error)
{
	gchar *str_value;
	gint value;

	g_return_val_if_fail (E_IS_CACHE (cache), -1);

	str_value = e_cache_dup_key (cache, key, error);
	if (!str_value)
		return -1;

	value = g_ascii_strtoll (str_value, NULL, 10);
	g_free (str_value);

	return value;
}

/**
 * e_cache_lock:
 * @cache: an #ECache
 * @lock_type: an #ECacheLockType
 *
 * Locks the @cache thus other threads cannot use it.
 * This can be called recursively within one thread.
 * Each call should have its pair e_cache_unlock().
 *
 * Since: 3.26
 **/
void
e_cache_lock (ECache *cache,
	      ECacheLockType lock_type)
{
	g_return_if_fail (E_IS_CACHE (cache));

	g_rec_mutex_lock (&cache->priv->lock);

	cache->priv->in_transaction++;
	g_return_if_fail (cache->priv->in_transaction > 0);

	if (cache->priv->in_transaction == 1) {
		/* It's important to make the distinction between a
		 * transaction which will read or one which will write.
		 *
		 * While it's not well documented, when receiving the SQLITE_BUSY
		 * error status, one can only safely retry at the beginning of
		 * the transaction.
		 *
		 * If a transaction is 'upgraded' to require a writer lock
		 * half way through the transaction and SQLITE_BUSY is returned,
		 * the whole transaction would need to be retried from the beginning.
		 */
		cache->priv->lock_type = lock_type;

		switch (lock_type) {
		case E_CACHE_LOCK_READ:
			e_cache_sqlite_exec_internal (cache, "BEGIN", NULL, NULL, NULL, NULL);
			break;
		case E_CACHE_LOCK_WRITE:
			e_cache_sqlite_exec_internal (cache, "BEGIN IMMEDIATE", NULL, NULL, NULL, NULL);
			break;
		}
	} else {
		/* Warn about cases where a read transaction might be upgraded */
		if (lock_type == E_CACHE_LOCK_WRITE && cache->priv->lock_type == E_CACHE_LOCK_READ)
			g_warning (
				"A nested transaction wants to write, "
				"but the outermost transaction was started "
				"without a writer lock.");
	}
}

/**
 * e_cache_unlock:
 * @cache: an #ECache
 * @action: an #ECacheUnlockAction
 *
 * Unlocks the cache which was previouly locked with e_cache_lock().
 * The cache locked with #E_CACHE_LOCK_WRITE should use either
 * @action #E_CACHE_UNLOCK_COMMIT or #E_CACHE_UNLOCK_ROLLBACK,
 * while the #E_CACHE_LOCK_READ should use #E_CACHE_UNLOCK_NONE @action.
 *
 * Since: 3.26
 **/
void
e_cache_unlock (ECache *cache,
		ECacheUnlockAction action)
{
	g_return_if_fail (E_IS_CACHE (cache));
	g_return_if_fail (cache->priv->in_transaction > 0);

	cache->priv->in_transaction--;

	if (cache->priv->in_transaction == 0) {
		switch (action) {
		case E_CACHE_UNLOCK_NONE:
		case E_CACHE_UNLOCK_COMMIT:
			e_cache_sqlite_exec_internal (cache, "COMMIT", NULL, NULL, NULL, NULL);
			break;
		case E_CACHE_UNLOCK_ROLLBACK:
			e_cache_sqlite_exec_internal (cache, "ROLLBACK", NULL, NULL, NULL, NULL);
			break;
		}
	}

	g_rec_mutex_unlock (&cache->priv->lock);
}

/**
 * e_cache_get_sqlitedb:
 * @cache: an #ECache
 *
 * Returns: (transfer none): An SQLite3 database pointer. It is owned by the @cache.
 *
 * Since: 3.26
 **/
gpointer
e_cache_get_sqlitedb (ECache *cache)
{
	g_return_val_if_fail (E_IS_CACHE (cache), NULL);

	return cache->priv->db;
}

/**
 * e_cache_sqlite_exec:
 * @cache: an #ECache
 * @sql_stmt: an SQLite statement to execute
 * @cancellable: optional #GCancellable object, or %NULL
 * @error: return location for a #GError, or %NULL
 *
 * Executes an SQLite statement. Use e_cache_sqlite_select() for
 * SELECT statements.
 *
 * Returns: Whether succeeded.
 *
 * Since: 3.26
 **/
gboolean
e_cache_sqlite_exec (ECache *cache,
		     const gchar *sql_stmt,
		     GCancellable *cancellable,
		     GError **error)
{
	g_return_val_if_fail (E_IS_CACHE (cache), FALSE);

	return e_cache_sqlite_exec_internal (cache, sql_stmt, NULL, NULL, cancellable, error);
}

/**
 * e_cache_sqlite_select:
 * @cache: an #ECache
 * @sql_stmt: an SQLite SELECT statement to execute
 * @func: (scope call): an #ECacheSelectFunc function to call for each row
 * @user_data: user data for @func
 * @cancellable: optional #GCancellable object, or %NULL
 * @error: return location for a #GError, or %NULL
 *
 * Executes a SELECT statement @sql_stmt and calls @func for each row of the result.
 * Use e_cache_sqlite_exec() for statements which do not return row sets.
 *
 * Returns: Whether succeeded.
 *
 * Since: 3.26
 **/
gboolean
e_cache_sqlite_select (ECache *cache,
		       const gchar *sql_stmt,
		       ECacheSelectFunc func,
		       gpointer user_data,
		       GCancellable *cancellable,
		       GError **error)
{
	g_return_val_if_fail (E_IS_CACHE (cache), FALSE);
	g_return_val_if_fail (sql_stmt, FALSE);
	g_return_val_if_fail (func, FALSE);

	return e_cache_sqlite_exec_internal (cache, sql_stmt, func, user_data, cancellable, error);
}

/**
 * e_cache_sqlite_stmt_append_printf:
 * @stmt: a #GString statement to append to
 * @format: a printf-like format
 * @...: arguments for the @format
 *
 * Appends an SQLite statement fragment based on the @format and
 * its arguments to the @stmt.
 * The @format can contain any values recognized by sqlite3_mprintf().
 *
 * Since: 3.26
 **/
void
e_cache_sqlite_stmt_append_printf (GString *stmt,
				   const gchar *format,
				   ...)
{
	va_list args;
	gchar *tmp_stmt;

	g_return_if_fail (stmt != NULL);
	g_return_if_fail (format != NULL);

	va_start (args, format);
	tmp_stmt = sqlite3_vmprintf (format, args);
	va_end (args);

	g_string_append (stmt, tmp_stmt);

	sqlite3_free (tmp_stmt);
}

/**
 * e_cache_sqlite_stmt_printf:
 * @format: a printf-like format
 * @...: arguments for the @format
 *
 * Creates an SQLite statement based on the @format and its arguments.
 * The @format can contain any values recognized by sqlite3_mprintf().
 *
 * Returns: (transfer full): A new SQLite statement. Free the returned
 *    string with e_cache_sqlite_stmt_free() when no longer needed.
 *
 * Since: 3.26
 **/
gchar *
e_cache_sqlite_stmt_printf (const gchar *format,
			    ...)
{
	va_list args;
	gchar *stmt;

	g_return_val_if_fail (format != NULL, NULL);

	va_start (args, format);
	stmt = sqlite3_vmprintf (format, args);
	va_end (args);

	return stmt;
}

/**
 * e_cache_sqlite_stmt_free:
 * @stmt: a statement to free
 *
 * Frees a statement previously constructed with e_cache_sqlite_stmt_printf().
 *
 * Since: 3.26
 **/
void
e_cache_sqlite_stmt_free (gchar *stmt)
{
	if (stmt)
		sqlite3_free (stmt);
}

/**
 * e_cache_sqlite_maybe_vacuum:
 * @cache: an #ECache
 * @cancellable: optional #GCancellable object, or %NULL
 * @error: return location for a #GError, or %NULL
 *
 * Runs vacuum (compacts the database file), if needed.
 *
 * Returns: Whether succeeded. It doesn't mean that the vacuum had been run,
 *    only that no error happened during the call.
 *
 * Since: 3.26
 **/
gboolean
e_cache_sqlite_maybe_vacuum (ECache *cache,
			     GCancellable *cancellable,
			     GError **error)
{
	guint64 page_count = 0, page_size = 0, freelist_count = 0;
	gboolean success = FALSE;
	GError *local_error = NULL;

	g_return_val_if_fail (E_IS_CACHE (cache), FALSE);

	g_rec_mutex_lock (&cache->priv->lock);

	if (e_cache_sqlite_exec_internal (cache, "PRAGMA page_count;", e_cache_get_uint64_cb, &page_count, cancellable, &local_error) &&
	    e_cache_sqlite_exec_internal (cache, "PRAGMA page_size;", e_cache_get_uint64_cb, &page_size, cancellable, &local_error) &&
	    e_cache_sqlite_exec_internal (cache, "PRAGMA freelist_count;", e_cache_get_uint64_cb, &freelist_count, cancellable, &local_error)) {
		/* Vacuum, if there's more than 5% of the free pages, or when free pages use more than 10MB */
		success = !page_count || !freelist_count ||
			(freelist_count * page_size < 1024 * 1024 * 10 && freelist_count * 1000 / page_count <= 50) ||
			e_cache_sqlite_exec_internal (cache, "vacuum;", NULL, NULL, cancellable, &local_error);
	}

	g_rec_mutex_unlock (&cache->priv->lock);

	if (local_error) {
		g_propagate_error (error, local_error);
		success = FALSE;
	}

	return success;
}

static gboolean
e_cache_put_locked_default (ECache *cache,
			    const gchar *uid,
			    const gchar *revision,
			    const gchar *object,
			    ECacheColumnValues *other_columns,
			    EOfflineState offline_state,
			    gboolean is_replace,
			    GCancellable *cancellable,
			    GError **error)
{
	GString *statement, *other_names = NULL, *other_values = NULL;
	gboolean success;

	g_return_val_if_fail (E_IS_CACHE (cache), FALSE);
	g_return_val_if_fail (uid != NULL, FALSE);
	g_return_val_if_fail (object != NULL, FALSE);

	statement = g_string_sized_new (255);

	e_cache_sqlite_stmt_append_printf (statement, "INSERT OR REPLACE INTO %Q ("
		E_CACHE_COLUMN_UID ","
		E_CACHE_COLUMN_REVISION ","
		E_CACHE_COLUMN_OBJECT ","
		E_CACHE_COLUMN_STATE,
		E_CACHE_TABLE_OBJECTS);

	if (other_columns) {
		GHashTableIter iter;
		gpointer key, value;

		e_cache_column_values_init_iter (other_columns, &iter);
		while (g_hash_table_iter_next (&iter, &key, &value)) {
			if (!other_names)
				other_names = g_string_new ("");
			g_string_append_c (other_names, ',');

			e_cache_sqlite_stmt_append_printf (other_names, "%Q", key);

			if (!other_values)
				other_values = g_string_new ("");

			g_string_append_c (other_values, ',');
			if (value) {
				e_cache_sqlite_stmt_append_printf (other_values, "%Q", value);
			} else {
				g_string_append (other_values, "NULL");
			}
		}
	}

	if (other_names)
		g_string_append (statement, other_names->str);

	g_string_append (statement, ") VALUES (");

	e_cache_sqlite_stmt_append_printf (statement, "%Q,%Q,%Q,%d", uid, revision ? revision : "", object, offline_state);

	if (other_values)
		g_string_append (statement, other_values->str);

	g_string_append_c (statement, ')');

	success = e_cache_sqlite_exec_internal (cache, statement->str, NULL, NULL, cancellable, error);

	if (other_names)
		g_string_free (other_names, TRUE);
	if (other_values)
		g_string_free (other_values, TRUE);
	g_string_free (statement, TRUE);

	return success;
}

static gboolean
e_cache_remove_locked_default (ECache *cache,
			       const gchar *uid,
			       GCancellable *cancellable,
			       GError **error)
{
	gboolean success = TRUE;

	g_return_val_if_fail (E_IS_CACHE (cache), FALSE);
	g_return_val_if_fail (uid != NULL, FALSE);

	g_signal_emit (cache,
		       signals[BEFORE_REMOVE],
		       0,
		       uid, cancellable, error,
		       &success);

	success = success && e_cache_sqlite_exec_printf (cache,
		"DELETE FROM " E_CACHE_TABLE_OBJECTS " WHERE " E_CACHE_COLUMN_UID " = %Q",
		NULL, NULL, cancellable, error,
		uid);

	return success;
}

static gboolean
e_cache_remove_all_locked_default (ECache *cache,
				   const GSList *uids,
				   GCancellable *cancellable,
				   GError **error)
{
	const GSList *link;
	gboolean success = TRUE;

	g_return_val_if_fail (E_IS_CACHE (cache), FALSE);

	for (link = uids; link && success; link = g_slist_next (link)) {
		const gchar *uid = link->data;

		g_signal_emit (cache,
			       signals[BEFORE_REMOVE],
			       0,
			       uid, cancellable, error,
			       &success);
	}

	if (success) {
		success = e_cache_sqlite_exec_printf (cache,
			"DELETE FROM " E_CACHE_TABLE_OBJECTS,
			NULL, NULL, cancellable, error);
	}

	return success;
}

static gboolean
e_cache_clear_offline_changes_locked_default (ECache *cache,
					      GCancellable *cancellable,
					      GError **error)
{
	gboolean success;

	g_return_val_if_fail (E_IS_CACHE (cache), FALSE);

	success = e_cache_sqlite_exec_printf (cache,
		"DELETE FROM " E_CACHE_TABLE_OBJECTS " WHERE " E_CACHE_COLUMN_STATE "=%d",
		NULL, NULL, cancellable, error,
		E_OFFLINE_STATE_LOCALLY_DELETED);

	success = success && e_cache_sqlite_exec_printf (cache,
		"UPDATE " E_CACHE_TABLE_OBJECTS " SET " E_CACHE_COLUMN_STATE "=%d"
		" WHERE " E_CACHE_COLUMN_STATE "!=%d",
		NULL, NULL, cancellable, error,
		E_OFFLINE_STATE_SYNCED, E_OFFLINE_STATE_SYNCED);

	return success;
}

static gboolean
e_cache_signals_accumulator (GSignalInvocationHint *ihint,
			     GValue *return_accu,
			     const GValue *handler_return,
			     gpointer data)
{
	gboolean handler_result;

	handler_result = g_value_get_boolean (handler_return);
	g_value_set_boolean (return_accu, handler_result);

	return handler_result;
}

static gboolean
e_cache_before_put_default (ECache *cache,
			    const gchar *uid,
			    const gchar *revision,
			    const gchar *object,
			    ECacheColumnValues *other_columns,
			    gboolean is_replace,
			    GCancellable *cancellable,
			    GError **error)
{
	return TRUE;
}

static gboolean
e_cache_before_remove_default (ECache *cache,
			       const gchar *uid,
			       GCancellable *cancellable,
			       GError **error)
{
	return TRUE;
}

static void
e_cache_finalize (GObject *object)
{
	ECache *cache = E_CACHE (object);

	g_free (cache->priv->filename);
	cache->priv->filename = NULL;

	g_clear_pointer (&cache->priv->db, sqlite3_close);

	g_rec_mutex_clear (&cache->priv->lock);

	g_warn_if_fail (cache->priv->cancellable == NULL);
	g_clear_object (&cache->priv->cancellable);

	/* Chain up to parent's method. */
	G_OBJECT_CLASS (e_cache_parent_class)->finalize (object);
}

static void
e_cache_class_init (ECacheClass *klass)
{
	GObjectClass *object_class;

	object_class = G_OBJECT_CLASS (klass);
	object_class->finalize = e_cache_finalize;

	klass->put_locked = e_cache_put_locked_default;
	klass->remove_locked = e_cache_remove_locked_default;
	klass->remove_all_locked = e_cache_remove_all_locked_default;
	klass->clear_offline_changes_locked = e_cache_clear_offline_changes_locked_default;
	klass->before_put = e_cache_before_put_default;
	klass->before_remove = e_cache_before_remove_default;

	signals[BEFORE_PUT] = g_signal_new (
		"before-put",
		G_OBJECT_CLASS_TYPE (klass),
		G_SIGNAL_RUN_LAST,
		G_STRUCT_OFFSET (ECacheClass, before_put),
		e_cache_signals_accumulator,
		NULL,
		g_cclosure_marshal_generic,
		G_TYPE_BOOLEAN, 7,
		G_TYPE_STRING,
		G_TYPE_STRING,
		G_TYPE_STRING,
		E_TYPE_CACHE_COLUMN_VALUES,
		G_TYPE_BOOLEAN,
		G_TYPE_CANCELLABLE,
		G_TYPE_POINTER);

	signals[BEFORE_REMOVE] = g_signal_new (
		"before-remove",
		G_OBJECT_CLASS_TYPE (klass),
		G_SIGNAL_RUN_LAST,
		G_STRUCT_OFFSET (ECacheClass, before_remove),
		e_cache_signals_accumulator,
		NULL,
		g_cclosure_marshal_generic,
		G_TYPE_BOOLEAN, 3,
		G_TYPE_STRING,
		G_TYPE_CANCELLABLE,
		G_TYPE_POINTER);

	signals[REVISION_CHANGED] = g_signal_new (
		"revision-changed",
		G_OBJECT_CLASS_TYPE (klass),
		G_SIGNAL_RUN_LAST,
		G_STRUCT_OFFSET (ECacheClass, revision_changed),
		NULL,
		NULL,
		g_cclosure_marshal_generic,
		G_TYPE_NONE, 0,
		G_TYPE_NONE);

	e_sqlite3_vfs_init ();
}

static void
e_cache_init (ECache *cache)
{
	cache->priv = e_cache_get_instance_private (cache);

	cache->priv->filename = NULL;
	cache->priv->db = NULL;
	cache->priv->cancellable = NULL;
	cache->priv->in_transaction = 0;
	cache->priv->revision_change_frozen = 0;
	cache->priv->revision_counter = 0;
	cache->priv->last_revision_time = 0;
	cache->priv->needs_revision_change = FALSE;

	g_rec_mutex_init (&cache->priv->lock);
}