Migrating from 7.3 to 7.7.2

Monday, 23 December 2013 · 4 messages · last reply 25 Dec 2013
WS
William Sandri23 Dec 2013 10:49 #
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 at mail.bpeng.com web site:http://www.bpeng.com email:wsandri at bpeng.com
CG
Christian Grün24 Dec 2013 17:35 #
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
Quoted text · William Sandri · 89 lines
On Mon, Dec 23, 2013 at 10:49 AM, William Sandri <wsandri at 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 at mail.bpeng.com > web site:http://www.bpeng.com > email:wsandri at bpeng.com > > > _______________________________________________ > BaseX-Talk mailing list > BaseX-Talk at mailman.uni-konstanz.de > https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk >
WS
William Sandri24 Dec 2013 18:15 #
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.
Quoted text · Christian Grün · 107 lines
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 at 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 at mail.bpeng.com >> web site:http://www.bpeng.com >> email:wsandri at bpeng.com >> >> >> _______________________________________________ >> BaseX-Talk mailing list >> BaseX-Talk at mailman.uni-konstanz.de >> https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk >>
CG
Christian Grün25 Dec 2013 01:56 #
Quoted text · William Sandri · 2 lines
> 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.
Quoted text · William Sandri · 2 lines
> 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

⚡Generated with XQuery