/*
 * Does a compression algorithm exist by the name of `compresstype'?
 */
bool
compresstype_is_valid(char *comptype)
{
	NameData	compname;
	bool		found = false;

	compname = comptype_to_name(comptype);

	found = (0 !=
			 caql_getcount(
					 NULL,
					 cql("SELECT COUNT(*) FROM pg_compression "
						 " WHERE compname = :1 ",
						 NameGetDatum(&compname))));

	/*
	 * FIXME: This is a hack. Should implement related handlers and register 
	 * in system tables instead. snappy handlers have already been implemented
	 * but not registerd in system tables (see comment in GetCompressionImplement()
	 * for details).
	 */
	if(!found)
	{
		if(strcmp(comptype, "snappy") == 0 || strcmp(comptype, "gzip") == 0)
			found = true;
	}

	return found;
}
Example #2
0
/*
 * Find the compression implementation (in pg_compression) for a particular
 * compression type.
 *
 * Comparison is case insensitive.
 */
PGFunction *
GetCompressionImplementation(char *comptype)
{
	HeapTuple		tuple;
	NameData		compname;
	PGFunction	   *funcs;
	Form_pg_compression ctup;
	FmgrInfo		finfo;
	Relation	comprel;
	ScanKeyData	scankey;
	SysScanDesc scan;

	comprel = heap_open(CompressionRelationId, AccessShareLock);

	compname = comptype_to_name(comptype);

	/* SELECT * FROM pg_compression WHERE compname = :1 */
	ScanKeyInit(&scankey,
				Anum_pg_compression_compname,
				BTEqualStrategyNumber, F_NAMEEQ,
				NameGetDatum(&compname));

	scan = systable_beginscan(comprel, CompressionCompnameIndexId, true,
							  SnapshotNow, 1, &scankey);
	tuple = systable_getnext(scan);
	if (!HeapTupleIsValid(tuple))
		ereport(ERROR,
				(errcode(ERRCODE_UNDEFINED_OBJECT),
				 errmsg("unknown compress type \"%s\"",
						comptype)));

	funcs = palloc0(sizeof(PGFunction) * NUM_COMPRESS_FUNCS);

	ctup = (Form_pg_compression)GETSTRUCT(tuple);

	Insist(OidIsValid(ctup->compconstructor));
	fmgr_info(ctup->compconstructor, &finfo);
	funcs[COMPRESSION_CONSTRUCTOR] = finfo.fn_addr;

	Insist(OidIsValid(ctup->compdestructor));
	fmgr_info(ctup->compdestructor, &finfo);
	funcs[COMPRESSION_DESTRUCTOR] = finfo.fn_addr;

	Insist(OidIsValid(ctup->compcompressor));
	fmgr_info(ctup->compcompressor, &finfo);
	funcs[COMPRESSION_COMPRESS] = finfo.fn_addr;

	Insist(OidIsValid(ctup->compdecompressor));
	fmgr_info(ctup->compdecompressor, &finfo);
	funcs[COMPRESSION_DECOMPRESS] = finfo.fn_addr;

	Insist(OidIsValid(ctup->compvalidator));
	fmgr_info(ctup->compvalidator, &finfo);
	funcs[COMPRESSION_VALIDATOR] = finfo.fn_addr;

	systable_endscan(scan);
	heap_close(comprel, AccessShareLock);

	return funcs;
}
/* 
 * Find the compression implementation (in pg_compression) for a particular
 * compression type.
 *
 * Comparison is case insensitive.
 */
PGFunction *
GetCompressionImplementation(char *comptype)
{
	HeapTuple		tuple;
	NameData		compname;
	PGFunction	   *funcs;
	Form_pg_compression ctup;
	FmgrInfo		finfo;

	compname = comptype_to_name(comptype);

	/*
	 * This is a hack: We added the snappy support for row oriented storage, however
	 * to make the feature friendly to upgradation in short term, we decide to not
	 * modify the system table to implement this. Following ugly hack is to
	 * complete this. Let's remove this piece of code after snappy support is
	 * added to the related system tables.
	 */
	if (strcmp(NameStr(compname), "snappy") == 0)
	{
		funcs = palloc0(sizeof(PGFunction) * NUM_COMPRESS_FUNCS);
		funcs[COMPRESSION_CONSTRUCTOR] = snappy_constructor;
		funcs[COMPRESSION_DESTRUCTOR] = snappy_destructor;
		funcs[COMPRESSION_COMPRESS] = snappy_compress_internal;
		funcs[COMPRESSION_DECOMPRESS] = snappy_decompress_internal;
		funcs[COMPRESSION_VALIDATOR] = snappy_validator;
		return funcs;
	}

	tuple = caql_getfirst(
			NULL,
			cql("SELECT * FROM pg_compression "
				" WHERE compname = :1 ",
				NameGetDatum(&compname)));

	if (!HeapTupleIsValid(tuple))
		ereport(ERROR,
				(errcode(ERRCODE_UNDEFINED_OBJECT),
				 errmsg("unknown compress type \"%s\"",
						comptype)));

	funcs = palloc0(sizeof(PGFunction) * NUM_COMPRESS_FUNCS);

	ctup = (Form_pg_compression)GETSTRUCT(tuple);

	Insist(OidIsValid(ctup->compconstructor));
	fmgr_info(ctup->compconstructor, &finfo);
	funcs[COMPRESSION_CONSTRUCTOR] = finfo.fn_addr;

	Insist(OidIsValid(ctup->compdestructor));
	fmgr_info(ctup->compdestructor, &finfo);
	funcs[COMPRESSION_DESTRUCTOR] = finfo.fn_addr;

	Insist(OidIsValid(ctup->compcompressor));
	fmgr_info(ctup->compcompressor, &finfo);
	funcs[COMPRESSION_COMPRESS] = finfo.fn_addr;

	Insist(OidIsValid(ctup->compdecompressor));
	fmgr_info(ctup->compdecompressor, &finfo);
	funcs[COMPRESSION_DECOMPRESS] = finfo.fn_addr;

	Insist(OidIsValid(ctup->compvalidator));
	fmgr_info(ctup->compvalidator, &finfo);
	funcs[COMPRESSION_VALIDATOR] = finfo.fn_addr;

	return funcs;
}