Forráskód Böngészése

Added GeomStreamPieceSelector.

Tatsuhiro Tsujikawa 14 éve
szülő
commit
201af99501

+ 58 - 0
src/BitfieldMan.cc

@@ -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

+ 6 - 0
src/BitfieldMan.h

@@ -135,6 +135,12 @@ public:
    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

+ 57 - 0
src/GeomStreamPieceSelector.cc

@@ -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 - 0
src/GeomStreamPieceSelector.h

@@ -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 - 0
src/Makefile.am

@@ -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\

+ 54 - 0
test/BitfieldManTest.cc

@@ -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();
@@ -66,6 +67,7 @@ public:
   void testZeroLengthFilter();
   void testGetFirstNMissingUnusedIndex();
   void testGetInorderMissingUnusedIndex();
+  void testGetGeomMissingUnusedIndex();
 };
 
 
@@ -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