Return-Path: X-Original-To: apmail-gora-commits-archive@www.apache.org Delivered-To: apmail-gora-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 5C34110991 for ; Sat, 24 Aug 2013 00:28:21 +0000 (UTC) Received: (qmail 9782 invoked by uid 500); 24 Aug 2013 00:28:21 -0000 Delivered-To: apmail-gora-commits-archive@gora.apache.org Received: (qmail 9746 invoked by uid 500); 24 Aug 2013 00:28:21 -0000 Mailing-List: contact commits-help@gora.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@gora.apache.org Delivered-To: mailing list commits@gora.apache.org Received: (qmail 9739 invoked by uid 99); 24 Aug 2013 00:28:21 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 24 Aug 2013 00:28:21 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 24 Aug 2013 00:28:06 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id A4D0D238899C; Sat, 24 Aug 2013 00:27:43 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1517093 [2/3] - in /gora/branches/GORA_94: ./ bin/ gora-cassandra/ gora-cassandra/src/main/java/org/apache/gora/cassandra/query/ gora-cassandra/src/main/java/org/apache/gora/cassandra/serializers/ gora-cassandra/src/main/java/org/apache/go... Date: Sat, 24 Aug 2013 00:27:41 -0000 To: commits@gora.apache.org From: lewismc@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20130824002743.A4D0D238899C@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Added: gora/branches/GORA_94/gora-core/src/examples/java/org/apache/gora/examples/generated/ImmutableFields.java URL: http://svn.apache.org/viewvc/gora/branches/GORA_94/gora-core/src/examples/java/org/apache/gora/examples/generated/ImmutableFields.java?rev=1517093&view=auto ============================================================================== --- gora/branches/GORA_94/gora-core/src/examples/java/org/apache/gora/examples/generated/ImmutableFields.java (added) +++ gora/branches/GORA_94/gora-core/src/examples/java/org/apache/gora/examples/generated/ImmutableFields.java Sat Aug 24 00:27:39 2013 @@ -0,0 +1,281 @@ +/** + * Autogenerated by Avro + * + * DO NOT EDIT DIRECTLY + */ +package org.apache.gora.examples.generated; +@SuppressWarnings("all") +/** Record with only immutable or dirtyable fields, used for testing */ +public class ImmutableFields extends org.apache.gora.persistency.impl.PersistentBase implements org.apache.avro.specific.SpecificRecord, org.apache.gora.persistency.Persistent { + public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"record\",\"name\":\"ImmutableFields\",\"namespace\":\"org.apache.gora.examples.generated\",\"doc\":\"Record with only immutable or dirtyable fields, used for testing\",\"fields\":[{\"name\":\"__g__dirty\",\"type\":\"bytes\",\"doc\":\"Bytes used to represent weather or not a field is dirty.\",\"default\":\"AA==\"},{\"name\":\"v1\",\"type\":\"int\"},{\"name\":\"v2\",\"type\":[{\"type\":\"record\",\"name\":\"V2\",\"fields\":[{\"name\":\"__g__dirty\",\"type\":\"bytes\",\"doc\":\"Bytes used to represent weather or not a field is dirty.\",\"default\":\"AA==\"},{\"name\":\"v3\",\"type\":\"int\"}]},\"null\"]}]}"); + /** Bytes used to represent weather or not a field is dirty. */ + private java.nio.ByteBuffer __g__dirty = java.nio.ByteBuffer.wrap(new byte[1]); + private int v1; + private org.apache.gora.examples.generated.V2 v2; + public org.apache.avro.Schema getSchema() { return SCHEMA$; } + // Used by DatumWriter. Applications should not call. + public java.lang.Object get(int field$) { + switch (field$) { + case 0: return __g__dirty; + case 1: return v1; + case 2: return v2; + default: throw new org.apache.avro.AvroRuntimeException("Bad index"); + } + } + // Used by DatumReader. Applications should not call. + @SuppressWarnings(value="unchecked") + public void put(int field$, java.lang.Object value$) { + switch (field$) { + case 0: __g__dirty = (java.nio.ByteBuffer)value$; break; + case 1: v1 = (java.lang.Integer)value$; break; + case 2: v2 = (org.apache.gora.examples.generated.V2)value$; break; + default: throw new org.apache.avro.AvroRuntimeException("Bad index"); + } + } + + /** + * Gets the value of the 'v1' field. + */ + public java.lang.Integer getV1() { + return v1; + } + + /** + * Sets the value of the 'v1' field. + * @param value the value to set. + */ + public void setV1(java.lang.Integer value) { + this.v1 = value; + setDirty(1); + } + + /** + * Checks the dirty status of the 'v1' field. A field is dirty if it represents a change that has not yet been written to the database. + * @param value the value to set. + */ + public boolean isV1Dirty(java.lang.Integer value) { + return isDirty(1); + } + + /** + * Gets the value of the 'v2' field. + */ + public org.apache.gora.examples.generated.V2 getV2() { + return v2; + } + + /** + * Sets the value of the 'v2' field. + * @param value the value to set. + */ + public void setV2(org.apache.gora.examples.generated.V2 value) { + this.v2 = value; + setDirty(2); + } + + /** + * Checks the dirty status of the 'v2' field. A field is dirty if it represents a change that has not yet been written to the database. + * @param value the value to set. + */ + public boolean isV2Dirty(org.apache.gora.examples.generated.V2 value) { + return isDirty(2); + } + + /** Creates a new ImmutableFields RecordBuilder */ + public static org.apache.gora.examples.generated.ImmutableFields.Builder newBuilder() { + return new org.apache.gora.examples.generated.ImmutableFields.Builder(); + } + + /** Creates a new ImmutableFields RecordBuilder by copying an existing Builder */ + public static org.apache.gora.examples.generated.ImmutableFields.Builder newBuilder(org.apache.gora.examples.generated.ImmutableFields.Builder other) { + return new org.apache.gora.examples.generated.ImmutableFields.Builder(other); + } + + /** Creates a new ImmutableFields RecordBuilder by copying an existing ImmutableFields instance */ + public static org.apache.gora.examples.generated.ImmutableFields.Builder newBuilder(org.apache.gora.examples.generated.ImmutableFields other) { + return new org.apache.gora.examples.generated.ImmutableFields.Builder(other); + } + + private static java.nio.ByteBuffer deepCopyToWriteOnlyBuffer( + java.nio.ByteBuffer input) { + java.nio.ByteBuffer copy = java.nio.ByteBuffer.allocate(input.capacity()); + int position = input.position(); + input.reset(); + int mark = input.position(); + int limit = input.limit(); + input.rewind(); + input.limit(input.capacity()); + copy.put(input); + input.rewind(); + copy.rewind(); + input.position(mark); + input.mark(); + copy.position(mark); + copy.mark(); + input.position(position); + copy.position(position); + input.limit(limit); + copy.limit(limit); + return copy.asReadOnlyBuffer(); + } + + /** + * RecordBuilder for ImmutableFields instances. + */ + public static class Builder extends org.apache.avro.specific.SpecificRecordBuilderBase + implements org.apache.avro.data.RecordBuilder { + + private java.nio.ByteBuffer __g__dirty; + private int v1; + private org.apache.gora.examples.generated.V2 v2; + + /** Creates a new Builder */ + private Builder() { + super(org.apache.gora.examples.generated.ImmutableFields.SCHEMA$); + } + + /** Creates a Builder by copying an existing Builder */ + private Builder(org.apache.gora.examples.generated.ImmutableFields.Builder other) { + super(other); + } + + /** Creates a Builder by copying an existing ImmutableFields instance */ + private Builder(org.apache.gora.examples.generated.ImmutableFields other) { + super(org.apache.gora.examples.generated.ImmutableFields.SCHEMA$); + if (isValidValue(fields()[0], other.__g__dirty)) { + this.__g__dirty = (java.nio.ByteBuffer) data().deepCopy(fields()[0].schema(), other.__g__dirty); + fieldSetFlags()[0] = true; + } + if (isValidValue(fields()[1], other.v1)) { + this.v1 = (java.lang.Integer) data().deepCopy(fields()[1].schema(), other.v1); + fieldSetFlags()[1] = true; + } + if (isValidValue(fields()[2], other.v2)) { + this.v2 = (org.apache.gora.examples.generated.V2) data().deepCopy(fields()[2].schema(), other.v2); + fieldSetFlags()[2] = true; + } + } + + /** Gets the value of the 'v1' field */ + public java.lang.Integer getV1() { + return v1; + } + + /** Sets the value of the 'v1' field */ + public org.apache.gora.examples.generated.ImmutableFields.Builder setV1(int value) { + validate(fields()[1], value); + this.v1 = value; + fieldSetFlags()[1] = true; + return this; + } + + /** Checks whether the 'v1' field has been set */ + public boolean hasV1() { + return fieldSetFlags()[1]; + } + + /** Clears the value of the 'v1' field */ + public org.apache.gora.examples.generated.ImmutableFields.Builder clearV1() { + fieldSetFlags()[1] = false; + return this; + } + + /** Gets the value of the 'v2' field */ + public org.apache.gora.examples.generated.V2 getV2() { + return v2; + } + + /** Sets the value of the 'v2' field */ + public org.apache.gora.examples.generated.ImmutableFields.Builder setV2(org.apache.gora.examples.generated.V2 value) { + validate(fields()[2], value); + this.v2 = value; + fieldSetFlags()[2] = true; + return this; + } + + /** Checks whether the 'v2' field has been set */ + public boolean hasV2() { + return fieldSetFlags()[2]; + } + + /** Clears the value of the 'v2' field */ + public org.apache.gora.examples.generated.ImmutableFields.Builder clearV2() { + v2 = null; + fieldSetFlags()[2] = false; + return this; + } + + @Override + public ImmutableFields build() { + try { + ImmutableFields record = new ImmutableFields(); + record.__g__dirty = fieldSetFlags()[0] ? this.__g__dirty : (java.nio.ByteBuffer) defaultValue(fields()[0]); + record.v1 = fieldSetFlags()[1] ? this.v1 : (java.lang.Integer) defaultValue(fields()[1]); + record.v2 = fieldSetFlags()[2] ? this.v2 : (org.apache.gora.examples.generated.V2) defaultValue(fields()[2]); + return record; + } catch (Exception e) { + throw new org.apache.avro.AvroRuntimeException(e); + } + } + } + + public ImmutableFields.Tombstone getTombstone(){ + return TOMBSTONE; + } + + private static final Tombstone TOMBSTONE = new Tombstone(); + + public static final class Tombstone extends ImmutableFields implements org.apache.gora.persistency.Tombstone { + + private Tombstone() { } + + /** + * Gets the value of the 'v1' field. + */ + public java.lang.Integer getV1() { + throw new java.lang.UnsupportedOperationException("Get is not supported on tombstones"); + } + + /** + * Sets the value of the 'v1' field. + * @param value the value to set. + */ + public void setV1(java.lang.Integer value) { + throw new java.lang.UnsupportedOperationException("Set is not supported on tombstones"); + } + + /** + * Checks the dirty status of the 'v1' field. A field is dirty if it represents a change that has not yet been written to the database. + * @param value the value to set. + */ + public boolean isV1Dirty(java.lang.Integer value) { + throw new java.lang.UnsupportedOperationException("IsDirty is not supported on tombstones"); + } + + /** + * Gets the value of the 'v2' field. + */ + public org.apache.gora.examples.generated.V2 getV2() { + throw new java.lang.UnsupportedOperationException("Get is not supported on tombstones"); + } + + /** + * Sets the value of the 'v2' field. + * @param value the value to set. + */ + public void setV2(org.apache.gora.examples.generated.V2 value) { + throw new java.lang.UnsupportedOperationException("Set is not supported on tombstones"); + } + + /** + * Checks the dirty status of the 'v2' field. A field is dirty if it represents a change that has not yet been written to the database. + * @param value the value to set. + */ + public boolean isV2Dirty(org.apache.gora.examples.generated.V2 value) { + throw new java.lang.UnsupportedOperationException("IsDirty is not supported on tombstones"); + } + + + } + +} \ No newline at end of file Added: gora/branches/GORA_94/gora-core/src/examples/java/org/apache/gora/examples/generated/V2.java URL: http://svn.apache.org/viewvc/gora/branches/GORA_94/gora-core/src/examples/java/org/apache/gora/examples/generated/V2.java?rev=1517093&view=auto ============================================================================== --- gora/branches/GORA_94/gora-core/src/examples/java/org/apache/gora/examples/generated/V2.java (added) +++ gora/branches/GORA_94/gora-core/src/examples/java/org/apache/gora/examples/generated/V2.java Sat Aug 24 00:27:39 2013 @@ -0,0 +1,199 @@ +/** + * Autogenerated by Avro + * + * DO NOT EDIT DIRECTLY + */ +package org.apache.gora.examples.generated; +@SuppressWarnings("all") +public class V2 extends org.apache.gora.persistency.impl.PersistentBase implements org.apache.avro.specific.SpecificRecord, org.apache.gora.persistency.Persistent { + public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"record\",\"name\":\"V2\",\"namespace\":\"org.apache.gora.examples.generated\",\"fields\":[{\"name\":\"__g__dirty\",\"type\":\"bytes\",\"doc\":\"Bytes used to represent weather or not a field is dirty.\",\"default\":\"AA==\"},{\"name\":\"v3\",\"type\":\"int\"}]}"); + /** Bytes used to represent weather or not a field is dirty. */ + private java.nio.ByteBuffer __g__dirty = java.nio.ByteBuffer.wrap(new byte[1]); + private int v3; + public org.apache.avro.Schema getSchema() { return SCHEMA$; } + // Used by DatumWriter. Applications should not call. + public java.lang.Object get(int field$) { + switch (field$) { + case 0: return __g__dirty; + case 1: return v3; + default: throw new org.apache.avro.AvroRuntimeException("Bad index"); + } + } + // Used by DatumReader. Applications should not call. + @SuppressWarnings(value="unchecked") + public void put(int field$, java.lang.Object value$) { + switch (field$) { + case 0: __g__dirty = (java.nio.ByteBuffer)value$; break; + case 1: v3 = (java.lang.Integer)value$; break; + default: throw new org.apache.avro.AvroRuntimeException("Bad index"); + } + } + + /** + * Gets the value of the 'v3' field. + */ + public java.lang.Integer getV3() { + return v3; + } + + /** + * Sets the value of the 'v3' field. + * @param value the value to set. + */ + public void setV3(java.lang.Integer value) { + this.v3 = value; + setDirty(1); + } + + /** + * Checks the dirty status of the 'v3' field. A field is dirty if it represents a change that has not yet been written to the database. + * @param value the value to set. + */ + public boolean isV3Dirty(java.lang.Integer value) { + return isDirty(1); + } + + /** Creates a new V2 RecordBuilder */ + public static org.apache.gora.examples.generated.V2.Builder newBuilder() { + return new org.apache.gora.examples.generated.V2.Builder(); + } + + /** Creates a new V2 RecordBuilder by copying an existing Builder */ + public static org.apache.gora.examples.generated.V2.Builder newBuilder(org.apache.gora.examples.generated.V2.Builder other) { + return new org.apache.gora.examples.generated.V2.Builder(other); + } + + /** Creates a new V2 RecordBuilder by copying an existing V2 instance */ + public static org.apache.gora.examples.generated.V2.Builder newBuilder(org.apache.gora.examples.generated.V2 other) { + return new org.apache.gora.examples.generated.V2.Builder(other); + } + + private static java.nio.ByteBuffer deepCopyToWriteOnlyBuffer( + java.nio.ByteBuffer input) { + java.nio.ByteBuffer copy = java.nio.ByteBuffer.allocate(input.capacity()); + int position = input.position(); + input.reset(); + int mark = input.position(); + int limit = input.limit(); + input.rewind(); + input.limit(input.capacity()); + copy.put(input); + input.rewind(); + copy.rewind(); + input.position(mark); + input.mark(); + copy.position(mark); + copy.mark(); + input.position(position); + copy.position(position); + input.limit(limit); + copy.limit(limit); + return copy.asReadOnlyBuffer(); + } + + /** + * RecordBuilder for V2 instances. + */ + public static class Builder extends org.apache.avro.specific.SpecificRecordBuilderBase + implements org.apache.avro.data.RecordBuilder { + + private java.nio.ByteBuffer __g__dirty; + private int v3; + + /** Creates a new Builder */ + private Builder() { + super(org.apache.gora.examples.generated.V2.SCHEMA$); + } + + /** Creates a Builder by copying an existing Builder */ + private Builder(org.apache.gora.examples.generated.V2.Builder other) { + super(other); + } + + /** Creates a Builder by copying an existing V2 instance */ + private Builder(org.apache.gora.examples.generated.V2 other) { + super(org.apache.gora.examples.generated.V2.SCHEMA$); + if (isValidValue(fields()[0], other.__g__dirty)) { + this.__g__dirty = (java.nio.ByteBuffer) data().deepCopy(fields()[0].schema(), other.__g__dirty); + fieldSetFlags()[0] = true; + } + if (isValidValue(fields()[1], other.v3)) { + this.v3 = (java.lang.Integer) data().deepCopy(fields()[1].schema(), other.v3); + fieldSetFlags()[1] = true; + } + } + + /** Gets the value of the 'v3' field */ + public java.lang.Integer getV3() { + return v3; + } + + /** Sets the value of the 'v3' field */ + public org.apache.gora.examples.generated.V2.Builder setV3(int value) { + validate(fields()[1], value); + this.v3 = value; + fieldSetFlags()[1] = true; + return this; + } + + /** Checks whether the 'v3' field has been set */ + public boolean hasV3() { + return fieldSetFlags()[1]; + } + + /** Clears the value of the 'v3' field */ + public org.apache.gora.examples.generated.V2.Builder clearV3() { + fieldSetFlags()[1] = false; + return this; + } + + @Override + public V2 build() { + try { + V2 record = new V2(); + record.__g__dirty = fieldSetFlags()[0] ? this.__g__dirty : (java.nio.ByteBuffer) defaultValue(fields()[0]); + record.v3 = fieldSetFlags()[1] ? this.v3 : (java.lang.Integer) defaultValue(fields()[1]); + return record; + } catch (Exception e) { + throw new org.apache.avro.AvroRuntimeException(e); + } + } + } + + public V2.Tombstone getTombstone(){ + return TOMBSTONE; + } + + private static final Tombstone TOMBSTONE = new Tombstone(); + + public static final class Tombstone extends V2 implements org.apache.gora.persistency.Tombstone { + + private Tombstone() { } + + /** + * Gets the value of the 'v3' field. + */ + public java.lang.Integer getV3() { + throw new java.lang.UnsupportedOperationException("Get is not supported on tombstones"); + } + + /** + * Sets the value of the 'v3' field. + * @param value the value to set. + */ + public void setV3(java.lang.Integer value) { + throw new java.lang.UnsupportedOperationException("Set is not supported on tombstones"); + } + + /** + * Checks the dirty status of the 'v3' field. A field is dirty if it represents a change that has not yet been written to the database. + * @param value the value to set. + */ + public boolean isV3Dirty(java.lang.Integer value) { + throw new java.lang.UnsupportedOperationException("IsDirty is not supported on tombstones"); + } + + + } + +} \ No newline at end of file Modified: gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/avro/store/AvroStore.java URL: http://svn.apache.org/viewvc/gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/avro/store/AvroStore.java?rev=1517093&r1=1517092&r2=1517093&view=diff ============================================================================== --- gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/avro/store/AvroStore.java (original) +++ gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/avro/store/AvroStore.java Sat Aug 24 00:27:39 2013 @@ -28,7 +28,9 @@ import org.apache.avro.io.BinaryEncoder; import org.apache.avro.io.DatumReader; import org.apache.avro.io.DatumWriter; import org.apache.avro.io.Decoder; +import org.apache.avro.io.DecoderFactory; import org.apache.avro.io.Encoder; +import org.apache.avro.io.EncoderFactory; import org.apache.avro.io.JsonDecoder; import org.apache.avro.io.JsonEncoder; import org.apache.avro.specific.SpecificDatumReader; @@ -220,20 +222,19 @@ public class AvroStore { + implements Deserializer { private BinaryDecoder decoder; - private Class persistentClass; + private Class persistentClass; private boolean reuseObjects; - private PersistentDatumReader datumReader; + private SpecificDatumReader datumReader; - public PersistentDeserializer(Class c, boolean reuseObjects) { + public PersistentDeserializer(Class c, boolean reuseObjects) { this.persistentClass = c; this.reuseObjects = reuseObjects; try { Schema schema = AvroUtils.getSchema(persistentClass); - datumReader = new PersistentDatumReader(schema, true); + datumReader = new SpecificDatumReader(schema); } catch (Exception ex) { throw new RuntimeException(ex); } } - //@Override + @Override public void open(InputStream in) throws IOException { /* It is very important to use a direct buffer, since Hadoop - * supplies an input stream that is only valid until the end of one - * record serialization. Each time deserialize() is called, the IS - * is advanced to point to the right location, so we should not - * buffer the whole input stream at once. - */ - decoder = new DecoderFactory().configureDirectDecoder(true) - .createBinaryDecoder(in, decoder); +* supplies an input stream that is only valid until the end of one +* record serialization. Each time deserialize() is called, the IS +* is advanced to point to the right location, so we should not +* buffer the whole input stream at once. +*/ + decoder = DecoderFactory.get().directBinaryDecoder(in, decoder); } - //@Override + @Override public void close() throws IOException { } @Override - public PersistentBase deserialize(PersistentBase persistent) throws IOException { + public Persistent deserialize(Persistent persistent) throws IOException { return datumReader.read(reuseObjects ? persistent : null, decoder); } } Modified: gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/mapreduce/PersistentSerialization.java URL: http://svn.apache.org/viewvc/gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/mapreduce/PersistentSerialization.java?rev=1517093&r1=1517092&r2=1517093&view=diff ============================================================================== --- gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/mapreduce/PersistentSerialization.java (original) +++ gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/mapreduce/PersistentSerialization.java Sat Aug 24 00:27:39 2013 @@ -17,13 +17,13 @@ */ package org.apache.gora.mapreduce; -import org.apache.gora.persistency.impl.PersistentBase; import org.apache.gora.persistency.Persistent; import org.apache.hadoop.io.serializer.Deserializer; import org.apache.hadoop.io.serializer.Serialization; import org.apache.hadoop.io.serializer.Serializer; -public class PersistentSerialization implements Serialization { +public class PersistentSerialization +implements Serialization { @Override public boolean accept(Class c) { @@ -31,12 +31,12 @@ public class PersistentSerialization imp } @Override - public Deserializer getDeserializer(Class c) { + public Deserializer getDeserializer(Class c) { return new PersistentDeserializer(c, true); } @Override - public Serializer getSerializer(Class c) { + public Serializer getSerializer(Class c) { return new PersistentSerializer(); } } Modified: gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/mapreduce/PersistentSerializer.java URL: http://svn.apache.org/viewvc/gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/mapreduce/PersistentSerializer.java?rev=1517093&r1=1517092&r2=1517093&view=diff ============================================================================== --- gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/mapreduce/PersistentSerializer.java (original) +++ gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/mapreduce/PersistentSerializer.java Sat Aug 24 00:27:39 2013 @@ -1,57 +1,57 @@ /** - * 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. - */ +* 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.gora.mapreduce; import java.io.IOException; import java.io.OutputStream; import org.apache.avro.io.BinaryEncoder; -import org.apache.gora.avro.PersistentDatumWriter; -import org.apache.gora.persistency.impl.PersistentBase; +import org.apache.avro.io.EncoderFactory; +import org.apache.avro.specific.SpecificDatumWriter; +import org.apache.gora.persistency.Persistent; import org.apache.hadoop.io.serializer.Serializer; /** - * Hadoop serializer using {@link PersistentDatumWriter} - * with {@link BinaryEncoder}. - */ -public class PersistentSerializer implements Serializer { +* Hadoop serializer using {@link SpecificDatumWriter} +* with {@link BinaryEncoder}. +*/ +public class PersistentSerializer implements Serializer { - private PersistentDatumWriter datumWriter; - private BinaryEncoder encoder; + private SpecificDatumWriter datumWriter; + private BinaryEncoder encoder; public PersistentSerializer() { - this.datumWriter = new PersistentDatumWriter(); + this.datumWriter = new SpecificDatumWriter(); } - //@Override + @Override public void close() throws IOException { encoder.flush(); } - //@Override + @Override public void open(OutputStream out) throws IOException { - encoder = new BinaryEncoder(out); + encoder = EncoderFactory.get().directBinaryEncoder(out, null); } @Override - public void serialize(PersistentBase persistent) throws IOException { + public void serialize(Persistent persistent) throws IOException { datumWriter.setSchema(persistent.getSchema()); - datumWriter.setPersistent(persistent); datumWriter.write(persistent, encoder); } Modified: gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/memory/store/MemStore.java URL: http://svn.apache.org/viewvc/gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/memory/store/MemStore.java?rev=1517093&r1=1517092&r2=1517093&view=diff ============================================================================== --- gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/memory/store/MemStore.java (original) +++ gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/memory/store/MemStore.java Sat Aug 24 00:27:39 2013 @@ -26,9 +26,11 @@ import java.util.List; import java.util.NavigableMap; import java.util.TreeMap; + +import org.apache.avro.Schema.Field; + import org.apache.gora.persistency.Persistent; import org.apache.gora.persistency.impl.PersistentBase; -import org.apache.gora.persistency.impl.StateManagerImpl; import org.apache.gora.query.PartitionQuery; import org.apache.gora.query.Query; import org.apache.gora.query.Result; @@ -37,6 +39,7 @@ import org.apache.gora.query.impl.QueryB import org.apache.gora.query.impl.ResultBase; import org.apache.gora.store.DataStore; import org.apache.gora.store.impl.DataStoreBase; +import org.apache.gora.util.AvroUtils; /** * Memory based {@link DataStore} implementation for tests. @@ -99,19 +102,19 @@ public class MemStore query) { - try{ - long deletedRows = 0; - Result result = query.execute(); - - while(result.next()) { - if(delete(result.getKey())) - deletedRows++; - } - return 0; - } - catch(Exception e){ - return 0; - } + try{ + long deletedRows = 0; + Result result = query.execute(); + + while(result.next()) { + if(delete(result.getKey())) + deletedRows++; + } + return 0; + } + catch(Exception e){ + return 0; + } } @Override @@ -142,15 +145,19 @@ public class MemStore T getPersistent(T obj, String[] fields) { - if(Arrays.equals(fields, obj.getFields())) { + List otherFields = obj.getSchema().getFields(); + String[] otherFieldStrings = new String[otherFields.size()]; + for(int i = 0; i getUnmanagedFields(); } \ No newline at end of file Added: gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/Tombstone.java URL: http://svn.apache.org/viewvc/gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/Tombstone.java?rev=1517093&view=auto ============================================================================== --- gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/Tombstone.java (added) +++ gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/Tombstone.java Sat Aug 24 00:27:39 2013 @@ -0,0 +1,16 @@ +package org.apache.gora.persistency; + +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; +import java.util.Map; +import java.util.Set; + +/** + * Marker interface used to signal a deletion to Gora. + */ +public interface Tombstone { + +} Added: gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/Tombstones.java URL: http://svn.apache.org/viewvc/gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/Tombstones.java?rev=1517093&view=auto ============================================================================== --- gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/Tombstones.java (added) +++ gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/Tombstones.java Sat Aug 24 00:27:39 2013 @@ -0,0 +1,242 @@ +/** +* 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.gora.persistency; + +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; +import java.util.Map; +import java.util.Set; + +public final class Tombstones { + + private Tombstones() { + + } + + public static final class MapTombstone implements Tombstone, Map { + + private MapTombstone() { + } + + private static final Map DELEGATE = Collections.EMPTY_MAP; + + public int size() { + return DELEGATE.size(); + } + + public boolean isEmpty() { + return DELEGATE.isEmpty(); + } + + public boolean containsKey(Object key) { + return DELEGATE.containsKey(key); + } + + public boolean containsValue(Object value) { + return DELEGATE.containsValue(value); + } + + @SuppressWarnings("unchecked") + public V get(Object key) { + return (V) DELEGATE.get(key); + } + + @SuppressWarnings("unchecked") + public V put(Object key, Object value) { + return (V) DELEGATE.put(key, value); + } + + @SuppressWarnings("unchecked") + public V remove(Object key) { + return (V) DELEGATE.remove(key); + } + + @SuppressWarnings("unchecked") + public void putAll(Map m) { + DELEGATE.putAll(m); + } + + public void clear() { + DELEGATE.clear(); + } + + @SuppressWarnings("unchecked") + public Set keySet() { + return DELEGATE.keySet(); + } + + @SuppressWarnings("unchecked") + public Collection values() { + return DELEGATE.values(); + } + + @SuppressWarnings("unchecked") + public Set entrySet() { + return DELEGATE.entrySet(); + } + + public boolean equals(Object o) { + return DELEGATE.equals(o); + } + + public int hashCode() { + return DELEGATE.hashCode(); + } + + } + + public static final class ListTombstone implements List, Tombstone { + + private static final List DELEGATE = Collections.EMPTY_LIST; + + private ListTombstone() { + } + + public int size() { + return DELEGATE.size(); + } + + public boolean isEmpty() { + return DELEGATE.isEmpty(); + } + + public boolean contains(Object o) { + return DELEGATE.contains(o); + } + + public Iterator iterator() { + return DELEGATE.iterator(); + } + + public Object[] toArray() { + return DELEGATE.toArray(); + } + + @SuppressWarnings("unchecked") + public Object[] toArray(Object[] a) { + return DELEGATE.toArray(a); + } + + @SuppressWarnings("unchecked") + public boolean add(Object e) { + return DELEGATE.add(e); + } + + public boolean remove(Object o) { + return DELEGATE.remove(o); + } + + @SuppressWarnings("unchecked") + public boolean containsAll(Collection c) { + return DELEGATE.containsAll(c); + } + + @SuppressWarnings("unchecked") + public boolean addAll(Collection c) { + return DELEGATE.addAll(c); + } + + @SuppressWarnings("unchecked") + public boolean addAll(int index, Collection c) { + return DELEGATE.addAll(index, c); + } + + @SuppressWarnings("unchecked") + public boolean removeAll(Collection c) { + return DELEGATE.removeAll(c); + } + + @SuppressWarnings("unchecked") + public boolean retainAll(Collection c) { + return DELEGATE.retainAll(c); + } + + public void clear() { + DELEGATE.clear(); + } + + public boolean equals(Object o) { + return DELEGATE.equals(o); + } + + public int hashCode() { + return DELEGATE.hashCode(); + } + + @SuppressWarnings("unchecked") + public T get(int index) { + return (T) DELEGATE.get(index); + } + + @SuppressWarnings("unchecked") + public Object set(int index, Object element) { + return DELEGATE.set(index, element); + } + + @SuppressWarnings("unchecked") + public void add(int index, Object element) { + DELEGATE.add(index, element); + } + + @SuppressWarnings("unchecked") + public T remove(int index) { + return (T) DELEGATE.remove(index); + } + + public int indexOf(Object o) { + return DELEGATE.indexOf(o); + } + + public int lastIndexOf(Object o) { + return DELEGATE.lastIndexOf(o); + } + + public ListIterator listIterator() { + return DELEGATE.listIterator(); + } + + public ListIterator listIterator(int index) { + return DELEGATE.listIterator(index); + } + + public List subList(int fromIndex, int toIndex) { + return DELEGATE.subList(fromIndex, toIndex); + } + + } + + public static final MapTombstone MAP_TOMBSTONE = new MapTombstone(); + + public static MapTombstone getMapTombstone() { + return MAP_TOMBSTONE; + } + + public static final ListTombstone LIST_TOMBSTONE = new ListTombstone(); + + public static final ListTombstone getListTombstone() { + return LIST_TOMBSTONE; + } + + public static boolean isTombstone(Object o) { + return (o instanceof Tombstone); + } + +} Modified: gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/impl/BeanFactoryImpl.java URL: http://svn.apache.org/viewvc/gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/impl/BeanFactoryImpl.java?rev=1517093&r1=1517092&r2=1517093&view=diff ============================================================================== --- gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/impl/BeanFactoryImpl.java (original) +++ gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/impl/BeanFactoryImpl.java Sat Aug 24 00:27:39 2013 @@ -31,16 +31,29 @@ import org.apache.gora.util.ReflectionUt */ public class BeanFactoryImpl implements BeanFactory { + /** Class of the key to be used */ private Class keyClass; + + /** Class of the persistent objects to be stored */ private Class persistentClass; + /** Constructor of the key */ private Constructor keyConstructor; + /** Object's key */ private K key; + + /** Persistent object of class T */ private T persistent; + /** Flag to be used to determine if a key is persistent or not */ private boolean isKeyPersistent = false; + /** + * + * @param keyClass + * @param persistentClass + */ public BeanFactoryImpl(Class keyClass, Class persistentClass) { this.keyClass = keyClass; this.persistentClass = persistentClass; @@ -59,21 +72,19 @@ public class BeanFactoryImpl + * The type of the list that this wrapper wraps. + */ +public class DirtyCollectionWrapper implements Dirtyable, + Collection { + + /** The delegate list that the wrapper wraps */ + private final Collection delegate; + /** + * The dirty flag, tracks if the structure of the underlying list has been + * modified + */ + private DirtyFlag dirtyFlag; + + DirtyCollectionWrapper(Collection delegate2, DirtyFlag dirtyFlag) { + this.delegate = delegate2; + this.dirtyFlag = dirtyFlag; + } + + @Override + public boolean isDirty() { + boolean anyDirty = false; + for (T value : this) { + anyDirty = anyDirty || (value instanceof Dirtyable) ? ((Dirtyable) value).isDirty():false; + } + return anyDirty || dirtyFlag.isDirty(); + } + + @Override + public void clearDirty() { + for (T value : this) { + if (value instanceof Dirtyable) + ((Dirtyable) value).clearDirty(); + } + dirtyFlag.clearDirty(); + } + + @Override + public int size() { + return delegate.size(); + } + + @Override + public boolean isEmpty() { + return delegate.isEmpty(); + } + + @Override + public boolean contains(Object o) { + return delegate.contains(o); + } + + @Override + public Iterator iterator() { + return new DirtyIteratorWrapper(delegate.iterator(), dirtyFlag); + } + + @Override + public Object[] toArray() { + return delegate.toArray(); + } + + @Override + public R[] toArray(R[] a) { + return delegate.toArray(a); + } + + @Override + public boolean add(T e) { + boolean change = delegate.add(e); + dirtyFlag.makeDirty(change); + return change; + } + + @Override + public boolean remove(Object o) { + boolean change = delegate.remove(o); + dirtyFlag.makeDirty(change); + return change; + } + + @Override + public boolean containsAll(Collection c) { + return delegate.containsAll(c); + } + + @Override + public boolean addAll(Collection c) { + boolean change = delegate.addAll(c); + dirtyFlag.makeDirty(change); + return change; + } + + @Override + public boolean removeAll(Collection c) { + boolean change = delegate.removeAll(c); + dirtyFlag.makeDirty(change); + return change; + } + + @Override + public boolean retainAll(Collection c) { + boolean change = delegate.retainAll(c); + dirtyFlag.makeDirty(change); + return change; + } + + @Override + public void clear() { + dirtyFlag.makeDirty(size() > 0); + delegate.clear(); + } + + @Override + public boolean equals(Object o) { + return delegate.equals(o); + } + + @Override + public int hashCode() { + return delegate.hashCode(); + } + + protected Collection getDelegate() { + return delegate; + } + + protected DirtyFlag getDirtyFlag() { + return dirtyFlag; + } + +} Added: gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/impl/DirtyFlag.java URL: http://svn.apache.org/viewvc/gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/impl/DirtyFlag.java?rev=1517093&view=auto ============================================================================== --- gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/impl/DirtyFlag.java (added) +++ gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/impl/DirtyFlag.java Sat Aug 24 00:27:39 2013 @@ -0,0 +1,56 @@ +/** + * 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.gora.persistency.impl; + +import org.apache.gora.persistency.Dirtyable; + +final class DirtyFlag implements Dirtyable { + + private boolean dirty; + + public DirtyFlag(boolean dirty) { + this.dirty = dirty; + } + + public DirtyFlag() { + this.dirty = false; + } + + @Override + public boolean isDirty() { + return dirty; + } + + @Override + public void clearDirty() { + this.dirty = false; + } + + /** + * Set this DirtyFlag to dirty if the dirty operand is true. If + * not, the state of the flag remains unchanged. + * + * @param dirty + * Weather or not to set this flag to dirty. If false, the state + * is unchanged. + */ + public void makeDirty(boolean dirty) { + this.dirty = this.dirty || dirty; + } + +} Added: gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/impl/DirtyIteratorWrapper.java URL: http://svn.apache.org/viewvc/gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/impl/DirtyIteratorWrapper.java?rev=1517093&view=auto ============================================================================== --- gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/impl/DirtyIteratorWrapper.java (added) +++ gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/impl/DirtyIteratorWrapper.java Sat Aug 24 00:27:39 2013 @@ -0,0 +1,35 @@ +package org.apache.gora.persistency.impl; + +import java.util.Iterator; + +/** + * Sets the dirty flag if the iterator's remove method is called. + */ +final class DirtyIteratorWrapper implements Iterator { + + private final DirtyFlag dirtyFlag; + private Iterator delegateIterator; + + DirtyIteratorWrapper(Iterator delegateIterator, + DirtyFlag dirtyFlag) { + this.delegateIterator = delegateIterator; + this.dirtyFlag = dirtyFlag; + } + + @Override + public boolean hasNext() { + return delegateIterator.hasNext(); + } + + @Override + public T next() { + return delegateIterator.next(); + } + + @Override + public void remove() { + dirtyFlag.makeDirty(true); + delegateIterator.remove(); + } + +} \ No newline at end of file Added: gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/impl/DirtyListIterator.java URL: http://svn.apache.org/viewvc/gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/impl/DirtyListIterator.java?rev=1517093&view=auto ============================================================================== --- gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/impl/DirtyListIterator.java (added) +++ gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/impl/DirtyListIterator.java Sat Aug 24 00:27:39 2013 @@ -0,0 +1,67 @@ +package org.apache.gora.persistency.impl; + +import java.util.ListIterator; + +/** + * Sets the dirty flag if the list iterator's modification methods (remove, + * set, add) are called. + */ +final class DirtyListIterator implements ListIterator { + + private final ListIterator iteratorDelegate; + private final DirtyFlag dirtyFlag; + + DirtyListIterator(ListIterator delegate, DirtyFlag dirtyFlag) { + this.iteratorDelegate = delegate; + this.dirtyFlag = dirtyFlag; + } + + @Override + public boolean hasNext() { + return iteratorDelegate.hasNext(); + } + + @Override + public T next() { + return iteratorDelegate.next(); + } + + @Override + public boolean hasPrevious() { + return iteratorDelegate.hasPrevious(); + } + + @Override + public T previous() { + return iteratorDelegate.previous(); + } + + @Override + public int nextIndex() { + return iteratorDelegate.nextIndex(); + } + + @Override + public int previousIndex() { + return iteratorDelegate.previousIndex(); + } + + @Override + public void remove() { + dirtyFlag.makeDirty(true); + iteratorDelegate.remove(); + } + + @Override + public void set(T e) { + dirtyFlag.makeDirty(true); + iteratorDelegate.set(e); + } + + @Override + public void add(T e) { + dirtyFlag.makeDirty(true); + iteratorDelegate.add(e); + } + +} \ No newline at end of file Added: gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/impl/DirtyListWrapper.java URL: http://svn.apache.org/viewvc/gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/impl/DirtyListWrapper.java?rev=1517093&view=auto ============================================================================== --- gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/impl/DirtyListWrapper.java (added) +++ gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/impl/DirtyListWrapper.java Sat Aug 24 00:27:39 2013 @@ -0,0 +1,97 @@ +package org.apache.gora.persistency.impl; + +import java.util.Collection; +import java.util.List; +import java.util.ListIterator; + +import org.apache.gora.persistency.Dirtyable; + +/** + * A {@link List} implementation that wraps another list, intercepting + * modifications to the list structure and reporting on weather or not the list + * has been modified, and also checking list elements for modification. + * + * @param + * The type of the list that this wrapper wraps. + */ +public class DirtyListWrapper extends DirtyCollectionWrapper implements + Dirtyable, List { + + /** + * Create a DirtyListWrapper that wraps a getDelegate(). + * + * @param delegate + * The getDelegate().to wrap. + */ + public DirtyListWrapper(List delegate) { + this(delegate, new DirtyFlag()); + } + + DirtyListWrapper(List delegate, DirtyFlag dirtyFlag) { + super(delegate, dirtyFlag); + } + + @Override + public boolean addAll(int index, Collection c) { + boolean change = getDelegate().addAll(index, c); + getDirtyFlag().makeDirty(change); + return change; + } + + @Override + public T get(int index) { + return getDelegate().get(index); + } + + @Override + public T set(int index, T element) { + getDirtyFlag().makeDirty(true); + return getDelegate().set(index, element); + } + + @Override + public void add(int index, T element) { + getDirtyFlag().makeDirty(true); + getDelegate().add(index, element); + } + + @Override + public T remove(int index) { + getDirtyFlag().makeDirty(true); + return getDelegate().remove(index); + } + + @Override + public int indexOf(Object o) { + return getDelegate().indexOf(o); + } + + @Override + public int lastIndexOf(Object o) { + return getDelegate().lastIndexOf(o); + } + + @Override + public ListIterator listIterator() { + return new DirtyListIterator(getDelegate().listIterator(), + getDirtyFlag()); + } + + @Override + public ListIterator listIterator(int index) { + return new DirtyListIterator(getDelegate().listIterator(index), + getDirtyFlag()); + } + + @Override + public List subList(int fromIndex, int toIndex) { + return new DirtyListWrapper(getDelegate().subList(fromIndex, toIndex), + getDirtyFlag()); + } + + @Override + protected List getDelegate() { + return (List) super.getDelegate(); + } + +} Added: gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/impl/DirtyMapWrapper.java URL: http://svn.apache.org/viewvc/gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/impl/DirtyMapWrapper.java?rev=1517093&view=auto ============================================================================== --- gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/impl/DirtyMapWrapper.java (added) +++ gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/impl/DirtyMapWrapper.java Sat Aug 24 00:27:39 2013 @@ -0,0 +1,194 @@ +package org.apache.gora.persistency.impl; + +import java.util.Collection; +import java.util.Map; +import java.util.Set; + +import org.apache.gora.persistency.Dirtyable; + +import com.google.common.base.Function; +import com.google.common.collect.Collections2; + +public class DirtyMapWrapper implements Map, Dirtyable { + + public static class DirtyEntryWrapper implements Entry, Dirtyable { + private final Entry entryDelegate; + private DirtyFlag dirtyFlag; + + public DirtyEntryWrapper(Entry delegate, DirtyFlag dirtyFlag) { + this.entryDelegate = delegate; + this.dirtyFlag = dirtyFlag; + } + + @Override + public K getKey() { + return entryDelegate.getKey(); + } + + @Override + public V getValue() { + return entryDelegate.getValue(); + } + + @Override + public V setValue(V value) { + dirtyFlag.makeDirty(valueChanged(value, entryDelegate.getValue())); + return entryDelegate.setValue(value); + } + + @Override + public boolean equals(Object o) { + return entryDelegate.equals(o); + } + + @Override + public int hashCode() { + return entryDelegate.hashCode(); + } + + @Override + public boolean isDirty() { + return dirtyFlag.isDirty() || (entryDelegate instanceof Dirtyable) ? ((Dirtyable) entryDelegate + .getValue()).isDirty() : false; + } + + @Override + public void clearDirty() { + dirtyFlag.clearDirty(); + } + + } + + private final Map delegate; + + private final DirtyFlag dirtyFlag; + + public DirtyMapWrapper(Map delegate) { + this(delegate, new DirtyFlag()); + } + + DirtyMapWrapper(Map delegate, DirtyFlag dirtyFlag) { + this.dirtyFlag = dirtyFlag; + this.delegate = delegate; + } + + @Override + public boolean isDirty() { + boolean anyDirty = false; + for (V v : this.values()) { + anyDirty = anyDirty || (v instanceof Dirtyable) ? ((Dirtyable) v) + .isDirty() : false; + } + return anyDirty || dirtyFlag.isDirty(); + } + + @Override + public void clearDirty() { + for (V v : this.values()) { + if (v instanceof Dirtyable) + ((Dirtyable) v).clearDirty(); + } + dirtyFlag.clearDirty(); + } + + @Override + public int size() { + return delegate.size(); + } + + @Override + public boolean isEmpty() { + return delegate.isEmpty(); + } + + @Override + public boolean containsKey(Object key) { + return delegate.containsKey(key); + } + + @Override + public boolean containsValue(Object value) { + return delegate.containsValue(value); + } + + @Override + public V get(Object key) { + return delegate.get(key); + } + + @Override + public V put(K key, V value) { + checkPutWillMakeDirty(key, value); + return delegate.put(key, value); + } + + private void checkPutWillMakeDirty(K key, V value) { + if (containsKey(key)) { + dirtyFlag.makeDirty(valueChanged(value, get(key))); + } else { + dirtyFlag.makeDirty(true); + } + } + + private static boolean valueChanged(V value, V oldValue) { + return (value == null && oldValue != null) + || (value != null && !value.equals(oldValue)); + } + + @Override + public V remove(Object key) { + dirtyFlag.makeDirty(containsKey(key)); + return delegate.remove(key); + } + + @Override + public void putAll(Map m) { + for (Entry entry : m.entrySet()) { + checkPutWillMakeDirty(entry.getKey(), entry.getValue()); + } + delegate.putAll(m); + } + + @Override + public void clear() { + if (delegate.size() != 0) { + dirtyFlag.makeDirty(true); + } + delegate.clear(); + } + + @Override + public Set keySet() { + return delegate.keySet(); + } + + @Override + public Collection values() { + return new DirtyCollectionWrapper(delegate.values(), dirtyFlag); + } + + @Override + @SuppressWarnings({ "unchecked", "rawtypes" }) + public Set> entrySet() { + Collection> dirtyEntrySet = Collections2.transform( + delegate.entrySet(), + new Function, DirtyEntryWrapper>() { + @Override + public DirtyEntryWrapper apply(java.util.Map.Entry input) { + return new DirtyEntryWrapper(input, dirtyFlag); + } + }); + return new DirtySetWrapper(dirtyEntrySet, dirtyFlag); + } + + @Override + public boolean equals(Object o) { + return delegate.equals(o); + } + + @Override + public int hashCode() { + return delegate.hashCode(); + } + +} Added: gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/impl/DirtySetWrapper.java URL: http://svn.apache.org/viewvc/gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/impl/DirtySetWrapper.java?rev=1517093&view=auto ============================================================================== --- gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/impl/DirtySetWrapper.java (added) +++ gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/impl/DirtySetWrapper.java Sat Aug 24 00:27:39 2013 @@ -0,0 +1,15 @@ +package org.apache.gora.persistency.impl; + +import java.util.Collection; +import java.util.Set; + +import org.apache.gora.persistency.Dirtyable; + +public class DirtySetWrapper extends + DirtyCollectionWrapper implements Set { + + DirtySetWrapper(Collection delegate2, DirtyFlag dirtyFlag) { + super(delegate2, dirtyFlag); + } + +} Modified: gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/impl/PersistentBase.java URL: http://svn.apache.org/viewvc/gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/impl/PersistentBase.java?rev=1517093&r1=1517092&r2=1517093&view=diff ============================================================================== --- gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/impl/PersistentBase.java (original) +++ gora/branches/GORA_94/gora-core/src/main/java/org/apache/gora/persistency/impl/PersistentBase.java Sat Aug 24 00:27:39 2013 @@ -18,310 +18,194 @@ package org.apache.gora.persistency.impl; import java.nio.ByteBuffer; -import java.util.HashMap; +import java.util.Collection; import java.util.List; -import java.util.Map; - import org.apache.avro.Schema.Field; -import org.apache.avro.Schema.Type; -import org.apache.avro.generic.GenericData; +import org.apache.avro.specific.SpecificData; import org.apache.avro.specific.SpecificRecord; -import org.apache.gora.avro.PersistentDatumReader; -import org.apache.gora.persistency.ListGenericArray; +import org.apache.avro.specific.SpecificRecordBase; +import org.apache.gora.persistency.Dirtyable; import org.apache.gora.persistency.Persistent; -import org.apache.gora.persistency.StateManager; -import org.apache.gora.persistency.StatefulHashMap; /** * Base classs implementing common functionality for Persistent * classes. */ -public abstract class PersistentBase implements Persistent, SpecificRecord { - - protected static Map, Map> FIELD_MAP = - new HashMap, Map>(); - - protected static Map, String[]> FIELDS = - new HashMap, String[]>(); +public abstract class PersistentBase extends SpecificRecordBase implements Persistent { - protected static final PersistentDatumReader datumReader = - new PersistentDatumReader(); - - private StateManager stateManager; + public static class PersistentData extends SpecificData { + private static final PersistentData INSTANCE = new PersistentData(); - protected PersistentBase() { - this(new StateManagerImpl()); - } - - protected PersistentBase(StateManager stateManager) { - this.stateManager = stateManager; - stateManager.setManagedPersistent(this); - } - - /** Subclasses should call this function for all the persistable fields - * in the class to register them. - * @param clazz the Persistent class - * @param fields the name of the fields of the class - */ - protected static void registerFields(Class clazz, String... fields) { - FIELDS.put(clazz, fields); - int fieldsLength = fields == null ? 0 :fields.length; - HashMap map = new HashMap(fieldsLength); - - for(int i=0; i < fieldsLength; i++) { - map.put(fields[i], i); + public static PersistentData get() { + return INSTANCE; } - FIELD_MAP.put(clazz, map); - } - - @Override - public StateManager getStateManager() { - return stateManager; - } - @Override - public String[] getFields() { - return FIELDS.get(getClass()); - } + public boolean equals(SpecificRecord obj1, SpecificRecord that) { + if (that == obj1) + return true; // identical object + if (!(that instanceof SpecificRecord)) + return false; // not a record + if (obj1.getClass() != that.getClass()) + return false; // not same schema + return PersistentData.get().compare(obj1, that, obj1.getSchema(), true) == 0; + } - @Override - public String getField(int index) { - return FIELDS.get(getClass())[index]; } @Override - public int getFieldIndex(String field) { - return FIELD_MAP.get(getClass()).get(field); + public void clearDirty() { + ByteBuffer dirtyBytes = getDirtyBytes(); + assert (dirtyBytes.position() == 0); + for (int i = 0; i < dirtyBytes.limit(); i++) { + dirtyBytes.put(i, (byte) 0); + } + for (Field field : getSchema().getFields()) { + clearDirynessIfFieldIsDirtyable(field.pos()); + } } - @Override - @SuppressWarnings("rawtypes") - public void clear() { - List fields = getSchema().getFields(); - - for(int i=0; i fields = getSchema().getFields(); + boolean isSubRecordDirty = false; + for (Field field : fields) { + isSubRecordDirty = isSubRecordDirty || checkIfMutableFieldAndDirty(field); + } + ByteBuffer dirtyBytes = getDirtyBytes(); + assert (dirtyBytes.position() == 0); + boolean dirty = false; + for (int i = 0; i < dirtyBytes.limit(); i++) { + dirty = dirty || dirtyBytes.get(i) != 0; + } + return isSubRecordDirty || dirty; } - @Override - public boolean isDirty() { - return getStateManager().isDirty(this); + private boolean checkIfMutableFieldAndDirty(Field field) { + if (field.pos() == 0) + return false; + switch (field.schema().getType()) { + case RECORD: + case MAP: + case ARRAY: + return ((Dirtyable) get(field.pos())).isDirty(); + case UNION: + Object value = get(field.pos()); + if (value instanceof Dirtyable) { + return ((Dirtyable) value).isDirty(); + } + default: + // TODO add sufficient logging + break; + } + return false; } @Override public boolean isDirty(int fieldIndex) { - return getStateManager().isDirty(this, fieldIndex); + Field field = getSchema().getFields().get(fieldIndex); + boolean isSubRecordDirty = checkIfMutableFieldAndDirty(field); + ByteBuffer dirtyBytes = getDirtyBytes(); + assert (dirtyBytes.position() == 0); + int byteOffset = fieldIndex / 8; + int bitOffset = fieldIndex % 8; + byte currentByte = dirtyBytes.get(byteOffset); + return isSubRecordDirty || 0 != ((1 << bitOffset) & currentByte); } @Override - public boolean isDirty(String field) { - return isDirty(getFieldIndex(field)); + public boolean isDirty(String fieldName) { + Field field = getSchema().getField(fieldName); + if(field == null){ + throw new IndexOutOfBoundsException("Field "+ fieldName + " does not exist in this schema."); + } + return isDirty(field.pos()); } @Override public void setDirty() { - getStateManager().setDirty(this); + ByteBuffer dirtyBytes = getDirtyBytes(); + assert (dirtyBytes.position() == 0); + for (int i = 0; i < dirtyBytes.limit(); i++) { + dirtyBytes.put(i, (byte) -128); + } } @Override public void setDirty(int fieldIndex) { - getStateManager().setDirty(this, fieldIndex); + ByteBuffer dirtyBytes = getDirtyBytes(); + assert (dirtyBytes.position() == 0); + int byteOffset = fieldIndex / 8; + int bitOffset = fieldIndex % 8; + byte currentByte = dirtyBytes.get(byteOffset); + currentByte = (byte) ((1 << bitOffset) | currentByte); + dirtyBytes.put(byteOffset, currentByte); } @Override public void setDirty(String field) { - setDirty(getFieldIndex(field)); - } - - @Override - public void clearDirty(int fieldIndex) { - getStateManager().clearDirty(this, fieldIndex); - } - - @Override - public void clearDirty(String field) { - clearDirty(getFieldIndex(field)); - } - - @Override - public void clearDirty() { - getStateManager().clearDirty(this); - } - - @Override - public boolean isReadable(int fieldIndex) { - return getStateManager().isReadable(this, fieldIndex); - } - - @Override - public boolean isReadable(String field) { - return isReadable(getFieldIndex(field)); + setDirty(getSchema().getField(field).pos()); } - @Override - public void setReadable(int fieldIndex) { - getStateManager().setReadable(this, fieldIndex); - } - - @Override - public void setReadable(String field) { - setReadable(getFieldIndex(field)); + private ByteBuffer getDirtyBytes() { + return (ByteBuffer) get(0); } @Override - public void clearReadable() { - getStateManager().clearReadable(this); - } - - @Override - public void clearReadable(int fieldIndex) { - getStateManager().clearReadable(this, fieldIndex); - } - - @Override - public void clearReadable(String field) { - clearReadable(getFieldIndex(field)); - } - - //@Override - public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof SpecificRecord)) return false; - - SpecificRecord r2 = (SpecificRecord)o; - if (!this.getSchema().equals(r2.getSchema())) return false; - - return this.hashCode() == r2.hashCode(); - } - - //@Override - public int hashCode() { - final int prime = 31; - int result = 1; - List fields = this.getSchema().getFields(); - int end = fields.size(); - for (int i = 0; i < end; i++) { - result = prime * result + getFieldHashCode(i, fields.get(i)); - } - return result; - } - - /** - * Computes a (record's) field's hash code. - * @param i Index of the field in the actual - * @param field - * @return - */ - private int getFieldHashCode(int i, Field field) { - Object o = get(i); - if(o == null) - return 0; - - // XXX Union special case: in a field being union we have to check the - // inner schemas for Type.BYTES special case, but because it is not a - // field we check it this way. Too simple case to create another - // private method - boolean isUnionField = false ; - int unionIndex = -1 ; - - if (field.schema().getType() == Type.UNION) { - isUnionField = true ; - unionIndex = GenericData.get().resolveUnion(field.schema(), o); - } - - if(field.schema().getType() == Type.BYTES - || (isUnionField - && field.schema().getTypes().get(unionIndex).getType() == Type.BYTES)) { - // ByteBuffer.hashCode() depends on internal 'position' index, but we must ignore that. - return getByteBufferHashCode((ByteBuffer)o); + public void clear() { + Collection unmanagedFields = getUnmanagedFields(); + for (Field field : getSchema().getFields()) { + if (!unmanagedFields.contains(field)) + continue; + /* +* TODO: Its more in the spirit of Gora's clear method to actually clear +* data structures, but since avro no-longer defaults to having empty +* structures the way to do this consistently would be complicated. +*/ + put(field.pos(), null); } - - return o.hashCode(); + clearDirty(); } - /** ByteBuffer.hashCode() takes into account the position of the - * buffer, but we do not want that*/ - private int getByteBufferHashCode(ByteBuffer buf) { - int h = 1; - int p = buf.arrayOffset(); - for (int j = buf.limit() - 1; j >= p; j--) - h = 31 * h + buf.get(j); - return h; - } - @Override - public Persistent clone() { - return datumReader.clone(this, getSchema()); + public boolean equals(Object that) { + if (that == this) { + return true; + } else if (that instanceof Persistent) { + return PersistentData.get().equals(this, (SpecificRecord) that); + } else { + return false; + } } - //@Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append(super.toString()); - builder.append(" {\n"); + public List getUnmanagedFields(){ List fields = getSchema().getFields(); - for(int i=0; i