Skip to content

Commit 296e25e

Browse files
committed
introduce base box to avoid duplication
1 parent 5881d84 commit 296e25e

File tree

7 files changed

+260
-178
lines changed

7 files changed

+260
-178
lines changed

dart/lib/src/sentry_trace_origins.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ class SentryTraceOrigins {
2020
'auto.db.sqflite.database_factory';
2121

2222
static const autoDbHive = 'auto.db.hive';
23-
static const autoDbHiveBox = 'auto.db.hive.box';
23+
static const autoDbHiveOpenDatabase = 'auto.db.hive.open_database';
24+
static const autoDbHiveBaseBox = 'auto.db.hive.base_box';
2425
}

hive/example/example.dart

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import 'package:hive/hive.dart';
2+
import 'package:sentry/sentry.dart';
3+
import 'package:sentry_hive/sentry_hive.dart';
4+
5+
part 'main.g.dart';
6+
7+
Future<void> main() async {
8+
// ATTENTION: Change the DSN below with your own to see the events in Sentry. Get one at sentry.io
9+
const dsn =
10+
'https://e85b375ffb9f43cf8bdf9787768149e0@o447951.ingest.sentry.io/5428562';
11+
12+
await Sentry.init(
13+
(options) {
14+
options.dsn = dsn;
15+
options.tracesSampleRate = 1.0;
16+
options.debug = true;
17+
},
18+
appRunner: runApp, // Init your App.
19+
);
20+
}
21+
22+
Future<void> runApp() async {
23+
Hive
24+
..init(Directory.current.path)
25+
..registerAdapter(PersonAdapter());
26+
27+
var box = await Hive.openBox('testBox');
28+
29+
var sentryBox = SentryBox
30+
31+
var person = Person(name: 'Dave', age: 23);
32+
33+
}
34+
35+
@HiveType(typeId: 1)
36+
class Person {
37+
Person({required this.name, required this.age});
38+
39+
@HiveField(0)
40+
String name;
41+
42+
@HiveField(1)
43+
int age;
44+
45+
@override
46+
String toString() {
47+
return '$name: $age';
48+
}
49+
}

hive/example/hive_example.dart

Lines changed: 0 additions & 6 deletions
This file was deleted.

hive/lib/src/sentry_box.dart

Lines changed: 5 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -2,93 +2,16 @@ import 'package:meta/meta.dart';
22
import 'package:hive/hive.dart';
33
import 'package:sentry/sentry.dart';
44

5-
import 'sentry_hive_impl.dart';
5+
import 'sentry_box_base.dart';
66

77
///
88
@experimental
9-
class SentryBox<E> implements Box<E> {
9+
class SentryBox<E> extends SentryBoxBase<E> implements Box<E> {
1010

1111
final Box<E> _box;
12-
final Hub _hub;
1312

14-
// ignore: public_member_api_docs
15-
SentryBox(this._box, @internal Hub? hub) : _hub = hub ?? HubAdapter();
16-
17-
@override
18-
Future<int> add(E value) async {
19-
return _asyncWrapInSpan('add', () async {
20-
return await _box.add(value);
21-
});
22-
}
23-
24-
@override
25-
Future<Iterable<int>> addAll(Iterable<E> values) async {
26-
return _asyncWrapInSpan('addAll', () async {
27-
return await _box.addAll(values);
28-
});
29-
}
30-
31-
@override
32-
Future<int> clear() async {
33-
return _asyncWrapInSpan('clear', () async {
34-
return await _box.clear();
35-
});
36-
}
37-
38-
@override
39-
Future<void> close() async {
40-
return _asyncWrapInSpan('close', () async {
41-
return await _box.close();
42-
});
43-
}
44-
45-
@override
46-
Future<void> compact() async {
47-
return _asyncWrapInSpan('compact', () async {
48-
return await _box.compact();
49-
});
50-
}
51-
52-
@override
53-
bool containsKey(key) {
54-
return _box.containsKey(key);
55-
}
56-
57-
@override
58-
Future<void> delete(key) async {
59-
return _asyncWrapInSpan('delete', () async {
60-
return await _box.delete(key);
61-
});
62-
}
63-
64-
@override
65-
// ignore: strict_raw_type
66-
Future<void> deleteAll(Iterable keys) async {
67-
return _asyncWrapInSpan('deleteAll', () async {
68-
return await _box.deleteAll(keys);
69-
});
70-
}
71-
72-
@override
73-
Future<void> deleteAt(int index) async {
74-
return _asyncWrapInSpan('deleteAt', () async {
75-
return await _box.deleteAt(index);
76-
});
77-
}
78-
79-
@override
80-
Future<void> deleteFromDisk() async {
81-
return _asyncWrapInSpan('deleteFromDisk', () async {
82-
return await _box.deleteFromDisk();
83-
});
84-
}
85-
86-
@override
87-
Future<void> flush() async {
88-
return _asyncWrapInSpan('flush', () async {
89-
return await _box.flush();
90-
});
91-
}
13+
///
14+
SentryBox(this._box, @internal Hub hub) : super(_box, hub);
9215

9316
@override
9417
E? get(key, {E? defaultValue}) {
@@ -100,102 +23,17 @@ class SentryBox<E> implements Box<E> {
10023
return _box.getAt(index);
10124
}
10225

103-
@override
104-
bool get isEmpty => _box.isEmpty;
105-
106-
@override
107-
bool get isNotEmpty => _box.isNotEmpty;
108-
109-
@override
110-
bool get isOpen => _box.isOpen;
111-
112-
@override
113-
dynamic keyAt(int index) {
114-
return _box.keyAt(index);
115-
}
116-
117-
@override
118-
// ignore: strict_raw_type
119-
Iterable get keys => _box.keys;
120-
121-
@override
122-
bool get lazy => _box.lazy;
123-
124-
@override
125-
int get length => _box.length;
126-
127-
@override
128-
String get name => _box.name;
129-
130-
@override
131-
String? get path => _box.path;
132-
133-
@override
134-
Future<void> put(key, E value) async {
135-
return _asyncWrapInSpan('put', () async {
136-
return await _box.put(key, value);
137-
});
138-
}
139-
140-
@override
141-
Future<void> putAll(Map<dynamic, E> entries) async {
142-
return _asyncWrapInSpan('putAll', () async {
143-
return await _box.putAll(entries);
144-
});
145-
}
146-
147-
@override
148-
Future<void> putAt(int index, E value) async {
149-
return _asyncWrapInSpan('putAt', () async {
150-
return await _box.putAt(index, value);
151-
});
152-
}
153-
15426
@override
15527
Map<dynamic, E> toMap() {
15628
return _box.toMap();
15729
}
15830

15931
@override
32+
// TODO: implement values
16033
Iterable<E> get values => _box.values;
16134

16235
@override
16336
Iterable<E> valuesBetween({startKey, endKey}) {
16437
return _box.valuesBetween(startKey: startKey, endKey: endKey);
16538
}
166-
167-
@override
168-
Stream<BoxEvent> watch({key}) {
169-
return _box.watch(key: key);
170-
}
171-
172-
// Helper
173-
174-
Future<T> _asyncWrapInSpan<T>(String description, Future<T> Function() execute) async {
175-
final currentSpan = _hub.getSpan();
176-
final span = currentSpan?.startChild(
177-
SentryHiveImpl.dbOp,
178-
description: description,
179-
);
180-
181-
// ignore: invalid_use_of_internal_member
182-
span?.origin = SentryTraceOrigins.autoDbHiveBox;
183-
184-
span?.setData(SentryHiveImpl.dbSystemKey, SentryHiveImpl.dbSystem);
185-
span?.setData(SentryHiveImpl.dbNameKey, name);
186-
187-
try {
188-
final result = await execute();
189-
span?.status = SpanStatus.ok();
190-
191-
return result;
192-
} catch (exception) {
193-
span?.throwable = exception;
194-
span?.status = SpanStatus.internalError();
195-
196-
rethrow;
197-
} finally {
198-
await span?.finish();
199-
}
200-
}
20139
}

0 commit comments

Comments
 (0)