• Topic
  • Discussion
  • VOS.VirtSPARQLOptGuideEx2(Last) -- DAVWikiAdmin? , 2017-06-13 05:35:59 Edit WebDAV System Administrator 2017-06-13 01:35:59

    Generating a Unique ID via SPARQL HTTP

    Suppose you need unique IDs from within Virtuoso (unique only to a given Virtuoso instance/cluster would suffice), and that this should be done via SPARQL HTTP prior to issuing a SPARQL insert.

    One solution would be to create a table and procedure that keeps track of an incremental ID and returns each ID only once. Then you can access this procedure via SPARQL HTTP by using:


    ( 
      SELECT ( bif:foo() ) AS ?id 
      WHERE 
        { 
          ?s ?p ?o 
        } limit 1 
    )
    

    An optimized solution

    Virtuoso's built-in functions (BIFs) sequence_next() and sequence_set() will streamline this task.

    Like any other SQL functions without INOUT or OUT parameters, these can be called from SPARQL simply as bif:sequence_next() or bif:sequence_set(), but an IN parameter may be used to get one sequence per graph, i.e., bif:sequence_next("GRAPH_IDENTIFIER"), e.g., bif:sequence_next("<http://my.example.com/graph1>").


    SQL> SPARQL 
      INSERT INTO GRAPH <http://mygraph.com> 
        { 
          <:a>  <:p>  `bif:sequence_next("<http://mygraph.com>")` 
        } ;
    
    callret-0
    VARCHAR
    _______________________________________________________________________________
    
    Insert into <http://mygraph.com>, 1 (or less) triples -- done
    
    1 Rows. -- 141 msec.
    SQL> SPARQL 
      INSERT INTO GRAPH <http://mygraph.com>
        {
          <:a>  <:p>  `bif:sequence_next("<http://mygraph.com>")` 
        } ;
    
    callret-0
    VARCHAR
    _______________________________________________________________________________
    
    Insert into <http://mygraph.com>, 1 (or less) triples -- done
    
    1 Rows. -- 4 msec.
    SQL> SPARQL 
      SELECT * 
      FROM <http://mygraph.com> 
      WHERE
        { 
          ?s  ?p  ?o 
        }
      ;
    
    s                  p                  o
    VARCHAR            VARCHAR            VARCHAR
    _______________________________________________________________________________
    
    :a                 :p                 0
    :a                 :p                 1
    
    2 Rows. -- 1 msec.
    SQL> 
    

    Side-note -- bif: prefix vs. sql: prefix

    For user-defined functions, the sql: prefix is preferred over the bif: prefix; e.g.,


    sql:foo(x) 
    

    -- will call --


    DB.DBA.foo(x)
    

    The difference is that bif:foo will make an unqualified call to foo(). If no built-in function foo() exists, and the system contains many users and database qualifiers, the search for exact name of XXX.YYY.foo() adds needless overhead. Further issues may arise if multiple XXX.YYY.foo() procedures have been defined.

    Related