BEGIN TRANSACTION;

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

-- TXTIDX type

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

CREATE FUNCTION txtidx_out(txtidx)
RETURNS cstring
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(cstring)
RETURNS query_txt
AS 'MODULE_PATHNAME'
LANGUAGE 'c' with (isstrict);

CREATE FUNCTION qtxt_out(query_txt)
RETURNS cstring
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(cstring)
RETURNS mquery_txt
AS 'MODULE_PATHNAME'
LANGUAGE 'c' with (isstrict);

--same C output function as for query_txt
CREATE FUNCTION mqtxt_out(mquery_txt)
RETURNS cstring
AS 'MODULE_PATHNAME', 'qtxt_out'
LANGUAGE 'c' with (isstrict);

CREATE TYPE mquery_txt (
internallength = -1,
input = mqtxt_in,
output = mqtxt_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 trigger as
        'MODULE_PATHNAME'
        language 'C';

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

CREATE FUNCTION gtxtidx_out(gtxtidx)
RETURNS cstring
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,internal,int4) RETURNS bool
        AS 'MODULE_PATHNAME' LANGUAGE 'c';

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

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

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

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

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

CREATE FUNCTION gtxtidx_same(gtxtidx, gtxtidx, internal) RETURNS internal
        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, internal, int4),
	FUNCTION	2	gtxtidx_union (bytea, internal),
	FUNCTION	3	gtxtidx_compress (internal),
	FUNCTION	4	gtxtidx_decompress (internal),
	FUNCTION	5	gtxtidx_penalty (internal, internal, internal),
	FUNCTION	6	gtxtidx_picksplit (internal, internal),
	FUNCTION	7	gtxtidx_same (gtxtidx, gtxtidx, internal),
	STORAGE		gtxtidx;


END TRANSACTION;
