Skip to content

Commit

Permalink
Added GeomStreamPieceSelector.
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsuhiro-t committed Aug 24, 2011
1 parent 0f72cba commit 201af99
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/BitfieldMan.cc
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,64 @@ bool BitfieldMan::getSparseMissingUnusedIndex
}
}

namespace {
template<typename Array>
bool getGeomMissingUnusedIndex
(size_t& index,
size_t minSplitSize,
const Array& bitfield,
const unsigned char* useBitfield,
size_t blockLength,
size_t blocks)
{
const size_t base = 2;
size_t start = 0;
size_t end = 1;
while(start < blocks) {
index = blocks;
bool ok = false;
for(size_t i = start, eoi = std::min(blocks, end); i < eoi; ++i) {
if(bitfield::test(useBitfield, blocks, i)) {
ok = false;
break;
} else if(index == blocks && !bitfield::test(bitfield, blocks, i)) {
ok = true;
index = i;
}
}
if(ok) {
return true;
} else {
start = end;
end *= base;
}
}
return getSparseMissingUnusedIndex(index, minSplitSize,
bitfield, useBitfield,
blockLength, blocks);
}
} // namespace

bool BitfieldMan::getGeomMissingUnusedIndex
(size_t& index,
size_t minSplitSize,
const unsigned char* ignoreBitfield,
size_t ignoreBitfieldLength) const
{
if(filterEnabled_) {
return aria2::getGeomMissingUnusedIndex
(index, minSplitSize,
array(ignoreBitfield)|~array(filterBitfield_)|
array(bitfield_)|array(useBitfield_),
useBitfield_, blockLength_, blocks_);
} else {
return aria2::getGeomMissingUnusedIndex
(index, minSplitSize,
array(ignoreBitfield)|array(bitfield_)|array(useBitfield_),
useBitfield_, blockLength_, blocks_);
}
}

namespace {
template<typename Array>
bool getInorderMissingUnusedIndex
Expand Down
6 changes: 6 additions & 0 deletions src/BitfieldMan.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ class BitfieldMan {
const unsigned char* ignoreBitfield,
size_t ignoreBitfieldLength) const;

bool getGeomMissingUnusedIndex
(size_t& index,
size_t minSplitSize,
const unsigned char* ignoreBitfield,
size_t ignoreBitfieldLength) const;

// Stores missing bit index to index. This function selects smallest
// index of missing piece, considering minSplitSize. Set bits in
// ignoreBitfield are excluded. Returns true if such bit index is
Expand Down
57 changes: 57 additions & 0 deletions src/GeomStreamPieceSelector.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2011 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 "GeomStreamPieceSelector.h"
#include "BitfieldMan.h"

namespace aria2 {

GeomStreamPieceSelector::GeomStreamPieceSelector
(BitfieldMan* bitfieldMan)
: bitfieldMan_(bitfieldMan)
{}

GeomStreamPieceSelector::~GeomStreamPieceSelector() {}

bool GeomStreamPieceSelector::select
(size_t& index,
size_t minSplitSize,
const unsigned char* ignoreBitfield,
size_t length)
{
return bitfieldMan_->getGeomMissingUnusedIndex
(index, minSplitSize, ignoreBitfield, length);
}

} // namespace aria2
60 changes: 60 additions & 0 deletions src/GeomStreamPieceSelector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2011 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 --> */
#ifndef D_GEOM_STREAM_PIECE_SELECTOR_H
#define D_GEOM_STREAM_PIECE_SELECTOR_H

#include "StreamPieceSelector.h"

namespace aria2 {

class BitfieldMan;

class GeomStreamPieceSelector:public StreamPieceSelector {
public:
GeomStreamPieceSelector(BitfieldMan* bitfieldMan);
virtual ~GeomStreamPieceSelector();

virtual bool select
(size_t& index,
size_t minSplitSize,
const unsigned char* ignoreBitfield,
size_t length);
private:
BitfieldMan* bitfieldMan_;
};

} // namespace aria2

#endif // D_GEOM_STREAM_PIECE_SELECTOR_H
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ SRCS = Socket.h\
StreamPieceSelector.h\
DefaultStreamPieceSelector.cc DefaultStreamPieceSelector.h\
InorderStreamPieceSelector.cc InorderStreamPieceSelector.h\
GeomStreamPieceSelector.cc GeomStreamPieceSelector.h\
MetalinkHttpEntry.cc MetalinkHttpEntry.h\
OutputFile.h\
NullOutputFile.h\
Expand Down
54 changes: 54 additions & 0 deletions test/BitfieldManTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class BitfieldManTest:public CppUnit::TestFixture {
CPPUNIT_TEST(testZeroLengthFilter);
CPPUNIT_TEST(testGetFirstNMissingUnusedIndex);
CPPUNIT_TEST(testGetInorderMissingUnusedIndex);
CPPUNIT_TEST(testGetGeomMissingUnusedIndex);
CPPUNIT_TEST_SUITE_END();
public:
void testGetBlockSize();
Expand Down Expand Up @@ -66,6 +67,7 @@ class BitfieldManTest:public CppUnit::TestFixture {
void testZeroLengthFilter();
void testGetFirstNMissingUnusedIndex();
void testGetInorderMissingUnusedIndex();
void testGetGeomMissingUnusedIndex();
};


Expand Down Expand Up @@ -730,4 +732,56 @@ void BitfieldManTest::testGetInorderMissingUnusedIndex()
CPPUNIT_ASSERT_EQUAL((size_t)3, index);
}

void BitfieldManTest::testGetGeomMissingUnusedIndex()
{
BitfieldMan bt(1024, 1024*20);
const size_t length = 3;
unsigned char ignoreBitfield[length];
memset(ignoreBitfield, 0, sizeof(ignoreBitfield));
size_t minSplitSize = 1024;
size_t index;
// 00000|00000|00000|00000
CPPUNIT_ASSERT
(bt.getGeomMissingUnusedIndex
(index, minSplitSize, ignoreBitfield, length));
CPPUNIT_ASSERT_EQUAL((size_t)0, index);
bt.setUseBit(0);
// 10000|00000|00000|00000
CPPUNIT_ASSERT
(bt.getGeomMissingUnusedIndex
(index, minSplitSize, ignoreBitfield, length));
CPPUNIT_ASSERT_EQUAL((size_t)1, index);
bt.setUseBit(1);
// 11000|00000|00000|00000
CPPUNIT_ASSERT
(bt.getGeomMissingUnusedIndex
(index, minSplitSize, ignoreBitfield, length));
CPPUNIT_ASSERT_EQUAL((size_t)2, index);
bt.setUseBit(2);
// 11100|00000|00000|00000
CPPUNIT_ASSERT
(bt.getGeomMissingUnusedIndex
(index, minSplitSize, ignoreBitfield, length));
CPPUNIT_ASSERT_EQUAL((size_t)4, index);
bt.setUseBit(4);
// 11110|00000|00000|00000
CPPUNIT_ASSERT
(bt.getGeomMissingUnusedIndex
(index, minSplitSize, ignoreBitfield, length));
CPPUNIT_ASSERT_EQUAL((size_t)8, index);
bt.setUseBit(8);
// 11110|00010|00000|00000
CPPUNIT_ASSERT
(bt.getGeomMissingUnusedIndex
(index, minSplitSize, ignoreBitfield, length));
CPPUNIT_ASSERT_EQUAL((size_t)16, index);
bt.setUseBit(16);
// 11110|00010|00000|01000
CPPUNIT_ASSERT
(bt.getGeomMissingUnusedIndex
(index, minSplitSize, ignoreBitfield, length));
CPPUNIT_ASSERT_EQUAL((size_t)12, index);
bt.setUseBit(12);
}

} // namespace aria2

0 comments on commit 201af99

Please sign in to comment.