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));
 }
Example #4
0
  private Pair<Map<String, String>, /*was it created now?*/ Boolean> getOrCreateIndexConfig(
      Class<? extends PropertyContainer> cls,
      String indexName,
      Map<String, String> suppliedConfig) {
    Pair<Map<String, String>, Boolean> result =
        findIndexConfig(cls, indexName, suppliedConfig, config.getParams());
    boolean createdNow = false;
    if (result.other()) { // Ok, we need to create this config
      synchronized (this) { // Were we the first ones to get here?
        Map<String, String> existing = indexStore.get(cls, indexName);
        if (existing != null) {
          // No, someone else made it before us, cool
          assertConfigMatches(
              getIndexProvider(existing.get(PROVIDER)), indexName, existing, result.first());
          return Pair.of(result.first(), false);
        }

        // We were the first one here, let's create this config
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        try {
          executorService.submit(new IndexCreatorJob(cls, indexName, result.first())).get();
          indexStore.set(cls, indexName, result.first());
          createdNow = true;
        } catch (ExecutionException ex) {
          throw new TransactionFailureException(
              "Index creation failed for " + indexName + ", " + result.first(), ex.getCause());
        } catch (InterruptedException ex) {
          Thread.interrupted();
        } finally {
          executorService.shutdownNow();
        }
      }
    }
    return Pair.of(result.first(), createdNow);
  }
 @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());
 }
Example #6
0
 protected void tryToFinishOffChannel(Channel channel) {
   Pair<RequestContext, AtomicLong> slave;
   slave = unmapSlave(channel);
   if (slave == null) {
     return;
   }
   tryToFinishOffChannel(channel, slave.first());
 }
  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));
 }
Example #10
0
 @Override
 protected void toStringFields(Collection<Pair<String, ?>> fields) {
   super.toStringFields(fields);
   fields.add(Pair.of("startNode", startNode));
   fields.add(Pair.of("endNode", endNode));
   if (hasTypeId()) {
     fields.add(Pair.of("typeId", typeId));
   } else {
     fields.add(Pair.of("type", type));
   }
 }
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);
   }
 }
 @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 #14
0
  private Pair<Map<String, String>, Boolean /*true=needs to be set*/> findIndexConfig(
      Class<? extends PropertyContainer> cls,
      String indexName,
      Map<String, String> suppliedConfig,
      Map<?, ?> dbConfig) {
    // Check stored config (has this index been created previously?)
    Map<String, String> storedConfig = indexStore.get(cls, indexName);
    if (storedConfig != null && suppliedConfig == null) {
      // Fill in "provider" if not already filled in, backwards compatibility issue
      Map<String, String> newConfig =
          injectDefaultProviderIfMissing(cls, indexName, dbConfig, storedConfig);
      if (newConfig != storedConfig) {
        indexStore.set(cls, indexName, newConfig);
      }
      return Pair.of(newConfig, Boolean.FALSE);
    }

    Map<String, String> configToUse = suppliedConfig;

    // Check db config properties for provider
    String provider = null;
    IndexImplementation indexProvider = null;
    if (configToUse == null) {
      provider = getDefaultProvider(indexName, dbConfig);
      configToUse = MapUtil.stringMap(PROVIDER, provider);
    } else {
      provider = configToUse.get(PROVIDER);
      provider = provider == null ? getDefaultProvider(indexName, dbConfig) : provider;
    }
    indexProvider = getIndexProvider(provider);
    configToUse = indexProvider.fillInDefaults(configToUse);
    configToUse = injectDefaultProviderIfMissing(cls, indexName, dbConfig, configToUse);

    // Do they match (stored vs. supplied)?
    if (storedConfig != null) {
      assertConfigMatches(indexProvider, indexName, storedConfig, suppliedConfig);
      // Fill in "provider" if not already filled in, backwards compatibility issue
      Map<String, String> newConfig =
          injectDefaultProviderIfMissing(cls, indexName, dbConfig, storedConfig);
      if (newConfig != storedConfig) {
        indexStore.set(cls, indexName, newConfig);
      }
      configToUse = newConfig;
    }

    boolean needsToBeSet = !indexStore.has(cls, indexName);
    return Pair.of(Collections.unmodifiableMap(configToUse), needsToBeSet);
  }
Example #15
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 #16
0
  /**
   * Managing timeouts is trickier than putting all of them in a long list, like regular message
   * delivery. Timeouts are ordered and can be cancelled, so they need special treatment. Hence a
   * separate method for managing timeouts triggering.
   */
  private Pair<ClusterAction, ClusterState> performNextTimeoutFrom(ClusterInstance instance)
      throws Exception {
    ClusterState newState = snapshot();
    ClusterAction clusterAction = newState.instance(instance.uri().toASCIIString()).popTimeout();
    clusterAction.perform(newState);

    return Pair.of(clusterAction, newState);
  }
Example #17
0
  /** Cypher supports queries with parameters which are submitted as JSON. */
  @Test
  @Documented
  @Title("Use parameters")
  @Graph(
      value = {"I know you"},
      autoIndexNodes = true)
  public void send_queries_with_parameters() throws Exception {
    data.get();
    String script =
        "START x  = node:node_auto_index(name={startName}) MATCH path = (x-[r]-friend) WHERE friend"
            + ".name = {name} RETURN TYPE(r)";
    String response =
        cypherRestCall(script, Status.OK, Pair.of("startName", "I"), Pair.of("name", "you"));

    assertEquals(2, (jsonToMap(response)).size());
    assertTrue(response.contains("know"));
    assertTrue(response.contains("data"));
  }
Example #18
0
  /**
   * Sending a query with syntax errors will give a bad request (HTTP 400) response together with an
   * error message.
   */
  @Test
  @Documented
  @Title("Syntax errors")
  @Graph(
      value = {"I know you"},
      autoIndexNodes = true)
  public void send_queries_with_syntax_errors() throws Exception {
    data.get();
    String script =
        "START x  = node:node_auto_index(name={startName}) MATC path = (x-[r]-friend) WHERE friend"
            + ".name = {name} RETURN TYPE(r)";
    String response =
        cypherRestCall(
            script, Status.BAD_REQUEST, Pair.of("startName", "I"), Pair.of("name", "you"));

    Map<String, Object> output = jsonToMap(response);
    assertTrue(output.containsKey("message"));
    assertTrue(output.containsKey("stacktrace"));
  }
Example #19
0
 @SuppressWarnings("unchecked")
 private SlaveContext slaveContextOf(GraphDatabaseService graphDb) {
   XaDataSourceManager dsManager =
       ((AbstractGraphDatabase) graphDb).getConfig().getTxModule().getXaDataSourceManager();
   List<Pair<String, Long>> txs = new ArrayList<Pair<String, Long>>();
   for (XaDataSource ds : dsManager.getAllRegisteredDataSources()) {
     txs.add(Pair.of(ds.getName(), ds.getLastCommittedTxId()));
   }
   return new SlaveContext(0, 0, txs.toArray(new Pair[0]));
 }
Example #20
0
  /** This example shows what happens if you misspell an identifier. */
  @Test
  @Documented
  @Title("Send queries with errors")
  @Graph(
      value = {"I know you"},
      autoIndexNodes = true)
  public void send_queries_with_errors() throws Exception {
    data.get();
    String script =
        "START x = node:node_auto_index(name={startName}) MATCH path = (x-[r]-friend) WHERE frien"
            + ".name = {name} RETURN type(r)";
    String response =
        cypherRestCall(
            script, Status.BAD_REQUEST, Pair.of("startName", "I"), Pair.of("name", "you"));

    Map<String, Object> responseMap = jsonToMap(response);
    assertEquals(4, responseMap.size());
    assertThat(response, containsString("message"));
    assertThat(((String) responseMap.get("message")), containsString("frien not defined"));
  }
Example #21
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));
    }
  }
 protected Pair<Integer, Node> getNode(Random random, boolean takeOut) {
   synchronized (nodes) {
     while (true) {
       int index = random.nextInt(nodes.length);
       Node node = nodes[index];
       if (null != node) {
         if (takeOut) nodes[index] = null;
         return Pair.of(index, node);
       }
     }
   }
 }
Example #23
0
  /**
   * Create a node with a label and a property using Cypher. See the request for the parameter sent
   * with the query.
   */
  @Test
  @Documented
  @Title("Create a node")
  @Graph
  public void send_query_to_create_a_node() throws Exception {
    data.get();
    String script = "CREATE (n:Person { name : {name} }) RETURN n";
    String response = cypherRestCall(script, Status.OK, Pair.of("name", "Andres"));

    assertTrue(response.contains("name"));
    assertTrue(response.contains("Andres"));
  }
Example #24
0
  @Test
  public void shouldRewriteLogFiles() throws IOException {
    // given
    final IOCursor<LogEntry> cursor = mock(IOCursor.class);
    final LogVersionedStoreChannel writeChannel = mock(LogVersionedStoreChannel.class);
    final LogHeader header = new LogHeader(CURRENT_LOG_VERSION, 1, 42);
    when(fs.listFiles(storeDir, versionedLegacyLogFilesFilter))
        .thenReturn(new File[] {new File(getLegacyLogFilename(1))});
    when(reader.openReadableChannel(new File(getLegacyLogFilename(1))))
        .thenReturn(Pair.of(header, cursor));
    when(writer.openWritableChannel(new File(migrationDir, getLegacyLogFilename(1))))
        .thenReturn(writeChannel);

    // when
    new LegacyLogs(fs, reader, writer).migrateLogs(storeDir, migrationDir);

    // then
    verify(writer, times(1)).writeLogHeader(writeChannel, header);
    verify(writer, times(1)).writeAllLogEntries(writeChannel, cursor);
  }
Example #25
0
  public static Pair<byte[] /*header in the first record*/, byte[] /*all other bytes*/>
      readFullByteArray(
          long startRecord,
          Iterable<DynamicRecord> records,
          AbstractDynamicStore store,
          PropertyType propertyType) {
    long recordToFind = startRecord;
    Map<Long, DynamicRecord> recordsMap = new HashMap<Long, DynamicRecord>();
    for (DynamicRecord record : records) {
      recordsMap.put(record.getId(), record);
    }
    byte[] header = null;
    List<byte[]> byteList = new LinkedList<byte[]>();
    int totalSize = 0;
    while (recordToFind != Record.NO_NEXT_BLOCK.intValue()) {
      DynamicRecord record = recordsMap.get(recordToFind);
      if (record.isLight()) {
        store.makeHeavy(record);
      }

      int offset = 0;
      if (recordToFind == startRecord) { // This is the first one, read out the header separately
        header = propertyType.readDynamicRecordHeader(record.getData());
        offset = header.length;
      }

      byteList.add(record.getData());
      totalSize += (record.getData().length - offset);
      recordToFind = record.getNextBlock();
    }
    byte[] bArray = new byte[totalSize];
    int sourceOffset = header.length;
    int offset = 0;
    for (byte[] currentArray : byteList) {
      System.arraycopy(
          currentArray, sourceOffset, bArray, offset, currentArray.length - sourceOffset);
      offset += (currentArray.length - sourceOffset);
      sourceOffset = 0;
    }
    return Pair.of(header, bArray);
  }
Example #26
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());
   }
 }
 @Override
 protected void doSomething() throws Throwable {
   Pair<Integer, Node> pair = getNode(random, false);
   Node node = pair.other();
   node.removeProperty(randomPropertyKey(random));
 }
Example #28
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());
 }
 private Pair<Integer, Object> extractValue(LegacyPropertyRecord propertyRecord) {
   int keyIndexId = propertyRecord.getKeyIndexId();
   Object value =
       propertyRecord.getType().getValue(propertyRecord, legacyStore.getDynamicRecordFetcher());
   return Pair.of(keyIndexId, value);
 }