-
Notifications
You must be signed in to change notification settings - Fork 3
/
SPSearchStoreAppDelegate.m
627 lines (442 loc) · 20.3 KB
/
SPSearchStoreAppDelegate.m
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
//
// SPSearchStoreAppDelegate.m
// SPSearchStore
//
// v0.9
//
// Created by Philip Dow on 6/6/11.
// Copyright 2011 Philip Dow /Sprouted. All rights reserved.
// phil@phildow.net / phil@getsprouted.com
//
/*
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions
and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the
distribution.
* Neither the name of the author nor the names of its contributors may be used to endorse or
promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
For non-attribution licensing options refer to http://phildow.net/licensing/
*/
#import "SPSearchStoreAppDelegate.h"
#import "SPSearchStore.h"
@implementation SPSearchStoreAppDelegate
@synthesize window;
@synthesize documentsTable;
@synthesize termsTable;
@synthesize containingTable;
#pragma mark -
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
/* Initializes our data model to display the document <-> term relationships */
fm = [[NSFileManager alloc] init];
documents = [[NSMutableSet alloc] init];
filteredDocuments = [[NSMutableArray alloc] init];
terms = [[NSMutableArray alloc] init];
containing = [[NSMutableArray alloc] init];
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"path" ascending:YES];
descriptors = [[NSArray arrayWithObject:descriptor] retain];
/* Initialize the store */
[self initializeStoreWithData:nil];
// Comment out the above line and uncomment the following lines to test with a
// file-based store. The openDocument and saveDocument methods will not work.
// You should also uncomment the saveChangesToStore call in the
// applicationWillTerminate method beloew.
// NSString *path = [@"~/spsearchstore.index" stringByExpandingTildeInPath];
// NSURL *url = [NSURL fileURLWithPath:path];
// [self initializeStoreWithURL:url];
}
- (void) applicationWillTerminate:(NSNotification *)aNotification {
// Close out the store before your document or application shuts down, whether
// you are in a garbage collected environment or not.
// We would uncomment the following line if we also wanted to guarantee that
// changes to the store have been written to the memory or disk backing.
// [searchStore saveChangesToStore]; // <- call to store
[searchStore closeStore]; //
}
- (void) initializeStoreWithData:(NSMutableData*)inData { // <- call to store
// ...
/* Begin store initialization */
// Initialize the search store. Before we create the search store we specify some
// default text analysis options we would like to use.
[SPSearchStore setDefaultTextAnalysisOption:[SPSearchStore stopWordsForLanguage:@"en"]
forKey:(NSString *)kSKStopWords];
[SPSearchStore setDefaultTextAnalysisOption:[NSNumber numberWithBool:YES]
forKey:(NSString *)kSKProximityIndexing];
[SPSearchStore setDefaultTextAnalysisOption:[NSNumber numberWithInteger:2]
forKey:(NSString *)kSKMinTermLength];
// We are creating an in-memory search store. File based stores are great too.
// If we wanted to persist our in-memory store we could grab the storeData from
// searchStore after adding documents to it and write it out to a file or other store.
// I do exactly this in the saveDocuemnt method below
searchStore = [[SPSearchStore alloc] initStoreWithMemory:inData type:kSKIndexInvertedVector];
if ( searchStore == nil ) NSLog(@"There was a problem creating the search store");
searchStore.usesSpotlightImporters = YES;
searchStore.usesConcurrentIndexing = YES;
searchStore.ignoresNumericTerms = YES;
/* Store initialization is complete at this point */
}
- (void) initializeStoreWithURL:(NSURL*)inFileURL { // <- call to store
// ...
// Refer to above method for an explanation...
[SPSearchStore setDefaultTextAnalysisOption:[SPSearchStore stopWordsForLanguage:@"en"]
forKey:(NSString *)kSKStopWords];
[SPSearchStore setDefaultTextAnalysisOption:[NSNumber numberWithBool:YES]
forKey:(NSString *)kSKProximityIndexing];
[SPSearchStore setDefaultTextAnalysisOption:[NSNumber numberWithInteger:2]
forKey:(NSString *)kSKMinTermLength];
searchStore = [[SPSearchStore alloc] initStoreWithURL:inFileURL type:kSKIndexInvertedVector];
if ( searchStore == nil ) NSLog(@"There was a problem creating the search store");
searchStore.usesSpotlightImporters = YES;
searchStore.usesConcurrentIndexing = YES;
searchStore.ignoresNumericTerms = YES;
if ( searchStore.didCreateStore == NO ) {
[searchStore compactStore:0.25];
// This is a good time to compact the store if necessary, but this function
// will block any other calls to the store. Usually that will not matter;
// it matters to us because we immediately call allDocuments on the store,
// as we do not otherwise keep track of the documents indexed.
[documents removeAllObjects];
[documents addObjectsFromArray:[searchStore allDocuments:YES]];
[self reloadDocumentsTableWithAllDocuments];
}
}
#pragma mark -
- (IBAction) addDocuments:(id)sender {
NSOpenPanel *op = [NSOpenPanel openPanel];
[op setAllowsMultipleSelection:YES];
[op setCanChooseDirectories:NO];
[op setCanChooseFiles:YES];
[op beginWithCompletionHandler:^(NSInteger result) {
if ( result == NSFileHandlingPanelOKButton ) {
NSArray *selection = [op URLs];
[selection enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
// add the document to the store
[searchStore addDocument:(NSURL*)obj typeHint:nil]; // <- call to store
// add the document to our model
[documents addObject:(NSURL*)obj];
}];
}
[self reloadDocumentsTableWithAllDocuments];
}];
}
- (IBAction) removeDocument:(id)sender {
NSInteger selectedRow = [documentsTable selectedRow];
if ( selectedRow == -1 ) {
NSBeep(); return;
}
NSURL *documentURL = [[filteredDocuments objectAtIndex:selectedRow] retain];
[filteredDocuments removeObject:documentURL];
[documents removeObject:documentURL];
[searchStore removeDocument:documentURL]; // <- call to store
[documentURL release];
[documentsTable deselectAll:self];
[documentsTable reloadData];
}
- (IBAction) reloadDocument:(id)sender {
// Reloading a document is a simple affair: just add it back to the index.
// However, if the document's location has changed you should replace it.
NSInteger selectedRow = [documentsTable selectedRow];
if ( selectedRow == -1 ) {
NSBeep(); return;
}
NSURL *documentURL = [[filteredDocuments objectAtIndex:selectedRow] retain];
[searchStore addDocument:documentURL typeHint:nil]; // <- call to store
// Fake a selection change in order to reload the document's terms
[[NSNotificationCenter defaultCenter]
postNotificationName:NSTableViewSelectionDidChangeNotification
object:documentsTable];
}
- (BOOL) validateMenuItem:(NSMenuItem *)menuItem {
SEL action = [menuItem action];
BOOL enabled = YES;
if ( action == @selector(removeDocument:) || action == @selector(reloadDocument:) )
enabled = ( [documentsTable selectedRow] != -1 );
return enabled;
}
#pragma mark -
- (void) reloadDocumentsTableWithAllDocuments {
[filteredDocuments removeAllObjects];
[filteredDocuments addObjectsFromArray:[[documents allObjects]
sortedArrayUsingDescriptors:descriptors]];
[documentsTable deselectAll:self];
[documentsTable reloadData];
[[[documentsTable tableColumnWithIdentifier:@"main"] headerCell] setStringValue:
[NSString stringWithFormat:@"Documents: %i",[filteredDocuments count]]];
// Don't care for the rest of the interface? Just uncomment the lines below and
// see what happens when you load documents into the store.
/*
NSArray *terms = [searchStore allTerms];
NSLog(@"%@", terms);
NSString *term = [terms objectAtIndex:0];
NSArray *docs = [searchStore documentsForTerm:term];
NSLog(@"%@ occurs in %@", term, docs);
NSURL *docURI = [docs objectAtIndex:0];
NSArray *docTerms = [searchStore termsForDocument:docURI];
NSLog(@"%@ has the terms %@", docURI, docTerms);
NSArray * results = nil;
NSArray * normalizedRanks = nil;
NSArray * ranks = nil;
[searchStore prepareSearch:term options:kSKSearchOptionDefault];
[searchStore fetchResults:&results ranksArray:&ranks untilFinished:YES];
normalizedRanks = [searchStore normalizedRankingsArray:ranks];
NSLog(@"%@ returns %@ with ranks %@", term, results, normalizedRanks);
NSArray *allDocs = [searchStore allDocuments];
NSLog(@"all documents: %@", allDocs);
*/
}
- (void) updateDocumentsTableWithSearchResults:(NSArray*)searchResults {
[filteredDocuments removeAllObjects];
[filteredDocuments addObjectsFromArray:[searchResults sortedArrayUsingDescriptors:descriptors]];
[documentsTable deselectAll:self];
[documentsTable reloadData];
[[[documentsTable tableColumnWithIdentifier:@"main"] headerCell] setStringValue:
[NSString stringWithFormat:@"Documents: %i / %i ",
[filteredDocuments count], [documents count]]];
}
#pragma mark -
- (IBAction) performSearch:(id)sender {
static NSInteger kMinTermLength = 2;
// Search options:
// kSKSearchOptionDefault
// kSKSearchOptionNoRelevanceScores
// kSKSearchOptionSpaceMeansOR
// kSKSearchOptionFindSimilar
// See the SPSearchStore fetchResults:ranksArray:untilFinished: header definition
// for more information on SearchKit queries.
// We perform a very basic, single threaded search here that may potentially block the
// interface if the index is adequately large. (Actually, SearchKit spawns a thread
// for the query but we block the main thread during fetch). A more advanced search
// might launch a separate operation or thread to perform the search and fetch, and
// then cancel that operation prior to performing any more searches.
NSString *searchValue = [sender stringValue];
if ( [searchValue length] < kMinTermLength ) {
// reset the documents array
[self reloadDocumentsTableWithAllDocuments];
return;
}
NSArray *results = nil;
NSArray *rankings = nil;
NSArray *normalizedRankings = nil;
[searchStore cancelSearch]; // <- call to store
[searchStore prepareSearch:searchValue options:kSKSearchOptionDefault]; //
[searchStore fetchResults:&results ranksArray:&rankings untilFinished:YES]; //
normalizedRankings = [searchStore normalizedRankingsArray:rankings]; //
// we aren't using the normalized rankings, but you should call
// this method if you are
[self updateDocumentsTableWithSearchResults:results];
}
- (IBAction) performConcurrentSearch:(id)sender {
if ( searchQue == nil ) {
searchQue = [[NSOperationQueue alloc] init];
[searchQue setMaxConcurrentOperationCount:1];
}
// This is a more advanced, concurrent search which performs both query preparation and
// result fetching in a non-blocking manner. We use a serial operation queue to permit
// a single search query to take place at a time. During the processing, we check if this
// query is still the active one, and if not, we end it, allowing the next, more current
// search operation to begin.
// We check if the active query is the current one by comparing a local copy of the query
// string to the current string. Objective-C exceptions must be enabled for this
// particular implementation to work.
// I imagine there are better ways to accomplish this. Concurrency programming is not my
// forte, and suggestions for improvements are gladly accepted.
static NSInteger kMinTermLength = 2;
@synchronized(searchString) {
// Lock here. If an operation block is currently in the middle of a fetch, when it
// finishes the fetch and checks its local searchString copy against this one, it
// will bail on the operation.
// On the other hand, if the operation block is in the middle of updating the UI, we
// want to allow that process to finish so that we can then immediately force the UI
// back to a clean state with a call to reloadDocumentsTableWithAllDocuments.
[searchString release], searchString = nil;
searchString = [[sender stringValue] copy];
}
if ( [searchString length] < kMinTermLength ) {
[searchString release], searchString = nil;
// reset the documents array
[self reloadDocumentsTableWithAllDocuments];
return;
}
[searchQue addOperationWithBlock:^(void) {
NSString *blockSearchValue = nil;
BOOL stillSearching = YES;
NSMutableArray *results = [NSMutableArray array];
NSMutableArray *rankings = [NSMutableArray array];
NSArray *normalizedRankings = nil;
@synchronized(searchString) {
blockSearchValue = [searchString copy];
}
[searchStore cancelSearch]; // <- call to store
[searchStore prepareSearch:blockSearchValue options:kSKSearchOptionDefault]; //
while ( stillSearching ) {
NSArray *localResults = nil;
NSArray *localRankings = nil;
@synchronized(searchString) {
// check the current state of the search prior to each fetch iteration
if ( ![blockSearchValue isEqualToString:searchString] ) {
[blockSearchValue release], blockSearchValue = nil;
[searchStore cancelSearch]; // <- call to store
return; // exit the block
}
}
// the fetch will return even if we haven't acquired all the results
stillSearching = [searchStore fetchResults:&localResults // <- call to store
ranksArray:&localRankings untilFinished:NO];
[rankings addObjectsFromArray:localRankings];
[results addObjectsFromArray:localResults];
// optionally, we could update the UI here each time we have acquired
// new results, instead of waiting until we are finished searching.
}
@synchronized(searchString) {
// check the current state of the search one last time before updating
// the user interface; lock the code until we've finished updating the UI
if ( ![blockSearchValue isEqualToString:searchString] ) {
[blockSearchValue release], blockSearchValue = nil;
[searchStore cancelSearch]; // <- call to store
return; // exit the block
}
normalizedRankings = [searchStore normalizedRankingsArray:rankings];
// we aren't using the normalized rankings, but you should call
// this method if you are
// always update the UI on the main thread
[self performSelectorOnMainThread:@selector(updateDocumentsTableWithSearchResults:)
withObject:results waitUntilDone:YES];
[blockSearchValue release];
blockSearchValue = nil;
}
}];
}
#pragma mark -
- (IBAction) saveDocument:(id)sender {
// Write the store data to disk. Because we have a memory based store, first we flush
// the index to the memory backing and then we write out the data object. If this were
// a file based store, we could simply call saveChangesToStore
NSData *storeData = nil;
[searchStore saveChangesToStore]; // <- call to store
storeData = [searchStore storeData]; //
NSSavePanel *sp = [NSSavePanel savePanel];
[sp setAllowedFileTypes:[NSArray arrayWithObject:@"spmstore"]];
[sp beginWithCompletionHandler:^(NSInteger result) {
if ( result != NSFileHandlingPanelOKButton )
return;
NSURL *fileURL = [sp URL];
NSError *error = nil;
[storeData writeToURL:fileURL options:NSDataWritingAtomic error:&error];
}];
}
- (IBAction) openDocument:(id)sender {
// Ask for a document of type .spmstore and if we have one, clean out the current
// store and replace it with the new store. Note that we reload our indexed documents
// from the store itself, whereas typically an application will maintain an object
// graph which keeps track of these files independently of the store.
NSOpenPanel *op = [NSOpenPanel openPanel];
[op setAllowedFileTypes:[NSArray arrayWithObject:@"spmstore"]];
[op setAllowsMultipleSelection:NO];
[op beginWithCompletionHandler:^(NSInteger result) {
if ( result != NSFileHandlingPanelOKButton )
return;
NSURL *fileURL = [op URL];
NSError *error = nil;
NSMutableData *storeData = [NSMutableData dataWithContentsOfURL:fileURL options:0 error:&error];
if ( storeData != nil ) {
[searchStore release];
searchStore = nil;
[self initializeStoreWithData:storeData];
if ( searchStore != nil ) {
// reload our local documents url array from the store
NSArray *loadedDocs = [searchStore allDocuments:YES]; // <- call to store
// see the header declaration for the allDocuments method
// for details on why this call may not return the results
// you expect
[documents removeAllObjects];
[documents addObjectsFromArray:loadedDocs];
[self reloadDocumentsTableWithAllDocuments];
// After loading a saved store you have a good opportunity to check for
// index bloat. Check to compact the index here. Note that we check
// after calling allDocuments on the store because this method, although
// it is performed on a separate thread, will block all other store
// queries and alterations, including the call to allDocuments.
[searchStore compactStore:0.25]; // <- call to store
}
}
}];
}
#pragma mark -
#pragma mark Table View Data Source & Delegateion
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView {
if ( aTableView == documentsTable )
{
return [filteredDocuments count];
}
else if ( aTableView == termsTable )
{
return [terms count];
}
else if ( aTableView == containingTable )
{
return [containing count];
}
return 0;
}
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn
row:(NSInteger)rowIndex {
if ( aTableView == documentsTable )
{
return [fm displayNameAtPath:[[filteredDocuments objectAtIndex:rowIndex] path]];
}
else if ( aTableView == termsTable )
{
return [terms objectAtIndex:rowIndex];
}
else if ( aTableView == containingTable )
{
return [fm displayNameAtPath:[[containing objectAtIndex:rowIndex] path]];
}
return nil;
}
#pragma mark -
- (void)tableViewSelectionDidChange:(NSNotification *)aNotification {
NSTableView *aTableView = [aNotification object];
NSInteger rowIndex = [aTableView selectedRow];
if ( aTableView == documentsTable )
{
[termsTable deselectAll:self];
[terms removeAllObjects];
if ( rowIndex != -1 ) {
NSURL *documentURL = [filteredDocuments objectAtIndex:rowIndex];
NSArray *documentTerms = [searchStore termsForDocument:documentURL]; // <- call to store
[terms addObjectsFromArray:[documentTerms sortedArrayUsingSelector:
@selector(caseInsensitiveCompare:)]];
}
[termsTable reloadData];
[[[termsTable tableColumnWithIdentifier:@"main"] headerCell] setStringValue:
[NSString stringWithFormat:@"Document Terms: %i",[terms count]]];
}
else if ( aTableView == termsTable )
{
[containing removeAllObjects];
if ( rowIndex != -1 ) {
NSString *term = [terms objectAtIndex:rowIndex];
NSArray *containgDocuments = [searchStore documentsForTerm:term]; // <- call to store
[containing addObjectsFromArray:[containgDocuments
sortedArrayUsingDescriptors:descriptors]];
}
[containingTable reloadData];
[[[containingTable tableColumnWithIdentifier:@"main"] headerCell] setStringValue:
[NSString stringWithFormat:@"Docs Containing Term: %i",[containing count]]];
}
}
@end