public static ObjectNode generate(Map<String, List<String>> map) { ObjectNode obj = jsonObject(); for (Map.Entry<String, List<String>> item : map.entrySet()) { String key = item.getKey(); obj.put(key, item.getValue().get(0)); } return obj; }
private Iterable<Map.Entry<String, JsonNode>> strip(Iterable<Map.Entry<String, JsonNode>> fields) { ObjectNode properties = JsonHelper.jsonObject(); for (Map.Entry<String, JsonNode> entry : fields) { String key = stripName(entry.getKey(), "property"); if (key.equals("id")) { key = "_id"; } properties.set(key, entry.getValue()); } return () -> properties.fields(); }
@Override public CompletableFuture<User> getUser(RequestContext context, Object userId) { return CompletableFuture.supplyAsync(() -> { GetItemResult item = dynamoDBClient.getItem("users", ImmutableMap.of( "project", new AttributeValue(context.project), "id", new AttributeValue(userId.toString()) )); Map<String, AttributeValue> attrs = item.getItem().get("properties").getM(); ObjectNode obj = JsonHelper.jsonObject(); for (Entry<String, AttributeValue> entry : attrs.entrySet()) { obj.set(entry.getKey(), getJsonValue(entry.getValue())); } return new User(userId, null, obj); }); }
private FullHttpResponse unauthorized(Optional<String> jsonp) { String encode = JsonHelper.jsonObject() .put("success", false) .put("message", UNAUTHORIZED.reasonPhrase()) .put("authentication", config.getAuthentication()) .put("googleApiKey", config.getGoogleClientId()).toString(); if (jsonp.isPresent()) { encode = jsonp.get() + "(" + encode + ")"; } DefaultFullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, wrappedBuffer(encode.getBytes(CharsetUtil.UTF_8))); DefaultCookie cookie = new DefaultCookie("session", ""); cookie.setHttpOnly(true); cookie.setMaxAge(0); response.headers().add(SET_COOKIE, STRICT.encode(cookie)); response.headers().set(CONTENT_TYPE, "application/json; charset=utf-8"); return response; }
ObjectNode properties = JsonHelper.jsonObject(); ResultSetMetaData metaData = resultSet.getMetaData(); int columnCount = metaData.getColumnCount() + 1;
@Test public void testSetSomeOfNullProperties() throws Exception { AbstractUserService userService = getUserService(); userService.setUserProperties(PROJECT_NAME, 3, JsonHelper.jsonObject() .put("test10", "val") .put("created_at", 100.0) .put("test", (String) null) .put("test1", (String) null) ); User test = userService.getUser(CONTEXT, 3).join(); assertEquals(test.id, 3); assertEquals((Object) test.properties, JsonHelper.jsonObject() .put("test10", "val") .put("created_at", Instant.ofEpochMilli(100).toString())); }
@Test public void testSetNullProperties() throws Exception { AbstractUserService userService = getUserService(); userService.setUserProperties(PROJECT_NAME, 3, JsonHelper.jsonObject() .put("test", (String) null) .put("test1", (String) null)); User test = userService.getUser(CONTEXT, 3).join(); assertEquals(test.id, 3); assertNotNull(test.properties.get("created_at").asText()); }
@Test public void testSetOncePropertiesLatterSet() throws Exception { AbstractUserService userService = getUserService(); userService.setUserProperties(PROJECT_NAME, 6, sampleProperties); userService.setUserPropertiesOnce(PROJECT_NAME, 6, JsonHelper.jsonObject() .put("test", 2) .put("test1 Naber Abi", "value1") .put("test4 samil", false) .put("created_at", Instant.now().toEpochMilli()) .put("test5", 2.5)); User test = userService.getUser(CONTEXT, 6).join(); assertEquals((Object) test.properties, samplePropertiesExpected); }
@Test public void testCastingSetProperties() throws Exception { AbstractUserService userService = getUserService(); userService.setUserProperties(PROJECT_NAME, 2, sampleProperties); userService.setUserProperties(PROJECT_NAME, 2, JsonHelper.jsonObject() .put("test", "2") .put("test1 Naber abi", 324) .put("test4 samil", "true") .put("created_at", "test") .put("test5", "2.5")); User test = userService.getUser(CONTEXT, 2).join(); assertEquals(test.id, 2); assertEquals((Object) test.properties, JsonHelper.jsonObject() .put("test", 2.0) .put("test1 naber abi", "324") .put("test4 samil", true) .put("created_at", Instant.ofEpochMilli(100).toString()) .put("test5", 2.5)); }
@Test public void testUnsetSetProperties() throws Exception { AbstractUserService userService = getUserService(); userService.setUserProperties(PROJECT_NAME, 7, sampleProperties); userService.unsetProperties(PROJECT_NAME, 7, ImmutableList.of( "test", "test1 Naber Abi", "test4 samil")); User test = userService.getUser(CONTEXT, 7).join(); assertEquals(test.id, 7); assertEquals((Object) test.properties, JsonHelper.jsonObject() .put("created_at", Instant.ofEpochMilli(100).toString()) .put("test5", 1.5)); }
public void incrementProperty(Connection conn, String project, Object userId, String property, double value) throws SQLException { Map<String, FieldType> columns = createMissingColumns(project, userId, ImmutableList.of(new SimpleImmutableEntry<>(property, new DoubleNode(value))), new CommitConnection(conn)); FieldType fieldType = columns.get(property); if (fieldType == null) { createColumn(project, userId, property, JsonHelper.numberNode(0)); } if (!fieldType.isNumeric()) { throw new RakamException(String.format("The property the is %s and it can't be incremented.", fieldType.name()), BAD_REQUEST); } String tableRef = checkTableColumn(stripName(property, "table column")); Statement statement = conn.createStatement(); ProjectCollection userTable = getUserTable(project, false); String table = checkProject(userTable.project, '"') + "." + checkCollection(userTable.collection); int execute = statement.executeUpdate("update " + table + " set " + tableRef + " = " + value + " + coalesce(" + tableRef + ", 0)"); if (execute == 0) { create(project, userId, JsonHelper.jsonObject().put(property, value)); } }
@Test public void testConcurrentSetProperties() throws Exception { ExecutorService executorService = Executors.newFixedThreadPool(8); Set<String> objects = new ConcurrentSkipListSet<>(); getUserService().setUserProperties(PROJECT_NAME, 3, JsonHelper.jsonObject().put("created_at", 10)); CountDownLatch countDownLatch = new CountDownLatch(1000); for (int x = 0; x < 1000; x++) { executorService.submit(() -> { AbstractUserService userService = getUserService(); ObjectNode builder = JsonHelper.jsonObject(); for (int i = 0; i < 4; i++) { String key = "test" + ((int) (Math.random() * 100)); objects.add(key); builder.put(key, 10L); } userService.setUserProperties(PROJECT_NAME, 3, builder); countDownLatch.countDown(); }); } countDownLatch.await(1, TimeUnit.MINUTES); User test = getUserService().getUser(CONTEXT, 3).join(); assertEquals(test.id, 3); ObjectNode builder = JsonHelper.jsonObject(); builder.put("created_at", Instant.ofEpochMilli(10).toString()); for (String object : objects) { builder.put(object, 10.0); } assertEquals((Object) test.properties, builder); }
@Test public void testChangeSchemaSetProperties() throws Exception { AbstractUserService userService = getUserService(); userService.setUserProperties(PROJECT_NAME, 4, sampleProperties); ObjectNode newProperties = JsonHelper.jsonObject() .put("test100", 1.0) .put("test200", "value") .put("test400", true); userService.setUserProperties(PROJECT_NAME, 4, newProperties); User test = userService.getUser(CONTEXT, 4).join(); assertEquals(test.id, 4); ObjectNode builder = JsonHelper.jsonObject(); builder.setAll(samplePropertiesExpected); builder.setAll(newProperties); assertEquals((Object) test.properties, builder); }
private void handleServerSentQueryExecutionInternal(RakamHttpRequest.StreamResponse response, QueryExecution query, boolean killOnConnectionClose, Consumer<QueryResult> exceptionMapper) { if (query == null) { LOGGER.error("Query execution is null"); response.send("result", encode(jsonObject() .put("success", false) .put("error", "Not running"))).end(); if (ex != null) { LOGGER.error(ex, "Error while executing query"); response.send("result", encode(jsonObject() .put("success", false) .put("error", ex.getCause() instanceof RakamException ? exceptionMapper.accept(result); response.send("result", encode(jsonObject() .put("success", false) .putPOJO("error", result.getError()) List<? extends SchemaField> metadata = result.getMetadata(); String encode = encode(jsonObject() .put("success", true) .putPOJO("properties", result.getProperties())
ObjectNode obj = JsonHelper.jsonObject(); for (Entry<String, AttributeValue> attributeValue : value.getM().entrySet()) { obj.set(attributeValue.getKey(), getJsonValue(attributeValue.getValue()));