Hello, I would like to migrate my current code from BaseX 7.3 to the latest version, but when I compile with the new library I get errors at Context.pin(...) and Context.unpin(...). I have some difficulties in finding out how to change the "pin" management. If you don't mind, I'd post two of my class's methods and I would ask you to give me some instructions how to update them. Thank you William
public synchronized Data openOrCreateContainer( Context ctx, final String path) throws IOException {
final IO io = IO.get("file:/"+ getDataDir().getAbsolutePath()); final String name = path;
// check if database is already opened *final Data data = ctx.pin(name);* if(data != null) { final IO in = data.meta.path; if(in.eq(io) && io.timeStamp() == in.timeStamp()) { // check permissions if(ctx.perm(Perm.READ, data.meta)) return data; throw new IOException(Util.info(Text.PERM_REQUIRED_X, Perm.READ)); } *ctx.unpin(data);* } // if found, an existing database is opened final Data ret; if (ctx.mprop.dbexists(name)) { ret = Open.open(name, ctx); ctx.openDB(ret); } else { new CreateDB(name).execute(ctx); ret = ctx.data(); ctx.openDB(ret); } buildIndex(); return ret; }
public boolean loadDocumentFromInputStream(InputStream stream, String docName) throws EtlException { Data cont = null; try { String containerName = docName; cont = openOrCreateContainer(CONTEXT, containerName);
IntList docs = cont.resources.docs(docName); for(final int pre : docs.toArray()) { cont.delete(pre); } Add cmd = new Add(docName); cmd.setInput(stream); cmd.execute(CONTEXT); buildIndex(); return true; } catch (Exception e) { log.error(e); throw new EtlException(e); } finally { if (cont != null) { *CONTEXT.unpin(cont);* cont.close(); } } }
Hi William,
I guess your code is too incomplete in order to tell what may be wrong. Somme spontaneous comments:
• I wonder if both methods use the same context (ctx/CONTEXT)? • It looks dangerous to me to mix static functions (e.g. Open.open) and instance functions (e.g. new CreateDB(name).execute) • A database won’t be unpinned when throwing the IOException in the first function • Why do you use the pin functions at all? If you want to do so, please ensure that pinned databases will always be unpinned.
Christian
On Mon, Dec 23, 2013 at 10:49 AM, William Sandri wsandri@bpeng.com wrote:
Hello, I would like to migrate my current code from BaseX 7.3 to the latest version, but when I compile with the new library I get errors at Context.pin(...) and Context.unpin(...). I have some difficulties in finding out how to change the "pin" management. If you don't mind, I'd post two of my class's methods and I would ask you to give me some instructions how to update them. Thank you William
public synchronized Data openOrCreateContainer( Context ctx, final
String path) throws IOException {
final IO io = IO.get("file:/"+ getDataDir().getAbsolutePath()); final String name = path; // check if database is already opened final Data data = ctx.pin(name); if(data != null) { final IO in = data.meta.path; if(in.eq(io) && io.timeStamp() == in.timeStamp()) { // check permissions if(ctx.perm(Perm.READ, data.meta)) return data; throw new IOException(Util.info(Text.PERM_REQUIRED_X,
Perm.READ)); } ctx.unpin(data); } // if found, an existing database is opened final Data ret; if (ctx.mprop.dbexists(name)) { ret = Open.open(name, ctx); ctx.openDB(ret); } else { new CreateDB(name).execute(ctx); ret = ctx.data(); ctx.openDB(ret); } buildIndex(); return ret; }
public boolean loadDocumentFromInputStream(InputStream stream, String
docName) throws EtlException { Data cont = null; try { String containerName = docName; cont = openOrCreateContainer(CONTEXT, containerName);
IntList docs = cont.resources.docs(docName); for(final int pre : docs.toArray()) { cont.delete(pre); } Add cmd = new Add(docName); cmd.setInput(stream); cmd.execute(CONTEXT); buildIndex(); return true; } catch (Exception e) { log.error(e); throw new EtlException(e); } finally { if (cont != null) { CONTEXT.unpin(cont); cont.close(); } } }
-- William Sandri Business Process Engineering S.r.l. 38068 Rovereto (TN) Via Per Marco 12/A tel: +39 0464 07 60 23 fax: +39 0464 07 20 09 web phone: sip:wsandri@mail.bpeng.com web site:http://www.bpeng.com email:wsandri@bpeng.com
BaseX-Talk mailing list BaseX-Talk@mailman.uni-konstanz.de https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk
Hi Christian, I'm sorry I was not very clear in my explanation. I'll try once more. First of all, at the very beginning I wrote my code using BaseX 6.x and basex's XQJ api. When I migrated to BaseX 7.3 my code was broken because XQJ changed a lot (or something like that, I can't remember) so I tried to update using BaseX core classes. I was not able to find much help in documentation on BaseX website so I sorted it out at my best and I ended up with the code I showed you. With BaseX 7.3 Context had methods pin and unpin that (supposely) returned the requested database. When I updated BaseX libs to 7.7.2, those methods disappeared leaving me with no clue about how to fix my code. So basically I just need to know how to replace the following statements if I use 7.7.2 version:
final Data data = ctx.pin(dbName); ctx.unpin(dbName);
Thanks William
ps: If you could point me to some documentation that can help me write better and safer code using BaseX's core classes I would very appreciate.
On 12/24/2013 05:35 PM, Christian Grün wrote:
Hi William,
I guess your code is too incomplete in order to tell what may be wrong. Somme spontaneous comments:
• I wonder if both methods use the same context (ctx/CONTEXT)? • It looks dangerous to me to mix static functions (e.g. Open.open) and instance functions (e.g. new CreateDB(name).execute) • A database won’t be unpinned when throwing the IOException in the first function • Why do you use the pin functions at all? If you want to do so, please ensure that pinned databases will always be unpinned.
Christian
On Mon, Dec 23, 2013 at 10:49 AM, William Sandri wsandri@bpeng.com wrote:
Hello, I would like to migrate my current code from BaseX 7.3 to the latest version, but when I compile with the new library I get errors at Context.pin(...) and Context.unpin(...). I have some difficulties in finding out how to change the "pin" management. If you don't mind, I'd post two of my class's methods and I would ask you to give me some instructions how to update them. Thank you William
public synchronized Data openOrCreateContainer( Context ctx, final
String path) throws IOException {
final IO io = IO.get("file:/"+ getDataDir().getAbsolutePath()); final String name = path; // check if database is already opened final Data data = ctx.pin(name); if(data != null) { final IO in = data.meta.path; if(in.eq(io) && io.timeStamp() == in.timeStamp()) { // check permissions if(ctx.perm(Perm.READ, data.meta)) return data; throw new IOException(Util.info(Text.PERM_REQUIRED_X,
Perm.READ)); } ctx.unpin(data); } // if found, an existing database is opened final Data ret; if (ctx.mprop.dbexists(name)) { ret = Open.open(name, ctx); ctx.openDB(ret); } else { new CreateDB(name).execute(ctx); ret = ctx.data(); ctx.openDB(ret); } buildIndex(); return ret; }
public boolean loadDocumentFromInputStream(InputStream stream, String
docName) throws EtlException { Data cont = null; try { String containerName = docName; cont = openOrCreateContainer(CONTEXT, containerName);
IntList docs = cont.resources.docs(docName); for(final int pre : docs.toArray()) { cont.delete(pre); } Add cmd = new Add(docName); cmd.setInput(stream); cmd.execute(CONTEXT); buildIndex(); return true; } catch (Exception e) { log.error(e); throw new EtlException(e); } finally { if (cont != null) { CONTEXT.unpin(cont); cont.close(); } } }
-- William Sandri Business Process Engineering S.r.l. 38068 Rovereto (TN) Via Per Marco 12/A tel: +39 0464 07 60 23 fax: +39 0464 07 20 09 web phone: sip:wsandri@mail.bpeng.com web site:http://www.bpeng.com email:wsandri@bpeng.com
BaseX-Talk mailing list BaseX-Talk@mailman.uni-konstanz.de https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk
final Data data = ctx.pin(dbName); ctx.unpin(dbName);
Hm, it's difficult what solution will lead you to a correct implementation. I still think it's better not to explicitly call these functions.
ps: If you could point me to some documentation that can help me write better and safer code using BaseX's core classes I would very appreciate.
It's surely worth if you could spend some more time to analyze the implementations of the existing commands. We tried to properly document all functions in the code. If you believe that specific functions need better comments, feel free to give us some concrete hints or (even better) send us a pull request.
Thanks, Christian
basex-talk@mailman.uni-konstanz.de