Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implement command shell for C++ client

  • Loading branch information...
commit 02bc2be2781578dc10cfc52926bbcc8c79e0bd30 1 parent e7dc90a
Rob Adams readams authored
23 bindings/cpp/include/voldemort/Version.h
View
@@ -21,6 +21,8 @@
#ifndef VERSION_H
#define VERSION_H
+#include <ostream>
+
namespace Voldemort {
/**
@@ -60,6 +62,27 @@ class Version
* @return one of the Occurred values
*/
virtual Occurred compare(Version* v) { throw "Pure abstract class"; }
+
+ /**
+ * Output a string version of the version object to the provided
+ * stream.
+ *
+ * @param output the output stream to write to
+ */
+ virtual void toStream(std::ostream& output) const = 0;
+
+ /**
+ * Stream insertion operator for Version
+ *
+ * @param output the stream
+ * @param ver the @ref Version object
+ * @return the stream
+ */
+ friend std::ostream& operator<<(std::ostream& output,
+ const Version& ver) {
+ ver.toStream(output);
+ return output;
+ }
};
} /* namespace Voldemort */
4 bindings/cpp/src/DefaultStoreClient.cpp
View
@@ -104,7 +104,11 @@ void DefaultStoreClient::put(const std::string* key,
} catch (...) {
if (version) delete version;
if (valuec) delete valuec;
+ throw;
}
+ } else {
+ curValue_ = *vv;
+ curValue_.setValue(new std::string(*value));
}
put(key, &curValue_);
}
15 bindings/cpp/src/VectorClock.cpp
View
@@ -46,11 +46,11 @@ VectorClock::~VectorClock() {
versions = NULL;
}
-VectorClock* VectorClock::copy() {
+VectorClock* VectorClock::copy() const{
return new VectorClock(versions, timestamp);
}
-VectorClock::Occurred VectorClock::compare(Version* v) {
+VectorClock::Occurred VectorClock::compare(Version* v) const {
VectorClock* vc = dynamic_cast<VectorClock*>(v);
if (vc)
return compare(this, vc);
@@ -58,7 +58,8 @@ VectorClock::Occurred VectorClock::compare(Version* v) {
throw std::invalid_argument("Could not cast Version to VectorClock");
}
-VectorClock::Occurred VectorClock::compare(VectorClock* v1, VectorClock* v2) {
+VectorClock::Occurred VectorClock::compare(const VectorClock* v1,
+ const VectorClock* v2) {
// We do two checks: v1 <= v2 and v2 <= v1 if both are true then
bool v1Bigger = false;
bool v2Bigger = false;
@@ -120,17 +121,15 @@ uint64_t VectorClock::getTimestamp() {
return timestamp;
}
-std::ostream& operator<<(std::ostream& output, const VectorClock& vc) {
+void VectorClock::toStream(std::ostream& output) const {
output << "version(";
std::list<std::pair<short, uint64_t> >::const_iterator it;
- for (it = vc.versions->begin(); it != vc.versions->end(); ++it) {
- if (it != vc.versions->begin())
+ for (it = versions->begin(); it != versions->end(); ++it) {
+ if (it != versions->begin())
output << ", ";
output << it->first << ":" << it->second;
}
output << ")";
-
- return output;
}
} /* namespace Voldemort */
34 bindings/cpp/src/include/VectorClock.h
View
@@ -62,19 +62,9 @@ class VectorClock: public Version
virtual ~VectorClock();
/**
- * Virtual copy constructor allocates a new VectorClock object
- * containing the same information as this one.
- */
- virtual VectorClock* copy();
-
- /**
- * Return whether or not the given vectorClock preceeded this one,
- * succeeded it, or is concurrant with it
- *
- * @param v The other vectorClock
- * @return one of the Occurred values
+ * Get the list of version entries
*/
- virtual Occurred compare(Version* v);
+ const std::list<std::pair<short, uint64_t> >* getEntries() const;
/**
* Compare two VectorClocks, the outcomes will be one of the
@@ -88,27 +78,17 @@ class VectorClock: public Version
* @param v2 The second VectorClock
* @return one of the Occurred values
*/
- static Occurred compare(VectorClock* v1, VectorClock* v2);
-
- /**
- * Get the list of version entries
- */
- const std::list<std::pair<short, uint64_t> >* getEntries() const;
+ static Occurred compare(const VectorClock* v1, const VectorClock* v2);
/**
* Get the timestamp
*/
uint64_t getTimestamp();
- /**
- * Stream insertion operator for cluster
- *
- * @param output the stream
- * @param vc the @ref VectorClock object
- * @return the stream
- */
- friend std::ostream& operator<<(std::ostream& output,
- const VectorClock& vc);
+ // Version interface
+ virtual VectorClock* copy() const;
+ virtual Occurred compare(Version* v) const;
+ virtual void toStream(std::ostream& output) const;
private:
// Disable copy constructor
143 bindings/cpp/utils/voldemortShell.cpp
View
@@ -23,11 +23,71 @@
#include <list>
#include <memory>
#include <iostream>
+#include <map>
+#include <sstream>
+#include <algorithm>
+#include <iterator>
+#include <vector>
using namespace std;
using namespace Voldemort;
+typedef bool (*command)(StoreClient* client, const vector<string>& tokens);
+
+bool getCommand(StoreClient* client, const vector<string>& tokens) {
+ if (tokens.size() < 2) {
+ cerr << "Usage: get {list of keys}" << endl;
+ return true;
+ }
+
+ for (unsigned int i = 1; i < tokens.size(); i++) {
+ const VersionedValue* result = client->get(&tokens[i]);
+ cout << tokens[i] << ": ";
+ if (result) {
+ cout << *(result->getValue()) << " " << *(result->getVersion());
+ } else {
+ cout << "null";
+ }
+ cout << endl;
+ }
+ return true;
+}
+
+bool putCommand(StoreClient* client, const vector<string>& tokens) {
+ if (tokens.size() < 3) {
+ cerr << "Usage: put key value" << endl;
+ return true;
+ }
+
+ client->put(&tokens[1], &tokens[2]);
+ return true;
+}
+
+bool deleteCommand(StoreClient* client, const vector<string>& tokens) {
+ if (tokens.size() < 2) {
+ cerr << "Usage: delete {list of keys}" << endl;
+ return true;
+ }
+
+ for (unsigned int i = 1; i < tokens.size(); i++) {
+ cout << tokens[i] << ": "
+ << client->deleteKey(&tokens[i]) << endl;
+ }
+ return true;
+}
+
+bool quitCommand(StoreClient* client, const vector<string>& tokens) {
+ return false;
+}
+
int main(int argc, char** argv) {
+ map<string, command> comMap;
+ comMap[string("get")] = getCommand;
+ comMap[string("put")] = putCommand;
+ comMap[string("delete")] = deleteCommand;
+ comMap[string("exit")] = quitCommand;
+ comMap[string("quit")] = quitCommand;
+
try {
// Initialize the bootstrap URLs. This is a list of server URLs
// in the cluster that we use to download metadata for the
@@ -53,39 +113,68 @@ int main(int argc, char** argv) {
// using the SocketStoreClientFactory which will connect to a
// Voldemort cluster over TCP.
SocketStoreClientFactory factory(config);
-
auto_ptr<StoreClient> client(factory.getStoreClient(storeName));
- if (!client.get()) {
- cerr << "Error could not construct client object" << endl;
- }
- // Get a value
- std::string key("hello");
- const VersionedValue* result = client->get(&key);
- VersionedValue value;
- if (result) {
- value = *result;
- cout << "Value: " << *(value.getValue()) << endl;
- } else {
- cout << "Value not set" << endl;
- }
+ while (!cin.eof()) {
+ try {
+ string line;
+ cerr << "> ";
+
+ getline (cin, line);
+ if (cin.eof()) {
+ cerr << endl;
+ break;
+ }
+
+ istringstream iss(line);
+ vector<string> tokens;
+ copy(istream_iterator<string>(iss),
+ istream_iterator<string>(),
+ back_inserter<vector<string> >(tokens));
+
+ map<string, command>::iterator it;
+ if (tokens.size() > 0)
+ it = comMap.find(tokens[0]);
+ if (it == comMap.end()) {
+ cerr << "Error: unrecognized command" << endl;
+ } else if (!(it)->second(client.get(), tokens))
+ break;
+#if 0
+ // Get a value
+ std::string key("hello");
+ const VersionedValue* result = client->get(&key);
+ VersionedValue value;
+ if (result) {
+ value = *result;
+ cout << "Value: " << *(value.getValue()) << endl;
+ } else {
+ cout << "Value not set" << endl;
+ }
- // Modify the value
- value.setValue(new string("world!"));
+ // Modify the value
+ value.setValue(new string("world!"));
- // update the value
- client->put(&key, &value);
+ // update the value
+ client->put(&key, &value);
- value = *client->get(&key);
- cout << "Value: " << *(value.getValue()) << endl;
+ value = *client->get(&key);
+ cout << "Value: " << *(value.getValue()) << endl;
- // Set and then delete a key
- std::string key2("keytest");
- std::string value2("valuetest");
- client->put(&key2, &value2);
- client->deleteKey(&key2);
+ // Set and then delete a key
+ std::string key2("keytest");
+ std::string value2("valuetest");
+ client->put(&key2, &value2);
+ client->deleteKey(&key2);
- } catch (VoldemortException& v) {
- cerr << "Voldemort Error: " << v.what() << endl;
+#endif
+ } catch (VoldemortException& v) {
+ cerr << "Error: " << v.what() << endl;
+ }
+ }
+
+ } catch (std::exception& e) {
+ cerr << "Error while initializing: " << e.what() << endl;
+ exit(1);
}
+
}
2  src/java/voldemort/VoldemortClientShell.java
View
@@ -67,7 +67,7 @@ public static void main(String[] args) throws Exception {
}
StoreClientFactory factory = new SocketStoreClientFactory(new ClientConfig().setBootstrapUrls(bootstrapUrl)
- .setRequestFormatType(RequestFormatType.VOLDEMORT));
+ .setRequestFormatType(RequestFormatType.PROTOCOL_BUFFERS));
DefaultStoreClient<Object, Object> client = null;
try {
client = (DefaultStoreClient<Object, Object>) factory.getStoreClient(storeName);
Please sign in to comment.
Something went wrong with that request. Please try again.