Skip to content

Commit

Permalink
modified: ../src/record.cpp
Browse files Browse the repository at this point in the history
	modified:   ../src/record.h
	modified:   ../src/util/key.cpp
	modified:   ../src/util/key.h
	modified:   ../tests/record_test.cpp
  • Loading branch information
Nickheythatsme committed Oct 30, 2019
1 parent db68610 commit 589ba7f
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 19 deletions.
22 changes: 11 additions & 11 deletions src/record.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,36 @@ using namespace networking;

Record Record::build(std::string record_contents)
{
auto uuid = UUID::init_random();
auto record_key = Key::init_random();
HashBuilder builder;
builder.write(reinterpret_cast<const unsigned char*>(record_contents.c_str()), record_contents.size());
return Record(
std::move(uuid),
std::move(record_key),
std::move(builder.finalize()),
std::move(record_contents)
);
}

Record::Record(UUID uuid, Hash256 sha256, std::string record_contents) :
uuid(std::move(uuid)),
Record::Record(Key record_key, Hash256 sha256, std::string record_contents) :
record_key(std::move(record_key)),
sha256(std::move(sha256)),
record_contents(std::move(record_contents))
{
}

bool operator==(const Record& lhs, const UUID& rhs)
bool operator==(const Record& lhs, const Key& rhs)
{
return lhs.uuid == rhs;
return lhs.record_key == rhs;
}

bool operator==(const Record& lhs, const Record &rhs)
{
return lhs.uuid == rhs.uuid;
return lhs.record_key == rhs.record_key;
}

const UUID& Record::get_uuid() const
const Key& Record::get_record_key() const
{
return uuid;
return record_key;
}

const Hash256& Record::get_hash256() const
Expand All @@ -57,7 +57,7 @@ const std::string& Record::get_record_contents() const

std::ostream& Record::serialize(std::ostream& out) const
{
out << uuid << sha256;
out << record_key << sha256;
uint64_t record_length = (uint64_t) record_contents.size();
writeNetworkLongLong(out, record_length);
out.write(record_contents.c_str(), record_length);
Expand All @@ -70,7 +70,7 @@ std::istream& Record::unserialize(std::istream& in)
uint64_t record_length;

HashBuilder builder;
in >> uuid;
in >> record_key;
sha256.unserialize(in);

record_length = readNetworkLongLong(in);
Expand Down
10 changes: 5 additions & 5 deletions src/record.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#define _RECORD_H_

#include "util/hash.hpp"
#include "util/uuid.h"
#include "util/key.h"
#include "networking/serialize.h"
#include <fstream>
#include <exception>
Expand Down Expand Up @@ -42,19 +42,19 @@ class Record: public Serializable
public:
static Record build(std::string record_contents);
Record() = default;
explicit Record(UUID uuid, Hash256 sha256, std::string record_contents);
explicit Record(Key record_key, Hash256 sha256, std::string record_contents);
Record(const Record &rhs) = default;
Record(Record &&rhs) noexcept = default;
virtual ~Record() = default;
friend bool operator==(const Record& lhs, const UUID& rhs);
friend bool operator==(const Record& lhs, const Key& rhs);
friend bool operator==(const Record& lhs, const Record &rhs);
const UUID& get_uuid() const;
const Key& get_record_key() const;
const Hash256& get_hash256() const;
const std::string& get_record_contents() const;
std::ostream& serialize(std::ostream& out) const;
std::istream& unserialize(std::istream& in);
protected:
UUID uuid; // key
Key record_key; // key
Hash256 sha256; // value
std::string record_contents;
};
Expand Down
10 changes: 10 additions & 0 deletions src/util/key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,15 @@ std::istream& Key::unserialize(std::istream& in)
return in;
}

std::ostream& operator<<(std::ostream& out, const Key &rhs)
{
return rhs.serialize(out);
}

std::istream& operator>>(std::istream& in, Key &rhs)
{
return rhs.unserialize(in);
}

} // namespace util
} // namespace p2p
2 changes: 2 additions & 0 deletions src/util/key.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class Key: public networking::Serializable
// Serialize functions
std::ostream& serialize(std::ostream& out) const;
std::istream& unserialize(std::istream& in);
friend std::ostream& operator<<(std::ostream& out, const Key &rhs);
friend std::istream& operator>>(std::istream& in, Key &rhs);
static const size_t NLONG = 2; // two ints
private:
Key(uint64_t data1, uint64_t data2);
Expand Down
6 changes: 3 additions & 3 deletions tests/record_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Record generate_record()

void print_record(const Record &r)
{
cout << "UUID: " << r.get_uuid() << endl
cout << "KEY: " << r.get_record_key().to_string() << endl
<< "HASH: " << r.get_hash256().to_string() << endl
<< "Record contents: " << r.get_record_contents() << endl
<< endl;
Expand All @@ -58,7 +58,7 @@ TEST(Record, serialize_unserialize)
print_record(record);
print_record(record2);

ASSERT_EQ(record.get_uuid(), record2.get_uuid());
ASSERT_EQ(record.get_record_key(), record2.get_record_key());
ASSERT_EQ(record.get_hash256(), record2.get_hash256());
ASSERT_EQ(record.get_record_contents(), record2.get_record_contents());
}
Expand All @@ -69,7 +69,7 @@ TEST(Record, unserialize_error)
HashBuilder builder;
builder.write(reinterpret_cast<const unsigned char*>("test"), 4);
Hash256 hash256 = builder.finalize();
Record record(util::UUID::init_random(), hash256, "not the right content");
Record record(util::Key::init_random(), hash256, "not the right content");

// Serialize it
std::stringstream ss;
Expand Down

0 comments on commit 589ba7f

Please sign in to comment.