KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > localstore > BlobStore


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.localstore;
12
13 import java.io.InputStream JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Set JavaDoc;
16 import org.eclipse.core.filesystem.EFS;
17 import org.eclipse.core.filesystem.IFileStore;
18 import org.eclipse.core.internal.utils.UniversalUniqueIdentifier;
19 import org.eclipse.core.runtime.Assert;
20 import org.eclipse.core.runtime.CoreException;
21
22 /**
23  * Blob store which maps UUIDs to blobs on disk. The UUID is mapped
24  * to a file in the file-system and the blob is the file contents. For scalability,
25  * the blobs are split among 255 directories with the names 00 to FF.
26  */

27 public class BlobStore {
28     protected IFileStore localStore;
29
30     /** Limits the range of directories' names. */
31     protected byte mask;
32
33     //private static short[] randomArray = {213, 231, 37, 85, 211, 29, 161, 175, 187, 3, 147, 246, 170, 30, 202, 183, 242, 47, 254, 189, 25, 248, 193, 2, 119, 133, 125, 12, 76, 213, 219, 79, 69, 133, 202, 80, 150, 190, 157, 190, 80, 190, 219, 150, 169, 117, 95, 10, 77, 214, 233, 70, 5, 188, 44, 91, 165, 149, 177, 93, 17, 112, 4, 41, 230, 148, 188, 107, 213, 31, 52, 60, 111, 246, 226, 121, 129, 197, 144, 248, 92, 133, 96, 116, 104, 67, 74, 144, 185, 141, 96, 34, 182, 90, 36, 217, 28, 205, 107, 52, 201, 14, 8, 1, 27, 216, 60, 35, 251, 194, 7, 156, 32, 5, 145, 29, 96, 61, 110, 145, 50, 56, 235, 239, 170, 138, 17, 211, 56, 98, 101, 126, 27, 57, 211, 144, 206, 207, 179, 111, 160, 50, 243, 69, 106, 118, 155, 159, 28, 57, 11, 175, 43, 173, 96, 181, 99, 169, 171, 156, 246, 243, 30, 198, 251, 81, 77, 92, 160, 235, 215, 187, 23, 71, 58, 247, 127, 56, 118, 132, 79, 188, 42, 188, 158, 121, 255, 65, 154, 118, 172, 217, 4, 47, 105, 204, 135, 27, 43, 90, 9, 31, 59, 115, 193, 28, 55, 101, 9, 117, 211, 112, 61, 55, 23, 235, 51, 104, 123, 138, 76, 148, 115, 119, 81, 54, 39, 46, 149, 191, 79, 16, 222, 69, 219, 136, 148, 181, 77, 250, 101, 223, 140, 194, 141, 44, 195, 217, 31, 223, 207, 149, 245, 115, 243, 183};
34
private static byte[] randomArray = {-43, -25, 37, 85, -45, 29, -95, -81, -69, 3, -109, -10, -86, 30, -54, -73, -14, 47, -2, -67, 25, -8, -63, 2, 119, -123, 125, 12, 76, -43, -37, 79, 69, -123, -54, 80, -106, -66, -99, -66, 80, -66, -37, -106, -87, 117, 95, 10, 77, -42, -23, 70, 5, -68, 44, 91, -91, -107, -79, 93, 17, 112, 4, 41, -26, -108, -68, 107, -43, 31, 52, 60, 111, -10, -30, 121, -127, -59, -112, -8, 92, -123, 96, 116, 104, 67, 74, -112, -71, -115, 96, 34, -74, 90, 36, -39, 28, -51, 107, 52, -55, 14, 8, 1, 27, -40, 60, 35, -5, -62, 7, -100, 32, 5, -111, 29, 96, 61, 110, -111, 50, 56, -21, -17, -86, -118, 17, -45, 56, 98, 101, 126, 27, 57, -45, -112, -50, -49, -77, 111, -96, 50, -13, 69, 106, 118, -101, -97, 28, 57, 11, -81, 43, -83, 96, -75, 99, -87, -85, -100, -10, -13, 30,
35             -58, -5, 81, 77, 92, -96, -21, -41, -69, 23, 71, 58, -9, 127, 56, 118, -124, 79, -68, 42, -68, -98, 121, -1, 65, -102, 118, -84, -39, 4, 47, 105, -52, -121, 27, 43, 90, 9, 31, 59, 115, -63, 28, 55, 101, 9, 117, -45, 112, 61, 55, 23, -21, 51, 104, 123, -118, 76, -108, 115, 119, 81, 54, 39, 46, -107, -65, 79, 16, -34, 69, -37, -120, -108, -75, 77, -6, 101, -33, -116, -62, -115, 44, -61, -39, 31, -33, -49, -107, -11, 115, -13, -73,};
36
37     /**
38      * The limit is the maximum number of directories managed by this store.
39      * This number must be power of 2 and do not exceed 256. The location
40      * should be an existing valid directory.
41      */

42     public BlobStore(IFileStore store, int limit) {
43         Assert.isNotNull(store);
44         localStore = store;
45         Assert.isTrue(localStore.fetchInfo().isDirectory());
46         Assert.isTrue(limit == 256 || limit == 128 || limit == 64 || limit == 32 || limit == 16 || limit == 8 || limit == 4 || limit == 2 || limit == 1);
47         mask = (byte) (limit - 1);
48     }
49
50     public UniversalUniqueIdentifier addBlob(IFileStore target, boolean moveContents) throws CoreException {
51         UniversalUniqueIdentifier uuid = new UniversalUniqueIdentifier();
52         folderFor(uuid).mkdir(EFS.NONE, null);
53         IFileStore destination = fileFor(uuid);
54         if (moveContents)
55             target.move(destination, EFS.NONE, null);
56         else
57             target.copy(destination, EFS.NONE, null);
58         return uuid;
59     }
60
61     /* (non-Javadoc)
62      * @see UniversalUniqueIdentifier#appendByteString(StringBuffer, byte)
63      */

64     private void appendByteString(StringBuffer JavaDoc buffer, byte value) {
65         String JavaDoc hexString;
66         if (value < 0)
67             hexString = Integer.toHexString(256 + value);
68         else
69             hexString = Integer.toHexString(value);
70         if (hexString.length() == 1)
71             buffer.append("0"); //$NON-NLS-1$
72
buffer.append(hexString);
73     }
74
75     /* (non-Javadoc)
76      * Converts an array of bytes into a String.
77      *
78      * @see UniversalUniqueIdentifier#toString()
79      */

80     private String JavaDoc bytesToHexString(byte[] b) {
81         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
82         for (int i = 0; i < b.length; i++)
83             appendByteString(buffer, b[i]);
84         return buffer.toString();
85     }
86
87     /**
88      * Deletes a blobFile.
89      */

90     public void deleteBlob(UniversalUniqueIdentifier uuid) {
91         Assert.isNotNull(uuid);
92         try {
93             fileFor(uuid).delete(EFS.NONE, null);
94         } catch (CoreException e) {
95             //ignore
96
}
97     }
98
99     /**
100      * Delete all of the blobs in the given set.
101      */

102     public void deleteBlobs(Set JavaDoc set) {
103         for (Iterator JavaDoc i = set.iterator(); i.hasNext();)
104             deleteBlob((UniversalUniqueIdentifier) i.next());
105     }
106
107     public IFileStore fileFor(UniversalUniqueIdentifier uuid) {
108         IFileStore root = folderFor(uuid);
109         return root.getChild(bytesToHexString(uuid.toBytes()));
110     }
111
112     /**
113      * Find out the name of the directory that fits better to this UUID.
114      */

115     public IFileStore folderFor(UniversalUniqueIdentifier uuid) {
116         byte hash = hashUUIDbytes(uuid);
117         hash &= mask; // limit the range of the directory
118
String JavaDoc dirName = Integer.toHexString(hash + (128 & mask)); // +(128 & mask) makes sure 00h is the lower value
119
return localStore.getChild(dirName);
120     }
121
122     public InputStream JavaDoc getBlob(UniversalUniqueIdentifier uuid) throws CoreException {
123         IFileStore blobFile = fileFor(uuid);
124         return blobFile.openInputStream(EFS.NONE, null);
125     }
126
127     /**
128      * Converts a byte array into a byte hash representation. It is used to
129      * get a directory name.
130      */

131     protected byte hashUUIDbytes(UniversalUniqueIdentifier uuid) {
132         byte[] bytes = uuid.toBytes();
133         byte hash = 0;
134         for (int i = 0; i < bytes.length; i++)
135             hash ^= randomArray[bytes[i] + 128]; // +128 makes sure the index is >0
136
return hash;
137     }
138 }
139
Popular Tags