BEGIN TRANSACTION;

-- Adjust this setting to control where the objects get created.
SET search_path = public;

-- TXTIDX type

CREATE FUNCTION txtidx_in(opaque)
RETURNS opaque
AS 'MODULE_PATHNAME'
LANGUAGE 'c' with (isstrict);

CREATE FUNCTION txtidx_out(opaque)
RETURNS opaque
AS 'MODULE_PATHNAME'
LANGUAGE 'c' with (isstrict);

CREATE TYPE txtidx (
internallength = -1,
input = txtidx_in,
output = txtidx_out,
storage = extended
);

CREATE FUNCTION txt2txtidx(text)
RETURNS txtidx
AS 'MODULE_PATHNAME'
LANGUAGE 'c' with (isstrict);

CREATE FUNCTION txtidxsize(txtidx)
RETURNS int4
AS 'MODULE_PATHNAME'
LANGUAGE 'c' with (isstrict);

--QUERYTYPES
--without morphology
CREATE FUNCTION qtxt_in(opaque)
RETURNS opaque
AS 'MODULE_PATHNAME'
LANGUAGE 'c' with (isstrict);

CREATE FUNCTION qtxt_out(opaque)
RETURNS opaque
AS 'MODULE_PATHNAME'
LANGUAGE 'c' with (isstrict);

CREATE TYPE query_txt (
internallength = -1,
input = qtxt_in,
output = qtxt_out
);

--with morphology
CREATE FUNCTION mqtxt_in(opaque)
RETURNS opaque
AS 'MODULE_PATHNAME'
LANGUAGE 'c' with (isstrict);

CREATE TYPE mquery_txt (
internallength = -1,
input = mqtxt_in,
output = qtxt_out
);

--only for debug
CREATE FUNCTION querytree(query_txt)
RETURNS text
AS 'MODULE_PATHNAME'
LANGUAGE 'c' with (isstrict);

CREATE FUNCTION querytree(mquery_txt)
RETURNS text
AS 'MODULE_PATHNAME'
LANGUAGE 'c' with (isstrict);

--operations
CREATE FUNCTION execqtxt(txtidx, query_txt) RETURNS bool
        AS 'MODULE_PATHNAME' LANGUAGE 'c' with (isstrict);

COMMENT ON FUNCTION execqtxt(txtidx, query_txt) IS 'boolean operation with text index';

CREATE FUNCTION execqtxt(txtidx, mquery_txt) RETURNS bool
        AS 'MODULE_PATHNAME' LANGUAGE 'c' with (isstrict);

COMMENT ON FUNCTION execqtxt(txtidx, mquery_txt) IS 'boolean operation with text index';

CREATE FUNCTION rexecqtxt(query_txt, txtidx) RETURNS bool
        AS 'MODULE_PATHNAME' LANGUAGE 'c' with (isstrict);

COMMENT ON FUNCTION rexecqtxt(query_txt, txtidx) IS 'boolean operation with text index';

CREATE FUNCTION rexecqtxt(mquery_txt, txtidx) RETURNS bool
        AS 'MODULE_PATHNAME' LANGUAGE 'c' with (isstrict);

COMMENT ON FUNCTION rexecqtxt(mquery_txt, txtidx) IS 'boolean operation with text index';

CREATE OPERATOR @@ (
   LEFTARG = txtidx, RIGHTARG = query_txt, PROCEDURE = execqtxt,
   COMMUTATOR = '~@', RESTRICT = contsel, JOIN = contjoinsel
);

CREATE OPERATOR ~@ (
   LEFTARG = query_txt, RIGHTARG = txtidx, PROCEDURE = rexecqtxt,
   COMMUTATOR = '@@', RESTRICT = contsel, JOIN = contjoinsel
);

CREATE OPERATOR ## (
   LEFTARG = txtidx, RIGHTARG = mquery_txt, PROCEDURE = execqtxt,
   COMMUTATOR = '~#', RESTRICT = contsel, JOIN = contjoinsel
);

CREATE OPERATOR ~# (
   LEFTARG = mquery_txt, RIGHTARG = txtidx, PROCEDURE = rexecqtxt,
   COMMUTATOR = '##', RESTRICT = contsel, JOIN = contjoinsel
);

--Trigger
create function tsearch() returns opaque as
        'MODULE_PATHNAME'
        language 'C';

--GiST
--GiST key type
CREATE FUNCTION gtxtidx_in(opaque)
RETURNS opaque
AS 'MODULE_PATHNAME'
LANGUAGE 'c' with (isstrict);

CREATE FUNCTION gtxtidx_out(opaque)
RETURNS opaque
AS 'MODULE_PATHNAME'
LANGUAGE 'c' with (isstrict);

CREATE TYPE gtxtidx (
internallength = -1,
input = gtxtidx_in,
output = gtxtidx_out
);

-- support functions
CREATE FUNCTION gtxtidx_consistent(gtxtidx,opaque,int4) RETURNS bool
        AS 'MODULE_PATHNAME' LANGUAGE 'c';

CREATE FUNCTION gtxtidx_compress(opaque) RETURNS opaque
        AS 'MODULE_PATHNAME' LANGUAGE 'c';

CREATE FUNCTION gtxtidx_decompress(opaque) RETURNS opaque
        AS 'MODULE_PATHNAME' LANGUAGE 'c';

CREATE FUNCTION gtxtidx_penalty(opaque,opaque,opaque) RETURNS opaque
        AS 'MODULE_PATHNAME' LANGUAGE 'c' with (isstrict);

CREATE FUNCTION gtxtidx_picksplit(opaque, opaque) RETURNS opaque
        AS 'MODULE_PATHNAME' LANGUAGE 'c';

CREATE FUNCTION gtxtidx_union(bytea, opaque) RETURNS _int4
        AS 'MODULE_PATHNAME' LANGUAGE 'c';

CREATE FUNCTION gtxtidx_same(gtxtidx, gtxtidx, opaque) RETURNS opaque
        AS 'MODULE_PATHNAME' LANGUAGE 'c';

-- create the operator class
CREATE OPERATOR CLASS gist_txtidx_ops
    DEFAULT FOR TYPE txtidx USING gist AS
	OPERATOR	1	@@ (txtidx, query_txt)	RECHECK ,
	OPERATOR	2	## (txtidx, mquery_txt)	RECHECK ,
	FUNCTION	1	gtxtidx_consistent (gtxtidx, opaque, int4),
	FUNCTION	2	gtxtidx_union (bytea, opaque),
	FUNCTION	3	gtxtidx_compress (opaque),
	FUNCTION	4	gtxtidx_decompress (opaque),
	FUNCTION	5	gtxtidx_penalty (opaque, opaque, opaque),
	FUNCTION	6	gtxtidx_picksplit (opaque, opaque),
	FUNCTION	7	gtxtidx_same (gtxtidx, gtxtidx, opaque),
	STORAGE		gtxtidx;


END TRANSACTION;
