private void appendGetByHashKeyMethod(StringBuilder classBuilder) { if(schema.getHashKey() != null) { String valueReturnType = parameterizeValue ? "V" : valueClassName; classBuilder.append(" public " + valueReturnType + " get("); classBuilder.append(getKeyFieldType(schema.getHashKey().getFieldPath(0))).append(" k0"); for(int i=1;i<schema.getHashKey().numFields();i++) classBuilder.append(", ").append(getKeyFieldType(schema.getHashKey().getFieldPath(i))).append(" k").append(i); classBuilder.append(") {\n"); classBuilder.append(" return findValue(k0"); for(int i=1;i<schema.getHashKey().numFields();i++) classBuilder.append(", k").append(i); classBuilder.append(");\n"); classBuilder.append(" }\n\n"); } }
@Override public void writeTo(OutputStream os) throws IOException { DataOutputStream dos = new DataOutputStream(os); if(getHashKey() != null) dos.write(SchemaType.MAP.getTypeIdWithPrimaryKey()); else dos.write(SchemaType.MAP.getTypeId()); dos.writeUTF(getName()); dos.writeUTF(getKeyType()); dos.writeUTF(getValueType()); if(getHashKey() != null) { VarInt.writeVInt(dos, getHashKey().numFields()); for(int i=0;i<getHashKey().numFields();i++) { dos.writeUTF(getHashKey().getFieldPath(i)); } } }
public void buildKeyDeriver() { if(getSchema().getHashKey() != null) this.keyDeriver = new HollowPrimaryKeyValueDeriver(getSchema().getHashKey(), getStateEngine()); for(int i=0; i<shards.length; i++) shards[i].setKeyDeriver(keyDeriver); }
void buildKeyMatcher() { PrimaryKey hashKey = getSchema().getHashKey(); if(hashKey != null) this.keyMatcher = new HistoricalPrimaryKeyMatcher(getDataAccess(), hashKey); }
@Override public boolean equals(Object other) { if(!(other instanceof HollowMapSchema)) return false; HollowMapSchema otherSchema = (HollowMapSchema)other; if(!getName().equals(otherSchema.getName())) return false; if(!getKeyType().equals(otherSchema.getKeyType())) return false; if(!getValueType().equals(otherSchema.getValueType())) return false; return isNullableObjectEquals(hashKey, otherSchema.getHashKey()); }
Assert.assertEquals(s1.getHashKey(), s2.getHashKey()); Assert.assertEquals(new PrimaryKey("TypeA", "f1"), s2.getHashKey()); Assert.assertEquals(s1.getHashKey(), s2.getHashKey()); Assert.assertEquals(new PrimaryKey("TypeA", "f1", "f2"), s2.getHashKey()); Assert.assertNotEquals(s1.getHashKey(), s2.getHashKey()); Assert.assertNotEquals(s1.getHashKey(), s2.getHashKey());
@Override public boolean containsKey(HollowMap<K, V> map, int ordinal, Object key) { if(getSchema().getHashKey() != null) { for(int i=0;i<ordinals.length;i+=2) { if(ordinals[i] != -1 && map.equalsKey(ordinals[i], key)) return true; } } else { int hashCode = dataAccess.getDataAccess().getHashCodeFinder().hashCode(key); int bucket = (HashCodes.hashInt(hashCode) & hashMask) * 2; while(ordinals[bucket] != -1) { if(map.equalsKey(ordinals[bucket], key)) { return true; } bucket += 2; bucket &= ordinals.length - 1; } } return false; }
@Override public boolean containsKey(HollowMap<K, V> map, int ordinal, Object key) { HollowMapEntryOrdinalIterator iter; if(getSchema().getHashKey() != null) { iter = dataAccess.ordinalIterator(ordinal); } else { int hashCode = dataAccess.getDataAccess().getHashCodeFinder().hashCode(key); iter = dataAccess.potentialMatchOrdinalIterator(ordinal, hashCode); } while(iter.next()) { if(map.equalsKey(iter.getKey(), key)) return true; } return false; }
public static HollowSchema withoutKeys(HollowSchema schema) { switch(schema.getSchemaType()) { case SET: HollowSetSchema setSchema = (HollowSetSchema)schema; if(setSchema.getHashKey() != null) setSchema = new HollowSetSchema(setSchema.getName(), setSchema.getElementType()); return setSchema; case MAP: HollowMapSchema mapSchema = (HollowMapSchema)schema; if(mapSchema.getHashKey() != null) mapSchema = new HollowMapSchema(mapSchema.getName(), mapSchema.getKeyType(), mapSchema.getValueType()); return mapSchema; default: return schema; } }
@Override public V get(HollowMap<K, V> map, int ordinal, Object key) { if(getSchema().getHashKey() != null) { for(int i=0;i<ordinals.length;i+=2) { if(ordinals[i] != -1 && map.equalsKey(ordinals[i], key)) return map.instantiateValue(ordinals[i+1]); } } else { int hashCode = dataAccess.getDataAccess().getHashCodeFinder().hashCode(key); int bucket = (HashCodes.hashInt(hashCode) & hashMask) * 2; while(ordinals[bucket] != -1) { if(map.equalsKey(ordinals[bucket], key)) { return map.instantiateValue(ordinals[bucket + 1]); } bucket += 2; bucket &= ordinals.length - 1; } } return null; }
@Override public V get(HollowMap<K, V> map, int ordinal, Object key) { HollowMapEntryOrdinalIterator iter; if(getSchema().getHashKey() != null) { iter = dataAccess.ordinalIterator(ordinal); } else { int hashCode = dataAccess.getDataAccess().getHashCodeFinder().hashCode(key); iter = dataAccess.potentialMatchOrdinalIterator(ordinal, hashCode); } while(iter.next()) { if(map.equalsKey(iter.getKey(), key)) return map.instantiateValue(iter.getValue()); } return null; }
if(getSchema().getHashKey() != null) primaryKeyHasher = new HollowWriteStateEnginePrimaryKeyHasher(getSchema().getHashKey(), getStateEngine());
@Test public void parsesMapSchemaWithPrimaryKey() throws IOException { String listSchema = "MapOfStringToTypeA Map<String, TypeA> @HashKey(value);\n"; HollowMapSchema schema = (HollowMapSchema) HollowSchemaParser.parseSchema(listSchema); Assert.assertEquals("MapOfStringToTypeA", schema.getName()); Assert.assertEquals("String", schema.getKeyType()); Assert.assertEquals("TypeA", schema.getValueType()); Assert.assertEquals(new PrimaryKey("String", "value"), schema.getHashKey()); Assert.assertEquals(schema, HollowSchemaParser.parseSchema(schema.toString())); }
@Test public void parsesMapSchemaWithMultiFieldPrimaryKey() throws IOException { String listSchema = "MapOfStringToTypeA Map<String, TypeA> @HashKey(id.value, region.country.id, key);\n"; HollowMapSchema schema = (HollowMapSchema) HollowSchemaParser.parseSchema(listSchema); Assert.assertEquals("MapOfStringToTypeA", schema.getName()); Assert.assertEquals("String", schema.getKeyType()); Assert.assertEquals("TypeA", schema.getValueType()); Assert.assertEquals(new PrimaryKey("String", "id.value", "region.country.id", "key"), schema.getHashKey()); Assert.assertEquals(schema, HollowSchemaParser.parseSchema(schema.toString())); }
if(getSchema().getHashKey() != null) primaryKeyHasher = new HollowWriteStateEnginePrimaryKeyHasher(getSchema().getHashKey(), getStateEngine());