1 /*
2  * Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
3  *
4  * Distributable under the terms of either the Apache License (Version 2.0) or
5  * the GNU Lesser General Public License, as specified in the COPYING file.
6  *
7  * Changes are Copyright(C) 2007, 2008 by Nokia Corporation and/or its subsidiary(-ies), all rights reserved.
8 */
9 #ifndef _lucene_store_TransactionalRAMDirectory_
10 #define _lucene_store_TransactionalRAMDirectory_
11 
12 #if defined(_LUCENE_PRAGMA_ONCE)
13 #   pragma once
14 #endif
15 
16 #include <QtCore/QString>
17 
18 #include "RAMDirectory.h"
19 #include "CLucene/util/VoidList.h"
20 
CL_NS_DEF(store)21 CL_NS_DEF(store)
22 
23 /***
24 This transactional in-memory Directory was created to address a specific
25 situation, and was deliberately pared down to the simplest viable
26 implementation.  For the sake of simplicity, this implementation imposes
27 restrictions on what operations can be performed in the directory while a
28 transaction is in progress (documented in TransactionalRAMDirectory.cpp).
29 
30 Because the Lucene Directory interface itself is rather simplistic, it
31 would not be difficult to expand TransactionalRAMDirectory so that it
32 provided fully general transactionality.  However, the developer of this
33 original implementation was of the opinion that the last thing CLucene
34 needs is gratuitous features that exceed their required complexity and
35 haven't been rigorously tested.
36 */
37 class TransactionalRAMDirectory : public RAMDirectory
38 {
39 private:
40     typedef CL_NS(util)::CLSet<QString, void*, CL_NS(util)::Compare::Qstring,
41         CL_NS(util)::Deletor::DummyQString> FilenameSet;
42     FilenameSet filesToRemoveOnAbort;
43 
44     typedef CL_NS(util)::CLSet<QString, RAMFile*, CL_NS(util)::Compare::Qstring,
45         CL_NS(util)::Deletor::DummyQString,
46         CL_NS(util)::Deletor::Object<RAMFile> > TransFileMap;
47     TransFileMap filesToRestoreOnAbort;
48 
49     bool transOpen;
50 
51     void transResolved();
52     bool archiveOrigFileIfNecessary(const QString& name);
53     void unarchiveOrigFile(const QString& name);
54 
55 protected:
56     bool doDeleteFile(const QString& name);
57 
58 public:
59     TransactionalRAMDirectory();
60     virtual ~TransactionalRAMDirectory();
61 
62     bool transIsOpen() const;
63     void transStart();
64     void transCommit();
65     void transAbort();
66 
67     // Constrained operations:
68     void renameFile(const QString& from, const QString& to);
69     IndexOutput* createOutput(const QString& name);
70 
71     void close();
72 };
73 
74 CL_NS_END
75 
76 #endif
77