Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
## 1.35.1

- Private fields that only memoize a public getter or method of the same
name (`String? _origin; String get origin => _origin ??= ...`) take that
name again, as before 1.35.0 renamed them to `*Cache`: 27 fields (e.g.
`_hostname`, `_port`, `_origin`, `_lastPathPart`, `_fieldsNames`,
`_fieldsWithTypeEntity`, `_totalRules`, `_tablesVersions`), and
`APIRequest._requesterAddressResolved` (its getter's name is taken by the
given address). Caches filled by a private helper or a differently named
member keep `*Cache`. Behavior unchanged (private names only).
- `sqlite3: ^3.9.1` (was `^3.8.0`): its build hook now reports a failed
native-library download from GitHub releases as the HTTP error (e.g.
`504`), not as a hash mismatch (simolus3/sqlite3.dart#435). 3.9.0 also
added `CommonDatabase.trace` and `Database.interrupt()`.

## 1.35.0

API/server layer clean-up (behavior-preserving):
Expand Down
22 changes: 11 additions & 11 deletions lib/src/bones_api_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ typedef APILogger = void Function(
/// Bones API Library class.
class BonesAPI {
// ignore: constant_identifier_names
static const String VERSION = '1.35.0';
static const String VERSION = '1.35.1';

static bool _boot = false;

Expand Down Expand Up @@ -2287,10 +2287,10 @@ class APIRequest extends APIMetricSet with APIPayload {

/// A cache of [lastPathPart]: the request's path doesn't change, and the
/// last path part is read on every routing of the request.
String? _lastPathPartCache;
String? _lastPathPart;

/// Returns the last [pathParts]. Sames as [pathPartReversed] for index `0`.
String get lastPathPart => _lastPathPartCache ??= pathPartReversed(0);
String get lastPathPart => _lastPathPart ??= pathPartReversed(0);

/// Returns from [parameters] a value for [name1]. Defaults to [def] if defined.
V? getParameter<V>(String name1, [V? def]) {
Expand Down Expand Up @@ -2521,9 +2521,9 @@ class APIRequest extends APIMetricSet with APIPayload {

/// Cache of [hostname], parsed from the `Host` header by [_hostnameImpl]
/// on first read (the headers don't change during the request).
String? _hostnameCache;
String? _hostname;

String get hostname => _hostnameCache ??= _hostnameImpl();
String get hostname => _hostname ??= _hostnameImpl();

String _hostnameImpl() {
var host = getHeader('host');
Expand All @@ -2547,9 +2547,9 @@ class APIRequest extends APIMetricSet with APIPayload {

/// Cache of [port], parsed from the `Port`/`Host` headers by [_portImpl]
/// on first read.
int? _portCache;
int? _port;

int get port => _portCache ??= _portImpl();
int get port => _port ??= _portImpl();

int _portImpl() {
var port = getHeader('port');
Expand Down Expand Up @@ -2578,10 +2578,10 @@ class APIRequest extends APIMetricSet with APIPayload {
/// Cache of [requesterAddress], resolved by [_requesterAddressImpl]
/// (the given address, or the `X-Forwarded-For`/`Remote-Address`
/// headers). A `null` result isn't cached (it's re-resolved on each read).
String? _requesterAddressCache;
String? _requesterAddressResolved;

String? get requesterAddress =>
_requesterAddressCache ??= _requesterAddressImpl();
_requesterAddressResolved ??= _requesterAddressImpl();

String? _requesterAddressImpl() {
if (_requesterAddress != null) return _requesterAddress;
Expand All @@ -2606,9 +2606,9 @@ class APIRequest extends APIMetricSet with APIPayload {

/// Cache of [origin], resolved by [_originImpl] (the `Origin` header, or
/// built from [scheme] and [hostnameAndPort]) on first read.
String? _originCache;
String? _origin;

String get origin => _originCache ??= _originImpl();
String get origin => _origin ??= _originImpl();

String _originImpl() {
var origin = getHeader('origin');
Expand Down
102 changes: 47 additions & 55 deletions lib/src/bones_api_entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -568,9 +568,9 @@ abstract class EntityHandler<O> with FieldsFromMap, EntityRulesResolver {

/// Cache of [idType]: the [Type] of the ID field, fixed per entity type
/// and looked up on every ID parse/compare.
Type? _idTypeCache;
Type? _idType;

Type idType([O? o]) => _idTypeCache ??= getFieldType(
Type idType([O? o]) => _idType ??= getFieldType(
o,
idFieldName(o),
resolveFiledName: false,
Expand All @@ -586,20 +586,17 @@ abstract class EntityHandler<O> with FieldsFromMap, EntityRulesResolver {
// The fields of an entity type are fixed, so each filter runs once.

/// Cache of [fieldsWithTypeList]: the fields of a `List` type.
Map<String, TypeInfo>? _fieldsWithTypeListCache;
Map<String, TypeInfo>? _fieldsWithTypeList;

Map<String, TypeInfo> fieldsWithTypeList([O? o]) =>
_fieldsWithTypeListCache ??= fieldsWithType(
(_, fieldType) => fieldType.isList,
o,
);
Map<String, TypeInfo> fieldsWithTypeList([O? o]) => _fieldsWithTypeList ??=
fieldsWithType((_, fieldType) => fieldType.isList, o);

/// Cache of [fieldsWithTypeEntity]: the fields holding an entity
/// (not an [EntityReference]).
Map<String, TypeInfo>? _fieldsWithTypeEntityCache;
Map<String, TypeInfo>? _fieldsWithTypeEntity;

Map<String, TypeInfo> fieldsWithTypeEntity([O? o]) =>
_fieldsWithTypeEntityCache ??= fieldsWithType(
_fieldsWithTypeEntity ??= fieldsWithType(
(_, fieldType) =>
!fieldType.isBasicType &&
!fieldType.isEntityReferenceListType &&
Expand All @@ -610,10 +607,10 @@ abstract class EntityHandler<O> with FieldsFromMap, EntityRulesResolver {

/// Cache of [fieldsWithTypeEntityOrReference]: the fields holding an
/// entity or an [EntityReference].
Map<String, TypeInfo>? _fieldsWithTypeEntityOrReferenceCache;
Map<String, TypeInfo>? _fieldsWithTypeEntityOrReference;

Map<String, TypeInfo> fieldsWithTypeEntityOrReference([O? o]) =>
_fieldsWithTypeEntityOrReferenceCache ??= fieldsWithType(
_fieldsWithTypeEntityOrReference ??= fieldsWithType(
(_, fieldType) =>
(!fieldType.isBasicType &&
!fieldType.isEntityReferenceListType &&
Expand All @@ -625,20 +622,20 @@ abstract class EntityHandler<O> with FieldsFromMap, EntityRulesResolver {

/// Cache of [fieldsWithEntityReference]: the fields of an
/// [EntityReference] type.
Map<String, TypeInfo>? _fieldsWithEntityReferenceCache;
Map<String, TypeInfo>? _fieldsWithEntityReference;

Map<String, TypeInfo> fieldsWithEntityReference([O? o]) =>
_fieldsWithEntityReferenceCache ??= Map<String, TypeInfo>.unmodifiable(
_fieldsWithEntityReference ??= Map<String, TypeInfo>.unmodifiable(
fieldsWithTypeEntityOrReference(o).entries
.where((e) => e.value.isEntityReferenceType)
.toMapFromEntries(),
);

/// Cache of [fieldsWithTypeListEntity]: the fields of a list of entities.
Map<String, TypeInfo>? _fieldsWithTypeListEntityCache;
Map<String, TypeInfo>? _fieldsWithTypeListEntity;

Map<String, TypeInfo> fieldsWithTypeListEntity([O? o]) =>
_fieldsWithTypeListEntityCache ??= fieldsWithType(
_fieldsWithTypeListEntity ??= fieldsWithType(
(_, fieldType) => fieldType.isValidListEntityType,
o,
);
Expand Down Expand Up @@ -668,23 +665,22 @@ abstract class EntityHandler<O> with FieldsFromMap, EntityRulesResolver {

/// Cache of [fieldsWithEntityReferenceList]: the (non-hidden) fields of an
/// [EntityReferenceList] type.
Map<String, TypeInfo>? _fieldsWithEntityReferenceListCache;
Map<String, TypeInfo>? _fieldsWithEntityReferenceList;

Map<String, TypeInfo> fieldsWithEntityReferenceList([O? o]) =>
_fieldsWithEntityReferenceListCache ??=
Map<String, TypeInfo>.unmodifiable(
fieldsWithTypeListEntityOrReference(o).entries
.where((e) => e.value.isEntityReferenceListType)
.toMapFromEntries(),
);
_fieldsWithEntityReferenceList ??= Map<String, TypeInfo>.unmodifiable(
fieldsWithTypeListEntityOrReference(o).entries
.where((e) => e.value.isEntityReferenceListType)
.toMapFromEntries(),
);

/// Cache of [fieldsEntityAnnotations] (also filled by subclasses):
/// field name -> its unmodifiable [EntityAnnotation]s. Built once (fixed
/// fields; a reflection lookup per field).
Map<String, List<EntityAnnotation>>? _fieldsEntityAnnotationsCache;
Map<String, List<EntityAnnotation>>? _fieldsEntityAnnotations;

Map<String, List<EntityAnnotation>> fieldsEntityAnnotations() =>
_fieldsEntityAnnotationsCache ??=
_fieldsEntityAnnotations ??=
Map<String, List<EntityAnnotation>>.unmodifiable(
(getAllFieldsEntityAnnotations() ?? {})
.map<String, List<EntityAnnotation>>(
Expand Down Expand Up @@ -1614,25 +1610,25 @@ abstract class EntityHandler<O> with FieldsFromMap, EntityRulesResolver {

/// Cache of [fieldsNamesIndexes]: field name -> index in [fieldsNames].
/// The fields of an entity type are fixed, so it's built once.
Map<String, int>? _fieldsNamesIndexesCache;
Map<String, int>? _fieldsNamesIndexes;

Map<String, int> fieldsNamesIndexes([O? o]) =>
_fieldsNamesIndexesCache ??= buildFieldsNamesIndexes(fieldsNames(o));
_fieldsNamesIndexes ??= buildFieldsNamesIndexes(fieldsNames(o));

/// Cache of [fieldsNamesLC]: the [fieldsNames] in lower case, used for
/// case-insensitive field lookups. Built once (fixed fields).
List<String>? _fieldsNamesLCCache;
List<String>? _fieldsNamesLC;

List<String> fieldsNamesLC([O? o]) =>
_fieldsNamesLCCache ??= buildFieldsNamesLC(fieldsNames(o));
_fieldsNamesLC ??= buildFieldsNamesLC(fieldsNames(o));

/// Cache of [fieldsNamesSimple]: the [fieldsNames] as simple keys
/// (`fieldToSimpleKey`), used for lenient field lookups. Built once (fixed
/// fields).
List<String>? _fieldsNamesSimpleCache;
List<String>? _fieldsNamesSimple;

List<String> fieldsNamesSimple([O? o]) =>
_fieldsNamesSimpleCache ??= buildFieldsNamesSimple(fieldsNames(o));
_fieldsNamesSimple ??= buildFieldsNamesSimple(fieldsNames(o));

TypeInfo? getFieldType(O? o, String key, {bool resolveFiledName = false});

Expand Down Expand Up @@ -2907,7 +2903,7 @@ class GenericEntityHandler<O extends Entity> extends EntityHandler<O> {
String? _idFieldNameCache;

/// Cache of [fieldsNames], filled by [inspectObject].
List<String>? _fieldsNamesCache;
List<String>? _fieldsNames;

@override
String idFieldName([O? o]) => _inspectedOrThrow(
Expand All @@ -2921,7 +2917,7 @@ class GenericEntityHandler<O extends Entity> extends EntityHandler<O> {
@override
List<String> fieldsNames([O? o]) => _inspectedOrThrow(
o,
() => _fieldsNamesCache,
() => _fieldsNames,
() =>
"`fieldsNames` Not populated yet! No Entity instance presented to this EntityHandler yet. Type: $type",
);
Expand Down Expand Up @@ -2971,7 +2967,7 @@ class GenericEntityHandler<O extends Entity> extends EntityHandler<O> {
if (o != null && _idFieldNameCache == null) {
_idFieldNameCache = o.idFieldName;

final fieldsNames = _fieldsNamesCache ??= List<String>.unmodifiable(
final fieldsNames = _fieldsNames ??= List<String>.unmodifiable(
o.fieldsNames,
);

Expand All @@ -2981,7 +2977,7 @@ class GenericEntityHandler<O extends Entity> extends EntityHandler<O> {
),
);

_fieldsEntityAnnotationsCache ??=
_fieldsEntityAnnotations ??=
Map<String, List<EntityAnnotation>>.unmodifiable(
Map<String, List<EntityAnnotation>>.fromEntries(
fieldsNames.map((f) {
Expand Down Expand Up @@ -3089,7 +3085,7 @@ class GenericEntityHandler<O extends Entity> extends EntityHandler<O> {
if (o != null) {
return o.getFieldEntityAnnotations(key);
} else {
return _fieldsEntityAnnotationsCache?[key];
return _fieldsEntityAnnotations?[key];
}
}

Expand Down Expand Up @@ -3141,10 +3137,10 @@ class ClassReflectionEntityHandler<O> extends EntityHandler<O> {

/// Cache of [siblingsEntityHandlers]: the handlers of the sibling classes
/// (same reflection bundle), fixed after the reflection is registered.
List<ClassReflectionEntityHandler>? _siblingsEntityHandlersCache;
List<ClassReflectionEntityHandler>? _siblingsEntityHandlers;

List<ClassReflectionEntityHandler> siblingsEntityHandlers() =>
_siblingsEntityHandlersCache ??=
_siblingsEntityHandlers ??=
List<ClassReflectionEntityHandler>.unmodifiable(
reflection.siblingsClassReflection().map((c) => c.entityHandler),
);
Expand Down Expand Up @@ -3343,7 +3339,7 @@ class ClassReflectionEntityHandler<O> extends EntityHandler<O> {
if (idFieldName != null) return idFieldName;

// Just to populate:
if (_fieldsNamesCache == null) {
if (_fieldsNames == null) {
fieldsNames(o);
}

Expand Down Expand Up @@ -3403,11 +3399,11 @@ class ClassReflectionEntityHandler<O> extends EntityHandler<O> {
}

/// Cache of [fieldsNames]: the keys of [fieldsTypes] (fixed per class).
List<String>? _fieldsNamesCache;
List<String>? _fieldsNames;

@override
List<String> fieldsNames([O? o]) =>
_fieldsNamesCache ??= List.unmodifiable(fieldsTypes(o).keys);
_fieldsNames ??= List.unmodifiable(fieldsTypes(o).keys);

@override
Map<String, TypeInfo> fieldsTypes([O? o]) {
Expand All @@ -3426,23 +3422,19 @@ class ClassReflectionEntityHandler<O> extends EntityHandler<O> {

/// Cache of [constructors]: constructor name -> its parameters' types.
/// Fixed per class, and a full reflection scan to build.
Map<String, Map<String, TypeInfo>>? _constructorsCache;
Map<String, Map<String, TypeInfo>>? _constructors;

@override
Map<String, Map<String, TypeInfo>>? constructors([O? o]) {
return _constructorsCache ??=
Map<String, Map<String, TypeInfo>>.unmodifiable(
reflectionWithObject(o).allConstructors().map((c) {
var name = c.name;
var args = c.allParameters
.map(
(p) =>
MapEntry<String, TypeInfo>(p.jsonName, p.type.typeInfo),
)
.toMapFromEntries();
return MapEntry(name, Map<String, TypeInfo>.unmodifiable(args));
}).toMapFromEntries(),
);
return _constructors ??= Map<String, Map<String, TypeInfo>>.unmodifiable(
reflectionWithObject(o).allConstructors().map((c) {
var name = c.name;
var args = c.allParameters
.map((p) => MapEntry<String, TypeInfo>(p.jsonName, p.type.typeInfo))
.toMapFromEntries();
return MapEntry(name, Map<String, TypeInfo>.unmodifiable(args));
}).toMapFromEntries(),
);
}

@override
Expand Down
14 changes: 7 additions & 7 deletions lib/src/bones_api_entity_db_memory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ class DBSQLMemoryAdapter extends DBSQLAdapter<DBSQLMemoryAdapterContext>
<String, MapHistory<Object, Map<String, dynamic>>>{};

void _onTablesModification() {
_tablesVersionsCache = null;
_tablesIndexesVersionsCache = null;
_tablesVersions = null;
_tablesIndexesVersions = null;
}

/// A cache of [tablesVersions], filled on its first call and reset by
Expand All @@ -248,10 +248,10 @@ class DBSQLMemoryAdapter extends DBSQLAdapter<DBSQLMemoryAdapterContext>
/// Why: each new connection ([DBSQLMemoryAdapterContext]) snapshots the
/// versions (to roll the tables back to them), usually more often than
/// the tables change: the map isn't rebuilt for each one.
Map<String, int>? _tablesVersionsCache;
Map<String, int>? _tablesVersions;

Map<String, int> get tablesVersions =>
_tablesVersionsCache ??= UnmodifiableMapView(
_tablesVersions ??= UnmodifiableMapView(
_tables.map((table, map) => MapEntry(table, map.version)),
);

Expand Down Expand Up @@ -616,11 +616,11 @@ class DBSQLMemoryAdapter extends DBSQLAdapter<DBSQLMemoryAdapterContext>
<String, Map<String, MapHistory<Object, Set<Object>>>>{};

/// A cache of [tablesIndexesVersions], filled on its first call and reset
/// by [_onTablesModification]. As [_tablesVersionsCache].
Map<String, Map<String, int>>? _tablesIndexesVersionsCache;
/// by [_onTablesModification]. As [_tablesVersions].
Map<String, Map<String, int>>? _tablesIndexesVersions;

Map<String, Map<String, int>> get tablesIndexesVersions =>
_tablesIndexesVersionsCache ??= UnmodifiableMapView(
_tablesIndexesVersions ??= UnmodifiableMapView(
_tablesIndexes.map(
(table, fieldIndex) => MapEntry(
table,
Expand Down
Loading
Loading