summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/clucene/src/CLucene/index/IndexWriter.cpp
blob: 5504cf6fad7debfe625de68b1217f5172da7d51d (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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
/*
 * Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
 *
 * Distributable under the terms of either the Apache License (Version 2.0) or 
 * the GNU Lesser General Public License, as specified in the COPYING file.
 *
 * Changes are Copyright(C) 2007, 2008 by Nokia Corporation and/or its subsidiary(-ies), all rights reserved.
*/
#include "CLucene/StdHeader.h"
#include "IndexWriter.h"

#include "CLucene/document/Document.h"
#include "CLucene/store/Directory.h"
#include "CLucene/store/Lock.h"
#include "CLucene/util/VoidList.h"
#include "DocumentWriter.h"
#include "SegmentInfos.h"
#include "SegmentMerger.h"

CL_NS_USE(store)
CL_NS_USE(util)
CL_NS_USE(document)
CL_NS_USE(analysis)
CL_NS_DEF(index)

const QLatin1String IndexWriter::WRITE_LOCK_NAME("write.lock");
const QLatin1String IndexWriter::COMMIT_LOCK_NAME("commit.lock");

IndexWriter::IndexWriter(const QString& path, Analyzer* a, const bool create,
    const bool _closeDir)
    : directory(FSDirectory::getDirectory(path, create))
    , analyzer(a)
    , segmentInfos(true)
    , closeDir(_closeDir)
{
    //Func - Constructor
    //       Constructs an IndexWriter for the index in path.
    //Pre  - path != NULL and contains a named directory path
    //       a holds a valid reference to an analyzer and analyzes the text to
    //       be indexed create indicates if the indexWriter must create a new
    //       index located at path or just open it
    //Post - If create is true, then a new, empty index has been created in
    //       path, replacing the index already there, if any. The named
    //       directory path is owned by this Instance

    CND_PRECONDITION(!path.isEmpty(), "path is NULL");

    //Continue initializing the instance by _IndexWriter
    _IndexWriter(create);
}

IndexWriter::IndexWriter(Directory* d, Analyzer* a, const bool create,
    const bool _closeDir)
    : directory(_CL_POINTER(d))
    , analyzer(a)
    , segmentInfos(true)
    , closeDir(_closeDir)
{
    //Func - Constructor
    //       Constructs an IndexWriter for the index in path.
    //Pre  - d contains a valid reference to a directory
    //       a holds a valid reference to an analyzer and analyzes the text to
    //       be indexed create indicates if the indexWriter must create a new
    //       index located at path or just open it
    //Post - If create is true, then a new, empty index has been created in
    //       path, replacing the index already there, if any. The directory d
    //       is not owned by this Instance

    //Continue initializing the instance by _IndexWriter
    _IndexWriter ( create );
}

void IndexWriter::_IndexWriter(const bool create)
{
    //Func - Initialises the instances
    //Pre  - create indicates if the indexWriter must create a new index
    //       located at path or just open it

    similarity = CL_NS(search)::Similarity::getDefault();

    useCompoundFile = true;
    if ( directory->getDirectoryType() == RAMDirectory::DirectoryType() )
        useCompoundFile = false;

    //Create a ramDirectory
    ramDirectory = _CLNEW TransactionalRAMDirectory;

    CND_CONDITION(ramDirectory != NULL, "ramDirectory is NULL");

    //Initialize the writeLock to
    writeLock  = NULL;

    //initialise the settings...
    maxFieldLength = DEFAULT_MAX_FIELD_LENGTH;
    mergeFactor = DEFAULT_MERGE_FACTOR;
    maxMergeDocs = DEFAULT_MAX_MERGE_DOCS;
    writeLockTimeout = WRITE_LOCK_TIMEOUT;
    commitLockTimeout = COMMIT_LOCK_TIMEOUT;
    minMergeDocs = DEFAULT_MAX_BUFFERED_DOCS;
    termIndexInterval = DEFAULT_TERM_INDEX_INTERVAL;

    //Create a new lock using the name "write.lock"
    LuceneLock* newLock = directory->makeLock(IndexWriter::WRITE_LOCK_NAME);

    //Condition check to see if newLock has been allocated properly
    CND_CONDITION(newLock != NULL,
        "No memory could be allocated for LuceneLock newLock");

    //Try to obtain a write lock
    if (!newLock->obtain(writeLockTimeout)){
        //Write lock could not be obtained so delete it
        _CLDELETE(newLock);
        //Reset the instance
        _finalize();
        //throw an exception because no writelock could be created or obtained
        _CLTHROWA(CL_ERR_IO, "Index locked for write or no write access." );
    }

    //The Write Lock has been obtained so save it for later use
    this->writeLock = newLock;

    //Create a new lock using the name "commit.lock"
    LuceneLock* lock = directory->makeLock(IndexWriter::COMMIT_LOCK_NAME);

    //Condition check to see if lock has been allocated properly
    CND_CONDITION(lock != NULL, "No memory could be allocated for LuceneLock lock");

    LockWith2 with(lock, commitLockTimeout, this, NULL, create);
    {
        SCOPED_LOCK_MUTEX(directory->THIS_LOCK) // in- & inter-process sync
        with.run();
    }

    //Release the commit lock
    _CLDELETE(lock);

    isOpen = true;
}

IndexWriter::~IndexWriter()
{
    //Func - Destructor
    //Pre  - true
    //Post - The instance has been destroyed
    close();
    _finalize();
}

void IndexWriter::close()
{
    //Func - Flushes all changes to an index, closes all associated files, and
    //       closes the directory that the index is stored in.
    //Pre  - closeDir indicates if the directory must be closed or not
    //Post - All the changes have been flushed to disk and the write lock has
    //       been released. The ramDirectory has also been closed. The 
    //       directory has been closed if the reference count of the directory
    //       reaches zero

    SCOPED_LOCK_MUTEX(THIS_LOCK)
    if (isOpen) {
        //Flush the Ram Segments
        flushRamSegments();
        //Close the ram directory
        if (ramDirectory != NULL) {
            ramDirectory->close();
            _CLDECDELETE(ramDirectory);
        }

        //Check if this instance must close the directory
        if (closeDir)
            directory->close();
        _CLDECDELETE(directory);

        // release write lock
        if (writeLock != NULL) {
            writeLock->release();
            _CLDELETE(writeLock);
        }
        isOpen = false;
    }
}

void IndexWriter::_finalize()
{
    //Func - Releases all the resources of the instance
    //Pre  - true
    //Post - All the releases have been released

    if(writeLock != NULL) {
        //release write lock
        writeLock->release();
        _CLDELETE( writeLock );
    }

    //Delete the ramDirectory
    if (ramDirectory != NULL) {
        ramDirectory->close();
        _CLDECDELETE(ramDirectory);
    }
}

int32_t IndexWriter::docCount()
{
    //Func - Counts the number of documents in the index
    //Pre  - true
    //Post - The number of documents have been returned

    SCOPED_LOCK_MUTEX(THIS_LOCK)

    //Initialize count
    int32_t count = 0;

    //Iterate through all segmentInfos
    for (int32_t i = 0; i < segmentInfos.size(); i++) {
        //Get the i-th SegmentInfo
        SegmentInfo* si = segmentInfos.info(i);
        //Retrieve the number of documents of the segment and add it to count
        count += si->docCount;
    }
    return count;
}

void IndexWriter::addDocument(Document* doc, Analyzer* analyzer)
{
    //Func - Adds a document to the index
    //Pre  - doc contains a valid reference to a document
    //       ramDirectory != NULL
    //Post - The document has been added to the index of this IndexWriter
    CND_PRECONDITION(ramDirectory != NULL, "ramDirectory is NULL");

    if (analyzer == NULL)
        analyzer = this->analyzer;

    ramDirectory->transStart();
    try {
        QString segmentName = newSegmentName();
        CND_CONDITION(!segmentName.isEmpty(), "segmentName is NULL");
        try {
            //Create the DocumentWriter using a ramDirectory and analyzer
            // supplied by the IndexWriter (this).
            DocumentWriter* dw = _CLNEW DocumentWriter(ramDirectory, analyzer,
                this );
            CND_CONDITION(dw != NULL, "dw is NULL");
            try {
                //Add the client-supplied document to the new segment.
                dw->addDocument(segmentName, doc);
            } _CLFINALLY (
                _CLDELETE(dw);
            );

            //Create a new SegmentInfo instance about this new segment.
            SegmentInfo* si = _CLNEW SegmentInfo(segmentName, 1, ramDirectory);
            CND_CONDITION(si != NULL, "Si is NULL");
            {
                SCOPED_LOCK_MUTEX(THIS_LOCK)

                //Add the info object for this particular segment to the list
                // of all segmentInfos->
                segmentInfos.add(si);

                //Check to see if the segments must be merged
                maybeMergeSegments();
            }
        } _CLFINALLY()
    } catch (...) {
        ramDirectory->transAbort();
        throw;
}
    ramDirectory->transCommit();
}

void IndexWriter::optimize()
{
    //Func - Optimizes the index for which this Instance is responsible
    //Pre  - true
    //Post -
    SCOPED_LOCK_MUTEX(THIS_LOCK)

    //Flush the RamSegments to disk
    flushRamSegments();
    while (segmentInfos.size() > 1
        || (segmentInfos.size() == 1
            && (SegmentReader::hasDeletions(segmentInfos.info(0))
            || segmentInfos.info(0)->getDir()!=directory
            || (useCompoundFile 
            && (!SegmentReader::usesCompoundFile(segmentInfos.info(0))
                || SegmentReader::hasSeparateNorms(segmentInfos.info(0))))))) {
            int32_t minSegment = segmentInfos.size() - mergeFactor;
            mergeSegments(minSegment < 0 ? 0 : minSegment);
    }
}


QString IndexWriter::newSegmentName()
{
    SCOPED_LOCK_MUTEX(THIS_LOCK)
    return QLatin1Char('_') + QString::number(segmentInfos.counter++, 36);
}

void IndexWriter::flushRamSegments()
{
    //Func - Merges all RAM-resident segments.
    //Pre  - ramDirectory != NULL
    //Post - The RAM-resident segments have been merged to disk

    CND_PRECONDITION(ramDirectory != NULL, "ramDirectory is NULL");

    int32_t minSegment = segmentInfos.size()-1; //don't make this unsigned...
    CND_CONDITION(minSegment >= -1, "minSegment must be >= -1");

    int32_t docCount = 0;
    //Iterate through all the segements and check if the directory is a ramDirectory
    while (minSegment >= 0 &&
        segmentInfos.info(minSegment)->getDir() == ramDirectory) {
            docCount += segmentInfos.info(minSegment)->docCount;
            minSegment--;
    }
    if (minSegment < 0 ||			  // add one FS segment?
        (docCount + segmentInfos.info(minSegment)->docCount) > mergeFactor ||
        !(segmentInfos.info(segmentInfos.size()-1)->getDir() == ramDirectory))
        minSegment++;

    CND_CONDITION(minSegment >= 0, "minSegment must be >= 0");
    if (minSegment >= segmentInfos.size())
        return;					  // none to merge
    mergeSegments(minSegment);
}

void IndexWriter::maybeMergeSegments() {
    //Func - Incremental Segment Merger
    //Pre  -
    //Post -

    int64_t targetMergeDocs = minMergeDocs;

    // find segments smaller than current target size
    while (targetMergeDocs <= maxMergeDocs) {
        int32_t minSegment = segmentInfos.size();
        int32_t mergeDocs = 0;

        while (--minSegment >= 0) {
            SegmentInfo* si = segmentInfos.info(minSegment);
            if (si->docCount >= targetMergeDocs)
                break;
            mergeDocs += si->docCount;
        }

        if (mergeDocs >= targetMergeDocs){
            // found a merge to do
            mergeSegments(minSegment+1);
        }else
            break;

        //increase target size
        targetMergeDocs *= mergeFactor;
    }
}

void IndexWriter::mergeSegments(const uint32_t minSegment)
{
    mergeSegments(minSegment, segmentInfos.size());
}

void IndexWriter::mergeSegments(const uint32_t minSegment, const uint32_t end)
{
    CLVector<SegmentReader*> segmentsToDelete(false);
    QString mergedName = newSegmentName();
#ifdef _CL_DEBUG_INFO
    fprintf(_CL_DEBUG_INFO, "merging segments\n");
#endif
    SegmentMerger merger(this, mergedName);
    for (size_t i = minSegment; i < end; i++) {
        SegmentInfo* si = segmentInfos.info(i);
#ifdef _CL_DEBUG_INFO
        fprintf(_CL_DEBUG_INFO, " %s (%d docs)\n", 
            si->name.toLocal8Bit().constData(), si->docCount);
#endif
        SegmentReader* reader = _CLNEW SegmentReader(si);
        merger.add(reader);
        // if we own the directory
        if ((reader->getDirectory() == this->directory)
            || (reader->getDirectory() == this->ramDirectory)) {
                // queue segment for deletion
                segmentsToDelete.push_back(reader);	  
        }
    }

    int32_t mergedDocCount = merger.merge();

#ifdef _CL_DEBUG_INFO
    fprintf(_CL_DEBUG_INFO, "\n into %s (%d docs)\n",
        mergedName.toLocal8Bit().constData(), mergedDocCount);
#endif

    segmentInfos.clearto(minSegment);// remove old infos & add new
    segmentInfos.add(_CLNEW SegmentInfo(mergedName, mergedDocCount, directory));

    // close readers before we attempt to delete now-obsolete segments
    merger.closeReaders();

    LuceneLock* lock = directory->makeLock(IndexWriter::COMMIT_LOCK_NAME);
    LockWith2 with (lock, commitLockTimeout, this, &segmentsToDelete, true);
    {
        SCOPED_LOCK_MUTEX(directory->THIS_LOCK) // in- & inter-process sync
        with.run();
    }
    _CLDELETE( lock );

    if (useCompoundFile) {
        QStringList filesToDelete;
        merger.createCompoundFile(mergedName + QLatin1String(".tmp"), filesToDelete);

        LuceneLock* lock = directory->makeLock(IndexWriter::COMMIT_LOCK_NAME);
        LockWithCFS with(lock, commitLockTimeout, directory, this, mergedName,
            filesToDelete);
        {
            SCOPED_LOCK_MUTEX(directory->THIS_LOCK) // in- & inter-process sync
            with.run();
        }
        _CLDELETE(lock);
    }
}

void IndexWriter::deleteSegments(CLVector<SegmentReader*>* segments)
{
    QStringList deletable;

    {//scope delete deleteArray object
        QStringList deleteArray;
        readDeleteableFiles(deleteArray);
        deleteFiles(deleteArray, deletable); // try to delete deleteable
    }

    QStringList files;
    for (uint32_t i = 0; i < segments->size(); i++) {
        SegmentReader* reader = (*segments)[i];
        files.clear();
        reader->files(files);
        if (reader->getDirectory() == this->directory)
            deleteFiles(files, deletable);	  // try to delete our files
        else
            deleteFiles(files, reader->getDirectory()); // delete, eg, RAM files
    }

    writeDeleteableFiles(deletable);		  // note files we can't delete
}

void IndexWriter::deleteFiles(const QStringList& files)
{
    QStringList currentDeletable;
    readDeleteableFiles(currentDeletable);

    // try to delete deleteable
    QStringList deletable;
    deleteFiles(currentDeletable, deletable);
    
    // try to delete our files
    deleteFiles(files, deletable);

    // note files we can't delete
    writeDeleteableFiles(deletable);
}

void IndexWriter::readDeleteableFiles(QStringList& result)
{
    if (!directory->fileExists(QLatin1String("deletable")))
        return;

    IndexInput* input = directory->openInput(QLatin1String("deletable"));
    try {
        // read file names
        TCHAR tname[CL_MAX_PATH];
        for (int32_t i = input->readInt(); i > 0; i--) {
            int32_t read = input->readString(tname, CL_MAX_PATH);
            result.push_back(QString::fromWCharArray(tname, read));
        }
    } _CLFINALLY (
        input->close();
        _CLDELETE(input);
    );
}

void IndexWriter::deleteFiles(const QStringList& files, QStringList& deletable)
{
    QStringList::const_iterator itr;
    for (itr = files.begin(); itr != files.end(); ++itr) {
        if (!getDirectory()->fileExists((*itr)))
            continue;

        if (!getDirectory()->deleteFile((*itr), false)) {
            if (directory->fileExists((*itr))) {
#ifdef _CL_DEBUG_INFO
                fprintf(_CL_DEBUG_INFO, "%s; Will re-try later.\n", err.what());
#endif
                // add to deletable
                deletable.push_back((*itr));
            }
        }
    }
}

void IndexWriter::deleteFiles(const QStringList& files, Directory* directory)
{
    QStringList::const_iterator itr;
    for (itr = files.begin(); itr != files.end(); ++itr)
        directory->deleteFile((*itr), true);
}

void IndexWriter::writeDeleteableFiles(const QStringList& files)
{
    IndexOutput* output = directory->createOutput(QLatin1String("deleteable.new"));
    try {
        output->writeInt(files.size());

        TCHAR tfile[CL_MAX_PATH];
        QStringList::const_iterator itr;
        for (itr  = files.begin(); itr != files.end(); ++itr) {
            tfile[(*itr).toWCharArray(tfile)] = '\0';
            output->writeString(tfile, _tcslen(tfile));
        }
    } _CLFINALLY (
        output->close();
        _CLDELETE(output);
    );

    directory->renameFile(QLatin1String("deleteable.new"),
        QLatin1String("deletable"));
}

void IndexWriter::addIndexes(Directory** dirs)
{
    //Func - Add several indexes located in different directories into the current
    //       one managed by this instance
    //Pre  - dirs != NULL and contains directories of several indexes
    //       dirsLength > 0 and contains the number of directories
    //Post - The indexes located in the directories in dirs have been merged with
    //       the pre(current) index. The Resulting index has also been optimized

    SCOPED_LOCK_MUTEX(THIS_LOCK)

    CND_PRECONDITION(dirs != NULL, "dirs is NULL");

    // start with zero or 1 seg so optimize the current
    optimize();

    int32_t start = segmentInfos.size();

    //Iterate through the directories
    for (int32_t i = 0; dirs[i] != NULL; ++i) {
        // DSR: Changed SegmentInfos constructor arg (see bug discussion below).
        SegmentInfos sis(false);
        sis.read(dirs[i]);
        for (int32_t j = 0; j < sis.size(); j++)
            segmentInfos.add(sis.info(j));	  // add each info
    }

    // commented out by tbusch to solve a bug and to be conform with
    // java lucene

    // merge newly added segments in log(n) passes
    //while (segmentInfos.size() > start + mergeFactor) {
    //    for (int32_t base = start; base < segmentInfos.size(); base++) {
    //        int32_t end = min(segmentInfos.size(), base + mergeFactor);
    //        if (end - base > 1)
    //            mergeSegments(base, end);
    //    }
    //}

    // cleanup
    optimize();
}


void IndexWriter::addIndexes(IndexReader** readers)
{
    SCOPED_LOCK_MUTEX(THIS_LOCK)
    optimize();					  // start with zero or 1 seg

    QString mergedName = newSegmentName();
    SegmentMerger merger(this, mergedName);

    CLVector<SegmentReader*> segmentsToDelete;
    SegmentReader* sReader = NULL;
    if (segmentInfos.size() == 1) { // add existing index, if any
        sReader = _CLNEW SegmentReader(segmentInfos.info(0));
        merger.add(sReader);
        segmentsToDelete.push_back(sReader);   // queue segment for deletion
    }

    int32_t readersLength = 0;
    while (readers[readersLength] != NULL)
        merger.add(readers[readersLength++]);

    int32_t docCount = merger.merge();                // merge 'em

    // pop old infos & add new
    segmentInfos.clearto(0);
    segmentInfos.add(_CLNEW SegmentInfo(mergedName, docCount, directory));

    if (sReader != NULL) {
        sReader->close();
        _CLDELETE(sReader);
    }

    LuceneLock* lock = directory->makeLock(IndexWriter::COMMIT_LOCK_NAME);
    LockWith2 with(lock, commitLockTimeout, this, &segmentsToDelete, true);
    {
        // in- & inter-process sync
        SCOPED_LOCK_MUTEX(directory->THIS_LOCK)
        with.run();
    }
    _CLDELETE(lock);

    if (useCompoundFile) {
        QStringList filesToDelete;
        merger.createCompoundFile(mergedName + QLatin1String(".tmp"),
            filesToDelete);

        LuceneLock* cfslock = directory->makeLock(IndexWriter::COMMIT_LOCK_NAME);
        LockWithCFS with(cfslock, commitLockTimeout, directory, this, mergedName,
            filesToDelete);
        {
            // in- & inter-process sync
            SCOPED_LOCK_MUTEX(directory->THIS_LOCK)
            with.run();
        }
        _CLDELETE(cfslock);
    }
}

// #pragma mark -- IndexWriter::LockWith2

IndexWriter::LockWith2::LockWith2(CL_NS(store)::LuceneLock* lock,
                                  int64_t lockWaitTimeout,
                                  IndexWriter* indexWriter,
                                  CL_NS(util)::CLVector<SegmentReader*>* std,
                                  bool _create)
    : CL_NS(store)::LuceneLockWith<void>(lock, lockWaitTimeout)
    , create(_create)
    , writer(indexWriter)
    , segmentsToDelete(std)
{
}

void IndexWriter::LockWith2::doBody()
{
    //Func - Writes segmentInfos to or reads  segmentInfos from disk
    //Pre  - writer != NULL
    //Post - if create is true then segementInfos has been written to disk 
    //       otherwise segmentInfos has been read from disk

    CND_PRECONDITION(writer != NULL, "writer is NULL");

    if (create) {
        writer->segmentInfos.write(writer->getDirectory());
        // delete now-unused segments
        if (segmentsToDelete != NULL)
            writer->deleteSegments(segmentsToDelete);
    } else {
        writer->segmentInfos.read(writer->getDirectory());
    }
}

// #pragma mark -- IndexWriter::LockWithCFS

IndexWriter::LockWithCFS::LockWithCFS(CL_NS(store)::LuceneLock* lock,
                                      int64_t lockWaitTimeout,
                                      CL_NS(store)::Directory* dir,
                                      IndexWriter* indexWriter,
                                      const QString& segmentName,
                                      const QStringList& ftd)
    : CL_NS(store)::LuceneLockWith<void>(lock, lockWaitTimeout)
    , segName(segmentName)
    , writer(indexWriter)
    , directory(dir)
    , filesToDelete(ftd)
{
}

void IndexWriter::LockWithCFS::doBody()
{
    //Func - Writes segmentInfos to or reads  segmentInfos from disk
    //Pre  - writer != NULL
    //Post - if create is true then segementInfos has been written to disk
    //       otherwise segmentInfos has been read from disk

    CND_PRECONDITION(directory != NULL, "directory is NULL");
    CND_PRECONDITION(!segName.isEmpty(), "mergedName is NULL");

    // make compound file visible for SegmentReaders
    directory->renameFile(segName + QLatin1String(".tmp"),
        segName + QLatin1String(".cfs"));
    // delete now unused files of segment 
    writer->deleteFiles(filesToDelete);   
}

CL_NS_END