|
| 1 | +import 'package:meta/meta.dart'; |
| 2 | +import 'package:hive/hive.dart'; |
| 3 | +import 'package:sentry/sentry.dart'; |
| 4 | + |
| 5 | +import '../sentry_hive.dart'; |
| 6 | + |
| 7 | +// ignore: public_member_api_docs |
| 8 | +class SentryBox<E> implements Box<E> { |
| 9 | + |
| 10 | + final Box<E> _box; |
| 11 | + final Hub _hub; |
| 12 | + |
| 13 | + // ignore: public_member_api_docs |
| 14 | + SentryBox(this._box, @internal Hub? hub) : _hub = hub ?? HubAdapter(); |
| 15 | + |
| 16 | + @override |
| 17 | + Future<int> add(E value) async { |
| 18 | + return _asyncWrapInSpan('add', () async { |
| 19 | + return await _box.add(value); |
| 20 | + }); |
| 21 | + } |
| 22 | + |
| 23 | + @override |
| 24 | + Future<Iterable<int>> addAll(Iterable<E> values) async { |
| 25 | + return _asyncWrapInSpan('addAll', () async { |
| 26 | + return await _box.addAll(values); |
| 27 | + }); |
| 28 | + } |
| 29 | + |
| 30 | + @override |
| 31 | + Future<int> clear() async { |
| 32 | + return _asyncWrapInSpan('clear', () async { |
| 33 | + return await _box.clear(); |
| 34 | + }); |
| 35 | + } |
| 36 | + |
| 37 | + @override |
| 38 | + Future<void> close() async { |
| 39 | + return _asyncWrapInSpan('close', () async { |
| 40 | + return await _box.close(); |
| 41 | + }); |
| 42 | + } |
| 43 | + |
| 44 | + @override |
| 45 | + Future<void> compact() async { |
| 46 | + return _asyncWrapInSpan('compact', () async { |
| 47 | + return await _box.compact(); |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + @override |
| 52 | + bool containsKey(key) { |
| 53 | + return _box.containsKey(key); |
| 54 | + } |
| 55 | + |
| 56 | + @override |
| 57 | + Future<void> delete(key) async { |
| 58 | + return _asyncWrapInSpan('delete', () async { |
| 59 | + return await _box.delete(key); |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + @override |
| 64 | + // ignore: strict_raw_type |
| 65 | + Future<void> deleteAll(Iterable keys) async { |
| 66 | + return _asyncWrapInSpan('delete', () async { |
| 67 | + return await _box.deleteAll(keys); |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + @override |
| 72 | + Future<void> deleteAt(int index) async { |
| 73 | + return _asyncWrapInSpan('deleteAt', () async { |
| 74 | + return await _box.deleteAt(index); |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + @override |
| 79 | + Future<void> deleteFromDisk() async { |
| 80 | + return _asyncWrapInSpan('deleteFromDisk', () async { |
| 81 | + return await _box.deleteFromDisk(); |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + @override |
| 86 | + Future<void> flush() async { |
| 87 | + return _asyncWrapInSpan('flush', () async { |
| 88 | + return await _box.flush(); |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + @override |
| 93 | + E? get(key, {E? defaultValue}) { |
| 94 | + return _box.get(key, defaultValue: defaultValue); |
| 95 | + } |
| 96 | + |
| 97 | + @override |
| 98 | + E? getAt(int index) { |
| 99 | + return _box.getAt(index); |
| 100 | + } |
| 101 | + |
| 102 | + @override |
| 103 | + bool get isEmpty => _box.isEmpty; |
| 104 | + |
| 105 | + @override |
| 106 | + bool get isNotEmpty => _box.isNotEmpty; |
| 107 | + |
| 108 | + @override |
| 109 | + bool get isOpen => _box.isOpen; |
| 110 | + |
| 111 | + @override |
| 112 | + dynamic keyAt(int index) { |
| 113 | + return _box.keyAt(index); |
| 114 | + } |
| 115 | + |
| 116 | + @override |
| 117 | + // ignore: strict_raw_type |
| 118 | + Iterable get keys => _box.keys; |
| 119 | + |
| 120 | + @override |
| 121 | + bool get lazy => _box.lazy; |
| 122 | + |
| 123 | + @override |
| 124 | + int get length => _box.length; |
| 125 | + |
| 126 | + @override |
| 127 | + String get name => _box.name; |
| 128 | + |
| 129 | + @override |
| 130 | + String? get path => _box.path; |
| 131 | + |
| 132 | + @override |
| 133 | + Future<void> put(key, E value) async { |
| 134 | + return _asyncWrapInSpan('put', () async { |
| 135 | + return await _box.put(key, value); |
| 136 | + }); |
| 137 | + } |
| 138 | + |
| 139 | + @override |
| 140 | + Future<void> putAll(Map<dynamic, E> entries) async { |
| 141 | + return _asyncWrapInSpan('putAll', () async { |
| 142 | + return await _box.putAll(entries); |
| 143 | + }); |
| 144 | + } |
| 145 | + |
| 146 | + @override |
| 147 | + Future<void> putAt(int index, E value) async { |
| 148 | + return _asyncWrapInSpan('putAt', () async { |
| 149 | + return await _box.putAt(index, value); |
| 150 | + }); |
| 151 | + } |
| 152 | + |
| 153 | + @override |
| 154 | + Map<dynamic, E> toMap() { |
| 155 | + return _box.toMap(); |
| 156 | + } |
| 157 | + |
| 158 | + @override |
| 159 | + Iterable<E> get values => _box.values; |
| 160 | + |
| 161 | + @override |
| 162 | + Iterable<E> valuesBetween({startKey, endKey}) { |
| 163 | + return _box.valuesBetween(startKey: startKey, endKey: endKey); |
| 164 | + } |
| 165 | + |
| 166 | + @override |
| 167 | + Stream<BoxEvent> watch({key}) { |
| 168 | + return _box.watch(key: key); |
| 169 | + } |
| 170 | + |
| 171 | + // Helper |
| 172 | + |
| 173 | + Future<T> _asyncWrapInSpan<T>(String description, Future<T> Function() execute) async { |
| 174 | + final currentSpan = _hub.getSpan(); |
| 175 | + final span = currentSpan?.startChild( |
| 176 | + SentryHive.dbOp, |
| 177 | + description: description, |
| 178 | + ); |
| 179 | + |
| 180 | + // ignore: invalid_use_of_internal_member |
| 181 | + span?.origin = SentryTraceOrigins.autoDbHiveBox; |
| 182 | + |
| 183 | + span?.setData(SentryHive.dbSystemKey, SentryHive.dbSystem); |
| 184 | + span?.setData(SentryHive.dbNameKey, name); |
| 185 | + |
| 186 | + try { |
| 187 | + final result = await execute(); |
| 188 | + span?.status = SpanStatus.ok(); |
| 189 | + |
| 190 | + return result; |
| 191 | + } catch (exception) { |
| 192 | + span?.throwable = exception; |
| 193 | + span?.status = SpanStatus.internalError(); |
| 194 | + |
| 195 | + rethrow; |
| 196 | + } finally { |
| 197 | + await span?.finish(); |
| 198 | + } |
| 199 | + } |
| 200 | +} |
0 commit comments