summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/db/gloda/test/unit/test_query_core.js
blob: 0849a62d507366c8278d9aea6671c40f347350b1 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
/* 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/. */

/*
 * Test the mechanics our query functionality.  Tests in this file are intended
 *  to cover extreme boundary cases and things that are just unlikely to happen
 *  in reasonable message use-cases.  (Which is to say, it could be hard to
 *  formulate a set of synthetic messages that result in the situation we want
 *  to test for.)
 */

var { prepareIndexerForTesting } = ChromeUtils.import(
  "resource://testing-common/gloda/GlodaTestHelper.jsm"
);
var { queryExpect } = ChromeUtils.import(
  "resource://testing-common/gloda/GlodaQueryHelper.jsm"
);
var { Gloda } = ChromeUtils.import("resource:///modules/gloda/GlodaPublic.jsm");
var { GlodaConstants } = ChromeUtils.import(
  "resource:///modules/gloda/GlodaConstants.jsm"
);
var { GlodaIndexer, IndexingJob } = ChromeUtils.import(
  "resource:///modules/gloda/GlodaIndexer.jsm"
);

/* ===== Test Noun ===== */
/*
 * Introduce a simple noun type for our testing so that we can avoid having to
 * deal with the semantics of messages/friends and all their complexity.
 */

var WidgetProvider = {
  providerName: "widget",
  *process() {
    yield GlodaConstants.kWorkDone;
  },
};

add_setup(function () {
  // Don't initialize the index message state
  prepareIndexerForTesting();
  GlodaIndexer.registerIndexer(GenericIndexer);
  Gloda.addIndexerListener(genericIndexerCallback);
});

var WidgetNoun;
add_task(function setup_test_noun_and_attributes() {
  // --- noun
  WidgetNoun = Gloda.defineNoun({
    name: "widget",
    clazz: Widget,
    allowsArbitraryAttrs: true,
    // It is vitally important to our correctness that we allow caching
    //  otherwise our in-memory representations will not be canonical and the db
    //  will load some.  Or we could add things to collections as we index them.
    cache: true,
    cacheCost: 32,
    schema: {
      columns: [
        ["id", "INTEGER PRIMARY KEY"],
        ["intCol", "NUMBER", "inum"],
        // datePRTime is special and creates a Date object.
        ["dateCol", "NUMBER", "datePRTime"],
        ["strCol", "STRING", "str"],
        ["notabilityCol", "NUMBER", "notability"],
        ["textOne", "STRING", "text1"],
        ["textTwo", "STRING", "text2"],
      ],
      indices: {
        intCol: ["intCol"],
        strCol: ["strCol"],
      },
      fulltextColumns: [
        ["fulltextOne", "TEXT", "text1"],
        ["fulltextTwo", "TEXT", "text2"],
      ],
      genericAttributes: true,
    },
  });

  const EXT_NAME = "test";

  // --- special (on-row) attributes
  Gloda.defineAttribute({
    provider: WidgetProvider,
    extensionName: EXT_NAME,
    attributeType: GlodaConstants.kAttrFundamental,
    attributeName: "inum",
    singular: true,
    special: GlodaConstants.kSpecialColumn,
    specialColumnName: "intCol",
    subjectNouns: [WidgetNoun.id],
    objectNoun: GlodaConstants.NOUN_NUMBER,
    canQuery: true,
  });
  Gloda.defineAttribute({
    provider: WidgetProvider,
    extensionName: EXT_NAME,
    attributeType: GlodaConstants.kAttrFundamental,
    attributeName: "date",
    singular: true,
    special: GlodaConstants.kSpecialColumn,
    specialColumnName: "dateCol",
    subjectNouns: [WidgetNoun.id],
    objectNoun: GlodaConstants.NOUN_DATE,
    canQuery: true,
  });
  Gloda.defineAttribute({
    provider: WidgetProvider,
    extensionName: EXT_NAME,
    attributeType: GlodaConstants.kAttrFundamental,
    attributeName: "str",
    singular: true,
    special: GlodaConstants.kSpecialString,
    specialColumnName: "strCol",
    subjectNouns: [WidgetNoun.id],
    objectNoun: GlodaConstants.NOUN_STRING,
    canQuery: true,
  });

  // --- fulltext attributes
  Gloda.defineAttribute({
    provider: WidgetProvider,
    extensionName: EXT_NAME,
    attributeType: GlodaConstants.kAttrFundamental,
    attributeName: "text1",
    singular: true,
    special: GlodaConstants.kSpecialFulltext,
    specialColumnName: "fulltextOne",
    subjectNouns: [WidgetNoun.id],
    objectNoun: GlodaConstants.NOUN_FULLTEXT,
    canQuery: true,
  });
  Gloda.defineAttribute({
    provider: WidgetProvider,
    extensionName: EXT_NAME,
    attributeType: GlodaConstants.kAttrFundamental,
    attributeName: "text2",
    singular: true,
    special: GlodaConstants.kSpecialFulltext,
    specialColumnName: "fulltextTwo",
    subjectNouns: [WidgetNoun.id],
    objectNoun: GlodaConstants.NOUN_FULLTEXT,
    canQuery: true,
  });
  Gloda.defineAttribute({
    provider: WidgetProvider,
    extensionName: EXT_NAME,
    attributeType: GlodaConstants.kAttrFundamental,
    attributeName: "fulltextAll",
    singular: true,
    special: GlodaConstants.kSpecialFulltext,
    specialColumnName: WidgetNoun.tableName + "Text",
    subjectNouns: [WidgetNoun.id],
    objectNoun: GlodaConstants.NOUN_FULLTEXT,
    canQuery: true,
  });

  // --- external (attribute-storage) attributes
  Gloda.defineAttribute({
    provider: WidgetProvider,
    extensionName: EXT_NAME,
    attributeType: GlodaConstants.kAttrFundamental,
    attributeName: "singleIntAttr",
    singular: true,
    subjectNouns: [WidgetNoun.id],
    objectNoun: GlodaConstants.NOUN_NUMBER,
    canQuery: true,
  });

  Gloda.defineAttribute({
    provider: WidgetProvider,
    extensionName: EXT_NAME,
    attributeType: GlodaConstants.kAttrFundamental,
    attributeName: "multiIntAttr",
    singular: false,
    emptySetIsSignificant: true,
    subjectNouns: [WidgetNoun.id],
    objectNoun: GlodaConstants.NOUN_NUMBER,
    canQuery: true,
  });
});

/* ===== Tests ===== */

const ALPHABET = "abcdefghijklmnopqrstuvwxyz";
add_task(async function test_lots_of_string_constraints() {
  let stringConstraints = [];
  for (let i = 0; i < 2049; i++) {
    stringConstraints.push(
      ALPHABET[Math.floor(i / (ALPHABET.length * 2)) % ALPHABET.length] +
        ALPHABET[Math.floor(i / ALPHABET.length) % ALPHABET.length] +
        ALPHABET[i % ALPHABET.length] +
        // Throw in something that will explode if not quoted
        // and use an uneven number of things so if we fail
        // to quote it won't get quietly eaten.
        "'\""
    );
  }

  let query = Gloda.newQuery(WidgetNoun.id);
  query.str.apply(query, stringConstraints);

  await queryExpect(query, []);
});

/* === Query === */

/**
 * Use a counter so that each test can have its own unique value for intCol so
 *  that it can use that as a constraint.  Otherwise we would need to purge
 *  between every test.  That's not an unreasonable alternative, but this works.
 * Every test should increment this before using it.
 */
var testUnique = 100;

/**
 * Widgets with multiIntAttr populated with one or more values.
 */
var nonSingularWidgets;
/**
 * Widgets with multiIntAttr unpopulated.
 */
var singularWidgets;

add_task(async function setup_non_singular_values() {
  testUnique++;
  let origin = new Date("2007/01/01");
  nonSingularWidgets = [
    new Widget(testUnique, origin, "ns1", 0, "", ""),
    new Widget(testUnique, origin, "ns2", 0, "", ""),
  ];
  singularWidgets = [
    new Widget(testUnique, origin, "s1", 0, "", ""),
    new Widget(testUnique, origin, "s2", 0, "", ""),
  ];
  nonSingularWidgets[0].multiIntAttr = [1, 2];
  nonSingularWidgets[1].multiIntAttr = [3];
  singularWidgets[0].multiIntAttr = [];
  // And don't bother setting it on singularWidgets[1].

  GenericIndexer.indexObjects(nonSingularWidgets.concat(singularWidgets));
  await promiseGenericIndexerCallback;

  // Reset promise.
  promiseGenericIndexerCallback = new Promise(resolve => {
    promiseGenericIndexerCallbackResolve = resolve;
  });
});

add_task(async function test_query_has_value_for_non_singular() {
  let query = Gloda.newQuery(WidgetNoun.id);
  query.inum(testUnique);
  query.multiIntAttr();
  await queryExpect(query, nonSingularWidgets);
});

/**
 * We should find the one singular object where we set the multiIntAttr to an
 *  empty set.  We don't find the one without the attribute since that's
 *  actually something different.
 * We also want to test that re-indexing properly adds/removes the attribute
 *  so change the object and make sure everything happens correctly.
 *
 * @tests gloda.datastore.sqlgen.kConstraintIn.emptySet
 * @tests gloda.query.test.kConstraintIn.emptySet
 */
add_task(async function test_empty_set_logic() {
  // - Initial query based on the setup previously.
  dump("Initial index case\n");
  let query = Gloda.newQuery(WidgetNoun.id);
  query.inum(testUnique);
  query.multiIntAttr(null);
  await queryExpect(query, [singularWidgets[0]]);

  // - Make one of the non-singulars move to empty and move the guy who matched
  //  to no longer match.
  dump("Incremental index case\n");
  nonSingularWidgets[0].multiIntAttr = [];
  singularWidgets[0].multiIntAttr = [4, 5];

  GenericIndexer.indexObjects([nonSingularWidgets[0], singularWidgets[0]]);
  await promiseGenericIndexerCallback;

  // Reset promise;
  promiseGenericIndexerCallback = new Promise(resolve => {
    promiseGenericIndexerCallbackResolve = resolve;
  });

  query = Gloda.newQuery(WidgetNoun.id);
  query.inum(testUnique);
  query.multiIntAttr(null);
  await queryExpect(query, [nonSingularWidgets[0]]);

  // Make sure that the query doesn't explode when it has to handle a case
  //  that's not supposed to match.
  Assert.ok(!query.test(singularWidgets[0]));
});

/* === Search === */
/*
 * The conceit of our search is that more recent messages are better than older
 *  messages.  But at the same time, we care about some messages more than
 *  others (in general), and we care about messages that match search terms
 *  more strongly too.  So we introduce a general 'score' heuristic which we
 *  then apply to message timestamps to make them appear more recent.  We
 *  then order by this 'date score' hybrid, which we dub "dascore".  Such a
 *  flattening heuristic is over-simple, but believed to be sufficient to
 *  generally get us the messages we want.  Post-processing based can then
 *  be more multi-dimensional and what not, but that is beyond the scope of
 *  this unit test.
 */

/**
 * How much time boost should a 'score point' amount to?  The authoritative,
 *  incontrivertible answer, across all time and space, is a week.
 *  Gloda and storage like to store things as PRTime and so we do it too,
 *  even though milliseconds are the actual granularity of JS Date instances.
 */
const SCORE_TIMESTAMP_FACTOR = 1000 * 1000 * 60 * 60 * 24 * 7;

/**
 * How many score points for each fulltext match?
 */
const SCORE_FOR_FULLTEXT_MATCH = 1;

/**
 * Roughly how many characters are in each offset match.
 */
const OFFSET_CHARS_PER_FULLTEXT_MATCH = 8;

var fooWidgets = null;
var barBazWidgets = null;

add_task(async function setup_search_ranking_idiom() {
  // --- Build some widgets for testing.
  // Use inum to represent the expected result sequence
  // Setup a base date.
  let origin = new Date("2008/01/01");
  let daymore = new Date("2008/01/02");
  let monthmore = new Date("2008/02/01");
  fooWidgets = [
    // -- Setup the term "foo" to do frequency tests.
    new Widget(5, origin, "", 0, "", "foo"),
    new Widget(4, origin, "", 0, "", "foo foo"),
    new Widget(3, origin, "", 0, "foo", "foo foo"),
    new Widget(2, origin, "", 0, "foo foo", "foo foo"),
    new Widget(1, origin, "", 0, "foo foo", "foo foo foo"),
    new Widget(0, origin, "", 0, "foo foo foo", "foo foo foo"),
  ];
  barBazWidgets = [
    // -- Setup score and matches to boost older messages over newer messages.
    new Widget(7, origin, "", 0, "", "bar"), // score boost: 1 + date: 0
    new Widget(6, daymore, "", 0, "", "bar"), // 1 + 0+
    new Widget(5, origin, "", 1, "", "bar"), // 2 + 0
    new Widget(4, daymore, "", 0, "bar", "bar"), // 2 + 0+
    new Widget(3, origin, "", 1, "bar", "baz"), // 3 + 0
    new Widget(2, monthmore, "", 0, "", "bar"), // 1 + 4
    new Widget(1, origin, "", 0, "bar baz", "bar baz bar bar"), // 6 + 0
    new Widget(0, origin, "", 1, "bar baz", "bar baz bar bar"), // 7 + 0
  ];

  GenericIndexer.indexObjects(fooWidgets.concat(barBazWidgets));
  await promiseGenericIndexerCallback;

  // Reset promise.
  promiseGenericIndexerCallback = new Promise(resolve => {
    promiseGenericIndexerCallbackResolve = resolve;
  });
});

// Add one because the last snippet shouldn't have a trailing space.
const OFFSET_SCORE_SQL_SNIPPET =
  "(((length(osets) + 1) / " +
  OFFSET_CHARS_PER_FULLTEXT_MATCH +
  ") * " +
  SCORE_FOR_FULLTEXT_MATCH +
  ")";

const SCORE_SQL_SNIPPET = "(" + OFFSET_SCORE_SQL_SNIPPET + " + notabilityCol)";

const DASCORE_SQL_SNIPPET =
  "((" + SCORE_SQL_SNIPPET + " * " + SCORE_TIMESTAMP_FACTOR + ") + dateCol)";

const WIDGET_FULLTEXT_QUERY_EXPLICIT_SQL =
  "SELECT ext_widget.*, offsets(ext_widgetText) AS osets " +
  "FROM ext_widget, ext_widgetText WHERE ext_widgetText MATCH ?" +
  " AND ext_widget.id == ext_widgetText.docid";

/**
 * Used by queryExpect to verify
 */
function verify_widget_order_and_stashing(
  aZeroBasedIndex,
  aWidget,
  aCollection
) {
  Assert.equal(aZeroBasedIndex, aWidget.inum);
  if (
    !aCollection.stashedColumns[aWidget.id] ||
    !aCollection.stashedColumns[aWidget.id].length
  ) {
    do_throw("no stashed information for widget: " + aWidget);
  }
}

/**
 * Test the fundamentals of the search ranking idiom we use elsewhere.  This
 *  is primarily a simplified
 */
add_task(async function test_search_ranking_idiom_offsets() {
  let query = Gloda.newQuery(WidgetNoun.id, {
    explicitSQL: WIDGET_FULLTEXT_QUERY_EXPLICIT_SQL,
    // osets becomes 0-based column number 7.
    // dascore becomes 0-based column number 8.
    outerWrapColumns: [DASCORE_SQL_SNIPPET + " AS dascore"],
    // Save our extra columns for analysis and debugging.
    stashColumns: [7, 8],
  });
  query.fulltextAll("foo");
  query.orderBy("-dascore");
  await queryExpect(
    query,
    fooWidgets,
    null,
    null,
    verify_widget_order_and_stashing
  );
});

add_task(async function test_search_ranking_idiom_score() {
  let query = Gloda.newQuery(WidgetNoun.id, {
    explicitSQL: WIDGET_FULLTEXT_QUERY_EXPLICIT_SQL,
    // osets becomes 0-based column number 7
    // dascore becomes 0-based column number 8
    outerWrapColumns: [
      DASCORE_SQL_SNIPPET + " AS dascore",
      SCORE_SQL_SNIPPET + " AS dabore",
      "dateCol",
    ],
    // Save our extra columns for analysis and debugging.
    stashColumns: [7, 8, 9, 10],
  });
  query.fulltextAll("bar OR baz");
  query.orderBy("-dascore");
  await queryExpect(
    query,
    barBazWidgets,
    null,
    null,
    verify_widget_order_and_stashing
  );
});

/**
 * Generic indexing mechanism; does nothing special, just uses
 *  Gloda.grokNounItem.  Call GenericIndexer.indexNewObjects() to queue
 *  queue your objects for initial indexing.
 */
var GenericIndexer = {
  _log: console.createInstance({
    prefix: "gloda.test",
    maxLogLevel: "Warn",
    maxLogLevelPref: "gloda.test.loglevel",
  }),
  /* public interface */
  name: "generic_indexer",
  enable() {
    this.enabled = true;
  },
  disable() {
    this.enabled = false;
  },
  get workers() {
    return [
      [
        "generic",
        {
          worker: this._worker_index_generic,
        },
      ],
    ];
  },
  initialSweep() {},
  /* mock interface */
  enabled: false,
  initialSweepCalled: false,
  indexObjects(aObjects) {
    indexingInProgress = true;
    this._log.debug(
      "enqueuing " +
        aObjects.length +
        " generic objects with id: " +
        aObjects[0].NOUN_ID
    );
    GlodaIndexer.indexJob(new IndexingJob("generic", null, aObjects.concat()));
  },
  /* implementation */
  *_worker_index_generic(aJob, aCallbackHandle) {
    this._log.debug(
      "Beginning indexing " + aJob.items.length + " generic items"
    );
    for (let item of aJob.items) {
      this._log.debug("Indexing: " + item);
      yield aCallbackHandle.pushAndGo(
        Gloda.grokNounItem(
          item,
          {},
          item.id === undefined,
          item.id === undefined,
          aCallbackHandle,
          item.NOUN_DEF.cache
        )
      );
      item._stash();
    }

    yield GlodaConstants.kWorkDone;
    this._log.debug("Done indexing");
  },
};

var indexingInProgress = false;
var promiseGenericIndexerCallbackResolve;
var promiseGenericIndexerCallback = new Promise(resolve => {
  promiseGenericIndexerCallbackResolve = resolve;
});
function genericIndexerCallback(aStatus) {
  // If indexingInProgress is false, we've received the synthetic
  // notification, so ignore it.
  if (indexingInProgress && aStatus == GlodaConstants.kIndexerIdle) {
    indexingInProgress = false;
    promiseGenericIndexerCallbackResolve();
  }
}

/**
 * Simple test object.
 *
 * Has some tricks for gloda indexing to deal with gloda's general belief that
 *  things are immutable.  When we get indexed we stash all of our attributes
 *  at that time in _indexStash.  Then when we get cloned we propagate our
 *  current attributes over to the cloned object and restore _indexStash.  This
 *  sets things up the way gloda expects them as long as we never de-persist
 *  from the db.
 */
function Widget(inum, date, str, notability, text1, text2) {
  this._id = undefined;
  this._inum = inum;
  this._date = date;
  this._str = str;
  this._notability = notability;
  this._text1 = text1;
  this._text2 = text2;

  this._indexStash = null;
  this._restoreStash = null;
}
Widget.prototype = {
  _clone() {
    let clonus = new Widget(
      this._inum,
      this._date,
      this._str,
      this._notability,
      this._text1,
      this._text2
    );
    clonus._id = this._id;
    clonus._iAmAClone = true;

    for (let key of Object.keys(this)) {
      let value = this[key];
      if (key.startsWith("_")) {
        continue;
      }
      clonus[key] = value;
      if (key in this._indexStash) {
        this[key] = this._indexStash[key];
      }
    }

    return clonus;
  },
  _stash() {
    this._indexStash = {};
    for (let key of Object.keys(this)) {
      let value = this[key];
      if (key[0].startsWith("_")) {
        continue;
      }
      this._indexStash[key] = value;
    }
  },

  get id() {
    return this._id;
  },
  set id(aVal) {
    this._id = aVal;
  },

  // Gloda's attribute idiom demands that row attributes be prefixed with a '_'
  //  (Because Gloda.grokNounItem detects attributes by just walking.).  This
  //  could be resolved by having the special attributes moot these dudes, but
  //  that's not how things are right now.
  get inum() {
    return this._inum;
  },
  set inum(aVal) {
    this._inum = aVal;
  },
  get date() {
    return this._date;
  },
  set date(aVal) {
    this._date = aVal;
  },

  get datePRTime() {
    return this._date.valueOf() * 1000;
  },
  // We need a special setter to convert back from PRTime to an actual
  //  date object.
  set datePRTime(aVal) {
    this._date = new Date(aVal / 1000);
  },

  get str() {
    return this._str;
  },
  set str(aVal) {
    this._str = aVal;
  },
  get notability() {
    return this._notability;
  },
  set notability(aVal) {
    this._notability = aVal;
  },
  get text1() {
    return this._text1;
  },
  set text1(aVal) {
    this._text1 = aVal;
  },
  get text2() {
    return this._text2;
  },
  set text2(aVal) {
    this._text2 = aVal;
  },

  toString() {
    return "" + this.id;
  },
};