Use std::vector instead of std::map for ContextAttribute objects

Now key for ContextAttribute object store is int instead of string.
pull/28/head
Tatsuhiro Tsujikawa 2012-09-22 17:37:30 +09:00
parent 0030025bb6
commit e2340efe27
25 changed files with 116 additions and 68 deletions

View File

@ -100,7 +100,7 @@ bool BtDependency::resolve()
dependee->getPieceStorage()->getDiskAdaptor();
diskAdaptor->openExistingFile();
std::string content = util::toString(diskAdaptor);
if(dependee->getDownloadContext()->hasAttribute(bittorrent::BITTORRENT)) {
if(dependee->getDownloadContext()->hasAttribute(CTX_ATTR_BT)) {
SharedHandle<TorrentAttribute> attrs =
bittorrent::getTorrentAttrs(dependee->getDownloadContext());
bittorrent::loadFromMemory

View File

@ -94,7 +94,7 @@ void BtSetup::setup(std::vector<Command*>& commands,
DownloadEngine* e,
const Option* option)
{
if(!requestGroup->getDownloadContext()->hasAttribute(bittorrent::BITTORRENT)){
if(!requestGroup->getDownloadContext()->hasAttribute(CTX_ATTR_BT)){
return;
}
SharedHandle<TorrentAttribute> torrentAttrs =

View File

@ -110,7 +110,7 @@ void printProgress
<< "#" << rg->getGID() << " ";
#ifdef ENABLE_BITTORRENT
if(rg->getDownloadContext()->hasAttribute(bittorrent::BITTORRENT) &&
if(rg->getDownloadContext()->hasAttribute(CTX_ATTR_BT) &&
!bittorrent::getTorrentAttrs(rg->getDownloadContext())->metadata.empty() &&
rg->downloadFinished()) {
o << "SEEDING" << "(" << "ratio:";

49
src/ContextAttribute.cc Normal file
View File

@ -0,0 +1,49 @@
/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2012 Tatsuhiro Tsujikawa
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*/
/* copyright --> */
#include "ContextAttribute.h"
namespace aria2 {
const char* strContextAttributeType(ContextAttributeType key)
{
switch(key) {
case CTX_ATTR_BT:
return "BitTorrent";
default:
return "UNKNOWN";
}
}
} // namespace aria2

View File

@ -43,6 +43,17 @@ struct ContextAttribute {
virtual ~ContextAttribute() {}
};
enum ContextAttributeType {
// For BitTorrent
CTX_ATTR_BT,
// Max value of attribute type to use allocate vector to hold
// attributes.
MAX_CTX_ATTR
};
// Returns human readable string representation of type |key|
const char* strContextAttributeType(ContextAttributeType key);
} // namespace aria2
#endif // D_CONTEXT_ATTRIBUTE_H

View File

@ -465,7 +465,7 @@ void DefaultPieceStorage::completePiece(const SharedHandle<Piece>& piece)
A2_LOG_INFO(MSG_DOWNLOAD_COMPLETED);
}
#ifdef ENABLE_BITTORRENT
if(downloadContext_->hasAttribute(bittorrent::BITTORRENT)) {
if(downloadContext_->hasAttribute(CTX_ATTR_BT)) {
SharedHandle<TorrentAttribute> torrentAttrs =
bittorrent::getTorrentAttrs(downloadContext_);
if(!torrentAttrs->metadata.empty()) {

View File

@ -227,7 +227,7 @@ bool DownloadCommand::executeInternal() {
if(
#ifdef ENABLE_BITTORRENT
(!getPieceStorage()->isEndGame() ||
!getDownloadContext()->hasAttribute(bittorrent::BITTORRENT)) &&
!getDownloadContext()->hasAttribute(CTX_ATTR_BT)) &&
#endif // ENABLE_BITTORRENT
segment->isHashCalculated()) {
A2_LOG_DEBUG(fmt("Hash is available! index=%lu",

View File

@ -43,7 +43,6 @@
#include "DlAbortEx.h"
#include "a2functional.h"
#include "Signature.h"
#include "ContextAttribute.h"
namespace aria2 {
@ -52,6 +51,7 @@ DownloadContext::DownloadContext():
checksumVerified_(false),
knowsTotalLength_(true),
ownerRequestGroup_(0),
attrs_(MAX_CTX_ATTR),
downloadStartTime_(0),
downloadStopTime_(downloadStartTime_),
metalinkServerContacted_(false) {}
@ -63,6 +63,7 @@ DownloadContext::DownloadContext(int32_t pieceLength,
checksumVerified_(false),
knowsTotalLength_(true),
ownerRequestGroup_(0),
attrs_(MAX_CTX_ATTR),
downloadStartTime_(0),
downloadStopTime_(0),
metalinkServerContacted_(false)
@ -151,32 +152,29 @@ void DownloadContext::setFileFilter(SegList<int>& sgl)
}
void DownloadContext::setAttribute
(const std::string& key, const SharedHandle<ContextAttribute>& value)
(ContextAttributeType key, const SharedHandle<ContextAttribute>& value)
{
std::map<std::string, SharedHandle<ContextAttribute> >::value_type p =
std::make_pair(key, value);
std::pair<std::map<std::string, SharedHandle<ContextAttribute> >::iterator,
bool> r = attrs_.insert(p);
if(!r.second) {
(*r.first).second = value;
}
assert(key < MAX_CTX_ATTR);
attrs_[key] = value;
}
const SharedHandle<ContextAttribute>& DownloadContext::getAttribute
(const std::string& key)
(ContextAttributeType key)
{
std::map<std::string, SharedHandle<ContextAttribute> >::const_iterator itr =
attrs_.find(key);
if(itr == attrs_.end()) {
throw DL_ABORT_EX(fmt("No attribute named %s", key.c_str()));
assert(key < MAX_CTX_ATTR);
const SharedHandle<ContextAttribute>& attr = attrs_[key];
if(attr) {
return attr;
} else {
return (*itr).second;
throw DL_ABORT_EX(fmt("No attribute named %s",
strContextAttributeType(key)));
}
}
bool DownloadContext::hasAttribute(const std::string& key) const
bool DownloadContext::hasAttribute(ContextAttributeType key) const
{
return attrs_.count(key) == 1;
assert(key < MAX_CTX_ATTR);
return attrs_[key];
}
void DownloadContext::releaseRuntimeResource()

View File

@ -46,13 +46,13 @@
#include "A2STR.h"
#include "ValueBase.h"
#include "SegList.h"
#include "ContextAttribute.h"
namespace aria2 {
class RequestGroup;
class Signature;
class FileEntry;
struct ContextAttribute;
class DownloadContext
{
@ -77,7 +77,7 @@ private:
RequestGroup* ownerRequestGroup_;
std::map<std::string, SharedHandle<ContextAttribute> > attrs_;
std::vector<SharedHandle<ContextAttribute> > attrs_;
Timer downloadStartTime_;
@ -203,11 +203,11 @@ public:
}
void setAttribute
(const std::string& key, const SharedHandle<ContextAttribute>& value);
(ContextAttributeType key, const SharedHandle<ContextAttribute>& value);
const SharedHandle<ContextAttribute>& getAttribute(const std::string& key);
const SharedHandle<ContextAttribute>& getAttribute(ContextAttributeType key);
bool hasAttribute(const std::string& key) const;
bool hasAttribute(ContextAttributeType key) const;
void resetDownloadStartTime();

View File

@ -189,7 +189,7 @@ SRCS = Socket.h\
Event.h\
timespec.h\
ValueBase.cc ValueBase.h\
ContextAttribute.h\
ContextAttribute.cc ContextAttribute.h\
TorrentAttribute.cc TorrentAttribute.h\
AdaptiveFileAllocationIterator.cc AdaptiveFileAllocationIterator.h\
TruncFileAllocationIterator.cc TruncFileAllocationIterator.h\

View File

@ -283,7 +283,7 @@ void RequestGroup::createInitialCommand
downloadContext_->resetDownloadStartTime();
#ifdef ENABLE_BITTORRENT
{
if(downloadContext_->hasAttribute(bittorrent::BITTORRENT)) {
if(downloadContext_->hasAttribute(CTX_ATTR_BT)) {
SharedHandle<TorrentAttribute> torrentAttrs =
bittorrent::getTorrentAttrs(downloadContext_);
bool metadataGetMode = torrentAttrs->metadata.empty();
@ -584,14 +584,14 @@ void RequestGroup::initPieceStorage()
// content-length = 0. Google's dl server used this before.
(downloadContext_->getTotalLength() > 0
#ifdef ENABLE_BITTORRENT
|| downloadContext_->hasAttribute(bittorrent::BITTORRENT)
|| downloadContext_->hasAttribute(CTX_ATTR_BT)
#endif // ENABLE_BITTORRENT
)) {
#ifdef ENABLE_BITTORRENT
DefaultPieceStorage* ps =
new DefaultPieceStorage(downloadContext_, option_.get());
SharedHandle<PieceStorage> psHolder(ps);
if(downloadContext_->hasAttribute(bittorrent::BITTORRENT)) {
if(downloadContext_->hasAttribute(CTX_ATTR_BT)) {
if(isUriSuppliedForRequsetFileEntry
(downloadContext_->getFileEntries().begin(),
downloadContext_->getFileEntries().end())) {
@ -1179,7 +1179,7 @@ DownloadResultHandle RequestGroup::createDownloadResult() const
}
}
#ifdef ENABLE_BITTORRENT
if(downloadContext_->hasAttribute(bittorrent::BITTORRENT)) {
if(downloadContext_->hasAttribute(CTX_ATTR_BT)) {
const unsigned char* p = bittorrent::getInfoHash(downloadContext_);
res->infoHash.assign(p, p+INFO_HASH_LENGTH);
}
@ -1196,7 +1196,7 @@ void RequestGroup::reportDownloadFinished()
downloadContext_->getBasePath().c_str()));
uriSelector_->resetCounters();
#ifdef ENABLE_BITTORRENT
if(downloadContext_->hasAttribute(bittorrent::BITTORRENT)) {
if(downloadContext_->hasAttribute(CTX_ATTR_BT)) {
TransferStat stat = calculateStat();
int64_t completedLength = getCompletedLength();
double shareRatio = completedLength == 0 ? 0.0 :
@ -1309,7 +1309,7 @@ void RequestGroup::setDownloadContext
bool RequestGroup::p2pInvolved() const
{
#ifdef ENABLE_BITTORRENT
return downloadContext_->hasAttribute(bittorrent::BITTORRENT);
return downloadContext_->hasAttribute(CTX_ATTR_BT);
#else // !ENABLE_BITTORRENT
return false;
#endif // !ENABLE_BITTORRENT

View File

@ -423,7 +423,7 @@ public:
// we don't remove it.
if(group->getOption()->getAsBool(PREF_BT_REMOVE_UNSELECTED_FILE) &&
!group->inMemoryDownload() &&
dctx->hasAttribute(bittorrent::BITTORRENT)) {
dctx->hasAttribute(CTX_ATTR_BT)) {
A2_LOG_INFO(fmt(MSG_REMOVING_UNSELECTED_FILE, group->getGID()));
const std::vector<SharedHandle<FileEntry> >& files =
dctx->getFileEntries();

View File

@ -801,7 +801,7 @@ void gatherProgress
{
gatherProgressCommon(entryDict, group, keys);
#ifdef ENABLE_BITTORRENT
if(group->getDownloadContext()->hasAttribute(bittorrent::BITTORRENT)) {
if(group->getDownloadContext()->hasAttribute(CTX_ATTR_BT)) {
SharedHandle<TorrentAttribute> torrentAttrs =
bittorrent::getTorrentAttrs(group->getDownloadContext());
const SharedHandle<BtObject>& btObject =
@ -1117,7 +1117,7 @@ void changeOption
if(option.defined(PREF_DIR) || option.defined(PREF_OUT)) {
if(dctx->getFileEntries().size() == 1
#ifdef ENABLE_BITTORRENT
&& !dctx->hasAttribute(bittorrent::BITTORRENT)
&& !dctx->hasAttribute(CTX_ATTR_BT)
#endif // ENABLE_BITTORRENT
) {
dctx->getFirstFileEntry()->setPath
@ -1127,7 +1127,7 @@ void changeOption
}
#ifdef ENABLE_BITTORRENT
if(option.defined(PREF_DIR) || option.defined(PREF_INDEX_OUT)) {
if(dctx->hasAttribute(bittorrent::BITTORRENT)) {
if(dctx->hasAttribute(CTX_ATTR_BT)) {
std::istringstream indexOutIn(grOption->get(PREF_INDEX_OUT));
std::vector<std::pair<size_t, std::string> > indexPaths =
util::createIndexPaths(indexOutIn);

View File

@ -58,7 +58,7 @@ bool UTMetadataPostDownloadHandler::Criteria::match
{
const SharedHandle<DownloadContext>& dctx =
requestGroup->getDownloadContext();
if(dctx->hasAttribute(bittorrent::BITTORRENT)) {
if(dctx->hasAttribute(CTX_ATTR_BT)) {
SharedHandle<TorrentAttribute> attrs = bittorrent::getTorrentAttrs(dctx);
if(attrs->metadata.empty()) {
return true;

View File

@ -107,8 +107,6 @@ const std::string C_CREATED_BY("created by");
const std::string DEFAULT_PEER_ID_PREFIX("aria2-");
} // namespace
const std::string BITTORRENT("bittorrent");
const std::string MULTI("multi");
const std::string SINGLE("single");
@ -515,7 +513,7 @@ void processRootDictionary
torrent->createdBy = util::encodeNonUtf8(createdBy->s());
}
ctx->setAttribute(BITTORRENT, torrent);
ctx->setAttribute(CTX_ATTR_BT, torrent);
}
} // namespace
@ -626,7 +624,7 @@ void loadFromMemory(const SharedHandle<ValueBase>& torrent,
SharedHandle<TorrentAttribute> getTorrentAttrs
(const SharedHandle<DownloadContext>& dctx)
{
return static_pointer_cast<TorrentAttribute>(dctx->getAttribute(BITTORRENT));
return static_pointer_cast<TorrentAttribute>(dctx->getAttribute(CTX_ATTR_BT));
}
const unsigned char*
@ -943,7 +941,7 @@ void loadMagnet
(const std::string& magnet, const SharedHandle<DownloadContext>& dctx)
{
SharedHandle<TorrentAttribute> attrs = parseMagnet(magnet);
dctx->setAttribute(BITTORRENT, attrs);
dctx->setAttribute(CTX_ATTR_BT, attrs);
}
std::string metadata2Torrent

View File

@ -64,9 +64,6 @@ extern const std::string SINGLE;
extern const std::string MULTI;
extern const std::string BITTORRENT;
void load(const std::string& torrentFile,
const SharedHandle<DownloadContext>& ctx,
const SharedHandle<Option>& option,

View File

@ -130,8 +130,7 @@ void BtDependencyTest::testResolve_originalNameNoMatch()
BtDependency dep(dependant.get(), dependee);
CPPUNIT_ASSERT(dep.resolve());
CPPUNIT_ASSERT(!dependant->getDownloadContext()->hasAttribute
(bittorrent::BITTORRENT));
CPPUNIT_ASSERT(!dependant->getDownloadContext()->hasAttribute(CTX_ATTR_BT));
}
void BtDependencyTest::testResolve_singleFileWithoutOriginalName()
@ -145,8 +144,7 @@ void BtDependencyTest::testResolve_singleFileWithoutOriginalName()
dependee->getPieceStorage()->markAllPiecesDone();
BtDependency dep(dependant.get(), dependee);
CPPUNIT_ASSERT(dep.resolve());
CPPUNIT_ASSERT(dependant->getDownloadContext()->hasAttribute
(bittorrent::BITTORRENT));
CPPUNIT_ASSERT(dependant->getDownloadContext()->hasAttribute(CTX_ATTR_BT));
}
void BtDependencyTest::testResolve_multiFile()
@ -163,8 +161,7 @@ void BtDependencyTest::testResolve_multiFile()
BtDependency dep(dependant.get(), dependee);
CPPUNIT_ASSERT(dep.resolve());
CPPUNIT_ASSERT(dependant->getDownloadContext()->hasAttribute
(bittorrent::BITTORRENT));
CPPUNIT_ASSERT(dependant->getDownloadContext()->hasAttribute(CTX_ATTR_BT));
const std::vector<SharedHandle<FileEntry> >& fileEntries =
dependant->getDownloadContext()->getFileEntries();
@ -194,7 +191,7 @@ void BtDependencyTest::testResolve_metadata()
pieceStorage->setDownloadFinished(true);
dependee->setPieceStorage(pieceStorage);
SharedHandle<TorrentAttribute> attrs(new TorrentAttribute());
dependee->getDownloadContext()->setAttribute(bittorrent::BITTORRENT, attrs);
dependee->getDownloadContext()->setAttribute(CTX_ATTR_BT, attrs);
BtDependency dep(dependant.get(), dependee);
CPPUNIT_ASSERT(dep.resolve());
@ -217,7 +214,7 @@ void BtDependencyTest::testResolve_loadError()
CPPUNIT_ASSERT(dep.resolve());
CPPUNIT_ASSERT
(!dependant->getDownloadContext()->hasAttribute(bittorrent::BITTORRENT));
(!dependant->getDownloadContext()->hasAttribute(CTX_ATTR_BT));
CPPUNIT_ASSERT_EQUAL(std::string("/tmp/outfile.path"),
dependant->getFirstFilePath());
}
@ -231,7 +228,7 @@ void BtDependencyTest::testResolve_dependeeFailure()
CPPUNIT_ASSERT(dep.resolve());
CPPUNIT_ASSERT
(!dependant->getDownloadContext()->hasAttribute(bittorrent::BITTORRENT));
(!dependant->getDownloadContext()->hasAttribute(CTX_ATTR_BT));
CPPUNIT_ASSERT_EQUAL(std::string("/tmp/outfile.path"),
dependant->getFirstFilePath());
}

View File

@ -69,10 +69,8 @@ void BtRegistryTest::testGetDownloadContext_infoHash()
attrs1->infoHash = "hash1";
SharedHandle<TorrentAttribute> attrs2(new TorrentAttribute());
attrs2->infoHash = "hash2";
btRegistry.getDownloadContext(1)->setAttribute
(bittorrent::BITTORRENT, attrs1);
btRegistry.getDownloadContext(2)->setAttribute
(bittorrent::BITTORRENT, attrs2);
btRegistry.getDownloadContext(1)->setAttribute(CTX_ATTR_BT, attrs1);
btRegistry.getDownloadContext(2)->setAttribute(CTX_ATTR_BT, attrs2);
CPPUNIT_ASSERT(btRegistry.getDownloadContext("hash1"));
CPPUNIT_ASSERT(btRegistry.getDownloadContext("hash1").get() ==

View File

@ -59,7 +59,7 @@ public:
dctx_.reset(new DownloadContext(pieceLength, totalLength));
SharedHandle<TorrentAttribute> torrentAttrs(new TorrentAttribute());
torrentAttrs->infoHash = std::string(vbegin(infoHash), vend(infoHash));
dctx_->setAttribute(bittorrent::BITTORRENT, torrentAttrs);
dctx_->setAttribute(CTX_ATTR_BT, torrentAttrs);
bittorrent::setStaticPeerId(peerId);
pieceStorage_.reset(new MockPieceStorage());

View File

@ -72,7 +72,7 @@ public:
dctx_.reset(new DownloadContext());
SharedHandle<TorrentAttribute> torrentAttrs(new TorrentAttribute());
torrentAttrs->infoHash = std::string(vbegin(infoHash), vend(infoHash));
dctx_->setAttribute(bittorrent::BITTORRENT, torrentAttrs);
dctx_->setAttribute(CTX_ATTR_BT, torrentAttrs);
const SharedHandle<FileEntry> fileEntries[] = {
SharedHandle<FileEntry>(new FileEntry("/path/to/file",totalLength,0))
};

View File

@ -97,7 +97,7 @@ void HandshakeExtensionMessageTest::testDoReceivedAction()
rg.setDownloadContext(dctx);
SharedHandle<TorrentAttribute> attrs(new TorrentAttribute());
dctx->setAttribute(bittorrent::BITTORRENT, attrs);
dctx->setAttribute(CTX_ATTR_BT, attrs);
dctx->markTotalLengthIsUnknown();
SharedHandle<Peer> peer(new Peer("192.168.0.1", 0));

View File

@ -35,7 +35,7 @@ public:
memset(infoHash, 0, sizeof(infoHash));
SharedHandle<TorrentAttribute> torrentAttrs(new TorrentAttribute());
torrentAttrs->infoHash = std::string(vbegin(infoHash), vend(infoHash));
dctx_->setAttribute(bittorrent::BITTORRENT, torrentAttrs);
dctx_->setAttribute(CTX_ATTR_BT, torrentAttrs);
}
void testHandshake();

View File

@ -84,7 +84,7 @@ void UTMetadataDataExtensionMessageTest::testDoReceivedAction()
MessageDigest::sha1(),
metadata.data(), metadata.size());
attrs->infoHash = std::string(&infoHash[0], &infoHash[20]);
dctx->setAttribute(bittorrent::BITTORRENT, attrs);
dctx->setAttribute(CTX_ATTR_BT, attrs);
UTMetadataDataExtensionMessage m(1);
m.setPieceStorage(pieceStorage);

View File

@ -53,7 +53,7 @@ void UTMetadataPostDownloadHandlerTest::testCanHandle()
CPPUNIT_ASSERT(!handler.canHandle(requestGroup_.get()));
SharedHandle<TorrentAttribute> attrs(new TorrentAttribute());
dctx_->setAttribute(bittorrent::BITTORRENT, attrs);
dctx_->setAttribute(CTX_ATTR_BT, attrs);
CPPUNIT_ASSERT(handler.canHandle(requestGroup_.get()));
@ -84,7 +84,7 @@ void UTMetadataPostDownloadHandlerTest::testGetNextRequestGroups()
announceTier.push_back("http://tracker");
announceList.push_back(announceTier);
attrs->announceList = announceList;
dctx_->setAttribute(bittorrent::BITTORRENT, attrs);
dctx_->setAttribute(CTX_ATTR_BT, attrs);
requestGroup_->setDiskWriterFactory
(SharedHandle<DiskWriterFactory>(new ByteArrayDiskWriterFactory()));
requestGroup_->initPieceStorage();

View File

@ -41,7 +41,7 @@ public:
dispatcher_.reset(new MockBtMessageDispatcher());
dctx_.reset(new DownloadContext());
SharedHandle<TorrentAttribute> attrs(new TorrentAttribute());
dctx_->setAttribute(bittorrent::BITTORRENT, attrs);
dctx_->setAttribute(CTX_ATTR_BT, attrs);
peer_.reset(new Peer("host", 6880));
peer_->allocateSessionResource(0, 0);
peer_->setExtension("ut_metadata", 1);