Line data Source code
1 : import 'dart:async';
2 :
3 : import 'package:at_client/src/client/remote_secondary.dart';
4 : import 'package:at_client/src/response/json_utils.dart';
5 : import 'package:at_commons/at_builders.dart';
6 : import 'package:at_commons/at_commons.dart';
7 : import 'package:at_persistence_secondary_server/at_persistence_secondary_server.dart';
8 : import 'package:at_utils/at_logger.dart';
9 :
10 : class SyncUtil {
11 3 : static var logger = AtSignLogger('SyncUtil');
12 :
13 0 : static Future<CommitEntry?> getCommitEntry(
14 : int sequenceNumber, String atSign) async {
15 : var commitLogInstance =
16 0 : await (AtCommitLogManagerImpl.getInstance().getCommitLog(atSign));
17 0 : var commitEntry = await commitLogInstance?.getEntry(sequenceNumber);
18 : return commitEntry;
19 : }
20 :
21 0 : static Future<void> updateCommitEntry(
22 : var commitEntry, int commitId, String atSign) async {
23 : var commitLogInstance =
24 0 : await (AtCommitLogManagerImpl.getInstance().getCommitLog(atSign));
25 0 : await commitLogInstance?.update(commitEntry, commitId);
26 : }
27 :
28 0 : static Future<CommitEntry?> getLastSyncedEntry(String? regex,
29 : {required String atSign}) async {
30 : var commitLogInstance =
31 0 : await AtCommitLogManagerImpl.getInstance().getCommitLog(atSign);
32 :
33 : CommitEntry? lastEntry;
34 : if (regex != null) {
35 0 : lastEntry = await commitLogInstance!.lastSyncedEntryWithRegex(regex);
36 : } else {
37 0 : lastEntry = await commitLogInstance!.lastSyncedEntry();
38 : }
39 : return lastEntry;
40 : }
41 :
42 0 : static Future<CommitEntry?> getEntry(int? seqNumber, String atSign) async {
43 0 : var commitLogInstance = await (AtCommitLogManagerImpl.getInstance()
44 0 : .getCommitLog(atSign) as FutureOr<AtCommitLog>);
45 0 : var entry = await commitLogInstance.getEntry(seqNumber);
46 : return entry;
47 : }
48 :
49 0 : static Future<List<CommitEntry>> getChangesSinceLastCommit(
50 : int? seqNum, String? regex,
51 : {required String atSign}) async {
52 : var commitLogInstance =
53 0 : await (AtCommitLogManagerImpl.getInstance().getCommitLog(atSign));
54 : if (commitLogInstance == null) {
55 0 : return [];
56 : }
57 0 : return commitLogInstance.getChanges(seqNum, regex);
58 : }
59 :
60 : //#TODO change return type to enum which says in sync, local ahead or server ahead
61 1 : static bool isInSync(List<CommitEntry?>? unCommittedEntries,
62 : int? serverCommitId, int? lastSyncedCommitId) {
63 3 : logger.finer('localCommitId:$lastSyncedCommitId');
64 3 : logger.finer('serverCommitId:$serverCommitId');
65 4 : logger.finer('changed entries: ${unCommittedEntries?.length}');
66 1 : return (unCommittedEntries == null || unCommittedEntries.isEmpty) &&
67 1 : _checkCommitIdsEqual(lastSyncedCommitId, serverCommitId);
68 : }
69 :
70 1 : static bool _checkCommitIdsEqual(lastSyncedCommitId, serverCommitId) {
71 : return (lastSyncedCommitId != null &&
72 : serverCommitId != null &&
73 1 : lastSyncedCommitId == serverCommitId) ||
74 : (lastSyncedCommitId == null && serverCommitId == null);
75 : }
76 :
77 0 : static Future<int?> getLatestServerCommitId(
78 : RemoteSecondary remoteSecondary, String? regex) async {
79 : int? commitId;
80 0 : var builder = StatsVerbBuilder()..statIds = '3';
81 0 : if (regex != null && regex != 'null' && regex.isNotEmpty) {
82 0 : builder.regex = regex;
83 : }
84 0 : var result = await remoteSecondary.executeVerb(builder);
85 0 : result = result.replaceAll('data: ', '');
86 0 : var statsJson = JsonUtils.decodeJson(result);
87 0 : if (statsJson[0]['value'] != 'null') {
88 0 : commitId = int.parse(statsJson[0]['value']);
89 : }
90 : return commitId;
91 : }
92 :
93 1 : static bool shouldSkipSync(String key) {
94 1 : if (key.startsWith(AT_PKAM_PRIVATE_KEY) ||
95 1 : key.startsWith(AT_PKAM_PUBLIC_KEY) ||
96 1 : key.startsWith(AT_ENCRYPTION_PRIVATE_KEY)) {
97 : return true;
98 : }
99 : return false;
100 : }
101 : }
|