1 /////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2009-2014 Alan Wright. All rights reserved.
3 // Distributable under the terms of either the Apache License (Version 2.0)
4 // or the GNU Lesser General Public License.
5 /////////////////////////////////////////////////////////////////////////////
6 
7 #ifndef DIRECTORY_H
8 #define DIRECTORY_H
9 
10 #include "LuceneObject.h"
11 
12 namespace Lucene {
13 
14 /// A Directory is a flat list of files.  Files may be written once, when they are created.  Once a file
15 /// is created it may only be opened for read, or deleted.  Random access is permitted both when reading
16 /// and writing.  Directory locking is implemented by an instance of {@link LockFactory}, and can be changed
17 /// for each Directory instance using {@link #setLockFactory}.
18 class LPPAPI Directory : public LuceneObject {
19 public:
20     Directory();
21     virtual ~Directory();
22 
23     LUCENE_CLASS(Directory);
24 
25 protected:
26     bool isOpen;
27 
28     /// Holds the LockFactory instance (implements locking for this Directory instance).
29     LockFactoryPtr lockFactory;
30 
31 public:
32     /// Returns an array of strings, one for each file in the directory.
33     virtual HashSet<String> listAll() = 0;
34 
35     /// Returns true if a file with the given name exists.
36     virtual bool fileExists(const String& name) = 0;
37 
38     /// Returns the time the named file was last modified.
39     virtual uint64_t fileModified(const String& name) = 0;
40 
41     /// Set the modified time of an existing file to now.
42     virtual void touchFile(const String& name) = 0;
43 
44     /// Removes an existing file in the directory.
45     virtual void deleteFile(const String& name) = 0;
46 
47     /// Returns the length of a file in the directory.
48     virtual int64_t fileLength(const String& name) = 0;
49 
50     /// Creates a new, empty file in the directory with the given name.
51     /// Returns a stream writing this file.
52     virtual IndexOutputPtr createOutput(const String& name) = 0;
53 
54     /// Returns a stream reading an existing file.
55     virtual IndexInputPtr openInput(const String& name) = 0;
56 
57     /// Closes the store.
58     virtual void close() = 0;
59 
60     /// Ensure that any writes to this file are moved to stable storage.  Lucene uses this to properly commit
61     /// changes to the index, to prevent a machine/OS crash from corrupting the index.
62     virtual void sync(const String& name);
63 
64     /// Returns a stream reading an existing file, with the specified read buffer size.  The particular Directory
65     /// implementation may ignore the buffer size.  Currently the only Directory implementations that respect
66     /// this parameter are {@link FSDirectory} and {@link CompoundFileReader}.
67     virtual IndexInputPtr openInput(const String& name, int32_t bufferSize);
68 
69     /// Construct a {@link Lock}.
70     /// @param name the name of the lock file.
71     virtual LockPtr makeLock(const String& name);
72 
73     /// Attempt to clear (forcefully unlock and remove) the specified lock.  Only call this at a time when you
74     /// are certain this lock is no longer in use.
75     /// @param name name of the lock to be cleared.
76     void clearLock(const String& name);
77 
78     /// Set the LockFactory that this Directory instance should use for its locking implementation.  Each * instance
79     /// of LockFactory should only be used for one directory (ie, do not share a single instance across multiple
80     /// Directories).
81     /// @param lockFactory instance of {@link LockFactory}.
82     void setLockFactory(const LockFactoryPtr& lockFactory);
83 
84     /// Get the LockFactory that this Directory instance is using for its locking implementation.  Note that this
85     /// may be null for Directory implementations that provide their own locking implementation.
86     LockFactoryPtr getLockFactory();
87 
88     /// Return a string identifier that uniquely differentiates this Directory instance from other Directory
89     /// instances. This ID should be the same if two Directory instances are considered "the same index".
90     /// This is how locking "scopes" to the right index.
91     virtual String getLockID();
92 
93     virtual String toString();
94 
95     /// Copy contents of a directory src to a directory dest. If a file in src already exists in dest then the one
96     /// in dest will be blindly overwritten.  NOTE: the source directory cannot change while this method is running.
97     /// Otherwise the results are undefined.
98     /// @param src source directory.
99     /// @param dest destination directory.
100     /// @param closeDirSrc if true, call {@link #close()} method on source directory.
101     static void copy(const DirectoryPtr& src, const DirectoryPtr& dest, bool closeDirSrc);
102 
103 protected:
104     /// @throws AlreadyClosed if this Directory is closed.
105     void ensureOpen();
106 };
107 
108 }
109 
110 #endif
111