veilid/veilid-flutter/lib/veilid_table_db.dart

70 lines
2.5 KiB
Dart
Raw Normal View History

2023-05-29 19:24:57 +00:00
import 'dart:async';
import 'dart:convert';
2023-07-26 18:20:17 +00:00
import 'dart:typed_data';
2023-05-29 19:24:57 +00:00
/////////////////////////////////////
/// VeilidTableDB
abstract class VeilidTableDBTransaction {
2023-07-19 14:07:51 +00:00
bool isDone();
2023-05-29 19:24:57 +00:00
Future<void> commit();
Future<void> rollback();
Future<void> store(int col, Uint8List key, Uint8List value);
2023-06-05 02:08:46 +00:00
Future<void> delete(int col, Uint8List key);
2023-05-29 19:24:57 +00:00
Future<void> storeJson(int col, Uint8List key, Object? object,
2023-07-30 19:57:06 +00:00
{Object? Function(Object? nonEncodable)? toEncodable}) async =>
store(col, key,
utf8.encoder.convert(jsonEncode(object, toEncodable: toEncodable)));
2023-05-29 19:24:57 +00:00
Future<void> storeStringJson(int col, String key, Object? object,
2023-07-30 19:57:06 +00:00
{Object? Function(Object? nonEncodable)? toEncodable}) =>
storeJson(col, utf8.encoder.convert(key), object,
toEncodable: toEncodable);
2023-05-29 19:24:57 +00:00
}
abstract class VeilidTableDB {
2023-07-19 14:07:51 +00:00
void close();
2023-05-29 19:24:57 +00:00
int getColumnCount();
Future<List<Uint8List>> getKeys(int col);
VeilidTableDBTransaction transact();
Future<void> store(int col, Uint8List key, Uint8List value);
Future<Uint8List?> load(int col, Uint8List key);
Future<Uint8List?> delete(int col, Uint8List key);
Future<void> storeJson(int col, Uint8List key, Object? object,
2023-07-30 19:57:06 +00:00
{Object? Function(Object? nonEncodable)? toEncodable}) =>
store(col, key,
utf8.encoder.convert(jsonEncode(object, toEncodable: toEncodable)));
2023-05-29 19:24:57 +00:00
Future<void> storeStringJson(int col, String key, Object? object,
2023-07-30 19:57:06 +00:00
{Object? Function(Object? nonEncodable)? toEncodable}) =>
storeJson(col, utf8.encoder.convert(key), object,
toEncodable: toEncodable);
2023-05-29 19:24:57 +00:00
Future<Object?> loadJson(int col, Uint8List key,
{Object? Function(Object? key, Object? value)? reviver}) async {
2023-07-26 18:20:17 +00:00
final s = await load(col, key);
2023-05-29 19:24:57 +00:00
if (s == null) {
return null;
}
return jsonDecode(utf8.decode(s, allowMalformed: false), reviver: reviver);
}
Future<Object?> loadStringJson(int col, String key,
2023-07-30 19:57:06 +00:00
{Object? Function(Object? key, Object? value)? reviver}) =>
loadJson(col, utf8.encoder.convert(key), reviver: reviver);
2023-05-29 19:24:57 +00:00
Future<Object?> deleteJson(int col, Uint8List key,
{Object? Function(Object? key, Object? value)? reviver}) async {
2023-07-26 18:20:17 +00:00
final s = await delete(col, key);
2023-05-29 19:24:57 +00:00
if (s == null) {
return null;
}
return jsonDecode(utf8.decode(s, allowMalformed: false), reviver: reviver);
}
Future<Object?> deleteStringJson(int col, String key,
2023-07-30 19:57:06 +00:00
{Object? Function(Object? key, Object? value)? reviver}) =>
deleteJson(col, utf8.encoder.convert(key), reviver: reviver);
2023-05-29 19:24:57 +00:00
}