Example #1
0
int PNode_moveToPath_createIfAbsent_(PNode *self, Datum *p, int createIfAbsent, int fromRoot)
{
	int r;
	Datum *cp = Datum_poolNew();
	Datum *np = Datum_poolNew();
	
	Datum_copy_(cp, p);
	if (fromRoot) PNode_setToRoot(self);
	
	do	
	{
		r = Datum_sepOnChars_with_(cp, "/", np);
		
		if (!Datum_isEmpty(cp)) 
		{
			if (createIfAbsent)
			{
				PNode_createMoveToKey_(self, cp);
			}
			else
			{
				if (PNode_moveToKey_(self, cp)) 
				{
					return -1;
				}
			}
		}

			
		Datum_copy_(cp, np);
	} while(r != -1);
	
	return 0;
}
Example #2
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;
}