You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xindice-users@xml.apache.org by Fernando EugĂȘnio <ph...@hotmail.com> on 2004/05/31 15:21:48 UTC

Problems using index

Hi.
I'm trying to use index, but it takes the same time with the index than 
without it.

Here are some methods that I'm using:
It creates the index file, but it isn't working.
index file (codCli_idx.idx): 6mb   table file (Testando.tbl): 1.4mb . I 
think it's strange.
I'm using only one xml file.

public Entidade consulta(Entidade e){
   EntidadeCliente c = (EntidadeCliente)e;  //EntidadeCliente is an entity
   EntidadeCliente ent = new EntidadeCliente();
   ent.setNome(consulta("//clientes/cliente[codCli = '" + c.getCodCli() + 
"']/nome/text()"));
   ent.setCpf(consulta("//clientes/cliente[codCli = '" + c.getCodCli() + 
"']/cpf/text()"));
   ent.setEndereco(consulta("//clientes/cliente[codCli = '" + c.getCodCli() 
+ "']/endereco/text()"));
   ent.setBairro(consulta("//clientes/cliente[codCli = '" + c.getCodCli() + 
"']/bairro/text()"));
   ent.setCidade(consulta("//clientes/cliente[codCli = '" + c.getCodCli() + 
"']/cidade/text()"));
   ent.setEstado(consulta("//clientes/cliente[codCli = '" + c.getCodCli() + 
"']/estado/text()"));
   ent.setCodCli(Integer.parseInt((consulta("//clientes/cliente[codCli = '" 
+ c.getCodCli()     + "']/codCli/text()"))));
   return ent;
}

//method that creates the index
public void criaIndice(Collection collec, String nome, String pattern){
   CollectionManager colman = null;
   String[] idx = null;
   try {
      colman = (CollectionManager) collec.getService("CollectionManager", 
"1.0");
      idx = colman.listIndexers();
   } catch (XMLDBException e1) {
      e1.printStackTrace();
   }
   boolean id = false;

   for(int i = 0; i < idx.length; i++){
      if (nome.equals(idx[i])){
         id = true;
      }
   }

   System.out.println(idx.length);
   System.out.println(id);

   if ((idx.length < 1) || (id == false)) {
      System.out.println("initializeXMLDB - Creating indexer");
      String document = "<index 
class='org.apache.xindice.core.indexer.ValueIndexer' name='" + nome + "' 
pattern='" + pattern + "' />";
      try {
          colman.createIndexer(DOMParser.toDocument(document));
      } catch (XMLDBException e2) {
           e2.printStackTrace();
      } catch (XindiceException e2) {
          e2.printStackTrace();
      }
   }
   else{
      System.out.println("initializeXMLDB - Indexes:\n");
      for (int i = 0; i < idx.length; i++) {
          System.out.println("initializeXMLDB - " + i + " - " + idx[i]);
      }
      System.out.println("initializeXMLDB - Total Indexes: " + idx.length);
   }
}

public static void main(String args[]){
   ClienteXML c = new ClienteXML();
   c.criaIndice(c.col, "codCli_idx", "codCli");  //creating index
   EntidadeCliente ent = new EntidadeCliente();
   Entidade ent1;

   long lg1, lg2;
   lg1 = System.currentTimeMillis();
   System.out.println(lg1 + "\n");
/************************************************************************/
   ent.setCodCli(6000);   //setting the codCli (that will be used to index)
   ent1 = c.consulta(ent);
/************************************************************************/
   lg2 = System.currentTimeMillis();
   System.out.println("\n" + lg2);
   System.out.println("\n Time: " + (lg2 - lg1) + " ms");
   System.out.println("\n Time: " + ((lg2 - lg1)/1000) + " s");
}



This is a sample of the xml file:

<clientes>
   <cliente>
      <nome>TESTE2</nome>
      <cpf>541844333</cpf>
      <endereco>Rua Fulano de Tal, 54</endereco>
      <bairro>Bauxita</bairro>
      <cidade>Ouro Preto</cidade>
      <estado>MG</estado>
      <codCli>325</codCli>
   </cliente>
</clientes>



Am I doing something wrong?
Please, help me.

Thanks.

_________________________________________________________________
MSN Hotmail, o maior webmail do Brasil.  http://www.hotmail.com


Re: Problems using index

Posted by Vadim Gritsenko <va...@reverycodes.com>.
Fernando EugĂȘnio wrote:

> Hi.
> I'm trying to use index, but it takes the same time with the index 
> than without it.

..

> public Entidade consulta(Entidade e){
>   EntidadeCliente c = (EntidadeCliente)e;  //EntidadeCliente is an entity
>   EntidadeCliente ent = new EntidadeCliente();
>   ent.setNome(consulta("//clientes/cliente[codCli = '" + c.getCodCli() 
> + "']/nome/text()"));
>   ent.setCpf(consulta("//clientes/cliente[codCli = '" + c.getCodCli() 
> + "']/cpf/text()"));
>   ent.setEndereco(consulta("//clientes/cliente[codCli = '" + 
> c.getCodCli() + "']/endereco/text()"));
>   ent.setBairro(consulta("//clientes/cliente[codCli = '" + 
> c.getCodCli() + "']/bairro/text()"));
>   ent.setCidade(consulta("//clientes/cliente[codCli = '" + 
> c.getCodCli() + "']/cidade/text()"));
>   ent.setEstado(consulta("//clientes/cliente[codCli = '" + 
> c.getCodCli() + "']/estado/text()"));
>   ent.setCodCli(Integer.parseInt((consulta("//clientes/cliente[codCli 
> = '" + c.getCodCli()     + "']/codCli/text()"))));
>   return ent;
> }


Are those "//..." expressions the XPath queries you run? I'll assume 
that yes...


> This is a sample of the xml file:
>
> <clientes>
>   <cliente>
>      <nome>TESTE2</nome>
>      <cpf>541844333</cpf>
>      <endereco>Rua Fulano de Tal, 54</endereco>
>      <bairro>Bauxita</bairro>
>      <cidade>Ouro Preto</cidade>
>      <estado>MG</estado>
>      <codCli>325</codCli>
>   </cliente>
> </clientes>
>
>
>
> Am I doing something wrong?


I see several issues:

1. You use "//clientes/cliente" instead of "/clientes/cliente".

2. How many <cliente/> you have in a single <clientes/> file?

3. I see multiple repetitions of "//clientes/cliente[codCli = '" + 
c.getCodCli() + "']". It might be more efficient to query once and get 
result as DOM and obtain the rest of information from the DOM - 
depending on answer on question 2.

4. You should have Value index with pattern "codCli" - seems that you 
already have it. Usefulness of the index also depends on question 2.

Vadim