KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > core > storage > gammaStore > ContainerLocationLoc


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Core License version 1 published by ozone-db.org.
3
//
4
// Copyright (C) 2003-@year@, Leo Mekenkamp. All rights reserved.
5
//
6
// $Id: ContainerLocationLoc.java,v 1.3 2004/03/21 21:05:51 leomekenkamp Exp $
7

8 package org.ozoneDB.core.storage.gammaStore;
9
10 import java.io.BufferedOutputStream JavaDoc;
11 import java.io.FileOutputStream JavaDoc;
12 import java.io.ObjectOutputStream JavaDoc;
13
14 /**
15  * @author <a HREF="mailto:leoATmekenkampD0Tcom">Leo Mekenkamp (mind the anti sp@m)</a>
16  * @version $Id: ContainerLocationLoc.java,v 1.3 2004/03/21 21:05:51 leomekenkamp Exp $
17  */

18 public class ContainerLocationLoc extends Loc {
19
20     protected int[] dataFileIds;
21     protected int[] positions;
22     
23 // Encoding both clusterId and position into a long and storing it into one
24
// long[] instead of two int[] yielded no significant performance increase.
25
// Maybe there is something I missed, so the code will remain in the source, in
26
// the form of comments.
27
// protected long[] both;
28

29     public ContainerLocationLoc(int capacity, float bufferFactor) {
30         super(capacity, bufferFactor);
31         init();
32     }
33
34     public ContainerLocationLoc(int capacity, int slack) {
35         super(capacity, slack);
36         init();
37     }
38
39     private void init() {
40 //
41
dataFileIds = new int[keys.length];
42         positions = new int[keys.length];
43 // both = new long[keys.length];
44
}
45     
46     /**
47      * Note that we do not store <code>containerLocation</code> as an
48      * object, we simply take its values and copy it into internal arrays.
49      * This saves a _lot_ of execution time during (de)serialization.
50      */

51     public void putContainerLocation(int pos, ContainerLocation containerLocation) {
52 //
53
dataFileIds[pos] = containerLocation.getDataFileId();
54         positions[pos] = containerLocation.getPosition();
55 // both[pos] = toLong(containerLocation);
56
}
57     
58     protected void move(int from, int to) {
59         super.move(from, to);
60 //
61
dataFileIds[to] = dataFileIds[from];
62         positions[to] = positions[from];
63 // both[to] = both[from];
64

65     }
66     
67 // /**
68
// * Convenience method to make it easy to reuse <code>ContainerLocation</code>
69
// * instances.
70
// */
71
// public void fillContainerLocation(int pos, ContainerLocation containerLocation) {
72
////
73
// containerLocation.getDataFileId() = dataFileIds[pos];
74
// containerLocation.getPosition() = positions[pos];
75
//// fromLong(both[pos], containerLocation);2
76
// }
77

78     public ContainerLocation getContainerLocation(int pos) {
79         ContainerLocation result = new ContainerLocation(dataFileIds[pos], positions[pos]);
80         return result;
81     }
82     
83 // private static long toLong(ContainerLocation containerLocation) {
84
// long result = (long) containerLocation.getDataFileId() << 32;
85
// result |= containerLocation.getPosition() & 0xFFFFFFFFL;
86
// return result;
87
// }
88
//
89
// private static void fromLong(long encoded, ContainerLocation containerLocation) {
90
// containerLocation.getDataFileId() = (int) (encoded >> 32);
91
// containerLocation.getPosition() = (int) encoded;
92
// }
93

94     public String JavaDoc toString() {
95         StringBuffer JavaDoc result = new StringBuffer JavaDoc(super.toString());
96         result.append(" nodes [");
97         
98         // comment for-loop out when using a long[]
99
for (int i = 0; i < dataFileIds.length; i++) {
100             result.append(dataFileIds[i]);
101             if (!isInUse(i)) {
102                 result.append("D");
103             }
104             if (i < dataFileIds.length - 1) {
105                 result.append(", ");
106             } else {
107                 result.append("]");
108             }
109         }
110         
111         return result.toString();
112     }
113     
114 // DEBUG and TEST code following only
115

116     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
117         long start = System.currentTimeMillis();
118         for (int test = 0; test < 1000; test++) {
119             int SIZE = 100;
120             ContainerLocationLoc containerLocationLoc = new ContainerLocationLoc(SIZE, 0.1F);
121             for(int i = 0; i < SIZE; i++) {
122                 ContainerLocation loc = new ContainerLocation((int) (Math.random() * Integer.MAX_VALUE), (int) (Math.random() * Integer.MAX_VALUE));
123                 int pos = containerLocationLoc.putKey(i);
124                 containerLocationLoc.putContainerLocation(pos, loc);
125             }
126             ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc(new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc("/home/leo/delme." + test), 8192));
127             out.writeObject(containerLocationLoc);
128             out.close();
129         }
130         System.out.println("that took " + (System.currentTimeMillis() - start) + " msec.");
131     }
132         
133 }
134
Popular Tags