示例#1
0
        public void TestSaveDataStoreToJson()
        {
            //given
            IDataStore <JObject> dataStore = new InMemoryDataStore <JObject>();

            dataStore.PersistPath = _dataPersistFile;
            var obj = new JObject();

            obj["key"] = "value";

            //when
            dataStore.Insert("main-key", obj);
            dataStore.Save();

            //then
            Assert.IsTrue(_ioService.Exists(_dataPersistFile));
            var content = _ioService.ReadFile(_dataPersistFile);

            Assert.AreEqual("{\"main-key\":{\"key\":\"value\"}}", content);
        }
        /// <summary>
        /// Load in-memory storage from file.
        /// </summary>
        /// <typeparam name="X"></typeparam>
        /// <param name="fullFilePath"></param>
        /// <returns></returns>
        public static InMemoryDataStore <X> Load <X>(string fullFilePath)
        {
            InMemoryDataStore <X> dataStore = new InMemoryDataStore <X>();

            dataStore.PersistPath = fullFilePath;
            IIOService  ioService = ServiceFinder.Resolve <IIOService> ();
            ILogService logger    = ServiceFinder.Resolve <ILogService> ();

            if (ioService.Exists(fullFilePath))
            {
                try {
                    string fileContent = ioService.ReadFile(fullFilePath);
                    dataStore.memoryStore = (Dictionary <string, X>)FHSyncUtils.DeserializeObject(fileContent, typeof(Dictionary <string, X>));
                } catch (Exception ex) {
                    logger.e("FHSyncClient.InMemoryDataStore", "Failed to load file " + fullFilePath, ex);
                    dataStore.memoryStore = new Dictionary <string, X> ();
                }
            }
            return(dataStore);
        }