@Nullable private Document getChunk(final int startChunkIndex) { if (cursor == null) { cursor = getCursor(startChunkIndex); } Document chunk = null; if (cursor.hasNext()) { chunk = cursor.next(); if (batchSize == 1) { discardCursor(); } if (chunk.getInteger("n") != startChunkIndex) { throw new MongoGridFSException(format("Could not find file chunk for file_id: %s at chunk index %s.", fileId, startChunkIndex)); } } return chunk; }
private byte[] getBufferFromChunk(@Nullable final Document chunk, final int expectedChunkIndex) { if (chunk == null || chunk.getInteger("n") != expectedChunkIndex) { throw new MongoGridFSException(format("Could not find file chunk for file_id: %s at chunk index %s.", fileId, expectedChunkIndex)); } if (!(chunk.get("data") instanceof Binary)) { throw new MongoGridFSException("Unexpected data format for the chunk"); } byte[] data = chunk.get("data", Binary.class).getData(); long expectedDataLength = 0; boolean extraChunk = false; if (expectedChunkIndex + 1 > numberOfChunks) { extraChunk = true; } else if (expectedChunkIndex + 1 == numberOfChunks) { expectedDataLength = length - (expectedChunkIndex * (long) chunkSizeInBytes); } else { expectedDataLength = chunkSizeInBytes; } if (extraChunk && data.length > expectedDataLength) { throw new MongoGridFSException(format("Extra chunk data for file_id: %s. Unexpected chunk at chunk index %s." + "The size was %s and it should be %s bytes.", fileId, expectedChunkIndex, data.length, expectedDataLength)); } else if (data.length != expectedDataLength) { throw new MongoGridFSException(format("Chunk size data length is not the expected size. " + "The size was %s for file_id: %s chunk index %s it should be %s bytes.", data.length, fileId, expectedChunkIndex, expectedDataLength)); } return data; }
String identifier = element.getString("resource"); int vote = element.getInteger("vote"); boolean committed = element.getBoolean("committed"); boolean rolledback = element.getBoolean("rolledback");
boolean compensable = document.getBoolean("compensable"); boolean coordinator = document.getBoolean("coordinator"); int compensableStatus = document.getInteger("status"); Integer recoveredTimes = document.getInteger("recovered_times"); Date recoveredAt = document.getDate("recovered_at");
collation = collation.strength(source.getInteger("strength"));
collation = collation.strength(source.getInteger("strength"));
/** * @return next ticket id */ public int getNextId() { Document find = new Document("_id", "ticketId"); Document increase = new Document("seq", 1); Document update = new Document("$inc", increase); Document result = countersCollection.findOneAndUpdate(find, update); return result.getInteger("seq"); }
while (iterator.hasNext()) { Document doc = iterator.next(); String name = doc.getString("name"); int age = doc.getInteger("age"); System.out.println("Name: " + name); System.out.println("Age: " + age); }
@Test public void testIsMaster() throws Exception { Document isMaster = db.runCommand(new Document("isMaster", Integer.valueOf(1))); assertThat(isMaster.getBoolean("ismaster")).isTrue(); assertThat(isMaster.getDate("localTime")).isInstanceOf(Date.class); assertThat(isMaster.getInteger("maxBsonObjectSize")).isGreaterThan(1000); assertThat(isMaster.getInteger("maxMessageSizeBytes")).isGreaterThan(isMaster.getInteger("maxBsonObjectSize")); }
@SuppressWarnings("unchecked") @Override public OBOIdSuffixSettings fromDocument(@Nonnull Document document) { int totalDigits = document.getInteger(TOTAL_DIGITS); List<UserIdRange> userIdRanges = ((List<Document>) document.get(USER_ID_RANGES)).stream() .map(d -> userIdRangeConverter.fromDocument(d)) .collect(toList()); return new OBOIdSuffixSettings(totalDigits, userIdRanges); } }
@Override public Document convertToStorageType(Project object) { Document document = super.convertToStorageType(object); document.put("uid", document.getInteger("uid").longValue()); return document; } }
@Override public int getFunds(String bankAccount) { Document search = new Document("_id", bankAccount); List<Document> results = accountsCollection.find(search).limit(1).into(new ArrayList<Document>()); if (results.size() > 0) { return results.get(0).getInteger("funds"); } else { return 0; } }
/** * Construct the Scored pattern from the given document * * @param document (mongo) to construct from */ public ScoredPattern(Document document) { pattern = document.getString(PATTERN_FACT_FIELD); frequency = document.getInteger(FREQUENCY_KEY); coherence = document.getDouble(COHERENCE_KEY); }
/** * Construct the Scored pattern from the given document * * @param document (mongo) to construct from */ public ScoredPattern(Document document) { pattern = document.getString(PATTERN_FACT_FIELD); frequency = document.getInteger(FREQUENCY_KEY); coherence = document.getDouble(COHERENCE_KEY); }
@Override public Map<String, Supplement> loadAll(Collection keys) { System.out.println("LoadAll " + keys); HashMap<String, Supplement> result = new HashMap<String, Supplement>(); FindIterable<Document> id = collection.find(in("_id", keys)); for (Document document : id) { String name = (String) document.get("name"); Integer price = document.getInteger("price"); result.put(document.get("_id").toString(), new Supplement(name, price)); } return result; }
private LotteryTicket docToTicket(Document doc) { PlayerDetails playerDetails = new PlayerDetails(doc.getString("email"), doc.getString("bank"), doc.getString("phone")); int[] numArray = Arrays.asList(doc.getString("numbers").split(",")).stream().mapToInt(Integer::parseInt).toArray(); Set<Integer> numbers = new HashSet<>(); for (int num: numArray) { numbers.add(num); } LotteryNumbers lotteryNumbers = LotteryNumbers.create(numbers); return new LotteryTicket(new LotteryTicketId(doc.getInteger("ticketId")), playerDetails, lotteryNumbers); } }
@Override public Supplement load(String key) { System.out.println("Load " + key); Document document = (Document) collection.find(eq("_id", key)).first(); String name = (String) document.get("name"); Integer price = document.getInteger("price"); return new Supplement(name, price); }
@Override public Variant convertToDataModelType(Document object) { return idConverter.buildVariant(object.getString(ID_FIELD), object.getInteger(END_FIELD), object.getString(REF_FIELD), object.getString(ALT_FIELD)); }
static MongoBlob fromDocument(Document doc) { MongoBlob blob = new MongoBlob(); blob.setId(doc.getString(KEY_ID)); blob.setLevel(doc.getInteger(KEY_LEVEL, 0)); if (doc.containsKey(KEY_LAST_MOD)) { blob.setLastMod(doc.getLong(KEY_LAST_MOD)); } if (doc.containsKey(KEY_DATA)) { blob.setData(doc.get(KEY_DATA, Binary.class).getData()); } return blob; }
protected NodeInfo toNodeInfo(Document doc){ NodeInfo nodeInfo = new NodeInfo(); nodeInfo.setName(doc.getString("name")); nodeInfo.setStart(doc.getDate("lastStart")); nodeInfo.setStat(doc.getInteger("stat")); nodeInfo.setGroup(doc.getString("group")); nodeInfo.setHost(doc.getString("host")); nodeInfo.setId(doc.getString("_id")); return nodeInfo; }