Author: bdekruijff at gmail.com
Date: Tue Nov 16 10:21:19 2010
New Revision: 430

Log:
AMDATU-176 keep list in memory / some refactoring

Modified:
   
trunk/amdatu-core/tenantstore-fs/src/main/java/org/amdatu/core/tenantstore/fs/internal/FSTenantIdList.java
   
trunk/amdatu-core/tenantstore-fs/src/main/java/org/amdatu/core/tenantstore/fs/internal/FSTenantStore.java
   
trunk/amdatu-core/tenantstore-fs/src/main/java/org/amdatu/core/tenantstore/fs/internal/FSUtil.java
   
trunk/amdatu-core/tenantstore-fs/src/main/java/org/amdatu/core/tenantstore/fs/service/FSTenantStorageProvider.java
   
trunk/amdatu-core/tenantstore-fs/src/test/java/org/amdatu/core/tenantstore/fs/service/FSTenantStorageProviderTest.java

Modified: 
trunk/amdatu-core/tenantstore-fs/src/main/java/org/amdatu/core/tenantstore/fs/internal/FSTenantIdList.java
==============================================================================
--- 
trunk/amdatu-core/tenantstore-fs/src/main/java/org/amdatu/core/tenantstore/fs/internal/FSTenantIdList.java
  (original)
+++ 
trunk/amdatu-core/tenantstore-fs/src/main/java/org/amdatu/core/tenantstore/fs/internal/FSTenantIdList.java
  Tue Nov 16 10:21:19 2010
@@ -33,26 +33,28 @@
 public final class FSTenantIdList {
 
     private final File m_file;
+    private List<String> m_tenantIdList;
 
     public FSTenantIdList(final File file) throws TenantStorageException {
         m_file = file;
-    }
-
-    public synchronized List<String> getAll() throws TenantStorageException {
+        m_tenantIdList = new LinkedList<String>();
         try {
-            return readTenantIdList();
+            readTenantIdList();
         }
         catch (IOException e) {
             throw new TenantStorageException(e);
         }
     }
 
+    public synchronized List<String> getAll() throws TenantStorageException {
+        return new LinkedList<String>(m_tenantIdList);
+    }
+
     public synchronized void addTenantId(final String entityId) throws 
TenantStorageException {
         try {
-            final List<String> tenantEntityIdList = readTenantIdList();
-            if (!tenantEntityIdList.contains(entityId)) {
-                tenantEntityIdList.add(entityId);
-                writeTenantIdList(m_file, tenantEntityIdList);
+            if (!m_tenantIdList.contains(entityId)) {
+                m_tenantIdList.add(entityId);
+                writeTenantIdList();
             }
         }
         catch (IOException e) {
@@ -62,10 +64,9 @@
 
     public synchronized void removeTenantId(final String entityId) throws 
TenantStorageException {
         try {
-            final List<String> tenantEntityIdList = readTenantIdList();
-            if (tenantEntityIdList.contains(entityId)) {
-                tenantEntityIdList.remove(entityId);
-                writeTenantIdList(m_file, tenantEntityIdList);
+            if (m_tenantIdList.contains(entityId)) {
+                m_tenantIdList.remove(entityId);
+                writeTenantIdList();
             }
         }
         catch (IOException e) {
@@ -73,10 +74,10 @@
         }
     }
 
-    private List<String> readTenantIdList() throws IOException {
-        final List<String> tenantEntityIdList = new LinkedList<String>();
+    private void readTenantIdList() throws IOException {
         if (!m_file.exists()) {
-            return tenantEntityIdList;
+            m_tenantIdList.clear();
+            return;
         }
         FileInputStream fis = null;
         ObjectInputStream ois = null;
@@ -85,30 +86,28 @@
             ois = new ObjectInputStream(fis);
             final int numberOfTenantEntityIds = ois.readInt();
             for (int i = 0; i < numberOfTenantEntityIds; i++) {
-                final int idLength = ois.readInt();
-                final byte[] idBytes = new byte[idLength];
-                ois.readFully(idBytes);
-                final String id = new String(idBytes, "utf-8");
-                tenantEntityIdList.add(id);
+                final String id = FSUtil.readString(ois);
+                m_tenantIdList.add(id);
             }
-            return tenantEntityIdList;
         }
         finally {
             FSUtil.closeInputStreamsSafely(ois, fis);
         }
     }
 
-    private void writeTenantIdList(final File file, final List<String> 
tenantEntityIdList) throws IOException {
+    private void writeTenantIdList() throws IOException {
+        if (m_tenantIdList.size() == 0 && m_file.exists()) {
+            m_file.delete();
+            return;
+        }
         FileOutputStream fos = null;
         ObjectOutputStream oos = null;
         try {
-            fos = new FileOutputStream(file);
+            fos = new FileOutputStream(m_file);
             oos = new ObjectOutputStream(fos);
-            oos.writeInt(tenantEntityIdList.size());
-            for (String tenantEntityId : tenantEntityIdList) {
-                final byte[] idBytes = tenantEntityId.getBytes("utf-8");
-                oos.writeInt(idBytes.length);
-                oos.write(idBytes);
+            oos.writeInt(m_tenantIdList.size());
+            for (final String tenantId : m_tenantIdList) {
+                FSUtil.writeString(oos, tenantId);
             }
             oos.flush();
         }

Modified: 
trunk/amdatu-core/tenantstore-fs/src/main/java/org/amdatu/core/tenantstore/fs/internal/FSTenantStore.java
==============================================================================
--- 
trunk/amdatu-core/tenantstore-fs/src/main/java/org/amdatu/core/tenantstore/fs/internal/FSTenantStore.java
   (original)
+++ 
trunk/amdatu-core/tenantstore-fs/src/main/java/org/amdatu/core/tenantstore/fs/internal/FSTenantStore.java
   Tue Nov 16 10:21:19 2010
@@ -35,7 +35,7 @@
 public final class FSTenantStore {
 
     private final File m_file;
-    private Map<String, TenantEntity> m_entities;
+    private final Map<String, TenantEntity> m_entities;
 
     public FSTenantStore(File file) throws TenantStorageException {
         m_file = file;
@@ -74,14 +74,11 @@
             return;
         }
         m_entities.clear();
-
         FileInputStream fis = null;
         ObjectInputStream ois = null;
-
         try {
             fis = new FileInputStream(m_file);
             ois = new ObjectInputStream(fis);
-
             final int numberOfTenants = ois.readInt();
             for (int i = 0; i < numberOfTenants; i++) {
                 final TenantEntity tenantEntity = readEntity(ois);
@@ -95,47 +92,22 @@
 
     private TenantEntity readEntity(ObjectInputStream ois) throws IOException,
         UnsupportedEncodingException {
-        final int idLength = ois.readInt();
-        final byte[] idBytes = new byte[idLength];
-        ois.readFully(idBytes);
-        final String id = new String(idBytes, "utf-8");
-
-        final int nameLength = ois.readInt();
-        final byte[] nameBytes = new byte[nameLength];
-        ois.readFully(nameBytes);
-        final String name = new String(nameBytes, "utf-8");
-
-        final int numberOfProperties = ois.readInt();
-        final Map<String, String> properties = new HashMap<String, String>();
-
-        for (int j = 0; j < numberOfProperties; j++) {
-
-            final int keyLength = ois.readInt();
-            final byte[] keyBytes = new byte[keyLength];
-            ois.readFully(keyBytes);
-            final String key = new String(keyBytes, "utf-8");
-
-            final int valueLength = ois.readInt();
-            final byte[] valueBytes = new byte[valueLength];
-            ois.readFully(valueBytes);
-            final String value = new String(valueBytes, "utf-8");
-
-            properties.put(key, value);
-        }
+        final String id = FSUtil.readString(ois);
+        final String name = FSUtil.readString(ois);
+        final Map<String, String> properties = FSUtil.readProperties(ois);
         return new TenantEntity(id, name, properties);
     }
 
     private void writeEntities() throws IOException {
         if ((m_entities == null || m_entities.size() == 0) && m_file.exists()) 
{
             m_file.delete();
+            return;
         }
-
         FileOutputStream fos = null;
         ObjectOutputStream oos = null;
         try {
             fos = new FileOutputStream(m_file);
             oos = new ObjectOutputStream(fos);
-
             oos.writeInt(m_entities.size());
             for (TenantEntity entity : m_entities.values()) {
                 writeEntity(oos, entity);
@@ -149,26 +121,8 @@
 
     private void writeEntity(ObjectOutputStream oos, TenantEntity entity) 
throws UnsupportedEncodingException,
         IOException {
-
-        final byte[] id = entity.getId().getBytes("utf-8");
-        oos.writeInt(id.length);
-        oos.write(id);
-
-        final byte[] name = entity.getName().getBytes("utf-8");
-        oos.writeInt(name.length);
-        oos.write(name);
-
-        oos.writeInt(entity.getProperties().size());
-
-        for (String propKey : entity.getProperties().keySet()) {
-
-            final byte[] key = propKey.getBytes("utf-8");
-            oos.writeInt(key.length);
-            oos.write(key);
-
-            final byte[] value = 
entity.getProperties().get(propKey).getBytes("utf-8");
-            oos.writeInt(value.length);
-            oos.write(value);
-        }
+        FSUtil.writeString(oos, entity.getId());
+        FSUtil.writeString(oos, entity.getName());
+        FSUtil.writeProperties(oos, entity.getProperties());
     }
 }

Modified: 
trunk/amdatu-core/tenantstore-fs/src/main/java/org/amdatu/core/tenantstore/fs/internal/FSUtil.java
==============================================================================
--- 
trunk/amdatu-core/tenantstore-fs/src/main/java/org/amdatu/core/tenantstore/fs/internal/FSUtil.java
  (original)
+++ 
trunk/amdatu-core/tenantstore-fs/src/main/java/org/amdatu/core/tenantstore/fs/internal/FSUtil.java
  Tue Nov 16 10:21:19 2010
@@ -21,12 +21,58 @@
 import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
+import java.util.HashMap;
+import java.util.Map;
 
 /**
  * Some generic utility methods.
  */
 public final class FSUtil {
 
+    public static String readString(final ObjectInputStream ois) throws 
IOException {
+        final int keyLength = ois.readInt();
+        final byte[] keyBytes = new byte[keyLength];
+        ois.readFully(keyBytes);
+        return new String(keyBytes, "utf-8");
+    }
+
+    public static Map<String, String> readProperties(final ObjectInputStream 
ois) throws IOException {
+        final int numberOfProperties = ois.readInt();
+        if (numberOfProperties == 0) {
+            return null;
+        }
+        final Map<String, String> properties = new HashMap<String, String>();
+        for (int i = 0; i < numberOfProperties; i++) {
+            final String key = readString(ois);
+            final String value = readString(ois);
+            properties.put(key, value);
+        }
+        return properties;
+    }
+
+    public static void writeString(final ObjectOutputStream oos, final String 
value) throws IOException {
+        String mvalue = value;
+        if (mvalue == null) {
+            mvalue = "";
+        }
+        final byte[] bytes = mvalue.getBytes("utf-8");
+        oos.writeInt(bytes.length);
+        oos.write(bytes);
+    }
+
+    public static void writeProperties(final ObjectOutputStream oos, final 
Map<String, String> properties)
+        throws IOException {
+        if (properties == null) {
+            oos.writeInt(0);
+            return;
+        }
+        oos.writeInt(properties.size());
+        for (final String key : properties.keySet()) {
+            writeString(oos, key);
+            writeString(oos, properties.get(key));
+        }
+    }
+
     public static void closeInputStreamsSafely(ObjectInputStream ois, 
FileInputStream fis) throws IOException {
         try {
             if (ois != null) {

Modified: 
trunk/amdatu-core/tenantstore-fs/src/main/java/org/amdatu/core/tenantstore/fs/service/FSTenantStorageProvider.java
==============================================================================
--- 
trunk/amdatu-core/tenantstore-fs/src/main/java/org/amdatu/core/tenantstore/fs/service/FSTenantStorageProvider.java
  (original)
+++ 
trunk/amdatu-core/tenantstore-fs/src/main/java/org/amdatu/core/tenantstore/fs/service/FSTenantStorageProvider.java
  Tue Nov 16 10:21:19 2010
@@ -37,7 +37,7 @@
     private static final String STORAGEFILE_PREFIX = "t_";
     private static final String STORAGEFILE_POSTFIX = ".ser";
 
-    private final FSTenantIdList m_FSEntityIdList;
+    private final FSTenantIdList m_fsSEntityIdList;
     private final File m_RootDirectory;
 
     public FSTenantStorageProvider(final File rootDirectory) throws 
TenantStorageException {
@@ -45,7 +45,7 @@
             throw new TenantStorageException("Unable to access root 
directory");
         }
         m_RootDirectory = rootDirectory;
-        m_FSEntityIdList = new FSTenantIdList(new File(m_RootDirectory, 
ENTITYLIST_FILENAME));
+        m_fsSEntityIdList = new FSTenantIdList(new File(rootDirectory, 
ENTITYLIST_FILENAME));
     }
 
     public FSTenantStorageProvider() throws TenantStorageException {
@@ -56,16 +56,16 @@
         FSTenantStore tenantStorageFile = getStorageFile(entity.getId());
         TenantEntity storedEntity = tenantStorageFile.addEntity(entity);
         if (storedEntity == null) {
-            m_FSEntityIdList.addTenantId(entity.getId());
+            m_fsSEntityIdList.addTenantId(entity.getId());
         }
         tenantStorageFile.save();
     }
 
     public synchronized void delete(final TenantEntity entity) throws 
TenantStorageException {
         FSTenantStore tenantStorageFile = getStorageFile(entity.getId());
-        TenantEntity storeEntity = 
tenantStorageFile.removeEntity(entity.getId());
-        if (storeEntity != null) {
-            m_FSEntityIdList.removeTenantId(entity.getId());
+        TenantEntity storedEntity = 
tenantStorageFile.removeEntity(entity.getId());
+        if (storedEntity != null) {
+            m_fsSEntityIdList.removeTenantId(entity.getId());
             tenantStorageFile.save();
         }
     }
@@ -76,7 +76,7 @@
     }
 
     public synchronized List<TenantEntity> getAll() throws 
TenantStorageException {
-        final List<String> entityIdList = m_FSEntityIdList.getAll();
+        final List<String> entityIdList = m_fsSEntityIdList.getAll();
         final List<TenantEntity> tenantEntityList = new 
LinkedList<TenantEntity>();
         for (String tenantEntityId : entityIdList) {
             tenantEntityList.add(getById(tenantEntityId));

Modified: 
trunk/amdatu-core/tenantstore-fs/src/test/java/org/amdatu/core/tenantstore/fs/service/FSTenantStorageProviderTest.java
==============================================================================
--- 
trunk/amdatu-core/tenantstore-fs/src/test/java/org/amdatu/core/tenantstore/fs/service/FSTenantStorageProviderTest.java
      (original)
+++ 
trunk/amdatu-core/tenantstore-fs/src/test/java/org/amdatu/core/tenantstore/fs/service/FSTenantStorageProviderTest.java
      Tue Nov 16 10:21:19 2010
@@ -51,7 +51,7 @@
     @Test
     public void testExtensiveCRUD() throws TenantStorageException {
 
-        final int testSize = 179;
+        final int testSize = 197;
 
         // creating
         for (int i = 0; i < testSize; i++) {

Reply via email to