Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix some iterator issues and get time-based resolver working

  • Loading branch information...
commit c5d3e861b45ae838f26ed419a7e3c9d1267c45fe 1 parent 1da3f79
@readams readams authored
View
17 bindings/cpp/src/TimeBasedInconsistencyResolver.cpp
@@ -35,23 +35,28 @@ resolveConflicts(std::list<VersionedValue>* items) const {
// Find the largest timestamp
uint64_t maxTimestamp = 0;
- std::list<VersionedValue>::iterator it;
- for (it = items->begin(); it != items->end(); ++it) {
+ std::list<VersionedValue>::iterator it = items->begin();
+ while (it != items->end()) {
uint64_t t = dynamic_cast<VectorClock*>(it->getVersion())->getTimestamp();
if (t > maxTimestamp) {
maxTimestamp = t;
+ ++it;
} else {
- items->erase(it);
+ it = items->erase(it);
}
}
// remove everything else. We arbitrary take the leftmost value
// if there's more than one value with the same timestamp.
bool found = false;
- for (it = items->begin(); it != items->end(); ++it) {
+ it = items->begin();
+ while (it != items->end()) {
uint64_t t = dynamic_cast<VectorClock*>(it->getVersion())->getTimestamp();
- if (t < maxTimestamp || (found && t == maxTimestamp))
- items->erase(it);
+ if (t < maxTimestamp || (found && t == maxTimestamp)) {
+ it = items->erase(it);
+ } else
+ ++it;
+
if (t == maxTimestamp)
found = true;
}
View
7 bindings/cpp/src/VectorClockInconsistencyResolver.cpp
@@ -43,7 +43,9 @@ resolveConflicts(std::list<VersionedValue>* items) const {
VectorClock* vc1 =
dynamic_cast<VectorClock*>(it->getVersion());
bool found = false;
- for (it2 = newitems.begin(); it2 != newitems.end(); ++it2) {
+ it2 = newitems.begin();
+ while (it2 != newitems.end()) {
+ bool advance = true;
VectorClock* vc2 =
dynamic_cast<VectorClock*>(it2->getVersion());
@@ -53,6 +55,7 @@ resolveConflicts(std::list<VersionedValue>* items) const {
case Version::AFTER:
if (found) {
it2 = newitems.erase(it2);
+ advance = false;
} else {
*it2 = *it;
}
@@ -64,6 +67,8 @@ resolveConflicts(std::list<VersionedValue>* items) const {
default:
break;
}
+ if (advance)
+ ++it2;
}
if (!found)
newitems.push_back(*it);
View
1  bindings/cpp/test/Makefile.am
@@ -26,6 +26,7 @@ voldemort_test_SOURCES = \
main.cpp \
VectorClock_test.cpp \
VectorClockInconsistencyResolver_test.cpp \
+ TimeBasedInconsistencyResolver_test.cpp \
ProtocolBuffersRequestFormat_test.cpp
voldemort_test_LDADD = ../src/libvoldemort.la $(BOOST_UNIT_TEST_FRAMEWORK_LIB)
View
27 bindings/cpp/test/VectorClockInconsistencyResolver_test.cpp
@@ -26,8 +26,8 @@ using namespace std;
#include <boost/test/unit_test.hpp>
-struct Fixture {
- Fixture() {
+struct VectorClockFixture {
+ VectorClockFixture() {
std::list<std::pair<short, uint64_t> > versions;
earlier.setVersion(new VectorClock(&versions, 0L));
@@ -49,6 +49,13 @@ struct Fixture {
concurrent.setVersion(new VectorClock(&versions, 0L));
versions.clear();
+ versions.push_back(make_pair((short)1, 1));
+ versions.push_back(make_pair((short)2, 1));
+ versions.push_back(make_pair((short)3, 1));
+ versions.push_back(make_pair((short)4, 1));
+ concurrent2.setVersion(new VectorClock(&versions, 0L));
+
+ versions.clear();
versions.push_back(make_pair((short)1, 2));
versions.push_back(make_pair((short)2, 2));
versions.push_back(make_pair((short)3, 1));
@@ -68,11 +75,12 @@ struct Fixture {
VersionedValue prior;
VersionedValue current;
VersionedValue concurrent;
+ VersionedValue concurrent2;
VersionedValue much_later;
VersionedValue earlier;
};
-BOOST_FIXTURE_TEST_SUITE(VectorClockInconsistencyResolver_test, Fixture)
+BOOST_FIXTURE_TEST_SUITE(VectorClockInconsistencyResolver_test, VectorClockFixture)
BOOST_AUTO_TEST_CASE( empty_list_test ) {
v.resolveConflicts(&items);
@@ -151,4 +159,17 @@ BOOST_AUTO_TEST_CASE( normal_resolve_test3 ) {
"'later' element should be returned element");
}
+BOOST_AUTO_TEST_CASE( normal_resolve_larger_concurrent ) {
+ items.clear();
+ items.push_back(concurrent);
+ items.push_back(concurrent2);
+ items.push_back(current);
+ items.push_back(concurrent2);
+ items.push_back(current);
+ items.push_back(concurrent);
+ items.push_back(current);
+ v.resolveConflicts(&items);
+ BOOST_CHECK_MESSAGE(items.size() == 3, "Should return three elements");
+}
+
BOOST_AUTO_TEST_SUITE_END()
View
16 bindings/cpp/test/VectorClock_test.cpp
@@ -56,6 +56,22 @@ BOOST_AUTO_TEST_CASE( single_addition_test ) {
"with a single additional event");
}
+BOOST_AUTO_TEST_CASE( larger_test ) {
+ std::list<std::pair<short, uint64_t> > versions;
+
+ versions.push_back(make_pair((short)1, 2));
+ versions.push_back(make_pair((short)2, 1));
+ VectorClock clock1(&versions, 0L);
+
+ versions.clear();
+ versions.push_back(make_pair((short)1, 1));
+ versions.push_back(make_pair((short)2, 1));
+ versions.push_back(make_pair((short)3, 1));
+ VectorClock clock2(&versions, 0L);
+ BOOST_CHECK_MESSAGE(clock1.compare(&clock2) == Version::CONCURRENTLY,
+ "Result should be CONCURRENTLY");
+}
+
BOOST_AUTO_TEST_CASE( different_events_test ) {
std::list<std::pair<short, uint64_t> > versions;
Please sign in to comment.
Something went wrong with that request. Please try again.