#include <algorithm>
#include <fstream>
#include <iostream>
#include "cbe/Account.h"
#include "cbe/CloudBackend.h"
#include "cbe/Container.h"
#include "cbe/Stream.h"
#include "cbe/Object.h"
#include "cbe/QueryChain.h"
#include "cbe/util/ErrorInfo.h"
#include "MyDelegates.cpp"
#include "../user_credentials.cpp"
int main(int argc, const char** argv) {
static constexpr const char testContainer[] = "z-MediumExample";
std::shared_ptr<MyLogInDelegate> myLogInDelegate =
std::make_shared<MyLogInDelegate>();
password,
tenant,
client,
myLogInDelegate);
myLogInDelegate->waitForRsp();
if (myLogInDelegate->error) {
std::cout << "Failed to login: error={" << myLogInDelegate->errorInfo << '}'
<< std::endl;
return 1;
}
cloudBackend = myLogInDelegate->cloudBackend;
myLogInDelegate.reset();
std::shared_ptr<MyQueryDelegate> myQueryDelegate =
std::make_shared<MyQueryDelegate>();
rootContainer.query(myQueryDelegate).getQueryResult();
myQueryDelegate->waitForRsp();
if (entry.type() == cbe::ItemType::Container &&
entry.name() == testContainer){
std::cout << "Using existing container:" << std::endl;
}
}
if (!myContainer){
std::cout << "Creating a new container:" << std::endl;
std::shared_ptr<MyCreateContainerDelegate> myCreateContainerDelegate =
std::make_shared<MyCreateContainerDelegate>();
rootContainer.createContainer(testContainer, myCreateContainerDelegate);
myCreateContainerDelegate->waitForRsp();
myContainer = myCreateContainerDelegate->container;
}
std::cout << "home://"
<< myContainer.name()
<< "/"
<< std::endl;
constexpr const char* const uploadPath = "/tmp/";
constexpr const char* const myObjectFileName = "Medium_A_a_message";
const std::string qualFile1Name = std::string{uploadPath} +
myObjectFileName;
std::cout << "Create local file " << qualFile1Name << std::endl;
std::ofstream ofs{qualFile1Name};
ofs << "Line a\n"
<< "Line b\n"
<< "Line c\n"
<< std::flush;
ofs.close();
std::cout << "Uploading object from "
<< qualFile1Name
<< std::endl;
std::shared_ptr<MyUploadDelegate> myUploadDelegate =
std::make_shared<MyUploadDelegate>();
myContainer.upload(qualFile1Name, myUploadDelegate);
myObject = myUploadDelegate->waitForRsp();
if (!myObject) {
std::cout << "Error! " << myUploadDelegate->errorInfo << std::endl;
}
std::shared_ptr<MyGetStreamsDelegate> getStreamsDelegate =
std::make_shared<MyGetStreamsDelegate>();
myObject.getStreams(getStreamsDelegate);
getStreamsDelegate->waitForRsp();
if (getStreamsDelegate->errorInfo) {
std::cout << "Error! " << myLogInDelegate->errorInfo << std::endl;
}
streams = *getStreamsDelegate->streams;
constexpr const char* const myFile2Name = "Medium_A_an_attachment";
const std::string qualFile2Name = std::string{uploadPath} + myFile2Name;
std::ofstream ofs2{qualFile2Name};
ofs2 << "line 21\n"
<< "line 22\n"
<< "line 23\n"
<< "line 24\n"
<< std::flush;
ofs2.close();
std::cout << "Uploading additional stream from: "
<< qualFile2Name
<< std::endl;
const auto nextStreamId =
std::max_element(std::begin(streams), std::end(streams),
})->streamId + 1;
std::shared_ptr<MyUploadStreamDelegate> myUploadStreamDelegate =
std::make_shared<MyUploadStreamDelegate>();
myObject.uploadStream(qualFile2Name, nextStreamId, myUploadStreamDelegate);
myUploadStreamDelegate->waitForRsp();
if (myUploadStreamDelegate->errorInfo) {
std::cout << "Error! " << myLogInDelegate->errorInfo << std::endl;
}
std::cout << "home://"
<< myContainer.name()
<< "/"
<< myObject.name()
<< " now has two streams."
<< std::endl;
myObject.getStreams(getStreamsDelegate);
getStreamsDelegate->waitForRsp();
if (getStreamsDelegate->errorInfo) {
std::cout << "Error! " << myLogInDelegate->errorInfo << std::endl;
}
streams = *getStreamsDelegate->streams;
std::cout << "Downloading stream:" << std::endl;
std::shared_ptr<MyDownloadDelegate> myDownloadDelegate =
std::make_shared<MyDownloadDelegate>();
constexpr const char* const downloadPath = "/tmp/Medium_A_download_stream_";
for (const auto& stream : streams) {
const auto path = std::string{downloadPath} +
std::to_string(stream.streamId) + "_of_";
std::cout << stream.streamId
<< " to: "
<< path
<< myObject.name()
<< std::endl;
myObject.downloadStream(path, stream, myDownloadDelegate);
myDownloadDelegate->waitForRsp();
if (!myDownloadDelegate) {
std::cout << "Error! " << myDownloadDelegate->errorInfo << std::endl;
}
}
std::cout << "Content of "
<< myContainer.name()
<< std::endl;
myContainer.query(objectFilter, myQueryDelegate);
myQueryDelegate->waitForRsp();
if (myQueryDelegate->errorInfo) {
std::cout << "Error! " << myQueryDelegate->errorInfo << std::endl;
} else {
myQueryDelegate->queryResult.getItemsSnapshot();
std::cout << "--------- table content -----------" << std::endl;
for (auto& item : itemsSnapshot) {
std::cout << " sum "
<< " Bytes\t"
<< anObject.name()
<< std::endl;
}
std::cout << "-----------------------------------" << std::endl;
}
std::cout << "Example Medium done" << std::endl;
}
cbe::Container rootContainer() const
Returns the rootContainer for the account.
std::string username() const
Returns the username of the account.
The session that holds the connection with the cloud.
Definition: CloudBackend.h:47
static cbe::CloudBackend logIn(const std::string &username, const std::string &password, const std::string &tenant, const std::string &client, LogInDelegatePtr delegate)
Logs in to the CloudBackend service.
static cbe::Container castContainer(cbe::Item item)
Casts an item to a container.
void terminate()
Terminates the CloudBackend service.
static cbe::Object castObject(cbe::Item item)
Casts an item to an object.
cbe::Account account()
Returns an account object with information on the user.
A collection of Item, can also represent a table or folder.
Definition: Container.h:63
Use to select Item that meets specific criterias when doing a query.
Definition: Filter.h:40
cbe::Filter & setDataType(cbe::ItemType itemType)
Set which datatypes to query for.
Holder of a set of data, can represent a table row.
Definition: Object.h:64
uint64_t length() const
Returns the binary length/size in bytes of current object.
cbe::Streams Streams
Collection of Stream objects.
Definition: Object.h:878
resultset of data retrieved.
Definition: QueryResult.h:51
ItemsSnapshot getItemsSnapshot() const
Returns a copy of a vector containing the items for this QueryResult.
std::vector< Item > ItemsSnapshot
A vector of items to hold a snapshot.
Definition: QueryResult.h:61
A data file attached to Object.
Definition: Stream.h:24
cbe::StreamId streamId
Definition: Stream.h:30
DefaultCtor
Default constructor marker.
Definition: Types.h:204