List of usage examples for com.mongodb DBObject get
Object get(String key);
From source file:com.conventus.mongodb.converter.ContentConverter.java
public Content toObject(DBObject doc) { Content content = new Content(); content.setName((String) doc.get("name")); content.setSource((String) doc.get("source")); content.setFile((byte[]) doc.get("file")); ObjectId id = (ObjectId) doc.get("_id"); content.setId(id.toString());//from www . j a v a 2s . c o m return content; }
From source file:com.conventus.mongodb.converter.FilmConverter.java
public Film toObject(DBObject doc) { Film content = new Film(); content.setName((String) doc.get("name")); content.setSource((String) doc.get("source")); content.setFile((byte[]) doc.get("file")); content.setTitle((String) doc.get("title")); content.setGenre((String) doc.get("genre")); content.setSize((String) doc.get("size")); content.setQuality((String) doc.get("quality")); content.setResolution((String) doc.get("resolution")); content.setFrameRate((String) doc.get("frameRate")); content.setLanguage((String) doc.get("language")); content.setDuration((String) doc.get("duration")); content.setImdbRating((String) doc.get("imdbRating")); content.setMpr((String) doc.get("mpr")); content.setPeersSeeds((String) doc.get("peersSeeds")); ObjectId id = (ObjectId) doc.get("_id"); content.setId(id.toString());/*from w ww . java 2s .c o m*/ return content; }
From source file:com.conventus.mongodb.dao.MongoDBGenericDAO.java
public T create(T obj) { DBObject doc = this.converter.toDBObject(obj); this.col.insert(doc); ObjectId id = (ObjectId) doc.get("_id"); ((IEntity) obj).setId(id.toString()); return obj;/*w w w. j a v a 2 s.c o m*/ }
From source file:com.conventus.mongodb.dao.MongoDBGenericDAO.java
public List<T> createAll(List<T> list) { List<DBObject> insertList = new ArrayList<DBObject>(); for (T obj : list) { DBObject doc = this.converter.toDBObject(obj); insertList.add(doc);/*from w w w . ja v a2 s . c om*/ } this.col.insert(insertList); for (Integer i = 0; i < list.size(); i++) { DBObject doc = insertList.get(i); T obj = list.get(i); ObjectId id = (ObjectId) doc.get("_id"); ((IEntity) obj).setId(id.toString()); } return list; }
From source file:com.crosstreelabs.cognitio.service.mongo.MongoCatalogueService.java
License:Apache License
@Override public void save(final CatalogueEntry entry) { if (entry == null) { throw new IllegalArgumentException("Entry cannot be null"); }//from w w w . j av a 2 s .com // Save the host if (entry.host.id != null) { hostService.save(entry.host); } else { // Attempt to find and update a host with the corresponding domain Host host = hostService.findByDomain(entry.host.host); if (host == null) { hostService.save(entry.host); } else { host.lastRequest = entry.host.lastRequest; host.robotsTxt = entry.host.robotsTxt; hostService.save(host); entry.host = host; } } // Save the entry if (entry.id == null) { DBObject obj = toMongoObject(entry); WriteResult result = catalogue.insert(obj, WriteConcern.JOURNALED); entry.id = obj.get("_id").toString(); } else { catalogue.update(new BasicDBObject("_id", new ObjectId(entry.id)), toMongoObject(entry), false, false, WriteConcern.JOURNALED); } }
From source file:com.crosstreelabs.cognitio.service.mongo.MongoCatalogueService.java
License:Apache License
@Override public synchronized CatalogueEntry getWorkEntry() { if (!workPool.isEmpty()) { return workPool.remove(); }/* w ww.j a v a 2s.c o m*/ // Find hosts that have been recently accessed // XXX The delay time needs to be configurable int hostDelaySeconds = 45; Collection<Host> recentHosts = hostService.recentlyRequested(hostDelaySeconds); Set<ObjectId> excludedHostIds = new HashSet<>(); for (Host host : recentHosts) { excludedHostIds.add(new ObjectId(host.id)); } // Find hosts that are currently queued List<ObjectId> queuedHosts = catalogue.distinct("host", new BasicDBObject("status", "QUEUED")); for (ObjectId id : queuedHosts) { excludedHostIds.add(id); } // Exclude recently visited hosts BasicDBObject excludedHosts = new BasicDBObject("host", new BasicDBObject("$nin", excludedHostIds.toArray(new ObjectId[0]))); // Retreive pending (un-indexed) resources BasicDBObject unindexed = new BasicDBObject("status", "PENDING"); // Reindex resources older than 7 days (XXX Make configurable/calculated BasicDBObject staleResources = new BasicDBObject("$and", Arrays.asList(new BasicDBObject("last_visit", new BasicDBObject("$exists", true)), new BasicDBObject("last_visit", new BasicDBObject("$lte", new DateTime().minusDays(7).toDate())), new BasicDBObject("status", "INDEXED"))); // Now put it all together BasicDBObject and = new BasicDBObject("$and", Arrays.asList(excludedHosts, new BasicDBObject("$or", Arrays.asList(unindexed, staleResources)))); DBObject match = new BasicDBObject("$match", and); DBObject sort = new BasicDBObject("$sort", new BasicDBObject("last_updated", 1)); DBObject group = new BasicDBObject("$group", new BasicDBObject("_id", "$host").append("first", new BasicDBObject("$first", "$$ROOT"))); List<DBObject> pipeline = Arrays.asList(match, group, sort); LOGGER.debug("PIPELINE: {}", pipeline); Iterable<DBObject> refs = catalogue.aggregate(pipeline).results(); for (DBObject obj : refs) { workPool.add(fromMongoObject((DBObject) obj.get("first"))); } LOGGER.debug("WORK POOL: {}", workPool); // Update the objects in mongo to queued state BasicDBList ids = new BasicDBList(); ids.addAll(listIds(workPool)); catalogue.update(new BasicDBObject("_id", new BasicDBObject("$in", ids)), new BasicDBObject("$set", new BasicDBObject("status", "QUEUED")), false, true, WriteConcern.JOURNALED); if (workPool.isEmpty()) { return null; } return workPool.remove(); }
From source file:com.crosstreelabs.cognitio.service.mongo.MongoCatalogueService.java
License:Apache License
protected CatalogueEntry fromMongoObject(final DBObject obj) { if (obj == null) { throw new IllegalArgumentException("Object cannot be null"); }/* w w w . j a v a 2 s . c om*/ CatalogueEntry entry = new CatalogueEntry(); entry.id = obj.get("_id").toString(); entry.status = Status.valueOf(obj.get("status").toString()); entry.status_reason = obj.get("status_reason").toString(); entry.location = obj.get("location").toString(); entry.host = hostService.findById(obj.get("host").toString()); entry.path = obj.get("path").toString(); entry.relocated = Relocated.valueOf(obj.get("relocated").toString()); entry.newLocation = obj.get("new_location").toString(); entry.initialParent = obj.get("initial_parent").toString(); entry.initialDepth = (int) obj.get("initial_depth"); entry.initialParentTitle = obj.get("initial_parent_title").toString(); if (obj.get("first_seen") != null) { entry.firstSeen = new DateTime((Date) obj.get("first_seen")); } if (obj.get("last_visit") != null) { entry.lastVisit = new DateTime((Date) obj.get("last_visit")); } return entry; }
From source file:com.crosstreelabs.cognitio.service.mongo.MongoHostService.java
License:Apache License
@Override public void save(Host host) { if (host == null) { throw new IllegalArgumentException("Host cannot be null"); }/*from w ww . ja v a2 s . c o m*/ if (StringUtils.isBlank(host.host)) { throw new IllegalArgumentException("Cannot save host with empty domain"); } // If the host is cached, let's check to see if it changed for (CacheEntry entry : hostCache) { if (entry.getHost().equals(host) && !entry.isDirty()) { return; } } // Save the entry if (host.id == null) { DBObject obj = toMongoObject(host); hosts.insert(obj, WriteConcern.JOURNALED); host.id = obj.get("_id").toString(); } else { hosts.update(new BasicDBObject("_id", new ObjectId(host.id)), toMongoObject(host), false, false, WriteConcern.JOURNALED); } // Add to cache if needed for (CacheEntry entry : hostCache) { if (entry.getHost().equals(host)) { return; } } hostCache.add(new CacheEntry(host)); }
From source file:com.crosstreelabs.cognitio.service.mongo.MongoHostService.java
License:Apache License
protected Host fromMongoObject(final DBObject obj) { Host result = new Host(); result.id = obj.get("_id").toString(); result.host = obj.get("host").toString(); if (obj.get("last_request") != null) { result.lastRequest = new DateTime((Date) obj.get("last_request")); }// w w w . j a v a2 s . c o m if (obj.get("robots_txt") != null) { result.robotsTxt = obj.get("robots_txt").toString(); } return result; }
From source file:com.crosstreelabs.cognitio.service.mongo.MongoReferenceService.java
License:Apache License
@Override public void save(final Reference reference) { Reference existing = find(reference.linker, reference.linkee); if (existing != null) { return;//from w w w.j av a 2 s.com } DBObject obj = toMongoObject(reference); references.insert(obj, WriteConcern.JOURNALED); reference.id = obj.get("_id").toString(); referenceCache.add(new CacheEntry(reference)); }