Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
*
* Copyright 2013 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.netflix.zeno.fastblob;
import com.netflix.zeno.fastblob.record.VarInt;
import com.netflix.zeno.fastblob.state.ByteArrayOrdinalMap;
import com.netflix.zeno.fastblob.state.FastBlobTypeDeserializationState;
import com.netflix.zeno.fastblob.state.FastBlobTypeSerializationState;
import com.netflix.zeno.fastblob.state.TypeDeserializationStateListener;
import com.netflix.zeno.serializer.NFTypeSerializer;
import com.netflix.zeno.serializer.SerializationFramework;
import com.netflix.zeno.serializer.SerializerFactory;
import com.netflix.zeno.util.SimultaneousExecutor;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
/**
* This is the SerializationFramework for the second-generation blob.
*
* The blob is a serialized representation of all data conforming to an object model (defined by a {@link SerializerFactory})
* in a single binary file.
*
* This class is the main interface for both serialization, as well as deserialization, of FastBlob data. For detailed
* usage of the FastBlobStateEngine, please see the Zeno documentation
*
* This class holds references to the "TypeSerializationStates", which are responsible for assigning and maintaining the mappings between
* serialized representations of Objects and "ordinals" (@see {@link ByteArrayOrdinalMap}).
*
* This class also holds references to "TypeDeserializationStates", which are responsible for assigning and maintaining the reverse mapping
* between ordinals and Objects.
*
* This class also maintains an ordered list of SerializationStateConfiguration objects, which define the specifications for object
* membership within images.
*
* Original presentation for the blob:
*
* This class has a lifecycle during which it alternates between two states:
*
*
Safe to add objects, but not safe to write contained objects to a stream.
*
Not safe to add objects, but safe to write contained objects to a stream.
*
*
* Initially the object will be in state (1).
* From state (1), if prepareForWrite() is called, it will be transitioned to state (2).
* From state (2), calling prepareForNextCycle() will transition back to state (1).
*
* @see https://github.com/Netflix/zeno/wiki
*
* @author dkoszewnik
*
*/
public class FastBlobStateEngine extends SerializationFramework {
/// all serialization and deserialization states, keyed by their unique names
private final Map> serializationTypeStates;
private final Map> deserializationTypeStates;
/// The serialization states, ordered such that all dependencies come *before* their dependents
public final List> orderedSerializationStates;
private final boolean shouldUseObjectIdentityOrdinalCaching;
private final int numberOfConfigurations;
private String latestVersion;
private Map headerTags = new HashMap();
private int maxSingleObjectLength;
private final long addToAllImagesFlags;
public FastBlobStateEngine(SerializerFactory factory) {
this(factory, 1, true);
}
public FastBlobStateEngine(SerializerFactory factory, int numberOfConfigurations) {
this(factory, numberOfConfigurations, true);
}
public FastBlobStateEngine(SerializerFactory factory, int numberOfConfigurations, boolean shouldUseObjectIdentityOrdinalCaching) {
super(factory);
this.shouldUseObjectIdentityOrdinalCaching = shouldUseObjectIdentityOrdinalCaching;
this.frameworkSerializer = new FastBlobFrameworkSerializer(this);
this.frameworkDeserializer = new FastBlobFrameworkDeserializer(this);
this.serializationTypeStates = new HashMap>();
this.deserializationTypeStates = new HashMap>();
this.orderedSerializationStates = new ArrayList>();
this.numberOfConfigurations = numberOfConfigurations;
addToAllImagesFlags = FastBlobImageUtils.getAllTrue(numberOfConfigurations);
createSerializationStates();
}
protected void createSerializationStates() {
for(NFTypeSerializer serializer : getOrderedSerializers()) {
createSerializationState(serializer);
}
}
private void createSerializationState(NFTypeSerializer serializer) {
FastBlobTypeSerializationState serializationState = new FastBlobTypeSerializationState(serializer, numberOfConfigurations,
shouldUseObjectIdentityOrdinalCaching);
serializationTypeStates.put(serializer.getName(), serializationState);
orderedSerializationStates.add(serializationState);
deserializationTypeStates.put(serializer.getName(), new FastBlobTypeDeserializationState(serializer));
}
/**
* Returns the images which can be generated from this FastBlobStateEngine. The ordering here is important.
*
* The index at which a SerializationStateConfiguration is returned must be used to specify whether or not
* each object added to the FastBlobStateEngine is included in that image (see add()).
*
*/
public int getNumberOfConfigurations() {
return numberOfConfigurations;
}
/**
* Add an object to this state engine. This object will be added to all images.
*/
public void add(String type, Object obj) {
add(type, obj, addToAllImagesFlags);
}
/**
* Add an object to this state engine. The images to which this object
* should be added are specified with the addToImageFlags[] array of
* booleans.
*
*
* For example, if the FastBlobStateEngine can produce 3 images,
* getImageConfigurations() will return a List of size 3.
*
*
* If an object added to this state engine should be contained in the images
* at index 1, but not at index 0 and 2, then the boolean[] passed into this
* method should be {false, true, false}.
*
*/
@Deprecated
public void add(String type, Object obj, boolean[] addToImageFlags) {
add(type, obj, FastBlobImageUtils.toLong(addToImageFlags));
}
/**
* Add an object to this state engine. The images to which this object should be added are specified with the addToImageFlags[] array of booleans.
*
* For example, if the FastBlobStateEngine can produce 3 images, getImageConfigurations() will return a List of size 3.
*
* If an object added to this state engine should be contained in the images at index 1, but not at index 0 and 2,
* then the boolean[] passed into this method should be {false, true, false}.
*
*/
public void add(String type, Object obj, long addToImageFlags) {
FastBlobTypeSerializationState