summaryrefslogtreecommitdiffstats
path: root/mailnews/extensions/fts3/src/nsFts3Tokenizer.cpp
blob: 12bd70ead1146f8c6be3fd77c71bd245eae4f17c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "nsFts3Tokenizer.h"

#include "nsGlodaRankerFunction.h"

#include "nsIFts3Tokenizer.h"
#include "mozIStorageConnection.h"
#include "mozIStorageStatement.h"
#include "nsStringGlue.h"

extern "C" void sqlite3Fts3PorterTokenizerModule(
  sqlite3_tokenizer_module const**ppModule);

extern "C" void glodaRankFunc(sqlite3_context *pCtx,
                              int nVal,
                              sqlite3_value **apVal);

NS_IMPL_ISUPPORTS(nsFts3Tokenizer,nsIFts3Tokenizer)

nsFts3Tokenizer::nsFts3Tokenizer()
{
}

nsFts3Tokenizer::~nsFts3Tokenizer()
{
}

NS_IMETHODIMP
nsFts3Tokenizer::RegisterTokenizer(mozIStorageConnection *connection)
{
  nsresult rv;
  nsCOMPtr<mozIStorageStatement> selectStatement;

  // -- register the tokenizer
  rv = connection->CreateStatement(NS_LITERAL_CSTRING(
    "SELECT fts3_tokenizer(?1, ?2)"),
    getter_AddRefs(selectStatement));
  NS_ENSURE_SUCCESS(rv, rv);

  const sqlite3_tokenizer_module* module = nullptr;
  sqlite3Fts3PorterTokenizerModule(&module);
  if (!module)
    return NS_ERROR_FAILURE;

  rv = selectStatement->BindUTF8StringParameter(
         0, NS_LITERAL_CSTRING("mozporter"));
  NS_ENSURE_SUCCESS(rv, rv);
  rv = selectStatement->BindBlobParameter(1,
                                          (uint8_t*)&module,
                                          sizeof(module));
  NS_ENSURE_SUCCESS(rv, rv);

  bool hasMore;
  rv = selectStatement->ExecuteStep(&hasMore);
  NS_ENSURE_SUCCESS(rv, rv);

  // -- register the ranking function
  nsCOMPtr<mozIStorageFunction> func = new nsGlodaRankerFunction();
  NS_ENSURE_TRUE(func, NS_ERROR_OUT_OF_MEMORY);
  rv = connection->CreateFunction(
         NS_LITERAL_CSTRING("glodaRank"),
         -1, // variable argument support
         func
       );
  NS_ENSURE_SUCCESS(rv, rv);

  return rv;
}