예제 #1
0
  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();
    }
  }
예제 #2
0
  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;
  }
예제 #3
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();
    }
  }
예제 #4
0
 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();
   }
 }