public static Projection array(String pattern, Integer from, Integer to, Sort sort) { return array(pattern, from, to, true, null, sort); }
/** * <pre> * [ projection ... ] * </pre> */ public static Projection project(Projection... projection) { if (projection.length == 1) { return projection[0]; } else { Projection x = new Projection(true); for (Projection p : projection) { x.addToArray(p.toJson()); } return x; } }
public static Projection includeField(String pattern) { return field(pattern, true, false); }
/** * Retrieves jobs that are available, and their scheduled time has passed. * Returns at most batchSize jobs starting at startIndex */ public MigrationJob[] retrieveJobs(int batchSize, int startIndex, JobType jobType) throws IOException, LightblueException { LOGGER.debug("Retrieving jobs: batchSize={}, startIndex={}", batchSize, startIndex); DataFindRequest findRequest = new DataFindRequest("migrationJob", null); List<Query> conditions = new ArrayList<>(Arrays.asList(new Query[] { // get jobs for this configuration Query.withValue("configurationName", Query.eq, migrationConfiguration.getConfigurationName()), // get jobs whose state ara available Query.withValue("status", Query.eq, "available"), // only get jobs that are Query.withValue("scheduledDate", Query.lte, new Date()) })); if (jobType == JobType.GENERATED) { LOGGER.debug("Looking for generated job"); conditions.add(Query.withValue("generated", Query.eq, true)); } else if (jobType == JobType.NONGENERATED) { LOGGER.debug("Looking for non generated job"); conditions.add(Query.withValue("generated", Query.eq, false)); } findRequest.where(Query.and(conditions)); findRequest.select(Projection.includeField("*")); findRequest.range(startIndex, startIndex + batchSize - 1); LOGGER.debug("Finding Jobs to execute: {}", findRequest.getBody()); return lbClient.data(findRequest, MigrationJob[].class); }
r.select(new Projection[] { Projection.includeField("_id"), Projection.array("subscriptionProducts", Query.withValue("inactiveDate", Query.eq, Literal.value(null))) }); r.where(Query.withValue("_id=0"));
/** * <pre> * { field: <pattern>, include: <include>, match: <query>, projection: <projection>, sort: <sort> } * </pre> */ public static Projection array(String pattern, Query match, boolean include, Projection projection, Sort sort) { Projection p = new Projection(false); p.add("field", pattern).add("include", include).add("match", match.toJson()); if (projection != null) { p.add("projection", projection.toJson()); } if (sort != null) { p.add("sort", sort.toJson()); } return p; }
public DataUpdateRequest returns(List<? extends Projection> projection, Integer begin, Integer maxResults) { this.projection = Projection.project(projection); this.begin = begin; this.maxResults = maxResults; return this; }
@Override public List<JsonNode> getSourceDocuments() { LOGGER.debug("Retrieving source docs"); try { DataFindRequest sourceRequest = new DataFindRequest(getMigrationConfiguration().getSourceEntityName(), getMigrationConfiguration().getSourceEntityVersion()); sourceRequest.where(Query.query((ContainerNode) JSON.toJsonNode(getMigrationJob().getQuery()))); sourceRequest.select(Projection.includeFieldRecursively("*"), Projection.excludeField("objectType")); LOGGER.debug("Source docs retrieval req: {}", sourceRequest.getBody()); JsonNode[] results = getSourceCli().data(sourceRequest, JsonNode[].class); LOGGER.debug("There are {} source docs", results.length); return Arrays.asList(results); } catch (Exception e) { LOGGER.error("Error while retrieving source documents:{}", e); throw new RuntimeException("Cannot retrieve source documents:" + e); } }
/** * <pre> * { field: <pattern>, include: <include>, recursive: <recursive> } * </pre> */ public static Projection field(String pattern, boolean include, boolean recursive) { Projection p = new Projection(false); p.add("field", pattern).add("include", include).add("recursive", recursive); return p; }
/** * Load migration configuration based on its id */ public MigrationConfiguration loadMigrationConfiguration(String migrationConfigurationId) throws IOException, LightblueException { DataFindRequest findRequest = new DataFindRequest("migrationConfiguration", null); findRequest.where(Query.withValue("_id", Query.eq, migrationConfigurationId)); findRequest.select(Projection.includeFieldRecursively("*")); LOGGER.debug("Loading configuration"); return lightblueClient.data(findRequest, MigrationConfiguration.class); }
@Override public ObjectNode getBodyJson() { ObjectNode node = (ObjectNode) super.getBodyJson(); if (queryExpression != null) { node.set("query", queryExpression.toJson()); } if (projection != null) { node.set("projection", projection.toJson()); } if (sort != null) { node.set("sort", sort.toJson()); } appendRangeToJson(node, begin, maxResults); return node; }
/** * Returns a projection based on an array or object node */ public static Projection project(ContainerNode node) { return new Projection(node); }
/** * Adds p into this array projection */ private void addToArray(JsonNode j) { if (j instanceof ArrayNode) { for (Iterator<JsonNode> itr = ((ArrayNode) j).elements(); itr.hasNext();) { addToArray(itr.next()); } } else { ((ArrayNode) node).add(j); } }
private LightblueResponse saveBatch(List<JsonNode> documentsToOverwrite) throws LightblueResponseException { // LightblueClient - save & overwrite documents DataSaveRequest saveRequest = new DataSaveRequest(getMigrationConfiguration().getDestinationEntityName(), getMigrationConfiguration().getDestinationEntityVersion()); saveRequest.setUpsert(true); saveRequest.create(documentsToOverwrite.toArray()); saveRequest.returns(Projection.includeField("*")); LightblueResponse response; try { response = getDestCli().data(saveRequest); } catch (LightblueException ex) { // bad things happened, bail! throw new RuntimeException(ex); } return response; }
/** * <pre> * { field: <pattern>, include: <include>, range: [from,to], projection: <projection>, sort: <sort> } * </pre> */ public static Projection array(String pattern, Integer from, Integer to, boolean include, Projection projection, Sort sort) { Projection p = new Projection(false); ArrayNode a = JsonNodeFactory.instance.arrayNode(); a.add(JsonNodeFactory.instance.numberNode(from)); if (to != null) { a.add(JsonNodeFactory.instance.numberNode(to)); } else { a.add(JsonNodeFactory.instance.nullNode()); } p.add("field", pattern).add("include", include).add("range", a); if (projection != null) { p.add("projection", projection.toJson()); } if (sort != null) { p.add("sort", sort.toJson()); } return p; }
public DataUpdateRequest returns(Projection[] projection, Integer begin, Integer maxResults) { this.projection = Projection.project(projection); this.begin = begin; this.maxResults = maxResults; return this; }
private void doDestinationDocumentFetch(List<Identity> ids, List<JsonNode> dest) throws Exception { if (ids != null && !ids.isEmpty()) { DataFindRequest destinationRequest = new DataFindRequest(getMigrationConfiguration().getDestinationEntityName(), getMigrationConfiguration().getDestinationEntityVersion()); List<Query> requestConditions = new ArrayList<>(); for (Identity id : ids) { List<Query> docConditions = new ArrayList<>(); int i = 0; for (String keyField : getMigrationConfiguration().getDestinationIdentityFields()) { Object v = id.get(i); Query docQuery = Query.withValue(keyField, Query.eq, v == null ? null : v.toString()); docConditions.add(docQuery); i++; } requestConditions.add(Query.and(docConditions)); } destinationRequest.where(Query.or(requestConditions)); destinationRequest.select(Projection.includeFieldRecursively("*"), Projection.excludeField("objectType")); LOGGER.debug("Fetching destination docs {}", destinationRequest.getBody()); JsonNode[] nodes = getDestCli().data(destinationRequest, JsonNode[].class); if (nodes != null) { LOGGER.debug("There are {} destination docs", nodes.length); for (JsonNode node : nodes) { dest.add(node); } } } }
/** * Read configurations from the database whose name matches this instance * name */ public MigrationConfiguration[] getMigrationConfigurations() throws IOException, LightblueException { DataFindRequest findRequest = new DataFindRequest("migrationConfiguration", null); findRequest.where(Query.withValue("consistencyCheckerName", Query.eq, cfg.getName())); findRequest.select(Projection.includeFieldRecursively("*")); LOGGER.debug("Loading configuration:{}", findRequest.getBody()); return lightblueClient.data(findRequest, MigrationConfiguration[].class); }
@Override public JsonNode getBodyJson() { ObjectNode node = (ObjectNode) super.getBodyJson(); if (projection != null) { node.set("projection", projection.toJson()); } if (objects.length == 1) { node.set("data", JSON.toJsonNode(objects[0])); } else { ArrayNode arr = JsonNodeFactory.instance.arrayNode(); for (int i = 0; i < objects.length; i++) { arr.add(JSON.toJsonNode(objects[i])); } node.set("data", arr); } appendRangeToJson(node, begin, maxResults); return node; }
private boolean migrationJobsExist() { LOGGER.debug("Checking if there are migration jobs for {}", migrationConfiguration.getConfigurationName()); DataFindRequest req = new DataFindRequest("migrationJob", null); req.where(Query.and(Query.withValue("configurationName", Query.eq, migrationConfiguration.getConfigurationName()), Query.withValue("generated", Query.eq, false), Query.withValue("status", Query.eq, MigrationJob.STATE_AVAILABLE))); req.select(Projection.includeField("_id")); req.range(1, 1); try { LightblueDataResponse resp = lbClient.data(req); return resp.parseMatchCount() > 0; } catch (Exception e) { LOGGER.error("Cannot query migration jobs:{}", e, e); return true; } }