doc.setSplitCount(0); doc.setInternalAddress(BFile.UNKNOWN_ADDRESS); doc.setPageCount(tempDoc.getPageCount());
doc.getMetadata().setSplitCount(0); doc.getMetadata().setPageCount(tempDoc.getMetadata().getPageCount());
	public void defrag(final Txn transaction, final DocumentImpl doc) {        //TODO : use dedicated function in XmldbURI		LOG.debug("============> Defragmenting document " + 		        doc.getCollection().getName() + "/" + doc.getFileName());//        Writer writer = new StringWriter();//        try {//            domDb.dump(writer);//        } catch (BTreeException e1) {//            //  Auto-generated catch block//            e1.printStackTrace();//        } catch (IOException e1) {//            //  Auto-generated catch block//            e1.printStackTrace();//        }//        System.out.println(writer.toString());        		final long start = System.currentTimeMillis();		try {//			checkTree(doc);//            try {//                domDb.printFreeSpaceList();//            } catch (IOException e1) {//                // Auto-generated catch block//                e1.printStackTrace();//            }			// remember this for later remove		    final long firstChild = doc.getFirstChildAddress();		    				// dropping old structure index			elementIndex.dropIndex(doc);			valueIndex.dropIndex(doc);			if (qnameValueIndex != null)				qnameValueIndex.dropIndex(doc);						// dropping dom index			NodeRef ref = new NodeRef(doc.getDocId());			final IndexQuery idx = new IndexQuery(IndexQuery.TRUNC_RIGHT, ref);			new DOMTransaction(this, domDb) {				public Object start() {					try {						domDb.remove(transaction, idx, null);						domDb.flush();					} catch (BTreeException e) {			            LOG.warn("start() - " + "error while removing doc", e);					} catch (IOException e) {			            LOG.warn("start() - " + "error while removing doc", e);					} catch (TerminatedException e) {			            LOG.warn("method terminated", e);			        } catch (DBException e) {			        	LOG.warn("start() - " + "error while removing doc", e);					}					return null;				}			}			.run();						// create a copy of the old doc to copy the nodes into it			DocumentImpl tempDoc = new DocumentImpl(this, doc.getFileName(), doc.getCollection());			tempDoc.copyOf(doc);			tempDoc.setDocId(doc.getDocId());						// copy the nodes			Iterator iterator;			NodeList nodes = doc.getChildNodes();			StoredNode n;			for (int i = 0; i < nodes.getLength(); i++) {			    n = (StoredNode) nodes.item(i);			    iterator =			        getNodeIterator(			                new NodeProxy(doc, n.getGID(), n.getInternalAddress()));			    iterator.next();			    copyNodes(transaction, iterator, n, new NodePath(), tempDoc, false);			}			flush();			//			checkTree(tempDoc);			// remove the old nodes			new DOMTransaction(this, domDb) {				public Object start() {					domDb.removeAll(transaction, firstChild);					try {						domDb.flush();					} catch (DBException e) {						LOG.warn("start() - " + "error while removing doc", e);					}					return null;				}			}			.run();			//			checkTree(tempDoc);						doc.copyChildren(tempDoc);			doc.setSplitCount(0);			doc.setInternalAddress(BFile.UNKNOWN_ADDRESS);			doc.setPageCount(tempDoc.getPageCount());						storeDocument(transaction, doc);			LOG.debug("new doc address = " + StorageAddress.toString(doc.getInternalAddress()));			closeDocument();//			new DOMTransaction(this, domDb, Lock.READ_LOCK) {//				public Object start() throws ReadOnlyException {//					LOG.debug("Pages used: " + domDb.debugPages(doc));//					return null;//				}//			}.run();			storeDocument(transaction, doc);//            checkTree(doc);			LOG.debug("Defragmentation took " + (System.currentTimeMillis() - start) + "ms.");		} catch (ReadOnlyException e) {			LOG.warn(DATABASE_IS_READ_ONLY, e);		}	}