Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2014 Dell, Inc.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *     http://www.apache.org/licenses/LICENSE-2.0
   * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.dell.doradus.common;
 
 import java.io.Reader;
 import java.util.List;
 import java.util.Map;

Represents a batch of DBObjects being added or updated. If all objects belong to the same table, m_tableDef will be defined. If objects may belong to different tables, m_tableDef will be null, and each DBObject points to the table to which it belongs.
 
 public class DBObjectBatch {
     // Members:
     private final List<DBObjectm_dbObjList = new ArrayList<DBObject>();
     
     // SajListener to parse JSON directly into DBObjectBatch/DBObject objects. Example JSON
     // structure we expect:
     //
     //    {"batch": {
     //        "docs": [
     //           {"doc": {
     //              "_ID": "sassafras",
     //              "_table": "Books",
     //              "ISBN": "978-0061673733",
     //              "Address": {
     //                 "City": "Aliso Viejo",
     //                 "State": "CA",
     //                 "Zipcode": 92656
     //              },
     //              "Children": {"add": [123, 456]},
     //              "Tags": {"add": ["Biography", "Philosophy"]}
     //           }},
     //           {"doc": {
     //              "_ID": "xyzzy",
     //              "_table": "Books",
     //              "_deleted": "true"
     //           }},
     //           {"doc": {
     //              ...
     //           }}
     //        ]
     //     }}
     // 
     // Since this class is not static, each instance belongs to a DBObjectBatch instance.
     class Listener implements JSONAnnie.SajListener {
         // Name keys use dotted notation; e.g. _ID=[sassafras], Address.City=[Aliso Viejo],
         Map<StringList<String>> valueMap = new HashMap<>();
         List<StringfieldStack = new ArrayList<>();
         StringBuilder buffer = new StringBuilder();
         int level = 0;   // 0=batch object, 1=docs array, 2=doc object, 3+=field object
         
         @Override
         public void onStartObject(String name) {
             switch () {
             case 0:     // outer batch level 
                 Utils.require(name.equals("batch"), "Root node must be 'batch': " + name);
                 break;
             case 1:     // docs level: can be an object with one "doc" child
                 Utils.require(name.equals("docs"), "'docs' array expected: " + name);
                 ++;
                 break;
             case 2:     // doc object
                 Utils.require(name.equals("doc"), "'doc' object expected: " + name);
                 .clear();
                 .clear();
                 break;
             default:     // outer or nested field
                 .add(name);
             }
             ++;
         }   // onStartObject
         
         @Override
         public void onEndObject() {
             if (.size() > 0) {
                 .remove(.size() - 1);
             }
             if (-- == 2) {
                 buildObject();    // just finished a "doc" object
             }
        }   // onEndObject
        
        @Override
        public void onStartArray(String name) {
            switch () {
            case 0:
            case 2:     // An array is unexpected for "batch" or "doc"
                Utils.require(false"Unexpected array start: " + name);
            case 1:     // Should be "docs" array.
                Utils.require(name.equals("docs"), "'docs' array expected: " + name);
                break;
            default:    // outer or nested field.
                .add(name);
            }
            ++;
        }   // onStartArray
        
        @Override
        public void onEndArray() {
            if (.size() > 0) {
                .remove(.size() - 1);
            }
            --;
        }   // onEndArray
        
        @Override
        public void onValue(String nameString value) {
            // Values are only expected within field elements (level >= 3)
            Utils.require( >= 3, "Unexpected recognized element: %s"name);
            saveValue(namevalue);
        }   // onValue
        
        // Save a leaf-level value
        private void saveValue(String nameString value) {
            String dottedName = getDottedName(name);
            List<Stringvalues = .get(dottedName);
            if (values == null) {
                values = new ArrayList<String>(1);
                .put(dottedNamevalues);
            }
            values.add(value);
        }   // saveValue
        
        // Turn parent node names, if any, into a dotted string and append name.
        private String getDottedName(String name) {
            .setLength(0);
            for (String parentName : ) {
                .append(parentName);
                .append(".");
            }
            .append(name);
            return .toString();
        }   // getDottedName
        
        // Create a DBObject for the just-parsed 'doc' element and add to batch.
        private void buildObject() {
            DBObject dbObj = addObject(nullnull);
            for (String dottedName : .keySet()) {
                List<Stringvalues = .get(dottedName);
                String[] names = dottedName.split("\\.");
                if (names.length == 1) {
                    dbObj.addFieldValues(names[0], values);
                } else {
                    addObjectValue(dbObjnames, 0, values);
                }
            }
        }   // buildObject
        
        // Possible name structures we expect:
        //      field.add.value or field.remove.value
        //      group.field
        //      group.field.[add|remove].value
        //      group1.group2.field
        //      group1.group2.field.[add|remove].value
        //      ...
        private void addObjectValue(DBObject dbObjString[] namesint inxList<Stringvalues) {
            String fieldName = names[inx];
            if ((names.length - inx) == 3 &&
                    names[inx + 2].equals("value") &&
                    (names[inx + 1].equals("add") || names[inx + 1].equals("remove"))) {
                // field.add.value or field.remove.value
                if (names[inx + 1].equals("add")) {
                    dbObj.addFieldValues(fieldNamevalues);
                } else {
                    dbObj.removeFieldValues(fieldNamevalues);
                }
            } else if ((names.length - inx) > 1) {
                // group.field...: recurse to inx + 1.
                addObjectValue(dbObjnamesinx + 1, values);
            } else {
                // field
                dbObj.addFieldValues(fieldNamevalues);
            }
        }   // addObjectValue
        
    }   // class Listener
    
    
Construct a new, empty batch object.
    public DBObjectBatch() {
    }   // constructor
    
    
Parse a batch update described in JSON from a Reader, adding DBObjects to this batch object for each 'doc' element found.

Parameters:
reader Character reader from which JSON text is to be read.
    public void parseJSON(Reader reader) {
        Listener sajListener = new Listener();
        new JSONAnnie(reader).parse(sajListener);
    }   // parseJSON
    
    
Parse a batch object update rooted at the given UNode. The root node must be a MAP named "batch". Child nodes must be a recognized option or a "docs" array containing "doc" objects. An exception is thrown if the batch is malformed.

Parameters:
rootNode Root node of a UNode tree defined an object update batch.
    public void parse(UNode rootNode) {
        assert rootNode != null;
        
        // Ensure root node is a MAP named "batch".
        Utils.require(rootNode.getName().equals("batch"),
                      "'batch' expected: " + rootNode.getName());
        Utils.require(rootNode.isMap(), "'batch' node must be a map of unique names");
        // Parse child nodes.
        for (String memberName : rootNode.getMemberNames()) {
            UNode childNode = rootNode.getMember(memberName);
            if (childNode.getName().equals("docs")) {
                Utils.require(childNode.isCollection(), "'docs' must be a collection: " + childNode);
                for (UNode docNode : childNode.getMemberList()) {
                    Utils.require(docNode.getName().equals("doc") && docNode.isMap(),
                                  "'doc' map node expected as child of 'docs': " + docNode);
                    addObject(new DBObject()).parse(docNode);
                }
            } else {
                Utils.require(false"Unrecognized child node of 'batch': " + memberName);
            }
        }
    }   // parse
    ////////// Getters
    
    
Get the collection of DBObjects contained in this batch. The collection is not copied.

Returns:
The list of DBObjects contained in this batch.
    public Iterable<DBObjectgetObjects() {
        return ;
    }   // getObjects

    
Return the number of objects currently contained in this batch.

Returns:
The number of objects currently contained in this batch.
    public int getObjectCount() {
        return .size();
    }   // getObjectCount

    
Serialize this DBObjectBatch object into a UNode tree and return the root node.

Returns:
Root node of a UNode tree representing this batch update.
    public UNode toDoc() {
        // Root object is a MAP called "batch".
        UNode batchNode = UNode.createMapNode("batch");
        
        // Add a "docs" node as an array.
        UNode docsNode = batchNode.addArrayNode("docs");
        for (DBObject dbObj : ) {
            docsNode.addChildNode(dbObj.toDoc());
        }
        return batchNode;
    }   // toDoc
    ////////// Setters
    
    
Add a DBObject to this batch.

Parameters:
dbObj DBObject to add to this batch.
Returns:
Same DBObject.
    public DBObject addObject(DBObject dbObj) {
        .add(dbObj);
        return dbObj;
    }   // addObject

    
Create a new DBObject with the given object ID and table name, add it to this DBObjectBatch, and return it.

Parameters:
objID New DBObject's object ID, if any.
tableName New DBObject's table name, if any.
Returns:
New DBObject.
    public DBObject addObject(String objIDString tableName) {
        DBObject dbObj = new DBObject(objIDtableName);
        .add(dbObj);
        return dbObj;
    }   // addObject
    
    
Clear this batch of all DBObjects, if any.
    public void clear() {
        .clear();
    }   // clear
    
}   // class DBObjectBatch
New to GrepCode? Check out our FAQ X