Exemplo n.º 1
0
  public CacheSchematicDb(Cache<String, SchematicEntry> store) {
    this.name = store.getName();
    this.store = store;
    String defaultContentTypeForDocs = Schematic.ContentTypes.JSON;
    String defaultContentTypeForBinary = Schematic.ContentTypes.BINARY;
    String defaultSchemaUri = JsonSchema.Version.Latest.CORE_METASCHEMA_URL;
    String description = "";
    String schemaCacheName = store.getName() + "Schemas";

    // Load the database document from the cache ...
    SchematicEntry databaseDocument = store.get("");
    if (databaseDocument != null && databaseDocument.hasDocumentContent()) {
      Document dbDoc = databaseDocument.getContentAsDocument();
      defaultContentTypeForDocs =
          dbDoc.getString("defaultContentTypeForDocuments", defaultContentTypeForDocs);
      defaultContentTypeForBinary =
          dbDoc.getString("defaultContentTypeForBinary", defaultContentTypeForBinary);
      defaultSchemaUri = dbDoc.getString("defaultSchemaUri", defaultSchemaUri);
      description = dbDoc.getString("description", description);
      schemaCacheName = dbDoc.getString("schemaCacheName", schemaCacheName);
    }

    this.defaultContentTypeForBinary = defaultContentTypeForBinary;
    this.defaultContentTypeForDocument = defaultContentTypeForDocs;
    this.defaultSchemaUri = defaultSchemaUri;
    this.description = description;
    this.schemaCacheName = schemaCacheName;
    this.context = new CacheContext(store.getAdvancedCache());
  }
  @Test
  public void shouldStoreDocumentWithUnusedKeyAndWithNonNullMetadata() {
    Document doc = Schematic.newDocument("k1", "value1", "k2", 2);
    Document metadata = Schematic.newDocument("mimeType", "text/plain");
    String key = "can be anything";
    SchematicEntry prior = db.put(key, doc, metadata);
    assert prior == null : "Should not have found a prior entry";

    // Read back from the database ...
    SchematicEntry entry = db.get(key);
    assert entry != null : "Should have found the entry";

    // Verify the content ...
    Document read = entry.getContentAsDocument();
    assert read != null;
    assert "value1".equals(read.getString("k1"));
    assert 2 == read.getInteger("k2");
    assert entry.getContentAsBinary() == null
        : "Should not have a Binary value for the entry's content";
    assert read.containsAll(doc);
    assert read.equals(doc);

    // Verify the metadata ...
    Document readMetadata = entry.getMetadata();
    assert readMetadata != null;
    assert readMetadata.getString("mimeType").equals(metadata.getString("mimeType"));
    assert readMetadata.containsAll(metadata);
    // metadata contains more than what we specified ...
    assert !readMetadata.equals(metadata) : "Expected:\n" + metadata + "\nFound: \n" + readMetadata;
  }
  @Test
  public void shouldStoreDocumentAndFetchAndModifyAndRefetch() throws Exception {
    // Store the document ...
    Document doc = Schematic.newDocument("k1", "value1", "k2", 2);
    Document metadata = Schematic.newDocument("mimeType", "text/plain");
    String key = "can be anything";
    SchematicEntry prior = db.put(key, doc, metadata);
    assert prior == null : "Should not have found a prior entry";

    // Read back from the database ...
    SchematicEntry entry = db.get(key);
    assert entry != null : "Should have found the entry";

    // Verify the content ...
    Document read = entry.getContentAsDocument();
    assert read != null;
    assert "value1".equals(read.getString("k1"));
    assert 2 == read.getInteger("k2");
    assert entry.getContentAsBinary() == null
        : "Should not have a Binary value for the entry's content";
    assert read.containsAll(doc);
    assert read.equals(doc);

    // Modify using an editor ...
    try {
      // tm.begin();
      EditableDocument editable = entry.editDocumentContent();
      editable.setBoolean("k3", true);
      editable.setNumber("k4", 3.5d);
    } finally {
      // tm.commit();
    }

    // Now re-read ...
    SchematicEntry entry2 = db.get(key);
    Document read2 = entry2.getContentAsDocument();
    assert read2 != null;
    assert "value1".equals(read2.getString("k1"));
    assert 2 == read2.getInteger("k2");
    assert true == read2.getBoolean("k3");
    assert 3.4d < read2.getDouble("k4");
  }
  @Test
  public void shouldStoreDocumentWithUnusedKeyAndWithNullMetadata() {
    Document doc = Schematic.newDocument("k1", "value1", "k2", 2);
    String key = "can be anything";
    SchematicEntry prior = db.put(key, doc, null);
    assert prior == null : "Should not have found a prior entry";
    SchematicEntry entry = db.get(key);
    assert entry != null : "Should have found the entry";

    // Verify the content ...
    Document read = entry.getContentAsDocument();
    assert read != null;
    assert "value1".equals(read.getString("k1"));
    assert 2 == read.getInteger("k2");
    assert entry.getContentAsBinary() == null
        : "Should not have a Binary value for the entry's content";
    assert read.containsAll(doc);
    assert read.equals(doc);

    // Verify the metadata ...
    Document readMetadata = entry.getMetadata();
    assert readMetadata != null;
    assert readMetadata.getString("id").equals(key);
  }
Exemplo n.º 5
0
 @Override
 public SchematicEntry put(Document entryDocument) {
   Document metadata = entryDocument.getDocument(FieldName.METADATA);
   Object content = entryDocument.getDocument(FieldName.CONTENT);
   if (metadata == null || content == null) {
     throw new IllegalArgumentException("The supplied document is not of the required format");
   }
   String key = metadata.getString(FieldName.ID);
   if (key == null) {
     throw new IllegalArgumentException("The supplied document is not of the required format");
   }
   SchematicEntry newEntry = null;
   if (content instanceof Document) {
     newEntry =
         new SchematicEntryLiteral(
             key, (Document) content, metadata, defaultContentTypeForDocument);
   } else {
     newEntry =
         new SchematicEntryLiteral(key, (Binary) content, metadata, defaultContentTypeForBinary);
   }
   SchematicEntry oldValue = store.put(key, newEntry);
   return oldValue != null ? removedResult(key, oldValue) : null;
 }