public void CreateIndex <T>(string indexName, Func <T, object> keyFunction) { var ixContainer = new YOLOtuple <T> .IndexContainer <T>(); ixContainer.IndexName = indexName; ixContainer.IndexFunction = keyFunction; //distributedRamDb.AddIndex<T>(null); if (ixContainer.Data == null) { ixContainer.Data = new ConcurrentDictionary <object, T>(); } if (!Tuples.ContainsKey(typeof(T))) { Init <T>(); } Debug.Assert(Tuples[typeof(T)] is YOLOtuple <T>); (Tuples[typeof(T)] as YOLOtuple <T>).Indexes[indexName] = ixContainer; Func <object, object> newKeyFunc = o => (object)keyFunction((T)o); if (!SetIndexNames.ContainsKey(typeof(T))) { SetIndexNames[typeof(T)] = new List <string>(); } SetIndexNames[typeof(T)].Add(indexName); Reindex <T>(); }
public void Init <T>() { Type t = typeof(T); if (Tuples.ContainsKey(t)) { return; //This type is already inited } var tuple = new YOLOtuple <T>(); int indexCount = 0; foreach (PropertyInfo property in t.GetProperties()) { object[] matchingProps = property.GetCustomAttributes(typeof(PrimaryKeyAttribute), true); if (!matchingProps.Any()) { continue; } Func <T, object> get = o => property.GetMethod.Invoke(o, null); var ixContainer = new YOLOtuple <T> .IndexContainer <T> { IndexName = property.Name, IndexFunction = get }; tuple.Indexes[property.Name] = ixContainer; indexCount++; } if (indexCount == 0) //All objects need at least one index { var ixContainer = new YOLOtuple <T> .IndexContainer <T> { IndexName = "_default_", IndexFunction = x => x }; tuple.Indexes["_default_"] = ixContainer; } Tuples[t] = tuple; }