Compare commits

...

6 Commits

Author SHA1 Message Date
Ammar Ahmed
cbcef9d44a mobile: update mmkv package 2023-11-25 15:18:53 +05:00
Abdullah Atta
b9aa49e17a core: fix logs sorting 2023-11-25 14:42:14 +05:00
Abdullah Atta
bab65528db core: optimize logger 2023-11-25 14:30:48 +05:00
Abdullah Atta
4a743d98b7 web: use removeMulti to bulk remove old logs 2023-11-25 13:40:19 +05:00
Ammar Ahmed
4b44c39ec8 mobile: fix app freeze when deleting old logs 2023-11-25 13:17:31 +05:00
Ammar Ahmed
a7d85cb3f6 core: add removeMulti to storage 2023-11-25 13:17:10 +05:00
8 changed files with 238 additions and 302 deletions

View File

@@ -16,8 +16,8 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { MMKVLoader } from "react-native-mmkv-storage";
import { initalize } from "@notesnook/core/dist/logger";
import { MMKVLoader } from "react-native-mmkv-storage";
import { KV } from "./storage";
const LoggerStorage = new MMKVLoader()
@@ -26,4 +26,4 @@ const LoggerStorage = new MMKVLoader()
initalize(new KV(LoggerStorage));
export {};
export { LoggerStorage };

View File

@@ -90,6 +90,10 @@ export class KV {
return this.storage.removeItem(key);
}
async removeMulti(keys) {
return this.storage.removeItems(...keys);
}
async clear() {
return this.storage.clearStore();
}
@@ -145,6 +149,7 @@ export default {
clear: () => DefaultStorage.clear(),
getAllKeys: () => DefaultStorage.getAllKeys(),
writeMulti: (items) => DefaultStorage.writeMulti(items),
removeMulti: (keys) => DefaultStorage.removeMulti(keys),
encrypt,
decrypt,
decryptMulti,

View File

@@ -327,7 +327,7 @@ PODS:
- React-Core
- react-native-keep-awake (1.2.0):
- React-Core
- react-native-mmkv-storage (0.10.0-alpha.9):
- react-native-mmkv-storage (0.10.0-alpha.11):
- MMKV (~> 1.3.1)
- React
- React-Core
@@ -875,7 +875,7 @@ SPEC CHECKSUMS:
react-native-image-resizer: 00ceb0e05586c7aadf061eea676957a6c2ec60fa
react-native-in-app-review: db8bb167a5f238e7ceca5c242d6b36ce8c4404a4
react-native-keep-awake: caee3ff89eaa21dfe29010f0d143566874a04441
react-native-mmkv-storage: d4ad55ab411b7f0d4e6269d801b31821ef48970b
react-native-mmkv-storage: e798639f91601896f1c84f4eb3d6f19af2f4a160
react-native-netinfo: ccbe1085dffd16592791d550189772e13bf479e2
react-native-notification-sounds: da78c828fe1bcbb92d8b505d5261890ed315ff39
react-native-orientation: f1caf84d65f1a4fd4511a18f2b924e634ad7a628

View File

@@ -44,7 +44,7 @@
"react-native-image-picker": "4.1.2",
"react-native-in-app-review": "4.3.3",
"react-native-keychain": "4.0.5",
"react-native-mmkv-storage": "^0.10.0-alpha.9",
"react-native-mmkv-storage": "^0.10.0-alpha.11",
"react-native-modal-datetime-picker": "14.0.0",
"react-native-navigation-bar-color": "2.0.2",
"react-native-notification-sounds": "0.5.5",

File diff suppressed because it is too large Load Diff

View File

@@ -70,6 +70,10 @@ export class NNStorage {
return this.database.delete(key);
}
removeMulti(keys: string[]) {
return this.database.deleteMany(keys);
}
clear() {
return this.database.clear();
}

View File

@@ -48,6 +48,10 @@ export default class Storage {
return this.storage.remove(key);
}
removeMulti(keys) {
return this.storage.removeMulti(keys);
}
getAllKeys() {
return this.storage.getAllKeys();
}

View File

@@ -59,8 +59,7 @@ class DatabaseLogWriter {
*/
constructor(storage) {
this.storage = storage;
this.key = new Date().toLocaleDateString();
this.queue = {};
this.queue = new Map();
this.hasCleared = false;
setInterval(() => {
setTimeout(() => {
@@ -74,34 +73,29 @@ class DatabaseLogWriter {
}
push(message) {
this.queue[`${this.key}:${message.timestamp}`] = message;
}
async read() {
const logKeys = await this.storage.getAllKeys();
const keys = [];
for (let key of logKeys) {
if (key.startsWith(this.key)) keys.push(key);
}
return Object.values(await this.storage.readMulti(keys));
const key = new Date(message.timestamp).toLocaleDateString();
this.queue.set(`${key}:${message.timestamp}`, message);
}
async flush() {
if (Object.keys(this.queue).length === 0) return;
const queueCopy = Object.entries(this.queue);
this.queue = {};
if (this.queue.size === 0) return;
const queueCopy = Array.from(this.queue.entries());
this.queue = new Map();
await this.storage.writeMulti(queueCopy);
}
async rotate() {
const logKeys = (await this.storage.getAllKeys()).sort();
const keysToRemove = [];
for (let key of logKeys) {
const keyParts = key.split(":");
if (keyParts.length === 1 || parseInt(keyParts[1]) < Date.now() - WEEK) {
await this.storage.remove(key);
keysToRemove.push(key);
}
}
if (keysToRemove.length) await this.storage.removeMulti(keysToRemove);
}
}
@@ -116,42 +110,41 @@ class DatabaseLogManager {
async get() {
const logKeys = await this.storage.getAllKeys();
const logs = {};
const logs = await this.storage.readMulti(logKeys);
const logGroups = {};
for (const logKey of logKeys) {
const keyParts = logKey.split(":");
for (const [key, log] of logs) {
const keyParts = key.split(":");
if (keyParts.length === 1) continue;
const key = keyParts[0];
const log = await this.storage.read(logKey, true);
if (!logs[key]) logs[key] = [];
logs[key].push(log);
const groupKey = keyParts[0];
if (!logGroups[groupKey]) logGroups[groupKey] = [];
logGroups[groupKey].push(log);
}
return Object.keys(logs).map((key) => ({
key: key,
logs: logs[key]?.sort((a, b) => a.timestamp - b.timestamp)
}));
return Object.keys(logGroups)
.sort((a, b) => b.localeCompare(a, undefined, { numeric: true }))
.map((key) => ({
key,
logs: logGroups[key]?.sort((a, b) => a.timestamp - b.timestamp)
}));
}
async clear() {
const logKeys = await this.storage.getAllKeys();
for (const key of logKeys) {
await this.storage.remove(key);
}
await this.storage.removeMulti(logKeys);
}
async delete(key) {
const logKeys = await this.storage.getAllKeys();
const keysToRemove = [];
for (const logKey of logKeys) {
const keyParts = logKey.split(":");
if (keyParts.length === 1) continue;
const currKey = keyParts[0];
if (currKey === key) {
await this.storage.remove(logKey);
}
if (currKey === key) keysToRemove.push(logKey);
}
if (keysToRemove.length) await this.storage.removeMulti(keysToRemove);
}
}