Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add minimal FTS support #3282

Merged
merged 12 commits into from
May 5, 2023
Merged

Add minimal FTS support #3282

merged 12 commits into from
May 5, 2023

Conversation

nirinchev
Copy link
Member

@nirinchev nirinchev commented Mar 31, 2023

Description

Fixes #3281

Blocked on realm/realm-core#6465

TODO

  • Changelog entry
  • Weaver support
  • FullTextMatch extension method
  • Schema tests
  • Tests

@nirinchev nirinchev changed the title Add schema and SG support for FTS Add minimal FTS support Mar 31, 2023
@coveralls
Copy link

coveralls commented Apr 18, 2023

Pull Request Test Coverage Report for Build 4887743905

  • 45 of 61 (73.77%) changed or added relevant lines in 6 files are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage decreased (-0.04%) to 81.872%

Changes Missing Coverage Covered Lines Changed/Added Lines %
Realm/Realm/Linq/QueryMethods.cs 0 3 0.0%
Realm/Realm/Schema/Property.cs 14 18 77.78%
Realm/Realm/Attributes/IndexedAttribute.cs 0 9 0.0%
Files with Coverage Reduction New Missed Lines %
Realm/Realm/Attributes/IndexedAttribute.cs 1 0%
Totals Coverage Status
Change from base Build 4575523356: -0.04%
Covered Lines: 5904
Relevant Lines: 7082

💛 - Coveralls

@nirinchev nirinchev requested review from papafe and fealebenpae April 18, 2023 15:08
@nirinchev nirinchev marked this pull request as ready for review April 18, 2023 15:08
EnsureIsOpen();

var (primitive, handles) = value.ToNative();
NativeMethods.string_fts(this, realm, propertyIndex, primitive, out var ex);

Check notice

Code scanning / CodeQL

Calls to unmanaged code

Replace this call with a call to managed code if possible.
@@ -61,6 +61,10 @@
public static extern void string_like(QueryHandle queryPtr, SharedRealmHandle realm, IntPtr property_ndx,
PrimitiveValue primitive, [MarshalAs(UnmanagedType.U1)] bool caseSensitive, out NativeException ex);

[DllImport(InteropConfig.DLL_NAME, EntryPoint = "query_string_fts", CallingConvention = CallingConvention.Cdecl)]
public static extern void string_fts(QueryHandle queryPtr, SharedRealmHandle realm, IntPtr property_ndx,

Check notice

Code scanning / CodeQL

Unmanaged code

Minimise the use of unmanaged code.
Copy link
Contributor

@papafe papafe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense!

Realm/Realm.SourceGenerator/InfoClasses.cs Outdated Show resolved Hide resolved
wrappers/src/utf8.hpp Show resolved Hide resolved
namespace Realms
{
#if PRIVATE_INDEXMODE
internal enum IndexMode
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to IndexType and set a base type.

#if TEST_WEAVER
[Required]
#endif
[Indexed(IndexMode.FullText)]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Design question: Doesn't this API prevent further customization of Full-Text? I.e I would assume it is very possible that we would want to be able to configure various attributes on Full-Text once/when we increase its capabilities. I.e. this is the full-text annotation in Android: https://developer.android.com/reference/androidx/room/Fts4

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It kinda does, yes. We could add more arguments to the Indexed attribute that relate only to FTS if we wanted to or we can introduce another attribute in the future. I opted against going the another attribute route right now because we don't have short/medium term plans to extend our FTS support and didn't want to bake too many assumptions about the future of the feature into the initial API. It's also the API we agreed on in the Scope, so figured everyone is on board with it.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The scope mention that the API for other SDKs should be fleshed out in the design doc, which never seemed to happen, so not sure I agree this was the API we wanted to ship with. I read it as just an example of how it could look if we exposed the Core API.

Maybe I'm biased because other frameworks on Android use a dedicated annotation for this, but it seems counterproductive to ship an API that we know for sure we most likely have to break if/when we add more features, even if they are not planned right now. Especially since this API doesn't really provide any meaningful type-safety anyway. Extending the current [Indexed] with more attributes also seems a bit weird as they would be meaningless for the normal index type.

So I would strongly encourage a direction with its own annotation, In Kotlin I exposed it with a @FullText annotation here: https://github.com/realm/realm-kotlin/pull/1368/files#diff-327b30a309e034a752eb56ac993d64f6d291cd9e7af0ddd9018947bf6fddacf2R88

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's nothing preventing us from shipping a new attribute in the future in a backwards compatible way. I'd rather not prematurely optimize for a case when more configuration options may come in before we know what these will look like and if an attribute would be the best way to go about them - e.g. if we want to allow declaring an FTS index over multiple properties, doing so via attributes could end up being clunky and we may choose a different strategy. So rather than trying to design an API for the future, I'd rather extend the existing one we have that has a meaningful name and is well positioned to support our current needs. If our needs change beyond what is currently supported, we'll figure something out, but as I said, I don't envision this happening in the short/medium term - if ever.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't call this a premature optimization, as semantically you are getting the same API, it is just more flexible. While I agree that an FTS index over multiple properties would be slightly awkward using annotations (but not impossible), most modifications like tokenizer strategy, casing, mixing normal and FTS index would all be supported easily by using a dedicated annotation.

internal static readonly LazyMethod ContainsStringComparison = Capture<string>(s => QueryMethods.Contains(s, string.Empty, StringComparison.Ordinal));

#pragma warning disable CS0618 // Type or member is obsolete
internal static readonly LazyMethod LegacyLike = Capture<string>(s => s.Like(string.Empty, true));

Check warning

Code scanning / CodeQL

Call to obsolete method

Call to obsolete method [Like](1).
@@ -59,9 +60,19 @@
{
internal static readonly LazyMethod Contains = Capture<string>(s => s.Contains(string.Empty));

internal static readonly LazyMethod ContainsStringComparison = Capture<string>(s => s.Contains(string.Empty, StringComparison.Ordinal));
#pragma warning disable CS0618 // Type or member is obsolete
internal static readonly LazyMethod InstanceContainsStringComparison = Capture<string>(s => s.Contains(string.Empty, StringComparison.Ordinal));

Check warning

Code scanning / CodeQL

Call to obsolete method

Call to obsolete method [Contains](1).
@nirinchev nirinchev merged commit 0f07286 into main May 5, 2023
@nirinchev nirinchev deleted the ni/fts branch May 5, 2023 18:57
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose Minimal FTS
4 participants