Skip to content

Commit

Permalink
fix(chunk): wake up database when find
Browse files Browse the repository at this point in the history
Signed-off-by: Godefroy Ponsinet <godefroy.ponsinet@outlook.com>
  • Loading branch information
90dy committed Jan 30, 2019
1 parent 9029d4f commit 37d7985
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 20 deletions.
Expand Up @@ -15,13 +15,12 @@

public class CoreModule extends ReactContextBaseJavaModule {
private Logger logger = new Logger("chat.berty.io");
private String filesDir = "";

private ReactApplicationContext reactContext;
private MobileNotification notificationDriver = Core.getNotificationDriver();

public CoreModule(ReactApplicationContext reactContext) {
super(reactContext);
this.filesDir = reactContext.getFilesDir().getAbsolutePath();

String storagePath = reactContext.getFilesDir().getAbsolutePath();
try {
Expand Down
20 changes: 3 additions & 17 deletions client/react-native/ios/modules/core/CoreModule.swift
Expand Up @@ -30,23 +30,11 @@ class CoreModule: NSObject {
}
}

func getFilesDir() throws -> String {
let filesDir = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first
let filesPath = filesDir?.path
let fileExist = FileManager.default.fileExists(atPath: filesPath!)

// do this in gomobile and set storage as global in gomobile
if fileExist == false {
try FileManager.default.createDirectory(at: filesDir!, withIntermediateDirectories: true, attributes: nil)
}
return filesPath!
}

@objc func initialize(_ resolve: RCTPromiseResolveBlock!, reject: RCTPromiseRejectBlock!) {
var err: NSError?

do {
CoreInitialize(logger, Core.storagePath(), &err)
CoreInitialize(logger, Core.deviceInfo().getStoragePath(), &err)
if let error = err {
throw error
}
Expand All @@ -61,7 +49,7 @@ class CoreModule: NSObject {
var err: NSError?

do {
let list = CoreListAccounts(try self.getFilesDir(), &err)
let list = CoreListAccounts(&err)
if let error = err {
throw error
}
Expand All @@ -76,10 +64,8 @@ class CoreModule: NSObject {
var err: NSError?

do {
let datastore = try self.getFilesDir()

guard let coreOptions = CoreMobileOptions()?
.withDatastorePath(datastore)?
.withLoggerDriver(logger)?
.withNickname(nickname as String)
else {
Expand Down Expand Up @@ -122,7 +108,7 @@ class CoreModule: NSObject {
var err: NSError?

do {
CoreDropDatabase(try self.getFilesDir(), &err)
CoreDropDatabase(&err)
if let error = err {
throw error
}
Expand Down
8 changes: 7 additions & 1 deletion core/chunk/storage.go
Expand Up @@ -96,7 +96,7 @@ func cleanDatabase() error {
return nil
}

func save(chunk *Chunk) error {
func wakeUpDatabase() {
for db == nil || db.DB().Ping() != nil {
if err := openDatabase(); err != nil {
logger().Error(err.Error())
Expand All @@ -105,13 +105,18 @@ func save(chunk *Chunk) error {
break
}
}
}

func save(chunk *Chunk) error {
wakeUpDatabase()
if err := db.Save(chunk).Error; err != nil {
return errorcodes.ErrChunkSave.Wrap(err)
}
return nil
}

func findAllSlices() ([][]*Chunk, error) {
wakeUpDatabase()
firstChunks := []*Chunk{}
if err := db.Where(&Chunk{Index: 0}).Find(&firstChunks).Error; err != nil {
return nil, errorcodes.ErrChunkFindAllSlices.Wrap(err)
Expand All @@ -128,6 +133,7 @@ func findAllSlices() ([][]*Chunk, error) {
}

func findSliceByID(sliceID string) ([]*Chunk, error) {
wakeUpDatabase()
slice := []*Chunk{}
if err := db.Order("index").Where(&Chunk{SliceID: sliceID}).Find(&slice).Error; err != nil {
return nil, errorcodes.ErrChunkFindSliceByID.Wrap(err)
Expand Down

0 comments on commit 37d7985

Please sign in to comment.