コード例 #1
0
  /**
   * Loads a resource from storage; the default implementation makes the assumption that the data is
   * stored as UTF-8 encoded text, such as JSON. This method should be overridden if that assumption
   * is invalid.
   */
  public Object load(String resourceId) throws IOException {
    String storedResourceId = getStoredResourceId(resourceId);

    log.info("Reading {} using {}", storedResourceId, storageIO.getInfo());

    InputStream inputStream = storageIO.openInputStream(storedResourceId);
    if (inputStream == null) {
      return null;
    }
    Object parsed = null;
    InputStreamReader reader = null;
    try {
      reader = new InputStreamReader(inputStream, UTF_8);
      parsed = parseText(reader, resourceId);
    } finally {
      if (reader != null) {
        try {
          reader.close();
        } catch (Exception ignore) {
        }
      }
    }

    String objectType = (parsed != null) ? parsed.getClass().getSimpleName() : "null";
    log.info(
        String.format(
            Locale.ROOT,
            "Loaded %s at path %s using %s",
            objectType,
            storedResourceId,
            storageIO.getInfo()));

    return parsed;
  }
コード例 #2
0
  /**
   * Creates a new StorageIO instance for a Solr core, taking into account whether the core is
   * running in cloud mode as well as initArgs.
   */
  public static StorageIO newStorageIO(
      String collection, SolrResourceLoader resourceLoader, NamedList<String> initArgs) {
    StorageIO storageIO = null;

    SolrZkClient zkClient = null;
    String zkConfigName = null;
    if (resourceLoader instanceof ZkSolrResourceLoader) {
      zkClient = ((ZkSolrResourceLoader) resourceLoader).getZkController().getZkClient();
      try {
        zkConfigName =
            ((ZkSolrResourceLoader) resourceLoader)
                .getZkController()
                .getZkStateReader()
                .readConfigName(collection);
      } catch (Exception e) {
        log.error(
            "Failed to get config name for collection {} due to: {}", collection, e.toString());
      }
      if (zkConfigName == null) {
        throw new SolrException(
            ErrorCode.SERVER_ERROR, "Could not find config name for collection:" + collection);
      }
    }

    if (initArgs.get(STORAGE_IO_CLASS_INIT_ARG) != null) {
      storageIO =
          resourceLoader.newInstance(initArgs.get(STORAGE_IO_CLASS_INIT_ARG), StorageIO.class);
    } else {
      if (zkClient != null) {
        String znodeBase = "/configs/" + zkConfigName;
        log.info(
            "Setting up ZooKeeper-based storage for the RestManager with znodeBase: " + znodeBase);
        storageIO = new ManagedResourceStorage.ZooKeeperStorageIO(zkClient, znodeBase);
      } else {
        storageIO = new FileStorageIO();
      }
    }

    if (storageIO instanceof FileStorageIO) {
      // using local fs, if storageDir is not set in the solrconfig.xml, assume the configDir for
      // the core
      if (initArgs.get(STORAGE_DIR_INIT_ARG) == null) {
        initArgs.add(STORAGE_DIR_INIT_ARG, resourceLoader.getConfigDir());
      }
    }

    storageIO.configure(resourceLoader, initArgs);

    return storageIO;
  }
コード例 #3
0
 /** Removes the given resourceId's persisted representation. */
 public boolean delete(String resourceId) throws IOException {
   return storageIO.delete(getStoredResourceId(resourceId));
 }