View Javadoc
1   package org.apache.commons.jcs3.auxiliary.disk.indexed;
2   
3   import org.apache.commons.jcs3.engine.CacheElement;
4   import org.apache.commons.jcs3.engine.ElementAttributes;
5   import org.apache.commons.jcs3.engine.behavior.ICacheElement;
6   import org.apache.commons.jcs3.engine.behavior.IElementAttributes;
7   
8   /*
9    * Licensed to the Apache Software Foundation (ASF) under one
10   * or more contributor license agreements.  See the NOTICE file
11   * distributed with this work for additional information
12   * regarding copyright ownership.  The ASF licenses this file
13   * to you under the Apache License, Version 2.0 (the
14   * "License"); you may not use this file except in compliance
15   * with the License.  You may obtain a copy of the License at
16   *
17   *   http://www.apache.org/licenses/LICENSE-2.0
18   *
19   * Unless required by applicable law or agreed to in writing,
20   * software distributed under the License is distributed on an
21   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
22   * KIND, either express or implied.  See the License for the
23   * specific language governing permissions and limitations
24   * under the License.
25   */
26  
27  import junit.framework.TestCase;
28  
29  /**
30   * Test store and load keys.
31   */
32  public class IndexedDiskCacheKeyStoreUnitTest
33      extends TestCase
34  {
35  
36      /**
37       * Add some keys, store them, load them from disk, then check to see that we
38       * can get the items.
39       *
40       * @throws Exception
41       *
42       */
43      public void testStoreKeys()
44          throws Exception
45      {
46          final IndexedDiskCacheAttributes cattr = new IndexedDiskCacheAttributes();
47          cattr.setCacheName( "testStoreKeys" );
48          cattr.setMaxKeySize( 100 );
49          cattr.setDiskPath( "target/test-sandbox/KeyStoreUnitTest" );
50          final IndexedDiskCache<String, String> disk = new IndexedDiskCache<>( cattr );
51  
52          disk.processRemoveAll();
53  
54          final int cnt = 25;
55          for ( int i = 0; i < cnt; i++ )
56          {
57              final IElementAttributes eAttr = new ElementAttributes();
58              eAttr.setIsSpool( true );
59              final ICacheElement<String, String> element = new CacheElement<>( cattr.getCacheName(), "key:" + i, "data:" + i );
60              element.setElementAttributes( eAttr );
61              disk.processUpdate( element );
62          }
63  
64          for ( int i = 0; i < cnt; i++ )
65          {
66              final ICacheElement<String, String> element = disk.processGet( "key:" + i );
67              assertNotNull( "presave, Should have received an element.", element );
68              assertEquals( "presave, element is wrong.", "data:" + i, element.getVal() );
69          }
70  
71          disk.saveKeys();
72  
73          disk.loadKeys();
74  
75          assertEquals( "The disk is the wrong size.", cnt, disk.getSize() );
76  
77          for ( int i = 0; i < cnt; i++ )
78          {
79              final ICacheElement<String, String> element = disk.processGet( "key:" + i );
80              assertNotNull( "postsave, Should have received an element.", element );
81              assertEquals( "postsave, element is wrong.", "data:" + i, element.getVal() );
82          }
83  
84          disk.dump();
85  
86      }
87  
88  
89      /**
90       * Add some elements, remove 1, call optimize, verify that the removed isn't present.
91       *
92       * We should also compare the data file sizes. . . .
93       *
94       * @throws Exception
95       *
96       */
97      public void testOptiimize()
98          throws Exception
99      {
100         final IndexedDiskCacheAttributes cattr = new IndexedDiskCacheAttributes();
101         cattr.setCacheName( "testOptimize" );
102         cattr.setMaxKeySize( 100 );
103         cattr.setDiskPath( "target/test-sandbox/KeyStoreUnitTest" );
104         final IndexedDiskCache<String, String> disk = new IndexedDiskCache<>( cattr );
105 
106         disk.processRemoveAll();
107 
108         final int cnt = 25;
109         for ( int i = 0; i < cnt; i++ )
110         {
111             final IElementAttributes eAttr = new ElementAttributes();
112             eAttr.setIsSpool( true );
113             final ICacheElement<String, String> element = new CacheElement<>( cattr.getCacheName(), "key:" + i, "data:" + i );
114             element.setElementAttributes( eAttr );
115             disk.processUpdate( element );
116         }
117 
118         final long preAddRemoveSize = disk.getDataFileSize();
119 
120         final IElementAttributes eAttr = new ElementAttributes();
121         eAttr.setIsSpool( true );
122         final ICacheElement<String, String> elementSetup = new CacheElement<>( cattr.getCacheName(), "key:" + "A", "data:" + "A" );
123         elementSetup.setElementAttributes( eAttr );
124         disk.processUpdate( elementSetup );
125 
126         final ICacheElement<String, String> elementRet = disk.processGet( "key:" + "A" );
127         assertNotNull( "postsave, Should have received an element.", elementRet );
128         assertEquals( "postsave, element is wrong.", "data:" + "A", elementRet.getVal() );
129 
130         disk.remove( "key:" + "A" );
131 
132         final long preSize = disk.getDataFileSize();
133         // synchronous versoin
134         disk.optimizeFile(); //deoptimizeRealTime();
135         final long postSize = disk.getDataFileSize();
136 
137         assertTrue( "Should be smaller. postsize="+postSize+" preSize="+preSize, postSize < preSize );
138         assertEquals( "Should be the same size after optimization as before add and remove.", preAddRemoveSize, postSize );
139 
140         for ( int i = 0; i < cnt; i++ )
141         {
142             final ICacheElement<String, String> element = disk.processGet( "key:" + i );
143             assertNotNull( "postsave, Should have received an element.", element );
144             assertEquals( "postsave, element is wrong.", "data:" + i, element.getVal() );
145         }
146     }
147 }