Exemplo n.º 1
0
  @Override
  public boolean convertRecords2Links() {
    final Map<OIdentifiable, Change> newChangedValues = new HashMap<OIdentifiable, Change>();
    for (Map.Entry<OIdentifiable, Change> entry : changes.entrySet()) {
      OIdentifiable identifiable = entry.getKey();
      if (identifiable instanceof ORecord) {
        ORID identity = identifiable.getIdentity();
        ORecord record = (ORecord) identifiable;
        identity = record.getIdentity();

        newChangedValues.put(identity, entry.getValue());
      } else newChangedValues.put(entry.getKey().getIdentity(), entry.getValue());
    }

    for (Map.Entry<OIdentifiable, Change> entry : newChangedValues.entrySet()) {
      if (entry.getKey() instanceof ORecord) {
        ORecord record = (ORecord) entry.getKey();

        newChangedValues.put(record, entry.getValue());
      } else return false;
    }

    newEntries.clear();

    changes.clear();
    changes.putAll(newChangedValues);

    return true;
  }
Exemplo n.º 2
0
  public void formatMultiValue(
      final Iterator<?> iIterator, final StringWriter buffer, final String format)
      throws IOException {
    if (iIterator != null) {
      int counter = 0;
      String objectJson;

      while (iIterator.hasNext()) {
        final Object entry = iIterator.next();
        if (entry != null) {
          if (counter++ > 0) buffer.append(", ");

          if (entry instanceof OIdentifiable) {
            ORecord<?> rec = ((OIdentifiable) entry).getRecord();
            try {
              objectJson = rec.getRecord().toJSON(format);

              buffer.append(objectJson);
            } catch (Exception e) {
              OLogManager.instance()
                  .error(this, "Error transforming record " + rec.getIdentity() + " to JSON", e);
            }
          } else if (OMultiValue.isMultiValue(entry))
            formatMultiValue(OMultiValue.getMultiValueIterator(entry), buffer, format);
          else buffer.append(OJSONWriter.writeValue(entry, format));
        }
      }
    }
  }
Exemplo n.º 3
0
  private List<OClusterPosition> getValidPositions(int clusterId) {
    final List<OClusterPosition> positions = new ArrayList<OClusterPosition>();

    final ORecordIteratorCluster<?> iteratorCluster =
        database.browseCluster(database.getClusterNameById(clusterId));

    for (int i = 0; i < 100; i++) {
      if (!iteratorCluster.hasNext()) break;
      ORecord<?> doc = iteratorCluster.next();
      positions.add(doc.getIdentity().getClusterPosition());
    }
    return positions;
  }
Exemplo n.º 4
0
  public void writeRecord(final ORecord<?> iRecord, final String iFetchPlan, String iFormat)
      throws IOException {
    if (iFormat == null) iFormat = JSON_FORMAT;

    final String format = iFetchPlan != null ? iFormat + ",fetchPlan:" + iFetchPlan : iFormat;
    if (iRecord != null)
      send(OHttpUtils.STATUS_OK_CODE, "OK", OHttpUtils.CONTENT_JSON, iRecord.toJSON(format), null);
  }
Exemplo n.º 5
0
 public OIdentifiable update(final OIdentifiable iValue) {
   final Iterator<OIdentifiable> underlying = getCurrentIterator();
   if (underlying instanceof OLazyIterator) {
     final OIdentifiable old = ((OLazyIterator<OIdentifiable>) underlying).update(iValue);
     if (sourceRecord != null && !old.equals(iValue)) sourceRecord.setDirty();
     return old;
   } else
     throw new UnsupportedOperationException(
         "Underlying iterator not supports lazy updates (Interface OLazyIterator");
 }
Exemplo n.º 6
0
  @Override
  public boolean convertRecords2Links() {
    for (int i = 0; i < entriesLength; i++) {
      final Object entry = entries[i];

      if (entry instanceof OIdentifiable) {
        final OIdentifiable identifiable = (OIdentifiable) entry;
        if (identifiable instanceof ORecord) {
          final ORecord record = (ORecord) identifiable;
          if (record.isDirty() || record.getIdentity().isNew()) {
            record.save();
          }

          entries[i] = record.getIdentity();
        }
      }
    }

    return true;
  }
Exemplo n.º 7
0
  @Override
  public int serialize(byte[] stream, int offset, UUID ownerUuid) {
    for (int i = 0; i < entriesLength; i++) {
      final Object entry = entries[i];

      if (entry instanceof OIdentifiable) {
        final OIdentifiable identifiable = (OIdentifiable) entry;
        if (identifiable instanceof ORecord) {
          final ORecord record = (ORecord) identifiable;
          if (record.isDirty() || record.getIdentity().isNew()) {
            record.save();
          }
        }
      }
    }

    if (!deserialized) {
      System.arraycopy(serializedContent, 0, stream, offset, serializedContent.length);

      if (contentWasChanged) {
        OIntegerSerializer.INSTANCE.serialize(size, stream, offset);
        offset += serializedContent.length;
      } else {
        offset += serializedContent.length;
        return offset;
      }

    } else {
      OIntegerSerializer.INSTANCE.serialize(size, stream, offset);
      offset += OIntegerSerializer.INT_SIZE;
    }

    for (Object entry : entries) {
      if (entry instanceof OIdentifiable) {
        OLinkSerializer.INSTANCE.serialize((OIdentifiable) entry, stream, offset);
        offset += OLinkSerializer.RID_SIZE;
      }
    }

    return offset;
  }
Exemplo n.º 8
0
  @SuppressWarnings("unchecked")
  private Map<Object, Object> convertToRIDsIfPossible(final Map<Object, Object> params) {
    final Map<Object, Object> newParams = new HashMap<Object, Object>(params.size());

    for (Entry<Object, Object> entry : params.entrySet()) {
      final Object value = entry.getValue();

      if (value instanceof Set<?> && ((Set<?>) value).iterator().next() instanceof ORecord) {
        // CONVERT RECORDS AS RIDS
        final Set<ORID> newSet = new HashSet<ORID>();
        for (ORecord rec : (Set<ORecord>) value) {
          newSet.add(rec.getIdentity());
        }
        newParams.put(entry.getKey(), newSet);

      } else if (value instanceof List<?> && ((List<?>) value).get(0) instanceof ORecord) {
        // CONVERT RECORDS AS RIDS
        final List<ORID> newList = new ArrayList<ORID>();
        for (ORecord rec : (List<ORecord>) value) {
          newList.add(rec.getIdentity());
        }
        newParams.put(entry.getKey(), newList);

      } else if (value instanceof Map<?, ?>
          && ((Map<?, ?>) value).values().iterator().next() instanceof ORecord) {
        // CONVERT RECORDS AS RIDS
        final Map<Object, ORID> newMap = new HashMap<Object, ORID>();
        for (Entry<?, ORecord> mapEntry : ((Map<?, ORecord>) value).entrySet()) {
          newMap.put(mapEntry.getKey(), mapEntry.getValue().getIdentity());
        }
        newParams.put(entry.getKey(), newMap);
      } else if (entry.getValue() instanceof ORecord) {
        newParams.put(entry.getKey(), ((OIdentifiable) entry.getValue()).getIdentity());
      } else newParams.put(entry.getKey(), entry.getValue());
    }

    return newParams;
  }
Exemplo n.º 9
0
  public OJSONWriter writeRecord(
      final int iIdentLevel, final boolean iNewLine, final Object iName, final ORecord<?> iRecord)
      throws IOException {
    if (!firstAttribute) out.append(",");

    format(iIdentLevel, iNewLine);

    if (iName != null) out.append("\"" + iName.toString() + "\":");

    out.append(iRecord.toJSON(format));

    firstAttribute = false;
    return this;
  }
Exemplo n.º 10
0
 public void setDirty() {
   if (sourceRecord != null) sourceRecord.setDirty();
 }
Exemplo n.º 11
0
  public static String writeValue(Object iValue, final String iFormat) throws IOException {
    final StringBuilder buffer = new StringBuilder();

    final boolean oldAutoConvertSettings;

    if (iValue instanceof ORecordLazyMultiValue) {
      oldAutoConvertSettings = ((ORecordLazyMultiValue) iValue).isAutoConvertToRecord();
      ((ORecordLazyMultiValue) iValue).setAutoConvertToRecord(false);
    } else oldAutoConvertSettings = false;

    if (iValue == null) buffer.append("null");
    else if (iValue instanceof Boolean || iValue instanceof Number)
      buffer.append(iValue.toString());
    else if (iValue instanceof OIdentifiable) {
      final OIdentifiable linked = (OIdentifiable) iValue;
      if (linked.getIdentity().isValid()) {
        buffer.append('\"');
        linked.getIdentity().toString(buffer);
        buffer.append('\"');
      } else {
        if (iFormat != null && iFormat.contains("shallow")) buffer.append("{}");
        else {
          final ORecord<?> rec = linked.getRecord();
          if (rec != null) buffer.append(rec.toJSON(iFormat));
          else buffer.append("null");
        }
      }

    } else if (iValue.getClass().isArray()) {

      if (iValue instanceof byte[]) {
        buffer.append('\"');
        final byte[] source = (byte[]) iValue;

        if (iFormat != null && iFormat.contains("shallow")) buffer.append(source.length);
        else buffer.append(OBase64Utils.encodeBytes(source));

        buffer.append('\"');
      } else {
        buffer.append('[');
        int size = Array.getLength(iValue);
        if (iFormat != null && iFormat.contains("shallow")) buffer.append(size);
        else
          for (int i = 0; i < size; ++i) {
            if (i > 0) buffer.append(",");
            buffer.append(writeValue(Array.get(iValue, i), iFormat));
          }
        buffer.append(']');
      }
    } else if (iValue instanceof Iterator<?>) iteratorToJSON((Iterator<?>) iValue, iFormat, buffer);
    else if (iValue instanceof Iterable<?>)
      iteratorToJSON(((Iterable<?>) iValue).iterator(), iFormat, buffer);
    else if (iValue instanceof Map<?, ?>) mapToJSON((Map<Object, Object>) iValue, iFormat, buffer);
    else if (iValue instanceof Map.Entry<?, ?>) {
      final Map.Entry<?, ?> entry = (Entry<?, ?>) iValue;
      buffer.append('{');
      buffer.append(writeValue(entry.getKey(), iFormat));
      buffer.append(":");
      buffer.append(writeValue(entry.getValue(), iFormat));
      buffer.append('}');
    } else if (iValue instanceof Date) {
      if (iFormat.indexOf("dateAsLong") > -1) buffer.append(((Date) iValue).getTime());
      else {
        buffer.append('"');
        buffer.append(ODateHelper.getDateTimeFormatInstance().format(iValue));
        buffer.append('"');
      }
    } else if (iValue instanceof BigDecimal) buffer.append(((BigDecimal) iValue).toPlainString());
    else if (iValue instanceof ORecordLazyMultiValue)
      iteratorToJSON(((ORecordLazyMultiValue) iValue).rawIterator(), iFormat, buffer);
    else if (iValue instanceof Iterable<?>)
      iteratorToJSON(((Iterable<?>) iValue).iterator(), iFormat, buffer);
    else {
      // TREAT IT AS STRING
      final String v = iValue.toString();
      buffer.append('"');
      buffer.append(encode(v));
      buffer.append('"');
    }

    if (iValue instanceof ORecordLazyMultiValue)
      ((ORecordLazyMultiValue) iValue).setAutoConvertToRecord(oldAutoConvertSettings);

    return buffer.toString();
  }
Exemplo n.º 12
0
 public void remove() {
   final Iterator<OIdentifiable> underlying = getCurrentIterator();
   underlying.remove();
   if (sourceRecord != null) sourceRecord.setDirty();
 }