Problems with libbz2 - c

I can't get libbz2 to work. After 10 000 characters it terminates with end of stream although the input file contains several gigabytes. I just cannot figure out what goes wrong. Thank you for any remarks.
Here is my code:
string read_bz2_file(const string& filename) {
string result;
FILE* f;
BZFILE* b;
const int BUFSIZE = 1000;
char buf[BUFSIZE];
int bzerror;
f = fopen(filename.c_str(), "r");
if (!f) {
/* handle error */
cerr << "cannot read file " << filename << endl;
return "";
}
b = BZ2_bzReadOpen(&bzerror, f, 0, 0, NULL, 0);
if (bzerror != BZ_OK) {
BZ2_bzReadClose(&bzerror, b);
cerr << "cannot read file " << filename << endl;
return "";
/* handle error */
}
bzerror = BZ_OK;
while (bzerror == BZ_OK /* arbitrary other conditions */) {
BZ2_bzRead(&bzerror, b, buf, BUFSIZE /* size of buf */);
cout << "bzerror: " << bzerror << endl;
if (bzerror == BZ_OK) {
/* do something with buf[0 .. nBuf-1] */
result.append(buf);
}
}
if (bzerror != BZ_STREAM_END) {
BZ2_bzReadClose(&bzerror, b);
cerr << "error while reading file " << filename << endl;
return "";
/* handle error */
} else {
result.append(buf);
BZ2_bzReadClose(&bzerror, b);
return result;
}
return result;
}

You can try to open the file in binary mode, adding a "b" to the fopen mode:
f = fopen(filename.c_str(), "rb");

Related

ffmpeg rtsp on C

I need to write RTSP steram from IP-cam to file. I use FFMPEG to do this. I found code example on C++, but i need to use only C. Can anyone help me?
i have problems in file operations. how can i write stream to file?
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <sstream>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavformat/avio.h>
#include <libswscale/swscale.h>
}
int main(int argc, char** argv) {
// Open the initial context variables that are needed
SwsContext *img_convert_ctx;
AVFormatContext* format_ctx = avformat_alloc_context();
AVCodecContext* codec_ctx = NULL;
int video_stream_index;
// Register everything
av_register_all();
avformat_network_init();
//open RTSP
if (avformat_open_input(&format_ctx, "rtsp://134.169.178.187:8554/h264.3gp",
NULL, NULL) != 0) {
return EXIT_FAILURE;
}
if (avformat_find_stream_info(format_ctx, NULL) < 0) {
return EXIT_FAILURE;
}
//search video stream
for (int i = 0; i < format_ctx->nb_streams; i++) {
if (format_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
video_stream_index = i;
}
AVPacket packet;
av_init_packet(&packet);
//open output file
AVFormatContext* output_ctx = avformat_alloc_context();
AVStream* stream = NULL;
int cnt = 0;
//start reading packets from stream and write them to file
av_read_play(format_ctx); //play RTSP
// Get the codec
AVCodec *codec = NULL;
codec = avcodec_find_decoder(AV_CODEC_ID_H264);
if (!codec) {
exit(1);
}
// Add this to allocate the context by codec
codec_ctx = avcodec_alloc_context3(codec);
avcodec_get_context_defaults3(codec_ctx, codec);
avcodec_copy_context(codec_ctx, format_ctx->streams[video_stream_index]->codec);
std::ofstream output_file;
if (avcodec_open2(codec_ctx, codec, NULL) < 0)
exit(1);
img_convert_ctx = sws_getContext(codec_ctx->width, codec_ctx->height,
codec_ctx->pix_fmt, codec_ctx->width, codec_ctx->height, AV_PIX_FMT_RGB24,
SWS_BICUBIC, NULL, NULL, NULL);
int size = avpicture_get_size(AV_PIX_FMT_YUV420P, codec_ctx->width,
codec_ctx->height);
uint8_t* picture_buffer = (uint8_t*) (av_malloc(size));
AVFrame* picture = av_frame_alloc();
AVFrame* picture_rgb = av_frame_alloc();
int size2 = avpicture_get_size(AV_PIX_FMT_RGB24, codec_ctx->width,
codec_ctx->height);
uint8_t* picture_buffer_2 = (uint8_t*) (av_malloc(size2));
avpicture_fill((AVPicture *) picture, picture_buffer, AV_PIX_FMT_YUV420P,
codec_ctx->width, codec_ctx->height);
avpicture_fill((AVPicture *) picture_rgb, picture_buffer_2, AV_PIX_FMT_RGB24,
codec_ctx->width, codec_ctx->height);
while (av_read_frame(format_ctx, &packet) >= 0 && cnt < 1000) { //read ~ 1000 frames
std::cout << "1 Frame: " << cnt << std::endl;
if (packet.stream_index == video_stream_index) { //packet is video
std::cout << "2 Is Video" << std::endl;
if (stream == NULL) { //create stream in file
std::cout << "3 create stream" << std::endl;
stream = avformat_new_stream(output_ctx,
format_ctx->streams[video_stream_index]->codec->codec);
avcodec_copy_context(stream->codec,
format_ctx->streams[video_stream_index]->codec);
stream->sample_aspect_ratio =
format_ctx->streams[video_stream_index]->codec->sample_aspect_ratio;
}
int check = 0;
packet.stream_index = stream->id;
std::cout << "4 decoding" << std::endl;
int result = avcodec_decode_video2(codec_ctx, picture, &check, &packet);
std::cout << "Bytes decoded " << result << " check " << check
<< std::endl;
if (cnt > 100) //cnt < 0)
{
sws_scale(img_convert_ctx, picture->data, picture->linesize, 0,
codec_ctx->height, picture_rgb->data, picture_rgb->linesize);
std::stringstream file_name;
file_name << "test" << cnt << ".ppm";
output_file.open(file_name.str().c_str());
output_file << "P3 " << codec_ctx->width << " " << codec_ctx->height
<< " 255\n";
for (int y = 0; y < codec_ctx->height; y++) {
for (int x = 0; x < codec_ctx->width * 3; x++)
output_file
<< (int) (picture_rgb->data[0]
+ y * picture_rgb->linesize[0])[x] << " ";
}
output_file.close();
}
cnt++;
}
av_free_packet(&packet);
av_init_packet(&packet);
}
av_free(picture);
av_free(picture_rgb);
av_free(picture_buffer);
av_free(picture_buffer_2);
av_read_pause(format_ctx);
avio_close(output_ctx->pb);
avformat_free_context(output_ctx);
return (EXIT_SUCCESS);
}
please help me to compile this by C-compiler.

How to set extended controls in V4L2 correctly?

I want to record a video from a V4L2 device (from the Raspberry Pi camera) in C.
The recording itself works and I can save the video to a file.
However I need to change the bitrate of the video. From the strace output of the v4l2-ctl --set-ctrl video_bitrate=10000000 command I know that the extended controls API of v4l2 is used to achieve this.
Here's my code which doesn't work so far:
#include <iostream>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/mman.h> //mmap
#include <fcntl.h>
#include <unistd.h>
#include <linux/videodev2.h>
using namespace std;
#define numbuffers 3
struct picturebuffer
{
void *startadress;
size_t length;
};
//array in which the buffer pointer are being stored
picturebuffer pb[numbuffers];
int main()
{
//open camera
int fd;
fd = open("/dev/video0", O_RDWR);
if(fd < 0)
{
cout << "error during opening the camera device!";
cout.flush();
}
cout << "camera opened";
//read capabilities
struct v4l2_capability caps;
if(ioctl(fd, VIDIOC_QUERYCAP, &caps) < 0)
{
cout << "error while reading the capabilities!";
cout.flush();
}
cout << "Capabilities " << caps.capabilities << endl;
//ToDo: check for required capabilities
//set image data
struct v4l2_format format;
format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
format.fmt.pix.pixelformat = V4L2_PIX_FMT_H264;
format.fmt.pix.width = 1920;
format.fmt.pix.height = 1080;
if(ioctl(fd, VIDIOC_S_FMT, &format) < 0)
{
cout << "error in the image format";
}
cout << "Image properties set" << endl;
//Todo: check if width and height fit together (VIDIOC_ENUM_FRAMESIZES)
//set extended Controls
struct v4l2_ext_controls ecs;
struct v4l2_ext_control ec;
memset(&ecs, 0, sizeof(ecs));
memset(&ec, 0, sizeof(ec));
ec.id = V4L2_CID_MPEG_VIDEO_BITRATE;
ec.value = 10000000;
ec.size = 0;
ecs.controls = &ec;
ecs.count = 1;
ecs.ctrl_class = V4L2_CTRL_CLASS_MPEG;
if(ioctl(fd, VIDIOC_S_EXT_CTRLS, &ecs) < 0)
{
cout << "error in extended controls bitrate";
cout.flush();
}
//allocate buffer in the kernel
struct v4l2_requestbuffers req;
req.count = numbuffers;
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
req.memory = V4L2_MEMORY_MMAP;
if(ioctl(fd, VIDIOC_REQBUFS, &req) < 0)
{
cout << "errro while allocating buffer";
cout.flush();
}
cout << "number of buffers: " << req.count << endl;
cout.flush();
//map buffers into userspace
for(int i=0; i<numbuffers; i++)
{
struct v4l2_buffer bufferinfo;
memset(&bufferinfo, 0, sizeof(bufferinfo));
bufferinfo.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
bufferinfo.memory = V4L2_MEMORY_MMAP;
bufferinfo.index = i;
if(ioctl(fd, VIDIOC_QUERYBUF, &bufferinfo) < 0)
{
cout << "error while querying bufferinfo";
cout.flush();
}
pb[i].startadress = mmap(NULL, bufferinfo.length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, bufferinfo.m.offset);
pb[i].length = bufferinfo.length;
if(pb[i].startadress == MAP_FAILED)
{
cout << "error during mmap" << endl;
}
memset(pb[i].startadress, 0, bufferinfo.length);
cout << "size of buffer: " << bufferinfo.length << endl;
}
cout << "buffers mapped into userspace" << endl;
cout.flush();
//queue in the buffers
for(int i=0; i<numbuffers; i++)
{
struct v4l2_buffer bufferinfo;
memset(&bufferinfo, 0, sizeof(bufferinfo));
bufferinfo.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
bufferinfo.memory = V4L2_MEMORY_MMAP;
bufferinfo.index = i;
if(ioctl(fd, VIDIOC_QBUF, &bufferinfo) < 0)
{
cout << "error while queueing the buffers in" << endl;
}
}
//since that point the driver starts capturing the pics
int type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if(ioctl(fd, VIDIOC_STREAMON, &type) < 0)
{
cout << "error while starting the stream" << endl;
}
int file;
if((file = open("/home/pi/image.h264", O_WRONLY | O_CREAT, 0660)) < 0)
{
cout << "error while writing the file";
}
//loop for managing the pics
for(int i=0; i<100; i++)
{
struct v4l2_buffer bufferinfo;
memset(&bufferinfo, 0, sizeof(bufferinfo));
bufferinfo.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
bufferinfo.memory = V4L2_MEMORY_MMAP;
if(ioctl(fd, VIDIOC_DQBUF, &bufferinfo) < 0)
{
cout << "error while getting the buffer!" << endl;
}
//do anything with the pic
char buf[pb[bufferinfo.index].length];
memcpy(&buf, pb[bufferinfo.index].startadress, pb[bufferinfo.index].length);
cout << bufferinfo.index << endl;
cout.flush();
//write picture into the file
write(file, pb[bufferinfo.index].startadress, pb[bufferinfo.index].length);
if(ioctl(fd, VIDIOC_QBUF, &bufferinfo) < 0)
{
cout << "error while enqueuing the buffer" << endl;
}
}
close(file);
if(ioctl(fd, VIDIOC_STREAMOFF, &type) < 0)
{
cout << "error while stopping the stream" << endl;
}
//clean up
for(int i=0; i<numbuffers; i++)
{
if(munmap(pb[i].startadress, pb[i].length) < 0)
{
cout << "error during unmap";
}
}
//close camera file
close(fd);
cout << "!!!Hello World!!!" << endl;
cout.flush();
return 0;
}
The ioctl call seems to succeed, however my output file always has the same size as of 199,2 MB. Does someone know what´s wrong in the code ?
You need to check if the camera driver supports that IOCTL command. If the driver doesn't support the IOCTL command by not implementing it, you still can execute the command and it is routed to v4l2 default implementation, no actual changes are applied to the camera setting
Try to change the lines:
pb[bufferinfo.index].length
By:
pb[bufferinfo.index].bytesused
For example:
write(file, pb[bufferinfo.index].startadress, pb[bufferinfo.index].bytesused);

avformat_write_header fails with error "invalid argument

I'm trying to write a chunk of a mp4 file to a .ts file, which does not yet exist.
So first I create AVFormatContext this way:
AVOutputFormat *oformat = av_guess_format( NULL,
"mpegts",
"video/x-mpegts" );
AVFormatContext * outFormatContext = NULL;
avformat_alloc_output_context2( &outFormatContext, oformat, NULL, dst_file)
outFormatContext is created and outFormatContext->oformat is set to oformat. Then I open I/O context on outFormatContext:
avio_open(&outFormatContext->pb, dst_file, AVIO_FLAG_WRITE)
avformat_write_header( outFormatContext, NULL );
according to this this is enough for avformat_write_header to work but it fails with error "invalid argument".
What else needs to be done to successfully write the header?
Although it's a little bit late, I hope it would be helpful.
Here is my way to call avformat_write_header, you should have a stream provided to write the header.
AVFormatContext* pOutFormatContext;
AVOutputFormat* avOutputFormat;
if ((avOutputFormat = av_guess_format(NULL, "mp4", "video/mp4")) == NULL) {
cerr << "Could not guess output format" << endl;
return -1;
}
avformat_alloc_output_context2(&pOutFormatContext,
av_guess_format("mp4", NULL, "video/mp4"), NULL, NULL);
if (pOutFormatContext == NULL) {
cerr << "Could not allocate output context" << endl;
return -1;
}
for (int i = 0; i < avFormatContext->nb_streams; i++) {
AVStream* inAVStream = avFormatContext->streams[i];
AVStream* outAVStream = avformat_new_stream(pOutFormatContext, inAVStream->codec->codec);
if (avcodec_copy_context(outAVStream->codec, inAVStream->codec) < 0) {
cerr << "Failed to copy codec context" << endl;
return -1;
}
outAVStream->codec->codec_tag = 0;
if (pOutFormatContext->oformat->flags & AVFMT_GLOBALHEADER) {
outAVStream->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
}
}
avio_open(&pOutFormatContext->pb, "test.mp4", AVIO_FLAG_READ_WRITE);
if (pOutFormatContext->pb == NULL) {
cerr << "Could not open for writing" << endl;
return -1;
}
if (avformat_write_header(pOutFormatContext, NULL) != 0) {
cerr << "Could not write header" << endl;
return -1;
}

System Calls: Outputing file and pausing every 20 lines

So I'm reading in files and outputting the contents of the files onto the console using system calls. Also I want to add a space to the output, every 20 lines I encounter. This is where I'm having trouble, in that despite the following few lines of code, the entire file is being displayed without the spaces
//Write file contenst
while( (nReadFile = read(nOpenFile, buffer, 1) != 0))
{
write(1, &nLineCount, sizeof(nLineCount));
if(nLineCount == 20)
{
write(1, "\n", 2);
nLineCount = 0;
}
if(nReadFile = write(1, buffer, nReadFile) == '\n')
{
nLineCount++;
}
}
Here is the entire program (Excluding the .h file containing the libraries)
#include"mymore.h"
int main(int argCount, char *argv[])
{
struct termios initial_settings, new_settings;
tcgetattr(fileno(stdin), &initial_settings);
new_settings=initial_settings;
new_settings.c_lflag &= ~ICANON;
new_settings.c_lflag &= ~ECHO;
new_settings.c_cc[VMIN] = 1;
new_settings.c_cc[VTIME] = 0;
if (tcsetattr(fileno(stdin), TCSANOW, &new_settings)!=0)
{
fprintf(stderr, "could not set attributes\n");
}
int nLineCount = 0;
int nCheckFile = 0;
int nFileCountCounter = 1; //first arguement interested in is argv[1]
int nOpenFile = 0;
int nReadFile = 0;
int nCount = 0;
char *cData;
char buffer[0];
//check that arguements have been provided
if( argCount < 2)
{
write(1, "There needs to be at least one file provided! \n", 50);
return 1;
}
do
{
printf("%d", argCount);
//check if file exists
nCheckFile = access(argv[nFileCountCounter], F_OK);
if(nCheckFile != 0) //if file does not exist
{
write(1, "The file ", 10);
write(1, argv[nFileCountCounter], strlen(argv[1]));
write(1," does not exist! \n", 25);
return 1;
}
else //file does exist
{
write(1, "Opening ", 10);
write(1, argv[nFileCountCounter], strlen(argv[1]));
write(1, "\n", 2);
}
//open the file
nOpenFile = open(argv[nFileCountCounter], O_RDONLY);
if(nOpenFile < 0)
{
write(1, "Failed to open file ", 10);
write(1, argv[nFileCountCounter], strlen(argv[nFileCountCounter]));
write(1, "\n", 2);
return 1;
}
//read file
cData = (char *) malloc(100 * sizeof(char));
cData[nReadFile] = '\0';//append null terminator
//find length of source file
while(cData[nCount] != 0)
{
nCount++;
}
//Write file contenst
while( (nReadFile = read(nOpenFile, buffer, 1) != 0))
{
write(1, &nLineCount, sizeof(nLineCount));
if(nLineCount == 20)
{
write(1, "\n", 2);
nLineCount = 0;
}
if(nReadFile = write(1, buffer, nReadFile) == '\n')
{
nLineCount++;
}
}
cout << nLineCount << endl;
//close file
close(nOpenFile);
//Increment to next file
nFileCountCounter++;
}while(nFileCountCounter < argCount);//while there are still arguements
tcsetattr(fileno(stdin), TCSANOW, &initial_settings);
return 0;
}
This is actually my first experience using system calls, and one thing I think I've noticed is that the write command is being executed before any of the c code?
Any ideas?
Thanks
There are plenty of mistakes in your code:
char buffer[0]; should be char buffer[1];
char buffer[0]; defines a buffer which can store zero character, I suppose it is not what you want.
write(1, "There needs to be at least one file provided! \n", 50); should be
char *msg = "There needs to be at least one file provided! \n";
write(1, msg, strlen(msg));
the message is longer than 50. There are serval similar mistakes, and can be fixed similarly.
while( (nReadFile = read(nOpenFile, buffer, 1) != 0)) should be
while( (nReadFile = read(nOpenFile, buffer, 1)) != 0)
In C, operator != has higher precedence than =, so nReadFile = read(nOpenFile, buffer, 1) != 0 meas nReadFile = (read(nOpenFile, buffer, 1) != 0).
if(nReadFile = write(1, buffer, nReadFile) == '\n')
If you want to check whether the currect character is a newline, you should to something like if (buffer[0] == '\n').
cout << nLineCount << endl;
This is C++, not C.

Example Code for MemCached in C

I am looking for some sample C code for using memcache to set a value
Connecting to Server/Port
using multiple memcache_set
Close
I have the app running in PHP in 5 lines of code but can not find any good memcache samples in C which I need to port to.
This is a great memcached sample in C
#include <libmemcached/memcached.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv) {
//memcached_servers_parse (char *server_strings);
memcached_server_st *servers = NULL;
memcached_st *memc;
memcached_return rc;
char *key = "keystring";
char *value = "keyvalue";
char *retrieved_value;
size_t value_length;
uint32_t flags;
memc = memcached_create(NULL);
servers = memcached_server_list_append(servers, "localhost", 11211, &rc);
rc = memcached_server_push(memc, servers);
if (rc == MEMCACHED_SUCCESS)
fprintf(stderr, "Added server successfully\n");
else
fprintf(stderr, "Couldn't add server: %s\n", memcached_strerror(memc, rc));
rc = memcached_set(memc, key, strlen(key), value, strlen(value), (time_t)0, (uint32_t)0);
if (rc == MEMCACHED_SUCCESS)
fprintf(stderr, "Key stored successfully\n");
else
fprintf(stderr, "Couldn't store key: %s\n", memcached_strerror(memc, rc));
retrieved_value = memcached_get(memc, key, strlen(key), &value_length, &flags, &rc);
printf("Yay!\n");
if (rc == MEMCACHED_SUCCESS) {
fprintf(stderr, "Key retrieved successfully\n");
printf("The key '%s' returned value '%s'.\n", key, retrieved_value);
free(retrieved_value);
}
else
fprintf(stderr, "Couldn't retrieve key: %s\n", memcached_strerror(memc, rc));
return 0;
}
Here is a slightly more complete answer, which also shows how to get and delete from a memcached server. The code is technically in C++, but doesn't use any features that would be unfamiliar to most C programmers.
To compile: g++ sample.cc -o sample -ggdb -O3 -lmemcached
To run: ./sample -s localhost -p 11211
The functions whose names begin with mcd_ show the basics of inserting, deleting, and getting, as well as connecting and disconnecting from memcached. The remaining code makes use of those functions to insert 50 key/value pairs, verify they all are in the server, remove half the pairs, and then verify that the right keys remain / are gone.
#include <iostream>
#include <string>
#include <unistd.h>
#include <vector>
#include <libmemcached/memcached.h>
// NB: I know that `using namespace std` at global scope is bad form, and that
// global variables are bad form. However, they help keep this answer under
// 200 lines.
using namespace std;
/// The connection to memcached
memcached_st *mcd;
/// A global set of key/value pairs that we manufacture for the sake of this
/// example
vector<pair<string, string>> kv_pairs;
/// Put a key/value pair into memcached with expiration 0, no special flags
bool mcd_set(const string &key, const string &val) {
auto rc = memcached_set(mcd, key.c_str(), key.length(), val.c_str(),
val.length(), (time_t)0, (uint32_t)0);
if (rc == MEMCACHED_SUCCESS)
return true;
cout << "Error in mcd_set(): " << memcached_strerror(mcd, rc) << endl;
return false;
}
/// Delete a key/value pair from memcached. return true on success
/// NB: `(time_t)0` is an expiration of `0`, i.e., immediately
bool mcd_delete(const string &key) {
auto rc = memcached_delete(mcd, key.c_str(), key.length(), (time_t)0);
if (rc == MEMCACHED_SUCCESS)
return true;
cout << "Error in mcd_delete(): " << memcached_strerror(mcd, rc) << endl;
return false;
}
/// Get a value from the kv store, using its key to do the lookup. return
/// true on success. On success, the by-ref out parameter `val` will be set.
bool mcd_get(const string &key, string &val) {
memcached_return rc;
size_t len;
uint32_t flags = 0;
char *res = memcached_get(mcd, key.c_str(), key.length(), &len, &flags, &rc);
if (rc == MEMCACHED_SUCCESS) {
val = string(res, len);
free(res);
return true;
}
// NB: skip next line, because we don't want error messages on a failed get:
//
// cout << "Error in mcd_get(): " << memcached_strerror(mcd, rc) << endl;
return false;
}
/// Connect to a single memcached server on the provided port
bool mcd_connect(const string &servername, const int port) {
mcd = memcached_create(nullptr);
memcached_return rc;
memcached_server_st *servers = nullptr;
servers =
memcached_server_list_append(servers, servername.c_str(), port, &rc);
rc = memcached_server_push(mcd, servers);
if (rc == MEMCACHED_SUCCESS) {
cout << " Successfully connected to " << servername << ":" << port << endl;
return true;
}
cout << "Error in mcd_connect(): " << memcached_strerror(mcd, rc) << endl;
return false;
}
/// Close the connection to memcached
void mcd_shutdown() {
memcached_free(mcd);
cout << " Successfully disconnected\n";
}
/// Create a bunch of key/value pairs
void build_kv_pairs(int howmany) {
for (int i = 0; i < howmany; ++i) {
string key = "key" + to_string(i) + "_______";
string val = "val" + to_string(i);
for (int i = 0; i < 100; ++i)
val += ("_" + to_string(i));
kv_pairs.push_back({key, val});
}
}
/// insert a bunch of k/v pairs into memcached
bool put_kv_pairs(int howmany) {
for (int i = 0; i < howmany; ++i) {
if (!mcd_set(kv_pairs[i].first, kv_pairs[i].second)) {
cout << "Error inserting key `" << kv_pairs[i].first << "`\n";
return false;
}
}
cout << " put_kv_pairs(" << howmany << ") completed successfully\n";
return true;
}
/// Remove a sequence of keys from memcached
///
/// NB: Here and below, we use first/last/stride so that we can vary wich
/// key/value pairs are operated on
bool delete_kv_pairs(int first, int last, int stride) {
for (int i = first; i <= last; i += stride) {
if (!mcd_delete(kv_pairs[i].first)) {
cout << "Error removing key `" << kv_pairs[i].first << "`\n";
return false;
}
}
cout << " delete_kv_pairs(" << first << ", " << last << ", " << stride
<< ") completed successfully\n";
return true;
}
/// Verify that a sequence of keys is in memcached, with the right expected
/// values
bool check_present_pairs(int first, int last, int stride) {
for (int i = first; i <= last; i += stride) {
string value;
if (!mcd_get(kv_pairs[i].first, value)) {
cout << "Error getting key `" << kv_pairs[i].first
<< "`: key not found\n";
return false;
}
if (value != kv_pairs[i].second) {
cout << "Value error while getting key `" << kv_pairs[i].first << "`\n";
cout << " Expected: `" << kv_pairs[i].second << "`\n";
cout << " Found: `" << value << "`\n";
return false;
}
}
cout << " check_present_pairs(" << first << ", " << last << ", " << stride
<< ") completed successfully\n";
return true;
}
/// Verify that a sequence of keys is *not* in memcached
bool check_missing_pairs(int first, int last, int stride) {
for (int i = first; i <= last; i += stride) {
string value;
if (mcd_get(kv_pairs[i].first, value)) {
cout << "Error getting key `" << kv_pairs[i].first
<< "`: key unexpectedly found\n";
return false;
}
}
cout << " check_missing_pairs(" << first << ", " << last << ", " << stride
<< ") completed successfully\n";
return true;
}
int main(int argc, char **argv) {
// Parse args to get server name (-s) and port (-p)
string servername = "";
int port;
long opt;
while ((opt = getopt(argc, argv, "s:p:")) != -1) {
switch (opt) {
case 's':
servername = string(optarg);
break;
case 'p':
port = atoi(optarg);
break;
}
}
// Create a bunch of key/value pairs to use for the experiments
int howmany = 50;
build_kv_pairs(howmany);
// Connect to memcached
mcd_connect(servername, port);
// insert all the pairs, make sure they are all present
put_kv_pairs(howmany);
check_present_pairs(0, howmany - 1, 1);
// Delete the even pairs
delete_kv_pairs(0, howmany - 2, 2);
// ensure the odd pairs are present
check_present_pairs(1, howmany - 1, 2);
// ensure the even pairs are not present
check_missing_pairs(0, howmany, 2);
mcd_shutdown();
}
The code should produce output like the following:
Successfully connected to localhost:11211
put_kv_pairs(50) completed successfully
check_present_pairs(0, 49, 1) completed successfully
delete_kv_pairs(0, 48, 2) completed successfully
check_present_pairs(1, 49, 2) completed successfully
check_missing_pairs(0, 50, 2) completed successfully
Successfully disconnected

Resources