Commit bf82ab36 authored by Sandeep S. Shewalkar's avatar Sandeep S. Shewalkar
Browse files

LazyWriter and SaveToFDB modifications

Purge method added and mkdir is set to true. Unwanted code from FDB removed
parent ffcaa72e
Branches
Tags
1 merge request!21LazyWriter and SaveToFDB modifications
Showing with 32 additions and 109 deletions
......@@ -102,114 +102,6 @@ func CheckModelTypeOfRequestedModel(filePath string) FDBModelType {
}
// //GetDataFromFDB GetDataFromFDB
// func GetDataFromFDB(filePath string,dataModel interface{}, predicateFunction func(interface{}) bool) interface{} {
// storage := GetStorage(dataModel)
// a := []model.Login{}
// fmt.Println("datamodel : ", reflect.TypeOf(dataModel))
// fmt.Println("datamodel : ", reflect.TypeOf(dataModel))
// fmt.Println("a : ", reflect.TypeOf(a))
// fmt.Println("a : ", reflect.TypeOf(a))
// linq.From(storage).
// WhereT(predicateFunction).
// ToSlice(&dataModel)
// fmt.Println(dataModel)
// fmt.Println(dataModel)
// return dataModel
// }
//GetDataFromFDB gets data from FDB
// func GetDataFromFDBOptimised(filePath string) (interface{}, error) {
// // Following checking will be done for the provided file path, which will let us know
// // whether the file is Read only/Write only/Update only
// // fDBModelType := CheckModelTypeOfRequestedModel(filePath)
// // if fDBModelType.ModelType == READ_ONLY {
// data := fileAndArrayMap[filePath]
// //}
// if data != nil || len(data) > 0 {
// return data, nil
// }
// data, err := filehelper.ReadFile(filePath)
// if err != nil {
// if hostAddressForDataFiles == "" {
// logginghelper.LogError("Call dalhelper.SetRemoteHostPath(remoteHost) to set remote file path ")
// return nil, err
// }
// relativeFilePath := strings.SplitAfter(filePath, splitString)
// remotePath := hostAddressForDataFiles + relativeFilePath[1]
// resp, httpError := http.Get(remotePath)
// if httpError != nil || resp.StatusCode == 404 {
// logginghelper.LogError("Error while receiving data from ", remotePath, " ", httpError, " Status code :", resp.StatusCode)
// return nil, httpError
// }
// data, readError := ioutil.ReadAll(resp.Body)
// if readError != nil {
// logginghelper.LogError("Error while reading data from response body ", readError)
// return nil, readError
// }
// defer resp.Body.Close()
// saveError := SaveDataToFDB(filePath, data, true)
// if saveError != nil {
// logginghelper.LogError("Error while saving data to ", filePath, " ", saveError)
// return nil, saveError
// }
// }
// fileAndArrayMap[filePath] = data
// return data, err
// }
// //GetDataFromFDB gets data from FDB
// func GetDataFromFDB(filePath string, dataModel interface{}, predicateFunction func(interface{}) bool) (interface{}, error) {
// //Following checking will be done for the provided file path, which will let us know
// // whether the file is Read only/Write only/Update only
// fDBModelType := CheckModelTypeOfRequestedModel(filePath)
// if fDBModelType.ModelType == READ_ONLY {
// storage := fileAndArrayMap[filePath]
// linq.From(storage).
// WhereT(predicateFunction).
// ToSlice(&dataModel)
// return dataModel, errors.New("")
// }
// data, err := filehelper.ReadFile(filePath)
// if err != nil {
// if hostAddressForDataFiles == "" {
// logginghelper.LogError("Call dalhelper.SetRemoteHostPath(remoteHost) to set remote file path ")
// return nil, err
// }
// relativeFilePath := strings.SplitAfter(filePath, splitString)
// remotePath := hostAddressForDataFiles + relativeFilePath[1]
// resp, httpError := http.Get(remotePath)
// if httpError != nil || resp.StatusCode == 404 {
// logginghelper.LogError("Error while receiving data from ", remotePath, " ", httpError, " Status code :", resp.StatusCode)
// return nil, httpError
// }
// data, readError := ioutil.ReadAll(resp.Body)
// if readError != nil {
// logginghelper.LogError("Error while reading data from response body ", readError)
// return nil, readError
// }
// defer resp.Body.Close()
// saveError := SaveDataToFDB(filePath, data, true)
// if saveError != nil {
// logginghelper.LogError("Error while saving data to ", filePath, " ", saveError)
// return nil, saveError
// }
// }
// return data, err
// }
//GetDataFromFDB gets data from FDB
func GetDataFromFDB(filePath string) ([]byte, error) {
//Following checking will be done for the provided file path, which will let us know
......
......@@ -186,7 +186,7 @@ func saveDataToFDB(filePath string, objectData interface{}) {
if marshalError != nil {
logginghelper.LogError("error occured while marshaling data ", marshalError)
}
saveError := SaveDataToFDB(filePath, byteArray, false)
saveError := SaveDataToFDB(filePath, byteArray, true)
if saveError != nil {
logginghelper.LogError("error occured while saving data ", saveError)
}
......@@ -270,3 +270,34 @@ func (lfd *LazyFDPHelper) GetAllFromCache() map[interface{}]interface{} {
func (lfd *LazyFDPHelper) GetCacheLength() int {
return lfd.gc.Len()
}
// PurgeCache first saves all data inside FDB and finally purge all Cache
func (lfd *LazyFDPHelper) PurgeCache() {
// Fetch All Rows and then save into db
cachedObjectList := lfd.gc.GetALL()
for item := range cachedObjectList {
//TODO: catch errors
cachedObject, getError := lfd.gc.Get(item)
if getError != nil {
logginghelper.LogError("error occured while getting ", item, " from gcache")
}
cachedObjectActual, ok := cachedObject.(LazyCacheObject)
if ok && cachedObjectActual.ChangeCount > 0 {
cachedObjectActual.IsLocked = true
saveDataToFDB(cachedObjectActual.FileName, cachedObjectActual.InterfaceData)
cachedObjectActual.ChangeCount = 0
cachedObjectActual.IsLocked = false
lazyMutex.Lock()
lfd.DISK_WRITE_COUNT++
lazyMutex.Unlock()
}
}
lazyMutex.Lock()
lfd.gc.Purge()
lazyMutex.Unlock()
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment