@Override public void prepare() throws QueryException { // build data with all documents, to prevent dirty reads md = new MemData(data); for (final Item d : docs) { final MemData docData; if (d.node()) { // adding a document node final ANode doc = (ANode) d; if (doc.ndType() != NodeType.DOC) UPDOCTYPE.thrw(input, doc); docData = new MemData(data); new DataBuilder(docData).build(doc); } else if (d.str()) { // adding file(s) from a path final String docpath = string(d.atom(input)); final String nm = ctx.mprop.random(data.meta.name); final DirParser p = new DirParser(IO.get(docpath), ctx.prop); final MemBuilder b = new MemBuilder(nm, p, ctx.prop); try { docData = b.build(); } catch (final IOException e) { throw DOCERR.thrw(input, docpath); } } else { throw STRNODTYPE.thrw(input, this, d.type); } md.insert(md.meta.size, -1, docData); } // set new names, if needed final IntList pres = md.doc(); if (pres.size() == 1 && name != null) { // name is specified and a single document is added: set the name final byte[] nm = path == null ? name : concat(path, SLASH, name); md.update(pres.get(0), Data.DOC, nm); } else if (path != null) { // path is specified: replace the path of each new document for (int i = 0, is = pres.size(); i < is; i++) { final int d = pres.get(i); final byte[] old = md.text(d, true); final int p = lastIndexOf(old, '/'); final byte[] nm = p < 0 ? old : subtoken(old, p + 1); md.update(d, Data.DOC, concat(path, SLASH, nm)); } } }