1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 #include "nsFts3Tokenizer.h"
7
8 #include "nsGlodaRankerFunction.h"
9
10 #include "nsIFts3Tokenizer.h"
11 #include "mozIStorageConnection.h"
12 #include "mozIStorageStatement.h"
13 #include "nsString.h"
14
15 extern "C" void sqlite3Fts3PorterTokenizerModule(
16 sqlite3_tokenizer_module const** ppModule);
17
18 extern "C" void glodaRankFunc(sqlite3_context* pCtx, int nVal,
19 sqlite3_value** apVal);
20
NS_IMPL_ISUPPORTS(nsFts3Tokenizer,nsIFts3Tokenizer)21 NS_IMPL_ISUPPORTS(nsFts3Tokenizer, nsIFts3Tokenizer)
22
23 nsFts3Tokenizer::nsFts3Tokenizer() {}
24
~nsFts3Tokenizer()25 nsFts3Tokenizer::~nsFts3Tokenizer() {}
26
27 NS_IMETHODIMP
RegisterTokenizer(mozIStorageConnection * connection)28 nsFts3Tokenizer::RegisterTokenizer(mozIStorageConnection* connection) {
29 nsresult rv;
30 nsCOMPtr<mozIStorageStatement> selectStatement;
31
32 // -- register the tokenizer
33 rv = connection->CreateStatement("SELECT fts3_tokenizer(?1, ?2)"_ns,
34 getter_AddRefs(selectStatement));
35 NS_ENSURE_SUCCESS(rv, rv);
36
37 const sqlite3_tokenizer_module* module = nullptr;
38 sqlite3Fts3PorterTokenizerModule(&module);
39 if (!module) return NS_ERROR_FAILURE;
40
41 rv = selectStatement->BindUTF8StringByIndex(0, "mozporter"_ns);
42 NS_ENSURE_SUCCESS(rv, rv);
43 rv = selectStatement->BindBlobByIndex(1, (uint8_t*)&module, sizeof(module));
44 NS_ENSURE_SUCCESS(rv, rv);
45
46 bool hasMore;
47 rv = selectStatement->ExecuteStep(&hasMore);
48 NS_ENSURE_SUCCESS(rv, rv);
49
50 // -- register the ranking function
51 nsCOMPtr<mozIStorageFunction> func = new nsGlodaRankerFunction();
52 NS_ENSURE_TRUE(func, NS_ERROR_OUT_OF_MEMORY);
53 rv = connection->CreateFunction("glodaRank"_ns,
54 -1, // variable argument support
55 func);
56 NS_ENSURE_SUCCESS(rv, rv);
57
58 return rv;
59 }
60