View Javadoc
1   package org.apache.commons.jcs3.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.jcs3.auxiliary.disk.DiskTestObject;
26  import org.apache.commons.jcs3.engine.CacheElement;
27  import org.apache.commons.jcs3.engine.behavior.ICacheElement;
28  import org.apache.commons.jcs3.utils.serialization.StandardSerializer;
29  
30  /**
31   * Utility for dealing with test objects.
32   */
33  public class DiskTestObjectUtil
34  {
35      /**
36       * Total from the start to the endPostion.
37       * <p>
38       * @param testObjects
39       * @param endPosition
40       * @return size
41       * @throws IOException
42       */
43      public static long totalSize( final DiskTestObject[] testObjects, final int endPosition )
44          throws IOException
45      {
46          final StandardSerializer serializer = new StandardSerializer();
47          long total = 0;
48          for ( int i = 0; i < endPosition; i++ )
49          {
50              final int tileSize = serializer.serialize( testObjects[i] ).length + IndexedDisk.HEADER_SIZE_BYTES;
51              total += tileSize;
52          }
53          return total;
54      }
55  
56      /**
57       * Total from the start to the endPostion.
58       * <p>
59       * @param elements
60       * @param endPosition
61       * @return size
62       * @throws IOException
63       */
64      public static <K, V> long totalSize( final ICacheElement<K, V>[] elements, final int endPosition )
65          throws IOException
66      {
67          return totalSize( elements, 0, endPosition );
68      }
69  
70      /**
71       * Total from the start to the endPostion.
72       * <p>
73       * @param elements
74       * @param startPosition
75       * @param endPosition
76       * @return size
77       * @throws IOException
78       */
79      public static <K, V> long totalSize( final ICacheElement<K, V>[] elements, final int startPosition, final int endPosition )
80          throws IOException
81      {
82          final StandardSerializer serializer = new StandardSerializer();
83          long total = 0;
84          for ( int i = startPosition; i < endPosition; i++ )
85          {
86              final int tileSize = serializer.serialize( elements[i] ).length + IndexedDisk.HEADER_SIZE_BYTES;
87              total += tileSize;
88          }
89          return total;
90      }
91  
92      /**
93       * Creates an array of ICacheElements with DiskTestObjects with payloads the byte size.
94       * <p>
95       * @param numToCreate
96       * @param bytes
97       * @param cacheName
98       * @return ICacheElement[]
99       */
100     public static ICacheElement<Integer, DiskTestObject>[] createCacheElementsWithTestObjects( final int numToCreate, final int bytes, final String cacheName )
101     {
102         @SuppressWarnings("unchecked")
103         final
104         ICacheElement<Integer, DiskTestObject>[] elements = new ICacheElement[numToCreate];
105         for ( int i = 0; i < numToCreate; i++ )
106         {
107             // 24 KB
108             final int size = bytes * 1024;
109             final DiskTestObject tile = new DiskTestObject( Integer.valueOf( i ), new byte[size] );
110 
111             final ICacheElement<Integer, DiskTestObject> 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<Integer, DiskTestObject>[] createCacheElementsWithTestObjectsOfVariableSizes( final int numToCreate, final String cacheName )
125     {
126         @SuppressWarnings("unchecked")
127         final
128         ICacheElement<Integer, DiskTestObject>[] elements = new ICacheElement[numToCreate];
129         final Random random = new Random( 89 );
130         for ( int i = 0; i < numToCreate; i++ )
131         {
132             final int bytes = random.nextInt( 20 );
133             // 4-24 KB
134             final int size = ( bytes + 4 ) * 1024;
135             final DiskTestObject tile = new DiskTestObject( Integer.valueOf( i ), new byte[size] );
136 
137             final ICacheElement<Integer, DiskTestObject> element = new CacheElement<>( cacheName, tile.id, tile );
138             elements[i] = element;
139         }
140         return elements;
141     }
142 
143 }