1   package org.apache.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.jcs.auxiliary.disk.DiskTestObject;
26  import org.apache.jcs.engine.CacheElement;
27  import org.apache.jcs.engine.behavior.ICacheElement;
28  import org.apache.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.RECORD_HEADER;
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 long totalSize( ICacheElement[] 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 long totalSize( ICacheElement[] 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.RECORD_HEADER;
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[] createCacheElementsWithTestObjects( int numToCreate, int bytes, String cacheName )
103     {
104         ICacheElement[] elements = new ICacheElement[numToCreate];
105         for ( int i = 0; i < numToCreate; i++ )
106         {
107             // 24 KB
108             int size = bytes * 1024;
109             DiskTestObject tile = new DiskTestObject( new Integer( i ), new byte[size] );
110 
111             ICacheElement element = new CacheElement( cacheName, tile.id, tile );
112             elements[i] = element;
113         }
114         return elements;
115     }
116 
117     /***
118      * Creates an array of ICacheElements with DiskTestObjects with payloads the byte size.
119      * <p>
120      * @param numToCreate
121      * @param cacheName
122      * @return ICacheElement[]
123      */
124     public static ICacheElement[] createCacheElementsWithTestObjectsOfVariableSizes( int numToCreate, String cacheName )
125     {
126         ICacheElement[] elements = new ICacheElement[numToCreate];
127         Random random = new Random( 89 );
128         for ( int i = 0; i < numToCreate; i++ )
129         {
130             int bytes = random.nextInt( 20 );
131             // 4-24 KB
132             int size = ( bytes + 4 ) * 1024;
133             DiskTestObject tile = new DiskTestObject( new Integer( i ), new byte[size] );
134 
135             ICacheElement element = new CacheElement( cacheName, tile.id, tile );
136             elements[i] = element;
137         }
138         return elements;
139     }
140 
141 }