[infinispan-commits] Infinispan SVN: r1079 - in demos/directory: src and 6 other directories.

infinispan-commits at lists.jboss.org infinispan-commits at lists.jboss.org
Fri Oct 30 10:03:35 EDT 2009


Author: galder.zamarreno at jboss.com
Date: 2009-10-30 10:03:35 -0400 (Fri, 30 Oct 2009)
New Revision: 1079

Added:
   demos/directory/pom.xml
   demos/directory/src/
   demos/directory/src/main/
   demos/directory/src/main/java/
   demos/directory/src/main/java/org/
   demos/directory/src/main/java/org/infinispan/
   demos/directory/src/main/java/org/infinispan/demos/
   demos/directory/src/main/java/org/infinispan/demos/directory/
   demos/directory/src/main/java/org/infinispan/demos/directory/FilesystemDirectory.java
Modified:
   demos/directory/
Log:
Commit Bela's directory test.


Property changes on: demos/directory
___________________________________________________________________
Name: svn:ignore
   + target
.settings
eclipse-output
test-output
output
.classpath
.project
temp-testng-customsuite.xml


Added: demos/directory/pom.xml
===================================================================
--- demos/directory/pom.xml	                        (rev 0)
+++ demos/directory/pom.xml	2009-10-30 14:03:35 UTC (rev 1079)
@@ -0,0 +1,26 @@
+<?xml version="1.0"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+   <modelVersion>4.0.0</modelVersion>
+
+   <parent>
+      <groupId>org.infinispan</groupId>
+      <artifactId>infinispan-parent</artifactId>
+      <version>4.0.0-SNAPSHOT</version>
+      <relativePath>../../parent/pom.xml</relativePath>
+   </parent>
+
+   <artifactId>infinispan-directory-demo</artifactId>
+   <version>4.0.0-SNAPSHOT</version>
+   <name>Infinispan Directory Demo</name>
+   <description>Infinispan - Directory Demo</description>
+   <dependencies>
+      <dependency>
+         <groupId>${project.groupId}</groupId>
+         <artifactId>infinispan-core</artifactId>
+         <version>${project.version}</version>
+      </dependency>
+   </dependencies>
+
+</project>

Added: demos/directory/src/main/java/org/infinispan/demos/directory/FilesystemDirectory.java
===================================================================
--- demos/directory/src/main/java/org/infinispan/demos/directory/FilesystemDirectory.java	                        (rev 0)
+++ demos/directory/src/main/java/org/infinispan/demos/directory/FilesystemDirectory.java	2009-10-30 14:03:35 UTC (rev 1079)
@@ -0,0 +1,221 @@
+package org.infinispan.demos.directory;
+
+import org.infinispan.Cache;
+import org.infinispan.config.Configuration;
+import org.infinispan.config.GlobalConfiguration;
+import org.infinispan.manager.CacheManager;
+import org.infinispan.manager.DefaultCacheManager;
+import org.jgroups.util.Util;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Arrays;
+import java.util.Map;
+
+/**
+ * @author Bela Ban
+ * @version $Id$
+ */
+public class FilesystemDirectory {
+    CacheManager manager;
+    Cache<String,byte[]> cache;
+
+
+    public void start() throws Exception {
+        GlobalConfiguration cfg=GlobalConfiguration.getClusteredDefault();
+        manager=new DefaultCacheManager(cfg);
+
+        Configuration config=new Configuration();
+
+        // config.setCacheMode(Configuration.CacheMode.REPL_ASYNC);
+        config.setCacheMode(Configuration.CacheMode.DIST_ASYNC);
+        // config.setStateRetrievalTimeout(10000);
+        config.setNumOwners(1); // no redundancy
+        // config.setNumOwners(2);
+
+
+        config.setL1CacheEnabled(true);
+
+        manager.defineConfiguration("bela", config);
+        cache=manager.getCache("bela");
+
+
+        boolean looping=true;
+        while(looping) {
+            int ch=Util.keyPress("[1] put dir [2] remove dir [3] dump [4] get [5] verify [x] exit");
+            switch(ch) {
+                case '1':
+                    String dir=Util.readStringFromStdin("dir: ");
+                    putDir(dir);
+                    break;
+                case '2':
+                    dir=Util.readStringFromStdin("dir: ");
+                    removeDir(dir);
+                    break;
+                case '3':
+                    int count=0;
+                    long bytes=0;
+                    for(Map.Entry<String,byte[]> entry: cache.entrySet()) {
+                        System.out.println(entry.getKey() + ": " + entry.getValue().length + " bytes");
+                        count++;
+                        bytes+=entry.getValue().length;
+                    }
+                    System.out.println(count + " files and " + Util.printBytes(bytes));
+                    break;
+                case '4':
+                    String key=Util.readStringFromStdin("key: ");
+                    byte[] val=cache.get(key);
+                    if(val == null)
+                        System.out.println("value for " + key + " not found");
+                    else
+                        System.out.println("val=" + val.length + " bytes");
+                    break;
+                case '5':
+                    dir=Util.readStringFromStdin("dir: ");
+                    verifyDir(dir);
+                    break;
+                case 'x':
+                case 'X':
+                    looping=false;
+                    break;
+                case -1:
+                    looping=false;
+                    break;
+            }
+        }
+        cache.stop();
+    }
+
+    private void verifyDir(String dir) {
+        if(dir == null)
+            return;
+
+        File root=new File(dir);
+        if(!root.exists()) {
+            System.err.println("Directory " + dir + " doesn't exist");
+            return;
+        }
+
+        int count=verifyDir(root);
+        System.out.println("OK: verified that " + root + " has " + count +
+                " files in the file system and in the cache, and that their sizes match");
+    }
+
+    private int verifyDir(File dir) {
+        File[] files=dir.listFiles();
+        if(files == null)
+            return 0;
+        int count=0;
+        for(File file: files) {
+            if(file.isDirectory())
+                count+=verifyDir(file);
+            else if(file.isFile()) {
+                try {
+                    String key=file.getPath();
+                    byte[] val=getContents(file);
+                    byte[] actual_val_in_cache=cache.get(key);
+                    if(!Arrays.equals(val, actual_val_in_cache)) {
+                        System.err.println("for key " + key + ": the file has " + val.length +
+                                " bytes in the file system, but " + actual_val_in_cache.length + " bytes in the cache");
+                    }
+                    count++;
+                }
+                catch(Throwable t) {
+                    System.err.println("failed verifying " + file);
+                }
+            }
+        }
+        return count;
+    }
+
+    private void removeDir(String dir) {
+        if(dir == null)
+            return;
+
+        File root=new File(dir);
+        if(!root.exists()) {
+            System.err.println("Directory " + dir + " doesn't exist");
+            return;
+        }
+
+        int count=removeDir(root);
+        System.out.println("removed " + count + " keys");
+    }
+
+
+    private int removeDir(File dir) {
+        File[] files=dir.listFiles();
+        if(files == null)
+            return 0;
+        int count=0;
+        for(File file: files) {
+            if(file.isDirectory())
+                count+=removeDir(file);
+            else if(file.isFile()) {
+                String key=file.getPath();
+                cache.remove(key);
+                count++;
+            }
+        }
+        return count;
+    }
+
+    private void putDir(String dir) {
+        File root=new File(dir);
+        if(!root.exists()) {
+            System.err.println("Directory " + dir + " doesn't exist");
+            return;
+        }
+
+        int count=putDir(root);
+        System.out.println("added " + count + " files");
+    }
+
+    private int putDir(File dir) {
+        File[] files=dir.listFiles();
+        if(files == null)
+            return 0;
+        int count=0;
+        for(File file: files) {
+            if(file.isDirectory())
+                count+=putDir(file);
+            else if(file.isFile()) {
+                String key=file.getPath();
+                byte[] val=null;
+                try {
+                    val=getContents(file);
+                    cache.put(key, val);
+                    count++;
+                }
+                catch(Throwable e) {
+                    System.err.println("failed reading contents of " + file);
+                }
+            }
+        }
+        return count;
+    }
+
+    private static byte[] getContents(File file) throws IOException {
+        InputStream in;
+        in=new FileInputStream(file);
+        try {
+            int length=(int)file.length();
+            byte[] buffer=new byte[length];
+            int read=in.read(buffer, 0, length);
+            if(read != length)
+                throw new IllegalStateException("length was " + length + ", but only " + read + " bytes were read");
+            return buffer;
+        }
+        finally {
+            Util.close(in);
+        }
+    }
+
+
+    public static void main(String[] args) throws Exception {
+        FilesystemDirectory test=new FilesystemDirectory();
+        test.start();
+    }
+}



More information about the infinispan-commits mailing list