/
dbutils.go
251 lines (209 loc) · 6.29 KB
/
dbutils.go
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
/*
* Copyright (c) 2010 Digaku.com
* writen by Robin Syihab (r[at]nosql.asia)
*
* License MIT
*
* Copyright (c) 2009 The Go Authors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package ndayak
import (
"fmt"
"os"
"net"
"time"
"encoding/hex"
"mongo"
)
var (
Con *net.UDPConn
ColStream *mongo.Collection
ColPost *mongo.Collection
ColChan *mongo.Collection
ColTun *mongo.Collection
ColUser *mongo.Collection
ColUserSettings *mongo.Collection
)
type OidSearch map[string]mongo.ObjectId
type HasMetaname interface {
Metaname() string
}
type SuperDoc struct {
Id_ []byte
Origin_id_ string
Metaname_ string
}
type UserPost struct {
Id_ []byte
Origin_id_ string
Metaname_ string
Popular_Post_ string
WriterId string
Message string
}
type Channel struct{
Id_ []byte
Name string
Desc string
Metaname_ string
}
func (s *Channel) Metaname() string {return s.Metaname_;}
type User struct{
Id_ []byte
Name string
Email_login string
Lang_id string
Desc string
Metaname_ string
Followed_user_ids_ []string
}
func (s *User) Metaname() string {return s.Metaname_;}
type UserSettings struct{
Profile_bgimage string
Profile_bgcolor int
Profile_bgfillmode int
No_email_notification int
Feed_pop_article bool
}
type Origin struct {
Id_ []byte
Name string
Metaname_ string
}
type PostStream struct {
Metaname_ string
UserId string
PostId string
Timepos int64
}
type searchPost struct {
Id_ []byte
}
func RmPostStream(postId string){
if dbcon == nil { Error("Database not connected.\n"); return;}
qfind, err := mongo.Marshal(map[string]string{"postid":postId}, atreps)
if err != nil{err = os.NewError("Cannot marshal"); return}
err = ColStream.Remove(qfind)
if err != nil{
Error("rmPost: Cannot remove post with id `%s`\n", postId)
}
}
func GetOrigin(originId string) (doc mongo.BSON, err os.Error){
if dbcon == nil { Error("Database not connected.\n"); return;}
qfind, err := mongo.Marshal(OidSearch{"_id":mongo.ObjectId{originId}}, atreps)
if err != nil{err = os.NewError("Cannot marshal"); return}
doc, err = ColUser.FindOne(qfind)
if err != nil{
doc, err = ColChan.FindOne(qfind)
if err != nil{
doc, err = ColTun.FindOne(qfind)
if err != nil{
err = os.NewError(fmt.Sprintf("Cannot find origin for id `%s`", originId))
return
}
}
}
return doc, nil
}
func GetUser(userId string) (user *User, err os.Error){
if dbcon == nil { Error("Database not connected.\n"); return;}
qfind, err := mongo.Marshal(OidSearch{"_id":mongo.ObjectId{userId}}, atreps)
if err != nil{
return nil, os.NewError(fmt.Sprintf("getUser: Cannot marshal. %s", err))
}
doc, err := ColUser.FindOne(qfind)
if err != nil{
return nil, os.NewError(fmt.Sprintf("getUser: Cannot find user by id `%s`. %s.",userId,err))
}
user = new(User)
mongo.Unmarshal(doc.Bytes(), user, atreps)
return user, nil
}
func GetUserSettings(userId string) (user_settings *UserSettings, err os.Error){
if dbcon == nil { Error("Database not connected.\n"); return;}
qfind, err := mongo.Marshal(map[string]string{"_user_id":userId}, atreps)
if err != nil{
return nil, os.NewError(fmt.Sprintf("getUserSettings: Cannot marshal. %s", err))
}
doc, err := ColUserSettings.FindOne(qfind)
if err != nil{
return nil, os.NewError(fmt.Sprintf("getUserSettings: Cannot find user settings by user id `%s`. %s.",userId,err))
}
user_settings = new(UserSettings)
mongo.Unmarshal(doc.Bytes(), user_settings, atreps)
return user_settings, nil
}
func GetPost(postId string) (user *UserPost, err os.Error){
if dbcon == nil { Error("Database not connected.\n"); return;}
qfind, err := mongo.Marshal(OidSearch{"_id":mongo.ObjectId{postId}}, atreps)
if err != nil{
return nil, os.NewError(fmt.Sprintf("getUser: Cannot marshal. %s", err))
}
doc, err := ColPost.FindOne(qfind)
if err != nil{
return nil, os.NewError(fmt.Sprintf("getUser: Cannot find user by id `%s`. %s.",postId,err))
}
var post *UserPost = new(UserPost)
mongo.Unmarshal(doc.Bytes(), post, atreps)
return post, nil
}
func PostStreamExists(userId string, postId string) bool {
if dbcon == nil { Error("Database not connected.\n"); return false;}
if len(userId) == 24 && len(postId) == 24{
qfind, err := mongo.Marshal(map[string]string{"userid":userId,"postid":postId}, atreps)
if err != nil{Error("Cannot marshal. %s\n", err); return false}
doc, err := ColStream.FindOne(qfind)
if err == nil || doc != nil{
return true
}
}
return false
}
func InsertPostStream(userId string, postId string) {
if dbcon == nil { Error("Database not connected.\n"); return;}
Info2("Inserting Post stream with id `%v`, writer id: `%v`\n", postId, userId)
if len(userId) == 0 || len(postId) == 0{
return
}
if PostStreamExists(userId, postId){
Warn("InsertPostStream: Cannot insert for userId: %v, postId: %v. Already exists.\n", userId, postId)
return
}
doc, err := mongo.Marshal(&PostStream{
Metaname_:"NdayakStream",
UserId:userId,
PostId:postId,
Timepos:time.Nanoseconds() / 1e6,
}, atreps)
if err != nil{
Error("Cannot insertPostStream for userId: %v, postId: %v. %v\n", userId, postId, err)
return
}
ColStream.Insert(doc)
}
func strid(byteId []byte) string {return hex.EncodeToString(byteId);}
func byteid(strId string) []byte{
rv, err := hex.DecodeString(strId)
if err != nil{
return nil
}
return rv
}