MongoDB  2.6.0
file_allocator.h
1 // @file file_allocator.h
2 
3 /* Copyright 2009 10gen Inc.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include "mongo/pch.h"
19 
20 #include <list>
21 #include <boost/filesystem/path.hpp>
22 #include <boost/thread/condition.hpp>
23 
24 #include "mongo/util/concurrency/mutex.h"
25 
26 namespace mongo {
27 
28  /*
29  * Handles allocation of contiguous files on disk. Allocation may be
30  * requested asynchronously or synchronously.
31  * singleton
32  */
33  class FileAllocator : boost::noncopyable {
34  /*
35  * The public functions may not be called concurrently. The allocation
36  * functions may be called multiple times per file, but only the first
37  * size specified per file will be used.
38  */
39  public:
40  void start();
41 
46  void requestAllocation( const string &name, long &size );
47 
48 
53  void allocateAsap( const string &name, unsigned long long &size );
54 
55  void waitUntilFinished() const;
56 
57  bool hasFailed() const;
58 
59  static void ensureLength(int fd, long size);
60 
62  static FileAllocator * get();
63 
64  private:
65 
66  FileAllocator();
67 
68  void checkFailure();
69 
70  // caller must hold pendingMutex_ lock. Returns size if allocated or
71  // allocation requested, -1 otherwise.
72  long prevSize( const string &name ) const;
73 
74  // caller must hold pendingMutex_ lock.
75  bool inProgress( const string &name ) const;
76 
78  static void run( FileAllocator * fa );
79 
80  // generate a unique name for temporary files
81  string makeTempFileName( boost::filesystem::path root );
82 
83  mutable mongo::mutex _pendingMutex;
84  mutable boost::condition _pendingUpdated;
85 
86  std::list< string > _pending;
87  mutable map< string, long > _pendingSize;
88 
89  // unique number for temporary files
90  static unsigned long long _uniqueNumber;
91 
92  bool _failed;
93 
94  static FileAllocator* _instance;
95 
96  };
97 
98 } // namespace mongo
void allocateAsap(const string &name, unsigned long long &size)
Returns when file has been allocated.
Definition: file_allocator.cpp:106
void requestAllocation(const string &name, long &size)
May be called if file exists.
Definition: file_allocator.cpp:92
On pthread systems, it is an error to destroy a mutex while held (boost mutex may use pthread)...
Definition: mutex.h:74
Definition: file_allocator.h:33