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 junit.framework.TestCase;
23  import org.apache.commons.jcs.engine.CacheElement;
24  import org.apache.commons.jcs.engine.ElementAttributes;
25  import org.apache.commons.jcs.engine.behavior.ICacheElement;
26  import org.apache.commons.jcs.engine.behavior.IElementAttributes;
27  
28  /**
29   * Test store and load keys.
30   *
31   * @author Aaron Smuts
32   *
33   */
34  public class IndexedDiskCacheKeyStoreUnitTest
35      extends TestCase
36  {
37  
38      /**
39       * Add some keys, store them, load them from disk, then check to see that we
40       * can get the items.
41       *
42       * @throws Exception
43       *
44       */
45      public void testStoreKeys()
46          throws Exception
47      {
48          IndexedDiskCacheAttributes cattr = new IndexedDiskCacheAttributes();
49          cattr.setCacheName( "testStoreKeys" );
50          cattr.setMaxKeySize( 100 );
51          cattr.setDiskPath( "target/test-sandbox/KeyStoreUnitTest" );
52          IndexedDiskCache<String, String> disk = new IndexedDiskCache<String, String>( cattr );
53  
54          disk.processRemoveAll();
55  
56          int cnt = 25;
57          for ( int i = 0; i < cnt; i++ )
58          {
59              IElementAttributes eAttr = new ElementAttributes();
60              eAttr.setIsSpool( true );
61              ICacheElement<String, String> element = new CacheElement<String, String>( cattr.getCacheName(), "key:" + i, "data:" + i );
62              element.setElementAttributes( eAttr );
63              disk.processUpdate( element );
64          }
65  
66          for ( int i = 0; i < cnt; i++ )
67          {
68              ICacheElement<String, String> element = disk.processGet( "key:" + i );
69              assertNotNull( "presave, Should have received an element.", element );
70              assertEquals( "presave, element is wrong.", "data:" + i, element.getVal() );
71          }
72  
73          disk.saveKeys();
74  
75          disk.loadKeys();
76  
77          assertEquals( "The disk is the wrong size.", cnt, disk.getSize() );
78  
79          for ( int i = 0; i < cnt; i++ )
80          {
81              ICacheElement<String, String> element = disk.processGet( "key:" + i );
82              assertNotNull( "postsave, Should have received an element.", element );
83              assertEquals( "postsave, element is wrong.", "data:" + i, element.getVal() );
84          }
85  
86          disk.dump();
87  
88      }
89  
90  
91      /**
92       * Add some elements, remove 1, call optimize, verify that the removed isn't present.
93       *
94       * We should also compare the data file sizes. . . .
95       *
96       * @throws Exception
97       *
98       */
99      public void testOptiimize()
100         throws Exception
101     {
102         IndexedDiskCacheAttributes cattr = new IndexedDiskCacheAttributes();
103         cattr.setCacheName( "testOptimize" );
104         cattr.setMaxKeySize( 100 );
105         cattr.setDiskPath( "target/test-sandbox/KeyStoreUnitTest" );
106         IndexedDiskCache<String, String> disk = new IndexedDiskCache<String, String>( cattr );
107 
108         disk.processRemoveAll();
109 
110         int cnt = 25;
111         for ( int i = 0; i < cnt; i++ )
112         {
113             IElementAttributes eAttr = new ElementAttributes();
114             eAttr.setIsSpool( true );
115             ICacheElement<String, String> element = new CacheElement<String, String>( cattr.getCacheName(), "key:" + i, "data:" + i );
116             element.setElementAttributes( eAttr );
117             disk.processUpdate( element );
118         }
119 
120         long preAddRemoveSize = disk.getDataFileSize();
121 
122         IElementAttributes eAttr = new ElementAttributes();
123         eAttr.setIsSpool( true );
124         ICacheElement<String, String> elementSetup = new CacheElement<String, String>( cattr.getCacheName(), "key:" + "A", "data:" + "A" );
125         elementSetup.setElementAttributes( eAttr );
126         disk.processUpdate( elementSetup );
127 
128         ICacheElement<String, String> elementRet = disk.processGet( "key:" + "A" );
129         assertNotNull( "postsave, Should have received an element.", elementRet );
130         assertEquals( "postsave, element is wrong.", "data:" + "A", elementRet.getVal() );
131 
132         disk.remove( "key:" + "A" );
133 
134         long preSize = disk.getDataFileSize();
135         // synchronous versoin
136         disk.optimizeFile(); //deoptimizeRealTime();
137         long postSize = disk.getDataFileSize();
138 
139         assertTrue( "Should be smaller. postsize="+postSize+" preSize="+preSize, postSize < preSize );
140         assertEquals( "Should be the same size after optimization as before add and remove.", preAddRemoveSize, postSize );
141 
142         for ( int i = 0; i < cnt; i++ )
143         {
144             ICacheElement<String, String> element = disk.processGet( "key:" + i );
145             assertNotNull( "postsave, Should have received an element.", element );
146             assertEquals( "postsave, element is wrong.", "data:" + i, element.getVal() );
147         }
148     }
149 }