Example usage for com.mongodb DefaultDBDecoder DefaultDBDecoder

List of usage examples for com.mongodb DefaultDBDecoder DefaultDBDecoder

Introduction

In this page you can find the example usage for com.mongodb DefaultDBDecoder DefaultDBDecoder.

Prototype

DefaultDBDecoder

Source Link

Usage

From source file:com.lakeside.data.mongo.MongoDbRestore.java

License:Open Source License

protected void restore(CollectionInfo info) {
    try {/*from   w w w . j a v  a  2s .  co  m*/
        String collectionName = info.getName();
        System.out.println("restoring collection " + collectionName);
        File[] files = new File(INPUT_DIR).listFiles();
        if (files != null) {
            List<File> filesToProcess = new ArrayList<File>();
            for (File file : files) {
                String name = file.getName();
                int indexOf = name.indexOf(".bson");
                if (name.startsWith(collectionName) && indexOf > 0) {
                    String temp = name.substring(collectionName.length() + 1, indexOf);
                    if (temp.matches("\\d*")) {
                        filesToProcess.add(file);
                    }
                }
            }
            Collections.sort(filesToProcess, new FilenameComparator());
            if (DROP_EXISTING) {
                try {
                    System.out.println("Dropping collection " + collectionName);

                    DB db = MongoDBConnectionManager.getConnection(DATABASE_HOST, "local", DATABASE_USER_NAME,
                            DATABASE_PASSWORD);

                    DBCollection coll = db.getCollection(collectionName);
                    coll.drop();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            long count = 0;
            long startTime = System.currentTimeMillis();
            long lastOutput = System.currentTimeMillis();
            for (File file : filesToProcess) {
                System.out.println("restoring file " + file.getName() + " to collection " + collectionName);
                InputStream inputStream = null;
                try {
                    if (file.getName().endsWith(".gz")) {
                        inputStream = new GZIPInputStream(new FileInputStream(file));
                    } else {
                        inputStream = new BufferedInputStream(new FileInputStream(file));
                    }
                    BSONDecoder decoder = new DefaultDBDecoder();
                    while (true) {
                        if (inputStream.available() == 0) {
                            break;
                        }
                        BSONObject obj = decoder.readObject(inputStream);
                        if (obj == null) {
                            break;
                        }
                        write(info, new BasicDBObject((BasicBSONObject) obj));
                        count++;

                        long duration = System.currentTimeMillis() - lastOutput;
                        if (duration > REPORT_DURATION) {
                            report(collectionName, count, System.currentTimeMillis() - startTime);
                            lastOutput = System.currentTimeMillis();
                        }
                    }
                } catch (java.io.EOFException e) {
                    break;
                } finally {
                    inputStream.close();
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.wordnik.system.mongodb.Analyzer.java

License:Open Source License

protected void run() {
    long startTime = System.currentTimeMillis();
    //  decide what collections to process
    selectCollections();// w  w  w .jav a2 s  .  com

    //  create any re-mappings
    Map<String, String> collectionMappings = new HashMap<String, String>();
    Map<String, String> databaseMappings = new HashMap<String, String>();
    createMappings(DATABASE_MAPPING_STRING, COLLECTION_MAPPING_STRING, databaseMappings, collectionMappings);

    try {
        File[] files = new File(INPUT_DIR).listFiles();
        if (files != null) {
            List<File> filesToProcess = new ArrayList<File>();
            for (File file : files) {
                if (file.getName().indexOf(".bson") > 0) {
                    filesToProcess.add(file);
                }
            }
            long operationsRead = 0;
            long operationsSkipped = 0;
            long lastOutput = System.currentTimeMillis();
            for (File file : filesToProcess) {
                System.out.println("analyzing file " + file.getName());
                BufferedInputStream inputStream = null;
                try {
                    if (file.getName().endsWith(".gz")) {
                        InputStream is = new GZIPInputStream(new FileInputStream(file));
                        inputStream = new BufferedInputStream(is);
                    } else {
                        inputStream = new BufferedInputStream(new FileInputStream(file));
                    }
                    BSONDecoder decoder = new DefaultDBDecoder();
                    while (true) {
                        if (inputStream.available() == 0) {
                            break;
                        }
                        BSONObject obj = decoder.readObject(inputStream);
                        if (obj == null) {
                            break;
                        }
                        BasicDBObject dbo = new BasicDBObject((BasicBSONObject) obj);

                        BSONTimestamp operationTimestamp = (BSONTimestamp) dbo.get("ts");
                        String namespace = dbo.getString("ns");

                        processRecord(dbo);
                        operationsRead++;

                        long durationSinceLastOutput = System.currentTimeMillis() - lastOutput;
                        if (durationSinceLastOutput > REPORT_INTERVAL) {
                            report(operationsRead, System.currentTimeMillis() - startTime);
                            lastOutput = System.currentTimeMillis();
                        }
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    report(0, System.currentTimeMillis() - startTime);
}

From source file:com.wordnik.system.mongodb.ReplayUtil.java

License:Open Source License

protected void run() {
    long startTime = System.currentTimeMillis();
    //   decide what collections to process
    selectCollections();//  w  w w. j ava2 s  .c  om

    OplogReplayWriter util = new OplogReplayWriter();

    //   create any re-mappings
    Map<String, String> collectionMappings = new HashMap<String, String>();
    Map<String, String> databaseMappings = new HashMap<String, String>();
    createMappings(DATABASE_MAPPING_STRING, COLLECTION_MAPPING_STRING, databaseMappings, collectionMappings);

    //   configure the writer
    util.setCollectionMappings(collectionMappings);
    util.setDatabaseMappings(databaseMappings);
    util.setDestinationDatabaseUsername(DEST_DATABASE_USER_NAME);
    util.setDestinationDatabasePassword(DEST_DATABASE_PASSWORD);
    util.setDestinationDatabaseHost(DEST_DATABASE_HOST);

    try {
        File[] files = new File(INPUT_DIR).listFiles();
        if (files != null) {
            List<File> filesToProcess = new ArrayList<File>();
            for (File file : files) {
                if (file.getName().indexOf(".bson") > 0) {
                    filesToProcess.add(file);
                }
            }
            long operationsRead = 0;
            long operationsSkipped = 0;
            long lastOutput = System.currentTimeMillis();
            for (File file : filesToProcess) {
                System.out.println("replaying file " + file.getName());
                BufferedInputStream inputStream = null;
                try {
                    if (file.getName().endsWith(".gz")) {
                        InputStream is = new GZIPInputStream(new FileInputStream(file));
                        inputStream = new BufferedInputStream(is);
                    } else {
                        inputStream = new BufferedInputStream(new FileInputStream(file));
                    }
                    BSONDecoder decoder = new DefaultDBDecoder();
                    while (true) {
                        if (inputStream.available() == 0) {
                            break;
                        }
                        BSONObject obj = decoder.readObject(inputStream);
                        if (obj == null) {
                            break;
                        }
                        BasicDBObject dbo = new BasicDBObject((BasicBSONObject) obj);

                        BSONTimestamp operationTimestamp = (BSONTimestamp) dbo.get("ts");
                        String namespace = dbo.getString("ns");
                        String collection = util.getUnmappedCollectionFromNamespace(namespace);

                        boolean shouldProcess = shouldProcessRecord(collection, operationTimestamp);

                        if (collection != null && shouldProcess) {
                            util.processRecord(dbo);
                            operationsRead++;
                        } else {
                            operationsSkipped++;
                        }

                        long durationSinceLastOutput = System.currentTimeMillis() - lastOutput;
                        if (durationSinceLastOutput > REPORT_INTERVAL) {
                            report(util.getInsertCount(), util.getUpdateCount(), util.getDeleteCount(),
                                    operationsRead, operationsSkipped, System.currentTimeMillis() - startTime);
                            lastOutput = System.currentTimeMillis();
                        }
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.wordnik.system.mongodb.RestoreUtil.java

License:Open Source License

protected void restore(CollectionInfo info) {
    try {//from  w w w  .j  a va  2  s .com
        System.out.println("restoring collection " + info.getName());
        File[] files = new File(INPUT_DIR).listFiles();
        if (files != null) {
            List<File> filesToProcess = new ArrayList<File>();
            for (File file : files) {
                if (file.getName().startsWith(info.getName()) && file.getName().indexOf(".bson") > 0) {
                    filesToProcess.add(file);
                }
            }
            Collections.sort(filesToProcess, new FilenameComparator());
            if (DROP_EXISTING) {
                try {
                    System.out.println("Dropping collection " + info.getName());

                    DB db = MongoDBConnectionManager.getConnection("DB", DATABASE_HOST, "local",
                            DATABASE_USER_NAME, DATABASE_PASSWORD, SchemaType.READ_WRITE());

                    DBCollection coll = db.getCollection(info.getName());
                    coll.drop();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            long count = 0;
            long startTime = System.currentTimeMillis();
            long lastOutput = System.currentTimeMillis();
            for (File file : filesToProcess) {
                System.out.println("restoring file " + file.getName() + " to collection " + info.getName());
                InputStream inputStream = null;
                try {
                    if (file.getName().endsWith(".gz")) {
                        inputStream = new GZIPInputStream(new FileInputStream(file));
                    } else {
                        inputStream = new BufferedInputStream(new FileInputStream(file));
                    }
                    BSONDecoder decoder = new DefaultDBDecoder();
                    while (true) {
                        if (inputStream.available() == 0) {
                            break;
                        }
                        BSONObject obj = decoder.readObject(inputStream);
                        if (obj == null) {
                            break;
                        }
                        write(info, new BasicDBObject((BasicBSONObject) obj));
                        count++;

                        long duration = System.currentTimeMillis() - lastOutput;
                        if (duration > REPORT_DURATION) {
                            report(info.getName(), count, System.currentTimeMillis() - startTime);
                            lastOutput = System.currentTimeMillis();
                        }
                    }
                } catch (java.io.EOFException e) {
                    break;
                } finally {
                    inputStream.close();
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.fastmongo.odm.mapping.exception.MappingException.java

License:Apache License

private DBObject decode() {
    return new DefaultDBDecoder().decode(data, (DBCollection) null);
}

From source file:org.mule.module.mongo.tools.RestoreFile.java

License:Open Source License

public List<DBObject> getCollectionObjects() throws IOException {
    BSONDecoder bsonDecoder = new DefaultDBDecoder();
    BufferedInputStream inputStream = null;
    List<DBObject> dbObjects = new ArrayList<DBObject>();

    try {//from w  w w  .j ava2  s .com
        inputStream = new BufferedInputStream(new FileInputStream(file));
        while (inputStream.available() != 0) {
            BSONObject bsonObject = bsonDecoder.readObject(inputStream);
            if (bsonObject != null) {
                dbObjects.add(new BasicDBObject((BasicBSONObject) bsonObject));
            }
        }
        return dbObjects;
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
    }
}