You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by be...@apache.org on 2014/04/13 01:37:33 UTC
[4/5] git commit: Refactored State::names to return a set instead of
vector.
Refactored State::names to return a set instead of vector.
Review: https://reviews.apache.org/r/19835
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/df2248bb
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/df2248bb
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/df2248bb
Branch: refs/heads/master
Commit: df2248bb660f145757a21f6c423df5eabf0694ee
Parents: bafce31
Author: Benjamin Hindman <be...@gmail.com>
Authored: Sat Mar 29 13:57:43 2014 -0700
Committer: Benjamin Hindman <be...@gmail.com>
Committed: Sat Apr 12 17:35:37 2014 -0600
----------------------------------------------------------------------
.../org_apache_mesos_state_AbstractState.cpp | 18 ++++++++---------
src/state/in_memory.cpp | 11 +++++-----
src/state/in_memory.hpp | 4 ++--
src/state/leveldb.cpp | 15 +++++++-------
src/state/leveldb.hpp | 4 ++--
src/state/state.hpp | 6 +++---
src/state/storage.hpp | 4 ++--
src/state/zookeeper.cpp | 21 +++++++++++---------
src/state/zookeeper.hpp | 4 ++--
src/tests/state_tests.cpp | 8 +++++---
10 files changed, 51 insertions(+), 44 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mesos/blob/df2248bb/src/java/jni/org_apache_mesos_state_AbstractState.cpp
----------------------------------------------------------------------
diff --git a/src/java/jni/org_apache_mesos_state_AbstractState.cpp b/src/java/jni/org_apache_mesos_state_AbstractState.cpp
index 476bb27..1accc8a 100644
--- a/src/java/jni/org_apache_mesos_state_AbstractState.cpp
+++ b/src/java/jni/org_apache_mesos_state_AbstractState.cpp
@@ -1,7 +1,7 @@
#include <jni.h>
+#include <set>
#include <string>
-#include <vector>
#include <process/check.hpp>
#include <process/future.hpp>
@@ -18,8 +18,8 @@ using namespace mesos::internal::state;
using process::Future;
+using std::set;
using std::string;
-using std::vector;
extern "C" {
@@ -614,8 +614,8 @@ JNIEXPORT jlong JNICALL Java_org_apache_mesos_state_AbstractState__1_1names
State* state = (State*) env->GetLongField(thiz, __state);
- Future<vector<string> >* future =
- new Future<vector<string> >(state->names());
+ Future<set<string> >* future =
+ new Future<set<string> >(state->names());
return (jlong) future;
}
@@ -629,7 +629,7 @@ JNIEXPORT jlong JNICALL Java_org_apache_mesos_state_AbstractState__1_1names
JNIEXPORT jboolean JNICALL Java_org_apache_mesos_state_AbstractState__1_1names_1cancel
(JNIEnv* env, jobject thiz, jlong jfuture)
{
- Future<vector<string> >* future = (Future<vector<string> >*) jfuture;
+ Future<set<string> >* future = (Future<set<string> >*) jfuture;
// We'll initiate a discard but we won't consider it cancelled since
// we don't know if/when the future will get discarded.
@@ -663,7 +663,7 @@ JNIEXPORT jboolean JNICALL Java_org_apache_mesos_state_AbstractState__1_1names_1
JNIEXPORT jboolean JNICALL Java_org_apache_mesos_state_AbstractState__1_1names_1is_1done
(JNIEnv* env, jobject thiz, jlong jfuture)
{
- Future<vector<string> >* future = (Future<vector<string> >*) jfuture;
+ Future<set<string> >* future = (Future<set<string> >*) jfuture;
return (jboolean) !future->isPending() || future->hasDiscard();
}
@@ -677,7 +677,7 @@ JNIEXPORT jboolean JNICALL Java_org_apache_mesos_state_AbstractState__1_1names_1
JNIEXPORT jobject JNICALL Java_org_apache_mesos_state_AbstractState__1_1names_1get
(JNIEnv* env, jobject thiz, jlong jfuture)
{
- Future<vector<string> >* future = (Future<vector<string> >*) jfuture;
+ Future<set<string> >* future = (Future<set<string> >*) jfuture;
future->await();
@@ -725,7 +725,7 @@ JNIEXPORT jobject JNICALL Java_org_apache_mesos_state_AbstractState__1_1names_1g
JNIEXPORT jobject JNICALL Java_org_apache_mesos_state_AbstractState__1_1names_1get_1timeout
(JNIEnv* env, jobject thiz, jlong jfuture, jlong jtimeout, jobject junit)
{
- Future<vector<string> >* future = (Future<vector<string> >*) jfuture;
+ Future<set<string> >* future = (Future<set<string> >*) jfuture;
jclass clazz = env->GetObjectClass(junit);
@@ -787,7 +787,7 @@ JNIEXPORT jobject JNICALL Java_org_apache_mesos_state_AbstractState__1_1names_1g
JNIEXPORT void JNICALL Java_org_apache_mesos_state_AbstractState__1_1names_1finalize
(JNIEnv* env, jobject thiz, jlong jfuture)
{
- Future<vector<string> >* future = (Future<vector<string> >*) jfuture;
+ Future<set<string> >* future = (Future<set<string> >*) jfuture;
delete future;
}
http://git-wip-us.apache.org/repos/asf/mesos/blob/df2248bb/src/state/in_memory.cpp
----------------------------------------------------------------------
diff --git a/src/state/in_memory.cpp b/src/state/in_memory.cpp
index b184565..ce04e47 100644
--- a/src/state/in_memory.cpp
+++ b/src/state/in_memory.cpp
@@ -1,5 +1,5 @@
+#include <set>
#include <string>
-#include <vector>
#include <process/dispatch.hpp>
#include <process/future.hpp>
@@ -16,8 +16,9 @@
using namespace process;
+// Note that we don't add 'using std::set' here because we need
+// 'std::' to disambiguate the 'set' member.
using std::string;
-using std::vector;
namespace mesos {
namespace internal {
@@ -60,10 +61,10 @@ public:
return true;
}
- vector<string> names()
+ std::set<string> names() // Use std:: to disambiguate 'set' member.
{
const hashset<string>& keys = entries.keys();
- return vector<string>(keys.begin(), keys.end());
+ return std::set<string>(keys.begin(), keys.end());
}
private:
@@ -105,7 +106,7 @@ Future<bool> InMemoryStorage::expunge(const Entry& entry)
}
-Future<vector<string> > InMemoryStorage::names()
+Future<std::set<string> > InMemoryStorage::names()
{
return dispatch(process, &InMemoryStorageProcess::names);
}
http://git-wip-us.apache.org/repos/asf/mesos/blob/df2248bb/src/state/in_memory.hpp
----------------------------------------------------------------------
diff --git a/src/state/in_memory.hpp b/src/state/in_memory.hpp
index 822a89a..2040618 100644
--- a/src/state/in_memory.hpp
+++ b/src/state/in_memory.hpp
@@ -1,8 +1,8 @@
#ifndef __STATE_IN_MEMORY_HPP__
#define __STATE_IN_MEMORY_HPP__
+#include <set>
#include <string>
-#include <vector>
#include <process/future.hpp>
@@ -31,7 +31,7 @@ public:
virtual process::Future<Option<Entry> > get(const std::string& name);
virtual process::Future<bool> set(const Entry& entry, const UUID& uuid);
virtual process::Future<bool> expunge(const Entry& entry);
- virtual process::Future<std::vector<std::string> > names();
+ virtual process::Future<std::set<std::string> > names();
private:
InMemoryStorageProcess* process;
http://git-wip-us.apache.org/repos/asf/mesos/blob/df2248bb/src/state/leveldb.cpp
----------------------------------------------------------------------
diff --git a/src/state/leveldb.cpp b/src/state/leveldb.cpp
index 6db3ecf..4303df3 100644
--- a/src/state/leveldb.cpp
+++ b/src/state/leveldb.cpp
@@ -4,8 +4,8 @@
#include <google/protobuf/io/zero_copy_stream_impl.h> // For ArrayInputStream.
+#include <set>
#include <string>
-#include <vector>
#include <process/dispatch.hpp>
#include <process/future.hpp>
@@ -27,8 +27,9 @@
using namespace process;
+// Note that we don't add 'using std::set' here because we need
+// 'std::' to disambiguate the 'set' member.
using std::string;
-using std::vector;
namespace mesos {
namespace internal {
@@ -47,7 +48,7 @@ public:
Future<Option<Entry> > get(const string& name);
Future<bool> set(const Entry& entry, const UUID& uuid);
Future<bool> expunge(const Entry& entry);
- Future<vector<string> > names();
+ Future<std::set<string> > names();
private:
// Helpers for interacting with leveldb.
@@ -88,20 +89,20 @@ void LevelDBStorageProcess::initialize()
}
-Future<vector<string> > LevelDBStorageProcess::names()
+Future<std::set<string> > LevelDBStorageProcess::names()
{
if (error.isSome()) {
return Failure(error.get());
}
- vector<string> results;
+ std::set<string> results;
leveldb::Iterator* iterator = db->NewIterator(leveldb::ReadOptions());
iterator->SeekToFirst();
while (iterator->Valid()) {
- results.push_back(iterator->key().ToString());
+ results.insert(iterator->key().ToString());
iterator->Next();
}
@@ -287,7 +288,7 @@ Future<bool> LevelDBStorage::expunge(const Entry& entry)
}
-Future<vector<string> > LevelDBStorage::names()
+Future<std::set<string> > LevelDBStorage::names()
{
return dispatch(process, &LevelDBStorageProcess::names);
}
http://git-wip-us.apache.org/repos/asf/mesos/blob/df2248bb/src/state/leveldb.hpp
----------------------------------------------------------------------
diff --git a/src/state/leveldb.hpp b/src/state/leveldb.hpp
index f2d6d27..53447c6 100644
--- a/src/state/leveldb.hpp
+++ b/src/state/leveldb.hpp
@@ -1,8 +1,8 @@
#ifndef __STATE_LEVELDB_HPP__
#define __STATE_LEVELDB_HPP__
+#include <set>
#include <string>
-#include <vector>
#include <process/future.hpp>
@@ -32,7 +32,7 @@ public:
virtual process::Future<Option<Entry> > get(const std::string& name);
virtual process::Future<bool> set(const Entry& entry, const UUID& uuid);
virtual process::Future<bool> expunge(const Entry& entry);
- virtual process::Future<std::vector<std::string> > names();
+ virtual process::Future<std::set<std::string> > names();
private:
LevelDBStorageProcess* process;
http://git-wip-us.apache.org/repos/asf/mesos/blob/df2248bb/src/state/state.hpp
----------------------------------------------------------------------
diff --git a/src/state/state.hpp b/src/state/state.hpp
index bebfe02..2c0bb20 100644
--- a/src/state/state.hpp
+++ b/src/state/state.hpp
@@ -19,8 +19,8 @@
#ifndef __STATE_STATE_HPP__
#define __STATE_STATE_HPP__
+#include <set>
#include <string>
-#include <vector>
#include <process/deferred.hpp> // TODO(benh): This is required by Clang.
#include <process/future.hpp>
@@ -111,7 +111,7 @@ public:
process::Future<bool> expunge(const Variable& variable);
// Returns the collection of variable names in the state.
- process::Future<std::vector<std::string> > names();
+ process::Future<std::set<std::string> > names();
private:
// Helpers to handle future results from fetch and swap. We make
@@ -189,7 +189,7 @@ inline process::Future<bool> State::expunge(const Variable& variable)
}
-inline process::Future<std::vector<std::string> > State::names()
+inline process::Future<std::set<std::string> > State::names()
{
return storage->names();
}
http://git-wip-us.apache.org/repos/asf/mesos/blob/df2248bb/src/state/storage.hpp
----------------------------------------------------------------------
diff --git a/src/state/storage.hpp b/src/state/storage.hpp
index a137075..a36a93e 100644
--- a/src/state/storage.hpp
+++ b/src/state/storage.hpp
@@ -19,8 +19,8 @@
#ifndef __STATE_STORAGE_HPP__
#define __STATE_STORAGE_HPP__
+#include <set>
#include <string>
-#include <vector>
#include <process/future.hpp>
@@ -50,7 +50,7 @@ public:
virtual process::Future<bool> expunge(const Entry& entry) = 0;
// Returns the collection of variable names in the state.
- virtual process::Future<std::vector<std::string> > names() = 0;
+ virtual process::Future<std::set<std::string> > names() = 0;
};
} // namespace state {
http://git-wip-us.apache.org/repos/asf/mesos/blob/df2248bb/src/state/zookeeper.cpp
----------------------------------------------------------------------
diff --git a/src/state/zookeeper.cpp b/src/state/zookeeper.cpp
index dd632c0..a833eaa 100644
--- a/src/state/zookeeper.cpp
+++ b/src/state/zookeeper.cpp
@@ -3,6 +3,7 @@
#include <google/protobuf/io/zero_copy_stream_impl.h> // For ArrayInputStream.
#include <queue>
+#include <set>
#include <string>
#include <vector>
@@ -33,6 +34,8 @@
using namespace process;
+// Note that we don't add 'using std::set' here because we need
+// 'std::' to disambiguate the 'set' member.
using std::queue;
using std::string;
using std::vector;
@@ -60,7 +63,7 @@ public:
Future<Option<Entry> > get(const string& name);
Future<bool> set(const Entry& entry, const UUID& uuid);
virtual Future<bool> expunge(const Entry& entry);
- Future<vector<string> > names();
+ Future<std::set<string> > names();
// ZooKeeper events.
void connected(bool reconnect);
@@ -72,7 +75,7 @@ public:
private:
// Helpers for getting the names, fetching, and swapping.
- Result<vector<string> > doNames();
+ Result<std::set<string> > doNames();
Result<Option<Entry> > doGet(const string& name);
Result<bool> doSet(const Entry& entry, const UUID& uuid);
Result<bool> doExpunge(const Entry& entry);
@@ -99,7 +102,7 @@ private:
struct Names
{
- Promise<vector<string> > promise;
+ Promise<std::set<string> > promise;
};
struct Get
@@ -191,7 +194,7 @@ void ZooKeeperStorageProcess::initialize()
}
-Future<vector<string> > ZooKeeperStorageProcess::names()
+Future<std::set<string> > ZooKeeperStorageProcess::names()
{
if (error.isSome()) {
return Failure(error.get());
@@ -201,7 +204,7 @@ Future<vector<string> > ZooKeeperStorageProcess::names()
return names->promise.future();
}
- Result<vector<string> > result = doNames();
+ Result<std::set<string> > result = doNames();
if (result.isNone()) { // Try again later.
Names* names = new Names();
@@ -308,7 +311,7 @@ void ZooKeeperStorageProcess::connected(bool reconnect)
while (!pending.names.empty()) {
Names* names = pending.names.front();
- Result<vector<string> > result = doNames();
+ Result<std::set<string> > result = doNames();
if (result.isNone()) {
return; // Try again later.
} else if (result.isError()) {
@@ -385,7 +388,7 @@ void ZooKeeperStorageProcess::deleted(const string& path)
}
-Result<vector<string> > ZooKeeperStorageProcess::doNames()
+Result<std::set<string> > ZooKeeperStorageProcess::doNames()
{
// Get all children to determine current memberships.
vector<string> results;
@@ -404,7 +407,7 @@ Result<vector<string> > ZooKeeperStorageProcess::doNames()
// TODO(benh): It might make sense to "mangle" the names so that we
// can determine when a znode has incorrectly been added that
// actually doesn't store an Entry.
- return results;
+ return std::set<string>(results.begin(), results.end());
}
@@ -627,7 +630,7 @@ Future<bool> ZooKeeperStorage::expunge(const Entry& entry)
}
-Future<vector<string> > ZooKeeperStorage::names()
+Future<std::set<string> > ZooKeeperStorage::names()
{
return dispatch(process, &ZooKeeperStorageProcess::names);
}
http://git-wip-us.apache.org/repos/asf/mesos/blob/df2248bb/src/state/zookeeper.hpp
----------------------------------------------------------------------
diff --git a/src/state/zookeeper.hpp b/src/state/zookeeper.hpp
index 420315b..1a8483d 100644
--- a/src/state/zookeeper.hpp
+++ b/src/state/zookeeper.hpp
@@ -1,8 +1,8 @@
#ifndef __STATE_ZOOKEEPER_HPP__
#define __STATE_ZOOKEEPER_HPP__
+#include <set>
#include <string>
-#include <vector>
#include <process/future.hpp>
@@ -39,7 +39,7 @@ public:
virtual process::Future<Option<Entry> > get(const std::string& name);
virtual process::Future<bool> set(const Entry& entry, const UUID& uuid);
virtual process::Future<bool> expunge(const Entry& entry);
- virtual process::Future<std::vector<std::string> > names();
+ virtual process::Future<std::set<std::string> > names();
private:
ZooKeeperStorageProcess* process;
http://git-wip-us.apache.org/repos/asf/mesos/blob/df2248bb/src/tests/state_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/state_tests.cpp b/src/tests/state_tests.cpp
index d0e0840..678b1c8 100644
--- a/src/tests/state_tests.cpp
+++ b/src/tests/state_tests.cpp
@@ -20,7 +20,6 @@
#include <set>
#include <string>
-#include <vector>
#include <mesos/mesos.hpp>
@@ -52,6 +51,9 @@ using namespace mesos::internal;
using namespace process;
+using std::set;
+using std::string;
+
using state::LevelDBStorage;
using state::Storage;
#ifdef MESOS_HAS_JAVA
@@ -297,10 +299,10 @@ void Names(State* state)
AWAIT_READY(future2);
ASSERT_SOME(future2.get());
- Future<std::vector<std::string> > names = state->names();
+ Future<set<string> > names = state->names();
AWAIT_READY(names);
ASSERT_TRUE(names.get().size() == 1);
- EXPECT_EQ("slaves", names.get()[0]);
+ EXPECT_NE(names.get().find("slaves"), names.get().end());
}