Commit bf00ea91 by Harish Butani

add collection, struct and class/trait stores

parent 6c00ad96
......@@ -18,6 +18,9 @@
package org.apache.hadoop.metadata.storage.memory;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import it.unimi.dsi.fastutil.booleans.BooleanArrayList;
import it.unimi.dsi.fastutil.bytes.ByteArrayList;
......@@ -26,6 +29,8 @@ import it.unimi.dsi.fastutil.floats.FloatArrayList;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.longs.LongArrayList;
import it.unimi.dsi.fastutil.shorts.ShortArrayList;
import org.apache.hadoop.metadata.ITypedStruct;
import org.apache.hadoop.metadata.MetadataException;
import org.apache.hadoop.metadata.storage.StructInstance;
import org.apache.hadoop.metadata.ITypedInstance;
import org.apache.hadoop.metadata.storage.RepositoryException;
......@@ -71,6 +76,10 @@ public class AttributeStores {
} else {
throw new RepositoryException(String.format("Unknown datatype %s", i.dataType()));
}
case ARRAY:
return new ImmutableListStore(i);
case MAP:
return new ImmutableMapStore(i);
default:
throw new RepositoryException(String.format("Unknown Category for datatype %s", i.dataType()));
}
......@@ -167,12 +176,12 @@ public class AttributeStores {
/*
* store the value from colPos in instance into the list.
*/
protected abstract void store(StructInstance instance, int colPos, int pos);
protected abstract void store(StructInstance instance, int colPos, int pos) throws RepositoryException;
/*
* load the value from pos in list into colPos in instance.
*/
protected abstract void load(StructInstance instance, int colPos, int pos);
protected abstract void load(StructInstance instance, int colPos, int pos) throws RepositoryException;
/*
* store the value from colPos in map as attrName
*/
......@@ -529,4 +538,52 @@ public class AttributeStores {
}
}
static class ImmutableListStore extends ObjectAttributeStore<ImmutableList> {
public ImmutableListStore( AttributeInfo attrInfo) {
super(ImmutableList.class, attrInfo);
}
protected void store(StructInstance instance, int colPos, int pos) {
list.set(pos, instance.arrays[colPos]);
}
protected void load(StructInstance instance, int colPos, int pos) {
instance.arrays[colPos] = list.get(pos);
}
protected void store(StructInstance instance, int colPos, String attrName, Map<String, Object> m) {
m.put(attrName, instance.arrays[colPos]);
}
protected void load(StructInstance instance, int colPos, Object val) {
instance.arrays[colPos] = (ImmutableList) val;
}
}
static class ImmutableMapStore extends ObjectAttributeStore<ImmutableMap> {
public ImmutableMapStore( AttributeInfo attrInfo) {
super(ImmutableMap.class, attrInfo);
}
protected void store(StructInstance instance, int colPos, int pos) {
list.set(pos, instance.maps[colPos]);
}
protected void load(StructInstance instance, int colPos, int pos) {
instance.maps[colPos] = list.get(pos);
}
protected void store(StructInstance instance, int colPos, String attrName, Map<String, Object> m) {
m.put(attrName, instance.maps[colPos]);
}
protected void load(StructInstance instance, int colPos, Object val) {
instance.maps[colPos] = (ImmutableMap) val;
}
}
}
......@@ -21,13 +21,17 @@ package org.apache.hadoop.metadata.storage.memory;
import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import org.apache.hadoop.metadata.storage.Id;
import org.apache.hadoop.metadata.ITypedReferenceableInstance;
import org.apache.hadoop.metadata.storage.Id;
import org.apache.hadoop.metadata.storage.ReferenceableInstance;
import org.apache.hadoop.metadata.storage.RepositoryException;
import org.apache.hadoop.metadata.types.AttributeInfo;
import org.apache.hadoop.metadata.types.HierarchicalType;
import org.apache.hadoop.metadata.types.IConstructableType;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
......@@ -36,10 +40,12 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
public class HierarchicalTypeStore {
final MemRepository repository;
final HierarchicalType hierarchicalType;
final IConstructableType hierarchicalType;
final ArrayList<String> typeNameList;
final ImmutableMap<AttributeInfo, IAttributeStore> attrStores;
final ImmutableList<HierarchicalTypeStore> superTypeStores;
/**
* Map Id to position in storage lists.
*/
......@@ -47,16 +53,19 @@ public class HierarchicalTypeStore {
List<Integer> freePositions;
int nextPos;
/**
* Lock for each Class/Trait.
*/
ReentrantReadWriteLock lock;
HierarchicalTypeStore(MemRepository repository, HierarchicalType hierarchicalType) throws RepositoryException {
this.hierarchicalType = hierarchicalType;
this.hierarchicalType = (IConstructableType) hierarchicalType;
this.repository = repository;
ImmutableMap.Builder<AttributeInfo, IAttributeStore> b = new ImmutableBiMap.Builder<AttributeInfo,
IAttributeStore>();
typeNameList = Lists.newArrayList((String)null);
ImmutableList<AttributeInfo>l = hierarchicalType.immediateAttrs;
for(AttributeInfo i : l) {
b.put(i, AttributeStores.createStore(i) );
......@@ -69,6 +78,8 @@ public class HierarchicalTypeStore {
b1.add(repository.getStore(s));
}
superTypeStores = b1.build();
nextPos = 0;
}
/**
......@@ -81,7 +92,22 @@ public class HierarchicalTypeStore {
* @throws RepositoryException
*/
int assignPosition(Id id) throws RepositoryException {
throw new RepositoryException("Not implemented");
int pos = -1;
if ( !freePositions.isEmpty() ) {
pos = freePositions.remove(0);
} else {
pos = nextPos++;
ensureCapacity(pos);
}
idPosMap.put(id, pos);
for(HierarchicalTypeStore s : superTypeStores) {
s.assignPosition(id);
}
return pos;
}
/**
......@@ -90,7 +116,16 @@ public class HierarchicalTypeStore {
* @throws RepositoryException
*/
void releaseId(Id id) {
throw new RuntimeException("Not implemented");
Integer pos = idPosMap.get(id);
if ( pos != null ) {
idPosMap.remove(id);
freePositions.add(pos);
for(HierarchicalTypeStore s : superTypeStores) {
s.releaseId(id);
}
}
}
/**
......@@ -100,8 +135,17 @@ public class HierarchicalTypeStore {
* @param i
* @throws RepositoryException
*/
void store(ITypedReferenceableInstance i) throws RepositoryException {
void store(ReferenceableInstance i) throws RepositoryException {
int pos = idPosMap.get(i.getId());
typeNameList.set(pos, i.getTypeName());
for(Map.Entry<AttributeInfo, IAttributeStore> e : attrStores.entrySet()) {
IAttributeStore attributeStore = e.getValue();
attributeStore.store(pos, hierarchicalType, i);
}
for(HierarchicalTypeStore s : superTypeStores) {
s.store(i);
}
}
/**
......@@ -110,7 +154,23 @@ public class HierarchicalTypeStore {
* @param i
* @throws RepositoryException
*/
void load(ITypedReferenceableInstance i) throws RepositoryException {
void load(ReferenceableInstance i) throws RepositoryException {
int pos = idPosMap.get(i.getId());
for(Map.Entry<AttributeInfo, IAttributeStore> e : attrStores.entrySet()) {
IAttributeStore attributeStore = e.getValue();
attributeStore.load(pos, hierarchicalType, i);
}
for(HierarchicalTypeStore s : superTypeStores) {
s.load(i);
}
}
public void ensureCapacity(int pos) throws RepositoryException {
typeNameList.ensureCapacity(pos);
for(Map.Entry<AttributeInfo, IAttributeStore> e : attrStores.entrySet()) {
IAttributeStore attributeStore = e.getValue();
attributeStore.ensureCapacity(pos);
}
}
}
package org.apache.hadoop.metadata.storage.memory;
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.hadoop.metadata.storage.memory;
import org.apache.hadoop.metadata.ITypedInstance;
import org.apache.hadoop.metadata.storage.RepositoryException;
import org.apache.hadoop.metadata.storage.StructInstance;
import org.apache.hadoop.metadata.types.IConstructableType;
......
......@@ -162,7 +162,7 @@ public class MemRepository implements IRepository {
for (ITypedReferenceableInstance instance : newInstances) {
HierarchicalTypeStore st = typeStores.get(instance.getTypeName());
st.store(instance);
st.store((ReferenceableInstance)instance);
}
} catch(RepositoryException re) {
for (ITypedReferenceableInstance instance : newInstances) {
......
......@@ -21,6 +21,7 @@ package org.apache.hadoop.metadata.storage.memory;
import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.apache.hadoop.metadata.MetadataException;
import org.apache.hadoop.metadata.storage.RepositoryException;
import org.apache.hadoop.metadata.storage.StructInstance;
import org.apache.hadoop.metadata.types.IConstructableType;
......@@ -55,32 +56,36 @@ public abstract class StructStore extends AttributeStores.AbstractAttributeStore
}
@Override
public void store(int pos, IConstructableType type, StructInstance instance) throws RepositoryException {
List<String> attrNames = type.getNames(attrInfo);
String attrName = attrNames.get(0);
int nullPos = instance.fieldMapping().fieldNullPos.get(attrName);
int colPos = instance.fieldMapping().fieldPos.get(attrName);
nullList.set(pos, instance.nullFlags[nullPos]);
if ( !instance.nullFlags[nullPos] ) {
StructInstance s = instance.structs[colPos];
for(Map.Entry<AttributeInfo, IAttributeStore> e : attrStores.entrySet()) {
e.getValue().store(pos, structType, s);
}
protected void store(StructInstance instance, int colPos, int pos) throws RepositoryException {
StructInstance s = instance.structs[colPos];
for(Map.Entry<AttributeInfo, IAttributeStore> e : attrStores.entrySet()) {
IAttributeStore attributeStore = e.getValue();
attributeStore.store(pos, structType, s);
}
}
if ( attrNames.size() > 1 ) {
storeHiddenVals(pos, type, instance);
protected void load(StructInstance instance, int colPos, int pos) throws RepositoryException {
for(Map.Entry<AttributeInfo, IAttributeStore> e : attrStores.entrySet()) {
IAttributeStore attributeStore = e.getValue();
attributeStore.load(pos, structType, instance);
}
}
@Override
public void load(int pos, IConstructableType type, StructInstance instance) throws RepositoryException {
protected void store(StructInstance instance, int colPos, String attrName, Map<String, Object> m) {
m.put(attrName, instance.structs[colPos]);
}
protected void load(StructInstance instance, int colPos, Object val) {
instance.structs[colPos] = (StructInstance) val;
}
@Override
public void ensureCapacity(int pos) throws RepositoryException {
for(Map.Entry<AttributeInfo, IAttributeStore> e : attrStores.entrySet()) {
IAttributeStore attributeStore = e.getValue();
attributeStore.ensureCapacity(pos);
}
nullList.ensureCapacity(pos);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment