From 296e25e7bf2b4639b63c4f0a6e19a9fdc4cb6026 Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Mon, 16 Oct 2023 17:05:37 +0200 Subject: [PATCH] introduce base box to avoid duplication --- dart/lib/src/sentry_trace_origins.dart | 3 +- hive/example/example.dart | 49 +++++++ hive/example/hive_example.dart | 6 - hive/lib/src/sentry_box.dart | 172 +----------------------- hive/lib/src/sentry_box_base.dart | 174 +++++++++++++++++++++++++ hive/lib/src/sentry_lazy_box.dart | 25 ++++ hive/test/sentry_box_test.dart | 9 +- 7 files changed, 260 insertions(+), 178 deletions(-) create mode 100644 hive/example/example.dart delete mode 100644 hive/example/hive_example.dart create mode 100644 hive/lib/src/sentry_box_base.dart create mode 100644 hive/lib/src/sentry_lazy_box.dart diff --git a/dart/lib/src/sentry_trace_origins.dart b/dart/lib/src/sentry_trace_origins.dart index 334f34cf62..78ab94aaba 100644 --- a/dart/lib/src/sentry_trace_origins.dart +++ b/dart/lib/src/sentry_trace_origins.dart @@ -20,5 +20,6 @@ class SentryTraceOrigins { 'auto.db.sqflite.database_factory'; static const autoDbHive = 'auto.db.hive'; - static const autoDbHiveBox = 'auto.db.hive.box'; + static const autoDbHiveOpenDatabase = 'auto.db.hive.open_database'; + static const autoDbHiveBaseBox = 'auto.db.hive.base_box'; } diff --git a/hive/example/example.dart b/hive/example/example.dart new file mode 100644 index 0000000000..ef5e6bac2a --- /dev/null +++ b/hive/example/example.dart @@ -0,0 +1,49 @@ +import 'package:hive/hive.dart'; +import 'package:sentry/sentry.dart'; +import 'package:sentry_hive/sentry_hive.dart'; + +part 'main.g.dart'; + +Future main() async { + // ATTENTION: Change the DSN below with your own to see the events in Sentry. Get one at sentry.io + const dsn = + 'https://e85b375ffb9f43cf8bdf9787768149e0@o447951.ingest.sentry.io/5428562'; + + await Sentry.init( + (options) { + options.dsn = dsn; + options.tracesSampleRate = 1.0; + options.debug = true; + }, + appRunner: runApp, // Init your App. + ); +} + +Future runApp() async { + Hive + ..init(Directory.current.path) + ..registerAdapter(PersonAdapter()); + + var box = await Hive.openBox('testBox'); + + var sentryBox = SentryBox + + var person = Person(name: 'Dave', age: 23); + +} + +@HiveType(typeId: 1) +class Person { + Person({required this.name, required this.age}); + + @HiveField(0) + String name; + + @HiveField(1) + int age; + + @override + String toString() { + return '$name: $age'; + } +} diff --git a/hive/example/hive_example.dart b/hive/example/hive_example.dart deleted file mode 100644 index 0c80ff23c3..0000000000 --- a/hive/example/hive_example.dart +++ /dev/null @@ -1,6 +0,0 @@ -import 'package:hive/sentry_hive.dart'; - -void main() { - var awesome = Awesome(); - print('awesome: ${awesome.isAwesome}'); -} diff --git a/hive/lib/src/sentry_box.dart b/hive/lib/src/sentry_box.dart index a35fa75ffa..5189a30920 100644 --- a/hive/lib/src/sentry_box.dart +++ b/hive/lib/src/sentry_box.dart @@ -2,93 +2,16 @@ import 'package:meta/meta.dart'; import 'package:hive/hive.dart'; import 'package:sentry/sentry.dart'; -import 'sentry_hive_impl.dart'; +import 'sentry_box_base.dart'; /// @experimental -class SentryBox implements Box { +class SentryBox extends SentryBoxBase implements Box { final Box _box; - final Hub _hub; - // ignore: public_member_api_docs - SentryBox(this._box, @internal Hub? hub) : _hub = hub ?? HubAdapter(); - - @override - Future add(E value) async { - return _asyncWrapInSpan('add', () async { - return await _box.add(value); - }); - } - - @override - Future> addAll(Iterable values) async { - return _asyncWrapInSpan('addAll', () async { - return await _box.addAll(values); - }); - } - - @override - Future clear() async { - return _asyncWrapInSpan('clear', () async { - return await _box.clear(); - }); - } - - @override - Future close() async { - return _asyncWrapInSpan('close', () async { - return await _box.close(); - }); - } - - @override - Future compact() async { - return _asyncWrapInSpan('compact', () async { - return await _box.compact(); - }); - } - - @override - bool containsKey(key) { - return _box.containsKey(key); - } - - @override - Future delete(key) async { - return _asyncWrapInSpan('delete', () async { - return await _box.delete(key); - }); - } - - @override - // ignore: strict_raw_type - Future deleteAll(Iterable keys) async { - return _asyncWrapInSpan('deleteAll', () async { - return await _box.deleteAll(keys); - }); - } - - @override - Future deleteAt(int index) async { - return _asyncWrapInSpan('deleteAt', () async { - return await _box.deleteAt(index); - }); - } - - @override - Future deleteFromDisk() async { - return _asyncWrapInSpan('deleteFromDisk', () async { - return await _box.deleteFromDisk(); - }); - } - - @override - Future flush() async { - return _asyncWrapInSpan('flush', () async { - return await _box.flush(); - }); - } + /// + SentryBox(this._box, @internal Hub hub) : super(_box, hub); @override E? get(key, {E? defaultValue}) { @@ -100,102 +23,17 @@ class SentryBox implements Box { return _box.getAt(index); } - @override - bool get isEmpty => _box.isEmpty; - - @override - bool get isNotEmpty => _box.isNotEmpty; - - @override - bool get isOpen => _box.isOpen; - - @override - dynamic keyAt(int index) { - return _box.keyAt(index); - } - - @override - // ignore: strict_raw_type - Iterable get keys => _box.keys; - - @override - bool get lazy => _box.lazy; - - @override - int get length => _box.length; - - @override - String get name => _box.name; - - @override - String? get path => _box.path; - - @override - Future put(key, E value) async { - return _asyncWrapInSpan('put', () async { - return await _box.put(key, value); - }); - } - - @override - Future putAll(Map entries) async { - return _asyncWrapInSpan('putAll', () async { - return await _box.putAll(entries); - }); - } - - @override - Future putAt(int index, E value) async { - return _asyncWrapInSpan('putAt', () async { - return await _box.putAt(index, value); - }); - } - @override Map toMap() { return _box.toMap(); } @override + // TODO: implement values Iterable get values => _box.values; @override Iterable valuesBetween({startKey, endKey}) { return _box.valuesBetween(startKey: startKey, endKey: endKey); } - - @override - Stream watch({key}) { - return _box.watch(key: key); - } - - // Helper - - Future _asyncWrapInSpan(String description, Future Function() execute) async { - final currentSpan = _hub.getSpan(); - final span = currentSpan?.startChild( - SentryHiveImpl.dbOp, - description: description, - ); - - // ignore: invalid_use_of_internal_member - span?.origin = SentryTraceOrigins.autoDbHiveBox; - - span?.setData(SentryHiveImpl.dbSystemKey, SentryHiveImpl.dbSystem); - span?.setData(SentryHiveImpl.dbNameKey, name); - - try { - final result = await execute(); - span?.status = SpanStatus.ok(); - - return result; - } catch (exception) { - span?.throwable = exception; - span?.status = SpanStatus.internalError(); - - rethrow; - } finally { - await span?.finish(); - } - } } diff --git a/hive/lib/src/sentry_box_base.dart b/hive/lib/src/sentry_box_base.dart new file mode 100644 index 0000000000..6910a8d928 --- /dev/null +++ b/hive/lib/src/sentry_box_base.dart @@ -0,0 +1,174 @@ + +import 'package:hive/hive.dart'; +import 'package:sentry/sentry.dart'; +import 'sentry_hive_impl.dart'; + +/// +class SentryBoxBase implements BoxBase { + + final BoxBase _boxBase; + final Hub _hub; + + /// + SentryBoxBase(this._boxBase, this._hub); + + @override + Future add(E value) async { + return _asyncWrapInSpan('add', () async { + return await _boxBase.add(value); + }); + } + + @override + Future> addAll(Iterable values) { + return _asyncWrapInSpan('addAll', () async { + return await _boxBase.addAll(values); + }); + } + + @override + Future clear() { + return _asyncWrapInSpan('clear', () async { + return await _boxBase.clear(); + }); + } + + @override + Future close() { + return _asyncWrapInSpan('close', () async { + return await _boxBase.close(); + }); + } + + @override + Future compact() { + return _asyncWrapInSpan('compact', () async { + return await _boxBase.compact(); + }); + } + + @override + bool containsKey(key) { + return _boxBase.containsKey(key); + } + + @override + Future delete(key) { + return _asyncWrapInSpan('delete', () async { + return await _boxBase.delete(key); + }); + } + + @override + Future deleteAll(Iterable keys) { + return _asyncWrapInSpan('deleteAll', () async { + return await _boxBase.deleteAll(keys); + }); + } + + @override + Future deleteAt(int index) { + return _asyncWrapInSpan('deleteAt', () async { + return await _boxBase.deleteAt(index); + }); + } + + @override + Future deleteFromDisk() { + return _asyncWrapInSpan('deleteFromDisk', () async { + return await _boxBase.deleteFromDisk(); + }); + } + + @override + Future flush() { + return _asyncWrapInSpan('flush', () async { + return await _boxBase.flush(); + }); + } + + @override + bool get isEmpty => _boxBase.isEmpty; + + @override + bool get isNotEmpty => _boxBase.isNotEmpty; + + @override + bool get isOpen => _boxBase.isOpen; + + @override + keyAt(int index) { + return _boxBase.keyAt(index); + } + + @override + Iterable get keys => _boxBase.keys; + + @override + bool get lazy => _boxBase.lazy; + + @override + int get length => _boxBase.length; + + @override + String get name => _boxBase.name; + + @override + String? get path => _boxBase.path; + + @override + Future put(key, value) { + return _asyncWrapInSpan('put', () async { + return await _boxBase.put(key, value); + }); + } + + @override + Future putAll(Map entries) { + return _asyncWrapInSpan('putAll', () async { + return await _boxBase.putAll(entries); + }); + } + + @override + Future putAt(int index, value) { + return _asyncWrapInSpan('putAt', () async { + return await _boxBase.putAt(index, value); + }); + } + + @override + Stream watch({key}) { + return _boxBase.watch(key: key); + } + + // Helper + + Future _asyncWrapInSpan(String description, Future Function() execute) async { + final currentSpan = _hub.getSpan(); + final span = currentSpan?.startChild( + SentryHiveImpl.dbOp, + description: description, + ); + + // ignore: invalid_use_of_internal_member + span?.origin = SentryTraceOrigins.autoDbHiveBaseBox; + + span?.setData(SentryHiveImpl.dbSystemKey, SentryHiveImpl.dbSystem); + span?.setData(SentryHiveImpl.dbNameKey, name); + + try { + final result = await execute(); + span?.status = SpanStatus.ok(); + + return result; + } catch (exception) { + span?.throwable = exception; + span?.status = SpanStatus.internalError(); + + rethrow; + } finally { + await span?.finish(); + } + } +} \ No newline at end of file diff --git a/hive/lib/src/sentry_lazy_box.dart b/hive/lib/src/sentry_lazy_box.dart new file mode 100644 index 0000000000..9cc14902d8 --- /dev/null +++ b/hive/lib/src/sentry_lazy_box.dart @@ -0,0 +1,25 @@ + +import 'package:hive/hive.dart'; +import 'package:meta/meta.dart'; +import 'package:sentry/sentry.dart'; + +import 'sentry_box_base.dart'; + +/// +class SentryLazyBox extends SentryBoxBase implements LazyBox { + + final LazyBox _lazyBox; + + /// + SentryLazyBox(this._lazyBox, @internal Hub hub) : super(_lazyBox, hub); + + @override + Future get(key, {E? defaultValue}) { + return _lazyBox.get(key, defaultValue: defaultValue); + } + + @override + Future getAt(int index) { + return _lazyBox.getAt(index); + } +} diff --git a/hive/test/sentry_box_test.dart b/hive/test/sentry_box_test.dart index bf296e4249..c26a7fe9a9 100644 --- a/hive/test/sentry_box_test.dart +++ b/hive/test/sentry_box_test.dart @@ -6,6 +6,7 @@ import 'package:hive/hive.dart'; import 'package:mockito/mockito.dart'; import 'package:sentry/sentry.dart'; import 'package:sentry_hive/sentry_hive.dart'; +import 'package:sentry_hive/src/sentry_hive_impl.dart'; import 'package:test/test.dart'; import 'package:sentry/src/sentry_tracer.dart'; @@ -14,13 +15,13 @@ import 'mocks/mocks.mocks.dart'; void main() { void verifySpan(String description, SentrySpan? span) { - expect(span?.context.operation, SentryHive.dbOp); + expect(span?.context.operation, SentryHiveImpl.dbOp); expect(span?.context.description, description); expect(span?.status, SpanStatus.ok()); // ignore: invalid_use_of_internal_member - expect(span?.origin, SentryTraceOrigins.autoDbHiveBox); - expect(span?.data[SentryHive.dbSystemKey], SentryHive.dbSystem); - expect(span?.data[SentryHive.dbNameKey], Fixture.dbName); + expect(span?.origin, SentryTraceOrigins.autoDbHiveBaseBox); + expect(span?.data[SentryHiveImpl.dbSystemKey], SentryHiveImpl.dbSystem); + expect(span?.data[SentryHiveImpl.dbNameKey], Fixture.dbName); } group('adds span', () {