Search in sources :

Example 41 with BasicBSONObject

use of org.bson.BasicBSONObject in project jackrabbit-oak by apache.

the class ReplicaSetInfo method updateRevisions.

private void updateRevisions(Iterable<BasicBSONObject> members) {
    Set<String> secondaries = new HashSet<String>();
    boolean unknownState = false;
    String primary = null;
    for (BasicBSONObject member : members) {
        MemberState state;
        try {
            state = MemberState.valueOf(member.getString("stateStr"));
        } catch (IllegalArgumentException e) {
            state = MemberState.UNKNOWN;
        }
        String name = member.getString("name");
        if (hiddenMembers.contains(name)) {
            continue;
        }
        switch(state) {
            case PRIMARY:
                primary = name;
                continue;
            case SECONDARY:
                secondaries.add(name);
                break;
            case ARBITER:
                continue;
            default:
                LOG.debug("Invalid state {} for instance {}", state, name);
                unknownState = true;
                break;
        }
    }
    Set<String> hostsToCheck = new HashSet<String>();
    if (secondaries.isEmpty()) {
        LOG.debug("No secondaries found: {}", members);
        unknownState = true;
    } else {
        hostsToCheck.addAll(secondaries);
    }
    if (primary == null) {
        LOG.debug("No primary found: {}", members);
        unknownState = true;
    } else {
        hostsToCheck.add(primary);
    }
    Map<String, Timestamped<RevisionVector>> vectors = null;
    if (!unknownState) {
        vectors = getRootRevisions(hostsToCheck);
        if (vectors.containsValue(null)) {
            unknownState = true;
        }
    }
    if (unknownState) {
        rootRevisions = null;
        secondariesSafeTimestamp = 0;
    } else {
        Timestamped<RevisionVector> primaryRevision = vectors.get(primary);
        Iterable<Timestamped<RevisionVector>> secondaryRevisions = filterKeys(vectors, in(secondaries)).values();
        rootRevisions = pmin(transform(secondaryRevisions, Timestamped.<RevisionVector>getExtractFunction()));
        if (rootRevisions == null || primaryRevision == null || isEmpty(secondaryRevisions)) {
            secondariesSafeTimestamp = 0;
        } else {
            secondariesSafeTimestamp = getSecondariesSafeTimestamp(primaryRevision, secondaryRevisions);
        }
    }
    LOG.debug("Minimum root revisions: {}. Current lag: {}", rootRevisions, getLag());
    nodeCollections.retain(hostsToCheck);
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) RevisionVector(org.apache.jackrabbit.oak.plugins.document.RevisionVector) HashSet(java.util.HashSet)

Example 42 with BasicBSONObject

use of org.bson.BasicBSONObject in project jackrabbit-oak by apache.

the class ReplicaSetInfo method getHiddenMembers.

List<String> getHiddenMembers() {
    BasicDBObject result;
    try {
        result = getReplicaConfig();
    } catch (MongoException e) {
        LOG.error("Can't get replica configuration", e);
        return null;
    }
    @SuppressWarnings("unchecked") Iterable<BasicBSONObject> members = (Iterable<BasicBSONObject>) result.get("members");
    if (members == null) {
        members = Collections.emptyList();
    }
    List<String> hiddenMembers = new ArrayList<String>();
    for (BasicBSONObject member : members) {
        if (member.getBoolean("hidden")) {
            hiddenMembers.add(member.getString("host"));
        }
    }
    return hiddenMembers;
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) BasicBSONObject(org.bson.BasicBSONObject) MongoException(com.mongodb.MongoException) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 43 with BasicBSONObject

use of org.bson.BasicBSONObject in project v7files by thiloplanz.

the class BucketsServletTest method testFormPostGET.

public void testFormPostGET() throws IOException, SAXException {
    BasicBSONObject bucket = prepareBucket("1", "FormPost", null, null);
    MongoContentStorage storage = new MongoContentStorage(getMongo().getDB("test"));
    ContentSHA sha = storage.storeContent(new ByteArrayInputStream("test".getBytes()));
    ServletUnitClient sc = sr.newClient();
    {
        WebRequest request = new GetMethodWebRequest("http://test/myServlet/1");
        request.setParameter("sha", sha.getDigest());
        try {
            sc.getResponse(request);
            fail("bucket not found => 404");
        } catch (HttpNotFoundException e) {
            assertEquals(String.format("Bucket '1' does not have a file matching digest '%s'", sha.getDigest()), e.getResponseMessage());
        }
    }
    bucket.append("FormPost", new BasicBSONObject("data", new BasicBSONObject("files", new BasicBSONObject("file", new BasicBSONObject("filename", "a.txt").append("sha", sha.getSHA())))));
    {
        WebRequest request = new GetMethodWebRequest("http://test/myServlet/1");
        request.setParameter("sha", sha.getDigest());
        WebResponse response = sc.getResponse(request);
        assertEquals("test", response.getText());
        assertEquals(sha.getDigest(), response.getHeaderField("ETag"));
        assertEquals(4, response.getContentLength());
        assertEquals("attachment; filename=\"a.txt\"", response.getHeaderField("Content-Disposition"));
    }
    {
        WebRequest request = new GetMethodWebRequest("http://test/myServlet/1");
        request.setParameter("sha", sha.getDigest());
        request.setParameter("filename", "x.txt");
        WebResponse response = sc.getResponse(request);
        assertEquals("test", response.getText());
        assertEquals(sha.getDigest(), response.getHeaderField("ETag"));
        assertEquals(4, response.getContentLength());
        assertEquals("attachment; filename=\"x.txt\"", response.getHeaderField("Content-Disposition"));
    }
    {
        WebRequest request = new GetMethodWebRequest("http://test/myServlet/1");
        request.setParameter("sha", sha.getDigest());
        request.setHeaderField("If-None-Match", sha.getDigest());
        WebResponse response = sc.getResponse(request);
        assertEquals(HttpServletResponse.SC_NOT_MODIFIED, response.getResponseCode());
    }
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) HttpNotFoundException(com.meterware.httpunit.HttpNotFoundException) MongoContentStorage(v7db.files.mongodb.MongoContentStorage) WebResponse(com.meterware.httpunit.WebResponse) WebRequest(com.meterware.httpunit.WebRequest) GetMethodWebRequest(com.meterware.httpunit.GetMethodWebRequest) PutMethodWebRequest(com.meterware.httpunit.PutMethodWebRequest) PostMethodWebRequest(com.meterware.httpunit.PostMethodWebRequest) ContentSHA(v7db.files.spi.ContentSHA) ByteArrayInputStream(java.io.ByteArrayInputStream) ServletUnitClient(com.meterware.servletunit.ServletUnitClient) GetMethodWebRequest(com.meterware.httpunit.GetMethodWebRequest)

Example 44 with BasicBSONObject

use of org.bson.BasicBSONObject in project v7files by thiloplanz.

the class MiltonServletTest method testCOPYFile.

public void testCOPYFile() throws IOException, SAXException {
    ServletUnitClient sc = sr.newClient();
    sc.setAuthentication("V7Files", "admin", "admin");
    prepareMockData("test.v7files.files", new BasicBSONObject("_id", new ObjectId()).append("filename", "a.txt").append("parent", "webdav").append("in", "abcd".getBytes()));
    {
        WebRequest request = new CopyWebRequest("http://test/myServlet/a.txt", "/myServlet/b.txt");
        WebResponse resp = sc.getResponse(request);
        assertEquals(HttpServletResponse.SC_CREATED, resp.getResponseCode());
    }
    assertGET(sc, "http://test/myServlet/a.txt", "abcd");
    assertGET(sc, "http://test/myServlet/b.txt", "abcd");
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) WebResponse(com.meterware.httpunit.WebResponse) WebRequest(com.meterware.httpunit.WebRequest) GetMethodWebRequest(com.meterware.httpunit.GetMethodWebRequest) PutMethodWebRequest(com.meterware.httpunit.PutMethodWebRequest) ObjectId(org.bson.types.ObjectId) ServletUnitClient(com.meterware.servletunit.ServletUnitClient)

Example 45 with BasicBSONObject

use of org.bson.BasicBSONObject in project v7files by thiloplanz.

the class MongoContentStorageTest method testFindByPrefix.

public void testFindByPrefix() throws IOException {
    byte[] data = "abcdefghijklmnopqrstuvwxyz".getBytes();
    byte[] sha = DigestUtils.sha(data);
    prepareMockData("test.v7files.content", new BasicBSONObject("_id", sha).append("in", data));
    Mongo mongo = getMongo();
    MongoContentStorage storage = new MongoContentStorage(mongo.getDB("test"));
    assertEquals(new String(data), IOUtils.toString(storage.findContentByPrefix(sha).getInputStream()));
    assertEquals(new String(data), IOUtils.toString(storage.findContentByPrefix(ArrayUtils.subarray(sha, 0, 5)).getInputStream()));
    assertNull(storage.findContentByPrefix(new byte[] { 0, 0, 0, 0 }));
    mongo.close();
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) Mongo(com.mongodb.Mongo)

Aggregations

BasicBSONObject (org.bson.BasicBSONObject)88 Test (org.junit.Test)39 BSONObject (org.bson.BSONObject)37 ArrayList (java.util.ArrayList)15 StructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)14 BSONWritable (com.mongodb.hadoop.io.BSONWritable)13 ListObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector)13 MapObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector)13 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)13 BasicDBObject (com.mongodb.BasicDBObject)11 ObjectId (org.bson.types.ObjectId)11 IOException (java.io.IOException)8 ByteArrayInputStream (java.io.ByteArrayInputStream)6 DataBag (org.apache.pig.data.DataBag)6 Map (java.util.Map)5 Tuple (org.apache.pig.data.Tuple)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 Mongo (com.mongodb.Mongo)4 Date (java.util.Date)4 DoubleWritable (org.apache.hadoop.io.DoubleWritable)4