Primary index of btree storage improved. Btree storage now uses special PrimaryIndex instead of SinglevaluedBtree index. This PrimaryIndex is much smaller and faster. BLD200504171800
authorthurka@netbeans.org
Fri, 15 Apr 2005 14:15:51 +0000
changeset 1679aa57fca12c0f
parent 1678 c534b798b56f
child 1680 9e528a97e1d3
Primary index of btree storage improved. Btree storage now uses special PrimaryIndex instead of SinglevaluedBtree index. This PrimaryIndex is much smaller and faster.
mdr/src/org/netbeans/mdr/persistence/btreeimpl/btreestorage/BtreeDataFile.java
mdr/src/org/netbeans/mdr/persistence/btreeimpl/btreestorage/BtreeDatabase.java
mdr/src/org/netbeans/mdr/persistence/btreeimpl/btreestorage/PrimaryIndex.java
     1.1 --- a/mdr/src/org/netbeans/mdr/persistence/btreeimpl/btreestorage/BtreeDataFile.java	Fri Apr 15 12:34:13 2005 +0000
     1.2 +++ b/mdr/src/org/netbeans/mdr/persistence/btreeimpl/btreestorage/BtreeDataFile.java	Fri Apr 15 14:15:51 2005 +0000
     1.3 @@ -72,8 +72,8 @@
     1.4      file cache page must be an even number of chunks.  Value is 32 */
     1.5      static final int BTREE_CHUNK_SIZE = 32;    
     1.6  
     1.7 -    /** the current (and, so far, only) version of btree. Value is 100 */
     1.8 -    static final int BTREE_VERSION = 100;
     1.9 +    /** the current version of btree. Inital value was 100 */
    1.10 +    static final int BTREE_VERSION = 101;
    1.11  
    1.12      /** the size of the data file header in bytes.  This must be an 
    1.13      even number of chunks. Value is 2048 */
    1.14 @@ -197,7 +197,7 @@
    1.15              UUID = storage.storageUUID;
    1.16          else
    1.17              UUID = new UUID().toString();
    1.18 -        counter = BtreeFactory.FIRST_EXTERNAL_ID + (UUID.hashCode() % 2048) + 2048; // adding a random increment to fix #46323
    1.19 +        counter = initialMofIdConter();
    1.20          fileHeader = hdr;
    1.21          deletedExtents = new int[MAX_CHUNKS_IN_EXTENT];
    1.22  
    1.23 @@ -648,6 +648,11 @@
    1.24          dirty = true;
    1.25      }
    1.26      
    1.27 +    /** computes first serial number of extenal MOFID */
    1.28 +    long initialMofIdConter() {
    1.29 +        return BtreeFactory.FIRST_EXTERNAL_ID + (UUID.hashCode() % 2048) + 2048; // adding a random increment to fix #46323
    1.30 +    }
    1.31 +    
    1.32      /** dump basic header info */
    1.33      public static final int DUMP_HEADER = 1;
    1.34  
     2.1 --- a/mdr/src/org/netbeans/mdr/persistence/btreeimpl/btreestorage/BtreeDatabase.java	Fri Apr 15 12:34:13 2005 +0000
     2.2 +++ b/mdr/src/org/netbeans/mdr/persistence/btreeimpl/btreestorage/BtreeDatabase.java	Fri Apr 15 14:15:51 2005 +0000
     2.3 @@ -274,12 +274,9 @@
     2.4          try {
     2.5              dataFile = new BtreeDataFile(this.myStorage, fileCache, 0);
     2.6              myStorage.gen = dataFile;
     2.7 -            BtreeFileSource source = 
     2.8 -                new BtreeFileSource(1, fileCache, PAGE_SIZE, 
     2.9 -                                    isNew || rebuildIndex, dataFile, myStorage);
    2.10 -            indexFile = new SinglevaluedBtree(repositoryName, 
    2.11 -                                Storage.EntryType.MOFID, Storage.EntryType.INT, 
    2.12 -                                source);        
    2.13 +
    2.14 +            indexFile = new PrimaryIndex(repositoryName, 1, fileCache, dataFile.initialMofIdConter(),
    2.15 +                                Storage.EntryType.MOFID, Storage.EntryType.INT);        
    2.16  
    2.17              if (rebuildIndex) {
    2.18                  rebuildIndexFile();
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/mdr/src/org/netbeans/mdr/persistence/btreeimpl/btreestorage/PrimaryIndex.java	Fri Apr 15 14:15:51 2005 +0000
     3.3 @@ -0,0 +1,177 @@
     3.4 +/*
     3.5 + *                 Sun Public License Notice
     3.6 + *
     3.7 + * The contents of this file are subject to the Sun Public License
     3.8 + * Version 1.0 (the "License"). You may not use this file except in
     3.9 + * compliance with the License. A copy of the License is available at
    3.10 + * http://www.sun.com/
    3.11 + *
    3.12 + * The Original Code is NetBeans. The Initial Developer of the Original
    3.13 + * Code is Sun Microsystems, Inc. Portions Copyright 1997-2001 Sun
    3.14 + * Microsystems, Inc. All Rights Reserved.
    3.15 + */
    3.16 +
    3.17 +package org.netbeans.mdr.persistence.btreeimpl.btreestorage;
    3.18 +
    3.19 +import java.text.MessageFormat;
    3.20 +import java.util.Collection;
    3.21 +import java.util.Set;
    3.22 +import org.netbeans.mdr.persistence.MOFID;
    3.23 +import org.netbeans.mdr.persistence.SinglevaluedIndex;
    3.24 +import org.netbeans.mdr.persistence.Storage;
    3.25 +import org.netbeans.mdr.persistence.StorageBadRequestException;
    3.26 +import org.netbeans.mdr.persistence.StorageException;
    3.27 +
    3.28 +
    3.29 +/**
    3.30 + *
    3.31 + * @author Tomas Hurka
    3.32 + */
    3.33 +public class PrimaryIndex implements SinglevaluedIndex {
    3.34 +
    3.35 +    private FileCache	fileCache;	// source of pages from file
    3.36 +    private int		fileId;		// file where our pages are stored,
    3.37 +    					// for making FileCache requests    
    3.38 +    private Storage.EntryType	keyType;
    3.39 +    private Storage.EntryType	dataType;
    3.40 +    private String 		indexName;
    3.41 +    private int                 valuesInPage;
    3.42 +    private final long          firstMofIdSerial;
    3.43 +    private static final int    NO_VALUE=0;
    3.44 +    private static final int    sizeof_int=4;
    3.45 +    private static final int    reservedValues=FileHeader.HEADER_SIZE/sizeof_int;
    3.46 +    
    3.47 +    private class ValInfo {
    3.48 +        private int index;
    3.49 +        private CachedPage page;
    3.50 +        
    3.51 +        ValInfo(Object mofId) throws StorageException {
    3.52 +            MOFID mid=(MOFID)mofId;
    3.53 +            long serial=mid.getSerialNumber();
    3.54 +            
    3.55 +            if (serial>=BtreeFactory.FIRST_EXTERNAL_ID) {
    3.56 +                assert serial>=firstMofIdSerial;
    3.57 +                serial-=firstMofIdSerial-BtreeFactory.FIRST_EXTERNAL_ID;
    3.58 +            }
    3.59 +            serial+=reservedValues;
    3.60 +            long pageNum=serial/valuesInPage;
    3.61 +            page=fileCache.getPage(fileId, (int)pageNum);
    3.62 +            
    3.63 +            index=(int)(serial%valuesInPage)*sizeof_int;
    3.64 +        }
    3.65 +    }
    3.66 +    
    3.67 +    PrimaryIndex(String name, int id, FileCache cache, long firstSerial, Storage.EntryType key, Storage.EntryType data) {
    3.68 +        indexName=name;
    3.69 +        fileId=id;
    3.70 +        fileCache=cache;
    3.71 +        firstMofIdSerial=firstSerial;
    3.72 +        keyType=key;
    3.73 +        dataType=data;
    3.74 +        valuesInPage=cache.getPageSize()/sizeof_int; 
    3.75 +        
    3.76 +    }
    3.77 +
    3.78 +    public void add(Object key, Object value) throws StorageException {
    3.79 +        put(key, value);
    3.80 +    }
    3.81 +
    3.82 +    public Object get(Object key) throws StorageException, StorageBadRequestException {
    3.83 +        Object result = getIfExists(key);
    3.84 +        if (result == null) {
    3.85 +            throw new StorageBadRequestException (
    3.86 +                MessageFormat.format("Key {0} not found in index",
    3.87 +                    new Object[] {key}));
    3.88 +        }
    3.89 +        return result;
    3.90 +    }
    3.91 +
    3.92 +    public Object getIfExists(Object key) throws StorageException {
    3.93 +        ValInfo info=new ValInfo(key);
    3.94 +        int value = Converter.readInt(info.page.contents,info.index);
    3.95 +        
    3.96 +        fileCache.unpin(info.page);
    3.97 +        if (value==NO_VALUE)
    3.98 +            return null;
    3.99 +        return new Integer(value);
   3.100 +   }
   3.101 +
   3.102 +    public Storage.EntryType getKeyType() throws StorageException {
   3.103 +        return keyType;
   3.104 +    }
   3.105 +
   3.106 +    public String getName() throws StorageException {
   3.107 +        return indexName;
   3.108 +    }
   3.109 +
   3.110 +    public Object getObject(Object key, SinglevaluedIndex repos) throws StorageException {
   3.111 +        Object result = getObjectIfExists(key, repos);
   3.112 +        if (result == null) {
   3.113 +            throw new StorageBadRequestException (
   3.114 +                MessageFormat.format("Key {0} not found in index",
   3.115 +                    new Object[] {key}));
   3.116 +        }
   3.117 +        return result;
   3.118 +    }
   3.119 +
   3.120 +    public Object getObjectIfExists(Object key, SinglevaluedIndex repos) throws StorageException {
   3.121 +        throw new UnsupportedOperationException();
   3.122 +    }
   3.123 +
   3.124 +    public Storage.EntryType getValueType() throws StorageException {
   3.125 +        return dataType;
   3.126 +    }
   3.127 +
   3.128 +    public Set keySet() throws StorageException {
   3.129 +        throw new UnsupportedOperationException();
   3.130 +    }
   3.131 +
   3.132 +    public boolean put(Object key, Object value) throws StorageException {
   3.133 +        ValInfo info=new ValInfo(key);
   3.134 +        int oldVal = Converter.readInt(info.page.contents,info.index);
   3.135 +        int val=((Integer)value).intValue();
   3.136 +        
   3.137 +        if (oldVal!=val) {
   3.138 +            fileCache.setWritable(info.page);
   3.139 +            Converter.writeInt(info.page.contents,info.index, val);
   3.140 +        }
   3.141 +        fileCache.unpin(info.page);
   3.142 +        return oldVal!=NO_VALUE;
   3.143 +    }
   3.144 +
   3.145 +    public Collection queryByKeyPrefix(Object prefix, SinglevaluedIndex repos) throws StorageException {
   3.146 +        throw new UnsupportedOperationException();
   3.147 +    }
   3.148 +
   3.149 +    public boolean remove(Object key) throws StorageException {
   3.150 +        ValInfo info=new ValInfo(key);
   3.151 +        int val=Converter.readInt(info.page.contents,info.index);
   3.152 +        
   3.153 +        if (val!=NO_VALUE) {
   3.154 +            fileCache.setWritable(info.page);
   3.155 +            Converter.writeInt(info.page.contents,info.index, NO_VALUE);
   3.156 +        }
   3.157 +        fileCache.unpin(info.page);
   3.158 +        return val!=NO_VALUE;
   3.159 +    }
   3.160 +
   3.161 +    public void replace(Object key, Object value) throws StorageException, StorageBadRequestException {
   3.162 +        ValInfo info=new ValInfo(key);
   3.163 +        int oldVal = Converter.readInt(info.page.contents,info.index);
   3.164 +        
   3.165 +        if (oldVal==NO_VALUE) {
   3.166 +            throw new StorageBadRequestException (
   3.167 +                MessageFormat.format("Key {0} not found in index",
   3.168 +                    new Object[] {key}));
   3.169 +        }            
   3.170 +        int val=((Integer)value).intValue();
   3.171 +        fileCache.setWritable(info.page);
   3.172 +        Converter.writeInt(info.page.contents,info.index, val);
   3.173 +        fileCache.unpin(info.page);
   3.174 +    }
   3.175 +
   3.176 +    public Collection values() throws StorageException {
   3.177 +        throw new UnsupportedOperationException();
   3.178 +    }
   3.179 +    
   3.180 +}