diff --git a/include/cassandra_uuid_operators.hpp b/include/cassandra_uuid_operators.hpp new file mode 100644 index 000000000..6d32dc016 --- /dev/null +++ b/include/cassandra_uuid_operators.hpp @@ -0,0 +1,48 @@ +/* + Copyright (c) DataStax, Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +#ifndef __UUID_OPERATORS_HPP_INCLUDED__ +#define __UUID_OPERATORS_HPP_INCLUDED__ + +#ifdef __cplusplus +#include "cassandra.h" + +/** + * Compare two UUIDs for ordering + * + * This operator is useful to use CassUuid variables as std::map keys, for + * instance. + * + * @param uuid1 A UUID + * @param uuid2 Another UUID + * @return true if, and only if, uuid1 is numerically less or equal than uuid2 + */ +bool operator<(const CassUuid& uuid1, const CassUuid& uuid2); + +/** + * Compare two UUIDs for equality + * + * This operator is useful to use CassUuid variables as std::map keys, for + * instance. + * + * @param uuid1 A UUID + * @param uuid2 Another UUID + * @return true if, and only if, uuid1 is numerically less or equal than uuid2 + */ +bool operator==(const CassUuid& uuid1, const CassUuid& uuid2); +#endif + +#endif /* __UUID_OPERATORS_HPP_INCLUDED__ */ diff --git a/packaging/debian/cassandra-cpp-driver-dev.install b/packaging/debian/cassandra-cpp-driver-dev.install index ef708a549..3b4ace7bd 100755 --- a/packaging/debian/cassandra-cpp-driver-dev.install +++ b/packaging/debian/cassandra-cpp-driver-dev.install @@ -1,6 +1,7 @@ #! /usr/bin/dh-exec usr/include/*.h +usr/include/*.hpp usr/lib/*.a usr/lib/${DEB_HOST_MULTIARCH} debian/cassandra.pc usr/lib/${DEB_HOST_MULTIARCH}/pkgconfig debian/cassandra_static.pc usr/lib/${DEB_HOST_MULTIARCH}/pkgconfig diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 06b84b759..39c45591e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -255,7 +255,7 @@ endif() # Determine if the header should be installed if(CASS_INSTALL_HEADER) - file(GLOB CASS_API_HEADER_FILES ${CASS_INCLUDE_DIR}/*.h) + file(GLOB CASS_API_HEADER_FILES ${CASS_INCLUDE_DIR}/*.h ${CASS_INCLUDE_DIR}/*.hpp) install(FILES ${CASS_API_HEADER_FILES} DESTINATION ${INSTALL_HEADER_DIR}) endif() diff --git a/src/uuids.cpp b/src/uuids.cpp index 1bd0d3bd3..32b90d48c 100644 --- a/src/uuids.cpp +++ b/src/uuids.cpp @@ -44,6 +44,18 @@ static uint64_t set_version(uint64_t timestamp, uint8_t version) { return (timestamp & 0x0FFFFFFFFFFFFFFFLL) | (static_cast(version) << 60); } +bool operator<(const CassUuid& uuid1, const CassUuid& uuid2) { + if (uuid1.time_and_version == uuid2.time_and_version) + return uuid1.clock_seq_and_node < uuid2.clock_seq_and_node; + else + return uuid1.time_and_version < uuid2.time_and_version; +} + +bool operator==(const CassUuid& uuid1, const CassUuid& uuid2) { + return uuid1.time_and_version == uuid2.time_and_version + && uuid1.clock_seq_and_node == uuid2.clock_seq_and_node; +} + extern "C" { CassUuidGen* cass_uuid_gen_new() { return CassUuidGen::to(new UuidGen()); } diff --git a/tests/src/unit/tests/test_uuids.cpp b/tests/src/unit/tests/test_uuids.cpp index af08bb477..ab2d85d05 100644 --- a/tests/src/unit/tests/test_uuids.cpp +++ b/tests/src/unit/tests/test_uuids.cpp @@ -17,6 +17,7 @@ #include #include "cassandra.h" +#include "cassandra_uuid_operators.hpp" #include "get_time.hpp" #include "scoped_ptr.hpp" @@ -167,3 +168,35 @@ TEST(UuidUnitTest, FromStringInvalid) { EXPECT_EQ(cass_uuid_from_string_n("00-00-00-00-11-11-11-11-22-22-22-22-deadbeaf", 36, &uuid), CASS_ERROR_LIB_BAD_PARAMS); } + +TEST(UuidUnitTest, Operators) { + CassUuid uuid1 = { 0x0000112233445566LL, 0x0000112233445566LL }; + CassUuid uuid2 = { 0x0000112233445566LL, 0x0000112233445566LL }; + CassUuid uuid3 = { 0x9988776655443322LL, 0x0000112233445566LL }; + CassUuid uuid4 = { 0x9988776655443322LL, 0x9988776655443322LL }; + + // uuid1 == uuid2 + EXPECT_TRUE(uuid1 == uuid2); + // uuid1 != uuid3 + EXPECT_FALSE(uuid1 == uuid3); + // uuid1 != uuid4 + EXPECT_FALSE(uuid1 == uuid4); + // uuid2 != uuid3 + EXPECT_FALSE(uuid2 == uuid3); + // uuid2 != uuid3 + EXPECT_FALSE(uuid2 == uuid4); + // uuid3 != uuid4 + EXPECT_FALSE(uuid3 == uuid4); + // uuid1 >= uuid2 + EXPECT_FALSE(uuid1 < uuid2); + // uuid1 < uuid3 + EXPECT_TRUE(uuid1 < uuid3); + // uuid1 < uuid4 + EXPECT_TRUE(uuid1 < uuid4); + // uuid2 < uuid3 + EXPECT_TRUE(uuid2 < uuid3); + // uuid2 < uuid3 + EXPECT_TRUE(uuid2 < uuid4); + // uuid3 < uuid4 + EXPECT_TRUE(uuid3 < uuid4); +}