-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathDefaultContentService.java
190 lines (149 loc) · 6.49 KB
/
DefaultContentService.java
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
package com.mongodb.socialite.content;
import com.mongodb.*;
import com.mongodb.socialite.MongoBackedService;
import com.mongodb.socialite.api.*;
import com.mongodb.socialite.configuration.DefaultContentServiceConfiguration;
import com.mongodb.socialite.services.ContentService;
import com.mongodb.socialite.services.ServiceImplementation;
import com.mongodb.socialite.util.SortOrder;
import com.yammer.dropwizard.config.Configuration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ServiceImplementation(
name = "DefaultContentService",
configClass = DefaultContentServiceConfiguration.class)
public class DefaultContentService
extends MongoBackedService implements ContentService{
private DBCollection content = null;
private ContentValidator contentValidator = null;
private final DefaultContentServiceConfiguration config;
public DefaultContentService(final MongoClientURI dbUri, final DefaultContentServiceConfiguration svcConfig ) {
super(dbUri, svcConfig);
this.config = svcConfig;
this.content = this.database.getCollection(config.content_collection_name);
this.content.createIndex( new BasicDBObject(
Content.AUTHOR_KEY,1).append(Content.ID_KEY,1));
this.contentValidator = new BasicContentValidator();
}
@Override
public List<Content> getContentFor(User author, ContentId anchor, int limit) {
// Special case going backward from head yields nothing
if(limit == 0 || (anchor == null && limit < 0)){
return Collections.emptyList();
}
DBCursor contentCursor = null;
SortOrder order = null;
if(anchor == null){
contentCursor = this.content.find(byUserId(author));
order = SortOrder.DESCENDING;
}
else if(limit > 0){
contentCursor = this.content.find(byUserAfterContentId(author, anchor));
order = SortOrder.DESCENDING;
}
else{
contentCursor = this.content.find(byUserBeforeContentId(author, anchor));
order = SortOrder.ASCENDING;
}
return getFromCursor(contentCursor, order, Math.abs(limit));
}
@Override
public List<Content> getContentFor(List<User> authors, ContentId anchor, int limit) {
// If no authors then no content !
if(authors == null || authors.isEmpty()){
return Collections.emptyList();
}
// Special case going backward from head yields nothing
if(anchor == null && limit < 0){
return Collections.emptyList();
}
DBCursor contentCursor = null;
SortOrder order = null;
if(anchor == null){
contentCursor = this.content.find(byUserList(authors));
order = SortOrder.DESCENDING;
}
else if(limit > 0){
contentCursor = this.content.find(byUserListAfterContentId(authors, anchor));
order = SortOrder.DESCENDING;
}
else{
contentCursor = this.content.find(byUserListBeforeContentId(authors, anchor));
order = SortOrder.ASCENDING;
}
return getFromCursor(contentCursor, order, Math.abs(limit));
}
@Override
public Content getContentById(final ContentId id) {
DBObject target = this.content.findOne(byContentId(id));
if( target == null )
throw new ServiceException(
ContentError.CONTENT_NOT_FOUND).set("contentId", id);
return new Content(target);
}
@Override
public void publishContent(User user, Content content) {
this.contentValidator.validateContent(content);
this.content.insert(content.toDBObject());
}
@Override
public Configuration getConfiguration() {
return this.config;
}
private static List<Content> getFromCursor(DBCursor results, SortOrder order, int limit) {
List<Content> contentList = new ArrayList<Content>();
// Setup the cursor with options
results.sort( new BasicDBObject(Content.ID_KEY, order.getValue()));
results.limit(-limit);
// Wind out the cursor and build a result list
while(results.hasNext()) {
final DBObject obj = results.next();
final Content content = new Content(obj);
contentList.add(content);
}
// For ascending queries, normalize return order
// so that latest content is always first
if(order == SortOrder.ASCENDING){
Collections.reverse(contentList);
}
return contentList;
}
private static BasicDBObject byContentId(ContentId id) {
return new BasicDBObject(Content.ID_KEY, id.getId());
}
private static BasicDBObject byUserAfterContentId(User author, ContentId id) {
return new BasicDBObject(Content.AUTHOR_KEY, author.getUserId()).
append(Content.ID_KEY, new BasicDBObject("$lt", id.getId()));
}
private static BasicDBObject byUserBeforeContentId(User author, ContentId id) {
return new BasicDBObject(Content.AUTHOR_KEY, author.getUserId()).
append(Content.ID_KEY, new BasicDBObject("$gt", id.getId()));
}
private static BasicDBObject byUserId(User author) {
return new BasicDBObject(Content.AUTHOR_KEY, author.getUserId());
}
private static BasicDBObject byUserList(List<User> authors) {
BasicDBList id_list = new BasicDBList();
for( User author : authors )
id_list.add( author.getUserId() );
BasicDBObject in = new BasicDBObject("$in", id_list);
return new BasicDBObject(Content.AUTHOR_KEY, in);
}
private static BasicDBObject byUserListAfterContentId(List<User> authors, ContentId id) {
BasicDBList id_list = new BasicDBList();
for( User author : authors )
id_list.add( author.getUserId() );
BasicDBObject in = new BasicDBObject("$in", id_list);
return new BasicDBObject(Content.AUTHOR_KEY, in).
append(Content.ID_KEY, new BasicDBObject("$lt", id.getId()));
}
private static BasicDBObject byUserListBeforeContentId(List<User> authors, ContentId id) {
BasicDBList id_list = new BasicDBList();
for( User author : authors )
id_list.add( author.getUserId() );
BasicDBObject in = new BasicDBObject("$in", id_list);
return new BasicDBObject(Content.AUTHOR_KEY, in).
append(Content.ID_KEY, new BasicDBObject("$gt", id.getId()));
}
}