1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
#include "qpid/linearstore/journal/EmptyFilePoolPartition.h"
#include <dirent.h>
#include <iomanip>
#include "qpid/linearstore/journal/jdir.h"
#include "qpid/linearstore/journal/jerrno.h"
#include "qpid/linearstore/journal/jexception.h"
#include "qpid/linearstore/journal/slock.h"
//#include <iostream> // DEBUG
namespace qpid {
namespace linearstore {
namespace journal {
// static
const std::string EmptyFilePoolPartition::s_efpTopLevelDir_("efp"); // Sets the top-level efp dir within a partition
EmptyFilePoolPartition::EmptyFilePoolPartition(const efpPartitionNumber_t partitionNum,
const std::string& partitionDir,
JournalLog& journalLogRef) :
partitionNum_(partitionNum),
partitionDir_(partitionDir),
journalLogRef_(journalLogRef)
{
validatePartitionDir();
}
EmptyFilePoolPartition::~EmptyFilePoolPartition() {
slock l(efpMapMutex_);
for (efpMapItr_t i = efpMap_.begin(); i != efpMap_.end(); ++i) {
delete i->second;
}
efpMap_.clear();
}
void
EmptyFilePoolPartition::findEmptyFilePools() {
//std::cout << "Reading " << partitionDir << std::endl; // DEBUG
std::vector<std::string> dirList;
jdir::read_dir(partitionDir_, dirList, true, false, false, false);
bool foundEfpDir = false;
for (std::vector<std::string>::iterator i = dirList.begin(); i != dirList.end(); ++i) {
if (i->compare(s_efpTopLevelDir_) == 0) {
foundEfpDir = true;
break;
}
}
if (foundEfpDir) {
std::string efpDir(partitionDir_ + "/" + s_efpTopLevelDir_);
//std::cout << "Reading " << efpDir << std::endl; // DEBUG
dirList.clear();
jdir::read_dir(efpDir, dirList, true, false, false, true);
for (std::vector<std::string>::iterator i = dirList.begin(); i != dirList.end(); ++i) {
EmptyFilePool* efpp = 0;
try {
efpp = new EmptyFilePool(*i, this, journalLogRef_);
{
slock l(efpMapMutex_);
efpMap_[efpp->dataSize_kib()] = efpp;
}
}
catch (const std::exception& e) {
if (efpp != 0) {
delete efpp;
efpp = 0;
}
//std::cerr << "WARNING: " << e.what() << std::endl;
}
if (efpp != 0)
efpp->initialize();
}
}
}
EmptyFilePool* EmptyFilePoolPartition::getEmptyFilePool(const efpDataSize_kib_t efpDataSize_kib) {
efpMapItr_t i = efpMap_.find(efpDataSize_kib);
if (i == efpMap_.end())
return 0;
return i->second;
}
void EmptyFilePoolPartition::getEmptyFilePools(std::vector<EmptyFilePool*>& efpList) {
for (efpMapItr_t i=efpMap_.begin(); i!=efpMap_.end(); ++i) {
efpList.push_back(i->second);
}
}
void EmptyFilePoolPartition::getEmptyFilePoolSizes_kib(std::vector<efpDataSize_kib_t>& efpDataSizesList_kib) const {
for (efpMapConstItr_t i=efpMap_.begin(); i!=efpMap_.end(); ++i) {
efpDataSizesList_kib.push_back(i->first);
}
}
std::string EmptyFilePoolPartition::getPartitionDirectory() const {
return partitionDir_;
}
efpPartitionNumber_t EmptyFilePoolPartition::getPartitionNumber() const {
return partitionNum_;
}
// static
std::string EmptyFilePoolPartition::getPartionDirectoryName(const efpPartitionNumber_t partitionNumber) {
std::ostringstream oss;
oss << "p" << std::setfill('0') << std::setw(3) << partitionNumber;
return oss.str();
}
//static
efpPartitionNumber_t EmptyFilePoolPartition::getPartitionNumber(const std::string& name) {
if (name.length() == 4 && name[0] == 'p' && ::isdigit(name[1]) && ::isdigit(name[2]) && ::isdigit(name[3])) {
long pn = ::strtol(name.c_str() + 1, 0, 0);
if (pn == 0 && errno) {
return 0;
} else {
return (efpPartitionNumber_t)pn;
}
}
return 0;
}
// --- protected functions ---
void EmptyFilePoolPartition::validatePartitionDir() {
if (!jdir::is_dir(partitionDir_)) {
std::ostringstream ss;
ss << "Invalid partition directory: \'" << partitionDir_ << "\' is not a directory";
throw jexception(jerrno::JERR_EFP_BADPARTITIONDIR, ss.str(), "EmptyFilePoolPartition", "validatePartitionDir");
}
// TODO: other validity checks here
}
}}}
|