Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add comparison operators for CassUuid #473

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions include/cassandra_uuid_operators.hpp
Original file line number Diff line number Diff line change
@@ -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__ */
1 change: 1 addition & 0 deletions packaging/debian/cassandra-cpp-driver-dev.install
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
12 changes: 12 additions & 0 deletions src/uuids.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ static uint64_t set_version(uint64_t timestamp, uint8_t version) {
return (timestamp & 0x0FFFFFFFFFFFFFFFLL) | (static_cast<uint64_t>(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()); }
Expand Down
33 changes: 33 additions & 0 deletions tests/src/unit/tests/test_uuids.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <gtest/gtest.h>

#include "cassandra.h"
#include "cassandra_uuid_operators.hpp"
#include "get_time.hpp"
#include "scoped_ptr.hpp"

Expand Down Expand Up @@ -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);
}