/// <summary> /// Dumps everything of a single type into the cache from the filesystem for BackingData /// </summary> /// <typeparam name="T">the type to get and store</typeparam> /// <returns>full or partial success</returns> public static bool LoadAllToCache(Type objectType) { if (!objectType.GetInterfaces().Contains(typeof(IData))) return false; var fileAccessor = new NetMud.DataAccess.FileSystem.BackingData(); var typeDirectory = fileAccessor.BaseDirectory + fileAccessor.CurrentDirectoryName + objectType.Name + "/"; if (!fileAccessor.VerifyDirectory(typeDirectory, false)) { LoggingUtility.LogError(new AccessViolationException(String.Format("Current directory for type {0} does not exist.", objectType.Name))); return false; } var filesDirectory = new DirectoryInfo(typeDirectory); foreach (var file in filesDirectory.EnumerateFiles()) { try { BackingDataCache.Add(fileAccessor.ReadEntity(file, objectType)); } catch(Exception ex) { LoggingUtility.LogError(ex); //Let it keep going } } return true; }
/// <summary> /// Writes everything in the cache back to the file system /// </summary> /// <returns>full or partial success</returns> public static bool WriteFullBackup() { var fileAccessor = new NetMud.DataAccess.FileSystem.BackingData(); try { LoggingUtility.Log("World BackingData backup to current INITIATED.", LogChannels.Backup, true); fileAccessor.ArchiveFull(); //Get all the entities (which should be a ton of stuff) var entities = BackingDataCache.GetAll(); foreach (var entity in entities) fileAccessor.WriteEntity(entity as IData); LoggingUtility.Log("Entire backing data set written to current.", LogChannels.Backup, true); } catch (Exception ex) { LoggingUtility.LogError(ex); return false; } return true; }
/// <summary> /// Remove this object from the db permenantly /// </summary> /// <returns>success status</returns> public virtual bool Remove() { var accessor = new NetMud.DataAccess.FileSystem.BackingData(); try { //Remove from cache first BackingDataCache.Remove(new BackingDataCacheKey(this.GetType(), this.ID)); //Remove it from the file system. accessor.ArchiveEntity(this); } catch (Exception ex) { LoggingUtility.LogError(ex); return false; } return true; }
/// <summary> /// Update the field data for this object to the db /// </summary> /// <returns>success status</returns> public virtual bool Save() { var accessor = new NetMud.DataAccess.FileSystem.BackingData(); try { accessor.WriteEntity(this); } catch (Exception ex) { LoggingUtility.LogError(ex); return false; } return true; }
/// <summary> /// Add it to the cache and save it to the file system /// </summary> /// <returns>the object with ID and other db fields set</returns> public virtual IData Create() { var accessor = new NetMud.DataAccess.FileSystem.BackingData(); try { //reset this guy's ID to the next one in the list GetNextId(); BackingDataCache.Add(this); accessor.WriteEntity(this); } catch (Exception ex) { LoggingUtility.LogError(ex); return null; } return this; }