This repository was archived by the owner on Mar 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 528
Expand file tree
/
Copy pathtableHelperFunctions.ts
More file actions
237 lines (197 loc) · 7.93 KB
/
Copy pathtableHelperFunctions.ts
File metadata and controls
237 lines (197 loc) · 7.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import * as fs from 'fs'
import * as fsPromises from 'fs/promises'
import { BrowserWindow } from 'electron'
import { chunkMarkdownByHeadingsAndByCharsIfBigWithPositions } from '../common/chunking'
import {
GetFilesInfoList,
flattenFileInfoTree,
readFile,
updateFileListForRenderer,
startWatchingDirectory,
} from '../filesystem/filesystem'
import { FileInfo, FileInfoTree, RenameFileProps } from '../filesystem/types'
import LanceDBTableWrapper, { convertRecordToDBType } from './lanceTableWrapper'
import { DBEntry, DatabaseFields } from './schema'
import WindowsManager from '../common/windowManager'
const convertFileTypeToDBType = async (file: FileInfo): Promise<DBEntry[]> => {
const fileContent = readFile(file.path)
const chunksWithPositions = await chunkMarkdownByHeadingsAndByCharsIfBigWithPositions(fileContent)
const entries = chunksWithPositions.map((chunkInfo, index) => ({
notepath: file.path,
content: chunkInfo.chunk,
subnoteindex: index,
timeadded: new Date(),
filemodified: file.dateModified,
filecreated: file.dateCreated,
startPos: chunkInfo.pos,
}))
return entries
}
export const handleFileRename = async (
windowsManager: WindowsManager,
windowInfo: { vaultDirectoryForWindow: string; dbTableClient: any },
renameFileProps: RenameFileProps,
sender: Electron.WebContents,
): Promise<void> => {
windowsManager.watcher?.unwatch(windowInfo.vaultDirectoryForWindow)
try {
await fsPromises.access(renameFileProps.newFilePath)
throw new Error(`A file already exists at destination: ${renameFileProps.newFilePath}`)
} catch (error) {
// If error is ENOENT (file doesn't exist), proceed with rename
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') {
throw error
}
}
if (process.platform === 'win32') {
await windowsManager.watcher?.close()
await new Promise<void>((resolve, reject) => {
fs.rename(renameFileProps.oldFilePath, renameFileProps.newFilePath, (err) => {
if (err) {
reject(err)
return
}
const win = BrowserWindow.fromWebContents(sender)
if (win) {
// eslint-disable-next-line no-param-reassign
windowsManager.watcher = startWatchingDirectory(win, windowInfo.vaultDirectoryForWindow)
updateFileListForRenderer(win, windowInfo.vaultDirectoryForWindow)
}
resolve()
})
})
} else {
await new Promise<void>((resolve, reject) => {
fs.rename(renameFileProps.oldFilePath, renameFileProps.newFilePath, (err) => {
if (err) {
reject(err)
return
}
windowsManager.watcher?.add(windowInfo.vaultDirectoryForWindow)
resolve()
})
})
}
await windowInfo.dbTableClient.updateDBItemsWithNewFilePath(renameFileProps.oldFilePath, renameFileProps.newFilePath)
}
export const convertFileInfoListToDBItems = async (filesInfoList: FileInfo[]): Promise<DBEntry[][]> => {
const promises = filesInfoList.map(convertFileTypeToDBType)
const filesAsChunksToAddToDB = await Promise.all(promises)
return filesAsChunksToAddToDB
}
const getTableAsArray = async (table: LanceDBTableWrapper): Promise<{ notepath: string; filemodified: Date }[]> => {
const nonEmptyResults = await table.lanceTable
.filter(`${DatabaseFields.NOTE_PATH} != ''`)
.select([DatabaseFields.NOTE_PATH, DatabaseFields.FILE_MODIFIED])
.execute()
const mapped = nonEmptyResults.map(convertRecordToDBType<DBEntry>)
return mapped as { notepath: string; filemodified: Date }[]
}
const areChunksMissingFromTable = (
chunksToCheck: DBEntry[],
tableArray: { notepath: string; filemodified: Date }[],
): boolean => {
// checking whether th
if (chunksToCheck.length === 0) {
// if there are no chunks and we are checking whether the table
return false
}
if (chunksToCheck[0].content === '') {
return false
}
// then we'd check if the filepaths are not present in the table at all:
const { notepath } = chunksToCheck[0]
const itemsAlreadyInTable = tableArray.filter((item) => item.notepath === notepath)
if (itemsAlreadyInTable.length === 0) {
// if we find no items in the table with the same notepath, then we should add the chunks to the table
return true
}
return chunksToCheck[0].filemodified > itemsAlreadyInTable[0].filemodified
}
const computeDbItemsToAddOrUpdate = async (
filesInfoList: FileInfo[],
tableArray: { notepath: string; filemodified: Date }[],
): Promise<DBEntry[][]> => {
const filesAsChunks = await convertFileInfoListToDBItems(filesInfoList)
const fileChunksMissingFromTable = filesAsChunks.filter((chunksBelongingToFile) =>
areChunksMissingFromTable(chunksBelongingToFile, tableArray),
)
return fileChunksMissingFromTable
}
const computeDBItemsToRemoveFromTable = async (
filesInfoList: FileInfo[],
tableArray: { notepath: string; filemodified: Date }[],
): Promise<{ notepath: string; filemodified: Date }[]> => {
const itemsInTableAndNotInFilesInfoList = tableArray.filter(
(item) => !filesInfoList.some((file) => file.path === item.notepath),
)
return itemsInTableAndNotInFilesInfoList
}
const convertFileTreeToDBEntries = async (tree: FileInfoTree): Promise<DBEntry[]> => {
const flattened = flattenFileInfoTree(tree)
const promises = flattened.map(convertFileTypeToDBType)
const entries = await Promise.all(promises)
return entries.flat()
}
export const removeFileTreeFromDBTable = async (
dbTable: LanceDBTableWrapper,
fileTree: FileInfoTree,
): Promise<void> => {
const flattened = flattenFileInfoTree(fileTree)
const filePaths = flattened.map((x) => x.path)
await dbTable.deleteDBItemsByFilePaths(filePaths)
}
export const updateFileInTable = async (dbTable: LanceDBTableWrapper, filePath: string): Promise<void> => {
await dbTable.deleteDBItemsByFilePaths([filePath])
const content = readFile(filePath)
const chunksWithPositions = await chunkMarkdownByHeadingsAndByCharsIfBigWithPositions(content)
const stats = fs.statSync(filePath)
const dbEntries = chunksWithPositions.map((chunkInfo, index) => ({
notepath: filePath,
content: chunkInfo.chunk,
subnoteindex: index,
timeadded: new Date(), // time now
filemodified: stats.mtime,
filecreated: stats.birthtime,
startPos: chunkInfo.pos,
}))
await dbTable.add(dbEntries)
}
export const RepopulateTableWithMissingItems = async (
table: LanceDBTableWrapper,
directoryPath: string,
onProgress?: (progress: number) => void,
) => {
const filesInfoTree = GetFilesInfoList(directoryPath)
const tableArray = await getTableAsArray(table)
const itemsToRemove = await computeDBItemsToRemoveFromTable(filesInfoTree, tableArray)
const filePathsToRemove = itemsToRemove.map((x) => x.notepath)
await table.deleteDBItemsByFilePaths(filePathsToRemove)
const dbItemsToAdd = await computeDbItemsToAddOrUpdate(filesInfoTree, tableArray)
if (dbItemsToAdd.length === 0) {
if (onProgress) onProgress(1)
return
}
const flattenedItemsToAdd = dbItemsToAdd.flat()
await table.add(flattenedItemsToAdd, onProgress)
if (onProgress) onProgress(1)
}
export const addFileTreeToDBTable = async (dbTable: LanceDBTableWrapper, fileTree: FileInfoTree): Promise<void> => {
const dbEntries = await convertFileTreeToDBEntries(fileTree)
await dbTable.add(dbEntries)
}
export function formatTimestampForLanceDB(date: Date): string {
const year = date.getFullYear()
const month = date.getMonth() + 1 // getMonth() is zero-based
const day = date.getDate()
const hours = date.getHours()
const minutes = date.getMinutes()
const seconds = date.getSeconds()
// Pad single digits with leading zeros
const monthPadded = month.toString().padStart(2, '0')
const dayPadded = day.toString().padStart(2, '0')
const hoursPadded = hours.toString().padStart(2, '0')
const minutesPadded = minutes.toString().padStart(2, '0')
const secondsPadded = seconds.toString().padStart(2, '0')
return `timestamp '${year}-${monthPadded}-${dayPadded} ${hoursPadded}:${minutesPadded}:${secondsPadded}'`
}