1 package org.apache.commons.jcs.auxiliary.disk.indexed;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.IOException;
23 import java.util.Random;
24
25 import org.apache.commons.jcs.auxiliary.disk.DiskTestObject;
26 import org.apache.commons.jcs.engine.CacheElement;
27 import org.apache.commons.jcs.engine.behavior.ICacheElement;
28 import org.apache.commons.jcs.utils.serialization.StandardSerializer;
29
30
31
32
33
34
35 public class DiskTestObjectUtil
36 {
37
38
39
40
41
42
43
44
45 public static long totalSize( DiskTestObject[] testObjects, int endPosition )
46 throws IOException
47 {
48 StandardSerializer serializer = new StandardSerializer();
49 long total = 0;
50 for ( int i = 0; i < endPosition; i++ )
51 {
52 int tileSize = serializer.serialize( testObjects[i] ).length + IndexedDisk.HEADER_SIZE_BYTES;
53 total += tileSize;
54 }
55 return total;
56 }
57
58
59
60
61
62
63
64
65
66 public static <K, V> long totalSize( ICacheElement<K, V>[] elements, int endPosition )
67 throws IOException
68 {
69 return totalSize( elements, 0, endPosition );
70 }
71
72
73
74
75
76
77
78
79
80
81 public static <K, V> long totalSize( ICacheElement<K, V>[] elements, int startPosition, int endPosition )
82 throws IOException
83 {
84 StandardSerializer serializer = new StandardSerializer();
85 long total = 0;
86 for ( int i = startPosition; i < endPosition; i++ )
87 {
88 int tileSize = serializer.serialize( elements[i] ).length + IndexedDisk.HEADER_SIZE_BYTES;
89 total += tileSize;
90 }
91 return total;
92 }
93
94
95
96
97
98
99
100
101
102 public static ICacheElement<Integer, DiskTestObject>[] createCacheElementsWithTestObjects( int numToCreate, int bytes, String cacheName )
103 {
104 @SuppressWarnings("unchecked")
105 ICacheElement<Integer, DiskTestObject>[] elements = new ICacheElement[numToCreate];
106 for ( int i = 0; i < numToCreate; i++ )
107 {
108
109 int size = bytes * 1024;
110 DiskTestObject tile = new DiskTestObject( Integer.valueOf( i ), new byte[size] );
111
112 ICacheElement<Integer, DiskTestObject> element = new CacheElement<Integer, DiskTestObject>( cacheName, tile.id, tile );
113 elements[i] = element;
114 }
115 return elements;
116 }
117
118
119
120
121
122
123
124
125 public static ICacheElement<Integer, DiskTestObject>[] createCacheElementsWithTestObjectsOfVariableSizes( int numToCreate, String cacheName )
126 {
127 @SuppressWarnings("unchecked")
128 ICacheElement<Integer, DiskTestObject>[] elements = new ICacheElement[numToCreate];
129 Random random = new Random( 89 );
130 for ( int i = 0; i < numToCreate; i++ )
131 {
132 int bytes = random.nextInt( 20 );
133
134 int size = ( bytes + 4 ) * 1024;
135 DiskTestObject tile = new DiskTestObject( Integer.valueOf( i ), new byte[size] );
136
137 ICacheElement<Integer, DiskTestObject> element = new CacheElement<Integer, DiskTestObject>( cacheName, tile.id, tile );
138 elements[i] = element;
139 }
140 return elements;
141 }
142
143 }