
com.netflix.zeno.fastblob.state.FreeOrdinalTracker Maven / Gradle / Ivy
/*
*
* 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.state;
import com.netflix.zeno.fastblob.record.VarInt;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
/**
* A stack of unused ordinals.
*
* This helps fill the "holes" generated by removing unused ordinals during server processing.
*
* @author dkoszewnik
*
*/
public class FreeOrdinalTracker {
private int freeOrdinals[];
private int size;
private int nextEmptyOrdinal;
public FreeOrdinalTracker() {
this(0);
}
private FreeOrdinalTracker(int nextEmptyOrdinal) {
this.freeOrdinals = new int[64];
this.nextEmptyOrdinal = nextEmptyOrdinal;
this.size = 0;
}
/**
* @return either an ordinal which was previously deallocated, or the next empty, previously unallocated ordinal in the sequence 0-n
*/
public int getFreeOrdinal() {
if(size == 0)
return nextEmptyOrdinal++;
return freeOrdinals[--size];
}
/**
* Return an ordinal to the pool after the object to which it was assigned is discarded.
*
* @param ordinal
*/
public void returnOrdinalToPool(int ordinal) {
if(size == freeOrdinals.length) {
freeOrdinals = Arrays.copyOf(freeOrdinals, freeOrdinals.length * 3 / 2);
}
freeOrdinals[size] = ordinal;
size++;
}
public void serializeTo(OutputStream os) throws IOException {
VarInt.writeVInt(os, nextEmptyOrdinal);
VarInt.writeVInt(os, size);
for(int i=0;i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy