Exemplo n.º 1
0
  /** Create directory entries for every item */
  boolean mkdirs(String src) {
    src = normalizePath(new UTF8(src));

    // Use this to collect all the dirs we need to construct
    Vector v = new Vector();

    // The dir itself
    v.add(src);

    // All its parents
    String parent = DFSFile.getDFSParent(src);
    while (parent != null) {
      v.add(parent);
      parent = DFSFile.getDFSParent(parent);
    }

    // Now go backwards through list of dirs, creating along
    // the way
    boolean lastSuccess = false;
    int numElts = v.size();
    for (int i = numElts - 1; i >= 0; i--) {
      String cur = (String) v.elementAt(i);
      INode inserted = unprotectedMkdir(cur);
      if (inserted != null) {
        logEdit(OP_MKDIR, new UTF8(inserted.computeName()), null);
        lastSuccess = true;
      } else {
        lastSuccess = false;
      }
    }
    return lastSuccess;
  }
Exemplo n.º 2
0
  /** Save the contents of the FS image */
  void saveFSImage(File fullimage, File edits) throws IOException {
    File curFile = new File(fullimage, FS_IMAGE);
    File newFile = new File(fullimage, NEW_FS_IMAGE);
    File oldFile = new File(fullimage, OLD_FS_IMAGE);

    //
    // Write out data
    //
    DataOutputStream out =
        new DataOutputStream(new BufferedOutputStream(new FileOutputStream(newFile)));
    try {
      out.writeInt(rootDir.numItemsInTree() - 1);
      rootDir.saveImage("", out);
    } finally {
      out.close();
    }

    //
    // Atomic move sequence
    //
    // 1.  Move cur to old
    curFile.renameTo(oldFile);

    // 2.  Move new to cur
    newFile.renameTo(curFile);

    // 3.  Remove pending-edits file (it's been integrated with newFile)
    edits.delete();

    // 4.  Delete old
    oldFile.delete();
  }
Exemplo n.º 3
0
 long computeContentsLength() {
   long total = computeFileLength();
   for (Iterator it = children.values().iterator(); it.hasNext(); ) {
     INode child = (INode) it.next();
     total += child.computeContentsLength();
   }
   return total;
 }
Exemplo n.º 4
0
 int numItemsInTree() {
   int total = 0;
   for (Iterator it = children.values().iterator(); it.hasNext(); ) {
     INode child = (INode) it.next();
     total += child.numItemsInTree();
   }
   return total + 1;
 }
Exemplo n.º 5
0
 /**
  * Called after a request has been handled.
  *
  * @param reval the request evaluator that handled the request
  */
 public void commit(RequestEvaluator reval, SessionManager smgr) {
   if (modifiedInRequest || cacheLastModified != cacheNode.lastModified()) {
     if (!registered) {
       smgr.registerSession(this);
       registered = true;
     }
     modifiedInRequest = false;
     cacheLastModified = cacheNode.lastModified();
   }
 }
Exemplo n.º 6
0
 /**
  * Collect all the blocks at this INode and all its children. This operation is performed after
  * a node is removed from the tree, and we want to GC all the blocks at this node and below.
  */
 void collectSubtreeBlocks(Vector v) {
   if (blocks != null) {
     for (int i = 0; i < blocks.length; i++) {
       v.add(blocks[i]);
     }
   }
   for (Iterator it = children.values().iterator(); it.hasNext(); ) {
     INode child = (INode) it.next();
     child.collectSubtreeBlocks(v);
   }
 }
Exemplo n.º 7
0
 String computeName() {
   if (parent != null) {
     return parent.computeName() + "/" + name;
   } else {
     return name;
   }
 }
Exemplo n.º 8
0
 static ByteIterable getNotNullValue(@NotNull final INode ln) {
   final ByteIterable value = ln.getValue();
   if (value == null) {
     throw new ExodusException("Value can't be null");
   }
   return value;
 }
Exemplo n.º 9
0
 /** Set the cache node for this session. */
 public void setCacheNode(INode node) {
   if (node == null) {
     throw new NullPointerException("cache node is null");
   }
   this.cacheNode = node;
   this.cacheLastModified = cacheNode.lastModified();
 }
Exemplo n.º 10
0
    INode getNode(Vector components, int index) {
      if (!name.equals((String) components.elementAt(index))) {
        return null;
      }
      if (index == components.size() - 1) {
        return this;
      }

      // Check with children
      INode child = (INode) children.get(components.elementAt(index + 1));
      if (child == null) {
        return null;
      } else {
        return child.getNode(components, index + 1);
      }
    }
Exemplo n.º 11
0
 void saveImage(String parentPrefix, DataOutputStream out) throws IOException {
   String fullName = "";
   if (parent != null) {
     fullName = parentPrefix + "/" + name;
     new UTF8(fullName).write(out);
     if (blocks == null) {
       out.writeInt(0);
     } else {
       out.writeInt(blocks.length);
       for (int i = 0; i < blocks.length; i++) {
         blocks[i].write(out);
       }
     }
   }
   for (Iterator it = children.values().iterator(); it.hasNext(); ) {
     INode child = (INode) it.next();
     child.saveImage(fullName, out);
   }
 }
Exemplo n.º 12
0
 /** Check whether the filepath could be created */
 public boolean isValidToCreate(UTF8 src) {
   String srcs = normalizePath(src);
   synchronized (rootDir) {
     if (srcs.startsWith("/") && !srcs.endsWith("/") && rootDir.getNode(srcs) == null) {
       return true;
     } else {
       return false;
     }
   }
 }
Exemplo n.º 13
0
 /** Get the blocks associated with the file */
 public Block[] getFile(UTF8 src) {
   waitForReady();
   synchronized (rootDir) {
     INode targetNode = rootDir.getNode(src.toString());
     if (targetNode == null) {
       return null;
     } else {
       return targetNode.blocks;
     }
   }
 }
Exemplo n.º 14
0
  /**
   * Get a listing of files given path 'src'
   *
   * <p>This function is admittedly very inefficient right now. We'll make it better later.
   */
  public DFSFileInfo[] getListing(UTF8 src) {
    String srcs = normalizePath(src);

    synchronized (rootDir) {
      INode targetNode = rootDir.getNode(srcs);
      if (targetNode == null) {
        return null;
      } else {
        Vector contents = new Vector();
        targetNode.listContents(contents);

        DFSFileInfo listing[] = new DFSFileInfo[contents.size()];
        int i = 0;
        for (Iterator it = contents.iterator(); it.hasNext(); i++) {
          listing[i] = new DFSFileInfo((INode) it.next());
        }
        return listing;
      }
    }
  }
Exemplo n.º 15
0
 boolean unprotectedAddFile(UTF8 name, Block blocks[]) {
   synchronized (rootDir) {
     if (blocks != null) {
       // Add file->block mapping
       for (int i = 0; i < blocks.length; i++) {
         activeBlocks.add(blocks[i]);
       }
     }
     return (rootDir.addNode(name.toString(), blocks) != null);
   }
 }
Exemplo n.º 16
0
 /**
  * Creates a new Session object.
  *
  * @param sessionId ...
  * @param app ...
  */
 public Session(String sessionId, Application app) {
   this.sessionId = sessionId;
   this.app = app;
   this.uid = null;
   this.userHandle = null;
   cacheNode = new TransientNode(app, "session");
   cacheLastModified = cacheNode.lastModified();
   // HACK - decrease timestamp by 1 to notice modifications
   // taking place immediately after object creation
   onSince = System.currentTimeMillis() - 1;
   lastTouched = lastModified = onSince;
 }
Exemplo n.º 17
0
  /** Attach the given user node to this session. */
  public void login(INode usernode) {
    if (usernode == null) {
      userHandle = null;
      uid = null;
    } else {
      userHandle = ((Node) usernode).getHandle();
      uid = usernode.getElementName();
    }

    lastModified = System.currentTimeMillis();
    modifiedInRequest = true;
  }
Exemplo n.º 18
0
 Block[] unprotectedDelete(UTF8 src) {
   synchronized (rootDir) {
     INode targetNode = rootDir.getNode(src.toString());
     if (targetNode == null) {
       return null;
     } else {
       //
       // Remove the node from the namespace and GC all
       // the blocks underneath the node.
       //
       if (!targetNode.removeNode()) {
         return null;
       } else {
         Vector v = new Vector();
         targetNode.collectSubtreeBlocks(v);
         for (Iterator it = v.iterator(); it.hasNext(); ) {
           Block b = (Block) it.next();
           activeBlocks.remove(b);
         }
         return (Block[]) v.toArray(new Block[v.size()]);
       }
     }
   }
 }
Exemplo n.º 19
0
 boolean unprotectedRenameTo(UTF8 src, UTF8 dst) {
   synchronized (rootDir) {
     INode removedNode = rootDir.getNode(src.toString());
     if (removedNode == null) {
       return false;
     }
     removedNode.removeNode();
     if (isDir(dst)) {
       dst = new UTF8(dst.toString() + "/" + new File(src.toString()).getName());
     }
     INode newNode = rootDir.addNode(dst.toString(), removedNode.blocks);
     if (newNode != null) {
       newNode.children = removedNode.children;
       for (Iterator it = newNode.children.values().iterator(); it.hasNext(); ) {
         INode child = (INode) it.next();
         child.parent = newNode;
       }
       return true;
     } else {
       rootDir.addNode(src.toString(), removedNode.blocks);
       return false;
     }
   }
 }
Exemplo n.º 20
0
 @Override
 public boolean add(@NotNull final INode ln) {
   return add(ln.getKey(), getNotNullValue(ln));
 }
Exemplo n.º 21
0
 @Override
 public void putRight(@NotNull final INode ln) {
   putRight(ln.getKey(), getNotNullValue(ln));
 }
Exemplo n.º 22
0
 INode unprotectedMkdir(String src) {
   synchronized (rootDir) {
     return rootDir.addNode(src, null);
   }
 }
Exemplo n.º 23
0
 /** Check whether the path specifies a directory */
 public boolean isDir(UTF8 src) {
   synchronized (rootDir) {
     INode node = rootDir.getNode(normalizePath(src));
     return node != null && node.isDir();
   }
 }