Subversion Repositories SmartDukaan

Rev

Rev 37149 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 37149 Rev 37206
Line 18... Line 18...
18
import org.json.JSONObject;
18
import org.json.JSONObject;
19
 
19
 
20
import java.lang.reflect.Type;
20
import java.lang.reflect.Type;
21
import java.util.ArrayList;
21
import java.util.ArrayList;
22
import java.util.Arrays;
22
import java.util.Arrays;
-
 
23
import java.util.Collection;
23
import java.util.HashMap;
24
import java.util.HashMap;
24
import java.util.List;
25
import java.util.List;
25
import java.util.Map;
26
import java.util.Map;
26
 
27
 
27
public class Mongo {
28
public class Mongo {
Line 100... Line 101...
100
        cursor.close();
101
        cursor.close();
101
        return result;
102
        return result;
102
    }
103
    }
103
 
104
 
104
    /**
105
    /**
105
     * siteContent docs for the /external catalogMaster feed, ordered by _id.
106
     * siteContent docs for the /external catalogMaster feed, ordered by _id,
-
 
107
     * restricted to the given categoryIds (docs without a stamped categoryId
-
 
108
     * are invisible — the one-time backfill is mandatory).
106
     * sinceMillis == null: all docs. Otherwise docs whose lastModified >= since
109
     * sinceMillis == null: all docs. Otherwise docs whose lastModified >= since
107
     * OR that have no lastModified yet (pre-backfill docs stay visible).
110
     * OR that have no lastModified yet (pre-backfill docs stay visible).
108
     */
111
     */
109
    public List<CatalogContentModel> getSiteContentPage(Long sinceMillis, int offset, int limit) {
112
    public List<CatalogContentModel> getSiteContentPage(Collection<Integer> categoryIds, Long sinceMillis,
-
 
113
            int offset, int limit) {
110
        List<CatalogContentModel> result = new ArrayList<>();
114
        List<CatalogContentModel> result = new ArrayList<>();
111
        DB db = contentMongoClient.getDB(CONTENT);
115
        DB db = contentMongoClient.getDB(CONTENT);
112
        DBCollection collection = db.getCollection(SITE_CONTENT);
116
        DBCollection collection = db.getCollection(SITE_CONTENT);
113
        DBCursor cursor = collection.find(siteContentSinceFilter(sinceMillis))
117
        DBCursor cursor = collection.find(siteContentFilter(categoryIds, sinceMillis))
114
                .sort(new BasicDBObject("_id", 1)).skip(offset).limit(limit);
118
                .sort(new BasicDBObject("_id", 1)).skip(offset).limit(limit);
115
        while (cursor.hasNext()) {
119
        while (cursor.hasNext()) {
116
            DBObject doc = cursor.next();
120
            DBObject doc = cursor.next();
117
            try {
121
            try {
118
                long docId = ((Number) doc.get("_id")).longValue();
122
                long docId = ((Number) doc.get("_id")).longValue();
Line 131... Line 135...
131
        }
135
        }
132
        cursor.close();
136
        cursor.close();
133
        return result;
137
        return result;
134
    }
138
    }
135
 
139
 
136
    public long countSiteContent(Long sinceMillis) {
140
    public long countSiteContent(Collection<Integer> categoryIds, Long sinceMillis) {
137
        DB db = contentMongoClient.getDB(CONTENT);
141
        DB db = contentMongoClient.getDB(CONTENT);
138
        DBCollection collection = db.getCollection(SITE_CONTENT);
142
        DBCollection collection = db.getCollection(SITE_CONTENT);
139
        return collection.count(siteContentSinceFilter(sinceMillis));
143
        return collection.count(siteContentFilter(categoryIds, sinceMillis));
140
    }
144
    }
141
 
145
 
142
    private static DBObject siteContentSinceFilter(Long sinceMillis) {
146
    private static DBObject siteContentFilter(Collection<Integer> categoryIds, Long sinceMillis) {
-
 
147
        List<DBObject> and = new ArrayList<>();
-
 
148
        and.add(new BasicDBObject("categoryId", new BasicDBObject("$in", new ArrayList<>(categoryIds))));
143
        if (sinceMillis == null) {
149
        if (sinceMillis != null) {
-
 
150
            List<DBObject> or = new ArrayList<>();
-
 
151
            or.add(new BasicDBObject("lastModified", new BasicDBObject("$gte", sinceMillis)));
-
 
152
            or.add(new BasicDBObject("lastModified", new BasicDBObject("$exists", false)));
144
            return new BasicDBObject();
153
            and.add(new BasicDBObject("$or", or));
145
        }
154
        }
146
        List<DBObject> or = new ArrayList<>();
-
 
147
        or.add(new BasicDBObject("lastModified", new BasicDBObject("$gte", sinceMillis)));
-
 
148
        or.add(new BasicDBObject("lastModified", new BasicDBObject("$exists", false)));
-
 
149
        return new BasicDBObject("$or", or);
155
        return new BasicDBObject("$and", and);
150
    }
156
    }
151
 
157
 
152
    public ContentPojo getEntityByName(String name) throws Exception {
158
    public ContentPojo getEntityByName(String name) throws Exception {
153
        //LOGGER.info("Name --- {}", name);
159
        //LOGGER.info("Name --- {}", name);
154
        DB db = contentMongoClient.getDB(CONTENT);
160
        DB db = contentMongoClient.getDB(CONTENT);
Line 163... Line 169...
163
    }
169
    }
164
 
170
 
165
    public void persistEntity(ContentPojo contentPojo) {
171
    public void persistEntity(ContentPojo contentPojo) {
166
        DB db = contentMongoClient.getDB(CONTENT);
172
        DB db = contentMongoClient.getDB(CONTENT);
167
        DBCollection collection = db.getCollection(SITE_CONTENT);
173
        DBCollection collection = db.getCollection(SITE_CONTENT);
-
 
174
        if (contentPojo.getCategoryId() == null) {
-
 
175
            // full-doc replace below would wipe the stamped categoryId; carry it forward
-
 
176
            DBObject existing = collection.findOne(new BasicDBObject("_id", contentPojo.getId()),
-
 
177
                    new BasicDBObject("categoryId", 1));
-
 
178
            if (existing != null && existing.get("categoryId") != null) {
-
 
179
                contentPojo.setCategoryId(((Number) existing.get("categoryId")).intValue());
-
 
180
            }
-
 
181
        }
168
        insertOrUpdateById(collection, contentPojo.getId(), contentPojo);
182
        insertOrUpdateById(collection, contentPojo.getId(), contentPojo);
169
 
-
 
170
    }
183
    }
171
 
184
 
172
    private static <T> void insertOrUpdateById(DBCollection collection, long id, T obj) {
185
    private static <T> void insertOrUpdateById(DBCollection collection, long id, T obj) {
173
        DBObject dbo = BasicDBObject.parse(gson.toJson(obj));
186
        DBObject dbo = BasicDBObject.parse(gson.toJson(obj));
174
        LOGGER.info("dbo {}", dbo);
187
        LOGGER.info("dbo {}", dbo);