View Javadoc
1   package org.apache.commons.jcs.auxiliary.disk.indexed;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Utility for dealing with test objects.
32   * <p>
33   * @author Aaron Smuts
34   */
35  public class DiskTestObjectUtil
36  {
37      /**
38       * Total from the start to the endPostion.
39       * <p>
40       * @param testObjects
41       * @param endPosition
42       * @return size
43       * @throws IOException
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       * Total from the start to the endPostion.
60       * <p>
61       * @param elements
62       * @param endPosition
63       * @return size
64       * @throws IOException
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       * Total from the start to the endPostion.
74       * <p>
75       * @param elements
76       * @param startPosition
77       * @param endPosition
78       * @return size
79       * @throws IOException
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       * Creates an array of ICacheElements with DiskTestObjects with payloads the byte size.
96       * <p>
97       * @param numToCreate
98       * @param bytes
99       * @param cacheName
100      * @return ICacheElement[]
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             // 24 KB
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      * Creates an array of ICacheElements with DiskTestObjects with payloads the byte size.
120      * <p>
121      * @param numToCreate
122      * @param cacheName
123      * @return ICacheElement[]
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             // 4-24 KB
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 }