using System; using System.Collections; using ln.types.odb.values; using System.Collections.Generic; using System.Linq; namespace ln.types.odb.ng { //public class ObjectCollection : IEnumerable //{ // public ODBMapper ODBMapper { get; } // public ODBCollection DocumentCollection { get; private set; } // public Type ElementType { get; } // public String CollectionName => DocumentCollection.CollectionName; // public int Count => DocumentCollection.Count; // public Type IDType => ODBMapper.GetDocumentIDType(ElementType); // internal ObjectCollection(ODBMapper odbmapper, Type elementType) // : this(odbmapper, elementType, elementType.FullName) // { } // internal ObjectCollection(ODBMapper odbmapper, Type elementType, String collectionAlias) // { // ODBMapper = odbmapper; // ElementType = elementType; // DocumentCollection = ODBMapper.StorageContainer.GetCollection(elementType.FullName); // } // public object SelectByID(object ID) // { // ODBValue documentID = ODBMapper.MapValue(ID); // return SelectByID(documentID); // } // public object SelectByID(ODBValue documentID) // { // if (ODBNull.Instance.Equals(documentID)) // return null; // lock (this) // { // object o = GetCachedObject(documentID); // if (object.ReferenceEquals(null, o)) // { // ODBDocument document = DocumentCollection.GetDocumentByID(documentID); // o = ODBMapper.UnmapValue(ElementType, document); // TouchCache(documentID, o); // } // return o; // } // } // public IEnumerable Select(Query query) // { // lock (this) // { // return new ObjectEnumeration(this, query.Execute(DocumentCollection).ToArray()); // } // } // public bool Ensure(object o) // { // if (!ElementType.IsInstanceOfType(o)) // throw new ArgumentException(String.Format("Object needs to be of type {0}", ElementType.FullName), nameof(o)); // lock (this) // { // ODBDocument document = ODBMapper.MapValue(o) as ODBDocument; // if (DocumentCollection.Ensure(document)) // { // TouchCache(document.ID, o); // return true; // } // return false; // } // } // public bool Insert(object o) // { // lock (this) // { // if (!ElementType.IsInstanceOfType(o)) // throw new ArgumentException(String.Format("Object needs to be of type {0}", ElementType.FullName), nameof(o)); // ODBDocument document = ODBMapper.MapValue(o) as ODBDocument; // if (DocumentCollection.Insert(document)) // { // TouchCache(document.ID, o); // return true; // } // return false; // } // } // public bool Update(object o) // { // lock (this) // { // lock (this) // { // if (!ElementType.IsInstanceOfType(o)) // throw new ArgumentException(String.Format("Object needs to be of type {0}", ElementType.FullName), nameof(o)); // ODBDocument document = ODBMapper.MapValue(o) as ODBDocument; // if (DocumentCollection.Update(document)) // { // TouchCache(document.ID, o); // return true; // } // return false; // } // } // } // public bool Upsert(object o) // { // if (!ElementType.IsInstanceOfType(o)) // throw new ArgumentException(String.Format("Object needs to be of type {0}", ElementType.FullName), nameof(o)); // lock (this) // { // ODBDocument document = ODBMapper.MapValue(o) as ODBDocument; // if (DocumentCollection.Upsert(document)) // { // TouchCache(document.ID, o); // return true; // } // return false; // } // } // public bool Delete(object o) => Delete(ODBMapper.MapValue(ODBMapper.GetDocumentID(o))); // public bool Delete(ODBValue documentID) // { // lock (this) // { // if (DocumentCollection.Delete(documentID)) // { // if (objectCache.ContainsKey(documentID)) // objectCache.Remove(documentID); // return true; // } // return false; // } // } // public bool HasProperty(string propName) // { // propName = IndexPath.TranslatePropertyPath(ElementType, propName); // ClassMapping classMapping = ODBMapper.GetMapping(ElementType) as ClassMapping; // if (classMapping != null) // { // return classMapping.HasField(propName); // } // return false; // } // /* Indeces */ // public void EnsureIndex(string propertyPath, bool unique = false) // { // //string translatedPath = IndexPath.TranslatePropertyPath(ElementType, propertyPath); // //DocumentCollection.EnsureIndex(translatedPath, translatedPath); // EnsureIndeces(false, new string[] { propertyPath }); // } // public void EnsureIndeces(params string[] propertyPaths) => EnsureIndeces(false, propertyPaths); // public void EnsureIndeces(bool unique, params string[] propertyPaths) // { // for (int n = 0; n < propertyPaths.Length; n++) // propertyPaths[n] = IndexPath.TranslatePropertyPath(ElementType, propertyPaths[n]); // DocumentCollection.EnsureIndeces(propertyPaths, false); // } // public void EnsureUniqueness(params string[] propertyPaths) // { // for (int n = 0; n < propertyPaths.Length; n++) // propertyPaths[n] = IndexPath.TranslatePropertyPath(ElementType, propertyPaths[n]); // DocumentCollection.EnsureUniqueness(propertyPaths); // } // /* Object Cache */ // public bool UseStrongCache { get; private set; } // Dictionary objectCache = new Dictionary(); // public void EnableStrongCache(bool enable) // { // lock (this) // { // if (!enable) // { // foreach (ODBValue key in objectCache.Keys.ToArray()) // { // if (!(objectCache[key] is WeakReference)) // objectCache.Remove(key); // } // } // else // { // foreach (ODBValue key in objectCache.Keys.ToArray()) // { // if ((objectCache[key] is WeakReference)) // objectCache[key] = (objectCache[key] as WeakReference).Target; // } // } // } // } // private object GetCachedObject(ODBValue documentID) // { // if (objectCache.ContainsKey(documentID)) // { // object o = objectCache[documentID]; // if (o is WeakReference) // { // WeakReference weak = o as WeakReference; // if (weak.IsAlive) // return weak.Target; // else // return null; // } // return o; // } // return null; // } // private void TouchCache(ODBValue documentID, object o) // { // if (object.ReferenceEquals(o, null) && objectCache.ContainsKey(documentID)) // { // objectCache.Remove(documentID); // } // else if (!object.ReferenceEquals(o, null)) // { // if (UseStrongCache) // objectCache[documentID] = o; // else // objectCache[documentID] = new WeakReference(o); // } // } // public object[] GetDocumentIDs() // { // return DocumentCollection.Index.Select((arg) => ODBMapper.UnmapValue(IDType, arg)).ToArray(); // } // public IEnumerable GetEnumeration() // { // lock (this) // { // return new ObjectEnumeration(this, DocumentCollection.Index.ToArray()); // } // } // public IEnumerator GetEnumerator() // { // return GetEnumeration().GetEnumerator(); // } // public void Close() // { // DocumentCollection = null; // } // class ObjectEnumeration : IEnumerable // { // ObjectCollection collection; // IEnumerable documentIDs; // public ObjectEnumeration(ObjectCollection collection,IEnumerable documentIDs) // { // this.collection = collection; // this.documentIDs = documentIDs; // } // public IEnumerator GetEnumerator() // { // foreach (ODBValue documentID in this.documentIDs) // { // yield return this.collection.SelectByID(documentID); // } // } // } //} //public class ObjectCollection : ObjectCollection where T:class //{ // public ObjectCollection(ODBMapper odbmapper) // :base(odbmapper,typeof(T)) // {} // public IEnumerable SelectQuery(Query query) => base.Select(query).Cast(); // public T Select(object id) => (T)base.SelectByID(id); // public bool Ensure(T o) => base.Ensure(o); // public bool Insert(T o) => base.Insert(o); // public bool Update(T o) => base.Update(o); // public bool Upsert(T o) => base.Upsert(o); // public void Delete(T o) => base.Delete(o); //} }