Default implementation of DataBag. This is the an abstract class used as a parent for all three of the types of data bags.
| 44 | * parent for all three of the types of data bags. |
| 45 | */ |
| 46 | @SuppressWarnings("serial") |
| 47 | public abstract class DefaultAbstractBag implements DataBag { |
| 48 | |
| 49 | private static final Log log = LogFactory.getLog(DataBag.class); |
| 50 | |
| 51 | // If we grow past 100K, may be worthwhile to register. |
| 52 | private static final int SPILL_REGISTER_THRESHOLD = 100 * 1024; |
| 53 | |
| 54 | private static PigLogger pigLogger; |
| 55 | |
| 56 | private static InterSedes sedes = InterSedesFactory.getInterSedesInstance(); |
| 57 | // Container that holds the tuples. Actual object instantiated by |
| 58 | // subclasses. |
| 59 | protected Collection<Tuple> mContents; |
| 60 | |
| 61 | // Spill files we've created. These need to be removed in finalize. |
| 62 | protected FileList mSpillFiles; |
| 63 | |
| 64 | // Total size, including tuples on disk. Stored here so we don't have |
| 65 | // to run through the disk when people ask. |
| 66 | protected long mSize = 0; |
| 67 | |
| 68 | // Number of tuples to sample per bag, to get an estimate of tuple size |
| 69 | private static final int SPILL_SAMPLE_SIZE = 100; |
| 70 | private static final int SPILL_SAMPLE_FREQUENCY = 10; |
| 71 | |
| 72 | long aggSampleTupleSize = 0; |
| 73 | |
| 74 | int sampled = 0; |
| 75 | |
| 76 | private boolean spillableRegistered = false; |
| 77 | |
| 78 | /** |
| 79 | * Get the number of elements in the bag, both in memory and on disk. |
| 80 | */ |
| 81 | @Override |
| 82 | public long size() { |
| 83 | return mSize; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | /** |
| 88 | * Sample every SPILL_SAMPLE_FREQUENCYth tuple |
| 89 | * until we reach a max of SPILL_SAMPLE_SIZE |
| 90 | * to get an estimate of the tuple sizes. |
| 91 | */ |
| 92 | protected void sampleContents() { |
| 93 | synchronized (mContents) { |
| 94 | Iterator<Tuple> iter = mContents.iterator(); |
| 95 | for (int i = 0; i < sampled * SPILL_SAMPLE_FREQUENCY && iter.hasNext(); i++) { |
| 96 | iter.next(); |
| 97 | } |
| 98 | for (int i = sampled; iter.hasNext() && sampled < SPILL_SAMPLE_SIZE; i++) { |
| 99 | Tuple t = iter.next(); |
| 100 | if (t != null && i % SPILL_SAMPLE_FREQUENCY == 0) { |
| 101 | aggSampleTupleSize += t.getMemorySize(); |
| 102 | sampled += 1; |
| 103 | } |
nothing calls this directly
no test coverage detected