Skip to content

Commit

Permalink
add askar scan
Browse files Browse the repository at this point in the history
  • Loading branch information
bruno-br committed Feb 13, 2025
1 parent e5ae0f3 commit 88af89d
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 14 deletions.
18 changes: 6 additions & 12 deletions lib/askar/askar_wrapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1198,21 +1198,15 @@ Future<AskarResult<EntryListHandle>> askarScanNext(ScanHandle handle) async {
completedResult.errorCode, EntryListHandle(completedResult.value));
}

Future<AskarResult<ScanHandle>> askarScanStart(
StoreHandle handle,
String profile,
String category,
Map tagFilter,
int offset,
int limit,
) async {
Future<AskarResult<ScanHandle>> askarScanStart(StoreHandle handle,
{String? profile, String? category, Map? tagFilter, int? offset, int? limit}) async {
Pointer<Utf8> profilePointer = nullptr;
Pointer<Utf8> categoryPointer = nullptr;
Pointer<Utf8> tagFilterPointer = nullptr;

try {
profilePointer = profile.toNativeUtf8();
categoryPointer = category.toNativeUtf8();
profilePointer = (profile ?? "").toNativeUtf8();
categoryPointer = (category ?? "").toNativeUtf8();
tagFilterPointer = jsonEncode(tagFilter).toNativeUtf8();

final callback = newCallbackWithHandle();
Expand All @@ -1222,8 +1216,8 @@ Future<AskarResult<ScanHandle>> askarScanStart(
profilePointer,
categoryPointer,
tagFilterPointer,
offset,
limit,
(offset ?? 0),
(limit ?? 0),
callback.nativeCallable.nativeFunction,
callback.id,
);
Expand Down
82 changes: 82 additions & 0 deletions lib/askar/store/scan.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import 'package:askar_flutter_sdk/askar/askar_wrapper.dart';
import 'package:askar_flutter_sdk/askar/crypto/handles.dart';
import 'package:askar_flutter_sdk/askar/exceptions/exceptions.dart';

import 'entry.dart';
import 'entry_list.dart';
import 'store.dart';

class Scan {
ScanHandle? handle;
final Store store;
final String? profile;
final String category;
final Map<String, dynamic>? tagFilter;
final int? offset;
final int? limit;
final String? orderBy;
final bool? descending;

Scan({
required this.store,
required this.category,
this.profile,
this.tagFilter,
this.offset,
this.limit,
this.orderBy,
this.descending,
});

Future<List<EntryObject>> fetchAll() async {
final rows = <EntryObject>[];
await _forEachRow((row, _) => rows.add(row.toJson()));
return rows;
}

Future<void> _forEachRow(void Function(Entry row, int? index) cb) async {
_initHandle();

try {
int recordCount = 0;

while (limit == null || recordCount < limit!) {
final scanResult = await askarScanNext(handle!);

if (!scanResult.errorCode.isSuccess()) {
break;
}

final list = EntryList(handle: scanResult.value);

recordCount += list.length;
for (int i = 0; i < list.length; i++) {
final entry = list.getEntryByIndex(i);
cb(entry, i);
}

askarEntryListFree(list.handle);
}
} catch (e) {
AskarScanException('Failed to scan: $e');
} finally {
if (handle != null) {
askarScanFree(handle!);
}
}
}

Future<void> _initHandle() async {
try {
handle ??= (await askarScanStart(store.handle,
profile: profile,
category: category,
limit: limit,
offset: offset,
tagFilter: tagFilter))
.getValueOrException();
} catch (e) {
AskarScanException('Failed to start scan: $e');
}
}
}
10 changes: 8 additions & 2 deletions test/askar_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -650,8 +650,14 @@ Future<AskarResult<ScanHandle>> scanStartTest(
final int offset = 0;
final int limit = 2;

final result =
await askarScanStart(handle, profile, category, tagFilter, offset, limit);
final result = await askarScanStart(
handle,
profile: profile,
category: category,
tagFilter: tagFilter,
offset: offset,
limit: limit,
);

printAskarResult('ScanStart', result);

Expand Down

0 comments on commit 88af89d

Please sign in to comment.