@Override public List<FeedValue> getFeed(final int count, final String relationshipEntityKey) throws NimbitsException { final User loggedInUser = getUser(); final User feedUser = getFeedUser(relationshipEntityKey, loggedInUser); if (feedUser != null) { final Point point = getFeedPoint(feedUser); if (point == null) { return new ArrayList<FeedValue>(0); } else { final List<Value> values = RecordedValueServiceFactory.getInstance().getTopDataSeries(point, count, new Date()); final List<FeedValue> retObj = new ArrayList<FeedValue>(values.size()); for (final Value v : values) { if (!Utils.isEmptyString(v.getData())) { try { retObj.add(GsonFactory.getInstance().fromJson(v.getData(), FeedValueModel.class)); } catch (JsonSyntaxException ignored) { } } } return retObj; } } else { return new ArrayList<FeedValue>(0); } }
@Override public Value lazyArrayChunk(final Key key) { final Key arykey = ValueArray.getArrayKey(key); // From the base file key final long off = (_iceRoot != null) ? 0 : ValueArray.getChunkOffset(key); // The offset final Path p = (_iceRoot != null) ? new Path(_iceRoot, getIceName(key, (byte) 'V')) : new Path(arykey.toString()); final Size sz = new Size(); run( new Callable() { @Override public Object call() throws Exception { FileSystem fs = FileSystem.get(p.toUri(), CONF); long rem = fs.getFileStatus(p).getLen() - off; sz._value = (rem > ValueArray.CHUNK_SZ * 2) ? (int) ValueArray.CHUNK_SZ : (int) rem; return null; } }, true, 0); Value val = new Value(key, sz._value, Value.HDFS); val.setdsk(); // But its already on disk. return val; }
private static void addFolder(FileSystem fs, Path p, JsonArray succeeded, JsonArray failed) { try { if (fs == null) return; for (FileStatus file : fs.listStatus(p)) { Path pfs = file.getPath(); if (file.isDir()) { addFolder(fs, pfs, succeeded, failed); } else { Key k = Key.make(pfs.toString()); long size = file.getLen(); Value val = null; if (pfs.getName().endsWith(Extensions.JSON)) { JsonParser parser = new JsonParser(); JsonObject json = parser.parse(new InputStreamReader(fs.open(pfs))).getAsJsonObject(); JsonElement v = json.get(Constants.VERSION); if (v == null) throw new RuntimeException("Missing version"); JsonElement type = json.get(Constants.TYPE); if (type == null) throw new RuntimeException("Missing type"); Class c = Class.forName(type.getAsString()); OldModel model = (OldModel) c.newInstance(); model.fromJson(json); } else if (pfs.getName().endsWith(Extensions.HEX)) { // Hex file? FSDataInputStream s = fs.open(pfs); int sz = (int) Math.min(1L << 20, size); // Read up to the 1st meg byte[] mem = MemoryManager.malloc1(sz); s.readFully(mem); // Convert to a ValueArray (hope it fits in 1Meg!) ValueArray ary = new ValueArray(k, 0).read(new AutoBuffer(mem)); val = new Value(k, ary, Value.HDFS); } else if (size >= 2 * ValueArray.CHUNK_SZ) { val = new Value( k, new ValueArray(k, size), Value.HDFS); // ValueArray byte wrapper over a large file } else { val = new Value(k, (int) size, Value.HDFS); // Plain Value val.setdsk(); } DKV.put(k, val); Log.info("PersistHdfs: DKV.put(" + k + ")"); JsonObject o = new JsonObject(); o.addProperty(Constants.KEY, k.toString()); o.addProperty(Constants.FILE, pfs.toString()); o.addProperty(Constants.VALUE_SIZE, file.getLen()); succeeded.add(o); } } } catch (Exception e) { Log.err(e); JsonObject o = new JsonObject(); o.addProperty(Constants.FILE, p.toString()); o.addProperty(Constants.ERROR, e.getMessage()); failed.add(o); } }
@Override public void store(Value v) { // Should be used only if ice goes to HDFS assert this == getIce(); assert !v.isPersisted(); byte[] m = v.memOrLoad(); assert (m == null || m.length == v._max); // Assert not saving partial files store(new Path(_iceRoot, getIceName(v)), m); v.setdsk(); // Set as write-complete to disk }
public void onException(Throwable ex) { UKV.remove(dest()); Value v = DKV.get(progressKey()); if (v != null) { ChunkProgress p = v.get(); p = p.error(ex.getMessage()); DKV.put(progressKey(), p); } cancel(ex); }
@Override public GraphObject deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { String localPropertyView = propertyView.get(null); GraphObject deserializedOutput = null; switch (propertyFormat) { case NestedKeyValueType: deserializedOutput = deserializeNestedKeyValueType(json, typeOfT, context, true, localPropertyView, 0); break; case NestedKeyValue: deserializedOutput = deserializeNestedKeyValueType(json, typeOfT, context, false, localPropertyView, 0); break; case FlatNameValue: deserializedOutput = deserializeFlatNameValue(json, typeOfT, context, localPropertyView, 0); break; } return deserializedOutput; }
@Override public JsonElement serialize(GraphObject src, Type typeOfSrc, JsonSerializationContext context) { String localPropertyView = propertyView.get(null); JsonElement serializedOutput = null; switch (propertyFormat) { case NestedKeyValueType: serializedOutput = serializeNestedKeyValueType(src, typeOfSrc, context, true, localPropertyView, 0); break; case NestedKeyValue: serializedOutput = serializeNestedKeyValueType(src, typeOfSrc, context, false, localPropertyView, 0); break; case FlatNameValue: serializedOutput = serializeFlatNameValue(src, typeOfSrc, context, localPropertyView, 0); break; } return serializedOutput; }
@Override public void postToFeed( final User user, final Entity entity, final Point originalPoint, final Value value, final FeedType type) throws NimbitsException { final Point point = getFeedPoint(user); if (point != null) { final FeedValue feedValue = new FeedValueModel(valueToHtml(entity, originalPoint, value), value.getData(), type); final String json = GsonFactory.getSimpleInstance().toJson(feedValue); final Value v = ValueModelFactory.createValueModel(value, json); RecordedValueServiceFactory.getInstance().recordValue(user, point, v, false); } }
private String valueToHtml(final Entity entity, final Entity point, final Value value) { final StringBuilder sb = new StringBuilder(SIZE); if (!(Double.compare(value.getDoubleValue(), Const.CONST_IGNORED_NUMBER_VALUE) == 0)) { sb.append("<img style=\"float:left\" src=\"") .append(ServerInfoImpl.getFullServerURL(this.getThreadLocalRequest())); switch (value.getAlertState()) { case LowAlert: sb.append("/resources/images/point_low.png\">"); break; case HighAlert: sb.append("/resources/images/point_high.png\">"); break; case IdleAlert: sb.append("/resources/images/point_idle.png\">"); break; case OK: sb.append("/resources/images/point_ok.png\">"); break; } } if (entity != null && point != null) { sb.append(" "); if (!(Double.compare(value.getDoubleValue(), Const.CONST_IGNORED_NUMBER_VALUE) == 0)) { sb.append("Alert Status:").append(value.getAlertState().name()); sb.append("<br>Value:").append(value.getDoubleValue()); } if (!Utils.isEmptyString(value.getNote())) { sb.append("<br>Note:").append(value.getNote()); } sb.append("<a href=\"#\" onclick=\"window.open('report.html?uuid=") .append(point.getKey()) .append("', 'Report',") .append("'height=800,width=800,toolbar=0,status=0,location=0' );\" >") .append(" [more]</a>"); } return sb.toString(); }
@Override public void delete(final Value v) { assert this == getIce(); assert !v.isPersisted(); // Upper layers already cleared out run( new Callable() { @Override public Object call() throws Exception { Path p = new Path(_iceRoot, getIceName(v)); FileSystem fs = FileSystem.get(p.toUri(), CONF); fs.delete(p, true); if (v.isArray()) { // Also nuke directory if the top-level ValueArray dies p = new Path(_iceRoot, getIceDirectory(v._key)); fs = FileSystem.get(p.toUri(), CONF); fs.delete(p, true); } return null; } }, false, 0); }
public WikidataValue snakToValue(final String type, Value snak) { String jsonStr = null; try { jsonStr = mapper.writeValueAsString(snak); } catch (JsonProcessingException e) { throw new IllegalArgumentException("Unknown snak: " + snak); } final JsonElement element = new JsonParser().parse(jsonStr); final JsonElement jsonValue = (element.isJsonObject() && element.getAsJsonObject().has("value")) ? element.getAsJsonObject().get("value") : null; WikidataValue val = snak.accept( new ValueVisitor<WikidataValue>() { @Override public WikidataValue visit(EntityIdValue value) { if (value.getEntityType().equals(EntityIdValue.ET_ITEM)) { return WikidataValue.forItem(Integer.valueOf(value.getId().substring(1))); } else if (value.getEntityType().equals(EntityIdValue.ET_PROPERTY)) { throw new IllegalArgumentException("Did not expect entity property"); } else { throw new IllegalArgumentException( "Unknown entity type: " + value.getEntityType()); } } @Override public WikidataValue visit(GlobeCoordinatesValue value) { return new WikidataValue(type, gsonToPrimitive(jsonValue), jsonValue); } @Override public WikidataValue visit(QuantityValue value) { return new WikidataValue(type, gsonToPrimitive(jsonValue), jsonValue); } @Override public WikidataValue visit(StringValue value) { return WikidataValue.forString(value.getString()); } @Override public WikidataValue visit(TimeValue value) { Calendar c = new GregorianCalendar( ((int) value.getYear()), value.getMonth() - 1, value.getDay(), value.getHour(), value.getMinute(), value.getSecond()); return new WikidataValue(WikidataValue.Type.TIME, c.getTime(), jsonValue); } @Override public WikidataValue visit(MonolingualTextValue value) { return WikidataValue.forString(value.getText()); } @Override public WikidataValue visit(DatatypeIdValue value) { throw new IllegalArgumentException(); } }); return val; }