@Override public Object getValue() { if (this.value == null) { return null; } return KryoManager.deserialize(this.value); }
@Override public byte[] serialize(final Object object) { return KryoManager.serialize(object); }
public static <T> T deserialize(final byte[] serialForm) { return getKryo().deserialize(serialForm); }
keyspaceToMetadata = KryoManager.deserialize(keyspaceToMatrixBinary); tx.store(NAME, branchName, KryoManager.serialize(keyspaceToMetadata)); logTrace("Inserting into NavigationMap. Branch = '" + branchName + "', keypsace = '" + keyspaceName + "', matrix = '" + matrixName + "'");
protected void writeCommitThroughCache(final String branchName, final long timestamp, final Map<String, Map<String, Object>> keyspaceToKeyToValue) { // perform the write-through in our cache Map<QualifiedKey, Object> keyValues = Maps.newHashMap(); boolean assumeImmutableValues = this.getOwningDB().getConfiguration().isAssumeCachedValuesAreImmutable(); for (Entry<String, Map<String, Object>> outerEntry : keyspaceToKeyToValue.entrySet()) { String keyspace = outerEntry.getKey(); Map<String, Object> keyToValue = outerEntry.getValue(); for (Entry<String, Object> innerEntry : keyToValue.entrySet()) { String key = innerEntry.getKey(); Object value = innerEntry.getValue(); if (assumeImmutableValues == false) { // values are not immutable, so we add a copy to the cache to prevent modification from outside value = KryoManager.deepCopy(value); } QualifiedKey qKey = QualifiedKey.create(keyspace, key); keyValues.put(qKey, value); } } this.getCache().writeThrough(branchName, timestamp, keyValues); }
@Test public void canSerializeAndDeserializeMultipleObjectsWithFiles() { try { File testFile = File.createTempFile("tempFile", "binary"); testFile.deleteOnExit(); Person p1 = new Person("John", "Doe"); Person p2 = new Person("Jane", "Doe"); KryoManager.serializeObjectsToFile(testFile, p1, p2); List<Object> deserializedObjects = KryoManager.deserializeObjectsFromFile(testFile); assertNotNull(deserializedObjects); assertEquals(2, deserializedObjects.size()); assertEquals(p1, deserializedObjects.get(0)); assertEquals(p2, deserializedObjects.get(1)); } catch (IOException ioe) { fail(ioe.toString()); } }
@Test public void canSerializeAndDeserializeWithFiles() { try { File testFile = File.createTempFile("tempFile", "binary"); testFile.deleteOnExit(); Person johnDoe = new Person("John", "Doe"); KryoManager.serializeObjectsToFile(testFile, johnDoe); Person deserialized = KryoManager.deserializeObjectFromFile(testFile); assertNotNull(deserialized); assertEquals(johnDoe, deserialized); } catch (IOException ioe) { fail(ioe.toString()); } }
try { List<Object> fileContents = KryoManager.deserializeObjectsFromFile(file);
/** * Writes the given chunk index data into the given file. * * @param file * The file to write the data into. Must not be <code>null</code>. Must refer to a file (not a directory) and must be writable. It will be fully overwritten! * @param chunkIndexData * The data to write into the file. Must not be <code>null</code>. * * @throws ChronosIOException * thrown if the writing process failed for any reason. */ public static void persistToFile(final File file, final DocumentChunkIndexData chunkIndexData) { try { if (file.exists()) { boolean deleted = file.delete(); if (!deleted) { throw new IOException("Could not delete existing index chunk file!"); } } boolean created = file.createNewFile(); if (!created) { throw new IOException("Could not create index chunk file!"); } ChunkIndexSeal seal = new ChunkIndexSeal(); KryoManager.serializeObjectsToFile(file, chunkIndexData, seal); } catch (IOException e) { throw new ChronosIOException("Failed to persist chunk index!", e); } }
this.commitLock.unlock(); KryoManager.destroyKryo();
private static KryoWrapper getKryo() { KryoWrapper kryoWrapper = THREAD_LOCAL_KRYO.get(); if (kryoWrapper == null) { return produceKryoWrapper(); } return kryoWrapper; }
@Test public void canSerializeAndDeserialize() { Person johnDoe = new Person("John", "Doe"); byte[] bytes = KryoManager.serialize(johnDoe); assertNotNull(bytes); Person deserialized = KryoManager.deserialize(bytes); assertEquals(johnDoe, deserialized); }
@Test public void canDuplicate() { Person johnDoe = new Person("John", "Doe"); Person clone = KryoManager.deepCopy(johnDoe); assertEquals(johnDoe, clone); }
KryoManager.destroyKryo();
@Test @SuppressWarnings("unchecked") public void canSerializeAndDeserializeMultipleTrees() { ImmutableMap<String, String> map = ImmutableMaps.newHashArrayMappedTreeMap(); ImmutableMap<String, String> map1 = map.put("Hello", "World"); ImmutableMap<String, String> map2 = map.put("Foo", "Bar"); ImmutableMap<String, String> map3 = map.put("Number", "42"); List<ImmutableMap<String, String>> allVersions = Lists.newArrayList(map, map1, map2, map3); byte[] bytes = KryoManager.serialize(allVersions); List<ImmutableMap<String, String>> allVersions2 = KryoManager.deserialize(bytes); assertNotNull(allVersions2); assertEquals(allVersions, allVersions2); }
@Override public Object deserialize(final byte[] serialForm) { return KryoManager.deserialize(serialForm); }
public PropertyRecord(final String key, final Object value) { checkNotNull(key, "Precondition violation - argument 'key' must not be NULL!"); checkNotNull(value, "Precondition violation - argument 'value' must not be NULL!"); this.key = key; this.value = KryoManager.serialize(value); }
private Object performGet(final String branchName, final QualifiedKey qKey, final long timestamp) { try (AutoLock lock = this.lockNonExclusive()) { // first, try to find the result in our cache CacheGetResult<Object> cacheGetResult = this.getCache().get(branchName, timestamp, qKey); if (cacheGetResult.isHit()) { Object result = cacheGetResult.getValue(); if (result == null) { return null; } // cache hit, return the result immediately if (this.getOwningDB().getConfiguration().isAssumeCachedValuesAreImmutable()) { // return directly return cacheGetResult.getValue(); } else { // return a copy return KryoManager.deepCopy(result); } } // need to contact the backing store. 'performRangedGet' automatically caches the result. GetResult<Object> getResult = this.performRangedGetInternal(branchName, qKey, timestamp); if (getResult.isHit() == false) { return null; } else { return getResult.getValue(); } } }
public static <T> T deepCopy(final T element) { return getKryo().deepCopy(element); }
@Test public void canSerializeAndDeserializeSingleTree() { ImmutableMap<String, String> map = ImmutableMaps.newHashArrayMappedTreeMap(); map = map.put("Hello", "World"); map = map.put("Foo", "Bar"); map = map.put("Number", "42"); byte[] bytes = KryoManager.serialize(map); ImmutableMap<String, String> map2 = KryoManager.deserialize(bytes); assertNotNull(map2); assertEquals(map.entrySet(), map2.entrySet()); for (Entry<String, String> entry : map.entrySet()) { assertEquals(entry.getValue(), map2.get(entry.getKey())); } for (Entry<String, String> entry : map2.entrySet()) { assertEquals(entry.getValue(), map.get(entry.getKey())); } }