浏览代码

use potdb.

oldj 4 年之前
父节点
当前提交
24ddf4a1e9

+ 19 - 0
package-lock.json

@@ -15,6 +15,7 @@
         "md5": "^2.3.0",
         "md5-file": "^5.0.0",
         "mkdirp": "^1.0.4",
+        "potdb": "^2.0.1",
         "uuid": "^8.3.2"
       },
       "devDependencies": {
@@ -17349,6 +17350,15 @@
         "node": ">=6"
       }
     },
+    "node_modules/potdb": {
+      "version": "2.0.1",
+      "resolved": "https://registry.npmjs.org/potdb/-/potdb-2.0.1.tgz",
+      "integrity": "sha512-lM0156IRWluFvLObDxCIs5YNhpoL5E2zDfiHKJQKn9amoQ46w8d6i15P+rORXUwQXY4fnR40oB88RXOHJjEf9Q==",
+      "dependencies": {
+        "lodash": "^4.17.21",
+        "mkdirp": "^1.0.4"
+      }
+    },
     "node_modules/power-assert": {
       "version": "1.6.1",
       "resolved": "https://registry.npmjs.org/power-assert/-/power-assert-1.6.1.tgz",
@@ -37431,6 +37441,15 @@
         "uniq": "^1.0.1"
       }
     },
+    "potdb": {
+      "version": "2.0.1",
+      "resolved": "https://registry.npmjs.org/potdb/-/potdb-2.0.1.tgz",
+      "integrity": "sha512-lM0156IRWluFvLObDxCIs5YNhpoL5E2zDfiHKJQKn9amoQ46w8d6i15P+rORXUwQXY4fnR40oB88RXOHJjEf9Q==",
+      "requires": {
+        "lodash": "^4.17.21",
+        "mkdirp": "^1.0.4"
+      }
+    },
     "power-assert": {
       "version": "1.6.1",
       "resolved": "https://registry.npmjs.org/power-assert/-/power-assert-1.6.1.tgz",

+ 1 - 0
package.json

@@ -25,6 +25,7 @@
     "md5": "^2.3.0",
     "md5-file": "^5.0.0",
     "mkdirp": "^1.0.4",
+    "potdb": "^2.0.1",
     "uuid": "^8.3.2"
   },
   "devDependencies": {

+ 1 - 1
src/main/actions/getBasicData.ts

@@ -44,7 +44,7 @@ export default async (): Promise<IHostsBasicData> => {
 
   let list = normalizeList(await swhdb.list.tree.all())
   let trashcan = normalizeTrashcan(await swhdb.list.trashcan.all())
-  let v = await swhdb.dict.meta.get('version', version)
+  let v = await swhdb.dict.meta.get<VersionType>('version', version)
 
   return {
     ...default_data,

+ 5 - 5
src/main/data/index.ts

@@ -5,15 +5,15 @@
  */
 
 import getDataFolder from '@main/libs/getDataFolder'
-import SwhDb from '@main/utils/db'
+import PotDb from 'potdb'
 import * as path from 'path'
 
-let swhdb: SwhDb
-let cfgdb: SwhDb
+let swhdb: PotDb
+let cfgdb: PotDb
 
 if (!global.swhdb) {
   let db_dir: string = path.join(getDataFolder(), 'data')
-  swhdb = new SwhDb(db_dir)
+  swhdb = new PotDb(db_dir)
   console.log(`data db: ${swhdb.dir}`)
   global.swhdb = swhdb
 } else {
@@ -22,7 +22,7 @@ if (!global.swhdb) {
 
 if (!global.cfgdb) {
   let db_dir: string = path.join(getDataFolder(), 'config')
-  cfgdb = new SwhDb(db_dir)
+  cfgdb = new PotDb(db_dir)
   console.log(`config db: ${cfgdb.dir}`)
   global.cfgdb = cfgdb
 } else {

+ 0 - 175
src/main/utils/db/core/db.ts

@@ -1,175 +0,0 @@
-/**
- * db
- * @author: oldj
- * @homepage: https://oldj.net
- */
-
-import getKeys, { IKeys } from './keys'
-import * as path from 'path'
-import settings from '../settings'
-import { DataTypeDocument, IBasicOptions, IDbDataJSON } from '../typings'
-import Collection from './type/collection'
-import Dict from './type/dict'
-import List from './type/list'
-import LatSet from './type/set'
-
-interface IDBOptions extends IBasicOptions {
-}
-
-export default class LatDb {
-  dir: string
-  options: IDBOptions
-  dict: { [key: string]: Dict }
-  list: { [key: string]: List }
-  set: { [key: string]: LatSet }
-  collection: { [key: string]: Collection }
-  private _dict: { [key: string]: Dict } = {}
-  private _list: { [key: string]: List } = {}
-  private _set: { [key: string]: LatSet } = {}
-  private _collection: { [key: string]: Collection } = {}
-
-  constructor(root_dir: string, options?: Partial<IDBOptions>) {
-    // if (!fs.existsSync(path) || !fs.statSync(path).isDirectory()) {
-    //   throw new Error(`'${path}' is not a directory.`)
-    // }
-
-    this.dir = root_dir
-    this.options = { ...this.getDefaultOptions(), ...options }
-
-    this.dict = new Proxy({}, {
-      get: (target: {}, key: PropertyKey, receiver: any): Dict => {
-        let name: string = key.toString()
-        if (!this._dict.hasOwnProperty(name)) {
-          this._dict[name] = new Dict(name, path.join(this.dir, 'dict'), this.options)
-        }
-
-        return this._dict[name]
-      },
-    })
-
-    this.list = new Proxy({}, {
-      get: (target: {}, key: PropertyKey, receiver: any): List => {
-        let name: string = key.toString()
-        if (!this._list.hasOwnProperty(name)) {
-          this._list[name] = new List(name, path.join(this.dir, 'list'), this.options)
-        }
-
-        return this._list[name]
-      },
-    })
-
-    this.set = new Proxy({}, {
-      get: (target: {}, key: PropertyKey, receiver: any): LatSet => {
-        let name: string = key.toString()
-        if (!this._set.hasOwnProperty(name)) {
-          this._set[name] = new LatSet(name, path.join(this.dir, 'set'), this.options)
-        }
-
-        return this._set[name]
-      },
-    })
-
-    this.collection = new Proxy({}, {
-      get: (target: {}, key: PropertyKey, receiver: any): Collection => {
-        let name: string = key.toString()
-        if (!this._collection.hasOwnProperty(name)) {
-          this._collection[name] = new Collection(this, name)
-        }
-
-        return this._collection[name]
-      },
-    })
-  }
-
-  private getDefaultOptions(): IDBOptions {
-    const options: IDBOptions = {
-      debug: false,
-      dump_delay: settings.io_dump_delay,
-      ignore_error: true,
-    }
-
-    return options
-  }
-
-  async keys(): Promise<IKeys> {
-    return await getKeys(this.dir)
-  }
-
-  async toJSON(): Promise<IDbDataJSON> {
-    let keys = await this.keys()
-    let data: IDbDataJSON = {}
-
-    // dict
-    data.dict = {}
-    if (keys.dict) {
-      for (let name of keys.dict) {
-        data.dict[name] = await this.dict[name].all()
-      }
-    }
-
-    // list
-    data.list = {}
-    if (keys.list) {
-      for (let name of keys.list) {
-        data.list[name] = await this.list[name].all()
-      }
-    }
-
-    // set
-    data.set = {}
-    if (keys.set) {
-      for (let name of keys.set) {
-        data.set[name] = await this.set[name].all()
-      }
-    }
-
-    // collection
-    data.collection = {}
-    if (keys.collection) {
-      for (let name of keys.collection) {
-        data.collection[name] = {
-          meta: await this.collection[name]._getMeta(),
-          data: await this.collection[name].all<DataTypeDocument>(),
-        }
-      }
-    }
-
-    return data
-  }
-
-  async loadJSON(data: IDbDataJSON) {
-    // dict
-    if (data.dict) {
-      for (let name of Object.keys(data.dict)) {
-        await this.dict[name].update(data.dict[name])
-      }
-    }
-
-    // list
-    if (data.list) {
-      for (let name of Object.keys(data.list)) {
-        await this.list[name].update(data.list[name])
-      }
-    }
-
-    // set
-    if (data.set) {
-      for (let name of Object.keys(data.set)) {
-        await this.set[name].update(data.set[name])
-      }
-    }
-
-    // collection
-    if (data.collection) {
-      for (let name of Object.keys(data.collection)) {
-        await this.collection[name].remove()
-        for (let doc of data.collection[name].data) {
-          await this.collection[name]._insert(doc)
-        }
-        if (data.collection[name].meta) {
-          await this.collection[name]._setMeta(data.collection[name].meta)
-        }
-      }
-    }
-  }
-}

+ 0 - 169
src/main/utils/db/core/io.ts

@@ -1,169 +0,0 @@
-/**
- * io
- * @author: oldj
- * @homepage: https://oldj.net
- */
-
-import * as fs from 'fs'
-import * as path from 'path'
-import { DataTypeDict, DataTypeList, DataTypeSet } from '../typings'
-import { ensureDir } from '../utils/fs2'
-import wait from '../utils/wait'
-
-type DataType = 'dict' | 'list' | 'set' | 'collection'
-
-interface IIOOptions {
-  debug?: boolean;
-  data_path: string;
-  data_type: DataType;
-  dump_delay: number;
-  formative?: boolean;
-}
-
-export default class IO {
-  private options: IIOOptions
-  private data_path: string
-  private data_type: DataType
-  private _dump_delay: number// dump 节流间隔,单位为 ms
-  private _last_dump_ts: number = 0
-  private _t_dump: any
-  private _is_dir_ensured: boolean = false
-  private _dump_status: number = 0 // 0: 不需要 dump; 1: 等待或正在 dump
-
-  constructor(options: IIOOptions) {
-    this.options = { ...options }
-    this.data_path = options.data_path
-    this.data_type = options.data_type
-    this._dump_delay = options.dump_delay
-  }
-
-  private async load_file(fn: string) {
-    let d: any
-    try {
-      let content: string = await fs.promises.readFile(fn, 'utf-8')
-      d = JSON.parse(content)
-    } catch (e) {
-      console.error(e)
-    }
-
-    return d
-  }
-
-  private async load_dict(): Promise<DataTypeDict> {
-    let data: DataTypeDict = {}
-
-    if (!fs.existsSync(this.data_path)) {
-      return data
-    }
-
-    let d: any = await this.load_file(this.data_path)
-    if (typeof d === 'object') {
-      data = { ...d }
-    }
-    // console.log(data)
-
-    return data
-  }
-
-  private async load_list(): Promise<DataTypeList> {
-    let data: DataTypeList = []
-
-    if (!fs.existsSync(this.data_path)) {
-      return data
-    }
-
-    let d: any = await this.load_file(this.data_path)
-    if (Array.isArray(d)) {
-      data = [...d]
-    }
-
-    return data
-  }
-
-  private async load_set(): Promise<DataTypeSet> {
-    let data: DataTypeSet = new Set()
-
-    if (!fs.existsSync(this.data_path)) {
-      return data
-    }
-
-    let d: any = await this.load_file(this.data_path)
-    if (Array.isArray(d)) {
-      d.map(v => data.add(v))
-    }
-
-    return data
-  }
-
-  async load<T>(): Promise<T> {
-    let data: any
-
-    if (!this._is_dir_ensured) {
-      let dir_path = path.dirname(this.data_path)
-      await ensureDir(dir_path)
-      this._is_dir_ensured = true
-    }
-
-    switch (this.data_type) {
-      case 'dict':
-        data = await this.load_dict()
-        break
-      case 'list':
-        data = await this.load_list()
-        break
-      case 'set':
-        data = await this.load_set()
-        break
-    }
-
-    return data
-  }
-
-  private async dump_file(data: any, fn: string) {
-    if (this.data_type === 'set') {
-      data = Array.from(data)
-    }
-
-    try {
-      let out = this.options.formative ?
-        JSON.stringify(data, null, 2) :
-        JSON.stringify(data)
-      await ensureDir(path.dirname(fn))
-      await fs.promises.writeFile(fn, out, 'utf-8')
-      if (this.options.debug) {
-        console.log(`io.dump_file: -> ${fn}`)
-      }
-    } catch (e) {
-      console.error(e)
-    }
-  }
-
-  async dump(data: any) {
-    this._dump_status = 1
-    clearTimeout(this._t_dump)
-
-    let ts = (new Date()).getTime()
-
-    if (ts - this._last_dump_ts < this._dump_delay) {
-      this._t_dump = setTimeout(() => this.dump(data), this._dump_delay)
-      return
-    }
-
-    this._last_dump_ts = ts
-
-    await this.dump_file(data, this.data_path)
-    await wait(50)
-    this._dump_status = 0
-  }
-
-  getDumpStatus() {
-    return this._dump_status
-  }
-
-  async remove() {
-    let fn = this.data_path
-    if (!fn || !fs.existsSync(fn)) return
-
-    await fs.promises.unlink(fn)
-  }
-}

+ 0 - 53
src/main/utils/db/core/keys.ts

@@ -1,53 +0,0 @@
-/**
- * keys
- * @author: oldj
- * @homepage: https://oldj.net
- */
-
-import { isDir, isFile } from '../utils/fs2'
-import { promises as fs } from 'fs'
-import * as path from 'path'
-
-export interface IKeys {
-  dict: string[];
-  list: string[];
-  set: string[];
-  collection: string[];
-}
-
-const byFile = (dir: string, filenames: string[], ext: string = '.json'): string[] => {
-  return filenames
-    .filter(fn => {
-      if (!fn.endsWith(ext)) return false
-      let p = path.join(dir, fn)
-      return isFile(p)
-    })
-    .map(fn => fn.substring(0, fn.length - ext.length))
-}
-
-const byDir = (dir: string, filenames: string[]): string[] => {
-  return filenames.filter(fn => isDir(path.join(dir, fn)))
-}
-
-const getKeys = async (dir: string): Promise<IKeys> => {
-  const types: (keyof IKeys)[] = [ 'dict', 'list', 'set', 'collection' ]
-  let data: Partial<IKeys> = {}
-
-  for (let type of types) {
-    let keys: string[] = []
-    let target_dir = path.join(dir, type)
-    if (!isDir(target_dir)) continue
-    let items = await fs.readdir(target_dir)
-    if (type === 'collection') {
-      keys = byDir(target_dir, items)
-    } else {
-      keys = byFile(target_dir, items)
-    }
-
-    data[type] = keys.sort()
-  }
-
-  return data as IKeys
-}
-
-export default getKeys

+ 0 - 209
src/main/utils/db/core/type/collection.ts

@@ -1,209 +0,0 @@
-/**
- * collection
- * @author: oldj
- * @homepage: https://oldj.net
- */
-
-import * as fs from 'fs'
-import lodash from 'lodash'
-import * as path from 'path'
-import { DataTypeDocument } from '../../typings'
-import { asInt } from '../../utils/asType'
-import { clone } from '../../utils/clone'
-import LatDb from '../db'
-import Dict from './dict'
-import List from './list'
-
-type FilterPredicate = (item: any) => boolean
-
-interface Options {
-
-}
-
-export default class Collection {
-  name: string
-  private _db: LatDb
-  private _path: string
-  private _path_data: string
-  private options: Options = {}
-  private _meta: Dict
-  private _ids: List
-  private _docs: { [key: string]: Dict } = {}
-
-  constructor(db: LatDb, name: string) {
-    this._db = db
-    this.name = name
-    this._path = path.join(db.dir, 'collection', name)
-    this._path_data = path.join(this._path, 'data')
-
-    this._meta = new Dict('meta', this._path, db.options)
-    this._ids = new List('ids', this._path, db.options)
-  }
-
-  updateConfig(options: Partial<Options>) {
-    this.options = {
-      ...this.options,
-      ...options,
-    }
-  }
-
-  private async makeId(): Promise<string> {
-    let index = asInt(await this._meta.get('index'), 0)
-    if (index < 0) index = 0
-    index++
-    await this._meta.set('index', index)
-
-    // let ts = ((new Date()).getTime() % 1000).toString().padStart(3, '0')
-    // return `${index}${ts}`
-
-    return index.toString()
-  }
-
-  private getDoc(_id: string): Dict {
-    if (!this._docs[_id]) {
-      this._docs[_id] = new Dict(_id, this._path_data, this._db.options)
-    }
-
-    return this._docs[_id]
-  }
-
-  async count(): Promise<number> {
-    return (await this._ids.all()).length
-  }
-
-  async insert<T>(doc: T): Promise<T & { _id: string }> {
-    let _id = await this.makeId()
-    let doc2 = { ...doc, _id }
-    await this._insert(doc2)
-    return doc2
-  }
-
-  /**
-   * 类似 insert 方法,但不同的是如果传入的 doc 包含 _id 参数,侧会尝试更新对应的文档
-   * 如果不存在 _id 参数,或者 _id 对应的文档不存在,则新建
-   * 这个方法一般用在 db.loadJSON() 等场景
-   */
-  async _insert(doc: DataTypeDocument) {
-    let _id = doc._id
-    await this._ids.push(_id)
-    let d = this.getDoc(_id)
-    await d.update(doc)
-  }
-
-  async all<T>(keys: string | string[] = '*'): Promise<T[]> {
-    let data = await Promise.all((await this._ids.all()).map(async _id => {
-      let d = this.getDoc(_id)
-      let doc: T = await d.toJSON<T>()
-
-      if (Array.isArray(keys)) {
-        doc = lodash.pick(doc, keys) as T
-      }
-
-      return doc
-    }))
-
-    return data as T[]
-  }
-
-  async index<T>(index: number, keys: string | string[] = '*'): Promise<T | undefined> {
-    let _id = await this._ids.index(index)
-    if (!_id) return
-
-    return await this.find<T>(i => i._id === _id, keys)
-  }
-
-  async find<T>(predicate: FilterPredicate, keys: string | string[] = '*'): Promise<T | undefined> {
-    let _ids = await this._ids.all()
-
-    for (let _id of _ids) {
-      let d = this.getDoc(_id)
-      let doc: T = await d.toJSON<T>()
-
-      if (predicate(doc)) {
-        if (Array.isArray(keys)) {
-          doc = lodash.pick(doc, keys) as T
-        }
-
-        return doc
-      }
-    }
-  }
-
-  async filter<T>(predicate: FilterPredicate, keys: string | string[] = '*'): Promise<T[]> {
-    let _ids = await this._ids.all()
-    let list: T[] = []
-
-    for (let _id of _ids) {
-      let d = this.getDoc(_id)
-      let doc: T = await d.toJSON<T>()
-
-      if (predicate(doc)) {
-        if (Array.isArray(keys)) {
-          doc = lodash.pick(doc, keys) as T
-        }
-
-        list.push(doc)
-      }
-    }
-
-    return list
-  }
-
-  async update<T>(predicate: FilterPredicate, data: T): Promise<T[]> {
-    let items = await this.filter<DataTypeDocument>(predicate)
-    let out: T[] = []
-
-    for (let item of items) {
-      let { _id } = item
-      let d = this.getDoc(_id)
-      let doc: T = await d.toJSON<T>()
-
-      doc = {
-        ...doc,
-        ...data,
-        _id,
-      }
-
-      let i = await d.update<T>(doc)
-      out.push(i)
-    }
-
-    return out
-  }
-
-  async delete(predicate: FilterPredicate) {
-    while (true) {
-      let item = await this.find<DataTypeDocument>(predicate)
-      if (!item) break
-
-      let index = await this._ids.indexOf(item._id)
-      if (index === -1) continue
-
-      await this._ids.splice(index, 1)
-      let d = this.getDoc(item._id)
-      await d.remove()
-      delete this._docs[item._id]
-    }
-  }
-
-  async remove() {
-    // remove current collection
-    await this._meta.remove()
-    await this._ids.remove()
-    this._docs = {}
-    await fs.promises.rmdir(this._path, { recursive: true })
-  }
-
-  @clone
-  async _getMeta() {
-    return await this._meta.all()
-  }
-
-  @clone
-  async _setMeta(data: any) {
-    let keys = Object.keys(data)
-    for (let k of keys) {
-      await this._meta.set(k, data[k])
-    }
-  }
-}

+ 0 - 112
src/main/utils/db/core/type/dict.ts

@@ -1,112 +0,0 @@
-/**
- * hash
- * @author: oldj
- * @homepage: https://oldj.net
- */
-
-import * as path from 'path'
-import { DataTypeDict, IBasicOptions } from '../../typings'
-import { clone } from '../../utils/clone'
-import IO from '../io'
-
-interface Options extends IBasicOptions {
-}
-
-export default class Dict {
-  private _data: DataTypeDict | null = null
-  private _io: IO
-  private _path: string
-  name: string
-
-  constructor(name: string, dir: string, options: Options) {
-    this._path = path.join(dir, name + '.json')
-    this.name = name
-    this._io = new IO({
-      data_type: 'dict',
-      data_path: this._path,
-      debug: options.debug,
-      dump_delay: options.dump_delay,
-    })
-  }
-
-  private async ensure(): Promise<DataTypeDict> {
-    if (this._data === null) {
-      this._data = await this._io.load<DataTypeDict>()
-    }
-
-    return this._data
-  }
-
-  private dump() {
-    if (this._data === null) return
-    this._io.dump({ ...this._data })
-      .catch(e => console.error(e))
-  }
-
-  @clone
-  async get(key: string, default_value?: any): Promise<any> {
-    this._data = await this.ensure()
-
-    if (this._data.hasOwnProperty(key)) {
-      return this._data[key]
-    }
-
-    return default_value
-  }
-
-  @clone
-  async set(key: string, value: any) {
-    this._data = await this.ensure()
-    this._data[key] = value
-    this.dump()
-  }
-
-  @clone
-  async update<T>(obj: { [key: string]: any }): Promise<T> {
-    this._data = await this.ensure()
-    this._data = {
-      ...this._data,
-      ...obj,
-    }
-    this.dump()
-
-    return this._data as T
-  }
-
-  async keys(): Promise<string[]> {
-    if (this._data === null) {
-      this._data = await this._io.load<DataTypeDict>()
-    }
-
-    return Object.keys(this._data)
-  }
-
-  @clone
-  async all(): Promise<DataTypeDict> {
-    return await this.ensure()
-  }
-
-  @clone
-  async toJSON<T>(): Promise<T> {
-    return (await this.all()) as Promise<T>
-  }
-
-  async delete(key: string) {
-    this._data = await this.ensure()
-    if (!this._data.hasOwnProperty(key)) {
-      return
-    }
-    delete this._data[key]
-    this.dump()
-  }
-
-  async clear() {
-    this._data = {}
-    this.dump()
-  }
-
-  async remove() {
-    this._data = {}
-    await this._io.remove()
-  }
-}

+ 0 - 208
src/main/utils/db/core/type/list.ts

@@ -1,208 +0,0 @@
-/**
- * list
- * @author: oldj
- * @homepage: https://oldj.net
- */
-
-import * as path from 'path'
-import { DataTypeList, IBasicOptions } from '../../typings'
-import { clone } from '../../utils/clone'
-import IO from '../io'
-
-interface Options extends IBasicOptions {
-}
-
-type FilterPredicate = (item: any) => boolean
-type MapFunction = (item: any) => any
-
-export default class List {
-  private _data: DataTypeList | null = null
-  private _io: IO
-  private _path: string
-  name: string
-
-  constructor(name: string, root_dir: string, options: Options) {
-    this._path = path.join(root_dir, name + '.json')
-    this.name = name
-    this._io = new IO({
-      data_type: 'list',
-      data_path: this._path,
-      debug: options.debug,
-      dump_delay: options.dump_delay,
-    })
-  }
-
-  private async ensure(): Promise<DataTypeList> {
-    if (this._data === null) {
-      this._data = await this._io.load<DataTypeList>()
-    }
-
-    return this._data
-  }
-
-  private dump() {
-    if (this._data === null) return
-    this._io.dump([...this._data])
-      .catch(e => console.error(e))
-  }
-
-  @clone
-  async rpush(value: any) {
-    this._data = await this.ensure()
-    this._data.push(value)
-    this.dump()
-  }
-
-  @clone
-  async rpop(): Promise<any> {
-    this._data = await this.ensure()
-    let v = this._data.pop()
-    this.dump()
-
-    return v
-  }
-
-  @clone
-  async rextend(...values: any[]) {
-    this._data = await this.ensure()
-    this._data = [...this._data, ...values]
-    this.dump()
-  }
-
-  @clone
-  async lpush(value: any) {
-    this._data = await this.ensure()
-    this._data.unshift(value)
-    this.dump()
-  }
-
-  @clone
-  async lpop(): Promise<any> {
-    this._data = await this.ensure()
-    let v = this._data.shift()
-    this.dump()
-
-    return v
-  }
-
-  @clone
-  async lextend(...values: any[]) {
-    this._data = await this.ensure()
-    this._data = [...values, ...this._data]
-    this.dump()
-  }
-
-  async push(value: any) {
-    await this.rpush(value)
-  }
-
-  async pop(): Promise<any> {
-    return await this.rpop()
-  }
-
-  async extend(...values: any[]) {
-    await this.rextend(...values)
-  }
-
-  @clone
-  async all(): Promise<any[]> {
-    return await this.ensure()
-  }
-
-  @clone
-  async find(predicate: FilterPredicate): Promise<any | undefined> {
-    this._data = await this.ensure()
-    return this._data.find(predicate)
-  }
-
-  @clone
-  async filter(predicate: FilterPredicate): Promise<any[]> {
-    this._data = await this.ensure()
-    return this._data.filter(predicate)
-  }
-
-  @clone
-  async map(predicate: MapFunction): Promise<any[]> {
-    this._data = await this.ensure()
-    return this._data.map(predicate)
-  }
-
-  @clone
-  async index(index: number): Promise<any | undefined> {
-    this._data = await this.ensure()
-
-    if (index < 0) {
-      let idx = Math.abs(index)
-      let length = this._data.length
-      if (length < idx) {
-        return undefined
-      }
-
-      index = length - idx
-    }
-
-    return this._data[index]
-  }
-
-  async indexOf(predicate: string | number | boolean | null | FilterPredicate): Promise<number> {
-    this._data = await this.ensure()
-
-    if (typeof predicate === 'function') {
-      for (let i = 0; i < this._data.length; i++) {
-        if (predicate(this._data[i])) {
-          return i
-        }
-      }
-      return -1
-    } else {
-      return this._data.indexOf(predicate)
-    }
-  }
-
-  @clone
-  async slice(start: number, end?: number): Promise<any[]> {
-    this._data = await this.ensure()
-    let args = [start]
-    if (typeof end === 'number') {
-      args.push(end)
-    }
-    return this._data.slice(...args)
-  }
-
-  @clone
-  async splice(start: number, delete_count: number, ...insert_items: any[]): Promise<any[]> {
-    this._data = await this.ensure()
-    let v = this._data.splice(start, delete_count, ...insert_items)
-    this.dump()
-    return v
-  }
-
-  @clone
-  async delete(predicate: FilterPredicate): Promise<any[]> {
-    this._data = await this.filter(i => !predicate(i))
-    this.dump()
-
-    return this._data
-  }
-
-  @clone
-  async set(data: any[]) {
-    this._data = data
-    this.dump()
-  }
-
-  async clear() {
-    this._data = []
-    this.dump()
-  }
-
-  async remove() {
-    this._data = []
-    await this._io.remove()
-  }
-
-  async update(data: any[]) {
-    this._data = data
-    this.dump()
-  }
-}

+ 0 - 90
src/main/utils/db/core/type/set.ts

@@ -1,90 +0,0 @@
-/**
- * set
- * @author: oldj
- * @homepage: https://oldj.net
- */
-
-import * as path from 'path'
-import { DataTypeSet, DataTypeSetItem, IBasicOptions } from '../../typings'
-import { clone } from '../../utils/clone'
-import IO from '../io'
-
-interface Options extends IBasicOptions {
-}
-
-export default class LatSet {
-  private _data: DataTypeSet | null = null
-  private _io: IO
-  private _path: string
-  name: string
-
-  constructor(name: string, root_dir: string, options: Options) {
-    this._path = path.join(root_dir, name + '.json')
-    this.name = name
-    this._io = new IO({
-      data_type: 'set',
-      data_path: this._path,
-      debug: options.debug,
-      dump_delay: options.dump_delay,
-    })
-  }
-
-  private async ensure(): Promise<DataTypeSet> {
-    if (this._data === null) {
-      this._data = await this._io.load<DataTypeSet>()
-    }
-
-    return this._data
-  }
-
-  private dump() {
-    if (this._data === null) return
-    this._io.dump(Array.from(this._data))
-      .catch(e => console.error(e))
-  }
-
-  async add(value: DataTypeSetItem) {
-    this._data = await this.ensure()
-    this._data.add(value)
-    this.dump()
-  }
-
-  async delete(value: DataTypeSetItem) {
-    this._data = await this.ensure()
-    this._data.delete(value)
-    this.dump()
-  }
-
-  async has(value: DataTypeSetItem): Promise<boolean> {
-    this._data = await this.ensure()
-    return this._data.has(value)
-  }
-
-  async all(): Promise<DataTypeSetItem[]> {
-    this._data = await this.ensure()
-    return Array.from(this._data)
-  }
-
-  async clear() {
-    this._data = new Set()
-    this.dump()
-  }
-
-  @clone
-  async set(data: any[]) {
-    let s = new Set<DataTypeSetItem>()
-    data.map(i => s.add(i))
-    this._data = s
-    this.dump()
-  }
-
-  async remove() {
-    this._data = new Set()
-    await this._io.remove()
-  }
-
-  async update(data: DataTypeSetItem[]) {
-    this._data = new Set(data)
-    this.dump()
-  }
-}

+ 0 - 9
src/main/utils/db/index.ts

@@ -1,9 +0,0 @@
-/**
- * index
- * @author: oldj
- * @homepage: https://oldj.net
- */
-
-import LatDb from './core/db'
-
-export default LatDb

+ 0 - 12
src/main/utils/db/settings.ts

@@ -1,12 +0,0 @@
-/**
- * settings
- * @author: oldj
- * @homepage: https://oldj.net
- */
-
-const settings = {
-  // 数据实际写入硬盘时的间隔,防止对硬盘的写操作过于频繁
-  io_dump_delay: 1000,
-}
-
-export default settings

+ 0 - 56
src/main/utils/db/typings.ts

@@ -1,56 +0,0 @@
-/**
- * typings.d.ts
- * @author: oldj
- * @homepage: https://oldj.net
- */
-
-export interface IBasicOptions {
-  debug: boolean;
-  dump_delay: number;
-  ignore_error: boolean;
-
-  [key: string]: any;
-}
-
-export interface DataTypeDict {
-  [key: string]: any;
-}
-
-export interface IDictsDumpJSON {
-  [key: string]: DataTypeDict;
-}
-
-export type DataTypeList = any[]
-
-export interface IListsDumpJSON {
-  [key: string]: DataTypeList;
-}
-
-export type DataTypeSetItem = string | number | boolean | null
-export type DataTypeSet = Set<DataTypeSetItem>
-
-export interface ISetsDumpJSON {
-  [key: string]: DataTypeSetItem[];
-}
-
-export interface DataTypeDocument {
-  _id: string;
-
-  [key: string]: any;
-}
-
-export interface ICollectionsDumpJSON {
-  [key: string]: {
-    meta?: {
-      [key: string]: any;
-    };
-    data: DataTypeDocument[];
-  };
-}
-
-export interface IDbDataJSON {
-  dict?: IDictsDumpJSON;
-  list?: IListsDumpJSON;
-  set?: ISetsDumpJSON;
-  collection?: ICollectionsDumpJSON;
-}

+ 0 - 10
src/main/utils/db/utils/asType.ts

@@ -1,10 +0,0 @@
-/**
- * asType
- * @author: oldj
- * @homepage: https://oldj.net
- */
-
-export const asInt = (value: any, default_value?: number): number => {
-  value = parseInt(value)
-  return isNaN(value) ? default_value : value
-}

+ 0 - 22
src/main/utils/db/utils/clone.ts

@@ -1,22 +0,0 @@
-/**
- * clone
- * @author: oldj
- * @homepage: https://oldj.net
- */
-
-import lodash from 'lodash'
-
-export const clone = (target: any, propertyName: string, descriptor: PropertyDescriptor) => {
-  const method = descriptor.value
-  descriptor.value = async function (...args: any[]) {
-    let result = await method.apply(this, args.map(i => {
-      return (i && typeof i === 'object') ? lodash.cloneDeep(i) : i
-    }))
-    if (result && typeof result === 'object') {
-      result = lodash.cloneDeep(result)
-    }
-
-    return result
-  }
-  return descriptor
-}

+ 0 - 21
src/main/utils/db/utils/fs2.ts

@@ -1,21 +0,0 @@
-/**
- * ensureDir
- * @author: oldj
- * @homepage: https://oldj.net
- */
-
-import fs from 'fs'
-import mkdirp from 'mkdirp'
-
-export const isDir = (dir_path: string) => {
-  return fs.existsSync(dir_path) && fs.lstatSync(dir_path).isDirectory()
-}
-
-export const isFile = (dir_path: string) => {
-  return fs.existsSync(dir_path) && fs.lstatSync(dir_path).isFile()
-}
-
-export const ensureDir = async (dir_path: string) => {
-  if (isDir(dir_path)) return
-  await mkdirp(dir_path)
-}

+ 0 - 7
src/main/utils/db/utils/wait.ts

@@ -1,7 +0,0 @@
-/**
- * wait
- * @author: oldj
- * @homepage: https://oldj.net
- */
-
-export default (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))

+ 1 - 1
src/version.json

@@ -1 +1 @@
-[4, 0, 0, 6025]
+[4, 0, 0, 6026]