Package org.apache.xindice.core.meta.inline

Source Code of org.apache.xindice.core.meta.inline.InlineMetaService$DatabaseEntry

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* $Id: InlineMetaService.java 511426 2007-02-25 03:25:02Z vgritsenko $
*/

package org.apache.xindice.core.meta.inline;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.xindice.core.FaultCodes;
import org.apache.xindice.core.data.Value;

/**
* If the documents in a collection have headers, the Collection
* object holds an instance of this class.  All services the collection
* needs for working with inline metadata are provided by this class.
*
* @version $Revision: 511426 $, $Date: 2007-02-24 22:25:02 -0500 (Sat, 24 Feb 2007) $
*/
public class InlineMetaService {

    private static final Log log = LogFactory.getLog(InlineMetaService.class);

    /**
     * The known readers.  All readers for header versions up to
     * and including the current version must be present in this array.
     */
    public static final InlineMetaReader[] readerByVersion = {new NullReader(), new ResourceTypeReader()};

    /**
     * The writer corresponding to the current header version.
     */
    public static final InlineMetaWriter currentWriter = new ResourceTypeWriter();

    /**
     * The InlineMetaMap class associated with the current header
     * version.  When the Collection fills in an InlineMetaMap object
     * prior to a store, it is working with this class.
     */
    public static final Class currentMapClass = ResourceTypeReader.ResourceTypeMap.class;

    /**
     * Get an <code>InlineMetaMap</code> instance corresponding to the
     * current header version.  Used by <code>Collection</code> to inform
     * the InlineMeta code of the metadata values for the entry being stored.
     *
     * @return InlineMetaMap instance
     */
    public InlineMetaMap getEmptyMap() {
        try {
            return (InlineMetaMap) currentMapClass.newInstance();
        } catch (Exception e) {
            String msg = "unable to create instance of current inline metadata map class '" + currentMapClass.getName() + "'";
            /*
             * This can only happen as a result of a programming error,
             * so log it and throw a runtime.  Shouldn't happen in production.
             */
            if (log.isFatalEnabled()) {
                log.fatal(msg, e);
            }
            throw new IllegalStateException(msg);
        }
    }

    /**
     * Create a Value object containing the appropriate header and
     * data.  This utility method takes the metadata and the data
     * for the entry to be stored in the database and creates a
     * <code>Value</code> object containing both, ready to store.
     *
     * @param map containing metadata
     * @param data containing entry to be stored in database
     * @param offset in the data to the beginning of the entry
     * @param length of the entry
     * @return Value object containing the header + entry, ready to store
     * @throws InlineMetaException if the metadata is incomplete
     */
    public Value createValue(InlineMetaMap map, byte[] data, int offset, int length) throws InlineMetaException {

        byte[] metadata = currentWriter.getMetadata(map);

        return InlineHeaderBuilder.createValue(currentWriter.getVersion(), metadata, data, offset, length);
    }

    /**
     * Returns true if a reader is available for the specified
     * version.
     *
     * @param version of the header
     * @return true if a reader is available for the given version
     */
    public boolean haveReaderForVersion(int version) {
        return version < readerByVersion.length;
    }

    /**
     * Given a <code>Value</code> object from the database,
     * disassemble it into its component metadata and entry data
     * components.
     *
     * @param rawValue containing raw header + entry data from the backing store
     * @return DatabaseEntry containing metadata in an <code>InlineMetaMap</code>
     *         and entry data in a <code>Value</code>.
     * @throws InlineMetaException if missing the reader for the header, or if
     *         the header is corrupted.
     */
    public DatabaseEntry readDatabaseEntry(Value rawValue) throws InlineMetaException {
        if (log.isDebugEnabled()) {
            log.debug("readDatabaseEntry: rawData: length=" + rawValue.getLength() +
                      " byte 0: " + rawValue.byteAt(0) + " byte 1: " + rawValue.byteAt(1));
        }

        /*
         * Read the header.
         */

        int headerLen = rawValue.byteAt(0);
        int version = rawValue.byteAt(1);
        if (!haveReaderForVersion(version)) {
            throw new InlineMetaException(FaultCodes.GEN_CRITICAL_ERROR,
                                          "No inline metadata reader available for version " + version);
        }

        final InlineMetaReader reader = readerByVersion[version];
        InlineMetaMap map = reader.read(rawValue.getSubvalue(2, headerLen - 2));
        if (log.isDebugEnabled()) {
            log.debug("readDatabaseEntry: map: type=" + map.get("type"));
        }

        /*
         * Exract the data into a Value object.
         */

        Value value = rawValue.getSubvalue(headerLen, rawValue.getLength() - headerLen);
        // FIXME: May be Record should be used instead? new Record(null, value, map);
        return new DatabaseEntry(map, value);
    }

    /**
     * Utility class for returning an InlineMetaMap and Value
     * from <code>readDatabaseEntry(Value)</code>.
     */
    public static class DatabaseEntry {
        public final InlineMetaMap map;
        public final Value value;

        public DatabaseEntry(InlineMetaMap map, Value value) {
            this.map = map;
            this.value = value;
        }
    }
}
TOP

Related Classes of org.apache.xindice.core.meta.inline.InlineMetaService$DatabaseEntry

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.