You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by vi...@apache.org on 2013/03/06 03:38:11 UTC
svn commit: r1453142 - in
/incubator/mesos/trunk/third_party/libprocess/third_party/stout:
Makefile.am include/stout/bytes.hpp include/stout/duration.hpp
tests/tests.cpp
Author: vinodkone
Date: Wed Mar 6 02:38:10 2013
New Revision: 1453142
URL: http://svn.apache.org/r1453142
Log:
Added a Bytes abstraction.
From: Ben Mahler <be...@gmail.com>
Review: https://reviews.apache.org/r/9120
Added:
incubator/mesos/trunk/third_party/libprocess/third_party/stout/include/stout/bytes.hpp
Modified:
incubator/mesos/trunk/third_party/libprocess/third_party/stout/Makefile.am
incubator/mesos/trunk/third_party/libprocess/third_party/stout/include/stout/duration.hpp
incubator/mesos/trunk/third_party/libprocess/third_party/stout/tests/tests.cpp
Modified: incubator/mesos/trunk/third_party/libprocess/third_party/stout/Makefile.am
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/third_party/libprocess/third_party/stout/Makefile.am?rev=1453142&r1=1453141&r2=1453142&view=diff
==============================================================================
--- incubator/mesos/trunk/third_party/libprocess/third_party/stout/Makefile.am (original)
+++ incubator/mesos/trunk/third_party/libprocess/third_party/stout/Makefile.am Wed Mar 6 02:38:10 2013
@@ -5,6 +5,7 @@ ACLOCAL_AMFLAGS = -I m4
AUTOMAKE_OPTIONS = foreign
EXTRA_DIST = \
+ include/stout/bytes.hpp \
include/stout/cache.hpp \
include/stout/duration.hpp \
include/stout/error.hpp \
Added: incubator/mesos/trunk/third_party/libprocess/third_party/stout/include/stout/bytes.hpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/third_party/libprocess/third_party/stout/include/stout/bytes.hpp?rev=1453142&view=auto
==============================================================================
--- incubator/mesos/trunk/third_party/libprocess/third_party/stout/include/stout/bytes.hpp (added)
+++ incubator/mesos/trunk/third_party/libprocess/third_party/stout/include/stout/bytes.hpp Wed Mar 6 02:38:10 2013
@@ -0,0 +1,134 @@
+#ifndef __STOUT_BYTES_HPP__
+#define __STOUT_BYTES_HPP__
+
+#include <ctype.h> // For 'isdigit'.
+#include <stdint.h>
+
+#include <iomanip>
+#include <iostream>
+#include <string>
+
+#include "numify.hpp"
+#include "strings.hpp"
+#include "try.hpp"
+
+
+class Bytes
+{
+public:
+ static Try<Bytes> parse(const std::string& s)
+ {
+ size_t index = 0;
+
+ while (index < s.size()) {
+ if (isdigit(s[index])) {
+ index++;
+ continue;
+ } else if (s[index] == '.') {
+ return Error("Fractional bytes '" + s + "'");
+ }
+
+ Try<uint64_t> value = numify<uint64_t>(s.substr(0, index));
+
+ if (value.isError()) {
+ return Error(value.error());
+ }
+
+ const std::string& unit = strings::upper(s.substr(index));
+
+ if (unit == "B") {
+ return Bytes(value.get(), BYTES);
+ } else if (unit == "KB") {
+ return Bytes(value.get(), KILOBYTES);
+ } else if (unit == "MB") {
+ return Bytes(value.get(), MEGABYTES);
+ } else if (unit == "GB") {
+ return Bytes(value.get(), GIGABYTES);
+ } else if (unit == "TB") {
+ return Bytes(value.get(), TERABYTES);
+ } else {
+ return Error("Unknown bytes unit '" + unit + "'");
+ }
+ }
+ return Error("Invalid bytes '" + s + "'");
+ }
+
+ Bytes(uint64_t bytes = 0) : value(bytes) {}
+ Bytes(uint64_t _value, uint64_t _unit) : value(_value * _unit) {}
+
+ // TODO(bmahler): Consider killing kilobytes to terabyte helpers, given
+ // they implicitly lose precision if not careful.
+ uint64_t bytes() const { return value; }
+ uint64_t kilobytes() const { return value / KILOBYTES; }
+ uint64_t megabytes() const { return value / MEGABYTES; }
+ uint64_t gigabytes() const { return value / GIGABYTES; }
+ uint64_t terabytes() const { return value / TERABYTES; }
+
+ bool operator < (const Bytes& that) const { return value < that.value; }
+ bool operator <= (const Bytes& that) const { return value <= that.value; }
+ bool operator > (const Bytes& that) const { return value > that.value; }
+ bool operator >= (const Bytes& that) const { return value >= that.value; }
+ bool operator == (const Bytes& that) const { return value == that.value; }
+ bool operator != (const Bytes& that) const { return value != that.value; }
+
+ // TODO(bmahler): Overload arithmetic operators..
+
+protected:
+ static const uint64_t BYTES = 1;
+ static const uint64_t KILOBYTES = 1024 * BYTES;
+ static const uint64_t MEGABYTES = 1024 * KILOBYTES;
+ static const uint64_t GIGABYTES = 1024 * MEGABYTES;
+ static const uint64_t TERABYTES = 1024 * GIGABYTES;
+
+private:
+ uint64_t value;
+};
+
+
+class Kilobytes : public Bytes
+{
+public:
+ explicit Kilobytes(uint64_t value) : Bytes(value, KILOBYTES) {}
+};
+
+
+class Megabytes : public Bytes
+{
+public:
+ explicit Megabytes(uint64_t value) : Bytes(value, MEGABYTES) {}
+};
+
+
+class Gigabytes : public Bytes
+{
+public:
+ explicit Gigabytes(uint64_t value) : Bytes(value, GIGABYTES) {}
+};
+
+
+class Terabytes : public Bytes
+{
+public:
+ explicit Terabytes(uint64_t value) : Bytes(value, TERABYTES) {}
+};
+
+
+inline std::ostream& operator << (std::ostream& stream, const Bytes& bytes)
+{
+ // Only raise the unit when there is no loss of information.
+ if (bytes.bytes() == 0) {
+ return stream << bytes.bytes() << "B";
+ } else if (bytes.bytes() % 1024 != 0) {
+ return stream << bytes.bytes() << "B";
+ } else if (bytes.kilobytes() % 1024 != 0) {
+ return stream << bytes.kilobytes() << "KB";
+ } else if (bytes.megabytes() % 1024 != 0) {
+ return stream << bytes.megabytes() << "MB";
+ } else if (bytes.gigabytes() % 1024 != 0) {
+ return stream << bytes.gigabytes() << "GB";
+ } else {
+ return stream << bytes.terabytes() << "TB";
+ }
+}
+
+#endif // __STOUT_BYTES_HPP__
Modified: incubator/mesos/trunk/third_party/libprocess/third_party/stout/include/stout/duration.hpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/third_party/libprocess/third_party/stout/include/stout/duration.hpp?rev=1453142&r1=1453141&r2=1453142&view=diff
==============================================================================
--- incubator/mesos/trunk/third_party/libprocess/third_party/stout/include/stout/duration.hpp (original)
+++ incubator/mesos/trunk/third_party/libprocess/third_party/stout/include/stout/duration.hpp Wed Mar 6 02:38:10 2013
@@ -58,18 +58,18 @@ public:
Duration() : value(0.0) {}
- double ns() const { return value; }
- double us() const { return value / MICROSECONDS; }
- double ms() const { return value / MILLISECONDS; }
- double secs() const { return value / SECONDS; }
- double mins() const { return value / MINUTES; }
- double hrs() const { return value / HOURS; }
- double days() const { return value / DAYS; }
+ double ns() const { return value; }
+ double us() const { return value / MICROSECONDS; }
+ double ms() const { return value / MILLISECONDS; }
+ double secs() const { return value / SECONDS; }
+ double mins() const { return value / MINUTES; }
+ double hrs() const { return value / HOURS; }
+ double days() const { return value / DAYS; }
double weeks() const { return value / WEEKS; }
- bool operator < (const Duration& that) const { return value < that.value; }
+ bool operator < (const Duration& that) const { return value < that.value; }
bool operator <= (const Duration& that) const { return value <= that.value; }
- bool operator > (const Duration& that) const { return value > that.value; }
+ bool operator > (const Duration& that) const { return value > that.value; }
bool operator >= (const Duration& that) const { return value >= that.value; }
bool operator == (const Duration& that) const { return value == that.value; }
bool operator != (const Duration& that) const { return value != that.value; }
@@ -77,20 +77,16 @@ public:
// TODO(vinod): Overload arithmetic operators.
protected:
- static const uint64_t NANOSECONDS = 1;
+ static const uint64_t NANOSECONDS = 1;
static const uint64_t MICROSECONDS = 1000 * NANOSECONDS;
static const uint64_t MILLISECONDS = 1000 * MICROSECONDS;
- static const uint64_t SECONDS = 1000 * MILLISECONDS;
- static const uint64_t MINUTES = 60 * SECONDS;
- static const uint64_t HOURS = 60 * MINUTES;
- static const uint64_t DAYS = 24 * HOURS;
- static const uint64_t WEEKS = 7 * DAYS;
-
- Duration(double _value, uint64_t _unit)
- {
- // Convert to the lowest unit (nanoseconds).
- value = _value * _unit;
- }
+ static const uint64_t SECONDS = 1000 * MILLISECONDS;
+ static const uint64_t MINUTES = 60 * SECONDS;
+ static const uint64_t HOURS = 60 * MINUTES;
+ static const uint64_t DAYS = 24 * HOURS;
+ static const uint64_t WEEKS = 7 * DAYS;
+
+ Duration(double _value, uint64_t _unit) : value(_value * _unit) {}
private:
double value;
@@ -100,64 +96,56 @@ private:
class Nanoseconds : public Duration
{
public:
- explicit Nanoseconds(double value)
- : Duration(value, NANOSECONDS) {}
+ explicit Nanoseconds(double value) : Duration(value, NANOSECONDS) {}
};
class Microseconds : public Duration
{
public:
- explicit Microseconds(double value)
- : Duration(value, MICROSECONDS) {}
+ explicit Microseconds(double value) : Duration(value, MICROSECONDS) {}
};
class Milliseconds : public Duration
{
public:
- explicit Milliseconds(double value)
- : Duration(value, MILLISECONDS) {}
+ explicit Milliseconds(double value) : Duration(value, MILLISECONDS) {}
};
class Seconds : public Duration
{
public:
- explicit Seconds(double value)
- : Duration(value, SECONDS) {}
+ explicit Seconds(double value) : Duration(value, SECONDS) {}
};
class Minutes : public Duration
{
public:
- explicit Minutes(double value)
- : Duration(value, MINUTES) {}
+ explicit Minutes(double value) : Duration(value, MINUTES) {}
};
class Hours : public Duration
{
public:
- explicit Hours(double value)
- : Duration(value, HOURS) {}
+ explicit Hours(double value) : Duration(value, HOURS) {}
};
class Days : public Duration
{
public:
- explicit Days(double value)
- : Duration(value, DAYS) {}
+ explicit Days(double value) : Duration(value, DAYS) {}
};
class Weeks : public Duration
{
public:
- explicit Weeks(double value)
- : Duration(value, WEEKS) {}
+ explicit Weeks(double value) : Duration(value, WEEKS) {}
};
Modified: incubator/mesos/trunk/third_party/libprocess/third_party/stout/tests/tests.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/third_party/libprocess/third_party/stout/tests/tests.cpp?rev=1453142&r1=1453141&r2=1453142&view=diff
==============================================================================
--- incubator/mesos/trunk/third_party/libprocess/third_party/stout/tests/tests.cpp (original)
+++ incubator/mesos/trunk/third_party/libprocess/third_party/stout/tests/tests.cpp Wed Mar 6 02:38:10 2013
@@ -7,6 +7,7 @@
#include <string>
#include <vector>
+#include <stout/bytes.hpp>
#include <stout/cache.hpp>
#include <stout/duration.hpp>
#include <stout/error.hpp>
@@ -130,6 +131,36 @@ TEST(Stout, None)
}
+TEST(Stout, Bytes)
+{
+ Try<Bytes> _1terabyte = Bytes::parse("1TB");
+
+ EXPECT_SOME_EQ(Terabytes(1), _1terabyte);
+ EXPECT_SOME_EQ(Gigabytes(1024), _1terabyte);
+ EXPECT_SOME_EQ(Megabytes(1024 * 1024), _1terabyte);
+ EXPECT_SOME_EQ(Kilobytes(1024 * 1024 * 1024), _1terabyte);
+ EXPECT_SOME_EQ(Bytes(1024LLU * 1024 * 1024 * 1024), _1terabyte);
+
+ EXPECT_EQ(Bytes(1024), Kilobytes(1));
+ EXPECT_LT(Bytes(1023), Kilobytes(1));
+ EXPECT_GT(Bytes(1025), Kilobytes(1));
+
+ EXPECT_NE(Megabytes(1023), Gigabytes(1));
+
+ EXPECT_EQ("0B", stringify(Bytes()));
+
+ EXPECT_EQ("1KB", stringify(Kilobytes(1)));
+ EXPECT_EQ("1MB", stringify(Megabytes(1)));
+ EXPECT_EQ("1GB", stringify(Gigabytes(1)));
+ EXPECT_EQ("1TB", stringify(Terabytes(1)));
+
+ EXPECT_EQ("1023B", stringify(Bytes(1023)));
+ EXPECT_EQ("1023KB", stringify(Kilobytes(1023)));
+ EXPECT_EQ("1023MB", stringify(Megabytes(1023)));
+ EXPECT_EQ("1023GB", stringify(Gigabytes(1023)));
+}
+
+
TEST(Stout, Duration)
{
Try<Duration> _3hrs = Duration::parse("3hrs");