RecapAnon commited on
Commit
b8ee0ee
·
1 Parent(s): e1426f8

Remove useless importDatabaseFromString.

Browse files
Files changed (1) hide show
  1. index.js +16 -33
index.js CHANGED
@@ -32,36 +32,6 @@ async function getNativeDB() {
32
  request.onerror = () => reject(request.error);
33
  });
34
  }
35
- /**
36
- * Import database data from a JSON string
37
- * @param {string} jsonString - JSON string containing database data
38
- * @returns {Promise<boolean>} True if successful
39
- */
40
- async function importDatabaseFromString(jsonString) {
41
- if (!vectorDB) {
42
- console.warn('VectorDB not initialized. Cannot import.');
43
- return false;
44
- }
45
-
46
- try {
47
- const nativeDB = await getNativeDB();
48
-
49
- return new Promise((resolve, reject) => {
50
- importFromJsonString(nativeDB, jsonString, (err) => {
51
- if (err) {
52
- console.error('Error importing database:', err);
53
- reject(err);
54
- } else {
55
- console.log('Database imported successfully');
56
- resolve(true);
57
- }
58
- });
59
- });
60
- } catch (error) {
61
- console.error('Error importing database:', error);
62
- return false;
63
- }
64
- }
65
 
66
  async function loadLargeTextFile(url) {
67
  const response = await fetch(url);
@@ -75,7 +45,6 @@ async function loadLargeTextFile(url) {
75
  chunks.push(value);
76
  }
77
  const fullBuffer = new Uint8Array(chunks.reduce((total, chunk) => total + chunk.length, 0));
78
- console.warn(chunks.length);
79
  let offset = 0;
80
  for (const chunk of chunks) {
81
  fullBuffer.set(chunk, offset);
@@ -83,7 +52,7 @@ async function loadLargeTextFile(url) {
83
  }
84
 
85
  const decoder = new TextDecoder('utf-8');
86
- databaseFileContents = decoder.decode(fullBuffer);
87
  }
88
 
89
  async function initializeVectorDB() {
@@ -108,6 +77,8 @@ async function initializeVectorDB() {
108
  ];
109
 
110
  try {
 
 
111
  // Load all chunk files concurrently
112
  const chunkPromises = chunkFiles.map(file => loadLargeTextFile(file));
113
  const chunkContents = await Promise.all(chunkPromises);
@@ -116,7 +87,19 @@ async function initializeVectorDB() {
116
  for (const contents of chunkContents) {
117
  if (contents) {
118
  console.log('Processing chunk, characters:', contents.length);
119
- await importDatabaseFromString(contents);
 
 
 
 
 
 
 
 
 
 
 
 
120
  }
121
  }
122
 
 
32
  request.onerror = () => reject(request.error);
33
  });
34
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
  async function loadLargeTextFile(url) {
37
  const response = await fetch(url);
 
45
  chunks.push(value);
46
  }
47
  const fullBuffer = new Uint8Array(chunks.reduce((total, chunk) => total + chunk.length, 0));
 
48
  let offset = 0;
49
  for (const chunk of chunks) {
50
  fullBuffer.set(chunk, offset);
 
52
  }
53
 
54
  const decoder = new TextDecoder('utf-8');
55
+ return decoder.decode(fullBuffer);
56
  }
57
 
58
  async function initializeVectorDB() {
 
77
  ];
78
 
79
  try {
80
+ const nativeDB = await getNativeDB();
81
+
82
  // Load all chunk files concurrently
83
  const chunkPromises = chunkFiles.map(file => loadLargeTextFile(file));
84
  const chunkContents = await Promise.all(chunkPromises);
 
87
  for (const contents of chunkContents) {
88
  if (contents) {
89
  console.log('Processing chunk, characters:', contents.length);
90
+
91
+ // Use promise wrapper for importFromJsonString callback
92
+ await new Promise((resolve, reject) => {
93
+ importFromJsonString(nativeDB, contents, (err) => {
94
+ if (err) {
95
+ console.error('Error importing database chunk:', err);
96
+ reject(err);
97
+ } else {
98
+ console.log('Database chunk imported successfully');
99
+ resolve();
100
+ }
101
+ });
102
+ });
103
  }
104
  }
105