Skip to content

Commit

Permalink
everything works again, fixed headers too
Browse files Browse the repository at this point in the history
  • Loading branch information
vagrant committed Feb 5, 2019
1 parent 67baaef commit ccd4e48
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 44 deletions.
6 changes: 6 additions & 0 deletions cpp/compile_clean
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
# Script that will make clean until I have a makefile

rm *.o
rm -rf service_layer
rm-rf backend_store
4 changes: 2 additions & 2 deletions cpp/key_value_client.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef KeyValClient
#define KeyValClient
#ifndef cpp_key_value_client_H

This comment has been minimized.

Copy link
@barath

barath Feb 5, 2019

Collaborator

Here's the preferred style, I think:

CPP_KEY_VALUE_CLIENT_H_

#define cpp_key_value_client_H
#include <deque>
#include <string>

Expand Down
4 changes: 2 additions & 2 deletions cpp/key_value_client_interface.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef KVClientInterface
#define KVClientInterface
#ifndef cpp_key_value_client_interface_H
#define cpp_key_value_client_interface_H
#include <deque>
#include <string>

Expand Down
4 changes: 2 additions & 2 deletions cpp/key_value_store.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef KVStore
#define KVStore
#ifndef cpp_key_value_store_H
#define cpp_key_value_store_H
#include <map>
#include <deque>
#include <string>
Expand Down
4 changes: 2 additions & 2 deletions cpp/key_value_store_service_impl.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef KVSSI_H
#define KVSSI_H
#ifndef cpp_key_value_store_service_impl_H
#define cpp_key_value_store_service_impl_H
#include <map>
#include <deque>
#include <string>
Expand Down
53 changes: 30 additions & 23 deletions cpp/service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ ServiceLayer::ServiceLayer(KeyValueClientInterface* key_value_connection) {
store_ = key_value_connection;
}
void ServiceLayer::registeruser(const std::string& username) {
const std::string& userKey = kuserChirps_+username;
const std::string& empty = "";
const std::string userKey = kuserChirps_+username;
const std::string empty = "";
store_->put(userKey, empty);

const std::string& followingUserKey = kuserFollowing_+username;
const std::string followingUserKey = kuserFollowing_+username;
store_->put(followingUserKey, empty);
}

chirp::Chirp ServiceLayer::chirp(const std::string& username, const std::string& text, const std::string& parent_id) {
const std::string& my_id = std::to_string(curr_id_);
const std::string my_id = std::to_string(curr_id_);
curr_id_ = curr_id_ + 1;
chirp::Chirp this_chirp;
this_chirp.set_username(username);
Expand All @@ -27,28 +27,28 @@ chirp::Chirp ServiceLayer::chirp(const std::string& username, const std::string&
chirp_timestamp->set_useconds(useconds);
this_chirp.set_allocated_timestamp(chirp_timestamp);

const std::string& this_chirp_key = kchirpValue_ + my_id;
const std::string this_chirp_key = kchirpValue_ + my_id;
std::string chirp_as_string;
this_chirp.SerializeToString(&chirp_as_string);
const std::string& value = chirp_as_string;
const std::string value = chirp_as_string;
store_->put(this_chirp_key, value);

const std::string& user_key = kuserChirps_+username;
const std::string user_key = kuserChirps_+username;
store_->put(user_key, my_id);

const std::string& this_chirp_reply_key = kchirpReplies_+my_id;
const std::string& empty = "";
const std::string this_chirp_reply_key = kchirpReplies_+my_id;
const std::string empty = "";
store_->put(this_chirp_reply_key, empty);

if (parent_id.length() > 0) {
const std::string& chirp_parent_id = kchirpReplies_+parent_id;
const std::string& this_chirp_id = my_id;
const std::string chirp_parent_id = kchirpReplies_+parent_id;
const std::string this_chirp_id = my_id;
store_->put(chirp_parent_id, my_id);
}
return this_chirp;
}
void ServiceLayer::follow(const std::string& username, const std::string& to_follow) {
const std::string& following_user_key = kuserFollowing_+username;
const std::string following_user_key = kuserFollowing_+username;
store_->put(following_user_key, to_follow);
}

Expand All @@ -61,19 +61,19 @@ std::deque<chirp::Chirp> ServiceLayer::read(const std::string& chirp_id) {
chirp_list.push_back(given_chirp);

while (chirp_list.size() > 0) {
const std::string& curr_chirp_id = chirp_list.at(0);
const std::string curr_chirp_id = chirp_list.at(0);
chirp_list.pop_front();

if (curr_chirp_id.length() > 0) {
// Adds chirps to the deque of chirps in this thread
chirp::Chirp thisChirp;
const std::string& this_chirp_key = kchirpValue_+curr_chirp_id;
const std::string this_chirp_key = kchirpValue_+curr_chirp_id;
const std::deque<std::string>& this_chirps_values = store_->get(this_chirp_key);
thisChirp.ParseFromString(this_chirps_values.at(0));
read_chirps.push_back(thisChirp);

// Adds ids of thisChirp's replies to chirp_list to be read
const std::string& this_chirp_reply_key = kchirpReplies_+curr_chirp_id;
const std::string this_chirp_reply_key = kchirpReplies_+curr_chirp_id;
const std::deque<std::string>& this_chirp_replies = store_->get(this_chirp_reply_key);
for (const std::string& reply : this_chirp_replies) {
chirp_list.push_back(reply);
Expand All @@ -86,16 +86,23 @@ std::deque<chirp::Chirp> ServiceLayer::read(const std::string& chirp_id) {

std::deque<chirp::Chirp> ServiceLayer::monitor(const std::string& username, chirp::Timestamp start) {
std::deque<chirp::Chirp> found_chirps;
const std::string& user_following_key = kuserFollowing_ + username;
const std::string user_following_key = kuserFollowing_ + username;
const std::deque<std::string>& user_following = store_->get(user_following_key);
for(std::string username : user_following){
const std::deque<std::string>& user_chirp_ids = store_->get(kuserChirps_ + username);
for(std::string id : user_chirp_ids){
const std::deque<std::string>& this_chirps_values = store_->get(kchirpValue_ + id);
chirp::Chirp thisChirp;
thisChirp.ParseFromString(this_chirps_values.at(0));
if(thisChirp.timestamp().useconds() > start.useconds()){
found_chirps.push_back(thisChirp);
if(username.length() > 0){
const std::deque<std::string>& user_chirp_ids = store_->get(kuserChirps_ + username);
for(std::string id : user_chirp_ids){
const std::deque<std::string>& this_chirps_values = store_->get(kchirpValue_ + id);
chirp::Chirp thisChirp;
if(this_chirps_values.size() > 0){
thisChirp.ParseFromString(this_chirps_values.at(0));
int64_t myMicroSeconds = thisChirp.timestamp().useconds();
int64_t baslineSeconds = start.useconds();
std::string thisText = thisChirp.text();
if(thisChirp.timestamp().useconds() >= start.useconds()){
found_chirps.push_back(thisChirp);
}
}
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions cpp/service.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef ServiceLay
#define ServiceLay
#ifndef cpp_service_H
#define cpp_service_H
#include <map>
#include <string>

Expand Down Expand Up @@ -30,9 +30,9 @@ class ServiceLayer final {
// connection to the KeyValueStore
KeyValueClientInterface* store_;
// constants for prepended values for categorizing keys
const std::string& kuserChirps_ = "0";
const std::string& kuserFollowing_ = "1";
const std::string& kchirpValue_ = "2";
const std::string& kchirpReplies_ = "3";
const std::string kuserChirps_ = "0";
const std::string kuserFollowing_ = "1";
const std::string kchirpValue_ = "2";
const std::string kchirpReplies_ = "3";
};
#endif
1 change: 1 addition & 0 deletions cpp/service_layer_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ grpc::Status ServiceLayerServiceImpl::monitor(grpc::ServerContext* context, grpc
}
initial_time.set_seconds(seconds);
initial_time.set_useconds(useconds);
usleep(10);
}
return grpc::Status::OK;
}
5 changes: 3 additions & 2 deletions cpp/service_layer_service_impl.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef ServiceImpl
#define ServiceImpl
#ifndef cpp_service_layer_service_impl_H
#define cpp_service_layer_service_impl_H
#include <map>
#include <string>

Expand All @@ -8,6 +8,7 @@
#include "service.h"
#include "key_value_client.h"
#include <google/protobuf/util/time_util.h>
#include <unistd.h>

// Class for logic and data behind the ServiceLayer
// The methods in this class are the implementation of the Service Layer Service from service.proto
Expand Down
Binary file modified tests/service_layer_service_impl_tests
Binary file not shown.
44 changes: 39 additions & 5 deletions tests/service_layer_service_impl_tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@

#include "../cpp/service_layer_service_impl.h"
#include "../cpp/service.h"
#include <google/protobuf/util/time_util.h>

// Registers a user and confirms that user key values are correct
TEST(RegisterUserTest, Simple) {
KeyValueStore* test_store = new KeyValueStore();
KeyValueStore test_store;
ASSERT_EQ(-1, -1);
ServiceLayer* service_layer = new ServiceLayer(test_store);
ServiceLayer service_layer(&test_store);
ASSERT_EQ(0, 0);
service_layer->registeruser("testUser");
/*ASSERT_EQ(1, 1);
service_layer.registeruser("testUser");
ASSERT_EQ(1, 1);
const std::deque<std::string>& user_value = test_store.get("0testUser");
ASSERT_EQ(1, user_value.size());

const std::deque<std::string>& user_following = test_store.get("1testUser");
ASSERT_EQ(1, user_following.size());*/
ASSERT_EQ(1, user_following.size());
}

// registers two users and makes one follow the other
TEST(RegisterUserTest, RegisterRegisterFollow) {
KeyValueStore test_store;
ServiceLayer service_layer(&test_store);
Expand All @@ -45,6 +47,7 @@ TEST(RegisterUserTest, RegisterRegisterFollow) {
ASSERT_EQ("testUser2", user_following_now.at(1));
}

// register a user, chirp from that user, and then read that chirp
TEST(RegisterUserTest, RegisterChirpRead) {
KeyValueStore test_store;
ServiceLayer service_layer(&test_store);
Expand All @@ -69,6 +72,7 @@ TEST(RegisterUserTest, RegisterChirpRead) {
ASSERT_EQ("testText", read_chirps.at(0).text());
}

// register a user, chirp once, chirp again in response to that chirp, then read the thread
TEST(RegisterUserTest, RegisterChirpChirpRead) {
KeyValueStore test_store;
ServiceLayer service_layer(&test_store);
Expand All @@ -89,6 +93,36 @@ TEST(RegisterUserTest, RegisterChirpChirpRead) {
ASSERT_EQ("testText2", read_chirps.at(1).text());
}

// register two users, have the first follow the second, get a current timestamp, second user chirps twice, monitor starting from timestamp
TEST(FollowingUserTest, FollowMonitor) {
KeyValueStore test_store;
ServiceLayer service_layer(&test_store);
service_layer.registeruser("testUser");
service_layer.registeruser("testUserToFollow");

const std::deque<std::string>& user_value = test_store.get("0testUser");
ASSERT_EQ(1, user_value.size());
const std::deque<std::string>& user_value2 = test_store.get("0testUserToFollow");
ASSERT_EQ(1, user_value2.size());

service_layer.follow("testUser", "testUserToFollow");
const std::deque<std::string>& user_following = test_store.get("1testUser");
ASSERT_EQ(2, user_following.size());

chirp::Timestamp initial_time;
int64_t seconds = google::protobuf::util::TimeUtil::TimestampToSeconds(google::protobuf::util::TimeUtil::GetEpoch());
int64_t useconds = google::protobuf::util::TimeUtil::TimestampToMicroseconds(google::protobuf::util::TimeUtil::GetEpoch());
initial_time.set_seconds(seconds);
initial_time.set_useconds(useconds);

service_layer.chirp("testUserToFollow", "testText", "");
service_layer.chirp("testUserToFollow", "testText2", "");
std::deque<chirp::Chirp> monitored_chirps = service_layer.monitor("testUser", initial_time);
ASSERT_EQ(2, monitored_chirps.size());
ASSERT_EQ("testText", monitored_chirps.at(0).text());
ASSERT_EQ("testText2", monitored_chirps.at(1).text());
}


int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
Expand Down

0 comments on commit ccd4e48

Please sign in to comment.