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 #ifndef _lucene_store_Directory
8 #define _lucene_store_Directory
9
10 #include "CLucene/util/Equators.h"
11 #include "CLucene/LuceneThreads.h"
12 #include <string>
13 #include <vector>
14
CL_CLASS_DEF(store,Lock)15 CL_CLASS_DEF(store,Lock)
16 CL_CLASS_DEF(store,IndexInput)
17 CL_CLASS_DEF(store,IndexOutput)
18 CL_CLASS_DEF(store,LockFactory)
19 CL_CLASS_DEF(store,LuceneLock)
20
21 CL_NS_DEF(store)
22
23 /** A Directory is a flat list of files. Files may be written once, when they
24 * are created. Once a file is created it may only be opened for read, or
25 * deleted. Random access is permitted both when reading and writing.
26 *
27 * <p> Direct i/o is not used directly, but rather all i/o is
28 * through this API. This permits things such as: <ul>
29 * <li> implementation of RAM-based indices;
30 * <li> implementation indices stored in a database, via a database;
31 * <li> implementation of an index as a single file;
32 * </ul>
33 *
34 */
35 class CLUCENE_EXPORT Directory: LUCENE_REFBASE, public CL_NS(util)::NamedObject {
36 protected:
37 LockFactory* lockFactory;
38
39 Directory();
40 // Removes an existing file in the directory.
41 virtual bool doDeleteFile(const char* name) = 0;
42 public:
43 DEFINE_MUTEX(THIS_LOCK)
44
45 virtual ~Directory();
46
47 // Returns an null terminated array of strings, one for each file in the directory.
48 char** list() const;
49 virtual bool list(std::vector<std::string>* names) const = 0; //todo: deprecate this...
50 bool list(std::vector<std::string>& names) const;
51
52 // Returns true iff a file with the given name exists.
53 virtual bool fileExists(const char* name) const = 0;
54
55 // Returns the time the named file was last modified.
56 virtual int64_t fileModified(const char* name) const = 0;
57
58 // Returns the length of a file in the directory.
59 virtual int64_t fileLength(const char* name) const = 0;
60
61 // An advanced overload to avoid throwing an error. if result is false, error is filled with the reason
62 virtual bool openInput(const char* name, IndexInput*& ret, CLuceneError& error, int32_t bufferSize = -1) = 0;
63
64 // Returns a stream reading an existing file.
65 IndexInput* openInput(const char* name, int32_t bufferSize=-1);
66
67 /// Set the modified time of an existing file to now. */
68 virtual void touchFile(const char* name) = 0;
69
70 // Removes an existing file in the directory.
71 virtual bool deleteFile(const char* name, const bool throwError=true);
72
73 // Renames an existing file in the directory.
74 // If a file already exists with the new name, then it is replaced.
75 // This replacement should be atomic.
76 virtual void renameFile(const char* from, const char* to) = 0;
77
78 // Creates a new, empty file in the directory with the given name.
79 // Returns a stream writing this file.
80 virtual IndexOutput* createOutput(const char* name) = 0;
81
82 // Construct a {@link Lock}.
83 // @param name the name of the lock file
84 virtual LuceneLock* makeLock(const char* name);
85
86 virtual void clearLock(const char* name);
87
88 // Closes the store.
89 virtual void close() = 0;
90
91 virtual std::string toString() const = 0;
92
93 void setLockFactory( LockFactory* lockFactory );
94
95 LockFactory* getLockFactory();
96
97 virtual std::string getLockID();
98 };
99 CL_NS_END
100 #endif
101