int VertexServer_api_rm(VertexServer *self) { PNode *node = PDB_allocNode(self->pdb); Datum *key = HttpRequest_queryValue_(self->httpRequest, "key"); if (VertexServer_api_setCursorPathOnNode_(self, node)) return 0; // ignore if it doesn't exist PNode_removeAt_(node, key); return 0; }
void PNode_removeAtCursor(PNode *self) { Datum *k = PNode_key(self); PDB_willWrite(self->pdb); if (k) { Datum *currentKey = Datum_clone(k); Datum *nextKey = 0x0; PNode_next(self); Datum *nk = PNode_key(self); if (nk) nextKey = Datum_clone(nk); PNode_removeAt_(self, currentKey); //StoreCursor_remove(self->storeCursor); if (nextKey) PNode_jump_(self, nextKey); Datum_free(currentKey); if (nextKey) Datum_free(nextKey); } /* PDB_willWrite(self->pdb); if(StoreCursor_remove(self->storeCursor)) { PNode_decrementSize(self); PNode_jumpToCurrentKey(self); } else { printf("PNode warning: remove failed\n"); } */ }
int VertexServer_api_queueExpireTo(VertexServer *self) { PNode *fromNode = PDB_allocNode(self->pdb); PNode *toNode = PDB_allocNode(self->pdb); PNode *itemNode = PDB_allocNode(self->pdb); Datum *toPath = HttpRequest_queryValue_(self->httpRequest, "toPath"); unsigned int itemsExpired = 0; if (PNode_moveToPathIfExists_(fromNode, HttpRequest_uriPath(self->httpRequest)) != 0) { VertexServer_setErrorCString_(self, "from path does not exist: "); VertexServer_appendError_(self, HttpRequest_uriPath(self->httpRequest)); return -1; } //PNode_moveToPath_(toNode, toPath); if (PNode_moveToPathIfExists_(toNode, toPath) != 0) { VertexServer_setErrorCString_(self, "to path does not exist: "); VertexServer_appendError_(self, toPath); return -1; } PNode_first(fromNode); { Datum *qTimeKey = Datum_newWithCString_("_qtime"); Datum *k; Datum *qExpireKey = Datum_newWithCString_("_qexpire"); long now = time(NULL); while (k = PNode_key(fromNode)) { Datum *pid = PNode_value(fromNode); Datum *qExpireValue; PNode_setPid_(itemNode, pid); qExpireValue = PNode_at_(itemNode, qExpireKey); if(!qExpireValue) { Log_Printf("WARNING: attempt to expire a node with no _qexpire value\n"); if(PNode_at_(itemNode, qTimeKey) == 0x0) { Log_Printf("WARNING: node also missing _qtime value\n"); } break; } if(qExpireValue == 0x0 || Datum_asLong(qExpireValue) < now) { PNode_removeAtCursor(fromNode); // the remove will go to the next item PNode_key(fromNode); PNode_removeAt_(itemNode, qTimeKey); PNode_removeAt_(itemNode, qExpireKey); PNode_atPut_(toNode, k, pid); PNode_jumpToCurrentKey(fromNode); itemsExpired ++; } else { PNode_next(fromNode); } } Datum_free(qTimeKey); Datum_free(qExpireKey); } yajl_gen_integer(self->yajl, (long)itemsExpired); Datum_appendYajl_(self->result, self->yajl); return 0; }
int VertexServer_api_queuePopTo(VertexServer *self) { PNode *fromNode = PDB_allocNode(self->pdb); PNode *toNode = PDB_allocNode(self->pdb); Datum *toPath = HttpRequest_queryValue_(self->httpRequest, "toPath"); long ttl = Datum_asLong(HttpRequest_queryValue_(self->httpRequest, "ttl")); if (PNode_moveToPathIfExists_(fromNode, HttpRequest_uriPath(self->httpRequest)) != 0) { VertexServer_setErrorCString_(self, "from path does not exist: "); VertexServer_appendError_(self, HttpRequest_uriPath(self->httpRequest)); return -1; } PNode_moveToPath_(toNode, toPath); //printf("to pid: %s\n", Datum_data(PNode_pid(toNode))); //printf("from pid: %s\n", Datum_data(PNode_pid(fromNode))); { PQuery *q = PNode_query(fromNode); VertexServer_setupPQuery_(self, q); PNode_startQuery(fromNode); Datum *k = PQuery_key(q); Datum *v = PNode_value(fromNode); if (k) { PNode_atPut_(toNode, k, v); PNode_moveToKey_(toNode, k); // insert queue time { long now = time(NULL); Datum *timeKey = Datum_poolNewWithCString_("_qtime"); Datum *timeValue = Datum_poolNew(); Datum_fromLong_(timeValue, now); PNode_atPut_(toNode, timeKey, timeValue); Datum_setCString_(timeKey, "_qexpire"); Datum_fromLong_(timeValue, now + (ttl == 0 ? 3600 : ttl)); PNode_atPut_(toNode, timeKey, timeValue); } //printf("queueing key %s\n", Datum_data(k)); yajl_gen_datum(self->yajl, k); PNode_removeAt_(fromNode, k); } else { yajl_gen_null(self->yajl); } } Datum_appendYajl_(self->result, self->yajl); return 0; }