src/org/ohmage/query/impl/DocumentQueries.java
private void init(int numFilesPerDirectory) throws DataAccessException {
		// Get the lock.
		DIRECTORY_CREATION_LOCK.lock();
		
		try {
			// If the current leaf directory has been set, we weren't the
			// first to call init(), so we can just unlock the lock and back
			// out.
			if(currLeafDirectory != null) {
				return;
			}
			
			// Get the root directory from the preference cache based on the
			// key.
			String rootFile;
			try {
				rootFile = PreferenceCache.instance().lookup(PreferenceCache.KEY_DOCUMENT_DIRECTORY);
			}
			catch(CacheMissException e) {
				throw new DataAccessException("Preference cache doesn't know about 'known' key: " + PreferenceCache.KEY_DOCUMENT_DIRECTORY, e);
			}
			File rootDirectory = new File(rootFile);
			if(! rootDirectory.exists()) {
				throw new DataAccessException("The root file doesn't exist suggesting an incomplete installation: " + rootFile);
			}
			else if(! rootDirectory.isDirectory()) {
				throw new DataAccessException("The root file isn't a directory.");
			}
			
			// Get the number of folders deep that documents are stored.
			int fileDepth;
			try {
				fileDepth = Integer.decode(PreferenceCache.instance().lookup(PreferenceCache.KEY_FILE_HIERARCHY_DEPTH));
			}
			catch(CacheMissException e) {
				throw new DataAccessException("Preference cache doesn't know about 'known' key: " + PreferenceCache.KEY_FILE_HIERARCHY_DEPTH, e);
			}
			catch(NumberFormatException e) {
				throw new DataAccessException("Stored value for key '" + PreferenceCache.KEY_FILE_HIERARCHY_DEPTH + "' is not decodable as a number.", e);
			}
			
			DirectoryFilter directoryFilter = new DirectoryFilter();
			File currDirectory = rootDirectory;
			for(int currDepth = 0; currDepth < fileDepth; currDepth++) {
				// Get the list of directories in the current directory.
				File[] currDirectories = currDirectory.listFiles(directoryFilter);
				
				// If there aren't any, create the first subdirectory in this
				// directory.
				if(currDirectories.length == 0) {
					String newFolderName = directoryNameBuilder(0, numFilesPerDirectory);
					currDirectory = new File(currDirectory.getAbsolutePath() + "/" + newFolderName);
					currDirectory.mkdir();
				}
				// If the directory is overly full, step back up in the
				// structure. This should never happen, as it indicates that
				// there is an overflow in the structure.
				else if(currDirectories.length > numFilesPerDirectory) {
					LOGGER.warn("Too many subdirectories in: " + currDirectory.getAbsolutePath());
					
					// Take a step back in our depth.
					currDepth--;
					
					// If, while backing up the tree, we back out of the root
					// directory, we have filled up the space.
					if(currDepth < 0) {
						throw new DataAccessException("Document structure full!");
					}

					// Get the next parent and the current directory to it.
					int nextDirectoryNumber = Integer.decode(currDirectory.getName()) + 1;
					currDirectory = new File(currDirectory.getParent() + "/" + nextDirectoryNumber);
					
					// If the directory already exists, then there is either a
					// concurrency issue or someone else is adding files.
					// Either way, this shouldn't happen.
					if(currDirectory.exists()) {
						LOGGER.error("Somehow the 'new' directory already exists. This should be looked into: " + currDirectory.getAbsolutePath());
					}
					// Otherwise, create the directory.
					else {
						currDirectory.mkdir();
					}
				}
				// Drill down to the directory with the largest, numeric value.
				else {
					currDirectory = getLargestSubfolder(currDirectories);
				}
			}
			
			// After we have found a suitable directory, set it.
			currLeafDirectory = currDirectory;
		}
		catch(SecurityException e) {
			throw new DataAccessException("The current process doesn't have sufficient permiossions to create new directories.", e);
		}
		finally {
			// No matter what happens, unlock the lock.
			DIRECTORY_CREATION_LOCK.unlock();
		}
	}
/**
	 * Initializes the directory structure by drilling down to the leaf
	 * directory with each step choosing the directory with the largest
	 * integer value.
	 */
Initializes the directory structure by drilling down to the leaf
directory with each step choosing the directory with the largest
integer value.
Initializes the directory structure by drilling down to the leaf directory with each step choosing the directory with the largest integer value .
private void init(int numFilesPerDirectory) throws DataAccessException {
		DIRECTORY_CREATION_LOCK.lock();
		try {
			if(currLeafDirectory != null) {
				return;
			}
			String rootFile;
			try {
				rootFile = PreferenceCache.instance().lookup(PreferenceCache.KEY_DOCUMENT_DIRECTORY);
			}
			catch(CacheMissException e) {
				throw new DataAccessException("Preference cache doesn't know about 'known' key: " + PreferenceCache.KEY_DOCUMENT_DIRECTORY, e);
			}
			File rootDirectory = new File(rootFile);
			if(! rootDirectory.exists()) {
				throw new DataAccessException("The root file doesn't exist suggesting an incomplete installation: " + rootFile);
			}
			else if(! rootDirectory.isDirectory()) {
				throw new DataAccessException("The root file isn't a directory.");
			}
			int fileDepth;
			try {
				fileDepth = Integer.decode(PreferenceCache.instance().lookup(PreferenceCache.KEY_FILE_HIERARCHY_DEPTH));
			}
			catch(CacheMissException e) {
				throw new DataAccessException("Preference cache doesn't know about 'known' key: " + PreferenceCache.KEY_FILE_HIERARCHY_DEPTH, e);
			}
			catch(NumberFormatException e) {
				throw new DataAccessException("Stored value for key '" + PreferenceCache.KEY_FILE_HIERARCHY_DEPTH + "' is not decodable as a number.", e);
			}
			DirectoryFilter directoryFilter = new DirectoryFilter();
			File currDirectory = rootDirectory;
			for(int currDepth = 0; currDepth < fileDepth; currDepth++) {
				File[] currDirectories = currDirectory.listFiles(directoryFilter);
				if(currDirectories.length == 0) {
					String newFolderName = directoryNameBuilder(0, numFilesPerDirectory);
					currDirectory = new File(currDirectory.getAbsolutePath() + "/" + newFolderName);
					currDirectory.mkdir();
				}
				else if(currDirectories.length > numFilesPerDirectory) {
					LOGGER.warn("Too many subdirectories in: " + currDirectory.getAbsolutePath());
					currDepth--;
					if(currDepth < 0) {
						throw new DataAccessException("Document structure full!");
					}
					int nextDirectoryNumber = Integer.decode(currDirectory.getName()) + 1;
					currDirectory = new File(currDirectory.getParent() + "/" + nextDirectoryNumber);
					if(currDirectory.exists()) {
						LOGGER.error("Somehow the 'new' directory already exists. This should be looked into: " + currDirectory.getAbsolutePath());
					}
					else {
						currDirectory.mkdir();
					}
				}
				else {
					currDirectory = getLargestSubfolder(currDirectories);
				}
			}
			currLeafDirectory = currDirectory;
		}
		catch(SecurityException e) {
			throw new DataAccessException("The current process doesn't have sufficient permiossions to create new directories.", e);
		}
		finally {
			DIRECTORY_CREATION_LOCK.unlock();
		}
	}
private void init ( int numFilesPerDirectory ) throws DataAccessException { DIRECTORY_CREATION_LOCK . lock ( ) ; try { if ( currLeafDirectory != null ) { return ; } String rootFile ; try { rootFile = PreferenceCache . instance ( ) . lookup ( PreferenceCache . KEY_DOCUMENT_DIRECTORY ) ; } catch ( CacheMissException e ) { throw new DataAccessException ( "Preference cache doesn't know about 'known' key: " + PreferenceCache . KEY_DOCUMENT_DIRECTORY , e ) ; } File rootDirectory = new File ( rootFile ) ; if ( ! rootDirectory . exists ( ) ) { throw new DataAccessException ( "The root file doesn't exist suggesting an incomplete installation: " + rootFile ) ; } else if ( ! rootDirectory . isDirectory ( ) ) { throw new DataAccessException ( "The root file isn't a directory." ) ; } int fileDepth ; try { fileDepth = Integer . decode ( PreferenceCache . instance ( ) . lookup ( PreferenceCache . KEY_FILE_HIERARCHY_DEPTH ) ) ; } catch ( CacheMissException e ) { throw new DataAccessException ( "Preference cache doesn't know about 'known' key: " + PreferenceCache . KEY_FILE_HIERARCHY_DEPTH , e ) ; } catch ( NumberFormatException e ) { throw new DataAccessException ( "Stored value for key '" + PreferenceCache . KEY_FILE_HIERARCHY_DEPTH + "' is not decodable as a number." , e ) ; } DirectoryFilter directoryFilter = new DirectoryFilter ( ) ; File currDirectory = rootDirectory ; for ( int currDepth = 0 ; currDepth < fileDepth ; currDepth ++ ) { File [ ] currDirectories = currDirectory . listFiles ( directoryFilter ) ; if ( currDirectories . length == 0 ) { String newFolderName = directoryNameBuilder ( 0 , numFilesPerDirectory ) ; currDirectory = new File ( currDirectory . getAbsolutePath ( ) + "/" + newFolderName ) ; currDirectory . mkdir ( ) ; } else if ( currDirectories . length > numFilesPerDirectory ) { LOGGER . warn ( "Too many subdirectories in: " + currDirectory . getAbsolutePath ( ) ) ; currDepth -- ; if ( currDepth < 0 ) { throw new DataAccessException ( "Document structure full!" ) ; } int nextDirectoryNumber = Integer . decode ( currDirectory . getName ( ) ) + 1 ; currDirectory = new File ( currDirectory . getParent ( ) + "/" + nextDirectoryNumber ) ; if ( currDirectory . exists ( ) ) { LOGGER . error ( "Somehow the 'new' directory already exists. This should be looked into: " + currDirectory . getAbsolutePath ( ) ) ; } else { currDirectory . mkdir ( ) ; } } else { currDirectory = getLargestSubfolder ( currDirectories ) ; } } currLeafDirectory = currDirectory ; } catch ( SecurityException e ) { throw new DataAccessException ( "The current process doesn't have sufficient permiossions to create new directories." , e ) ; } finally { DIRECTORY_CREATION_LOCK . unlock ( ) ; } }
Initializes the directory structure by drilling down to the leaf
 directory with each step choosing the directory with the largest
 integer value.
Initializes the directory structure by drilling down to the leaf directory with each step choosing the directory with the largest integer value .
// Get the lock. // If the current leaf directory has been set, we weren't the // first to call init(), so we can just unlock the lock and back // out. // Get the root directory from the preference cache based on the // key. // Get the number of folders deep that documents are stored. // Get the list of directories in the current directory. // If there aren't any, create the first subdirectory in this // directory. // If the directory is overly full, step back up in the // structure. This should never happen, as it indicates that // there is an overflow in the structure. // Take a step back in our depth. // If, while backing up the tree, we back out of the root // directory, we have filled up the space. // Get the next parent and the current directory to it. // If the directory already exists, then there is either a // concurrency issue or someone else is adding files. // Either way, this shouldn't happen. // Otherwise, create the directory. // Drill down to the directory with the largest, numeric value. // After we have found a suitable directory, set it. // No matter what happens, unlock the lock.