/** * Converts a collection of bytes to a datastore short blob. * * @param value The pojo collection. * * @return A {@code ShortBlob} representing the byte collection. */ private ShortBlob convertByteCollectionToShortBlob(Collection<Byte> value) { return new ShortBlob(PrimitiveArrays.toByteArray(value)); }
/** * Converts a datastore blob to either a byte[] or a Byte[]. * * @param shortBlob The short blob to convert. * @param pojoType The destination type for the array. * @return Object instead of Object[] because primitive arrays don't extend * Object[]. */ private Object convertShortBlobToByteArray(ShortBlob shortBlob, Class<?> pojoType) { if (pojoType.isPrimitive()) { return shortBlob.getBytes(); } else { byte[] bytes = shortBlob.getBytes(); Byte[] array = (Byte[]) Array.newInstance(pojoType, bytes.length); for (int i = 0; i < bytes.length; i++) { array[i] = bytes[i]; } return array; } }
@Test public void testSize() { String kind = kindName + "-size"; int recordSize = (1000 * 1000); // Max. 1000000. byte[] filledRec = new byte[recordSize]; Arrays.fill(filledRec, (byte) 0x41); Blob bigBlob = new Blob(filledRec); assertEquals(recordSize, bigBlob.getBytes().length); Entity eBlob = new Entity(kind, rootKey); eBlob.setProperty("blobProp", bigBlob); service.put(eBlob); recordSize = 500 ; // Max. 500. filledRec = new byte[recordSize]; Arrays.fill(filledRec, (byte) 0x41); ShortBlob shortBlob = new ShortBlob(filledRec); assertEquals(recordSize, shortBlob.getBytes().length); Entity eShortBlob = new Entity(kind, rootKey); eShortBlob.setProperty("byteStrProp", shortBlob); service.put(eShortBlob); service.delete(eBlob.getKey(), eShortBlob.getKey()); }
@Test public void testShortBlobType() { String propertyName = "byteStrProp"; List<Entity> elist = doQuery(kindName, propertyName, ShortBlob.class, true); ShortBlob shortblob = (ShortBlob) elist.get(0).getProperty(propertyName); ShortBlob sameDat = (ShortBlob) elist.get(0).getProperty(propertyName); ShortBlob diffDat = (ShortBlob) elist.get(1).getProperty(propertyName); assertTrue(shortblob.equals(sameDat)); assertFalse(shortblob.equals(diffDat)); Arrays.equals("shortblob".getBytes(), shortblob.getBytes()); assertEquals(0, shortblob.compareTo(sameDat)); assertTrue(shortblob.compareTo(diffDat) != 0); assertEquals(shortblob.hashCode(), shortblob.hashCode()); }
/** * Converts an array of bytes to a datastore short blob. * * @param value The pojo array. It could be a primitive array, which is why * the type of the param is Object and not Object[]. * * @return A {@code ShortBlob} representing the byte array. */ private ShortBlob convertByteArrayToShortBlob(Object value) { if (value.getClass().getComponentType().isPrimitive()) { return new ShortBlob((byte[]) value); } else { return convertByteCollectionToShortBlob(Arrays.asList((Byte[]) value)); } }
public static byte[] getBytes(Object ob) { if (ob instanceof Blob) { Blob gob = (Blob) ob; return gob.getBytes(); } else { if (ob instanceof ShortBlob) { ShortBlob gob = (ShortBlob) ob; return gob.getBytes(); } else { throw new IllegalArgumentException(ob+" is not byte[] datastore type"); } } } public static int getInt(Object ob)
@Override public Object getCellEditorValue() { byte[] bytes = dialog.getBytes(); if (bytes != null) { return new ShortBlob(bytes); } else { return null; } }
@Override public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { columnName = table.getColumnName(column); if (value != null) { ShortBlob blob = (ShortBlob) value; dialog.setBytes(blob.getBytes()); button.setText(dialog.getContentDescription()); } else { button.setText(null); } return button; }
private Object pojoParamToDatastoreParam(Object param, Class type) { if (param instanceof Enum) { // TODO Cater for persisting Enum as ordinal. Need the mmd of the other side param = ((Enum) param).name(); } else if (param instanceof byte[]) { param = new ShortBlob((byte[]) param); } else if (param instanceof Byte[]) { param = new ShortBlob(PrimitiveArrays.toByteArray(Arrays.asList((Byte[]) param))); } else if (param instanceof BigDecimal) { if (type.equals(Double.class)|| type.equals(double.class) ||type.equals(Float.class)|| type.equals(float.class)) { param = ((BigDecimal) param).doubleValue(); } } else if (param instanceof Character) { param = param.toString(); } return param; }
public static void serialize(SerializationStreamWriter streamWriter, ShortBlob instance) throws SerializationException { byte[] bytes = instance.getBytes(); if (bytes == null) { streamWriter.writeInt(-1); } else { streamWriter.writeInt(bytes.length); for (byte b : bytes) { streamWriter.writeByte(b); } } } }
public static ShortBlob instantiate(SerializationStreamReader streamReader) throws SerializationException { byte[] bytes; int len = streamReader.readInt(); if (len == -1) { bytes = null; } else { bytes = new byte[len]; for (int i = 0; i < len; i++) { bytes[i] = streamReader.readByte(); } } return new ShortBlob(bytes); }
@Override protected void setValue(Object value) { if (value != null) { ShortBlob blob = (ShortBlob) value; MagicResult guess; try { guess = magic.guess(blob.getBytes()); } catch (IOException ex) { throw new IllegalArgumentException(ex); } if (guess != null) { super.setValue(guess.getDescription()); } else { super.setValue("???"); } } else { super.setValue(value); } } }
return new ShortBlob(label.getBytes());
@Test public void testSetFilterShortBlob() { Query query = new Query(kindName, rootKey); Filter filter1 = Query.FilterOperator.EQUAL.of("shortBlobData", new ShortBlob("shortBlobData0".getBytes())); Filter filter2 = Query.FilterOperator.LESS_THAN_OR_EQUAL.of("shortBlobData", new ShortBlob("shortBlobData1".getBytes())); query.setFilter(Query.CompositeFilterOperator.or(filter1, filter2)); assertEquals(2, service.prepare(query).countEntities(fo)); }
@Before public void addData() throws InterruptedException { Query query = new Query(kindName, rootKey); if (service.prepare(query).countEntities(fetchOption) == 0) { List<Entity> elist = new ArrayList<Entity>(); for (int i = 0; i < count; i++) { Entity newRec = new Entity(kindName, rootKey); newRec.setProperty("stringData", "string test data " + i); newRec.setProperty("intData", 10 * i); newRec.setProperty("stringList", Arrays.asList("abc" + i, "xyz" + i, "abc" + i)); newRec.setProperty("intList", Arrays.asList(i, 50 + i, 90 + i)); newRec.setProperty("timestamp", new Date()); newRec.setProperty("floatData", new Float(i + 0.1)); newRec.setProperty("ratingData", new Rating(i + 20)); newRec.setProperty("booleanData", true); newRec.setProperty("geoptData", new GeoPt((float) (i * 20 - 90), new Float(i * 30 - 179.1))); newRec.setProperty("byteStrProp", new ShortBlob(("shortblob" + (i * 30)).getBytes())); elist.add(newRec); } service.put(elist); sync(waitTime); } }
@Test public void testFilter() { doAllFilters(kindName, "stringProp", "mno"); doEqOnlyFilter(kindName, "phoneProp", new PhoneNumber("650-321-7654")); doEqOnlyFilter(kindName, "addressProp", new PostalAddress("19451 Via Monte Rd. CA95070")); doEqOnlyFilter(kindName, "emailProp", new Email("somebody2@gmail.com")); doEqOnlyFilter(kindName, "linkProp", new Link("http://www.google.com.com")); doEqOnlyFilter(kindName, "categoryProp", new Category("test")); doEqOnlyFilter(kindName, "byteStrProp", new ShortBlob("shortText".getBytes())); String[] inDat = {"abc", "xyz"}; doInFilter(kindName, "stringProp", inDat); }
new Category("manager")}; Text[] textDat = {new Text("english"), new Text("chinese"), new Text("japanese")}; ShortBlob[] byteString = {new ShortBlob("shortblob".getBytes()), new ShortBlob("shortText".getBytes()), new ShortBlob("shortImage".getBytes())}; Blob[] blobDat = {new Blob("blobImage".getBytes()), new Blob("blobText".getBytes()), new Blob("blobData".getBytes())};
@Before public void createData() throws InterruptedException { clearData(kindName); List<Entity> elist = new ArrayList<>(); for (int i = 0; i < count; i++) { Entity newRec = new Entity(kindName, rootKey); newRec.setProperty("stringData", "string data" + i); newRec.setProperty("timestamp", new Date()); newRec.setProperty("shortBlobData", new ShortBlob(("shortBlobData" + i).getBytes())); newRec.setProperty("intData", 20 * i); newRec.setProperty("textData", new Text("textData" + i)); newRec.setProperty("floatData", 1234 + 0.1 * i); newRec.setProperty("booleanData", true); newRec.setProperty("urlData", new Link("http://www.google.com")); newRec.setProperty("emailData", new Email("somebody123" + i + "@google.com")); newRec.setProperty("phoneData", new PhoneNumber("408-123-000" + i)); newRec.setProperty("adressData", new PostalAddress("123 st. CA 12345" + i)); newRec.setProperty("ratingData", new Rating(10 * i)); newRec.setProperty("geoptData", new GeoPt((float) (i + 0.12), (float) (i + 0.98))); newRec.setProperty("categoryData", new Category("category" + i)); newRec.setProperty("intList", Arrays.asList(i, 50 + i, 90 + i)); elist.add(newRec); } service.put(elist); sync(1000); }
newRec.setProperty("name", kindDat[k] + c); newRec.setProperty("timestamp", new Date()); newRec.setProperty("shortBlobData", new ShortBlob("shortBlobData".getBytes())); newRec.setProperty("intData", 12345); newRec.setProperty("textData", new Text("textData"));