Example #1
0
  public void testRun() throws Exception {
    String wsName = "test";
    DefaultCacheManager dcm = new DefaultCacheManager();
    Cache<String, Metadata> metaCache = dcm.getCache("meta");
    Cache<String, byte[]> dataCache = dcm.getCache("data");

    GridFilesystem gfs = new GridFilesystem(dataCache, metaCache);

    BuildContext bcontext =
        DirectoryBuilder.newDirectoryInstance(metaCache, dataCache, metaCache, wsName);
    bcontext.chunkSize(1024 * 1024);
    Directory directory = bcontext.create();
    Central central = CentralConfig.oldFromDir(directory).build();

    central.newIndexer().index(IndexJobs.create("/bleujin", 10));

    central.newSearcher().createRequest("").find().debugPrint();

    OutputStream output = gfs.getOutput("/test.data");
    IOUtil.copyNClose(new StringInputStream("hello bleujin"), output);

    Debug.line(IOUtil.toStringWithClose(gfs.getInput("/test.data")));

    central.newSearcher().createRequest("").find().debugPrint();

    File root = gfs.getFile("/");
    viewFile(root);

    dcm.stop();
  }
  public void save(Map<String, Long> map) {
    FileOutputStream os = null;
    try {

      if (!TMP.mkdirs()) {
        throw new IllegalStateException("Unable to create directory: " + TMP.getAbsolutePath());
      }
      File f = new File(TMP, storeName);
      if (!f.createNewFile()) {
        throw new IllegalStateException("Unable to create temp file: " + f.getAbsolutePath());
      }
      if (!f.canWrite()) {
        throw new IllegalStateException();
      }

      os = new FileOutputStream(f);

      for (Map.Entry<String, Long> e : properties.entrySet()) {
        os.write((append(e)).getBytes("UTF-8"));
      }
      os.flush();
    } catch (Throwable e) {
      Debug.warn(e.getMessage(), e);
    } finally {
      IOUtil.closeQuietly(os);
    }
  }
Example #3
0
  public void testModify() throws Exception {
    createSampleNode();

    VFile dept = entry.resolveFile("node://dept/dept.node");
    IOUtil.copyNClose(
        new StringReader("{greeting:'�ѱ�', age:20}"), dept.getOutputStream(), "UTF-8");

    ReadNode node = session.pathBy("/dept");
    assertEquals("�ѱ�", node.property("greeting").asString());
  }
Example #4
0
  private NettyHttpResponse writeEntity(Response ares, Representation entity) {
    InputStream in = null;
    try {
      setHeader(ares);

      if (!ares.getStatus().isSuccess()) {
        if (ares.getStatus().getCode() == 401) {
          header("WWW-Authenticate", "Basic realm=\"secure\"");
        }
        if (ares.getEntity() == null || ares.getEntity().getStream() == null) return end();
        InputStream istream = ares.getEntity().getStream();
        try {
          return content(IOUtil.toByteArray(istream)).end();
        } finally {
          IOUtil.closeQuietly(istream);
        }
      }

      // TODO: Shouldn't have to do this, but without it we sometimes seem to get two Content-Length
      // headers in the response.
      header("Content-Length", (String) null);
      header("Content-Length", entity.getSize());
      header("Content-Type", entity.getMediaType().getName());

      in = entity.getStream();
      // byte[] data = IOUtil.toByteArray(in) ;
      // content(data) ;

      byte[] data = new byte[1024 * 16];
      int nRead = 0;
      while ((nRead = in.read(data, 0, data.length)) != -1) {
        content(ChannelBuffers.copiedBuffer(data, 0, nRead));
      }

    } catch (Throwable ex) {
      error(ex);
    } finally {
      IOUtil.closeQuietly(in);
      end();
    }
    return this;
  }
Example #5
0
  public void testContentTypeAndSize() throws Exception {
    createSampleNode();
    VFile dept = entry.resolveFile("node://dept/dept.node");

    VFileContent vc = dept.getContent();

    assertEquals("application/json", vc.getContentType());
    assertEquals("utf-8", vc.getEncoding());

    assertEquals(true, vc.getSize() > 0);
    assertEquals(IOUtil.toByteArray(vc.getInputStream()).length, vc.getSize());
  }
Example #6
0
  public <T> T eachNode(ReadChildrenEach<T> readJob) {
    DBCursor cursor = null;
    try {
      cursor = collection.find(filters(), fields(), skip, offset).sort(orderBy).limit(offset);
      if (this.hint != null) cursor.hint(hint);
      else if (this.hintIndexName != null) cursor.hint(hintIndexName);

      session.attribute(Explain.class.getCanonicalName(), Explain.create(cursor.explain()));
      ReadChildrenIterator citer = ReadChildrenIterator.create(session, cursor);
      T result = readJob.handle(citer);
      return result;
    } finally {
      IOUtil.close(cursor);
    }
  }
  private void indexSpeed(Directory dir) throws LockObtainFailedException, IOException {
    Central cram = SimpleCentralConfig.createCentral(dir);
    Indexer indexer = cram.newIndexer();

    final String readString =
        IOUtil.toString(
            new FileReader(
                new File("test/" + StringUtil.replace(this.getClass().getCanonicalName(), ".", "/"))
                    + ".java"));
    final String extString = IOUtil.toString(new FileReader(new File("build.xml")));
    indexer.index(
        new IndexJob<Void>() {

          public Void handle(IndexSession session) throws Exception {
            for (int i = 0; i < 1000; i++) {
              MyDocument doc = MyDocument.testDocument();
              doc.keyword("name", "bleujin");
              doc.text("file", extString);
              session.insertDocument(doc);
            }
            return null;
          }
        });
  }
Example #8
0
 public String toString() {
   StringWriter writer = new StringWriter();
   XmlWriter swriter = null;
   try {
     swriter = getXmlWriter();
     if (swriter == null) {
       writer.append(RowsImpl.class.getCanonicalName() + "@" + hashCode());
     } else {
       swriter.writeXML(this, writer);
       // this.writeXml(writer);
     }
   } catch (Exception ignore) {
     ignore.printStackTrace();
   } finally {
     writer.flush();
     IOUtil.closeQuietly(writer);
   }
   return writer.toString();
 }
Example #9
0
  public void testRead() throws Exception {
    createSampleNode();

    VFile dept = entry.resolveFile("node://dept");
    assertEquals(FileType.FOLDER, dept.getType());
    assertEquals(true, dept.exists());

    String fileName = "node://dept/dept.node";
    VFile deptNode = entry.resolveFile(fileName);
    assertEquals(FileType.FILE, deptNode.getType());

    assertEquals(true, deptNode.exists());
    Reader reader = new InputStreamReader(deptNode.getInputStream(), "UTF-8");
    String result = IOUtil.toString(reader);
    JsonObject jso = JsonParser.fromString(result).getAsJsonObject();
    assertEquals(20, jso.asInt("year"));

    VFile bigboy = entry.resolveFile("node://dept/dev");
    assertEquals(FileType.FOLDER, bigboy.getType());
    // Debug.debug(IOUtil.toString(new InputStreamReader(bigboy.getInputStream(), "UTF-8"))) ;
  }
Example #10
0
  public void testCreate() throws Exception {
    createSampleNode();
    String fileName = "node://dept/dev2";

    VFile dev2 = entry.resolveFile(fileName);
    assertEquals(FileType.FOLDER, dev2.getType());
    assertEquals(FileType.FOLDER, dev2.getName().getType());

    dev2.createFolder();
    assertEquals(true, dev2.exists());

    VFile def2File = entry.resolveFile("node://dept/dev2/dev2.node");
    assertEquals(true, def2File.exists());

    IOUtil.copyNClose(
        new StringReader("{name:'dev2 team', desc:'������', date:" + new Date().getTime() + "}"),
        def2File.getOutputStream(),
        "UTF-8");

    ReadNode found = session.pathBy("/dept/dev2");
    assertEquals("dev2 team", found.property("name").asString());
    assertEquals("������", found.property("desc").asString());
  }
Example #11
0
 @Override
 public void stop() {
   IOUtil.closeQuietly(central);
 }