@Override
  public <T> List<T> findAllAndRemove(String jxQuery, String collectionName) {
    CollectionMetaData cmd = cmdMap.get(collectionName);
    @SuppressWarnings("unchecked")
    Map<Object, T> collection = (Map<Object, T>) collectionsRef.get().get(collectionName);
    if((null == cmd) || (null == collection)) {
      throw new InvalidJsonDbApiUsageException("Collection by name '" + collectionName + "' not found. Create collection first.");
    }
    cmd.getCollectionLock().writeLock().lock();
    try {
      JXPathContext context = contextsRef.get().get(collectionName);
      @SuppressWarnings("unchecked")
      Iterator<T> resultItr = context.iterate(jxQuery);
      Set<Object> removeIds = new HashSet<Object>();
      while (resultItr.hasNext()) {
        T objectToRemove = resultItr.next();
        Object idToRemove = Util.getIdForEntity(objectToRemove, cmd.getIdAnnotatedFieldGetterMethod());
        removeIds.add(idToRemove);
      }

      if(removeIds.size() < 1) {
        return null;
      }

      JsonWriter jw;
      try {
        jw = new JsonWriter(dbConfig, cmd, collectionName, fileObjectsRef.get().get(collectionName));
      } catch (IOException ioe) {
        logger.error("Failed to obtain writer for " + collectionName, ioe);
        throw new JsonDBException("Failed to save " + collectionName, ioe);
      }
      boolean substractResult = jw.removeFromJsonFile(collection, removeIds);

      List<T> removedObjects = null;
      if(substractResult) {
        removedObjects = new ArrayList<T>();
        for (Object id : removeIds) {
          // Don't need to clone it, this object no more exists in the collection
          removedObjects.add(collection.remove(id));
        }
      }
      return removedObjects;

    } finally {
      cmd.getCollectionLock().writeLock().unlock();
    }
  }
@ Override public < T > List < T > findAllAndRemove ( String jxQuery , String collectionName ) { CollectionMetaData cmd = cmdMap . get ( collectionName ) ; @ SuppressWarnings ( "unchecked" ) Map < Object , T > collection = ( Map < Object , T > ) collectionsRef . get ( ) . get ( collectionName ) ; if ( ( null == cmd ) || ( null == collection ) ) { throw new InvalidJsonDbApiUsageException ( "Collection by name '" + collectionName + "' not found. Create collection first." ) ; } cmd . getCollectionLock ( ) . writeLock ( ) . lock ( ) ; try { JXPathContext context = contextsRef . get ( ) . get ( collectionName ) ; @ SuppressWarnings ( "unchecked" ) Iterator < T > resultItr = context . iterate ( jxQuery ) ; Set < Object > removeIds = new HashSet < Object > ( ) ; while ( resultItr . hasNext ( ) ) { T objectToRemove = resultItr . next ( ) ; Object idToRemove = Util . getIdForEntity ( objectToRemove , cmd . getIdAnnotatedFieldGetterMethod ( ) ) ; removeIds . add ( idToRemove ) ; } if ( removeIds . size ( ) < 1 ) { return null ; } JsonWriter jw ; try { jw = new JsonWriter ( dbConfig , cmd , collectionName , fileObjectsRef . get ( ) . get ( collectionName ) ) ; } catch ( IOException ioe ) { logger . error ( "Failed to obtain writer for " + collectionName , ioe ) ; throw new JsonDBException ( "Failed to save " + collectionName , ioe ) ; } boolean substractResult = jw . removeFromJsonFile ( collection , removeIds ) ; List < T > removedObjects = null ; if ( substractResult ) { removedObjects = new ArrayList < T > ( ) ; for ( Object id : removeIds ) { removedObjects . add ( collection . remove ( id ) ) ; } } return removedObjects ; } finally { cmd . getCollectionLock ( ) . writeLock ( ) . unlock ( ) ; } }
/* (non-Javadoc)
@see org.jsondb.JsonDBOperations#findAllAndRemove(java.lang.String, java.lang.String)