From: Loic Dachary Date: Wed, 18 Dec 2013 09:00:30 +0000 (+0100) Subject: erasure-code: refactor the example to use chunk size helpers X-Git-Tag: v0.78~335^2~2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=eb2374ea21594ee41f3903dbacab16ca5b14b386;p=ceph.git erasure-code: refactor the example to use chunk size helpers The get_chunk_size and get_chunk_count methods are implemented using the code found in the encode() method. The encode method is modified to use the chunk size helpers. The unit tests are modified to use the helpers. Signed-off-by: Loic Dachary --- diff --git a/src/test/osd/ErasureCodeExample.h b/src/test/osd/ErasureCodeExample.h index 0710189cba4..ab2bcf02909 100644 --- a/src/test/osd/ErasureCodeExample.h +++ b/src/test/osd/ErasureCodeExample.h @@ -81,6 +81,18 @@ public: return minimum_to_decode(want_to_read, available_chunks, minimum); } + virtual unsigned int get_chunk_count() const { + return DATA_CHUNKS + CODING_CHUNKS; + } + + virtual unsigned int get_data_chunk_count() const { + return DATA_CHUNKS; + } + + virtual unsigned int get_chunk_size(unsigned int object_size) const { + return ( object_size / DATA_CHUNKS ) + 1; + } + virtual int encode(const set &want_to_encode, const bufferlist &in, map *encoded) { @@ -88,11 +100,11 @@ public: // make sure all data chunks have the same length, allocating // padding if necessary. // - unsigned chunk_length = ( in.length() / DATA_CHUNKS ) + 1; - unsigned length = chunk_length * ( DATA_CHUNKS + CODING_CHUNKS ); + unsigned int chunk_length = get_chunk_size(in.length()); bufferlist out(in); - bufferptr pad(length - in.length()); - pad.zero(0, DATA_CHUNKS); + unsigned int width = get_chunk_count() * get_chunk_size(in.length()); + bufferptr pad(width - in.length()); + pad.zero(0, get_data_chunk_count()); out.push_back(pad); // // compute the coding chunk with first chunk ^ second chunk diff --git a/src/test/osd/TestErasureCodeExample.cc b/src/test/osd/TestErasureCodeExample.cc index f12e80c8cd0..17e77a0bdaa 100644 --- a/src/test/osd/TestErasureCodeExample.cc +++ b/src/test/osd/TestErasureCodeExample.cc @@ -20,6 +20,13 @@ #include "global/global_context.h" #include "gtest/gtest.h" +TEST(ErasureCodeExample, chunk_size) +{ + ErasureCodeExample example; + EXPECT_EQ(3u, example.get_chunk_count()); + EXPECT_EQ(11u, example.get_chunk_size(20)); +} + TEST(ErasureCodeExample, minimum_to_decode) { ErasureCodeExample example; @@ -105,13 +112,13 @@ TEST(ErasureCodeExample, encode_decode) bufferlist in; in.append("ABCDE"); - int want_to_encode[] = { 0, 1, 2 }; + set want_to_encode; + for(unsigned int i = 0; i < example.get_chunk_count(); i++) + want_to_encode.insert(i); map encoded; - EXPECT_EQ(0, example.encode(set(want_to_encode, want_to_encode+3), - in, - &encoded)); - EXPECT_EQ(3u, encoded.size()); - EXPECT_EQ(3u, encoded[0].length()); + EXPECT_EQ(0, example.encode(want_to_encode, in, &encoded)); + EXPECT_EQ(example.get_chunk_count(), encoded.size()); + EXPECT_EQ(example.get_chunk_size(in.length()), encoded[0].length()); EXPECT_EQ('A', encoded[0][0]); EXPECT_EQ('B', encoded[0][1]); EXPECT_EQ('C', encoded[0][2]);