|
17 | 17 |
|
18 | 18 | import { User } from '../auth/user';
|
19 | 19 | import { Target } from '../core/target';
|
| 20 | +import { FirestoreIndexValueWriter } from '../index/firestore_index_value_writer'; |
| 21 | +import { IndexByteEncoder } from '../index/index_byte_encoder'; |
| 22 | +import { IndexEntry, indexEntryComparator } from '../index/index_entry'; |
20 | 23 | import {
|
21 | 24 | documentKeySet,
|
22 | 25 | DocumentKeySet,
|
23 | 26 | DocumentMap
|
24 | 27 | } from '../model/collections';
|
25 |
| -import { FieldIndex, IndexOffset } from '../model/field_index'; |
| 28 | +import { Document } from '../model/document'; |
| 29 | +import { DocumentKey } from '../model/document_key'; |
| 30 | +import { |
| 31 | + FieldIndex, |
| 32 | + fieldIndexGetArraySegment, |
| 33 | + fieldIndexGetDirectionalSegments, |
| 34 | + IndexKind, |
| 35 | + IndexOffset |
| 36 | +} from '../model/field_index'; |
26 | 37 | import { ResourcePath } from '../model/path';
|
| 38 | +import { isArray } from '../model/values'; |
| 39 | +import { Value as ProtoValue } from '../protos/firestore_proto_api'; |
27 | 40 | import { debugAssert } from '../util/assert';
|
| 41 | +import { logDebug } from '../util/log'; |
28 | 42 | import { immediateSuccessor } from '../util/misc';
|
| 43 | +import { diffSortedSets, SortedSet } from '../util/sorted_set'; |
29 | 44 |
|
30 | 45 | import {
|
31 | 46 | decodeResourcePath,
|
@@ -53,6 +68,8 @@ import { PersistencePromise } from './persistence_promise';
|
53 | 68 | import { PersistenceTransaction } from './persistence_transaction';
|
54 | 69 | import { SimpleDbStore } from './simple_db';
|
55 | 70 |
|
| 71 | +const LOG_TAG = 'IndexedDbIndexManager'; |
| 72 | + |
56 | 73 | /**
|
57 | 74 | * A persisted implementation of IndexManager.
|
58 | 75 | *
|
@@ -194,6 +211,40 @@ export class IndexedDbIndexManager implements IndexManager {
|
194 | 211 | return PersistencePromise.resolve<FieldIndex | null>(null);
|
195 | 212 | }
|
196 | 213 |
|
| 214 | + /** |
| 215 | + * Returns the byte encoded form of the directional values in the field index. |
| 216 | + * Returns `null` if the document does not have all fields specified in the |
| 217 | + * index. |
| 218 | + */ |
| 219 | + private encodeDirectionalElements( |
| 220 | + fieldIndex: FieldIndex, |
| 221 | + document: Document |
| 222 | + ): Uint8Array | null { |
| 223 | + const encoder = new IndexByteEncoder(); |
| 224 | + for (const segment of fieldIndexGetDirectionalSegments(fieldIndex)) { |
| 225 | + const field = document.data.field(segment.fieldPath); |
| 226 | + if (field == null) { |
| 227 | + return null; |
| 228 | + } |
| 229 | + const directionalEncoder = encoder.forKind(segment.kind); |
| 230 | + FirestoreIndexValueWriter.INSTANCE.writeIndexValue( |
| 231 | + field, |
| 232 | + directionalEncoder |
| 233 | + ); |
| 234 | + } |
| 235 | + return encoder.encodedBytes(); |
| 236 | + } |
| 237 | + |
| 238 | + /** Encodes a single value to the ascending index format. */ |
| 239 | + private encodeSingleElement(value: ProtoValue): Uint8Array { |
| 240 | + const encoder = new IndexByteEncoder(); |
| 241 | + FirestoreIndexValueWriter.INSTANCE.writeIndexValue( |
| 242 | + value, |
| 243 | + encoder.forKind(IndexKind.ASCENDING) |
| 244 | + ); |
| 245 | + return encoder.encodedBytes(); |
| 246 | + } |
| 247 | + |
197 | 248 | getFieldIndexes(
|
198 | 249 | transaction: PersistenceTransaction,
|
199 | 250 | collectionGroup?: string
|
@@ -269,8 +320,181 @@ export class IndexedDbIndexManager implements IndexManager {
|
269 | 320 | transaction: PersistenceTransaction,
|
270 | 321 | documents: DocumentMap
|
271 | 322 | ): PersistencePromise<void> {
|
272 |
| - // TODO(indexing): Implement |
273 |
| - return PersistencePromise.resolve(); |
| 323 | + // Porting Note: `getFieldIndexes()` on Web does not cache index lookups as |
| 324 | + // it could be used across different IndexedDB transactions. As any cached |
| 325 | + // data might be invalidated by other multi-tab clients, we can only trust |
| 326 | + // data within a single IndexedDB transaction. We therefore add a cache |
| 327 | + // here. |
| 328 | + const memoizedIndexes = new Map<string, FieldIndex[]>(); |
| 329 | + return PersistencePromise.forEach(documents, (key, doc) => { |
| 330 | + const memoizedCollectionIndexes = memoizedIndexes.get( |
| 331 | + key.collectionGroup |
| 332 | + ); |
| 333 | + const fieldIndexes = memoizedCollectionIndexes |
| 334 | + ? PersistencePromise.resolve(memoizedCollectionIndexes) |
| 335 | + : this.getFieldIndexes(transaction, key.collectionGroup); |
| 336 | + |
| 337 | + return fieldIndexes.next(fieldIndexes => { |
| 338 | + memoizedIndexes.set(key.collectionGroup, fieldIndexes); |
| 339 | + return PersistencePromise.forEach( |
| 340 | + fieldIndexes, |
| 341 | + (fieldIndex: FieldIndex) => { |
| 342 | + return this.getExistingIndexEntries( |
| 343 | + transaction, |
| 344 | + key, |
| 345 | + fieldIndex |
| 346 | + ).next(existingEntries => { |
| 347 | + const newEntries = this.computeIndexEntries(doc, fieldIndex); |
| 348 | + if (!existingEntries.isEqual(newEntries)) { |
| 349 | + return this.updateEntries( |
| 350 | + transaction, |
| 351 | + doc, |
| 352 | + existingEntries, |
| 353 | + newEntries |
| 354 | + ); |
| 355 | + } |
| 356 | + return PersistencePromise.resolve(); |
| 357 | + }); |
| 358 | + } |
| 359 | + ); |
| 360 | + }); |
| 361 | + }); |
| 362 | + } |
| 363 | + |
| 364 | + private addIndexEntry( |
| 365 | + transaction: PersistenceTransaction, |
| 366 | + document: Document, |
| 367 | + indexEntry: IndexEntry |
| 368 | + ): PersistencePromise<void> { |
| 369 | + const indexEntries = indexEntriesStore(transaction); |
| 370 | + return indexEntries.put( |
| 371 | + new DbIndexEntry( |
| 372 | + indexEntry.indexId, |
| 373 | + this.uid, |
| 374 | + indexEntry.arrayValue, |
| 375 | + indexEntry.directionalValue, |
| 376 | + encodeResourcePath(document.key.path) |
| 377 | + ) |
| 378 | + ); |
| 379 | + } |
| 380 | + |
| 381 | + private deleteIndexEntry( |
| 382 | + transaction: PersistenceTransaction, |
| 383 | + document: Document, |
| 384 | + indexEntry: IndexEntry |
| 385 | + ): PersistencePromise<void> { |
| 386 | + const indexEntries = indexEntriesStore(transaction); |
| 387 | + return indexEntries.delete([ |
| 388 | + indexEntry.indexId, |
| 389 | + this.uid, |
| 390 | + indexEntry.arrayValue, |
| 391 | + indexEntry.directionalValue, |
| 392 | + encodeResourcePath(document.key.path) |
| 393 | + ]); |
| 394 | + } |
| 395 | + |
| 396 | + private getExistingIndexEntries( |
| 397 | + transaction: PersistenceTransaction, |
| 398 | + documentKey: DocumentKey, |
| 399 | + fieldIndex: FieldIndex |
| 400 | + ): PersistencePromise<SortedSet<IndexEntry>> { |
| 401 | + const indexEntries = indexEntriesStore(transaction); |
| 402 | + let results = new SortedSet<IndexEntry>(indexEntryComparator); |
| 403 | + return indexEntries |
| 404 | + .iterate( |
| 405 | + { |
| 406 | + index: DbIndexEntry.documentKeyIndex, |
| 407 | + range: IDBKeyRange.only([ |
| 408 | + fieldIndex.indexId, |
| 409 | + this.uid, |
| 410 | + encodeResourcePath(documentKey.path) |
| 411 | + ]) |
| 412 | + }, |
| 413 | + (_, entry) => { |
| 414 | + results = results.add( |
| 415 | + new IndexEntry( |
| 416 | + fieldIndex.indexId, |
| 417 | + documentKey, |
| 418 | + entry.arrayValue, |
| 419 | + entry.directionalValue |
| 420 | + ) |
| 421 | + ); |
| 422 | + } |
| 423 | + ) |
| 424 | + .next(() => results); |
| 425 | + } |
| 426 | + |
| 427 | + /** Creates the index entries for the given document. */ |
| 428 | + private computeIndexEntries( |
| 429 | + document: Document, |
| 430 | + fieldIndex: FieldIndex |
| 431 | + ): SortedSet<IndexEntry> { |
| 432 | + let results = new SortedSet<IndexEntry>(indexEntryComparator); |
| 433 | + |
| 434 | + const directionalValue = this.encodeDirectionalElements( |
| 435 | + fieldIndex, |
| 436 | + document |
| 437 | + ); |
| 438 | + if (directionalValue == null) { |
| 439 | + return results; |
| 440 | + } |
| 441 | + |
| 442 | + const arraySegment = fieldIndexGetArraySegment(fieldIndex); |
| 443 | + if (arraySegment != null) { |
| 444 | + const value = document.data.field(arraySegment.fieldPath); |
| 445 | + if (isArray(value)) { |
| 446 | + for (const arrayValue of value.arrayValue!.values || []) { |
| 447 | + results = results.add( |
| 448 | + new IndexEntry( |
| 449 | + fieldIndex.indexId, |
| 450 | + document.key, |
| 451 | + this.encodeSingleElement(arrayValue), |
| 452 | + directionalValue |
| 453 | + ) |
| 454 | + ); |
| 455 | + } |
| 456 | + } |
| 457 | + } else { |
| 458 | + results = results.add( |
| 459 | + new IndexEntry( |
| 460 | + fieldIndex.indexId, |
| 461 | + document.key, |
| 462 | + new Uint8Array(), |
| 463 | + directionalValue |
| 464 | + ) |
| 465 | + ); |
| 466 | + } |
| 467 | + |
| 468 | + return results; |
| 469 | + } |
| 470 | + |
| 471 | + /** |
| 472 | + * Updates the index entries for the provided document by deleting entries |
| 473 | + * that are no longer referenced in `newEntries` and adding all newly added |
| 474 | + * entries. |
| 475 | + */ |
| 476 | + private updateEntries( |
| 477 | + transaction: PersistenceTransaction, |
| 478 | + document: Document, |
| 479 | + existingEntries: SortedSet<IndexEntry>, |
| 480 | + newEntries: SortedSet<IndexEntry> |
| 481 | + ): PersistencePromise<void> { |
| 482 | + logDebug(LOG_TAG, "Updating index entries for document '%s'", document.key); |
| 483 | + |
| 484 | + const promises: Array<PersistencePromise<void>> = []; |
| 485 | + diffSortedSets( |
| 486 | + existingEntries, |
| 487 | + newEntries, |
| 488 | + indexEntryComparator, |
| 489 | + /* onAdd= */ entry => { |
| 490 | + promises.push(this.addIndexEntry(transaction, document, entry)); |
| 491 | + }, |
| 492 | + /* onRemove= */ entry => { |
| 493 | + promises.push(this.deleteIndexEntry(transaction, document, entry)); |
| 494 | + } |
| 495 | + ); |
| 496 | + |
| 497 | + return PersistencePromise.waitFor(promises); |
274 | 498 | }
|
275 | 499 |
|
276 | 500 | private getNextSequenceNumber(
|
|
0 commit comments