Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*-
   * See the file LICENSE for redistribution information.
   *
   * Copyright (c) 2002, 2013 Oracle and/or its affiliates.  All rights reserved.
   *
   */
  
  package com.sleepycat.persist.impl;
  
 import java.util.Set;
 
A persistence secondary key creator/nullifier. This class always uses rawAccess=true to avoid depending on the presence of the proxy class.

Author(s):
Mark Hayes
 
 public class PersistKeyCreator implements SecondaryKeyCreator,
                                           SecondaryMultiKeyCreator,
                                           ForeignMultiKeyNullifier {
 
     static boolean isManyType(Class cls) {
         return cls.isArray() || Collection.class.isAssignableFrom(cls);
     }
 
     /* See Store.refresh for an explanation of the use of volatile fields. */
     private volatile Catalog catalog;
     private volatile Format priKeyFormat;
     private final String keyName;
     private volatile Format keyFormat;
     private final boolean toMany;

    
Creates a key creator/nullifier for a given entity class and key name.
 
     public PersistKeyCreator(Catalog catalogParam,
                              EntityMetadata entityMeta,
                              String keyClassName,
                              SecondaryKeyMetadata secKeyMeta,
                              boolean rawAccess) {
          = catalogParam;
         try {
             getFormats(entityMetakeyClassNamesecKeyMetarawAccess);
         } catch (RefreshException e) {
             /* Must assign catalog field in constructor. */
              = e.refresh();
             try {
                 getFormats(entityMetakeyClassNamesecKeyMetarawAccess);
             } catch (RefreshException e2) {
                 throw DbCompat.unexpectedException(e2);
             }
         }
          = secKeyMeta.getKeyName();
         Relationship rel = secKeyMeta.getRelationship();
          = (rel == . ||
                   rel == .);
     }
 
     private void getFormats(EntityMetadata entityMeta,
                             String keyClassName,
                             SecondaryKeyMetadata secKeyMeta,
                             boolean rawAccess)
         throws RefreshException {
 
          = PersistEntityBinding.getOrCreateFormat
             (entityMeta.getPrimaryKey().getClassName(), rawAccess);
          = PersistEntityBinding.getOrCreateFormat
             (keyClassNamerawAccess);
         if ( == null) {
             throw new IllegalArgumentException
                 ("Not a key class: " + keyClassName);
         }
         if (.isPrimitive()) {
             throw new IllegalArgumentException
                 ("Use a primitive wrapper class instead of class: " +
                  .getClassName());
         }
     }
 
     public boolean createSecondaryKey(SecondaryDatabase secondary,
                                       DatabaseEntry key,
                                       DatabaseEntry data,
                                       DatabaseEntry result) {
         try {
             return createSecondaryKeyInternal(secondarykeydataresult);
         } catch (RefreshException e) {
            e.refresh();
            try {
                return createSecondaryKeyInternal(secondarykeydata,
                                                  result);
            } catch (RefreshException e2) {
                throw DbCompat.unexpectedException(e2);
            }
        }
    }
    private boolean createSecondaryKeyInternal(SecondaryDatabase secondary,
                                               DatabaseEntry key,
                                               DatabaseEntry data,
                                               DatabaseEntry result)
        throws RefreshException {
        if () {
            throw DbCompat.unexpectedState();
        }
        KeyLocation loc = moveToKey(keydata);
        if (loc != null) {
            RecordOutput output = new RecordOutput
                (true /*rawAccess*/);
            loc.format.copySecKey(loc.inputoutput);
            TupleBase.outputToEntry(outputresult);
            return true;
        } else {
            /* Key field is not present or null. */
            return false;
        }
    }
    public void createSecondaryKeys(SecondaryDatabase secondary,
                                    DatabaseEntry key,
                                    DatabaseEntry data,
                                    Set results) {
        try {
            createSecondaryKeysInternal(secondarykeydataresults);
        } catch (RefreshException e) {
            e.refresh();
            try {
                createSecondaryKeysInternal(secondarykeydataresults);
            } catch (RefreshException e2) {
                throw DbCompat.unexpectedException(e2);
            }
        }
    }
    private void createSecondaryKeysInternal(SecondaryDatabase secondary,
                                             DatabaseEntry key,
                                             DatabaseEntry data,
                                             Set results)
        throws RefreshException {
        if (!) {
            throw DbCompat.unexpectedState();
        }
        KeyLocation loc = moveToKey(keydata);
        if (loc != null) {
            loc.format.copySecMultiKey(loc.inputresults);
        }
        /* Else key field is not present or null. */
    }
    public boolean nullifyForeignKey(SecondaryDatabase secondary,
                                     DatabaseEntry key,
                                     DatabaseEntry data,
                                     DatabaseEntry secKey) {
        try {
            return nullifyForeignKeyInternal(secondarykeydatasecKey);
        } catch (RefreshException e) {
            e.refresh();
            try {
                return nullifyForeignKeyInternal(secondarykeydatasecKey);
            } catch (RefreshException e2) {
                throw DbCompat.unexpectedException(e2);
            }
        }
    }
    private boolean nullifyForeignKeyInternal(SecondaryDatabase secondary,
                                              DatabaseEntry key,
                                              DatabaseEntry data,
                                              DatabaseEntry secKey)
        throws RefreshException {
        /* Deserialize the entity and get its current class format. */
        RawObject entity = (RawObject) PersistEntityBinding.readEntity
            (keynulldatatrue /*rawAccess*/);
        Format entityFormat = (Formatentity.getType();
        /*
         * Set the key to null.  For a TO_MANY key, pass the key object to be
         * removed from the array/collection.
         */
        Object secKeyObject = null;
        if () {
            secKeyObject = PersistKeyBinding.readKey
                (secKey.getData(), secKey.getOffset(),
                 secKey.getSize(), true /*rawAccess*/);
        }
        if (entityFormat.nullifySecKey
            (entitysecKeyObject)) {
            /*
             * Using the current format for the entity, serialize the modified
             * entity back to the data entry.
             */
            PersistEntityBinding.writeEntity
                (entityFormatentitydatatrue /*rawAccess*/);
            return true;
        } else {
            /* Key field is not present or null. */
            return false;
        }
    }

    
Returns the location from which the secondary key field can be copied.
    private KeyLocation moveToKey(DatabaseEntry priKeyDatabaseEntry data)
        throws RefreshException {
        RecordInput input = new RecordInput
            (true /*rawAccess*/priKey.getId(),
             data.getData(), data.getOffset(), data.getSize());
        int formatId = input.readPackedInt();
        Format entityFormat =
            .getFormat(formatIdtrue /*expectStored*/);
        input.registerEntityFormat(entityFormat);
        Format fieldFormat = entityFormat.skipToSecKey(input);
        if (fieldFormat != null) {
            /* Returns null if key field is null. */
            return input.getKeyLocation(fieldFormat);
        } else {
            /* Key field is not present in this class. */
            return null;
        }
    }

    
See Store.refresh.
    void refresh(final PersistCatalog newCatalog) {
         = newCatalog;
    }
New to GrepCode? Check out our FAQ X