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) 2009 Nokia Corporation and/or its subsidiary(-ies).
8*/
9#ifndef _lucene_store_Directory
10#define _lucene_store_Directory
11
12#if defined(_LUCENE_PRAGMA_ONCE)
13# pragma once
14#endif
15
16#include <QtCore/QStringList>
17
18#include "CLucene/store/Lock.h"
19#include "CLucene/util/VoidList.h"
20#include "CLucene/util/Misc.h"
21
22#include "IndexInput.h"
23#include "IndexOutput.h"
24
25CL_NS_DEF(store)
26
27/** A Directory is a flat list of files. Files may be written once, when they
28* are created. Once a file is created it may only be opened for read, or
29* deleted. Random access is permitted both when reading and writing.
30*
31* <p> Direct i/o is not used directly, but rather all i/o is
32* through this API. This permits things such as: <ul>
33* <li> implementation of RAM-based indices;
34* <li> implementation indices stored in a database, via a database;
35* <li> implementation of an index as a single file;
36* </ul>
37*
38*/
39class Directory : LUCENE_REFBASE
40{
41protected:
42 Directory() {}
43 // Removes an existing file in the directory.
44 virtual bool doDeleteFile(const QString& name) = 0;
45
46public:
47 DEFINE_MUTEX(THIS_LOCK)
48
49 virtual ~Directory() {};
50
51 // Returns an array of strings, one for each file in the directory.
52 virtual QStringList list() const = 0;
53
54 // Returns true iff a file with the given name exists.
55 virtual bool fileExists(const QString& name) const = 0;
56
57 // Returns the time the named file was last modified.
58 virtual int64_t fileModified(const QString& name) const = 0;
59
60 // Returns the length of a file in the directory.
61 virtual int64_t fileLength(const QString& name) const = 0;
62
63 // Returns a stream reading an existing file.
64 virtual IndexInput* openInput(const QString& name) = 0;
65 virtual IndexInput* openInput(const QString& name, int32_t bufferSize)
66 {
67 // didnt overload bufferSize
68 return openInput(name);
69 }
70
71 // Set the modified time of an existing file to now.
72 virtual void touchFile(const QString& name) = 0;
73
74 // Removes an existing file in the directory.
75 virtual bool deleteFile(const QString& name, const bool throwError = true) {
76 bool ret = doDeleteFile(name);
77 if (!ret && throwError) {
78 char buffer[200];
79 _snprintf(buffer, 200, "couldn't delete file %s",
80 name.toLocal8Bit().constData());
81 _CLTHROWA(CL_ERR_IO, buffer);
82 }
83 return ret;
84 }
85
86 // Renames an existing file in the directory.
87 // If a file already exists with the new name, then it is replaced.
88 virtual void renameFile(const QString& from, const QString& to) = 0;
89
90 // Creates a new, empty file in the directory with the given name.
91 // Returns a stream writing this file.
92 virtual IndexOutput* createOutput(const QString& name) = 0;
93
94 // Construct a {@link Lock}.
95 // @param name the name of the lock file
96 virtual LuceneLock* makeLock(const QString& name) = 0;
97
98 // Closes the store.
99 virtual void close() = 0;
100
101 virtual QString toString() const = 0;
102
103 virtual QString getDirectoryType() const = 0;
104};
105
106CL_NS_END
107
108#endif
109