Commit e72c14a1 authored by Manuel Wudka-Robles's avatar Manuel Wudka-Robles
Browse files

Update write examples

parent 3baec654
Loading
Loading
Loading
Loading
+10 −1
Original line number Diff line number Diff line
#include "kinetic/kinetic.h"
#include "glog/logging.h"

#include "command_line_flags.h"

int example_main(std::unique_ptr<kinetic::ConnectionHandle> connection, int argc, char** argv);

int main(int argc, char** argv) {
    google::InitGoogleLogging(argv[0]);

    std::unique_ptr<kinetic::ConnectionHandle> connection;
    if (!parse_flags(&argc, &argv, connection)) {
        return 1;
    }

    return example_main(move(connection), argc, argv);
    int ret = example_main(move(connection), argc, argv);

    google::protobuf::ShutdownProtobufLibrary();
    google::ShutdownGoogleLogging();
    google::ShutDownCommandLineFlags();

    return ret;
}
+6 −32
Original line number Diff line number Diff line
@@ -18,33 +18,11 @@ using std::make_shared;
using std::unique_ptr;
using std::to_string;

int main(int argc, char* argv[]) {
    google::InitGoogleLogging(argv[0]);
DEFINE_string(kinetic_key, "my_file", "Key prefix for storing file chunks");
DEFINE_string(local_file, "local_file", "Path of file to store in kinetic");

    if (argc != 4) {
        printf("Usage: %s <host> <kinetic key> <input file name>\n", argv[0]);
        return 1;
    }

    const char* host = argv[1];
    const char* kinetic_key = argv[2];
    const char* input_file_name = argv[3];

    kinetic::ConnectionOptions options;
    options.host = host;
    options.port = 8123;
    options.user_id = 1;
    options.hmac_key = "asdfasdf";

    KineticConnectionFactory kinetic_connection_factory = kinetic::NewKineticConnectionFactory();

    unique_ptr<kinetic::ConnectionHandle> connection;
    if (!kinetic_connection_factory.NewConnection(options, 5, connection).ok()) {
        printf("Unable to connect\n");
        return 1;
    }

    int file = open(input_file_name, O_RDONLY);
int example_main(std::unique_ptr<kinetic::ConnectionHandle> connection, int argc, char** argv) {
    int file = open(FLAGS_local_file.c_str(), O_RDONLY);
    struct stat inputfile_stat;
    fstat(file, &inputfile_stat);
    char* inputfile_data = (char*)mmap(0, inputfile_stat.st_size, PROT_READ, MAP_SHARED, file, 0);
@@ -55,7 +33,7 @@ int main(int argc, char* argv[]) {
            value_size = inputfile_stat.st_size - i + 1;
        }

        sprintf(key_buffer, "%s-%10" PRId64, kinetic_key, i);
        sprintf(key_buffer, "%s-%10" PRId64, FLAGS_kinetic_key.c_str(), i);

        std::string key(key_buffer);
        std::string value(inputfile_data + i, value_size);
@@ -74,7 +52,7 @@ int main(int argc, char* argv[]) {


    if (!connection->blocking().Put(
            kinetic_key,
            FLAGS_kinetic_key,
            "",
            kinetic::IGNORE_VERSION,
            KineticRecord(to_string(inputfile_stat.st_size), "", "", Message_Algorithm_SHA1)).ok()) {
@@ -89,9 +67,5 @@ int main(int argc, char* argv[]) {

    printf("Done!\n");

    google::protobuf::ShutdownProtobufLibrary();
    google::ShutdownGoogleLogging();
    google::ShutDownCommandLineFlags();

    return 0;
}
+8 −28
Original line number Diff line number Diff line
@@ -35,34 +35,18 @@ private:
    int* remaining_;
};

DEFINE_string(kinetic_key, "my_file", "Key prefix for storing file chunks");
DEFINE_string(local_file, "local_file", "Path of file to store in kinetic");

int main(int argc, char* argv[]) {
    google::InitGoogleLogging(argv[0]);

    if (argc != 4) {
        printf("Usage: %s <host> <kinetic key> <input file name>\n", argv[0]);
        return 1;
    }

    const char* host = argv[1];
    const char* kinetic_key = argv[2];
    const char* input_file_name = argv[3];
int example_main(std::unique_ptr<kinetic::ConnectionHandle> connection, int argc, char** argv) {
    int file = open(FLAGS_local_file.c_str(), O_RDONLY);

    kinetic::ConnectionOptions options;
    options.host = host;
    options.port = 8123;
    options.user_id = 1;
    options.hmac_key = "asdfasdf";

    KineticConnectionFactory kinetic_connection_factory = kinetic::NewKineticConnectionFactory();

    unique_ptr<kinetic::ConnectionHandle> connection;
    if (!kinetic_connection_factory.NewConnection(options, 5, connection).ok()) {
        printf("Unable to connect\n");
    if (file < 0) {
        printf("Unable to open %s\n", FLAGS_local_file.c_str());
        return 1;
    }

    int file = open(input_file_name, O_RDONLY);
    struct stat inputfile_stat;
    fstat(file, &inputfile_stat);
    char* inputfile_data = (char*)mmap(0, inputfile_stat.st_size, PROT_READ, MAP_SHARED, file, 0);
@@ -79,7 +63,7 @@ int main(int argc, char* argv[]) {
            value_size = inputfile_stat.st_size - i + 1;
        }

        sprintf(key_buffer, "%s-%10" PRId64, kinetic_key, i);
        sprintf(key_buffer, "%s-%10" PRId64, FLAGS_kinetic_key.c_str(), i);

        std::string key(key_buffer);
        std::string value(inputfile_data + i, value_size);
@@ -93,7 +77,7 @@ int main(int argc, char* argv[]) {
    auto record = make_shared<KineticRecord>(std::to_string(inputfile_stat.st_size), "", "", Message_Algorithm_SHA1);
    remaining++;

    connection->nonblocking().Put(kinetic_key, "", kinetic::IGNORE_VERSION, record, callback);
    connection->nonblocking().Put(FLAGS_kinetic_key.c_str(), "", kinetic::IGNORE_VERSION, record, callback);

    connection->nonblocking().Run(&read_fds, &write_fds, &num_fds);
    while (remaining > 0) {
@@ -106,10 +90,6 @@ int main(int argc, char* argv[]) {
        return 1;
    }

    google::protobuf::ShutdownProtobufLibrary();
    google::ShutdownGoogleLogging();
    google::ShutDownCommandLineFlags();

    printf("Done!\n");

    return 0;