-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindexfetchers.ts
99 lines (92 loc) · 3.21 KB
/
indexfetchers.ts
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
interface Document {
id : string,
text : string
}
class IndexFetcher {
combinedIndex: Map<string, Document> = new Map()
shardMap : [string]
/**
* key is shardid, value is true if the shard has been fetched and incorporated into the index var
*/
shardsFetched : Map<number,boolean> = new Map()
/**
* Fetch shard and incorporate it into the index.
*/
async fetchShard(shardid: number) : Promise<void>{
if(this.shardsFetched.has(shardid)){
console.debug("not needing to fetch shard "+shardid)
return
}
console.debug("started fetching inx shard " + shardid)
this.shardsFetched.set(shardid, false)
let shard = await loadIndexFromURL(meta.inxURLBase + shardid.toString())
for(let i of shard.keys()){
if(!inxFetcher.combinedIndex.has(i)){
inxFetcher.combinedIndex.set(i, <Document>shard.get(i))
}else{
//console.debug("this is weird, we fetched a token twice.")
//This is not weird if you're on firefox, bc there, the first key of a set is always an empty string.
if(i != ""){
console.warn("srsly weird")
}
}
}
console.debug("shard " + shardid + " fetched!")
inxFetcher.shardsFetched.set(shardid, true)
}
/**
* Gets shardid that contains a given token/docid. Needs to have partMap fetched.
* @param token
* @return shardid
*/
getIndexFor(token : string) : number{
let needle = 0
while(meta.inxsplits[needle] < token){
needle++
}
if(needle !== 0){
return needle-1
}else return needle
}
}
class InvertedIndexFetcher extends IndexFetcher {
combinedInvIndex : Map<string, string[]> = new Map()
/**
* Fetch shard and incorporate it into the index.
*/
async fetchShard(shardid: number) : Promise<void>{
if(this.shardsFetched.has(shardid)){
return
}
console.debug("started fetching invinx shard " + shardid)
this.shardsFetched.set(shardid, false)
let shard = await loadInvertedIndexFromURL(meta.invURLBase + shardid.toString())
for(let i of shard.keys()){
if(!invinxFetcher.combinedInvIndex.has(i)){
invinxFetcher.combinedInvIndex.set(i, shard.get(i))
}else{
//console.debug("this is weird, we fetched a token twice.")
//This is not weird if you're on firefox, bc there, the first key of a set is always an empty string.
if(i != ""){
console.warn("srsly weird")
}
}
}
console.debug("invinx shard " + shardid + " fetched!")
invinxFetcher.shardsFetched.set(shardid, true)
}
/**
* Gets shardid that contains a given token/docid. Needs to have partMap fetched.
* @param token
* @return shardid
*/
getIndexFor(token : string) : number{
let needle = 0
while(meta.invsplits[needle] < token){
needle++
}
if(needle !== 0){
return needle-1
}else return needle
}
}