--DROP table "url";
--DROP table "urlinfo";
--DROP table "dict";
--DROP SEQUENCE next_url_id;
--DROP TABLE server;
--DROP SEQUENCE next_server_id;
--DROP TABLE links;

CREATE SEQUENCE next_url_id;

CREATE TABLE "url" (
	"rec_id"          int4 DEFAULT nextval('next_url_id') PRIMARY KEY,
	"status"          int4 NOT NULL DEFAULT 0,
	"docsize"         int4 NOT NULL DEFAULT 0,
	"next_index_time" int4 NOT NULL,
	"last_mod_time"   int4,
	"referrer"        int4 NOT NULL DEFAULT 0,
	"hops"            int4 NOT NULL DEFAULT 0,
	"crc32"           int4 NOT NULL DEFAULT 0,
	"seed"            int NOT NULL DEFAULT 0,
	"bad_since_time"  int4,
	"site_id"	  int4,
	"server_id"	  int4,
	"pop_rank"	  float NOT NULL DEFAULT 0,
	"url"             text NOT NULL
);

CREATE TABLE "urlinfo" (
       url_id int4 NOT NULL,
       sname  text NOT NULL,
       sval   text NOT NULL
);

CREATE INDEX urlinfo_id ON urlinfo (url_id);

CREATE TABLE "dict" (
	"url_id" int4 NOT NULL,
	"word"   text NOT NULL,
	"intag"  int4 NOT NULL
);

CREATE  INDEX "dict_word"  on "dict" using btree ( "word"   );
CREATE  INDEX "dict_url"   on "dict" using btree ( "url_id" );
CREATE INDEX dict_word_url_id ON dict (word, url_id);

CREATE  UNIQUE INDEX "url_url" on "url" using btree ( "url" "varchar_ops" );
CREATE  INDEX "url_crc" on "url" using btree ( "crc32" );
CREATE  INDEX "url_seed" on "url" USING btree ( "seed" );
CREATE  INDEX "url_referrer" on "url" USING btree ( "referrer" );
CREATE INDEX "url_next_index_time" ON "url" USING btree ( "next_index_time" );
CREATE INDEX "url_status" ON "url" ( "status" );
CREATE INDEX "url_bad_since_time" on "url" ( "bad_since_time");
CREATE INDEX url_crc32_status_docsize ON url (crc32,status,docsize);
CREATE INDEX url_site_hops_seed_nit on url (site_id,hops,seed,next_index_time);


CREATE SEQUENCE next_server_id;

CREATE TABLE "server" (
        "rec_id" int4 DEFAULT nextval('next_server_id') PRIMARY KEY,
        "enabled"               int             not null        default 0,
        "url"                   text            not null        default '',
        "alias"                 text            not null        default '',
        "period"                int             not null        default 604800,
        "tag"                   text            not null        default '',
        "category"              text            not null        default '',
        "charset"               text            not null        default '',
        "lang"                  text            not null        default '',
        "basic_auth"            text            not null        default '',
        "proxy"                 text            not null        default '',
        "proxy_port"            int		not null        default 3128,
        "proxy_auth"            text            not null        default '',

        "maxhops"               int             not null        default 9999,
        "gindex"                int             not null        default 1,
        "follow"                int             not null        default 1,
        "deletebad"             int             not null        default 0,
        "use_robots"            int             not null        default 1,
        "use_clones"            int             not null        default 1,

        "max_net_errors"        int             not null        default 16,
        "net_delay_time"        int             not null        default 86400,
        "read_timeout"          int             not null        default 90,

	"command"		char(1)		not null	default 'S',
	"match_type"		int		not null	default 1,
	"case_sense"		int		not null	default 1,
	"nomatch"		int		not null	default 1,
	"ordre"			int		not null	default 0,
	"parent"		int		not null	default 0,
	"weight"		float		not null	default 1,
	"pop_weight"		float		not null	default 0
);

CREATE UNIQUE INDEX srv_url ON server (url);
CREATE INDEX srv_ordre ON server (ordre);
CREATE INDEX srv_parent ON server (parent);
CREATE INDEX srv_command ON server ("command");


CREATE TABLE "links" (
	"ot"	 int4 not null,
	"k"	 int4 not null,
	"weight" float not null default 0
);

CREATE UNIQUE INDEX links_links ON links (ot, k);
CREATE INDEX links_ot ON links (ot);
CREATE INDEX links_k ON links (k);

