Skip to content

Commit

Permalink
Refactor package layout
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeDucret committed Jan 6, 2021
1 parent 8b45cfa commit 5299cf7
Show file tree
Hide file tree
Showing 20 changed files with 109 additions and 93 deletions.
6 changes: 4 additions & 2 deletions lib/hive_mirror.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import 'src/hive_mirror.dart';
import 'src/hive_mirror_impl.dart';

export 'src/backend/git/git_patch.dart';
export 'src/handlers/box_mirror_handler.dart';
export 'src/datasource/git_patch.dart';
export 'src/datasource/file.dart';
export 'src/extension.dart';
export 'src/handlers/box.dart';
export 'src/hive_mirror.dart';

final HiveMirrorInterface HiveMirror = HiveMirrorImpl();
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import 'dart:async';

import 'git_patch.dart';
import '../datasource/git_patch.dart';

class Commit {
final String revision;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,24 @@ abstract class FileDescriptorInterface {
class FileDescriptor implements FileDescriptorInterface {
final File _file;
final Uri _uri;
final Decode _decode;
final DecodeKey _decodeKey;
final FileDecode _decode;
final FileDecodeKey _decodeKey;
String _eTag;

FileDescriptor.file(File file, {Decode decode, DecodeKey decodeKey})
: _file = file,
FileDescriptor.file(
File file, {
FileDecode decode,
FileDecodeKey decodeKey,
}) : _file = file,
_uri = null,
_decode = decode,
_decodeKey = decodeKey;

FileDescriptor.uri(Uri uri, {Decode decode, DecodeKey decodeKey})
: _file = null,
FileDescriptor.uri(
Uri uri, {
FileDecode decode,
FileDecodeKey decodeKey,
}) : _file = null,
_uri = uri,
_decode = decode,
_decodeKey = decodeKey;
Expand Down Expand Up @@ -63,5 +69,5 @@ class FileDescriptor implements FileDescriptorInterface {
dynamic decodeKey(String line) => _decodeKey(line);
}

typedef dynamic Decode(String line);
typedef dynamic DecodeKey(String line);
typedef dynamic FileDecode(String line);
typedef dynamic FileDecodeKey(String line);
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,43 @@ class GitPatch implements GitPatchInterface {
final String _uriTemplate;
final String _initialBaseRevision;
final Filter _filter;
final Decode _decode;
final DecodeKey _decodeKey;
final PatchDecode _decode;
final PatchDecodeKey _decodeKey;

GitPatch.file(File file, {Filter filter, Decode decode, DecodeKey decodeKey})
: _file = file,
GitPatch.file(
File file, {
Filter filter,
PatchDecode decode,
PatchDecodeKey decodeKey,
}) : _file = file,
_uriTemplate = null,
_initialBaseRevision = null,
_filter = filter,
_decode = decode,
_decodeKey = decodeKey;

GitPatch.uri(String uriTemplate,
{String initialBaseRevision = 'initial',
Filter filter,
Decode decode,
DecodeKey decodeKey})
: _file = null,
GitPatch.uri(
String uriTemplate, {
String initialBaseRevision = 'initial',
Filter filter,
PatchDecode decode,
PatchDecodeKey decodeKey,
}) : _file = null,
_uriTemplate = uriTemplate,
_initialBaseRevision = initialBaseRevision,
_filter = filter,
_decode = decode,
_decodeKey = decodeKey;

factory GitPatch.githubCompareView(String userName, String repoName,
{String initialBaseRevision = 'initial',
String compareRevision = 'master',
Filter filter,
Decode decode,
DecodeKey decodeKey}) {
factory GitPatch.githubCompareView(
String userName,
String repoName, {
String initialBaseRevision = 'initial',
String compareRevision = 'master',
Filter filter,
PatchDecode decode,
PatchDecodeKey decodeKey,
}) {
final _uriTemplate =
'https://github.com/$userName/$repoName/compare/\$baseRevision..$compareRevision.patch';
return GitPatch.uri(_uriTemplate,
Expand Down Expand Up @@ -87,5 +95,5 @@ class GitPatch implements GitPatchInterface {
}

typedef bool Filter(String filePath);
typedef dynamic Decode(String filePath, String line);
typedef dynamic DecodeKey(String filePath, String line);
typedef dynamic PatchDecode(String filePath, String line);
typedef dynamic PatchDecodeKey(String filePath, String line);
4 changes: 2 additions & 2 deletions lib/extension.dart → lib/src/extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import 'dart:typed_data';

import 'package:hive/hive.dart';
import 'package:hive_mirror/src/handlers/box_mirror_handler.dart';

import 'hive_mirror.dart';
import '../hive_mirror.dart';
import 'handlers/box.dart';

extension Mirror on HiveInterface {
Future<Box<E>> openMirrorBox<E>(
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion lib/src/hive_mirror_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'dart:async';
import 'package:hive/hive.dart';
import 'package:isolate/isolate.dart';

import 'handlers/dynamic_mirror_handler.dart';
import 'handlers/dynamic.dart';
import 'hive_mirror.dart';
import 'remote.dart';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

import 'dart:convert';

import '../../handlers/dynamic_mirror_handler.dart';
import '../../handlers/handler_holder.dart';
import '../../hive_mirror.dart';
import '../../metadata.dart';
import '../mirror_manager.dart';
import 'file_descriptor.dart';
import '../datasource/file.dart';
import '../handlers/dynamic.dart';
import '../handlers/handler_holder.dart';
import '../hive_mirror.dart';
import '../metadata.dart';
import 'mirror_manager.dart';

class FileMirrorManager implements MirrorManager {
final MirrorHandlerHolder _handler;
Expand All @@ -28,7 +28,7 @@ class FileMirrorManager implements MirrorManager {
loadFile(fileDescriptor as FileDescriptorInterface);

Future<void> loadFile(FileDescriptorInterface fileDescriptor) async {
final etag = _metadata.get(metaEtag);
final etag = _metadata.get(etagMeta);
final fileData = fileDescriptor.open(etag);

if (fileDescriptor.etag != etag) {
Expand All @@ -43,8 +43,8 @@ class FileMirrorManager implements MirrorManager {
}
}

Future<void> _applyLines(String etag, Iterable<String> lines, Decode decode,
DecodeKey decodeKey) async {
Future<void> _applyLines(String etag, Iterable<String> lines,
FileDecode decode, FileDecodeKey decodeKey) async {
MapEntry<dynamic, dynamic> decodeLine(String line) {
final dynamic key = decodeKey(line);
if (key != null) {
Expand All @@ -63,8 +63,8 @@ class FileMirrorManager implements MirrorManager {
await (await _handler.use()).putAll(putEntries);
}

await _metadata.put(metaEtag, etag);
await _metadata.put(etagMeta, etag);
}

static const metaEtag = 'etag';
static const etagMeta = 'file_etag';
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

import 'dart:convert';

import '../../handlers/dynamic_mirror_handler.dart';
import '../../handlers/handler_holder.dart';
import '../../hive_mirror.dart';
import '../../metadata.dart';
import '../mirror_manager.dart';
import 'git_patch.dart';
import 'git_patch_parser.dart';
import '../convert/git_patch_parser.dart';
import '../datasource/git_patch.dart';
import '../handlers/dynamic.dart';
import '../handlers/handler_holder.dart';
import '../hive_mirror.dart';
import '../metadata.dart';
import 'mirror_manager.dart';

class GitMirrorManager implements MirrorManager {
final MirrorHandlerHolder _handler;
Expand All @@ -28,7 +28,7 @@ class GitMirrorManager implements MirrorManager {
Future<void> mirror(dynamic patch) => applyPatch(patch as GitPatchInterface);

Future<void> applyPatch(GitPatchInterface patch) async {
final head = _metadata.get(metaHead);
final head = _metadata.get(headMeta);
final commits = patch
.format(head)
.transform(Utf8Decoder())
Expand All @@ -45,14 +45,15 @@ class GitMirrorManager implements MirrorManager {
}

Future<void> _applyCommit(
Commit commit, Decode decode, DecodeKey decodeKey) async {
Commit commit, PatchDecode decode, PatchDecodeKey decodeKey) async {
for (Diff diff in commit.diffs) {
await _applyDiff(diff, decode, decodeKey);
}
await _metadata.put(metaHead, commit.revision);
await _metadata.put(headMeta, commit.revision);
}

Future<void> _applyDiff(Diff diff, Decode decode, DecodeKey decodeKey) async {
Future<void> _applyDiff(
Diff diff, PatchDecode decode, PatchDecodeKey decodeKey) async {
MapEntry<dynamic, dynamic> decodeAddLine(String line) {
final dynamic key = decodeKey(diff.path, line);

Expand Down Expand Up @@ -81,5 +82,5 @@ class GitMirrorManager implements MirrorManager {
}
}

static const metaHead = 'head';
static const headMeta = 'git_head';
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

import '../datasource/file.dart';
import '../datasource/git_patch.dart';
import '../hive_mirror.dart';
import '../metadata.dart';
import 'file/file_descriptor.dart';
import 'file/file_mirror_manager.dart';
import 'git/git_mirror_manager.dart';
import 'git/git_patch.dart';
import 'file.dart';
import 'git_patch.dart';

abstract class MirrorManager {
Future<void> mirror(dynamic source);
Expand Down
4 changes: 2 additions & 2 deletions lib/src/metadata.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ class Metadata {
String _dbKey(String key) => '$_handlerId:$key';

static Future<Metadata> open(MirrorHandler handler) async {
final db = await Hive.openBox<String>('.hive_mirror_metadata');
final db = await Hive.openBox<String>('.hive_mirror');
return Metadata._(handler.id, db);
}

static Future<void> close() async {
final db = Hive.box<String>('.hive_mirror_metadata');
final db = Hive.box<String>('.hive_mirror');
await db?.close();
}
}
2 changes: 1 addition & 1 deletion lib/src/remote.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import 'package:hive/hive.dart';

import 'backend/mirror_manager.dart';
import 'managers/mirror_manager.dart';
import 'hive_mirror.dart';
import 'metadata.dart';

Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name: hive_mirror
description: A new Dart package project.
description: One way sync for hive databases.
version: 0.0.1
author: Guillaume Ducret
homepage:
homepage: https://github.com/GuillaumeDucret/hive_mirror

environment:
sdk: ">=2.8.0 <3.0.0"
Expand Down
6 changes: 3 additions & 3 deletions test/file_descriptors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import 'dart:io';

import 'package:hive_mirror/src/backend/file/file_descriptor.dart';
import 'package:hive_mirror/src/datasource/file.dart';

import 'type.dart';

Expand All @@ -13,8 +13,8 @@ abstract class TestFileDescriptorBase implements FileDescriptorInterface {
final etag;

final String _filePath;
final Decode _decode;
final DecodeKey _decodeKey;
final FileDecode _decode;
final FileDecodeKey _decodeKey;

TestFileDescriptorBase._(
this.etag, this._filePath, this._decode, this._decodeKey);
Expand Down
1 change: 0 additions & 1 deletion test/integration/extension_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import 'package:hive/hive.dart';
import 'package:hive_mirror/hive_mirror.dart';
import 'package:hive_mirror/extension.dart';
import 'package:test/test.dart';

import '../patches.dart';
Expand Down
6 changes: 3 additions & 3 deletions test/patches.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

import 'dart:io';

import 'package:hive_mirror/src/backend/git/git_patch.dart';
import 'package:hive_mirror/src/datasource/git_patch.dart';

import 'type.dart';

abstract class TestPatchBase implements GitPatchInterface {
final String _filePath;
final Filter _filter;
final Decode _decode;
final DecodeKey _decodeKey;
final PatchDecode _decode;
final PatchDecodeKey _decodeKey;

TestPatchBase._(this._filePath, this._decode, this._decodeKey,
[this._filter]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import 'dart:convert';
import 'dart:io';

import 'package:hive_mirror/src/backend/git/git_patch_parser.dart';
import 'package:hive_mirror/src/convert/git_patch_parser.dart';
import 'package:test/test.dart';

import '../../../patches.dart';
import '../../patches.dart';

void main() {
group('GitPatchParser', () {
Expand Down
Loading

0 comments on commit 5299cf7

Please sign in to comment.