Skip to content

Commit 540b1c9

Browse files
committed
Fixed remove. Added a functions to remove all documents with the same base
1 parent 4ea16c8 commit 540b1c9

File tree

3 files changed

+32
-19
lines changed

3 files changed

+32
-19
lines changed

packages/spomet/client.coffee

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ Spomet.update = (findable) ->
1111
Spomet.remove = (findable) ->
1212
Meteor.call 'spometRemove', findable, () ->
1313

14+
Spomet.removeBase = (baseId) ->
15+
Meteor.call 'spometRemoveBase', baseId, () ->
16+
1417
class Spomet.Search
1518

1619
constructor: () ->

packages/spomet/indexes/index.coffee

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
index = (findable, tokens, collection) ->
1+
indexTokens = (findable, tokens, collection) ->
22
tokens.forEach (token) ->
33
doc =
44
docId: findable.docId
@@ -18,8 +18,8 @@ index = (findable, tokens, collection) ->
1818
documentsCount: 1,
1919
documents: [doc]
2020

21-
tokenizeWithIndex = (index, text) ->
22-
tokenizer = new index.Tokenizer
21+
tokenizeWithIndex = (ind, text) ->
22+
tokenizer = new ind.Tokenizer
2323
text.split('').forEach (c, i) ->
2424
tokenizer.parseCharacter c, i
2525
tokenizer.finalize()
@@ -61,8 +61,8 @@ Index.add = (findable, callback) ->
6161

6262
unless Documents.exists findable
6363
#init indexer for each index
64-
tokenizers = Spomet.options.indexes.map (index) ->
65-
new index.Tokenizer
64+
tokenizers = Spomet.options.indexes.map (i) ->
65+
new i.Tokenizer
6666

6767
#normalize and tokenize over all indexes in one go
6868
findable.text.split('').forEach (c, pos) ->
@@ -71,8 +71,8 @@ Index.add = (findable, callback) ->
7171

7272
tokenizers.forEach (t) ->
7373
t.finalize()
74-
index findable, t.tokens, t.collection
75-
74+
indexTokens findable, t.tokens, t.collection
75+
7676
Documents.add findable, tokenizers.map((i) -> i.tokens).reduce (s, a) -> s.concat a
7777
callback? findable.docId, 'Document added to all indexes'
7878
else
@@ -92,8 +92,8 @@ Index.setup = () ->
9292

9393

9494
Index.remove = (docId, indexName, remToken) ->
95-
index = i for i in Spomet.options.indexes when i.name is indexName
96-
index.collection.update {token: remToken},
95+
ind = i for i in Spomet.options.indexes when i.name is indexName
96+
ind.collection.update {token: remToken},
9797
$pull: {documents: {docId: docId}}
9898
$inc: {documentsCount: -1}
9999

packages/spomet/server.coffee

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,25 +68,33 @@ cleanupSearches = () ->
6868
Spomet.add = (findable, callback) ->
6969
cleanupSearches()
7070
Spomet.Index.add findable, callback
71-
71+
72+
73+
removeTokens = (indexTokens) ->
74+
result = {}
75+
indexTokens.forEach (e) ->
76+
id = e.indexName + e.token
77+
result[id] =
78+
token: e.token
79+
indexName: e.indexName
80+
_.values result
81+
7282
Spomet.remove = (docId) ->
7383
cleanupSearches()
7484
doc = Spomet.Documents.collection.findOne {docId: docId}
7585
if doc?
76-
removeTokens = (indexTokens) ->
77-
result = {}
78-
indexTokens.forEach (e) ->
79-
id = e.indexName + e.token
80-
result[id] =
81-
token: e.token
82-
indexName: e.indexName
83-
_.values result
84-
8586
removeTokens(doc.indexTokens).forEach (rToken) ->
8687
Spomet.Index.remove docId, rToken.indexName, rToken.token
8788

8889
Spomet.Documents.collection.remove {_id: doc._id}
8990

91+
Spomet.removeBase = (baseId) ->
92+
cleanupSearches()
93+
c = Spomet.Documents.collection.find {'findable.base': baseId}
94+
c.forEach (e) ->
95+
removeTokens(e.indexTokens).forEach (rToken) ->
96+
Spomet.Index.remove e.docId, rToken.indexName, rToken.token
97+
Spomet.Documents.collection.remove {_id: e._id}
9098

9199
Spomet.reset = () ->
92100
cleanupSearches()
@@ -107,6 +115,8 @@ Meteor.methods
107115
Spomet.remove findable.docId
108116
else if findable?
109117
Spomet.remove findable
118+
spometRemoveBase: (baseId) ->
119+
Spomet.removeBase baseId
110120
spometUpdate: (findable) ->
111121
Spomet.remove findable.previousVersionDocId
112122
Spomet.add findable

0 commit comments

Comments
 (0)