[longomatch] Add a simple filter interface method



commit 736486e9f105e322621ecc3e698c52138a861e29
Author: Jorge Zapata <jorgeluis zapata gmail com>
Date:   Thu Mar 12 18:16:39 2015 +0100

    Add a simple filter interface method
    
    Implement it on the FileStorage and add the test unit

 LongoMatch.Core/Interfaces/IStorage.cs      |    7 +++
 LongoMatch.Services/Services/FileStorage.cs |   65 +++++++++++++++++++++++++++
 Tests/Services/TestFileStorage.cs           |   12 +++++
 3 files changed, 84 insertions(+), 0 deletions(-)
---
diff --git a/LongoMatch.Core/Interfaces/IStorage.cs b/LongoMatch.Core/Interfaces/IStorage.cs
index 604f856..bf12859 100644
--- a/LongoMatch.Core/Interfaces/IStorage.cs
+++ b/LongoMatch.Core/Interfaces/IStorage.cs
@@ -29,6 +29,13 @@ namespace LongoMatch.Core.Interfaces
                List<T> RetrieveAll<T>() where T : IStorable;
 
                /// <summary>
+               /// Retrieve every object of type T, where T must implement IStorable using on the dictionary 
as a filter on its properties
+               /// </summary>
+               /// <typeparam name="T">The type of IStorable you want to retrieve.</typeparam>
+               /// <param name="filter">The dictionary used to filter the returned List</param>
+               List<T> Retrieve<T>(Dictionary<string,object> filter) where T : IStorable;
+
+               /// <summary>
                /// Store the specified object
                /// </summary>
                /// <param name="t">The object to store.</param>
diff --git a/LongoMatch.Services/Services/FileStorage.cs b/LongoMatch.Services/Services/FileStorage.cs
index 0e25c84..8f57227 100644
--- a/LongoMatch.Services/Services/FileStorage.cs
+++ b/LongoMatch.Services/Services/FileStorage.cs
@@ -18,6 +18,7 @@
 using System;
 using System.IO;
 using System.Collections.Generic;
+using System.Reflection;
 using LongoMatch.Core.Interfaces;
 using LongoMatch.Core.Common;
 
@@ -80,6 +81,70 @@ namespace LongoMatch.Services.Services
                        return l;
                }
 
+               public List<T> Retrieve<T> (Dictionary<string,object> dict) where T : IStorable
+               {
+                       List<T> l = new List<T> ();
+                       string typePath = ResolvePath<T>();
+                       string extension = GetExtension(typeof(T));
+
+                       if (dict == null)
+                               return RetrieveAll<T>();
+
+                       // Get the name of the class and look for a folder on the
+                       // basePath with the same name
+                       foreach (string path in Directory.GetFiles (typePath, "*" + extension)) {
+                               T t = (T)Serializer.LoadSafe<T>(path);
+                               bool matches = true;
+
+                               foreach (KeyValuePair<string, object> entry in dict)
+                               {
+                                       FieldInfo finfo = t.GetType().GetField(entry.Key);
+                                       PropertyInfo pinfo = t.GetType().GetProperty(entry.Key);
+                                       object ret = null;
+
+                                       if (pinfo == null && finfo == null)
+                                       {
+                                               Log.Warning ("Property/Field does not exist " + entry.Key);
+                                               matches = false;
+                                               break;
+                                       }
+
+                                       if (pinfo != null)
+                                               ret = pinfo.GetValue(t, null);
+                                       else
+                                               ret = finfo.GetValue(t);
+
+                                       if (ret == null && entry.Value != null)
+                                       {
+                                               matches = false;
+                                               break;
+                                       }
+
+                                       if (ret != null && entry.Value == null)
+                                       {
+                                               matches = false;
+                                               break;
+                                       }
+
+                                       if (ret.GetType() == entry.Value.GetType())
+                                       {
+                                               if (Object.Equals(ret, entry.Value))
+                                               {
+                                                       matches = true;
+                                               }
+                                       }
+                               }
+
+                               if (matches)
+                               {
+                                       Log.Information ("Retrieving " + t.ID.ToString() + " at " + typePath);
+                                       l.Add (t);
+                               }
+                       }
+                       return l;
+               }
+
+
                public void Store<T> (T t) where T : IStorable
                {
                        string typePath = ResolvePath<T>();
diff --git a/Tests/Services/TestFileStorage.cs b/Tests/Services/TestFileStorage.cs
index a718c8a..b17c7fd 100644
--- a/Tests/Services/TestFileStorage.cs
+++ b/Tests/Services/TestFileStorage.cs
@@ -60,6 +60,18 @@ namespace Tests.Services
                        // Check that the object is the same
                        Assert.AreEqual(ts2.memberString, ts1.memberString);
 
+                       // Get based on memberString
+                       Dictionary<string, object> dict = new Dictionary<string, object>();
+                       dict.Add("memberString", "first");
+                       lts = fs.Retrieve<TestStorable>(dict);
+
+                       // Check that we have stored one object
+                       Assert.AreEqual(lts.Count, 1);
+
+                       // Check that the returned object is the one we are looking for
+                       ts2 = lts[0];
+                       Assert.AreNotSame(ts2, null);
+
                        // Check that the storage is empty
                        fs.Delete<TestStorable>(ts2);
                        lts = fs.RetrieveAll<TestStorable>();


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]