int VertexServer_api_link(VertexServer *self) { PNode *toNode = PDB_allocNode(self->pdb); PNode *fromNode = PDB_allocNode(self->pdb); Datum *key = HttpRequest_queryValue_(self->httpRequest, "key"); Datum *fromPath = HttpRequest_uriPath(self->httpRequest); Datum *toPath = HttpRequest_queryValue_(self->httpRequest, "toPath"); if (PNode_moveToPathIfExists_(toNode, toPath) != 0) { VertexServer_setErrorCString_(self, "to path does not exist: "); VertexServer_appendError_(self, toPath); return -1; } if (PNode_moveToPathIfExists_(fromNode, fromPath) != 0) { VertexServer_setErrorCString_(self, "from path does not exist: "); VertexServer_appendError_(self, fromPath); return -1; } PNode_atPut_(toNode, key, PNode_pid(fromNode)); return 0; }
int VertexServer_api_write(VertexServer *self) { PNode *node = PDB_allocNode(self->pdb); Datum *mode = HttpRequest_queryValue_(self->httpRequest, "mode"); Datum *key = HttpRequest_queryValue_(self->httpRequest, "key"); Datum *value = HttpRequest_queryValue_(self->httpRequest, "value"); /* if(Datum_size(value) == 0) { value = self->post; //VertexServer_setErrorCString_(self, "empty keys not accepted"); //return -1; } */ /* if(Datum_size(value) == 0) { VertexServer_setErrorCString_(self, "empty values not accepted"); return -1; } */ if (PNode_moveToPathIfExists_(node, HttpRequest_uriPath(self->httpRequest)) != 0) { VertexServer_setErrorCString_(self, "write path does not exist: "); VertexServer_appendError_(self, HttpRequest_uriPath(self->httpRequest)); return -1; } if(Datum_equalsCString_(mode, "append")) { PNode_atCat_(node, key, value); } else { PNode_atPut_(node, key, value); } return 0; }
int PNode_createMoveToKey_(PNode *self, Datum *key) { Datum *v = PNode_at_(self, key); if (v) { Datum_copy_(self->parentPid, self->pid); PNode_setPid_(self, v); return 1; } else { PNode *n = PDB_allocNode(self->pdb); PNode_create(n); PNode_atPut_(self, key, PNode_pid(n)); Datum_copy_(self->parentPid, self->pid); PNode_setPid_(self, PNode_pid(n)); } return 0; }
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; }