Esempio n. 1
0
 @Override
 public void save(final DataOutput out) {
   try {
     out.writeByte(CLASS_TYPE);
     DataInputOutputUtil.writeINT(out, className);
     DataInputOutputUtil.writeINT(out, typeArgs.length);
     for (AbstractType t : typeArgs) {
       t.save(out);
     }
   } catch (IOException e) {
     throw new BuildDataCorruptedException(e);
   }
 }
  public static int findRootRecord(String rootUrl) throws IOException {
    try {
      w.lock();
      DbConnection.markDirty();
      final int root = getNames().enumerate(rootUrl);

      final DataInputStream input = readAttribute(1, CHILDREN_ATT);
      int[] names = ArrayUtil.EMPTY_INT_ARRAY;
      int[] ids = ArrayUtil.EMPTY_INT_ARRAY;

      if (input != null) {
        try {
          final int count = DataInputOutputUtil.readINT(input);
          names = ArrayUtil.newIntArray(count);
          ids = ArrayUtil.newIntArray(count);
          for (int i = 0; i < count; i++) {
            final int name = DataInputOutputUtil.readINT(input);
            final int id = DataInputOutputUtil.readINT(input);
            if (name == root) {
              return id;
            }

            names[i] = name;
            ids[i] = id;
          }
        } finally {
          input.close();
        }
      }

      final DataOutputStream output = writeAttribute(1, CHILDREN_ATT, false);
      int id;
      try {
        id = createRecord();
        DataInputOutputUtil.writeINT(output, names.length + 1);
        for (int i = 0; i < names.length; i++) {
          DataInputOutputUtil.writeINT(output, names[i]);
          DataInputOutputUtil.writeINT(output, ids[i]);
        }
        DataInputOutputUtil.writeINT(output, root);
        DataInputOutputUtil.writeINT(output, id);
      } finally {
        output.close();
      }

      return id;
    } finally {
      w.unlock();
    }
  }
 @Override
 public void save(@NotNull DataOutput out, TIntArrayList list) throws IOException {
   if (list.size() == 2) {
     DataInputOutputUtil.writeINT(out, list.getQuick(0));
     DataInputOutputUtil.writeINT(out, list.getQuick(1));
   } else {
     DataInputOutputUtil.writeINT(out, -list.size());
     int prev = 0;
     for (int i = 0, len = list.size(); i < len; i += 2) {
       int value = list.getQuick(i);
       DataInputOutputUtil.writeINT(out, value - prev);
       prev = value;
       DataInputOutputUtil.writeINT(out, list.getQuick(i + 1));
     }
   }
 }
  public void save(final DataOutput out) {
    super.save(out);
    myType.save(out);

    try {
      if (myValue instanceof String) {
        out.writeByte(STRING);
        String value = (String) myValue;
        RW.writeUTF(out, value);
      } else if (myValue instanceof Integer) {
        out.writeByte(INTEGER);
        DataInputOutputUtil.writeINT(out, ((Integer) myValue).intValue());
      } else if (myValue instanceof Long) {
        out.writeByte(LONG);
        out.writeLong(((Long) myValue).longValue());
      } else if (myValue instanceof Float) {
        out.writeByte(FLOAT);
        out.writeFloat(((Float) myValue).floatValue());
      } else if (myValue instanceof Double) {
        out.writeByte(DOUBLE);
        out.writeDouble(((Double) myValue).doubleValue());
      } else if (myValue instanceof Type) {
        out.writeByte(TYPE);
        RW.writeUTF(out, ((Type) myValue).getDescriptor());
      } else {
        out.writeByte(NONE);
      }
    } catch (IOException e) {
      throw new BuildDataCorruptedException(e);
    }
  }
Esempio n. 5
0
 @Override
 public void save(final DataOutput out) {
   try {
     out.writeByte(PRIMITIVE_TYPE);
     DataInputOutputUtil.writeINT(out, type);
   } catch (IOException e) {
     throw new BuildDataCorruptedException(e);
   }
 }
  private static int findAttributePage(int fileId, String attrId, boolean toWrite)
      throws IOException {
    checkFileIsValid(fileId);

    Storage storage = getAttributesStorage();

    int encodedAttrId = DbConnection.getAttributeId(attrId);
    int recordId = getAttributeRecordId(fileId);

    if (recordId == 0) {
      if (!toWrite) return 0;

      recordId = storage.createNewRecord();
      setAttributeRecordId(fileId, recordId);
    } else {
      DataInputStream attrRefs = storage.readStream(recordId);
      try {
        while (attrRefs.available() > 0) {
          final int attIdOnPage = DataInputOutputUtil.readINT(attrRefs);
          final int attrAddress = DataInputOutputUtil.readINT(attrRefs);

          if (attIdOnPage == encodedAttrId) return attrAddress;
        }
      } finally {
        attrRefs.close();
      }
    }

    if (toWrite) {
      Storage.AppenderStream appender = storage.appendStream(recordId);
      DataInputOutputUtil.writeINT(appender, encodedAttrId);
      int attrAddress = storage.createNewRecord();
      DataInputOutputUtil.writeINT(appender, attrAddress);
      DbConnection.REASONABLY_SMALL.myAttrPageRequested = true;
      try {
        appender.close();
      } finally {
        DbConnection.REASONABLY_SMALL.myAttrPageRequested = false;
      }
      return attrAddress;
    }

    return 0;
  }
  public static void deleteRootRecord(int id) throws IOException {
    try {
      w.lock();
      DbConnection.markDirty();
      final DataInputStream input = readAttribute(1, CHILDREN_ATT);
      assert input != null;
      int count;
      int[] names;
      int[] ids;
      try {
        count = DataInputOutputUtil.readINT(input);

        names = ArrayUtil.newIntArray(count);
        ids = ArrayUtil.newIntArray(count);
        for (int i = 0; i < count; i++) {
          names[i] = DataInputOutputUtil.readINT(input);
          ids[i] = DataInputOutputUtil.readINT(input);
        }
      } finally {
        input.close();
      }

      final int index = ArrayUtil.find(ids, id);
      assert index >= 0;

      names = ArrayUtil.remove(names, index);
      ids = ArrayUtil.remove(ids, index);

      final DataOutputStream output = writeAttribute(1, CHILDREN_ATT, false);
      try {
        DataInputOutputUtil.writeINT(output, count - 1);
        for (int i = 0; i < names.length; i++) {
          DataInputOutputUtil.writeINT(output, names[i]);
          DataInputOutputUtil.writeINT(output, ids[i]);
        }
      } finally {
        output.close();
      }
    } finally {
      w.unlock();
    }
  }
 public static void updateList(int id, @NotNull int[] children) {
   try {
     w.lock();
     DbConnection.markDirty();
     final DataOutputStream record = writeAttribute(id, CHILDREN_ATT, false);
     DataInputOutputUtil.writeINT(record, children.length);
     for (int child : children) {
       if (child == id) {
         LOG.error("Cyclic parent child relations");
       } else {
         child = child > id ? child - id : -child;
         DataInputOutputUtil.writeINT(record, child);
       }
     }
     record.close();
   } catch (Throwable e) {
     throw DbConnection.handleError(e);
   } finally {
     w.unlock();
   }
 }
Esempio n. 9
0
 private static void saveVersion(File versionFile) {
   try {
     DataOutputStream versionOutputStream =
         new DataOutputStream(new BufferedOutputStream(new FileOutputStream(versionFile)));
     try {
       DataInputOutputUtil.writeINT(versionOutputStream, VERSION);
     } finally {
       versionOutputStream.close();
     }
   } catch (IOException ignore) {
   }
 }
 @Override
 public void save(final DataOutput out, final TIntArrayList value) throws IOException {
   int size = value.size();
   if (size == 0) {
     DataInputOutputUtil.writeSINT(out, Integer.MAX_VALUE);
   } else if (size == 1) {
     DataInputOutputUtil.writeSINT(out, -value.get(0));
   } else {
     DataInputOutputUtil.writeSINT(out, size);
     for (int i = 0; i < size; i++) {
       DataInputOutputUtil.writeINT(out, value.get(i));
     }
   }
 }
Esempio n. 11
0
 public void writeToStream(final DataOutputStream stream) throws IOException {
   if (myIndexStamps != null) {
     final int size = myIndexStamps.size();
     final int[] count = new int[] {0};
     DataInputOutputUtil.writeINT(stream, size);
     myIndexStamps.forEachEntry(
         new TObjectLongProcedure<ID<?, ?>>() {
           public boolean execute(final ID<?, ?> id, final long timestamp) {
             try {
               DataInputOutputUtil.writeINT(stream, (int) id.getUniqueId());
               DataInputOutputUtil.writeTIME(stream, timestamp);
               count[0]++;
               return true;
             } catch (IOException e) {
               throw new RuntimeException(e);
             }
           }
         });
     assert count[0] == size;
   }
 }
Esempio n. 12
0
  @Override
  public void saveTo(DataOutput out, DataExternalizer<Value> externalizer) throws IOException {
    DataInputOutputUtil.writeINT(out, size());

    for (final Iterator<Value> valueIterator = getValueIterator(); valueIterator.hasNext(); ) {
      final Value value = valueIterator.next();
      externalizer.save(out, value);
      Object input = getInput(value);

      if (input instanceof Integer) {
        DataInputOutputUtil.writeINT(
            out, (Integer) input); // most common 90% case during index building
      } else {
        // serialize positive file ids with delta encoding
        ChangeBufferingList originalInput = (ChangeBufferingList) input;
        IntIterator intIterator = originalInput.intIterator();
        DataInputOutputUtil.writeINT(out, -intIterator.size());

        if (intIterator.hasAscendingOrder()) {
          IdSet checkSet = originalInput.getCheckSet();
          if (checkSet != null && checkSet.size() != intIterator.size()) { // debug code
            int a = 1;
            assert false;
          }
          int prev = 0;

          while (intIterator.hasNext()) {
            int fileId = intIterator.next();
            if (checkSet != null && !checkSet.contains(fileId)) { // debug code
              int a = 1;
              assert false;
            }
            DataInputOutputUtil.writeINT(out, fileId - prev);
            prev = fileId;
          }
        } else {
          // sorting numbers via bitset before writing deltas
          int max = 0, min = Integer.MAX_VALUE;

          while (intIterator.hasNext()) {
            int nextInt = intIterator.next();
            max = Math.max(max, nextInt);
            min = Math.min(min, nextInt);
          }

          assert min > 0;

          final int offset = (min >> INT_BITS_SHIFT) << INT_BITS_SHIFT;
          final int bitsLength = ((max - offset) >> INT_BITS_SHIFT) + 1;
          final int[] bits = ourSpareBuffer.getBuffer(bitsLength);
          for (int i = 0; i < bitsLength; ++i) bits[i] = 0;

          intIterator = originalInput.intIterator();
          while (intIterator.hasNext()) {
            final int id = intIterator.next() - offset;
            bits[id >> INT_BITS_SHIFT] |= (1 << (id));
          }

          int pos = nextSetBit(0, bits, bitsLength);
          int prev = 0;

          while (pos != -1) {
            DataInputOutputUtil.writeINT(out, pos + offset - prev);
            prev = pos + offset;
            pos = nextSetBit(pos + 1, bits, bitsLength);
          }
        }
      }
    }
  }