Example #1
0
int PNode_op_pairs(PNode *self, Datum *d)
{	
	PQuery *q = PNode_startQuery(self);
	PNode *tmpNode = PDB_allocNode(self->pdb);
	Datum *k;
	
	yajl_gen_array_open(self->yajl);
	
	while (k = PQuery_key(q))
	{		
		yajl_gen_array_open(self->yajl);

		yajl_gen_datum(self->yajl, k);
					
		if(!Datum_beginsWithCString_(k, "_"))
		{
			PNode_setPid_(tmpNode, PNode_value(self));
			PNode_op_object(tmpNode, d);
		}
		else
		{
			yajl_gen_datum(self->yajl, PNode_value(self));
		}
				
		PQuery_enumerate(q);
		yajl_gen_array_close(self->yajl);
	}
	
	yajl_gen_array_close(self->yajl);
	Datum_appendYajl_(d, self->yajl);
	return 0; 
}
Example #2
0
int PNode_op_rm(PNode *self, Datum *d)
{
	PQuery *q = PNode_startQuery(self);
	Datum *k;
	long size = PNode_size(self);
	long removeCount = 0;
	
	if(!q) PNode_first(self);
	
	while (k = PQuery_key(q))
	{
		PDB_willWrite(self->pdb);
		if(!StoreCursor_remove(self->storeCursor)) break;
		
		// HACK to avoid skipping a step by backing up in the reverse enum direction before calling PQuery_enumerate
		if(PQuery_stepDirection(q) == 1)
		{
			StoreCursor_previous(self->storeCursor);
		}

		removeCount ++;
		//count += PNode_removeAt_(self, k);
		PQuery_enumerate(q);
	}

	PNode_setSize_(self, size - removeCount);
	
	
	yajl_gen_integer(self->yajl, removeCount);
	Datum_appendYajl_(d, self->yajl);
	//Datum_appendLong_(d, removeCount);
	return 0;  
}
Example #3
0
int PNode_op_sizes(PNode *self, Datum *d)
{	
	PNode *tmpNode = PDB_allocNode(self->pdb);
	Datum *k;
	PQuery *q = PNode_startQuery(self);
	
	yajl_gen_map_open(self->yajl);
	
	while (k = PQuery_key(q))
	{
		if(!Datum_beginsWithCString_(k, "_"))
		{			
			yajl_gen_datum(self->yajl, PNode_key(self));
		
			PNode_setPid_(tmpNode, PNode_value(self));
			yajl_gen_integer(self->yajl, PNode_size(tmpNode));
		}
		
		if (q) { PQuery_enumerate(q); } else { PNode_next(self); }
	}
	
	yajl_gen_map_close(self->yajl);
	Datum_appendYajl_(d, self->yajl);
	return 0;  
}
Example #4
0
int PNode_op_values(PNode *self, Datum *d)
{	
	PQuery *q = PNode_startQuery(self);
	PNode *tmpNode = PDB_allocNode(self->pdb);
	Datum *k;
	Datum *attribute = PQuery_attribute(q);
	int hasAttribute = Datum_size(attribute) != 0;
	
	yajl_gen_array_open(self->yajl);
	
	while (k = PQuery_key(q))
	{		
		if(!Datum_beginsWithCString_(k, "_"))
		{
			PNode_setPid_(tmpNode, PNode_value(self));
			
			if(hasAttribute)
			{
				//Datum *a = PQuery_attributeValue(q);
				Datum *a = PNode_at_(tmpNode, attribute);
				
				yajl_gen_array_open(self->yajl);
				yajl_gen_datum(self->yajl, k);
				
				if(a)
				{
					yajl_gen_datum(self->yajl, a);
				}
				else 
				{
					yajl_gen_null(self->yajl);
				}
				
				yajl_gen_array_close(self->yajl);

			}
			else 
			{
				PNode_setPid_(tmpNode, PNode_value(self));
				PNode_op_object(tmpNode, 0x0);
			}
		}
		else
		{
				yajl_gen_datum(self->yajl, PNode_value(self));
		}

		PQuery_enumerate(q);
	}
	
	yajl_gen_array_close(self->yajl);
	Datum_appendYajl_(d, self->yajl);
	return 0; 
}
Example #5
0
int PNode_op_count(PNode *self, Datum *d)
{	
	PQuery *q = PNode_startQuery(self);
	Datum *k;
	long count = 0;
		
	while (k = PQuery_key(q))
	{
		count ++;
		PQuery_enumerate(q);
	}
	
	yajl_gen_integer(self->yajl, count);
	Datum_appendYajl_(d, self->yajl);
	return 0;  
}
Example #6
0
int PNode_op_keys(PNode *self, Datum *d)
{	
	PQuery *q = PNode_startQuery(self);
	Datum *k;
	
	yajl_gen_array_open(self->yajl);
	
	while (k = PQuery_key(q))
	{
		yajl_gen_datum(self->yajl, k);
		PQuery_enumerate(q);
	}

	yajl_gen_array_close(self->yajl);
	Datum_appendYajl_(d, self->yajl);
	return 0;
}
Example #7
0
int VertexServer_api_collectGarbage(VertexServer *self)
{
	time_t t1 = time(NULL);
	long savedCount = PDB_collectGarbage(self->pdb);
	double dt = difftime(time(NULL), t1);
	
	yajl_gen_map_open(self->yajl);
	yajl_gen_cstring(self->yajl, "saved");
	yajl_gen_integer(self->yajl, savedCount);
	yajl_gen_cstring(self->yajl, "seconds");
	yajl_gen_integer(self->yajl, (int)dt);
	yajl_gen_map_close(self->yajl);
	
	Datum_appendYajl_(self->result, self->yajl);
	Log_Printf_("gc: %s\n", Datum_data(self->result));

	return 0;
}
Example #8
0
int PNode_op_object(PNode *self, Datum *d)
{
	PQuery *q = PNode_startQuery(self);
	Datum *k;
	
	yajl_gen_map_open(self->yajl);
	
	while (k = PQuery_key(q))
	{
		yajl_gen_datum(self->yajl, k);
		yajl_gen_datum(self->yajl, PNode_value(self));
		PQuery_enumerate(q);
	}
	
	yajl_gen_map_close(self->yajl);
	if(d) Datum_appendYajl_(d, self->yajl);
	return 0;
}
Example #9
0
int VertexServer_api_read(VertexServer *self)
{
	PNode *node = PDB_allocNode(self->pdb);
	Datum *key = HttpRequest_queryValue_(self->httpRequest, "key");	
	Datum *value;
	
	if (VertexServer_api_setCursorPathOnNode_(self, node)) return 2;

	value = PNode_at_(node, key);

	if (value) 
	{
		yajl_gen_datum(self->yajl, value);
	}
	else
	{
		yajl_gen_null(self->yajl);
	}
	
	Datum_appendYajl_(self->result, self->yajl);
	return 0;
}
Example #10
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;
}
Example #11
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;
}