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_select(VertexServer *self) { PNode *node = PDB_allocNode(self->pdb); PQuery *q = PNode_query(node); VertexServer_setupPQuery_(self, q); Datum *opName; if (VertexServer_api_setCursorPathOnNode_(self, node)) return 2; opName = HttpRequest_queryValue_(self->httpRequest, "op"); if (opName) { PNodeOp *op = (PNodeOp *)CHash_at_(self->ops, opName); if (op) { return op(node, self->result); } } VertexServer_setErrorCString_(self, "invalid node op"); return -1; }
int VertexServer_api_setCursorPathOnNode_(VertexServer *self, PNode *node) { int r = PNode_moveToPathIfExists_(node, HttpRequest_uriPath(self->httpRequest)); if (r) { VertexServer_setErrorCString_(self, "path does not exist: "); VertexServer_appendError_(self, HttpRequest_uriPath(self->httpRequest)); } return r; }
int VertexServer_api_ampie(VertexServer *self) { Datum *slot1 = HttpRequest_queryValue_(self->httpRequest, "slot1"); //Datum *slot2 = HttpRequest_queryValue_(self->httpRequest, "slot2"); //Datum *slot3 = HttpRequest_queryValue_(self->httpRequest, "slot3"); //Datum *subpath = HttpRequest_queryValue_(self->httpRequest, "subpath"); PNode *node = PDB_allocNode(self->pdb); PQuery *q = PNode_query(node); VertexServer_setupPQuery_(self, q); PNode *tmpNode = PDB_allocNode(self->pdb); Datum *d = self->result; Datum *k; if (PNode_moveToPathIfExists_(node, HttpRequest_uriPath(self->httpRequest)) != 0) { VertexServer_setErrorCString_(self, "path does not exist: "); VertexServer_appendError_(self, HttpRequest_uriPath(self->httpRequest)); return -1; } //PNode_startQuery(node); PNode_first(node); while(k = PNode_key(node)) { PNode_setPid_(tmpNode, PNode_value(node)); Datum *v = PNode_at_(tmpNode, slot1); if(v) { Datum_append_(d, k); Datum_appendCString_(d, ";"); Datum_append_(d, v); Datum_appendCString_(d, "\n"); } PNode_next(node); } HttpResponse_setContentType_(self->httpResponse, "text/plain"); //HttpResponse_setContentType_(self->httpResponse, "text/xml; charset=utf-8"); return 0; }
int VertexServer_api_transaction(VertexServer *self) { Datum *uri = Datum_new(); Datum *post = Datum_new(); int error = 0; int r, result; PDB_commit(self->pdb); Datum_copy_(post, HttpRequest_postData(self->httpRequest)); do { Datum_copy_(uri, post); r = Datum_sepOnChars_with_(uri, "\n", post); if (Datum_size(uri) == 0) { VertexServer_setErrorCString_(self, "empty line in transaction"); error = 1; break; } HttpRequest_parseUri_(self->httpRequest, Datum_data(uri)); error = VertexServer_process(self); Pool_globalPoolFreeRefs(); } while ((r != -1) && (!error)); if (error) { PDB_abort(self->pdb); result = -1; } else { PDB_commit(self->pdb); result = 0; } Datum_free(uri); return result; }
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 VertexServer_process(VertexServer *self) { Datum *actionName = (Datum *)HttpRequest_queryValue_(self->httpRequest, "action"); //if(self->debug) { Log_Printf_("REQUEST ERROR: %s\n", HttpRequest_uri(self->httpRequest)); } if (Datum_size(actionName)) { VertexAction *action = (VertexAction *)CHash_at_(self->actions, actionName); if (action) { return action(self); } } else { return VertexServer_api_view(self); } VertexServer_setErrorCString_(self, "invalid action"); return -1; }
int VertexServer_api_view(VertexServer *self) { Datum *before = HttpRequest_queryValue_(self->httpRequest, "before"); Datum *after = HttpRequest_queryValue_(self->httpRequest, "after"); Datum *mode = HttpRequest_queryValue_(self->httpRequest, "mode"); PNode *node = PDB_allocNode(self->pdb); PNode *tmpNode = PDB_allocNode(self->pdb); Datum *d = self->result; int maxCount = 200; if (PNode_moveToPathIfExists_(node, HttpRequest_uriPath(self->httpRequest)) != 0) { VertexServer_setErrorCString_(self, "path does not exist: "); VertexServer_appendError_(self, HttpRequest_uriPath(self->httpRequest)); return -1; } Datum_appendCString_(d, "<!DOCTYPE html>\n"); Datum_appendCString_(d, "<html>\n"); Datum_appendCString_(d, "<head>\n"); Datum_appendCString_(d, "<title>"); Datum_append_(d, HttpRequest_uriPath(self->httpRequest)); Datum_appendCString_(d, "</title>\n"); Datum_appendCString_(d, "<meta charset=\"utf-8\">\n"); Datum_appendCString_(d, "<style>"); Datum_appendCString_(d, "body, td { font-family: Helvetica; font-size: 12px; margin-top:2em; margin-left:2em; }"); Datum_appendCString_(d, ".path { font-weight: normal; }"); Datum_appendCString_(d, ".note { color:#aaaaaa; }"); Datum_appendCString_(d, ".key { color:#000000; }"); Datum_appendCString_(d, ".value { color:#888888; white-space:pre; }"); Datum_appendCString_(d, "a { color: #0000aa; text-decoration: none; }"); Datum_appendCString_(d, "</style>\n"); Datum_appendCString_(d, "</head>\n"); Datum_appendCString_(d, "<body>\n"); /* if(Datum_size(HttpRequest_uriPath(self->httpRequest)) == 0) { Datum_appendCString_(d, "/"); } else { */ Datum_appendCString_(d, "<font class=path>"); Datum_appendCString_(d, "/"); Datum_append_(d, HttpRequest_uriPath(self->httpRequest)); Datum_appendCString_(d, "</font>"); Datum_appendCString_(d, "<br>\n"); //} PNode_first(node); if(Datum_size(before)) { PNode_jump_(node, before); int i; for(i = 0; i < maxCount; i++) { PNode_previous(node); } PNode_next(node); } else if(Datum_size(after)) { PNode_jump_(node, after); } if (Datum_size(after) && PNode_key(node)) { Datum_appendCString_(d, "<a href=/"); Datum_append_(d, HttpRequest_uriPath(self->httpRequest)); Datum_appendCString_(d, "?before="); Datum_append_(d, PNode_key(node)); Datum_appendCString_(d, ">previous</a>"); } Datum_appendCString_(d, "<ul>\n"); Datum_appendCString_(d, "<table cellpadding=0 cellspacing=0 border=0>"); { Datum *k; int count = 0; while ((k = PNode_key(node)) && count < maxCount) { if (Datum_beginsWithCString_(k , "_")) { Datum_appendCString_(d, "<tr>"); Datum_appendCString_(d, "<td align=right style=\"line-height:1.5em\">"); Datum_appendCString_(d, "<font class=key>"); Datum_append_(d, k); Datum_appendCString_(d, "</font>"); Datum_appendCString_(d, " "); Datum_appendCString_(d, "</td>"); Datum_appendCString_(d, "<td>"); Datum_appendCString_(d, " <span class=value>"); Datum_append_(d, PNode_value(node)); Datum_appendCString_(d, "</span>"); Datum_appendCString_(d, "<br>\n"); Datum_appendCString_(d, "</td>"); Datum_appendCString_(d, "</tr>"); } else { if(Datum_equalsCString_(mode, "table")) { PNode_setPid_(tmpNode, PNode_value(node)); PNode_asHtmlRow(tmpNode, d); } else { Datum_appendCString_(d, "<tr>"); Datum_appendCString_(d, "<td align=right>"); Datum_appendCString_(d, "<font class=key>"); Datum_append_(d, k); Datum_appendCString_(d, "</font><br>\n"); Datum_appendCString_(d, "</td>"); Datum_appendCString_(d, "<td style=\"line-height:1.5em\">"); Datum_appendCString_(d, " <a href="); if(Datum_size(HttpRequest_uriPath(self->httpRequest)) != 0) Datum_appendCString_(d, "/"); Datum_append_(d, HttpRequest_uriPath(self->httpRequest)); Datum_appendCString_(d, "/"); Datum_append_(d, k); Datum_appendCString_(d, "> ↠ "); Datum_appendLong_(d, PNode_nodeSizeAtCursor(node)); Datum_appendCString_(d, "</a> "); Datum_appendCString_(d, "<font class=value>"); Datum_appendCString_(d, "</td>"); Datum_appendCString_(d, "</tr>"); } } PNode_next(node); count ++; } } Datum_appendCString_(d, "</table>"); Datum_appendCString_(d, "</ul>\n"); if(PNode_key(node)) { Datum_appendCString_(d, "<a href=/"); Datum_append_(d, HttpRequest_uriPath(self->httpRequest)); Datum_appendCString_(d, "?after="); Datum_append_(d, PNode_key(node)); Datum_appendCString_(d, ">next</a><br>"); } Datum_appendCString_(d, "</body>\n"); Datum_appendCString_(d, "</html>\n"); HttpResponse_setContentType_(self->httpResponse, "text/html; charset=utf-8"); 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; }
int VertexServer_api_amchart(VertexServer *self) { Datum *slot1 = HttpRequest_queryValue_(self->httpRequest, "slot1"); //Datum *slot2 = HttpRequest_queryValue_(self->httpRequest, "slot2"); //Datum *slot3 = HttpRequest_queryValue_(self->httpRequest, "slot3"); Datum *subpath = HttpRequest_queryValue_(self->httpRequest, "subpath"); PNode *node = PDB_allocNode(self->pdb); PQuery *q = PNode_query(node); VertexServer_setupPQuery_(self, q); PNode *tmpNode = PDB_allocNode(self->pdb); Datum *d = self->result; Datum *title = 0x0; //int isFirst = 1; if (PNode_moveToPathIfExists_(node, HttpRequest_uriPath(self->httpRequest)) != 0) { VertexServer_setErrorCString_(self, "path does not exist: "); VertexServer_appendError_(self, HttpRequest_uriPath(self->httpRequest)); return -1; } Datum_appendCString_(d, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); Datum_appendCString_(d, "<chart approx_count=\""); // series ----------------- if(Datum_size(subpath)) PNode_moveToPathIfExists_(node, subpath); Datum_appendLong_(d, PNode_size(node)); Datum_appendCString_(d, "\">\n"); PNode_startQuery(node); PNode_amSeries(node, d); PNode_moveToPathIfExists_(node, HttpRequest_uriPath(self->httpRequest)); // series ----------------- Datum_appendCString_(d, "<graphs>\n"); PNode_first(node); if(Datum_size(subpath)) { Datum *k; while(k = PNode_key(node)) { Datum *title = Datum_poolNew(); PNode_setPid_(tmpNode, PNode_value(node)); PNode_moveToSubpathIfExists_(tmpNode, subpath); Datum_encodeUri(k); Datum_copy_(title, k); //Datum_appendCString_(title, "-"); //Datum_append_(title, slot1); PNode_amGraphKey_(tmpNode, title, slot1, d); //printf("graph: %s\n", Datum_data(k)); PNode_next(node); } } else if(Datum_size(slot1)) { char slotKey[64]; int slotNumber = 1; for (;;) { sprintf(slotKey, "slot%i", slotNumber); Datum *slotName = HttpRequest_queryValue_(self->httpRequest, slotKey); if(Datum_size(slotName) == 0) break; PNode_amGraphKey_(node, title, slotName, d); slotNumber ++; } } Datum_appendCString_(d, "</graphs>\n"); Datum_appendCString_(d, "</chart>\n"); HttpResponse_setContentType_(self->httpResponse, "text/xml"); //HttpResponse_setContentType_(self->httpResponse, "text/xml; charset=utf-8"); return 0; }