Example #1
1
  protected static <T extends Enum<T>> T parseEnum(
      Class<T> enumClass, String name, T defaultValue, Pair<String, T>... additionalPairs) {
    if (name == null) {
      return defaultValue;
    }

    name = name.toLowerCase();
    for (T enumConstant : enumClass.getEnumConstants()) {
      if (enumConstant.name().equalsIgnoreCase(name)) {
        return enumConstant;
      }
    }
    for (T enumConstant : enumClass.getEnumConstants()) {
      if (enumConstant.name().toLowerCase().startsWith(name)) {
        return enumConstant;
      }
    }

    for (Pair<String, T> additional : additionalPairs) {
      if (additional.first().equalsIgnoreCase(name)) {
        return additional.other();
      }
    }
    for (Pair<String, T> additional : additionalPairs) {
      if (additional.first().toLowerCase().startsWith(name)) {
        return additional.other();
      }
    }

    throw new IllegalArgumentException("No '" + name + "' or '" + name + ".*' in " + enumClass);
  }
 public static Object getStringFor(
     AbstractDynamicStore store, long startRecord, Collection<DynamicRecord> dynamicRecords) {
   Pair<byte[], byte[]> source =
       readFullByteArray(startRecord, dynamicRecords, store, PropertyType.STRING);
   // A string doesn't have a header in the data array
   return getStringFor(source.other());
 }
 @Override
 protected void doSomething() throws Throwable {
   Pair<Integer, Node> pair = getNode(random, false);
   Node node = pair.other();
   node.setProperty(
       randomPropertyKey(random), randomLongPropertyValue(random.nextInt(8) + 2, random));
 }
 @Override
 protected void doSomething() throws Throwable {
   Pair<Integer, Node> pair = getNode(random, true);
   int index = pair.first();
   Node node = pair.other();
   node.delete();
   setNode(index, db.createNode());
 }
  private String createParameterString(Pair<String, String>[] params) {
    String paramString = "";
    for (Pair<String, String> param : params) {
      String delimiter = paramString.isEmpty() || paramString.endsWith("{") ? "" : ",";

      paramString += delimiter + "\"" + param.first() + "\":\"" + param.other() + "\"";
    }

    return paramString;
  }
  protected String createParameterString(Pair<String, String>[] params) {
    String paramString = "\"params\": {";
    for (Pair<String, String> param : params) {
      String delimiter = paramString.endsWith("{") ? "" : ",";

      paramString += delimiter + "\"" + param.first() + "\":\"" + param.other() + "\"";
    }
    paramString += "}";

    return paramString;
  }
 private void assertBitPackedArrayGetsCorrectlySerializedAndDeserialized(
     Object array, PropertyType type, int expectedBitsUsedPerItem, int expectedRecordCount) {
   Collection<DynamicRecord> records = storeArray(array);
   Pair<byte[], byte[]> asBytes = loadArray(records);
   assertArrayHeader(asBytes.first(), type, expectedBitsUsedPerItem);
   Bits bits = Bits.bitsFromBytes(asBytes.other());
   int length = Array.getLength(array);
   for (int i = 0; i < length; i++)
     assertEquals(
         ((Number) Array.get(array, i)).longValue(), bits.getLong(expectedBitsUsedPerItem));
 }
 @Test
 public void stringArrayGetsStoredAsUtf8() throws Exception {
   String[] array = new String[] {"first", "second"};
   Collection<DynamicRecord> records = arrayStore.allocateRecords(arrayStore.nextBlockId(), array);
   Pair<byte[], byte[]> loaded = loadArray(records);
   assertStringHeader(loaded.first(), array.length);
   ByteBuffer buffer = ByteBuffer.wrap(loaded.other());
   for (String item : array) {
     byte[] expectedData = UTF8.encode(item);
     assertEquals(expectedData.length, buffer.getInt());
     byte[] loadedItem = new byte[expectedData.length];
     buffer.get(loadedItem);
     assertTrue(Arrays.equals(expectedData, loadedItem));
   }
 }
 @Override
 public void startup(Pair<String, String> config) throws Throwable {
   String storeDir = config.first();
   String backupConfigValue = config.other();
   if (backupConfigValue == null) {
     this.db = new GraphDatabaseFactory().newEmbeddedDatabase(storeDir);
   } else {
     // TODO This is using the old config style - is this class even used anywhere!?
     this.db =
         new GraphDatabaseFactory()
             .newEmbeddedDatabaseBuilder(storeDir)
             .setConfig("enable_online_backup", backupConfigValue)
             .newGraphDatabase();
   }
 }
Example #10
0
 PluginManager(Config serverConfig, Iterable<ServerPlugin> plugins, LogProvider logProvider) {
   Map<String, Pair<ServerPlugin, ServerExtender>> extensions =
       new HashMap<String, Pair<ServerPlugin, ServerExtender>>();
   Log log = logProvider.getLog(getClass());
   for (ServerPlugin plugin : plugins) {
     PluginPointFactory factory = new PluginPointFactoryImpl();
     final ServerExtender extender = new ServerExtender(factory);
     try {
       plugin.loadServerExtender(extender);
     } catch (Exception ex) {
       log.warn("Failed to load plugin [%s]: %s", plugin.toString(), ex.getMessage());
       continue;
     } catch (LinkageError err) {
       log.warn("Failed to load plugin [%s]: %s", plugin.toString(), err.getMessage());
       continue;
     }
     Pair<ServerPlugin, ServerExtender> old =
         extensions.put(plugin.name, Pair.of(plugin, extender));
     if (old != null) {
       log.warn(
           String.format(
               "Extension naming conflict \"%s\" between \"%s\" and \"%s\"",
               plugin.name, old.first().getClass(), plugin.getClass()));
     }
   }
   for (Pair<ServerPlugin, ServerExtender> extension : extensions.values()) {
     log.info(String.format("Loaded server plugin \"%s\"", extension.first().name));
     for (PluginPoint point : extension.other().all()) {
       log.info(
           String.format(
               "  %s.%s: %s",
               point.forType().getSimpleName(), point.name(), point.getDescription()));
     }
     this.extensions.put(extension.first().name, extension.other());
   }
 }
Example #11
0
 @Override
 public void writeComplete(ChannelHandlerContext ctx, WriteCompletionEvent e) throws Exception {
   /*
    * This is here to ensure that channels that have stuff written to them for a long time, long transaction
    * pulls and store copies (mainly the latter), will not timeout and have their transactions rolled back.
    * This is actually not a problem, since both mentioned above have no transaction associated with them
    * but it is more sanitary and leaves less exceptions in the logs
    * Each time a write completes, simply update the corresponding channel's timestamp.
    */
   Pair<RequestContext, AtomicLong> slave = connectedSlaveChannels.get(ctx.getChannel());
   if (slave != null) {
     slave.other().set(clock.currentTimeMillis());
     super.writeComplete(ctx, e);
   }
 }
Example #12
0
 protected ChannelBuffer mapSlave(Channel channel, RequestContext slave) {
   synchronized (connectedSlaveChannels) {
     // Checking for machineId -1 excludes the "empty" slave contexts
     // which some communication points pass in as context.
     if (slave != null && slave.machineId() != RequestContext.EMPTY.machineId()) {
       Pair<RequestContext, AtomicLong> previous = connectedSlaveChannels.get(channel);
       if (previous != null) {
         previous.other().set(System.currentTimeMillis());
       } else {
         connectedSlaveChannels.put(
             channel, Pair.of(slave, new AtomicLong(System.currentTimeMillis())));
       }
     }
   }
   return ChannelBuffers.dynamicBuffer();
 }
Example #13
0
  @POST
  public Response exec(@Context InputFormat input, String data) {
    Map<String, Object> args;
    try {
      args = input.readMap(data);
    } catch (BadInputException e) {
      return output.badRequest(e);
    }

    if (!args.containsKey("command")) {
      return Response.status(Status.BAD_REQUEST)
          .entity("Expected command argument not present.")
          .build();
    }

    ScriptSession scriptSession;
    try {
      scriptSession = getSession(args);
    } catch (IllegalArgumentException e) {
      return output.badRequest(e);
    }

    log.debug(scriptSession.toString());
    try {
      Pair<String, String> result = scriptSession.evaluate((String) args.get("command"));
      List<Representation> list =
          new ArrayList<Representation>(
              asList(
                  ValueRepresentation.string(result.first()),
                  ValueRepresentation.string(result.other())));

      return output.ok(new ListRepresentation(RepresentationType.STRING, list));
    } catch (Exception e) {
      List<Representation> list =
          new ArrayList<Representation>(
              asList(
                  ValueRepresentation.string(e.getClass() + " : " + e.getMessage() + "\n"),
                  ValueRepresentation.string(null)));
      return output.ok(new ListRepresentation(RepresentationType.STRING, list));
    }
  }
 @Override
 protected void doSomething() throws Throwable {
   Pair<Integer, Node> pair = getNode(random, false);
   Node node = pair.other();
   node.removeProperty(randomPropertyKey(random));
 }
Example #15
0
 public String getStringFor(Collection<DynamicRecord> dynamicRecords) {
   Pair<byte[], byte[]> source =
       stringPropertyStore.readFullByteArray(dynamicRecords, PropertyType.STRING);
   // A string doesn't have a header in the data array
   return decodeString(source.other());
 }