STXXL  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fileperblock_file.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * lib/io/fileperblock_file.cpp
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2008, 2009 Johannes Singler <[email protected]>
7  * Copyright (C) 2008 Andreas Beckmann <[email protected]>
8  *
9  * Distributed under the Boost Software License, Version 1.0.
10  * (See accompanying file LICENSE_1_0.txt or copy at
11  * http://www.boost.org/LICENSE_1_0.txt)
12  **************************************************************************/
13 
14 #include <cassert>
15 #include <cstdio>
16 #include <iomanip>
17 #include <sstream>
18 #include <string>
19 
24 #include <stxxl/bits/config.h>
28 #include <stxxl/bits/io/file.h>
31 #include <stxxl/bits/io/request.h>
35 #include <stxxl/bits/namespace.h>
36 #include <stxxl/bits/unused.h>
37 #include <stxxl/bits/verbose.h>
38 #include "ufs_platform.h"
39 
40 
42 
43 template <class base_file_type>
45  const std::string& filename_prefix,
46  int mode,
47  int queue_id,
48  int allocator_id)
49  : disk_queued_file(queue_id, allocator_id),
50  filename_prefix(filename_prefix),
51  mode(mode),
52  current_size(0),
53  lock_file_created(false),
54  lock_file(filename_prefix + "_fpb_lock", mode, queue_id)
55 { }
56 
57 template <class base_file_type>
59 {
60  if (lock_file_created)
61  {
62  if (::remove((filename_prefix + "_fpb_lock").c_str()) != 0)
63  STXXL_ERRMSG("remove() error on path=" << filename_prefix << "_fpb_lock error=" << strerror(errno));
64  }
65 }
66 
67 template <class base_file_type>
69 {
70  std::ostringstream name;
71  //enough for 1 billion blocks
72  name << filename_prefix << "_fpb_" << std::setw(20) << std::setfill('0') << offset;
73  return name.str();
74 }
75 
76 template <class base_file_type>
78 {
79  assert(req->get_file() == this);
80 
81  base_file_type base_file(filename_for_block(req->get_offset()), mode, get_queue_id());
82  base_file.set_size(req->get_size());
83 
84  request_ptr derived(new serving_request(default_completion_handler(), &base_file, req->get_buffer(), 0, req->get_size(), req->get_type()));
85  request_ptr dummy = derived;
86  derived->serve();
87 }
88 
89 template <class base_file_type>
91 {
92  if (!lock_file_created)
93  {
94  //create lock file and fill it with one page, an empty file cannot be locked
95  const int page_size = BLOCK_ALIGN;
96  void* one_page = aligned_alloc<BLOCK_ALIGN>(page_size);
97 #if STXXL_WITH_VALGRIND
98  memset(one_page, 0, page_size);
99 #endif
100  lock_file.set_size(page_size);
101  request_ptr r = lock_file.awrite(one_page, 0, page_size, default_completion_handler());
102  r->wait();
103  aligned_dealloc<BLOCK_ALIGN>(one_page);
104  lock_file_created = true;
105  }
106  lock_file.lock();
107 }
108 
109 template <class base_file_type>
111 {
112  STXXL_UNUSED(length);
113 #ifdef STXXL_FILEPERBLOCK_NO_DELETE
114  if (::truncate(filename_for_block(offset).c_str(), 0) != 0)
115  STXXL_ERRMSG("truncate() error on path=" << filename_for_block(offset) << " error=" << strerror(errno));
116 #else
117  if (::remove(filename_for_block(offset).c_str()) != 0)
118  STXXL_ERRMSG("remove() error on path=" << filename_for_block(offset) << " error=" << strerror(errno));
119 #endif
120 
121  STXXL_VERBOSE2("discard " << offset << " + " << length);
122 }
123 
124 template <class base_file_type>
126 {
127  std::string original(filename_for_block(offset));
128  filename.insert(0, original.substr(0, original.find_last_of("/") + 1));
129  if (::remove(filename.c_str()) != 0)
130  STXXL_ERRMSG("remove() error on path=" << filename << " error=" << strerror(errno));
131 
132  if (::rename(original.c_str(), filename.c_str()) != 0)
133  STXXL_ERRMSG("rename() error on path=" << filename << " to=" << original << " error=" << strerror(errno));
134 
135 #if !STXXL_WINDOWS
136  //TODO: implement on Windows
137  if (::truncate(filename.c_str(), length) != 0) {
138  STXXL_THROW_ERRNO(io_error, "Error doing truncate()");
139  }
140 #else
141  STXXL_UNUSED(length);
142 #endif
143 }
144 
145 template <class base_file_type>
147 {
148  return "fileperblock";
149 }
150 
151 ////////////////////////////////////////////////////////////////////////////
152 
153 template class fileperblock_file<syscall_file>;
154 
155 #if STXXL_HAVE_MMAP_FILE
156 template class fileperblock_file<mmap_file>;
157 #endif
158 
159 #if STXXL_HAVE_WINCALL_FILE
160 template class fileperblock_file<wincall_file>;
161 #endif
162 
163 #if STXXL_HAVE_BOOSTFD_FILE
164 template class fileperblock_file<boostfd_file>;
165 #endif
166 
virtual void discard(offset_type offset, offset_type length)
Frees the specified region. Actually deletes the corresponding file if the whole thing is deleted...
Default completion handler class.
#define STXXL_VERBOSE2(x)
Definition: verbose.h:107
virtual void export_files(offset_type offset, offset_type length, std::string filename)
Rename the file corresponding to the offset such that it is out of reach for deleting.
Request which serves an I/O by calling the synchronous routine of the file.
virtual void wait(bool measure_time=true)=0
Suspends calling thread until completion of the request.
#define BLOCK_ALIGN
Definition: request.h:34
virtual void serve(const request *req)
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
void STXXL_UNUSED(const U &)
Definition: unused.h:23
Implementation of some file methods based on serving_request.
Implementation of file based on other files, dynamically allocate one file per block. Allows for dynamic disk space consumption.
#define STXXL_ERRMSG(x)
Definition: verbose.h:79
#define STXXL_THROW_ERRNO(exception_type, error_message)
Throws exception_type with &quot;Error in [function] : [error_message] : [errno message]&quot;.
request::offset_type offset_type
the offset of a request, also the size of the file
Definition: file.h:64
Request with basic properties like file and offset.
Definition: request.h:39
virtual void lock()
Locks file for reading and writing (acquires a lock in the file system).
std::string filename_for_block(offset_type offset)
Constructs a file name for a given block.
const char * io_type() const
Identifies the type of I/O implementation.
virtual void serve()=0
#define STXXL_END_NAMESPACE
Definition: namespace.h:17