vgritsenko 2003/12/23 04:20:07
Modified: java/src/org/apache/xindice/core Collection.java Database.java Log: Some refactorings to reduce code duplication in insertBinary/insertDocument/insertObject Revision Changes Path 1.41 +62 -104 xml-xindice/java/src/org/apache/xindice/core/Collection.java Index: Collection.java =================================================================== RCS file: /home/cvs/xml-xindice/java/src/org/apache/xindice/core/Collection.java,v retrieving revision 1.40 retrieving revision 1.41 diff -u -r1.40 -r1.41 --- Collection.java 22 Dec 2003 14:05:33 -0000 1.40 +++ Collection.java 23 Dec 2003 12:20:07 -0000 1.41 @@ -157,16 +157,16 @@ } public void commit() throws DBException { - putDocument(key, document /*, false */); + putDocument(this.key, this.document /*, false */); } public void commit(Document doc) throws DBException { - document = doc; + this.document = doc; commit(); } public String getCanonicalName() throws DBException { - return getCanonicalDocumentName(key); + return Collection.this.getCanonicalDocumentName(key); } public Collection getCollection() { @@ -174,11 +174,11 @@ } public Document getDocument() { - return document; + return this.document; } public Key getKey() { - return key; + return this.key; } public void remove() throws DBException { @@ -186,8 +186,8 @@ } public Document rollback() throws DBException { - document = Collection.this.getDocument(key); - return document; + this.document = Collection.this.getDocument(key); + return this.document; } } @@ -261,11 +261,9 @@ } /** - * * @param parentCollection */ public Collection(Collection parentCollection) { - this(); this.parent = parentCollection; } @@ -280,6 +278,7 @@ * @see org.apache.xindice.core.DBObject#close() */ public boolean close() throws DBException { + // TODO: Why this is no-op? How filers (and all RandomAccessFiles) will ever get closed? return true; } @@ -318,18 +317,17 @@ } /** - * createNewKey allocates a new Key to be used as a Key in the - * Collection. + * createNewKey allocates a new key to be used as a key in the + * collection. Passed in <code>key</code> parameter string value + * used for the key. If passed key parameter is null, new OID is generated. * - * @param key The Key hint + * @param key The Key hint, can be null * @return The newly generated Key */ protected final Key createNewKey(Object key) { if (key == null) { return createNewOID(); - } - - if (key instanceof Key) { + } else if (key instanceof Key) { return (Key) key; } else { return new Key(key.toString()); @@ -340,11 +338,10 @@ * createNewOID allocates a new Object ID to be used as a Key in the * Collection. * - * @return The newly generated Key + * @return The newly generated key */ public final Key createNewOID() { long ct = System.currentTimeMillis(); - synchronized (oidMutex) { if (ct <= documentId) { ct = documentId + 1; @@ -356,7 +353,6 @@ String document = Long.toString(documentId, 16); sb.insert(32 - document.length(), document); sb.setLength(32); - return new Key(sb.toString()); } @@ -396,8 +392,7 @@ // Drop the meta if necessary if (isMetaEnabled()) { - MetaSystemCollection metacol = getMetaSystemCollection(); - metacol.dropCollectionMeta(this); + getMetaSystemCollection().dropCollectionMeta(this); } // Drop Child Collections @@ -485,8 +480,8 @@ * header corruption */ public final byte[] getBinary(Object key) throws DBException { - if (log.isDebugEnabled()) { - log.debug(debugHeader() + "Get binary: " + key); + if (log.isTraceEnabled()) { + log.trace(debugHeader() + "Get binary: " + key); } if (inlineMetaService == null) { @@ -776,10 +771,9 @@ if (compressed) { document = new DocumentImpl(value.getData(), symbols, new NodeSource(this, key)); flushSymbolTable(); - if (log.isTraceEnabled()) { - log.trace(localDebugHeader - + "Compressed XML document=<" + TextWriter.toString(document) + ">"); + log.trace(localDebugHeader + + "Compressed XML document=<" + TextWriter.toString(document) + ">"); } if (documentCache != null) { @@ -942,45 +936,33 @@ * error occurs while saving. */ public Key insertBinary(byte[] bytes) throws DBException { - if (inlineMetaService == null) { - throw new DBException(FaultCodes.COL_CANNOT_STORE, - "Cannot insert a binary resource in collection " + name + - ": inline-metadata is not enabled."); - } - - Key key = createNewOID(); - if (log.isInfoEnabled()) { - log.info(debugHeader() + "Insert binary. Key created: " + key); - } - putBinary(key, bytes, true); - - // Update the meta information if necessary - updateCollectionMeta(); - return key; + return insertBinary(null, bytes); } /** * insertBinary inserts a new binary object into a Xindice Collection. * - * @param key The document Key + * @param docKey The document Key * @param bytes The document to insert * @throws DBException if inline-metadata is not enabled, the key is * already in the database, or an error occurs while saving. */ - public void insertBinary(Object key, byte[] bytes) throws DBException { + public Key insertBinary(Object docKey, byte[] bytes) throws DBException { if (inlineMetaService == null) { throw new DBException(FaultCodes.COL_CANNOT_STORE, "Cannot insert a binary resource in collection " + name + ": inline-metadata is not enabled."); } + Key key = createNewKey(docKey); if (log.isInfoEnabled()) { log.info(debugHeader() + "Insert binary: " + key); } - putBinary(createNewKey(key), bytes, true); + putBinary(key, bytes, true); // update the meta information if necessary updateCollectionMeta(); + return key; } /** @@ -990,15 +972,7 @@ * @return The new Object Identifier */ public final Key insertDocument(Document document) throws DBException { - Key key = createNewOID(); - if (log.isInfoEnabled()) { - log.info(debugHeader() + "Insert document. Key created: " + key); - } - putDocument(key, document /*, true */); - - // update the meta information if necessary - updateCollectionMeta(); - return key; + return insertDocument(null, document); } /** @@ -1007,46 +981,42 @@ * @param docKey The document Key * @param document The document to insert */ - public final void insertDocument(Object docKey, Document document) throws DBException { + public final Key insertDocument(Object docKey, Document document) throws DBException { + Key key = createNewKey(docKey); if (log.isInfoEnabled()) { - log.info(debugHeader() + "Insert document: " + docKey); + log.info(debugHeader() + "Insert document: " + key); } - putDocument(createNewKey(docKey), document /*, true */); + putDocument(key, document /*, true */); // update the meta information if necessary updateCollectionMeta(); + return key; } /** - * insertObject inserts an XMLSerializable object into the Collection based - * on the specified Key. Xindice takes care of associating the + * insertObject inserts an XMLSerializable object into the Collection and + * returns a newly generated Key. Xindice takes care of associating the * implementation class with the XMLSerializable object. * - * @param key The Key to use * @param obj The Object to insert + * @return The newly generated Key */ - public final void insertObject(String key, XMLSerializable obj) throws DBException { - if (log.isInfoEnabled()) { - log.info(debugHeader() + "Insert object: " + key); - } - putObject(createNewKey(key), obj /*, true */); - - // update the meta information if necessary - updateCollectionMeta(); + public final Key insertObject(XMLSerializable obj) throws DBException { + return insertObject(null, obj); } /** - * insertObject inserts an XMLSerializable object into the Collection and - * returns a newly generated Key. Xindice takes care of associating the + * insertObject inserts an XMLSerializable object into the Collection based + * on the specified Key. Xindice takes care of associating the * implementation class with the XMLSerializable object. * + * @param objKey The Key to use * @param obj The Object to insert - * @return The newly generated Key */ - public final Key insertObject(XMLSerializable obj) throws DBException { - Key key = createNewOID(); + public final Key insertObject(String objKey, XMLSerializable obj) throws DBException { + Key key = createNewKey(objKey); if (log.isInfoEnabled()) { - log.info(debugHeader() + "Insert object. Key created: " + key); + log.info(debugHeader() + "Insert object: " + key); } putObject(key, obj /*, true */); @@ -1055,7 +1025,6 @@ return key; } - // META DATA RELATED DOCS /** * Returns whether or not meta data is enabled. * @return boolean whether or not meta data is enabled. @@ -1139,7 +1108,7 @@ /* * Lowest-level method for saving a binary entry into the database. - * Does not update non-inline metadata. + * It now does update non-inline metadata if the user has configured it. */ private void putBinary(Key key, byte[] bytes, boolean create) throws DBException { if (inlineMetaService == null) { @@ -1151,9 +1120,7 @@ if (!create) { byte[] storedBytes = getBinary(key); if (storedBytes == null) { - /* - * TODO: Do we need a COL_KEY_ALREADY_PRESENT fault so that the caller can interpret this exception? - */ + // TODO: Do we need a COL_KEY_ALREADY_PRESENT fault so that the caller can interpret this exception? throw new DBException(FaultCodes.COL_CANNOT_STORE, "Error storing binary object with key '" + key + "': the 'create' flag is false and" + " the key is not in the database"); @@ -1164,6 +1131,10 @@ map.put("type", ResourceTypeReader.BINARY); Value value = inlineMetaService.createValue(map, bytes, 0, bytes.length); filer.writeRecord(key, value); + + // update the meta for this document + updateDocumentMeta(key.toString()); + // Not observable. DBObserver.getInstance().putDocument(this, key, document, oldDoc == null); } /** @@ -1200,21 +1171,19 @@ * otherwise without headers, for the BTree. */ - byte[] packedDocument = null; - byte[] utf8Document = null; + byte[] documentBytes = null; if (compressed) { try { - packedDocument = DOMCompressor.Compress(document, symbols); + documentBytes = DOMCompressor.Compress(document, symbols); if (log.isTraceEnabled()) { - log.trace(localDebugHeader + "length=" + packedDocument.length); + log.trace(localDebugHeader + "length=" + documentBytes.length); } // Why must it be re-created? - document = new DocumentImpl(packedDocument, symbols, new NodeSource(this, key)); - + document = new DocumentImpl(documentBytes, symbols, new NodeSource(this, key)); if (log.isTraceEnabled()) { - log.trace(localDebugHeader + "packedDocument: length=" + packedDocument.length + + log.trace(localDebugHeader + "packedDocument: length=" + documentBytes.length + " document=<" + TextWriter.toString(document) + ">"); } } catch (Exception e) { @@ -1223,10 +1192,10 @@ } } else { try { - utf8Document = TextWriter.toString(document).getBytes("utf-8"); + documentBytes = TextWriter.toString(document).getBytes("utf-8"); if (log.isTraceEnabled()) { - log.trace(localDebugHeader + "utf8Document: length=" + utf8Document.length + - " document=<" + new String(utf8Document, "utf-8") + ">"); + log.trace(localDebugHeader + "utf8Document: length=" + documentBytes.length + + " document=<" + new String(documentBytes, "utf-8") + ">"); } } catch (UnsupportedEncodingException e) { // Should never happen @@ -1235,9 +1204,6 @@ } } - /* - * TODO: Can this be moved into the if(compressed) block? - */ flushSymbolTable(); // Temporary until insert and update are separate @@ -1252,26 +1218,18 @@ */ Value value; if (inlineMetaService == null) { - if (compressed) { - value = new Value(packedDocument); - } else { - value = new Value(utf8Document); - } + value = new Value(documentBytes); } else { InlineMetaMap map = inlineMetaService.getEmptyMap(); map.put("type", ResourceTypeReader.XML); - if (compressed) { - value = inlineMetaService.createValue(map, packedDocument, 0, packedDocument.length); - } else { - value = inlineMetaService.createValue(map, utf8Document, 0, utf8Document.length); - } + value = inlineMetaService.createValue(map, documentBytes, 0, documentBytes.length); } filer.writeRecord(key, value); // Cache Stuff if (documentCache != null) { if (compressed) { - documentCache.putDocument(this, key, packedDocument); + documentCache.putDocument(this, key, documentBytes); } else { documentCache.putDocument(this, key, document); } @@ -1668,7 +1626,7 @@ return; } - Document doc = getDocument(id); + Object doc = getEntry(id); if (null == doc) { throw new DBException(FaultCodes.COL_DOCUMENT_NOT_FOUND, "Document " + id + " does not exist in " + getCanonicalName()); 1.33 +2 -3 xml-xindice/java/src/org/apache/xindice/core/Database.java Index: Database.java =================================================================== RCS file: /home/cvs/xml-xindice/java/src/org/apache/xindice/core/Database.java,v retrieving revision 1.32 retrieving revision 1.33 diff -u -r1.32 -r1.33 --- Database.java 22 Dec 2003 14:04:04 -0000 1.32 +++ Database.java 23 Dec 2003 12:20:07 -0000 1.33 @@ -196,7 +196,6 @@ * properly flushed to disk after a modification. */ public void flushConfig() { - try { Document d = getConfig().getElement().getOwnerDocument(); systemCollection.getCollection(SystemCollection.CONFIGS).setDocument(COLKEY, d);