

create procedure lbl_ins (inout id_arr any array, inout o_arr any array, inout text_arr any array, inout lng_arr any array, in fill int)
{
  if (fill < length (id_arr))
    {
    id_arr := subseq (id_arr, 0, fill);
    o_arr := subseq (o_arr, 0, fill);
    text_arr := subseq (text_arr, 0, fill);
    lng_arr := subseq (lng_arr, 0, fill);
    }
  for vectored (in o any := o_arr, in id int  := id_arr, in txt varchar := text_arr, in lng int := lng_arr)
		 {
		   if (not exists (select 1 from rdf_label where rl_o = o))
		     insert soft rdf_label  (rl_o, rl_ro_id, rl_text, rl_lang) values (o, id, txt, lng);
		 }
}
;

create procedure
URILBL_AC_INIT_SLICE (in slid int) 
{
  --pl_debug+
  declare n, n_ins, n_strange, fill integer;
  declare o_str varchar;
  declare o_arr, text_arr, lng_arr, id_arr any array;
  log_enable (2, 1);
  cl_set_slice ('DB.DBA.RDF_QUAD', 'RDF_QUAD', slid);
  o_arr := make_array (100000, 'any');
  id_arr := make_array (100000, 'any');
  text_arr := make_array (100000, 'any');
  lng_arr := make_array (100000, 'any');
  fill := 0;
  set isolation = 'committed';

  declare exit handler for sqlstate '*' {
    urilbl_ac_init_log (sprintf ('***ERROR %s:%s', __SQL_STATE, __SQL_MESSAGE));
    cl_exec('registry_set(''urilbl_ac_init_status'',''4711'')');
    goto finished;
  };


  for (select s, __ro2sq (o) as o from rdf_quad table option (index rdf_quad_pogs, no cluster, with 'facets') where p = __i2id ('http://www.openlinksw.com/schemas/virtrdf#label') and o > rdf_box (0, 257, 257, 1, 0) ) do
     {
      declare lng, id int;
      lng := 257;
      if (__tag of rdf_box = __tag(o)) 
        {
	  o_str := cast (o as varchar);
	  lng := rdf_box_lang (o);
	  id := rdf_box_ro_id (o);
	}
      else
	{
          n_strange := n_strange + 1;
	  goto cont;
        } 
      n_ins := n_ins + 1;

    o_str := urilbl_ac_ruin_label ("LEFT"(o_str, 50));
    if (o_str is null)
      goto cont;

    id_arr[fill] := id;
    lng_arr[fill] := lng;
    o_arr[fill] := o;
    text_arr[fill] := o_str;
    fill := fill + 1;
    if (fill >= 100000)
	{
	  lbl_ins (id_arr, o_arr, text_arr, lng_arr, fill);
	  fill := 0;
	  commit work;
	}
    cont:;
    n := n + 1;
    if (mod (n, 1000000) = 0) 
      urilbl_ac_init_log (sprintf ('urilbl_ac_init_db: %d rows, %d ins, %d strange...\n', 
				   n, n_ins, n_strange));

    }
  if (fill)
    {
      lbl_ins (id_arr, o_arr, text_arr, lng_arr, fill);
      commit work;
    }
  cl_exec('registry_set (''urilbl_ac_init_status'',''2'')');
 finished:;
  urilbl_ac_init_log (sprintf ('urilbl_ac_init_db: Finished. %d rows, %d ins, %d strange./n',
              n, n_ins, n_strange));
}
;


create procedure urilbl_cl_init_srv ()
{
  cl_detach_thread ();
  cl_call_local_slices ('DB.DBA.RDF_QUAD', 'RDF_QUAD', 'DB.DBA.URILBL_AC_INIT_SLICE', vector ());
}
;


create procedure
urilbl_ac_init_db () 
{
  urilbl_ac_init_log ('urilbl_ac_init_db: started');
  cl_exec('registry_set (''urilbl_ac_init_status'',''1'')');
  cl_exec ('urilbl_cl_init_srv ()');
}
;

create procedure
cmp_label (in lbl_str varchar, in langs varchar)
{
  declare res any;
  declare q,best_q float;
  declare cur_iid any;
  declare cur_lbl varchar;
  declare n integer;

  res := vector();

--  dbg_printf ('cmp_label');
  cur_iid := null;
  best_q := 0;

  {
    declare exit handler for sqlstate 'S1TAT' { 
      goto done;
    };

    for (select rl_lang, s as ull_iid, __ro2sq (o) as ull_label from rdf_label, rdf_quad, rdf_p_score  
	where rl_text like urilbl_ac_ruin_label (lbl_str) || '%' and rl_o = o and p = p_iri order by p_score) do
      {
	declare ull_label_lang varchar;
	ull_label_lang := '';
	if (rl_lang <> 257)
	  ull_label_lang := coalesce ((select rl_id from rdf_language where rl_twobyte = rl_lang), '');
        if (cur_iid is not null and ull_iid <> cur_iid)
          {
            res := vector_concat (res, vector (cur_lbl, id_to_iri(cur_iid)));
            n := n + 1;
            if (n >= 50) goto done;
            best_q := 0;
  	}

        cur_iid := ull_iid;
        q := cmp_get_lang_by_q (langs, ull_label_lang);

        if (q >= best_q) 
          {
            best_q := q;
	    if (__tag (ull_label) = 246)
	      cur_lbl := rdf_box_data (ull_label);
	    else
	      cur_lbl := ull_label;
	  }
      }
    res := vector_concat (res, vector (cur_lbl, id_to_iri (cur_iid)));
   done:;
    return res;
  }
}
;

