@@ -15,13 +15,11 @@ interface DocData {
1515
1616export class ZVecStore {
1717 private collection : ZVecCollection ;
18- private dimension : number ;
1918 private filePath ! : string ;
2019 private docs : Map < string , DocData > = new Map ( ) ;
2120
22- constructor ( collection : ZVecCollection , dimension : number ) {
21+ constructor ( collection : ZVecCollection ) {
2322 this . collection = collection ;
24- this . dimension = dimension ;
2523 }
2624
2725 static async create ( filePath : string , dimension : number = 384 ) : Promise < ZVecStore > {
@@ -43,14 +41,10 @@ export class ZVecStore {
4341 if ( fs . existsSync ( filePath ) ) {
4442 collection = ZVecOpen ( filePath ) ;
4543 } else {
46- const dir = path . dirname ( filePath ) ;
47- if ( ! fs . existsSync ( dir ) ) {
48- fs . mkdirSync ( dir , { recursive : true } ) ;
49- }
5044 collection = ZVecCreateAndOpen ( filePath , schema ) ;
5145 }
5246
53- const store = new ZVecStore ( collection , dimension ) ;
47+ const store = new ZVecStore ( collection ) ;
5448 store . filePath = filePath ;
5549 await store . loadMeta ( ) ;
5650 return store ;
@@ -75,8 +69,8 @@ export class ZVecStore {
7569 this . docs . set ( id , { content, meta } ) ;
7670 }
7771
78- search ( queryVector : number [ ] , topK : number ) : Array < { id : string ; score : number } > {
79- const results = this . collection . querySync ( {
72+ async search ( queryVector : number [ ] , topK : number ) : Promise < Array < { id : string ; score : number } > > {
73+ const results = await this . collection . query ( {
8074 fieldName : 'embedding' ,
8175 vector : queryVector ,
8276 topk : topK ,
@@ -93,17 +87,4 @@ export class ZVecStore {
9387 this . saveMeta ( ) ;
9488 }
9589
96- close ( ) : void {
97- this . saveMeta ( ) ;
98- this . collection . closeSync ( ) ;
99- }
100-
101- clear ( ) : void {
102- const ids = Array . from ( this . docs . keys ( ) ) ;
103- if ( ids . length > 0 ) {
104- this . collection . deleteSync ( ids ) ;
105- }
106- this . docs . clear ( ) ;
107- this . saveMeta ( ) ;
108- }
10990}
0 commit comments