public rrd4n.DataAccess.Data.FetchData GetData(rrd4n.DataAccess.Data.FetchRequest request) { RrdDb rrdDb = null; try { string dataPath; if (DataPath.Contains("${APPDATA}")) { dataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); dataPath += DataPath.Substring(10); } else dataPath = DataPath; rrdDb = new RrdDb(dataPath + request.DatabasePath); FetchRequest coreRequest = new FetchRequest(rrdDb.getPath(), request.ConsolidateFunctionName, request.FetchStart, request.FetchEnd, request.Resolution); FetchData coreFetchData = rrdDb.fetchData(coreRequest); return new rrd4n.DataAccess.Data.FetchData(coreFetchData.getArcStep(), coreFetchData.getArcEndTime(), coreFetchData.getDsNames()) { Values = coreFetchData.getValues(), Timestamps = coreFetchData.getTimestamps() }; } finally { if (rrdDb != null) rrdDb.close(); } }
public void StoreData(rrd4n.DataAccess.Data.Sample sample) { RrdDb rrdDb = null; try { rrdDb = new RrdDb(DataPath + sample.DatabasePath, false); Sample coreSample = new Sample(rrdDb.getPath(),rrdDb.getDsNames(), sample.Time); coreSample.setValues(sample.Values); rrdDb.store(coreSample); } finally { if (rrdDb != null) rrdDb.close(); } }
/** * Requests a RrdDb reference for the given path. The file will be created from * external data (from XML dump, RRD file or RRDTool's binary RRD file).<p> * <ul> * <li>If the file with the path specified is already open, * the method blocks until the file is closed. * <li>If the file is not already open and the number of already open RRD files is less than * {@link #INITIAL_CAPACITY}, a new RRD file will be created and a its RrdDb reference will be returned. * If the file is not already open and the number of already open RRD files is equal to * {@link #INITIAL_CAPACITY}, the method blocks until some RRD file is closed. * </ul> * * @param path Path to RRD file which should be created * @param sourcePath Path to external data which is to be converted to Rrd4n's native RRD file format * @return Reference to the newly created RRD file * @ Thrown in case of I/O error */ //public RrdDb requestRrdDb(String path, String sourcePath) { // lock (sync) // { // String canonicalPath = Util.getCanonicalPath(path); // while (rrdMap.containsKey(canonicalPath) || rrdMap.size() >= capacity) // { // Thread.Sleep(10); // } // RrdDb rrdDb = new RrdDb(canonicalPath, sourcePath); // rrdMap.put(canonicalPath, new RrdEntry(rrdDb)); // return rrdDb; // } //} /** * Releases RrdDb reference previously obtained from the pool. When a reference is released, its usage * count is decremented by one. If usage count drops to zero, the underlying RRD file will be closed. * * @param rrdDb RrdDb reference to be returned to the pool * @ Thrown in case of I/O error */ public void release(RrdDb rrdDb) { lock (sync) { // null pointer should not kill the thread, just ignore it if (rrdDb == null) { return; } String canonicalPath = Util.getCanonicalPath(rrdDb.getPath()); if (!rrdMap.ContainsKey(canonicalPath)) { throw new ApplicationException("Could not release [" + canonicalPath + "], the file was never requested"); } RrdEntry entry = rrdMap[canonicalPath]; if (--entry.count <= 0) { // no longer used rrdMap.Remove(canonicalPath); entry.rrdDb.close(); } } }