-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost.ts
251 lines (216 loc) · 6.06 KB
/
post.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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import {
DB,
PaginationParams,
Post,
PostPaginatedResponse,
PostStorage,
SearchIndex,
SortIndex
} from "../types";
import { SearchUtils } from "../utils";
export class PostDB implements DB {
private static fields: { [key: string]: string } = {
name: "name",
image: "image",
description: "description",
dateLastEdited: "dateLastEdited"
};
private entries: PostStorage;
private count: number;
// sort indexes
private nameSortIndex: SortIndex[];
private dateModifiedSortIndex: SortIndex[];
// search indexes
private nameSearchIndex: SearchIndex;
private descriptionSearchIndex: SearchIndex;
private searchUtils: SearchUtils;
constructor() {
this.entries = {};
this.count = 0;
this.nameSortIndex = [];
this.dateModifiedSortIndex = [];
this.nameSearchIndex = {};
this.descriptionSearchIndex = {};
this.searchUtils = new SearchUtils();
}
private getAutoIncrementID(): number {
if (this.count === 0) {
return 0;
}
return 1 + this.entries[this.count - 1].id;
}
private returnPostsByID(ids: number[]): Post[] {
const posts: Post[] = [];
ids.forEach((id) => {
posts.push(this.entries[id]);
});
return posts;
}
private intersection(l1: number[], l2: number[]): number[] {
const r: number[] = [];
let i = 0;
let j = 0;
for (; i < l1.length && j < l2.length; ) {
if (l1[i] < l2[j]) {
i++;
} else if (l1[i] > l2[j]) {
j++;
} else {
r.push(l1[i]);
i++;
j++;
}
}
return r;
}
private paginateSearchResponse(
posts: Post[],
start: number,
end: number
): PostPaginatedResponse {
return {
count: end > start ? end - start : 0,
next: end !== posts.length,
posts: end > start ? posts.slice(start, end) : []
};
}
add(name: string, image: string, description: string, dateLastEdited?: Date): Post {
const post: Post = {
id: this.getAutoIncrementID(),
name,
image,
description,
dateLastEdited: dateLastEdited || new Date()
};
this.entries[post.id] = post;
this.count++;
// create name search index
const nameTokens = this.searchUtils.analyze(post.name);
nameTokens.forEach((token) => {
let ids = this.nameSearchIndex[token];
if (!ids) {
this.nameSearchIndex[token] = [];
ids = this.nameSearchIndex[token];
}
if (ids.length !== 0 && ids[ids.length - 1] === post.id) {
return;
}
this.nameSearchIndex[token].push(post.id);
});
// create description search index
const descriptionTokens = this.searchUtils.analyze(post.description);
descriptionTokens.forEach((token) => {
let ids = this.descriptionSearchIndex[token];
if (!ids) {
this.descriptionSearchIndex[token] = [];
ids = this.descriptionSearchIndex[token];
}
if (ids[ids.length - 1] === post.id) {
return;
}
this.descriptionSearchIndex[token].push(post.id);
});
// create name sort index
let i = 0;
for (; i < this.nameSortIndex.length; i++) {
const field = <string>this.nameSortIndex[i].field;
if (post.name.toLowerCase() < field.toLowerCase()) {
break;
}
}
this.nameSortIndex.splice(i, 0, { id: post.id, field: post.name });
// create date modified sort index
i = 0;
for (; i < this.dateModifiedSortIndex.length; i++) {
if (post.description < this.dateModifiedSortIndex[i].field) {
break;
}
}
this.dateModifiedSortIndex.splice(i, 0, {
id: post.id,
field: post.dateLastEdited
});
return post;
}
get(
sortyBy: string = PostDB.fields.dateLastEdited,
asc: boolean = true,
pagination: PaginationParams
): PostPaginatedResponse {
const index: SortIndex[] =
PostDB.fields[sortyBy] === PostDB.fields.name
? this.nameSortIndex
: this.dateModifiedSortIndex;
const ids: number[] = [];
let start: number;
let end: number;
let next: boolean;
if (asc) {
start = pagination.page * pagination.limit;
end =
start + pagination.limit >= index.length
? index.length
: start + pagination.limit;
next = end !== index.length;
for (let i = start; i < end; i++) {
ids.push(index[i].id);
}
} else {
start = index.length - pagination.page * pagination.limit - 1;
end = start - pagination.limit < -1 ? 0 : start - pagination.limit;
next = end !== 0;
for (let i = start; i > end; i--) {
ids.push(index[i].id);
}
}
return {
count: ids.length,
next,
posts: this.returnPostsByID(ids)
};
}
search(
query: string,
searchIn: string = PostDB.fields.name,
pagination: PaginationParams
): PostPaginatedResponse {
let posts: Post[] = [];
const exact = query[0] === '"' && query[query.length - 1] === '"';
if (exact) {
query = query.split('"')[1];
}
const parsedSearchQuery = this.searchUtils.analyze(query);
const index: SearchIndex =
PostDB.fields[searchIn] === PostDB.fields.name
? this.nameSearchIndex
: this.descriptionSearchIndex;
let hits: number[] = [];
parsedSearchQuery.forEach((token) => {
if (token in index) {
if (hits.length === 0) {
hits = index[token];
} else {
hits = this.intersection(hits, index[token]);
}
}
});
const injectedPosts = this.returnPostsByID(hits);
if (exact) {
injectedPosts.forEach((post) => {
const field =
PostDB.fields[searchIn] === PostDB.fields.name ? post.name : post.description;
if (field.toLowerCase().includes(query.toLowerCase())) {
posts.push(post);
}
});
} else {
posts = posts.concat(injectedPosts);
}
const start = pagination.page * pagination.limit;
const end =
start + pagination.limit >= posts.length
? posts.length
: start + pagination.limit;
return this.paginateSearchResponse(posts, start, end);
}
}