Skip to content

Commit

Permalink
more
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyokone committed Oct 28, 2024
1 parent 47163aa commit e9a3202
Show file tree
Hide file tree
Showing 16 changed files with 235 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1924,8 +1924,9 @@ void findNearest(
@NonNull String path,
@NonNull Boolean isCollectionGroup,
@NonNull PigeonQueryParameters parameters,
@NonNull PigeonGetOptions options,
@NonNull List<Double> queryVector,
@NonNull VectorSource source,
@NonNull Long limit,
@NonNull VectorQueryOptions queryOptions,
@NonNull DistanceMeasure distanceMeasure,
@NonNull Result<PigeonQuerySnapshot> result);
Expand Down Expand Up @@ -2614,10 +2615,11 @@ public void error(Throwable error) {
String pathArg = (String) args.get(1);
Boolean isCollectionGroupArg = (Boolean) args.get(2);
PigeonQueryParameters parametersArg = (PigeonQueryParameters) args.get(3);
PigeonGetOptions optionsArg = (PigeonGetOptions) args.get(4);
List<Double> queryVectorArg = (List<Double>) args.get(4);
VectorSource sourceArg = VectorSource.values()[(int) args.get(5)];
VectorQueryOptions queryOptionsArg = (VectorQueryOptions) args.get(6);
DistanceMeasure distanceMeasureArg = DistanceMeasure.values()[(int) args.get(7)];
Number limitArg = (Number) args.get(6);
VectorQueryOptions queryOptionsArg = (VectorQueryOptions) args.get(7);
DistanceMeasure distanceMeasureArg = DistanceMeasure.values()[(int) args.get(8)];
Result<PigeonQuerySnapshot> resultCallback =
new Result<PigeonQuerySnapshot>() {
public void success(PigeonQuerySnapshot result) {
Expand All @@ -2636,8 +2638,9 @@ public void error(Throwable error) {
pathArg,
isCollectionGroupArg,
parametersArg,
optionsArg,
queryVectorArg,
sourceArg,
(limitArg == null) ? null : limitArg.longValue(),
queryOptionsArg,
distanceMeasureArg,
resultCallback);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1323,28 +1323,30 @@ void FirebaseFirestoreHostApiSetup(id<FlutterBinaryMessenger> binaryMessenger,
if (api) {
NSCAssert([api respondsToSelector:@selector
(findNearestApp:
path:isCollectionGroup:parameters:options:source:queryOptions
:distanceMeasure:completion:)],
path:isCollectionGroup:parameters:queryVector:source:limit
:queryOptions:distanceMeasure:completion:)],
@"FirebaseFirestoreHostApi api (%@) doesn't respond to "
@"@selector(findNearestApp:path:isCollectionGroup:parameters:options:source:"
@"queryOptions:distanceMeasure:completion:)",
@"@selector(findNearestApp:path:isCollectionGroup:parameters:queryVector:source:"
@"limit:queryOptions:distanceMeasure:completion:)",
api);
[channel setMessageHandler:^(id _Nullable message, FlutterReply callback) {
NSArray *args = message;
FirestorePigeonFirebaseApp *arg_app = GetNullableObjectAtIndex(args, 0);
NSString *arg_path = GetNullableObjectAtIndex(args, 1);
NSNumber *arg_isCollectionGroup = GetNullableObjectAtIndex(args, 2);
PigeonQueryParameters *arg_parameters = GetNullableObjectAtIndex(args, 3);
PigeonGetOptions *arg_options = GetNullableObjectAtIndex(args, 4);
NSArray<NSNumber *> *arg_queryVector = GetNullableObjectAtIndex(args, 4);
VectorSource arg_source = [GetNullableObjectAtIndex(args, 5) integerValue];
VectorQueryOptions *arg_queryOptions = GetNullableObjectAtIndex(args, 6);
DistanceMeasure arg_distanceMeasure = [GetNullableObjectAtIndex(args, 7) integerValue];
NSNumber *arg_limit = GetNullableObjectAtIndex(args, 6);
VectorQueryOptions *arg_queryOptions = GetNullableObjectAtIndex(args, 7);
DistanceMeasure arg_distanceMeasure = [GetNullableObjectAtIndex(args, 8) integerValue];
[api findNearestApp:arg_app
path:arg_path
isCollectionGroup:arg_isCollectionGroup
parameters:arg_parameters
options:arg_options
queryVector:arg_queryVector
source:arg_source
limit:arg_limit
queryOptions:arg_queryOptions
distanceMeasure:arg_distanceMeasure
completion:^(PigeonQuerySnapshot *_Nullable output,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,9 @@ NSObject<FlutterMessageCodec> *FirebaseFirestoreHostApiGetCodec(void);
path:(NSString *)path
isCollectionGroup:(NSNumber *)isCollectionGroup
parameters:(PigeonQueryParameters *)parameters
options:(PigeonGetOptions *)options
queryVector:(NSArray<NSNumber *> *)queryVector
source:(VectorSource)source
limit:(NSNumber *)limit
queryOptions:(VectorQueryOptions *)queryOptions
distanceMeasure:(DistanceMeasure)distanceMeasure
completion:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import 'dart:convert';
// ignore: unnecessary_import
import 'dart:typed_data';

import 'package:cloud_firestore/src/vector_query.dart';
import 'package:cloud_firestore_platform_interface/cloud_firestore_platform_interface.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_core_platform_interface/firebase_core_platform_interface.dart'
Expand All @@ -31,6 +30,7 @@ export 'package:cloud_firestore_platform_interface/cloud_firestore_platform_inte
ServerTimestampBehavior,
SetOptions,
ListenSource,
VectorSource,
DocumentChangeType,
PersistenceSettings,
Settings,
Expand Down Expand Up @@ -67,4 +67,6 @@ part 'src/query_snapshot.dart';
part 'src/snapshot_metadata.dart';
part 'src/transaction.dart';
part 'src/utils/codec_utility.dart';
part 'src/vector_query.dart';
part 'src/vector_query_snapshot.dart';
part 'src/write_batch.dart';
42 changes: 41 additions & 1 deletion packages/cloud_firestore/cloud_firestore/lib/src/query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ abstract class Query<T extends Object?> {

VectorQuery findNearest(
String field, {
required Object queryVector, // List<double> or VectorValue
/// List<double> or VectorValue
required Object queryVector,
required int limit,
required DistanceMeasure distanceMeasure,
required VectorQueryOptions options,
Expand Down Expand Up @@ -908,6 +909,27 @@ class _JsonQuery implements Query<Map<String, dynamic>> {
this,
);
}

@override
VectorQuery findNearest(
String field, {
/// List<double> or VectorValue
required Object queryVector,
required int limit,
required DistanceMeasure distanceMeasure,
required VectorQueryOptions options,
}) {
return VectorQuery._(
_delegate.findNearest(
field,
queryVector: queryVector,
limit: limit,
distanceMeasure: distanceMeasure,
options: options,
),
this,
);
}
}

class _WithConverterQuery<T extends Object?> implements Query<T> {
Expand Down Expand Up @@ -1152,4 +1174,22 @@ class _WithConverterQuery<T extends Object?> implements Query<T> {
aggregateField30,
);
}

@override
VectorQuery findNearest(
String field, {
/// List<double> or VectorValue
required Object queryVector,
required int limit,
required DistanceMeasure distanceMeasure,
required VectorQueryOptions options,
}) {
return _originalQuery.findNearest(
field,
queryVector: queryVector,
limit: limit,
distanceMeasure: distanceMeasure,
options: options,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class VectorQuery {

/// Returns an [VectorQuerySnapshot] with the count of the documents that match the query.
Future<VectorQuerySnapshot> get({
AggregateSource source = AggregateSource.server,
VectorSource source = VectorSource.server,
}) async {
return VectorQuerySnapshot._(await _delegate.get(source: source), query);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2022, the Chromium project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

part of cloud_firestore;

/// [VectorQuerySnapshot] represents a response to an [VectorQuery] request.
class VectorQuerySnapshot extends _JsonQuerySnapshot {
VectorQuerySnapshot._(
super.firestore,
super.delegate,
) : super();
}
47 changes: 27 additions & 20 deletions packages/cloud_firestore/cloud_firestore/windows/messages.g.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2161,21 +2161,27 @@ void FirebaseFirestoreHostApi::SetUp(flutter::BinaryMessenger* binary_messenger,
const auto& parameters_arg =
std::any_cast<const PigeonQueryParameters&>(
std::get<CustomEncodableValue>(encodable_parameters_arg));
const auto& encodable_options_arg = args.at(4);
if (encodable_options_arg.IsNull()) {
reply(WrapError("options_arg unexpectedly null."));
const auto& encodable_query_vector_arg = args.at(4);
if (encodable_query_vector_arg.IsNull()) {
reply(WrapError("query_vector_arg unexpectedly null."));
return;
}
const auto& options_arg = std::any_cast<const PigeonGetOptions&>(
std::get<CustomEncodableValue>(encodable_options_arg));
const auto& query_vector_arg =
std::get<EncodableList>(encodable_query_vector_arg);
const auto& encodable_source_arg = args.at(5);
if (encodable_source_arg.IsNull()) {
reply(WrapError("source_arg unexpectedly null."));
return;
}
const VectorSource& source_arg =
(VectorSource)encodable_source_arg.LongValue();
const auto& encodable_query_options_arg = args.at(6);
const auto& encodable_limit_arg = args.at(6);
if (encodable_limit_arg.IsNull()) {
reply(WrapError("limit_arg unexpectedly null."));
return;
}
const int64_t limit_arg = encodable_limit_arg.LongValue();
const auto& encodable_query_options_arg = args.at(7);
if (encodable_query_options_arg.IsNull()) {
reply(WrapError("query_options_arg unexpectedly null."));
return;
Expand All @@ -2184,26 +2190,27 @@ void FirebaseFirestoreHostApi::SetUp(flutter::BinaryMessenger* binary_messenger,
std::any_cast<const VectorQueryOptions&>(
std::get<CustomEncodableValue>(
encodable_query_options_arg));
const auto& encodable_distance_measure_arg = args.at(7);
const auto& encodable_distance_measure_arg = args.at(8);
if (encodable_distance_measure_arg.IsNull()) {
reply(WrapError("distance_measure_arg unexpectedly null."));
return;
}
const DistanceMeasure& distance_measure_arg =
(DistanceMeasure)encodable_distance_measure_arg.LongValue();
api->FindNearest(app_arg, path_arg, is_collection_group_arg,
parameters_arg, options_arg, source_arg,
query_options_arg, distance_measure_arg,
[reply](ErrorOr<PigeonQuerySnapshot>&& output) {
if (output.has_error()) {
reply(WrapError(output.error()));
return;
}
EncodableList wrapped;
wrapped.push_back(CustomEncodableValue(
std::move(output).TakeValue()));
reply(EncodableValue(std::move(wrapped)));
});
api->FindNearest(
app_arg, path_arg, is_collection_group_arg, parameters_arg,
query_vector_arg, source_arg, limit_arg, query_options_arg,
distance_measure_arg,
[reply](ErrorOr<PigeonQuerySnapshot>&& output) {
if (output.has_error()) {
reply(WrapError(output.error()));
return;
}
EncodableList wrapped;
wrapped.push_back(
CustomEncodableValue(std::move(output).TakeValue()));
reply(EncodableValue(std::move(wrapped)));
});
} catch (const std::exception& exception) {
reply(WrapError(exception.what()));
}
Expand Down
4 changes: 2 additions & 2 deletions packages/cloud_firestore/cloud_firestore/windows/messages.g.h
Original file line number Diff line number Diff line change
Expand Up @@ -754,8 +754,8 @@ class FirebaseFirestoreHostApi {
virtual void FindNearest(
const FirestorePigeonFirebaseApp& app, const std::string& path,
bool is_collection_group, const PigeonQueryParameters& parameters,
const PigeonGetOptions& options, const VectorSource& source,
const VectorQueryOptions& query_options,
const flutter::EncodableList& query_vector, const VectorSource& source,
int64_t limit, const VectorQueryOptions& query_options,
const DistanceMeasure& distance_measure,
std::function<void(ErrorOr<PigeonQuerySnapshot> reply)> result) = 0;
virtual void WriteBatchCommit(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'dart:async';
import 'package:_flutterfire_internals/_flutterfire_internals.dart';
import 'package:cloud_firestore_platform_interface/cloud_firestore_platform_interface.dart';
import 'package:cloud_firestore_platform_interface/src/internal/pointer.dart';
import 'package:cloud_firestore_platform_interface/src/method_channel/method_channel_vector_query.dart';
import 'package:cloud_firestore_platform_interface/src/platform_interface/platform_interface_query.dart'
as query;
import 'package:collection/collection.dart';
Expand Down Expand Up @@ -417,6 +418,31 @@ class MethodChannelQuery extends QueryPlatform {
);
}

@override
VectorQueryPlatform findNearest(
String field, {
/// List<double> or VectorValue
required Object queryVector,
required int limit,
required DistanceMeasure distanceMeasure,
required VectorQueryOptions options,
}) {
return MethodChannelVectorQuery(
firestore,
this,
_pigeonParameters,
_pointer.path,
pigeonApp,
queryVector is List<double>
? queryVector
: (queryVector as VectorValue).toList(),
limit,
distanceMeasure,
options,
isCollectionGroupQuery,
);
}

@override
bool operator ==(Object other) {
return runtimeType == other.runtimeType &&
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2022, the Chromium project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:cloud_firestore_platform_interface/src/method_channel/method_channel_query_snapshot.dart';
import 'package:cloud_firestore_platform_interface/src/method_channel/utils/exception.dart';

import '../../cloud_firestore_platform_interface.dart';
import 'method_channel_firestore.dart';

/// An implementation of [VectorQueryPlatform] for the [MethodChannel]
class MethodChannelVectorQuery extends VectorQueryPlatform {
MethodChannelVectorQuery(
this.firestore,
query,
this._pigeonParameters,
this._path,
this._pigeonApp,
this._queryVector,
this._limit,
this._distanceMeasure,
this._options,
this._isCollectionGroupQuery,
) : super(query);

final FirebaseFirestorePlatform firestore;
final FirestorePigeonFirebaseApp _pigeonApp;
final String _path;
final PigeonQueryParameters _pigeonParameters;
final bool _isCollectionGroupQuery;

final int _limit;
final DistanceMeasure _distanceMeasure;
final List<double> _queryVector;
final VectorQueryOptions _options;

@override
Future<MethodChannelQuerySnapshot> get({
required VectorSource source,
}) async {
try {
final PigeonQuerySnapshot result =
await MethodChannelFirebaseFirestore.pigeonChannel.findNearest(
_pigeonApp,
_path,
_isCollectionGroupQuery,
_pigeonParameters,
_queryVector,
source,
_limit,
_options,
_distanceMeasure,
);

return MethodChannelQuerySnapshot(firestore, result);
} catch (e, stack) {
convertPlatformException(e, stack);
}
}
}
Loading

0 comments on commit e9a3202

Please sign in to comment.