Java DOM Interface and updating XQueries
Wednesday, 12 June 2013 · 5 messages · last reply 13 Jun 2013
ID
Ingo Dengler12 Jun 2013 09:08 #
Hi,
I have a problem using BaseX Java DOM Node Interface and updating XQuerys in combination. The problem is that on updating the XML structure the node "pointer" moves to another location therefore the BXNode Object points to the wrong node.
I have investigated a bit and the problem seems to be the 'pre' attribute in Class DBNode that points to the wrong/old location. There is already an uninque ID in ANode that could be used to identify the right node. But a ID->pre mapping table is missing.
Any solution/workaround for this issue?
Best regards,
Ingo
See following snippet that demonstrates the issue:
import java.io.ByteArrayInputStream;
import org.basex.api.dom.BXNode;
import org.basex.core.Context;
import org.basex.core.Prop;
import org.basex.core.cmd.CreateDB;
import org.basex.core.cmd.Set;
import org.basex.query.QueryProcessor;
import org.basex.query.value.item.Item;
import org.basex.query.value.node.ANode;
public class UpdateAndDOM {
public static void main(String[] args) throws Exception { // create test DB in memory
Context context = new Context();
new Set(Prop.
MAINMEM, true).execute(context);
ByteArrayInputStream bais = new ByteArrayInputStream("<root><node1/></root>".getBytes());
CreateDB cmd = new CreateDB("testDB");
cmd.setInput(bais);
cmd.execute(context);
//get DOM node "node1"
QueryProcessor query = new QueryProcessor("//node1", context);
query.execute();
Item item = query.iter().next();
Object elem = BXNode.get((ANode)item);
BXNode node1 = (BXNode)elem;
System.out.println("Expected node1 is: " + node1.getNodeName());
//updating query that inserts data before node1
String xpath = "insert node <newNode/> before /root/node1";
query = new QueryProcessor(xpath, context);
query.execute();
System.out.println("Expected node1 is: " + node1.getNodeName());
}
}ID
Ingo Dengler12 Jun 2013 10:55 #
Hi,
Sorry for posting the first message in HTML format (damn web mailer...).
Here is it again in Text only:
I have a problem using BaseX Java DOM Node Interface and updating XQuerys in combination. The problem is that on updating the XML structure the node "pointer" moves to another location therefore the BXNode Object points to the wrong node. I have investigated a bit and the problem seems to be the 'pre' attribute in Class DBNode that points to the wrong/old location. There is already an uninque ID in ANode that could be used to identify the right node. But a ID->pre mapping table is missing. Any solution/workaround for this issue?
Best regards,
Ingo
See following snippet that demonstrates the issue:
import java.io.ByteArrayInputStream;
import org.basex.api.dom.BXNode;
import org.basex.core.Context;
import org.basex.core.Prop;
import org.basex.core.cmd.CreateDB;
import org.basex.core.cmd.Set;
import org.basex.query.QueryProcessor;
import org.basex.query.value.item.Item;
import org.basex.query.value.node.ANode;
public class UpdateAndDOM {
public static void main(String[] args) throws Exception {
// create test DB in memory
Context context = new Context();
new Set(Prop.MAINMEM, true).execute(context);
ByteArrayInputStream bais = new ByteArrayInputStream("<root><node1/></root>".getBytes());
CreateDB cmd = new CreateDB("testDB");
cmd.setInput(bais);
cmd.execute(context);
//get DOM node "node1"
QueryProcessor query = new QueryProcessor("//node1", context);
query.execute();
Item item = query.iter().next();
Object elem = BXNode.get((ANode)item);
BXNode node1 = (BXNode)elem;
System.out.println("Expected node1 is: " + node1.getNodeName());
//updating query that inserts data before node1
String xpath = "insert node <newNode/> before /root/node1";
query = new QueryProcessor(xpath, context);
query.execute();
System.out.println("Expected node1 is: " + node1.getNodeName());
}
}CG
Hi Ingo,
as you correctly observed, we are using 'pre' values to organize nodes in our database, which will change after updates (see [1] for further details on pre and id values and the id/pre mapping). It is generally possible to use our DOM interface, but I’m inclined to recommend you the database functions db:open-pre() and
db:node-pre(). If you perform updates, db:open-id() and db:node-id() and the UPDINDEX option will help you [2]. You could also work with the DBNode instances, which are returned by the query processor, as this will be much faster than using the DOM wrapper, but it may take more time to get acquainted with the internal node interface.
Regards,
Christian
[1] http://docs.basex.org/wiki/Node_Storage#PRE_Value
[2] http://docs.basex.org/wiki/Database_Module#db:open-preQuoted text · Ingo Dengler · 54 lines
> I have a problem using BaseX Java DOM Node Interface and updating XQuerys in > combination. The problem is that on updating the XML structure the node > "pointer" moves to another location therefore the BXNode Object points to > the wrong node. > I have investigated a bit and the problem seems to be the 'pre' attribute in > Class DBNode that points to the wrong/old location. There is already an > uninque ID in ANode that could be used to identify the right node. But a > ID->pre mapping table is missing. > Any solution/workaround for this issue? > > Best regards, > Ingo > > See following snippet that demonstrates the issue: > > import java.io.ByteArrayInputStream; > import org.basex.api.dom.BXNode; > import org.basex.core.Context; > import org.basex.core.Prop; > import org.basex.core.cmd.CreateDB; > import org.basex.core.cmd.Set; > import org.basex.query.QueryProcessor; > import org.basex.query.value.item.Item; > import org.basex.query.value.node.ANode; > public class UpdateAndDOM { > public static void main(String[] args) throws Exception { > // create test DB in memory > Context context = new Context(); > new Set(Prop.MAINMEM, true).execute(context); > ByteArrayInputStream bais = new > ByteArrayInputStream("<root><node1/></root>".getBytes()); > CreateDB cmd = new CreateDB("testDB"); > cmd.setInput(bais); > cmd.execute(context); > //get DOM node "node1" > QueryProcessor query = new QueryProcessor("//node1", context); > query.execute(); > Item item = query.iter().next(); > Object elem = BXNode.get((ANode)item); > BXNode node1 = (BXNode)elem; > System.out.println("Expected node1 is: " + node1.getNodeName()); > //updating query that inserts data before node1 > String xpath = "insert node <newNode/> before /root/node1"; > query = new QueryProcessor(xpath, context); > query.execute(); > System.out.println("Expected node1 is: " + node1.getNodeName()); > } > } > > _______________________________________________ > BaseX-Talk mailing list > BaseX-Talk at mailman.uni-konstanz.de > https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk >
ID
Ingo Dengler13 Jun 2013 13:01 #
Hi Christian,
the problem also exists with DBNode, isn't it? DBNode internally stores the pre of the node at time of instantiation of the object. After an update of DB the internally stored pre points to a wrong node. Of course one could use the ID of DBNode and use db:open-id(id) to get a new DBNode with correct pre. But this approach doesn't work with DOM API. My question is, is there a possible solution that allows me to use DOM interface (We need DOM, because we want to use BaseX to replace an old DOM based parser class without reimplementing everything) and updating expressions?
Best regards,
Ingo
Quoted text · Christian Grün · 18 lines
> Hi Ingo, > > as you correctly observed, we are using 'pre' values to organize nodes > in our database, which will change after updates (see [1] for further > details on pre and id values and the id/pre mapping). It is generally > possible to use our DOM interface, but I’m inclined to recommend you > the database functions db:open-pre() and db:node-pre(). If you perform > updates, db:open-id() and db:node-id() and the UPDINDEX option will > help you [2]. You could also work with the DBNode instances, which are > returned by the query processor, as this will be much faster than > using the DOM wrapper, but it may take more time to get acquainted > with the internal node interface. > > Regards, > Christian > > [1] http://docs.basex.org/wiki/Node_Storage#PRE_Value > [2] http://docs.basex.org/wiki/Database_Module#db:open-pre[http://docs.basex.org/wiki/Database_Module#db:open-pre]
CG
Christian Grün13 Jun 2013 14:39 #
Hi Ingo,
Quoted text · Ingo Dengler · 1 line
> the problem also exists with DBNode, isn't it?
A DBNode contains a reference to the Data instance, which can be used to retrieve the id value of a node ( DBNode.id(pre) ).
Quoted text · Ingo Dengler · 1 line
> My question is, is there a possible solution that allows me to use DOM interface (We need DOM, because we want to use BaseX to replace an old DOM based parser class without reimplementing everything) and updating expressions?
This sounds like an audacious venture, because our DOM wrapper was never built to be applied as a serious replacement for the internal node representation… Which is why I would personally avoid pursuing such an approach. A classical DOM implementation is based on a main-memory structure without transaction semantics, whereas BaseX database nodes refer to disk structures, which require an open database, may be modified by other concurrent processes, etc. However, I agree that the existing DOM implementation of BaseX may have the potential to be successfully applied to specific use cases. I just doubt it can be made water-proof for all kinds of scenarios (which is the reason why we already thought about completely removing it from the core package, and instead build new DOM nodes from serialized BaseX results).
I hope this helps,
Christian
Quoted text · Ingo Dengler · 25 lines
> Best regards, > Ingo > >> Hi Ingo, >> >> as you correctly observed, we are using 'pre' values to organize nodes >> in our database, which will change after updates (see [1] for further >> details on pre and id values and the id/pre mapping). It is generally >> possible to use our DOM interface, but I’m inclined to recommend you >> the database functions db:open-pre() and db:node-pre(). If you perform >> updates, db:open-id() and db:node-id() and the UPDINDEX option will >> help you [2]. You could also work with the DBNode instances, which are >> returned by the query processor, as this will be much faster than >> using the DOM wrapper, but it may take more time to get acquainted >> with the internal node interface. >> >> Regards, >> Christian >> >> [1] http://docs.basex.org/wiki/Node_Storage#PRE_Value >> [2] http://docs.basex.org/wiki/Database_Module#db:open-pre[http://docs.basex.org/wiki/Database_Module#db:open-pre] > _______________________________________________ > BaseX-Talk mailing list > BaseX-Talk at mailman.uni-konstanz.de > https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk
Written for BaseX 7.6. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery