/** This method emits this delta rec to a file */
  public void emit(DataOutput out) throws IOException {
    DBObjectBase baseDef;
    DBObjectBaseField fieldDef;

    /* -- */

    // write the object id

    baseDef = Ganymede.db.getObjectBase(invid.getType());

    if (debug) {
      System.err.println("Emitting delta rec for invid " + invid.toString());
    }

    invid.emit(out);

    // write the fieldrec count

    out.writeInt(fieldRecs.size());

    if (debug) {
      System.err.println(
          "Emitting " + fieldRecs.size() + " field records for invid " + invid.toString());
    }

    // now let's write out the fields

    fieldDeltaRec fdRec;
    Object value;

    for (int i = 0; i < fieldRecs.size(); i++) {
      fdRec = (fieldDeltaRec) fieldRecs.elementAt(i);

      // write out our field code.. this will be used by the loader
      // code to determine what kind of field this is, and what
      // kind of data types need to be loaded.

      if (debug) {
        System.err.println("Emitting fieldDeltaRec:\n\t" + fdRec.toString());
      }

      out.writeShort(fdRec.fieldcode);

      // are we deleting?

      if (!fdRec.vector && fdRec.scalarValue == null) {
        // yes, we're deleting this field
        out.writeBoolean(true);
        continue;
      }

      // no, we're redefining this field

      out.writeBoolean(false);

      // write out our field type code.. this will be used by the loader
      // to verify that the schema hasn't undergone an incompatible
      // change since the journal was written.

      fieldDef = (DBObjectBaseField) baseDef.getField(fdRec.fieldcode);

      out.writeShort(fieldDef.getType());

      if (fdRec.scalarValue != null) {
        out.writeBoolean(true); // scalar redefinition
        fdRec.scalarValue.emit(out);
        continue;
      }

      out.writeBoolean(false); // vector mod

      // write out what is being added to this vector

      if (debug) {
        System.err.println("====== Emitting " + fdRec.addValues.size() + " addition elements");
      }

      if (fdRec.addValues == null) {
        out.writeInt(0);
      } else {
        out.writeInt(fdRec.addValues.size());

        for (int j = 0; j < fdRec.addValues.size(); j++) {
          value = fdRec.addValues.elementAt(j);

          // we only support 3 vector field types

          if (value instanceof String) {
            out.writeUTF((String) value);
          } else if (value instanceof Invid) {
            ((Invid) value).emit(out);
          } else if (value instanceof IPwrap) {
            Byte[] bytes = ((IPwrap) value).address;

            out.writeByte(bytes.length);

            for (int k = 0; k < bytes.length; k++) {
              out.writeByte(bytes[k].byteValue());
            }
          } else {
            Ganymede.debug("DBObjectDeltaRec.emit(): Error!  Unrecognized element in vector!");
          }
        }
      }

      // write out what is being removed from this vector

      if (fdRec.delValues == null) {
        out.writeInt(0);
      } else {
        out.writeInt(fdRec.delValues.size());

        for (int j = 0; j < fdRec.delValues.size(); j++) {
          value = fdRec.delValues.elementAt(j);

          // we only support 3 vector field types

          if (value instanceof String) {
            out.writeUTF((String) value);
          } else if (value instanceof Invid) {
            Invid invid = (Invid) value;

            out.writeShort(invid.getType());
            out.writeInt(invid.getNum());
          } else if (value instanceof IPwrap) {
            Byte[] bytes = ((IPwrap) value).address;

            out.writeByte(bytes.length);

            for (int k = 0; k < bytes.length; k++) {
              out.writeByte(bytes[k].byteValue());
            }
          }
        }
      }
    }
  }
  /** This DBObjectDeltaRec constructor is used to load a delta record from a Journal stream. */
  public DBObjectDeltaRec(DataInput in) throws IOException {
    short fieldcode;
    short typecode;
    boolean scalar;
    DBObjectBase baseDef;
    DBObjectBaseField fieldDef;
    String fieldName = null;
    String status = null;
    DBObject obj = null;
    /* -- */

    status = "Reading invid";

    boolean debug = true;

    try {
      invid = Invid.createInvid(in);
      baseDef = Ganymede.db.getObjectBase(invid.getType());
      obj = Ganymede.db.viewDBObject(invid);

      if (debug) {
        System.err.println(
            "\n>*> Reading delta rec for " + baseDef.getName() + " <" + invid.getNum() + ">");
      }

      status = "Reading field count";
      int fieldcount = in.readInt();

      if (debug) {
        System.err.println(
            ">> DBObjectDeltaRec(): " + fieldcount + " fields in on-disk delta rec.");
      }

      for (int i = 0; i < fieldcount; i++) {
        status = "\nReading field code for field " + i;
        if (debug) {
          System.err.println(status);
        }
        fieldcode = in.readShort();
        fieldDef = (DBObjectBaseField) baseDef.getField(fieldcode);
        typecode = fieldDef.getType();
        fieldName = fieldDef.getName();
        status = "Reading deletion boolean for field " + i;
        if (in.readBoolean()) {
          // we're deleting this field
          if (debug) {
            System.err.println(
                "Reading field deletion record field ("
                    + fieldName
                    + ":"
                    + fieldcode
                    + ") for field "
                    + i);
          }

          fieldRecs.addElement(new fieldDeltaRec(fieldcode, null));
          continue;
        }

        // okay, we've got a field redefinition.. check the type
        // code to make sure we don't have an incompatible journal
        // entry
        status = "Reading type code for field " + i;
        if (in.readShort() != typecode) {
          throw new RuntimeException("Error, field type mismatch in journal file");
        }

        // ok.. now, is it a total redefinition, or a vector delta record?
        status = "Reading scalar/vector delta boolean for field " + i;
        scalar = in.readBoolean();

        if (scalar) {
          fieldDeltaRec f_r = null;
          status = "Reading field (" + fieldName + ":" + fieldcode + ") for field " + i;
          if (debug) {
            System.err.println(status);
          }

          f_r = new fieldDeltaRec(fieldcode, DBField.readField(obj, in, fieldDef));
          fieldRecs.addElement(f_r);
          if (debug) {
            System.err.println("Value: " + f_r.toString());
          }
        } else {
          // ok, we have a vector delta chunk
          fieldDeltaRec fieldRec = new fieldDeltaRec(fieldcode);
          Object value = null;

          // read in the additions
          status = "Reading vector addition count for field " + fieldName + "(" + i + ")";
          if (debug) {
            System.err.println(status);
          }

          int size = in.readInt();

          if (debug) {
            System.err.println(">> DBObjectDeltaRec(): reading " + size + " additions.");
          }

          for (int j = 0; j < size; j++) {
            // we only support 3 vector field types
            switch (typecode) {
              case STRING:
                status = "Reading string addition " + j + " for field " + i + ":" + fieldName;
                if (debug) {
                  System.err.println(status);
                }
                value = in.readUTF();
                break;

              case INVID:
                status = "Reading invid addition " + j + " for field " + i + ":" + fieldName;
                if (debug) {
                  System.err.println(status);
                }
                value = Invid.createInvid(in);
                break;

              case IP:
                status = "Reading ip addition " + j + " for field " + i + ":" + fieldName;
                if (debug) {
                  System.err.println(status);
                }
                byte bytelength = in.readByte();
                Byte[] bytes = new Byte[bytelength];
                for (int k = 0; k < bytelength; k++) {
                  bytes[k] = Byte.valueOf(in.readByte());
                }
                value = bytes;
            }

            fieldRec.addValue(value);
          }

          // read in the deletions
          status = "Reading vector deletion count for field " + i;
          if (debug) {
            System.err.println(status);
          }

          size = in.readInt();
          if (debug) {
            System.err.println(">> DBObjectDeltaRec(): reading " + size + " deletions.");
          }

          for (int j = 0; j < size; j++) {
            // we only support 3 vector field types

            switch (typecode) {
              case STRING:
                status = "Reading string deletion " + j + " for field " + i + ":" + fieldName;
                value = in.readUTF();
                break;

              case INVID:
                status = "Reading invid deletion " + j + " for field " + i + ":" + fieldName;
                value = Invid.createInvid(in);
                break;

              case IP:
                status = "Reading IP deletion " + j + " for field " + i + ":" + fieldName;
                byte bytelength = in.readByte();
                Byte[] bytes = new Byte[bytelength];
                for (int k = 0; k < bytelength; k++) {
                  bytes[k] = Byte.valueOf(in.readByte());
                }
                value = bytes;
            }
            fieldRec.delValue(value);
          }
          // and save this field
          fieldRecs.addElement(fieldRec);
        }
      }
    } catch (IOException ex) {
      System.err.println("DBObjectDeltaRec constructor: IOException in state " + status);
      Ganymede.logError(ex);
      throw ex;
    }
  }