void testEndThreadException(CuTest *tc) {

    const int MAX_DOCS=1500;
	RAMDirectory ram;
	WhitespaceAnalyzer an;
	IndexWriter* writer = _CLNEW IndexWriter(&ram, &an, true);

    // add some documents
    Document doc;
    for (int i = 0; i < MAX_DOCS; i++) {
        TCHAR * tmp = English::IntToEnglish(i);
        doc.add(* new Field(_T("content"), tmp, Field::STORE_YES | Field::INDEX_UNTOKENIZED));
        writer->addDocument(&doc);
        doc.clear();
        _CLDELETE_ARRAY( tmp );
    }

    CuAssertEquals(tc, MAX_DOCS, writer->docCount());
    
    writer->close();
	_CLLDELETE(writer);
    
    // this sequence is OK: delete searcher after search thread finish
    {
        IndexSearcher * searcher = _CLNEW IndexSearcher(&ram);
        _LUCENE_THREADID_TYPE thread = _LUCENE_THREAD_CREATE(&searchDocs, searcher);
        SCOPED_LOCK_MUTEX(searchMutex);

        CONDITION_WAIT(searchMutex, searchCondition);
//        _LUCENE_SLEEP(9999); //make sure that deleteMutex is being waited on...
        CONDITION_NOTIFYALL(deleteCondition);

        _LUCENE_THREAD_JOIN(thread);

        searcher->close();
        _CLLDELETE(searcher);
    }

    // this produces memory exception: delete searcher after search finish but before thread finish
    {
        IndexSearcher * searcher = _CLNEW IndexSearcher(&ram);
        _LUCENE_THREADID_TYPE thread = _LUCENE_THREAD_CREATE(&searchDocs, searcher);
        SCOPED_LOCK_MUTEX(searchMutex);

        CONDITION_WAIT(searchMutex, searchCondition);
        searcher->close();
        _CLLDELETE(searcher);
        CONDITION_NOTIFYALL(deleteCondition);

        _LUCENE_THREAD_JOIN(thread);
    }


    ram.close();
}
 void IndexReader::setNorm(int32_t doc, const TCHAR* field, uint8_t value){
   SCOPED_LOCK_MUTEX(THIS_LOCK)
   this->ensureOpen();
   this->acquireWriteLock();
   this->hasChanges = true;
   this->doSetNorm(doc, field, value);
 }
Beispiel #3
0
/** IndexInput methods */
void FSDirectory::FSIndexInput::readInternal(uint8_t* b, const int32_t len) {
	SCOPED_LOCK_MUTEX(handle->THIS_LOCK)
	CND_PRECONDITION(handle!=NULL,"shared file handle has closed");
	CND_PRECONDITION(handle->fhandle>=0,"file is not open");

	if ( handle->_fpos != _pos ){
		if ( fileSeek(handle->fhandle,_pos,SEEK_SET) != _pos ){
			_CLTHROWA( CL_ERR_IO, "File IO Seek error");
		}
		handle->_fpos = _pos;
	}

	bufferLength = _read(handle->fhandle,b,len); // 2004.10.31:SF 1037836
	if (bufferLength == 0){
		_CLTHROWA(CL_ERR_IO, "read past EOF");
	}
	if (bufferLength == -1){
		//if (EINTR == errno) we could do something else... but we have
		//to guarantee some return, or throw EOF
		
		_CLTHROWA(CL_ERR_IO, "read error");
	}
	_pos+=bufferLength;
	handle->_fpos=_pos;
}
Beispiel #4
0
   void SingleInstanceLock::release()
   {
	   SCOPED_LOCK_MUTEX(*locks_LOCK);
	   LocksType::iterator itr = locks->find( lockName );
	   if ( itr != locks->end() ) {
		   locks->remove(itr, true);
	   }
   }
  void RAMDirectory::list(vector<string>* names) const{
    SCOPED_LOCK_MUTEX(files_mutex);

	FileMap::const_iterator itr = files.begin();
    while (itr != files.end()){
        names->push_back(itr->first);
        ++itr;
    }
  }
Beispiel #6
0
void ThreadLocalBase::UnregisterCurrentThread(){
	_LUCENE_THREADID_TYPE id = _LUCENE_CURRTHREADID;
	SCOPED_LOCK_MUTEX(ThreadLocalBase_THIS_LOCK)
	
	ThreadLocalsType::iterator itr = threadLocals.lower_bound(id);
	ThreadLocalsType::iterator end = threadLocals.upper_bound(id);
	while ( itr != end ){
		itr->second->setNull();
		++itr;
	}
}
  bool RAMDirectory::RAMLock::obtain(){
    SCOPED_LOCK_MUTEX(directory->files_mutex);
    if (!directory->fileExists(fname)) {
        IndexOutput* tmp = directory->createOutput(fname);
        tmp->close();
        _CLDELETE(tmp);

      return true;
    }
    return false;
  }
Beispiel #8
0
  FSDirectory::FSIndexInput::FSIndexInput(const FSIndexInput& other): BufferedIndexInput(other){
  //Func - Constructor
  //       Uses clone for its initialization
  //Pre  - clone is a valide instance of FSIndexInput
  //Post - The instance has been created and initialized by clone
	if ( other.handle == NULL )
		_CLTHROWA(CL_ERR_NullPointer, "other handle is null");

	SCOPED_LOCK_MUTEX(other.handle->THIS_LOCK)
	handle = _CL_POINTER(other.handle);
	_pos = other.handle->_fpos; //note where we are currently...
  }
Beispiel #9
0
void ThreadLocalBase::shutdown(){
	SCOPED_LOCK_MUTEX(ThreadLocalBase_THIS_LOCK)
	
	ThreadLocalsType::iterator itr = threadLocals.begin();
	while ( itr != threadLocals.end() ){
		itr->second->setNull();
		++itr;
	}

	ShutdownHooksType::iterator itr2 = shutdownHooks.begin();
	while ( itr2 != shutdownHooks.end() ){
		ShutdownHook* hook = *itr2;
		hook(false);
	}
}
_LUCENE_THREAD_FUNC(searchDocs, _searcher) {

    WhitespaceAnalyzer an;
    IndexSearcher * searcher = (IndexSearcher *)_searcher;
    Query * query = QueryParser::parse(_T("one"), _T("content"), &an);
    Hits * hits = searcher->search(query);
    
//    _LUCENE_SLEEP(9999); //make sure that searchMutex is being waited on...

    CONDITION_NOTIFYALL(searchCondition);
    SCOPED_LOCK_MUTEX(deleteMutex);

    _CLLDELETE(hits);
    _CLLDELETE(query);

    CONDITION_WAIT(deleteMutex, deleteCondition);
//    _LUCENE_THREAD_FUNC_RETURN(0);
}
_LUCENE_THREAD_FUNC(indexDocs, _data) {

    ThreadData * data = (ThreadData *)_data;
    int cnt = 0;
    TCHAR * text;
    for (int j=1; j<docsPerThread; j++) {
        Document doc;
        text = English::IntToEnglish(data->num*docsPerThread+j);
        doc.add(*new Field(_T("sizeContent"), text, Field::STORE_YES | Field::INDEX_UNTOKENIZED));
        data->writer->addDocument(&doc);
        _CLDELETE_ARRAY(text);
        {
            SCOPED_LOCK_MUTEX(data->dir->THIS_LOCK);
            CuAssertTrue(data->tc, data->dir->sizeInBytes == data->dir->getRecomputedSizeInBytes());
        }
    }
    _LUCENE_THREAD_FUNC_RETURN( 0 );
}
Beispiel #12
0
  ScoreDocComparator* FieldSortedHitQueue::lookup (IndexReader* reader, const TCHAR* field, int32_t type, SortComparatorSource* factory) {
    ScoreDocComparator* sdc = NULL;
    FieldCacheImpl::FileEntry* entry = (factory != NULL)
	  ? _CLNEW FieldCacheImpl::FileEntry (field, factory)
      : _CLNEW FieldCacheImpl::FileEntry (field, type);
	
	{
		SCOPED_LOCK_MUTEX(Comparators.THIS_LOCK)
		hitqueueCacheReaderType* readerCache = Comparators.get(reader);
		if (readerCache == NULL){
			_CLDELETE(entry);
			return NULL;
		}
		
		sdc = readerCache->get (entry);
		_CLDELETE(entry);
	}
	return sdc;
  }
IndexReader* IndexReader::open(Directory* directory, bool closeDirectory)
{
    //Func - Static method.
    //       Returns an IndexReader reading the index in an FSDirectory in the named path. 
    //Pre  - directory represents a directory 
    //       closeDir indicates if the directory needs to be closed
    //Post - An IndexReader has been returned that reads the index located at directory

    // in- & inter-process sync
    SCOPED_LOCK_MUTEX(directory->THIS_LOCK)

    //Instantiate an IndexReader::LockWith which can produce an IndexReader
    LuceneLock* lock = directory->makeLock(QLatin1String("commit.lock"));
    IndexReader::LockWith with(lock, directory);

    IndexReader* ret = NULL;
    try {
        //Create an IndexReader reading the index
        ret = with.runAndReturn();
    } _CLFINALLY (
        _CLDELETE(lock);
    );
Beispiel #14
0
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;
}
	  _CLTHROWA(CL_ERR_UnsupportedOperation, "This reader does not support reopen().");
  }
  CL_NS(store)::Directory* IndexReader::directory() {
    ensureOpen();
    if (NULL != _internal->directory) {
      return _internal->directory;
    } else {
	  _CLTHROWA(CL_ERR_UnsupportedOperation, "This reader does not support this method.");
    }
  }

  void IndexReader::ensureOpen(){
  }

  void IndexReader::acquireWriteLock(){
    SCOPED_LOCK_MUTEX(THIS_LOCK)
    /* NOOP */
  }

  CL_NS(document)::Document* IndexReader::document(const int32_t n){
    CL_NS(document)::Document* ret = _CLNEW CL_NS(document)::Document;
    if (!document(n,*ret) )
        _CLDELETE(ret);
    return ret;
  }

  uint64_t IndexReader::lastModified(const char* directory2) {
  //Func - Static method
  //       Returns the time the index in the named directory was last modified.
  //Pre  - directory != NULL and contains the path name of the directory to check
  //Post - The last modified time of the index has been returned
Beispiel #16
0
void ThreadLocalBase::registerShutdownHook(ShutdownHook* hook){
	SCOPED_LOCK_MUTEX(ThreadLocalBase_THIS_LOCK)
	shutdownHooks.insert(hook);
}
Beispiel #17
0
   bool SingleInstanceLock::obtain()
   {
	   SCOPED_LOCK_MUTEX(*locks_LOCK);
	   return locks->insert( lockName ).second;
   }
Beispiel #18
0
   bool SingleInstanceLock::isLocked()
   {
	   SCOPED_LOCK_MUTEX(*locks_LOCK);
	   return locks->find( lockName ) == locks->end();
   }