This repository has been archived by the owner on Aug 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.coffee
127 lines (105 loc) · 3.43 KB
/
update.coffee
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
_ = require "lodash"
mongodb = require "mongodb"
nroonga = require "nroonga"
moment = require "moment"
common = require "./lib"
config = require "./config"
client = mongodb.MongoClient
groongaSchema =
fakie_page:
def:
name: "fakie_page"
key_type: "ShortText"
columns: [
{ name: "path", type: "ShortText" }
{ name: "body", type: "LongText" }
{ name: "updatedAt", type: "Time" }
]
fakie_page_terms:
def:
name: "fakie_page_terms"
key_type: "ShortText"
default_tokenizer: "TokenMecab"
flags: "TABLE_PAT_KEY|KEY_NORMALIZE"
columns: [
{ name: "idx_path", type: "fakie_page", flags: "COLUMN_INDEX|WITH_POSITION", source: "path" }
{ name: "idx_body", type: "fakie_page", flags: "COLUMN_INDEX|WITH_POSITION", source: "body" }
]
db = new nroonga.Database config.database
tables = db.commandSync "table_list"
console.log tables
createTable = (schema) ->
db.commandSync "table_create", schema.def
for column in schema.columns
db.commandSync "column_create", _.assign({ table: schema.def.name }, column)
# Initialize tables
for tableName, schema of groongaSchema
console.log tableName, schema
tableExists = false
for table in tables
if table.length < 2
console.log "pass", table
continue
if table[1] == schema.name
tableExists = true
if not tableExists
try
createTable schema
catch e
console.log e
client.connect "mongodb://#{ config.mongodb.user }:#{ config.mongodb.password }@#{ config.mongodb.host }:#{ config.mongodb.port }/#{ config.mongodb.name }", (err, mongo) ->
console.log "connected: mongodb"
if err
throw err
setTimeout ->
console.log "timeout."
mongo.close()
, 10*1000
pageCollection = mongo.collection "pages"
revisionCollection = mongo.collection "revisions"
# pageCollection.find().limit(10).each (err, doc) -> # 10件取得 (debug)
# pageCollection.find({$or: [{grant: null}, {grant: 1}]}).each (err, doc) -> # 全件取得
pageCollection.find({
updatedAt:
$gte: moment().subtract('day', 2).toDate()
$or: [{grant: null}, {grant: 1}]
}).sort(updatedAt: -1).each (err, doc) -> # 最近の 2 日間を取得
# mongo.close()
if err or not doc
console.log "No page: #{ err }"
return
console.log "Index: ", doc.path
do (doc) ->
doc_updatedAt = new Date(doc.updatedAt).getTime()
doc_body = ""
revisionCollection.find({ _id: doc.revision }).limit(1).nextObject (err, revision) ->
if err or not revision
console.log "No revision: #{ err }"
return
db.command "select", {
table: "fakie_page"
filter: "_key == \"#{ doc._id }\""
limit: 1
}, (err, data) ->
data = common.format_groonga_data data
if data.length == 0
# console.log "No record: #{ doc._id }"
else
# console.log "Find: #{ doc._id }"
row = data.shift()
if row.updatedAt == doc_updatedAt
# console.log "skip"
return
db.command "load", {
table: "fakie_page"
input_type: "json"
values: JSON.stringify [
{
_key: doc._id
path: doc.path
body: revision.body
updatedAt: doc_updatedAt
}
]
}, (err, data) ->
console.log "db.command load" , err, data