Skip to content

Commit

Permalink
It seems just work but don't know why
Browse files Browse the repository at this point in the history
  • Loading branch information
TaYaKi71751 committed Dec 5, 2023
1 parent e5072c7 commit a653e98
Showing 1 changed file with 76 additions and 72 deletions.
148 changes: 76 additions & 72 deletions lib/version/sync.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ class SyncInfoRecord {
}

class SyncManager {
static const String syncInfoURL =
'https://raw.githubusercontent.com/violet-dev/sync-data/master/syncversion.txt';
static String syncInfoURL(String branch){
return 'https://raw.githubusercontent.com/violet-dev/sync-data/'+(branch)+'/syncversion.txt';
}

static bool firstSync = false;
static bool syncRequire = false; // database sync require
Expand All @@ -52,78 +53,81 @@ class SyncManager {
static int requestSize = 0;

static Future<void> checkSync() async {
try {
var ls = const LineSplitter();
var infoRaw = (await http.get(syncInfoURL)).body;
var lines = ls.convert(infoRaw);

final prefs = await SharedPreferences.getInstance();
var latest = prefs.getInt('synclatest');
var lastDB = prefs.getString('databasesync');

if (latest == null) {
syncRequire = firstSync = true;
latest = 0;
} else if (lastDB != null &&
DateTime.now().difference(DateTime.parse(lastDB)).inDays > 7) {
syncRequire = true;
}
for(int i = 0;i < 100;i++){
try {
var ls = const LineSplitter();
var infoRaw = (await http.get(syncInfoURL('master~'+i.toString()))).body;
var lines = ls.convert(infoRaw);

// lines: [old ... latest]
// _rows: [latest ... old]
_rows = [];

/*
syncversion은
...
chunk 1640992742 https://github.com/violet-dev/chunk/releases/download/1640992742/data-637765895426855402.json 31519
db 1640997991 https://github.com/violet-dev/db/releases/download/2022.01.01/rawdata
chunk 1640998430 https://github.com/violet-dev/chunk/releases/download/1640998430/data-637765952304854006.json 47546
chunk 1640998430 https://github.com/violet-dev/chunk/releases/download/1640998430/data-637765952304854006.db 45056
chunk 1641003001 https://github.com/violet-dev/chunk/releases/download/1641003001/data-637765998015030026.json 30911
chunk 1641003001 https://github.com/violet-dev/chunk/releases/download/1641003001/data-637765998015030026.db 32768
...
와 같은 형식으로 아래쪽이 항상 최신 청크다. 따라서 reversed 탐색을 시도한다.
*/
for (var element in lines.reversed) {
if (element.startsWith('#')) continue;

var split = element.split(' ');
var type = split[0];
var timestamp = int.parse(split[1]);
var url = split[2];
var size = 0;
if (type == 'chunk') size = int.parse(split[3]);

// We require only json files when synchronize with chunk.
if (type == 'chunk' && !url.endsWith('.json')) continue;

//
// 마지막으로 동기화한 시간보다 작은 경우 해당 청크는 무시한다.
//
if (type == 'chunk' && timestamp <= latest) continue;

requestSize += size;
_rows!.add(SyncInfoRecord(
type: type,
timestamp: timestamp,
url: url,
size: size,
));
}
final prefs = await SharedPreferences.getInstance();
var latest = prefs.getInt('synclatest');
var lastDB = prefs.getString('databasesync');

if (latest == null) {
syncRequire = firstSync = true;
latest = 0;
} else if (lastDB != null &&
DateTime.now().difference(DateTime.parse(lastDB)).inDays > 7) {
syncRequire = true;
}

/*
너무 많은 청크를 다운로드해야하는 경우 동기화를 추천한다.
그 이유는 다운로드해야하는 파일이 너무 많아지기 때문이며,
또한 데이터베이스의 무결성이 훼손될 가능성이 있기 때문이다.
*/
if (requestSize > ignoreUserAcceptThreshold) syncRequire = true;
if (_rows!.any((element) => element.type == 'chunk')) chunkRequire = true;
} catch (e, st) {
Logger.error('[Sync-check] E: $e\n'
'$st');
// lines: [old ... latest]
// _rows: [latest ... old]
_rows = [];

/*
syncversion은
...
chunk 1640992742 https://github.com/violet-dev/chunk/releases/download/1640992742/data-637765895426855402.json 31519
db 1640997991 https://github.com/violet-dev/db/releases/download/2022.01.01/rawdata
chunk 1640998430 https://github.com/violet-dev/chunk/releases/download/1640998430/data-637765952304854006.json 47546
chunk 1640998430 https://github.com/violet-dev/chunk/releases/download/1640998430/data-637765952304854006.db 45056
chunk 1641003001 https://github.com/violet-dev/chunk/releases/download/1641003001/data-637765998015030026.json 30911
chunk 1641003001 https://github.com/violet-dev/chunk/releases/download/1641003001/data-637765998015030026.db 32768
...
와 같은 형식으로 아래쪽이 항상 최신 청크다. 따라서 reversed 탐색을 시도한다.
*/
for (var element in lines.reversed) {
if (element.startsWith('#')) continue;

var split = element.split(' ');
var type = split[0];
var timestamp = int.parse(split[1]);
var url = split[2];
var size = 0;
if (type == 'chunk') size = int.parse(split[3]);

// We require only json files when synchronize with chunk.
if (type == 'chunk' && !url.endsWith('.json')) continue;

//
// 마지막으로 동기화한 시간보다 작은 경우 해당 청크는 무시한다.
//
if (type == 'chunk' && timestamp <= latest) continue;

requestSize += size;
_rows!.add(SyncInfoRecord(
type: type,
timestamp: timestamp,
url: url,
size: size,
));
}

/*
너무 많은 청크를 다운로드해야하는 경우 동기화를 추천한다.
그 이유는 다운로드해야하는 파일이 너무 많아지기 때문이며,
또한 데이터베이스의 무결성이 훼손될 가능성이 있기 때문이다.
*/
if (requestSize > ignoreUserAcceptThreshold) syncRequire = true;
if (_rows!.any((element) => element.type == 'chunk')) chunkRequire = true;
} catch (e, st) {
continue;
Logger.error('[Sync-check] E: $e\n'
'$st');
}
}
}

Expand Down

0 comments on commit a653e98

Please sign in to comment.