jexidb 2.2.0 → 2.2.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jexidb",
3
- "version": "2.2.0",
3
+ "version": "2.2.2",
4
4
  "type": "module",
5
5
  "description": "JexiDB is a pure JS NPM library for managing data on disk efficiently, without the need for a server.",
6
6
  "main": "./dist/Database.cjs",
package/src/Database.mjs CHANGED
@@ -4056,12 +4056,23 @@ class Database extends EventEmitter {
4056
4056
  this.termManager.termMappingFields &&
4057
4057
  this.termManager.termMappingFields.includes(fieldName)
4058
4058
 
4059
+ // CRITICAL FIX: Ensure index is loaded before accessing data
4060
+ // After idle unload, index.data[fieldName] becomes {} (truthy but empty)
4061
+ // Use _ensureLazyIndexLoaded() to reload from disk instead of _rebuildIndexesIfNeeded()
4062
+ // which requires allowIndexRebuild=true and rebuilds from scratch
4063
+ if (!this.indexManager.indexLoaded) {
4064
+ await this._ensureLazyIndexLoaded()
4065
+ }
4066
+
4059
4067
  // Access the index for this field
4060
4068
  const fieldIndex = this.indexManager.index.data[fieldName]
4061
- if (!fieldIndex) {
4069
+ if (!fieldIndex || Object.keys(fieldIndex).length === 0) {
4062
4070
  return []
4063
4071
  }
4064
4072
 
4073
+ // Reset idle unload timer since we're using the index
4074
+ this.indexManager.markIndexUsed()
4075
+
4065
4076
  // Accumulate scores for each line number
4066
4077
  const scoreMap = new Map()
4067
4078
  const countMap = opts.mode === 'avg' ? new Map() : null
@@ -103,6 +103,11 @@ export default class IndexManager {
103
103
  console.log(`🧹 IndexManager.unload: Unloading index data after ${this.indexIdleUnloadMs}ms idle`)
104
104
  }
105
105
 
106
+ // Mark rebuild as needed so score() can trigger lazy reload
107
+ if (this.database) {
108
+ this.database._indexRebuildNeeded = true
109
+ }
110
+
106
111
  this._clearIdleUnloadTimer()
107
112
  const newData = {}
108
113
  for (const field of this.indexedFields) {
@@ -2336,6 +2341,27 @@ export default class IndexManager {
2336
2341
  this.markIndexUsed()
2337
2342
  }
2338
2343
 
2344
+ /**
2345
+ * Restored public API: return the indexed terms (keys) currently held in memory
2346
+ * for a given column.
2347
+ *
2348
+ * IMPORTANT: This method reads ONLY the in-memory index. It never triggers a lazy
2349
+ * reload and never performs any disk I/O. If the index was idle-unloaded
2350
+ * (`indexLoaded === false`), it returns an empty Set. Callers that need the
2351
+ * on-disk index after an unload must first ensure it is loaded again, e.g.
2352
+ * `await database._ensureLazyIndexLoaded()`.
2353
+ *
2354
+ * @param {string} column - Indexed field name (e.g. 'nameTerms', 'groupTerms', 'category')
2355
+ * @returns {Set<string>} Terms currently indexed for the column (empty Set if the
2356
+ * column is not indexed or the index is not loaded)
2357
+ */
2358
+ readColumnIndex(column) {
2359
+ if (!this.index || !this.index.data || !this.index.data[column]) {
2360
+ return new Set()
2361
+ }
2362
+ return new Set(Object.keys(this.index.data[column]))
2363
+ }
2364
+
2339
2365
  /**
2340
2366
  * Convert index to JSON-serializable format for debugging and export
2341
2367
  * This resolves the issue where Sets appear as empty objects in JSON.stringify