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 COMPOUNDFILEWRITER_H
8 #define COMPOUNDFILEWRITER_H
9 
10 #include "LuceneObject.h"
11 
12 namespace Lucene {
13 
14 /// Combines multiple files into a single compound file.
15 /// The file format:
16 ///    VInt fileCount
17 ///    {Directory}
18 ///    fileCount entries with the following structure:
19 ///        int64_t dataOffset
20 ///        String fileName
21 ///    {File Data}
22 ///    fileCount entries with the raw data of the corresponding file
23 ///
24 /// The fileCount integer indicates how many files are contained in this compound file. The {directory}
25 /// that follows has that many entries. Each directory entry contains a long pointer to the start of
26 /// this file's data section, and a string with that file's name.
27 class LPPAPI CompoundFileWriter : public LuceneObject {
28 public:
29     CompoundFileWriter(const DirectoryPtr& dir, const String& name, const CheckAbortPtr& checkAbort = CheckAbortPtr());
30     virtual ~CompoundFileWriter();
31 
32     LUCENE_CLASS(CompoundFileWriter);
33 
34 protected:
35     struct FileEntry {
36         /// source file
37         String file;
38 
39         /// temporary holder for the start of directory entry for this file
40         int64_t directoryOffset;
41 
42         /// temporary holder for the start of this file's data section
43         int64_t dataOffset;
44     };
45 
46     DirectoryWeakPtr _directory;
47     String fileName;
48     HashSet<String> ids;
49     Collection<FileEntry> entries;
50     bool merged;
51     CheckAbortPtr checkAbort;
52 
53 public:
54     /// Returns the directory of the compound file.
55     DirectoryPtr getDirectory();
56 
57     /// Returns the name of the compound file.
58     String getName();
59 
60     /// Add a source stream. file is the string by which the sub-stream will be known in the
61     /// compound stream.
62     void addFile(const String& file);
63 
64     /// Merge files with the extensions added up to now.  All files with these extensions are
65     /// combined sequentially into the compound stream. After successful merge, the source
66     /// are deleted.files
67     void close();
68 
69 protected:
70     /// Copy the contents of the file with specified extension into the provided output stream.
71     /// Use the provided buffer for moving data to reduce memory allocation.
72     void copyFile(const FileEntry& source, const IndexOutputPtr& os, ByteArray buffer);
73 };
74 
75 }
76 
77 #endif
78