Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
nheko
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Nheko Reborn
nheko
Commits
88349eae
Commit
88349eae
authored
7 years ago
by
Konstantinos Sideris
Browse files
Options
Downloads
Patches
Plain Diff
Recover from corrupted cache data
Make Cache constructor exception free fixes #74
parent
73222aa9
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
include/Cache.h
+3
-7
3 additions, 7 deletions
include/Cache.h
src/Cache.cc
+16
-5
16 additions, 5 deletions
src/Cache.cc
src/ChatPage.cc
+19
-15
19 additions, 15 deletions
src/ChatPage.cc
with
38 additions
and
27 deletions
include/Cache.h
+
3
−
7
View file @
88349eae
...
@@ -35,9 +35,9 @@ public:
...
@@ -35,9 +35,9 @@ public:
inline
void
deleteData
();
inline
void
deleteData
();
inline
void
unmount
();
inline
void
unmount
();
inline
QString
memberDbName
(
const
QString
&
roomid
);
void
removeRoom
(
const
QString
&
roomid
);
void
removeRoom
(
const
QString
&
roomid
);
void
setup
();
private:
private:
void
setNextBatchToken
(
lmdb
::
txn
&
txn
,
const
QString
&
token
);
void
setNextBatchToken
(
lmdb
::
txn
&
txn
,
const
QString
&
token
);
...
@@ -59,15 +59,11 @@ Cache::unmount()
...
@@ -59,15 +59,11 @@ Cache::unmount()
isMounted_
=
false
;
isMounted_
=
false
;
}
}
inline
QString
Cache
::
memberDbName
(
const
QString
&
roomid
)
{
return
QString
(
"m.%1"
).
arg
(
roomid
);
}
inline
void
inline
void
Cache
::
deleteData
()
Cache
::
deleteData
()
{
{
qInfo
()
<<
"Deleting cache data"
;
if
(
!
cacheDirectory_
.
isEmpty
())
if
(
!
cacheDirectory_
.
isEmpty
())
QDir
(
cacheDirectory_
).
removeRecursively
();
QDir
(
cacheDirectory_
).
removeRecursively
();
}
}
This diff is collapsed.
Click to expand it.
src/Cache.cc
+
16
−
5
View file @
88349eae
...
@@ -37,10 +37,21 @@ Cache::Cache(const QString &userId)
...
@@ -37,10 +37,21 @@ Cache::Cache(const QString &userId)
,
isMounted_
{
false
}
,
isMounted_
{
false
}
,
userId_
{
userId
}
,
userId_
{
userId
}
{
{
}
void
Cache
::
setup
()
{
qDebug
()
<<
"Setting up cache"
;
auto
statePath
=
QString
(
"%1/%2/state"
)
auto
statePath
=
QString
(
"%1/%2/state"
)
.
arg
(
QStandardPaths
::
writableLocation
(
QStandardPaths
::
CacheLocation
))
.
arg
(
QStandardPaths
::
writableLocation
(
QStandardPaths
::
CacheLocation
))
.
arg
(
QString
::
fromUtf8
(
userId_
.
toUtf8
().
toHex
()));
.
arg
(
QString
::
fromUtf8
(
userId_
.
toUtf8
().
toHex
()));
cacheDirectory_
=
QString
(
"%1/%2"
)
.
arg
(
QStandardPaths
::
writableLocation
(
QStandardPaths
::
CacheLocation
))
.
arg
(
QString
::
fromUtf8
(
userId_
.
toUtf8
().
toHex
()));
bool
isInitial
=
!
QFile
::
exists
(
statePath
);
bool
isInitial
=
!
QFile
::
exists
(
statePath
);
env_
=
lmdb
::
env
::
create
();
env_
=
lmdb
::
env
::
create
();
...
@@ -48,7 +59,7 @@ Cache::Cache(const QString &userId)
...
@@ -48,7 +59,7 @@ Cache::Cache(const QString &userId)
env_
.
set_max_dbs
(
1024UL
);
env_
.
set_max_dbs
(
1024UL
);
if
(
isInitial
)
{
if
(
isInitial
)
{
qDebug
()
<<
"
[cache]
First time initializing LMDB"
;
qDebug
()
<<
"First time initializing LMDB"
;
if
(
!
QDir
().
mkpath
(
statePath
))
{
if
(
!
QDir
().
mkpath
(
statePath
))
{
throw
std
::
runtime_error
(
throw
std
::
runtime_error
(
...
@@ -83,10 +94,7 @@ Cache::Cache(const QString &userId)
...
@@ -83,10 +94,7 @@ Cache::Cache(const QString &userId)
txn
.
commit
();
txn
.
commit
();
isMounted_
=
true
;
isMounted_
=
true
;
cacheDirectory_
=
QString
(
"%1/%2"
)
.
arg
(
QStandardPaths
::
writableLocation
(
QStandardPaths
::
CacheLocation
))
.
arg
(
QString
::
fromUtf8
(
userId_
.
toUtf8
().
toHex
()));
}
}
void
void
...
@@ -156,6 +164,9 @@ Cache::insertRoomState(lmdb::txn &txn, const QString &roomid, const RoomState &s
...
@@ -156,6 +164,9 @@ Cache::insertRoomState(lmdb::txn &txn, const QString &roomid, const RoomState &s
void
void
Cache
::
removeRoom
(
const
QString
&
roomid
)
Cache
::
removeRoom
(
const
QString
&
roomid
)
{
{
if
(
!
isMounted_
)
return
;
auto
txn
=
lmdb
::
txn
::
begin
(
env_
,
nullptr
,
0
);
auto
txn
=
lmdb
::
txn
::
begin
(
env_
,
nullptr
,
0
);
lmdb
::
dbi_del
(
txn
,
roomDb_
,
lmdb
::
val
(
roomid
.
toUtf8
(),
roomid
.
toUtf8
().
size
()),
nullptr
);
lmdb
::
dbi_del
(
txn
,
roomDb_
,
lmdb
::
val
(
roomid
.
toUtf8
(),
roomid
.
toUtf8
().
size
()),
nullptr
);
...
...
This diff is collapsed.
Click to expand it.
src/ChatPage.cc
+
19
−
15
View file @
88349eae
...
@@ -248,16 +248,23 @@ ChatPage::bootstrap(QString userid, QString homeserver, QString token)
...
@@ -248,16 +248,23 @@ ChatPage::bootstrap(QString userid, QString homeserver, QString token)
client_
->
setAccessToken
(
token
);
client_
->
setAccessToken
(
token
);
client_
->
getOwnProfile
();
client_
->
getOwnProfile
();
cache_
=
QSharedPointer
<
Cache
>
(
new
Cache
(
userid
));
try
{
try
{
cache_
=
QSharedPointer
<
Cache
>
(
new
Cache
(
userid
));
cache_
->
setup
();
}
catch
(
const
std
::
exception
&
e
)
{
qCritical
()
<<
e
.
what
();
if
(
cache_
->
isInitialized
())
{
loadStateFromCache
();
return
;
}
}
catch
(
const
lmdb
::
error
&
e
)
{
qCritical
()
<<
"Cache failure"
<<
e
.
what
();
cache_
->
unmount
();
cache_
->
deleteData
();
qInfo
()
<<
"Falling back to initial sync ..."
;
}
}
if
(
cache_
->
isInitialized
())
client_
->
initialSync
();
loadStateFromCache
();
else
client_
->
initialSync
();
}
}
void
void
...
@@ -367,6 +374,7 @@ ChatPage::syncCompleted(const SyncResponse &response)
...
@@ -367,6 +374,7 @@ ChatPage::syncCompleted(const SyncResponse &response)
qCritical
()
<<
"The cache couldn't be updated: "
<<
e
.
what
();
qCritical
()
<<
"The cache couldn't be updated: "
<<
e
.
what
();
// TODO: Notify the user.
// TODO: Notify the user.
cache_
->
unmount
();
cache_
->
unmount
();
cache_
->
deleteData
();
}
}
client_
->
setNextBatchToken
(
response
.
nextBatch
());
client_
->
setNextBatchToken
(
response
.
nextBatch
());
...
@@ -416,6 +424,7 @@ ChatPage::initialSyncCompleted(const SyncResponse &response)
...
@@ -416,6 +424,7 @@ ChatPage::initialSyncCompleted(const SyncResponse &response)
}
catch
(
const
lmdb
::
error
&
e
)
{
}
catch
(
const
lmdb
::
error
&
e
)
{
qCritical
()
<<
"The cache couldn't be initialized: "
<<
e
.
what
();
qCritical
()
<<
"The cache couldn't be initialized: "
<<
e
.
what
();
cache_
->
unmount
();
cache_
->
unmount
();
cache_
->
deleteData
();
}
}
client_
->
setNextBatchToken
(
response
.
nextBatch
());
client_
->
setNextBatchToken
(
response
.
nextBatch
());
...
@@ -492,14 +501,8 @@ ChatPage::loadStateFromCache()
...
@@ -492,14 +501,8 @@ ChatPage::loadStateFromCache()
{
{
qDebug
()
<<
"Restoring state from cache"
;
qDebug
()
<<
"Restoring state from cache"
;
try
{
qDebug
()
<<
"Restored nextBatchToken"
<<
cache_
->
nextBatchToken
();
qDebug
()
<<
"Restored nextBatchToken"
<<
cache_
->
nextBatchToken
();
client_
->
setNextBatchToken
(
cache_
->
nextBatchToken
());
client_
->
setNextBatchToken
(
cache_
->
nextBatchToken
());
}
catch
(
const
lmdb
::
error
&
e
)
{
qCritical
()
<<
"Failed to load next_batch_token from cache"
<<
e
.
what
();
// TODO: Clean the environment
return
;
}
// Fetch all the joined room's state.
// Fetch all the joined room's state.
auto
rooms
=
cache_
->
states
();
auto
rooms
=
cache_
->
states
();
...
@@ -612,6 +615,7 @@ ChatPage::removeRoom(const QString &room_id)
...
@@ -612,6 +615,7 @@ ChatPage::removeRoom(const QString &room_id)
qCritical
()
<<
"The cache couldn't be updated: "
<<
e
.
what
();
qCritical
()
<<
"The cache couldn't be updated: "
<<
e
.
what
();
// TODO: Notify the user.
// TODO: Notify the user.
cache_
->
unmount
();
cache_
->
unmount
();
cache_
->
deleteData
();
}
}
room_list_
->
removeRoom
(
room_id
,
room_id
==
current_room_
);
room_list_
->
removeRoom
(
room_id
,
room_id
==
current_room_
);
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment