コード例 #1
0
ファイル: DiskStoreTest.java プロジェクト: GeomaticM/ehcache
  /** Any disk store with an auto generated random directory should not be able to be loaded. */
  @Test
  public void testCannotLoadPersistentStoreWithAutoDir() throws IOException, InterruptedException {
    // initialise
    String cacheName = "testPersistent";
    Store diskStore = createAutoPersistentDiskStore(cacheName);
    diskStore.removeAll();

    for (int i = 0; i < 100; i++) {
      byte[] data = new byte[1024];
      diskStore.put(new Element("key" + (i + 100), data));
    }
    waitLonger();
    assertEquals(ELEMENT_ON_DISK_SIZE * 100, diskStore.getOnDiskSizeInBytes());
    assertEquals(100, diskStore.getSize());
    manager2.removeCache(cacheName);
    Thread.sleep(1000);

    Cache cache = new Cache(cacheName, 10000, true, false, 5, 1, true, 600);
    manager2.addCache(cache);

    File dataFile = ((DiskPersistentStore) diskStore).getDataFile();
    assertTrue("File exists", dataFile.exists());
    assertEquals(0, dataFile.length());
    assertEquals(0, cache.getSize());
    manager.removeCache(cacheName);
    assertTrue("File exists", dataFile.exists());
    assertEquals(0, dataFile.length());
  }
コード例 #2
0
  /** Test elements can be put in the store */
  protected void putTest() throws IOException {
    Element element;

    assertEquals(0, store.getSize());

    element = new Element("key1", "value1");
    store.put(element);
    assertEquals(1, store.getSize());
    assertEquals("value1", store.get("key1").getObjectValue());

    element = new Element("key2", "value2");
    store.put(element);
    assertEquals(2, store.getSize());
    assertEquals("value2", store.get("key2").getObjectValue());

    for (int i = 0; i < 1999; i++) {
      store.put(new Element("" + i, new Date()));
    }

    assertEquals(4, store.getSize());
    assertEquals(2001, cache.getSize());
    assertEquals(3998, cache.getDiskStoreSize());

    /** Non serializable test class */
    class NonSerializable {
      //
    }

    store.put(new Element(new NonSerializable(), new NonSerializable()));

    assertEquals(4, store.getSize());
    assertEquals(2002, cache.getSize());
    assertEquals(1999, cache.getDiskStoreSize());
    //        assertEquals(1998, cache.getDiskStoreSize());    ???

    // smoke test
    for (int i = 0; i < 2000; i++) {
      store.get("" + i);
    }
  }
コード例 #3
0
 /** Test that the memory store can fit 65k elements in a 64Mb heap. */
 @Test
 public void testMemoryStoreOutOfMemoryLimit() throws Exception {
   Assume.assumeThat(JvmInformation.isJRockit(), is(false));
   LOG.info("Starting out of memory limit test");
   // Set size so the second element overflows to disk.
   cache = manager.getCache("memoryLimitTest");
   if (cache == null) {
     cache = new Cache(new CacheConfiguration("memoryLimitTest", 0));
     manager.addCache(cache);
   }
   for (int i = 0; i < 65000; i++) {
     cache.put(new Element(Integer.valueOf(i), new String(new char[218])));
   }
   assertEquals(65000, cache.getSize());
 }
コード例 #4
0
ファイル: Reader.java プロジェクト: BuiTran/bdm
  private void initCache() {
    // For macs
    File fileDirectory = new File("dict/");
    File cacheFile = new File("dict/cache.txt");
    // For windows
    //		File fileDirectory = new File("dict\\");
    //		File cacheFile = new File(CACHE_FILE_NAME);
    boolean renewCache = false;
    if (!cacheFile.exists()) {
      renewCache = true;
    } else {
      if (fileDirectory.lastModified() > cacheFile.lastModified()) {
        renewCache = true;
      }
    }
    if (renewCache) {
      for (File f : fileDirectory.listFiles()) {
        BufferedReader br = null;
        try {
          FileReader fr = new FileReader(f);
          br = new BufferedReader(fr);
          String line = br.readLine();
          while (line != null) {
            // Split it into multiple words
            String[] listWord = line.split(" |-|_|\\.");
            // Work with each word in the listWord
            for (String word : listWord) {
              boolean addWord = true;
              word = word.replaceAll("[^\\p{L}\\p{Nd}]+", "");
              // Check if it's a single character word
              if (word.length() == 1) {
                addWord = false;
              }
              // Check if it contains number
              if (word.matches(".*\\d.*")) {
                addWord = false;
              }
              word = word.toLowerCase();
              if (addWord) {
                // Create a Word object
                Word w = new Word(word);
                myCache.addWord(w);
              }
            }
            line = br.readLine();
          }
        } catch (IOException ex) {
          System.err.println("Trouble with file :" + ex.getMessage());
        } finally {
          try {
            if (br != null) {
              br.close();
            }
          } catch (Exception ex1) {
            ex1.printStackTrace(); // give up
            System.exit(-1);
          }
        }
      }
      System.out.println("Finished reading all text files");

      // Create a new cache file.
      File newCache = new File(CACHE_FILE_NAME);
      try {
        newCache.createNewFile();
        FileWriter writer = new FileWriter(newCache);
        System.out.println(myCache.getSize());
        writer.write(String.format("%d", myCache.getSize()));
        writer.write("\n");
        for (Word w : myCache.getMyCollection()) {
          writer.write(w.getText());
          writer.write("\n");
        }
        writer.flush();
        writer.close();

      } catch (IOException e) {
        System.out.println("Trouble with writing file");
        e.printStackTrace();
      }
    }
    // If not, we don't have to recreate the cache file. Just read directly from the cache.txt

    BufferedReader br = null;
    try {
      FileReader fr = new FileReader(cacheFile);
      br = new BufferedReader(fr);
      String str = br.readLine();
      // May use later.
      int numberOfWord = Integer.parseInt(str);
      // Now we start reading the words
      str = br.readLine();
      while (str != null) {
        Word w = new Word(str);
        myCache.addWord(w);
        str = br.readLine();
      }
    } catch (IOException ex) {
      System.err.println("Trouble with file :" + ex.getMessage());
    } finally {
      try {
        if (br != null) {
          br.close();
        }
      } catch (Exception ex1) {
        ex1.printStackTrace(); // give up
        System.exit(-1);
      }
    }
  }