C++ problem removing all items in a bucket
I am using some of the example documents to try and remove all the items from a bucket. I am using C++ and Couchbase 2.0.
I am using the following:
Complete_Callback
/*
*This Function takes in a view from a bucket. This is stored in a JSON object and parsed for the keys.
*Inside the for loop I am calling remove on all the keys.
*/
static void complete_callback(lcb_http_request_t request,lcb_t instance, const void *cookie, lcb_error_t error,const lcb_http_resp_t *resp)
{
const void *buffer = resp->v.v0.bytes;
const char *temp = (const char *)buffer;
QString qs = temp;
QByteArray json = qs.toLocal8Bit();
QJson::Parser parser;
bool ok;
QVariantMap result = parser.parse (json, &ok).toMap();
if (!ok) {
qFatal("An error occurred during parsing");
exit (1);
}
int i = result["total_rows"].toInt();
QList<QVariant> rows = result["rows"].toList();
if(i>0)
{
for(int k = 0; k<i; k++)
{
QVariantMap rows2 = rows[k].toMap();
QString key = rows2["id"].toString();
string skey = key.toStdString();
lcb_remove_cmd_t *remove = static_cast<lcb_remove_cmd_t *>(calloc(1, sizeof(*remove)));
remove->version = 0;
remove->v.v0.key = skey.c_str();
remove->v.v0.nkey = skey.length();
remove->v.v0.cas = 0x000;
lcb_remove_cmd_t* commands[] = { remove };
lcb_remove(instance, NULL, 1, commands);
lcb_wait(instance);
}
}
lcb_wait(instance);
}Main Function
/*
*In this function I connect to couchbase and then query a view. After the view is called it enters
*the callback function above and then all the current items are removed. At the end of this function
*I add the new Documents to the bucket.
*/
void addCouchbase()
{
create_options.v.v0.host ="localhost:8091";
create_options.v.v0.user = "username";
create_options.v.v0.passwd = "password";
create_options.v.v0.bucket = "schedule";
err = lcb_create(&instance, &create_options);
if (err != LCB_SUCCESS) {
qDebug("Failed to create libcouchbase instance");
}
if ((err = lcb_connect(instance)) != LCB_SUCCESS) {
qDebug("Failed to initiate connect");
}
(void)lcb_set_http_complete_callback(instance, complete_callback);
(void)lcb_set_store_callback(instance, store_callback);
lcb_wait(instance);
lcb_error_t rc;
//Query the getKeys View
lcb_http_cmd_t cmd ;
cmd.version = 0;
cmd.v.v0.path = "_design/getKeys/_view/geyKeys?connection_timeout=1000&skip=0";
cmd.v.v0.npath = strlen(cmd.v.v0.path);
cmd.v.v0.body =NULL;
cmd.v.v0.nbody = 0;
cmd.v.v0.method = LCB_HTTP_METHOD_GET;
cmd.v.v0.chunked = 0;
cmd.v.v0.content_type = "application/json";
rc = lcb_make_http_request(instance, NULL, LCB_HTTP_TYPE_VIEW, &cmd, NULL);
lcb_wait(instance);
int itSize;
QString kt ;
QString tj;
//Items are stored in couchbase in the For-Loop below
for(int i = 0; i < 7;i++)
{
QStringList dl = descriptionLists[i];
itSize = dl.length();
if(dl.length()>0)
{
for(int j = 0; j < itSize; j++)
{
string json;
string key;
lcb_store_cmd_st *store = static_cast<lcb_store_cmd_st *>(calloc(1, sizeof(*store)));
tj = "Json stuff";
json = tj.toStdString();
store->v.v0.bytes = json.c_str();
store->v.v0.nbytes = json.size();
//In actual code key is my UTC time + Milliseconds
kt = "Random Key";
key = kt.toStdString();
store->v.v0.key = key.c_str();
store->v.v0.nkey = key.size();
store->v.v0.cas = 0x000;
store->v.v0.operation = LCB_SET;
lcb_store_cmd_st* commands[] = { store };
err = lcb_store(instance, NULL, 1, commands);
if (err != LCB_SUCCESS) {
qDebug("Failed to set");
}
lcb_wait(instance);
}
}
}
lcb_wait(instance);
lcb_flush_buffers(instance,NULL);
lcb_destroy(instance);
}I am having two major problems:
1. When this is ran, only some of the items are removed. Sometimes it works correctly but mostly is just removes
the exact amount I am adding. Sometimes it does not remove at all. This causes the bucket to fill rapidly.
Example: 0 Items in bucket
Call 1: 0 Items removed 9 Inserted = 9 Items in bucket
Call 2: 0 Items removed 9 Inserted = 18 Items in bucket
Call 3: 9 Items removed 9 Inserted = 18 Items in bucket
2. When I call the function again without reloading a new instance of the program the view data I receive is out dated.
Example: 19 items in bucket.
Call 1: 19 Items removed 9 Items added = 9 Items in bucket.
Call 2: 19 Items are still returned in the view but none exist. 9 Items added = 18 Items in bucket.
Hi! Thank you for the reply!
I am confused on your first question:
Have you checked in the Administration Console what is the "exact" list of your document?
I am not 100% sure what you mean buy the exact lists of my document. Can you clarify? Sorry! Very new to some of these aspects.
After adding "stale=false" to the end of my query for the view all seams to be working. I have you specifically to thank for that. The link you gave me was very helpful!. However, I did notice in the documentation that it states:
For both scenarios, you should use the observer command with the persistto argument to verify the persistent state for the document being, then force an update of the view using stale=false. This will ensure that the document is correctly updated in the view index.
I can not find anything on an observer command in the C++ documentation. Do you have any suggestions?
What I meant by "Have you checked in the Administration Console what is the "exact" list of your document?"
Is simply: go to the Admin Console ( http://127.0.0.1:8091 ) and look if the data are still here after the delete or not.
Tug
Hello,
Have you checked in the Administration Console what is the "exact" list of your document?
You are calling a view and the index may not be up to date:
- the views are using the information that are persisted on the drive
- the views are using index ( see http://www.couchbase.com/docs/couchbase-manual-2.0/couchbase-views-writi... )
So check the Web console and let me know.
Tug
Tug
@tgrall