public VR33BSqliteStorage() { var defaultSetting = VR33BSqliteStorageSetting.Default; var fileName = SettingFileName; var filePath = Environment.CurrentDirectory + "//" + fileName; XmlSerializer serializer = new XmlSerializer(typeof(VR33BSqliteStorageSetting)); if (!File.Exists(filePath)) { using (FileStream settingStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { serializer.Serialize(settingStream, defaultSetting); }; Setting = defaultSetting; } else { using (FileStream settingStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { Setting = (VR33BSqliteStorageSetting)serializer.Deserialize(settingStream); }; } _DataContext = new VR33BSqliteStorageContext(); _BeforeStoreBuffer = new List <VR33BSampleValueEntity>(); _InMemoryBuffer = new List <VR33BSampleValueEntity>(); _BeforeStoreBufferLock = new object(); _DataContextLock = new ReaderWriterLockSlim(); _InMemoryBufferLock = new ReaderWriterLockSlim(); _MemoryToDataBaseTransferLock = new object(); }
public async Task <List <VR33BSampleProcess> > GetAllSampleProcessAsync() { using (VR33BSqliteStorageContext dbContext = new VR33BSqliteStorageContext()) { _DataContextLock.EnterReadLock(); var result = await(from entity in dbContext.SampleProcessEntities select entity.ToStruct()).ToListAsync(); _DataContextLock.ExitReadLock(); return(result); } }
public Task <List <VR33BSampleValue> > QueryAsync(VR33BSampleValueQueryDelegate queryFunc) { return(Task.Run(() => { _InMemoryBufferLock.EnterReadLock(); var inMemoryStructBuffer = from entity in _InMemoryBuffer where entity.SampleProcessGuid == _CurrentSampleProcess.Guid select entity.ToStruct(); var inMemoryResult = queryFunc(inMemoryStructBuffer).ToList(); _InMemoryBufferLock.ExitReadLock(); List <VR33BSampleValue> inDBResult; IEnumerable <VR33BSampleValue> inDBStructBuffer; using (var dbcontext = new VR33BSqliteStorageContext()) { _DataContextLock.EnterReadLock(); if (inMemoryResult.Count > 0) { inDBStructBuffer = from entity in dbcontext.SampleValueEntities where entity.SampleProcessGuid == _CurrentSampleProcess.Guid && entity.SampleIndex < inMemoryResult.First().SampleIndex select entity.ToStruct(); } else { inDBStructBuffer = from entity in dbcontext.SampleValueEntities where entity.SampleProcessGuid == _CurrentSampleProcess.Guid select entity.ToStruct(); } inDBResult = queryFunc(inDBStructBuffer).ToList(); _DataContextLock.ExitReadLock(); } inDBResult.Sort((value1, value2) => { if (value1.SampleIndex < value2.SampleIndex) { return -1; } else if (value1.SampleIndex == value2.SampleIndex) { return 0; } else { return 1; } }); var result = new List <VR33BSampleValue>(); result.AddRange(inDBResult); result.AddRange(inMemoryResult); return result; })); }
public Task <List <VR33BSampleValue> > GetFromSampleIndexRangeAsync(long minIndex, long maxIndex) { return(Task.Run(() => { List <VR33BSampleValue> inMemoryQueryResult = new List <VR33BSampleValue>(); List <VR33BSampleValue> inDatabaseQueryResult = new List <VR33BSampleValue>(); bool inDatabaseQueryNeeded = true; _InMemoryBufferLock.EnterReadLock(); { if (_InMemoryBuffer.Count > 0) { if (minIndex >= _InMemoryBuffer.First().SampleIndex&& maxIndex <= _InMemoryBuffer.Last().SampleIndex) { inDatabaseQueryNeeded = false; } inMemoryQueryResult = (from entity in _InMemoryBuffer where entity.SampleProcessGuid == _CurrentSampleProcess.Guid && entity.SampleIndex >= minIndex && entity.SampleIndex <= maxIndex select entity.ToStruct()).ToList(); inMemoryQueryResult.Sort((value1, value2) => { if (value1.SampleIndex < value2.SampleIndex) { return -1; } else if (value1.SampleIndex == value2.SampleIndex) { return 0; } else { return 1; } }); } } _InMemoryBufferLock.ExitReadLock(); if (inDatabaseQueryNeeded) { using (var dbcontext = new VR33BSqliteStorageContext()) { _DataContextLock.EnterReadLock(); { inDatabaseQueryResult.AddRange((from entity in dbcontext.SampleValueEntities where entity.SampleProcessGuid == _CurrentSampleProcess.Guid && entity.SampleIndex >= minIndex && entity.SampleIndex <= maxIndex select entity.ToStruct()).ToList()); } _DataContextLock.ExitReadLock(); } inDatabaseQueryResult.Sort((value1, value2) => { if (value1.SampleIndex < value2.SampleIndex) { return -1; } else if (value1.SampleIndex == value2.SampleIndex) { return 0; } else { return 1; } }); } var list = new List <VR33BSampleValue>(); list.AddRange(inDatabaseQueryResult); list.AddRange(inMemoryQueryResult); return list; })); }
public Task <List <VR33BSampleValue> > GetFromDateTimeRangeAsync(DateTime startDateTime, DateTime endDateTime) { return(Task.Run(() => { List <VR33BSampleValue> inMemoryQueryResult = new List <VR33BSampleValue>(); List <VR33BSampleValue> inDatabaseQueryResult = new List <VR33BSampleValue>(); bool inDatabaseQueryNeeded = true; _InMemoryBufferLock.EnterReadLock(); { if (_InMemoryBuffer.Count != 0) { if (startDateTime > _InMemoryBuffer.First().SampleDateTime /*&& endDateTime <= _InMemoryBuffer.Last().SampleDateTime*/) { inDatabaseQueryNeeded = false; } inMemoryQueryResult.AddRange( (from entity in _InMemoryBuffer where entity.SampleProcessGuid == _CurrentSampleProcess.Guid && entity.SampleDateTime >= startDateTime && entity.SampleDateTime <= endDateTime select entity.ToStruct()).ToList() ); } } _InMemoryBufferLock.ExitReadLock(); if (inDatabaseQueryNeeded) { using (VR33BSqliteStorageContext dbcontext = new VR33BSqliteStorageContext()) { _DataContextLock.EnterReadLock(); { DateTime beforeReadDateTime = DateTime.Now; System.Diagnostics.Debug.WriteLine("BEGIN QUERY FROM DB"); inDatabaseQueryResult.AddRange( (from entity in dbcontext.SampleValueEntities where entity.SampleProcessGuid == _CurrentSampleProcess.Guid && entity.SampleDateTime >= startDateTime && entity.SampleDateTime <= endDateTime select entity.ToStruct()).ToList() ); System.Diagnostics.Debug.WriteLine("QUERY FROM DB TAKES " + (DateTime.Now - beforeReadDateTime).TotalMilliseconds + "Ms"); } _DataContextLock.ExitReadLock(); } inDatabaseQueryResult.Sort((value1, value2) => { if (value1.SampleIndex < value2.SampleIndex) { return -1; } else if (value1.SampleIndex == value2.SampleIndex) { return 0; } else { return 1; } }); } var list = new List <VR33BSampleValue>(); list.AddRange(inDatabaseQueryResult); list.AddRange(inMemoryQueryResult); return list; })); }