@1-/scan 0.1.8 → 0.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  ---
4
4
 
5
5
  <a id="en"></a>
6
- # @1-/scan : SQLite-backed incremental directory scanner
6
+ # @1-/scan : SQLite-backed incremental directory file scanner
7
7
 
8
8
  Incrementally scans directory files, compares file sizes and modification times to detect changes, synchronizes metadata to SQLite database, and returns list of changed relative paths.
9
9
 
@@ -13,7 +13,7 @@ Incrementally scans directory files, compares file sizes and modification times
13
13
  - **Key Length Optimization**: Stores raw bytes for paths up to 16 bytes. Converts longer paths into 16-byte MD5 hashes to optimize database index space and query performance.
14
14
  - **Memory Optimization**: Uses BinMap and BinSet to store binary keys in memory, avoiding string decoding overhead and reducing memory footprint.
15
15
  - **Transactional Integrity**: Performs metadata updates and deletions in database transactions to ensure consistency.
16
- - **Zero Configuration**: Integrates @1-/sqlite to initialize database schema and manage database connections automatically.
16
+ - **Auto Configuration**: Integrates @1-/sqlite to initialize database schema and manage database connections automatically, updating .gitignore when new database is detected.
17
17
 
18
18
  ## 2. Usage
19
19
 
@@ -58,12 +58,12 @@ db.close();
58
58
 
59
59
  Main entry orchestrates modules to scan directories and synchronize metadata.
60
60
 
61
- ![](https://fastly.jsdelivr.net/gh/webc-fs/-@-a/YhYlWMxhnYfN4bgT4FrA.svg)
61
+ ![](https://fastly.jsdelivr.net/gh/webc-fs/-@K0/4mOzfZ1TGQuIAaily4Jw.svg)
62
62
 
63
- 1. **Initialize Connection**: Calls `@1-/sqlite` to open SQLite database.
64
- 2. **Load Records**: `load.js` checks `scanMtimeLen` table, creates it if missing. Reads stored hashes, sizes, and modification times to restore memory mappings.
65
- 3. **Compare Files**: Iterates over input file list. Maps paths to 16-byte binary keys via `@1-/hash`. Adds files with mismatched size or modification time to update list.
66
- 4. **Delete and Return**: Deletes absent or unscanned records in transaction. Returns changed paths list and `upsert` function for persistence.
63
+ 1. **Initialize Connection**: Calls `@1-/sqlite` to open SQLite database. Updates `.gitignore` in the database directory if the database is newly created to prevent tracking.
64
+ 2. **Load Records**: `load.js` checks and creates `scanMtimeLen` table. Reads stored hashes, sizes, and modification times to restore memory mappings inside `BinMap`.
65
+ 3. **Compare Files**: `scan.js` iterates over input file list, calling `stat.js` for metadata and utilizing `@1-/hash` to map paths to 16-byte binary keys. Adds files with mismatched size or modification time to change list.
66
+ 4. **Delete and Return**: `rm.js` deletes absent or unscanned records in transaction. Returns changed paths list and `upsert` function (provided by `upsert.js`) for persistence, supporting automatic resource disposal.
67
67
 
68
68
  ## 4. Tech Stack
69
69
 
@@ -78,9 +78,13 @@ Main entry orchestrates modules to scan directories and synchronize metadata.
78
78
  ```text
79
79
  .
80
80
  ├── src
81
- │ ├── _.js # Core flow controller
82
- │ ├── load.js # Table schema initialization and loader
83
- └── save.js # Bulk update and delete helper
81
+ │ ├── _.js # Core controller flow
82
+ │ ├── load.js # Table initialization and loading
83
+ ├── rm.js # Batch deletion of metadata
84
+ │ ├── save.js # Batch storage and updates
85
+ │ ├── scan.js # Scans and compares files
86
+ │ ├── stat.js # Retrieves file metadata and path hash
87
+ │ └── upsert.js # Single-record updates and auto-dispose
84
88
  └── tests # Unit tests
85
89
  ```
86
90
 
@@ -98,17 +102,17 @@ This library is developed by [WebC.site](https://webc.site).
98
102
  ---
99
103
 
100
104
  <a id="zh"></a>
101
- # @1-/scan : 基于 SQLite 的目录增量扫描工具
105
+ # @1-/scan : 基于 SQLite 的目录文件增量扫描器
102
106
 
103
107
  增量扫描目录文件,比对文件大小与修改时间检测变更,同步元数据至 SQLite 数据库,返回已变更相对路径列表。
104
108
 
105
109
  ## 1. 功能介绍
106
110
 
107
111
  - **增量扫描**:比对大小与修改时间,过滤未变更文件,减少磁盘读写。
108
- - **键长优化**:路径长度不大于 16 字节存储原始字节,超出 16 字节转换为 16 字节 MD5 值,优化索引空间与查询性能。
112
+ - **键长优化**:路径长度不大于 16 字节时存储原始字节,超出 16 字节转换为 16 字节 MD5 值,优化索引空间与查询性能。
109
113
  - **内存优化**:使用 BinMap 与 BinSet 存储二进制键,避免字符串解码,降低内存占用。
110
114
  - **事务保障**:元数据变更与删除操作合并在数据库事务中执行,确保数据一致性。
111
- - **零配置**:集成 @1-/sqlite,自动初始化数据库表结构,管理数据库连接。
115
+ - **自动配置**:集成 @1-/sqlite,自动初始化数据库表结构,并在检测到新数据库时自动更新 .gitignore。
112
116
 
113
117
  ## 2. 使用演示
114
118
 
@@ -153,12 +157,12 @@ db.close();
153
157
 
154
158
  主入口调度各模块,协作完成目录扫描与数据同步。
155
159
 
156
- ![](https://fastly.jsdelivr.net/gh/webc-fs/-@e2/-ae7ErmK8GpR3UFyhddA.svg)
160
+ ![](https://fastly.jsdelivr.net/gh/webc-fs/-@EB/fMlrHP9fXkA64lXr2r2Q.svg)
157
161
 
158
- 1. **初始化连接**:调用 `@1-/sqlite` 打开 SQLite 数据库。
159
- 2. **加载记录**:`load.js` 检查 `scanMtimeLen` 表,表不存在则自动创建。读取已记录哈希、大小及修改时间,恢复内存映射。
160
- 3. **比对文件**:遍历输入文件列表,利用 `@1-/hash` 将路径映射为 16 字节二进制键。若大小或修改时间不一致,则加入变更列表。
161
- 4. **删除与返回**:在事务中批量删除已被物理移除或不再扫描记录。返回变更路径列表与 `upsert` 函数,供外部持久化。
162
+ 1. **初始化连接**:调用 `@1-/sqlite` 打开 SQLite 数据库。若数据库为新创建,自动更新所在目录的 `.gitignore` 阻断提交。
163
+ 2. **加载记录**:`load.js` 检查并创建 `scanMtimeLen` 表。读取已记录的哈希、大小及修改时间,恢复至内存映射 `BinMap` 中。
164
+ 3. **比对文件**:`scan.js` 遍历输入文件列表,调用 `stat.js` 获取元数据,并利用 `@1-/hash` 将路径映射为 16 字节二进制键。比对大小或修改时间,差异项归入变更列表。
165
+ 4. **删除与返回**:`rm.js` 在事务中批量删除物理移除或不再扫描的记录。返回变更路径列表与 `upsert` 函数(`upsert.js` 提供),用以更新数据库,支持自动释放。
162
166
 
163
167
  ## 4. 技术栈
164
168
 
@@ -168,14 +172,18 @@ db.close();
168
172
  - **@3-/vb**:Varint 变长整型编码与解码器。
169
173
  - **@3-/binmap / @3-/binset**:基于 Rust 与 WebAssembly 的高效二进制键容器。
170
174
 
171
- ## 5. Code Structure
175
+ ## 5. 代码结构
172
176
 
173
177
  ```text
174
178
  .
175
179
  ├── src
176
180
  │ ├── _.js # 核心控制流程
177
181
  │ ├── load.js # 元数据表初始化与加载
178
- └── save.js # 批量更新与删除工具
182
+ ├── rm.js # 批量删除元数据
183
+ │ ├── save.js # 批量存储元数据
184
+ │ ├── scan.js # 扫描与比对文件
185
+ │ ├── stat.js # 获取文件元数据及路径哈希
186
+ │ └── upsert.js # 逐个更新与自动关闭
179
187
  └── tests # 单元测试
180
188
  ```
181
189
 
package/_.js CHANGED
@@ -1,63 +1,20 @@
1
1
  import sqlite from "@1-/sqlite";
2
- import tx from "@1-/sqlite/tx.js";
3
2
  import { BinMap } from "@3-/binmap";
4
3
  import vbE from "@3-/vb/vbE.js";
5
- import load from "./load.js";
6
- import { stat as fsStat } from "node:fs/promises";
7
- import { join } from "node:path";
8
- import int from "@3-/int";
9
- import strmd5 from "@1-/hash/strmd5.js";
10
- import { BinSet } from "@3-/binset";
11
- import u8eq from "@3-/u8/u8eq.js";
12
4
  import { availableParallelism } from "node:os";
13
5
  import pLimit from "@3-/plimit";
14
-
15
- const stat = async (dir, rel_path) => {
16
- const { size, mtimeMs: mtime_ms } = await fsStat(join(dir, rel_path));
17
- return [size, int(mtime_ms), strmd5(rel_path)];
18
- },
19
- scanFiles = async (dir, files, existing, limit) => {
20
- const scanned = new BinSet(),
21
- update = [];
22
- await Promise.all(
23
- files.map((rel_path) =>
24
- limit(async () => {
25
- try {
26
- const [size, mtime, hash] = await stat(dir, rel_path),
27
- val = existing.get(hash);
28
-
29
- scanned.add(hash);
30
-
31
- if (!val || !u8eq(val, vbE([size, mtime]))) {
32
- update.push(rel_path);
33
- }
34
- } catch {}
35
- }),
36
- ),
37
- );
38
- return [scanned, update];
39
- },
40
- rmHashes = (db, rm) => {
41
- if (rm.length > 0) {
42
- tx(db, () => {
43
- const del = db.prepare("DELETE FROM scanMtimeLen WHERE hash=?");
44
- rm.forEach((hash) => del.run(hash));
45
- });
46
- }
47
- },
48
- newUpsert = (db, dir) => {
49
- const insert = db.prepare("INSERT OR REPLACE INTO scanMtimeLen(hash,size,mtime)VALUES(?,?,?)"),
50
- upsert = async (rel_path) => {
51
- try {
52
- const [size, mtime, hash] = await stat(dir, rel_path);
53
- insert.run(hash, size, mtime);
54
- } catch {}
55
- };
56
- upsert[Symbol.dispose] = () => db.close();
57
- return upsert;
58
- };
6
+ import upsertGitignore from "@1-/upsert_gitignore";
7
+ import { existsSync } from "node:fs";
8
+ import { join, dirname, basename } from "node:path";
9
+ import load from "./load.js";
10
+ import scan from "./scan.js";
11
+ import rm from "./rm.js";
12
+ import upsert from "./upsert.js";
59
13
 
60
14
  export default async (dir, db_path, files) => {
15
+ if (!existsSync(db_path)) {
16
+ upsertGitignore(join(dirname(db_path), ".gitignore"), basename(db_path));
17
+ }
61
18
  const db = sqlite(db_path),
62
19
  existing = new BinMap(),
63
20
  db_rows = load(db),
@@ -65,10 +22,10 @@ export default async (dir, db_path, files) => {
65
22
 
66
23
  db_rows.forEach(({ hash, size, mtime }) => existing.set(hash, vbE([size, mtime])));
67
24
 
68
- const [scanned, update] = await scanFiles(dir, files, existing, limit),
69
- rm = db_rows.filter(({ hash }) => !scanned.has(hash)).map(({ hash }) => hash);
25
+ const [scanned, update] = await scan(dir, files, existing, limit),
26
+ rm_hashes = db_rows.filter(({ hash }) => !scanned.has(hash)).map(({ hash }) => hash);
70
27
 
71
- rmHashes(db, rm);
28
+ rm(db, rm_hashes);
72
29
 
73
- return [update, newUpsert(db, dir)];
30
+ return [update, upsert(db, dir)];
74
31
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@1-/scan",
3
- "version": "0.1.8",
3
+ "version": "0.1.9",
4
4
  "description": "Incrementally scan directory files and track metadata in SQLite / 增量扫描目录文件并使用 SQLite 记录元数据",
5
5
  "keywords": [
6
6
  "directory",
@@ -30,6 +30,7 @@
30
30
  "@3-/plimit": "^0.1.3",
31
31
  "@3-/u8": "^0.1.2",
32
32
  "@3-/utf8": "^0.1.1",
33
- "@3-/vb": "^0.1.6"
33
+ "@3-/vb": "^0.1.6",
34
+ "@1-/upsert_gitignore": "^0.1.3"
34
35
  }
35
36
  }
package/rm.js ADDED
@@ -0,0 +1,10 @@
1
+ import tx from "@1-/sqlite/tx.js";
2
+
3
+ export default (db, rm) => {
4
+ if (rm.length > 0) {
5
+ tx(db, () => {
6
+ const del = db.prepare("DELETE FROM scanMtimeLen WHERE hash=?");
7
+ rm.forEach((hash) => del.run(hash));
8
+ });
9
+ }
10
+ };
package/scan.js ADDED
@@ -0,0 +1,26 @@
1
+ import { BinSet } from "@3-/binset";
2
+ import u8eq from "@3-/u8/u8eq.js";
3
+ import vbE from "@3-/vb/vbE.js";
4
+ import stat from "./stat.js";
5
+
6
+ export default async (dir, files, existing, limit) => {
7
+ const scanned = new BinSet(),
8
+ update = [];
9
+ await Promise.all(
10
+ files.map((rel_path) =>
11
+ limit(async () => {
12
+ try {
13
+ const [size, mtime, hash] = await stat(dir, rel_path),
14
+ val = existing.get(hash);
15
+
16
+ scanned.add(hash);
17
+
18
+ if (!val || !u8eq(val, vbE([size, mtime]))) {
19
+ update.push(rel_path);
20
+ }
21
+ } catch {}
22
+ }),
23
+ ),
24
+ );
25
+ return [scanned, update];
26
+ };
package/stat.js ADDED
@@ -0,0 +1,9 @@
1
+ import { stat as fsStat } from "node:fs/promises";
2
+ import { join } from "node:path";
3
+ import int from "@3-/int";
4
+ import strmd5 from "@1-/hash/strmd5.js";
5
+
6
+ export default async (dir, rel_path) => {
7
+ const { size, mtimeMs: mtime_ms } = await fsStat(join(dir, rel_path));
8
+ return [size, int(mtime_ms), strmd5(rel_path)];
9
+ };
package/upsert.js ADDED
@@ -0,0 +1,13 @@
1
+ import stat from "./stat.js";
2
+
3
+ export default (db, dir) => {
4
+ const insert = db.prepare("INSERT OR REPLACE INTO scanMtimeLen(hash,size,mtime)VALUES(?,?,?)"),
5
+ upsert = async (rel_path) => {
6
+ try {
7
+ const [size, mtime, hash] = await stat(dir, rel_path);
8
+ insert.run(hash, size, mtime);
9
+ } catch {}
10
+ };
11
+ upsert[Symbol.dispose] = () => db.close();
12
+ return upsert;
13
+ };