@Test public void testPathToAndFromTuple() throws Exception { KeySpace root = new KeySpace( new DirectoryLayerDirectory("production", "production") .addSubdirectory(new KeySpaceDirectory("userid", KeyType.LONG) .addSubdirectory(new DirectoryLayerDirectory("application") .addSubdirectory(new KeySpaceDirectory("dataStore", KeyType.NULL)) .addSubdirectory(new DirectoryLayerDirectory("metadataStore", "S")))), new DirectoryLayerDirectory("test", "test") .addSubdirectory(new KeySpaceDirectory("userid", KeyType.LONG) .addSubdirectory(new DirectoryLayerDirectory("application") .addSubdirectory(new KeySpaceDirectory("dataStore", KeyType.NULL)) .addSubdirectory(new DirectoryLayerDirectory("metadataStore", "S"))))); final KeySpacePath path1 = root.path("production") .add("userid", 123456789L) .add("application", "com.mybiz.application1") .add("dataStore"); final KeySpacePath path2 = root.path("test") .add("userid", 987654321L) .add("application", "com.mybiz.application2") .add("metadataStore"); final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase(); final Tuple path1Tuple; final Tuple path2Tuple; try (FDBRecordContext context = database.openContext()) { path1Tuple = path1.toTuple(context); path2Tuple = path2.toTuple(context); context.commit(); } final Tuple path1ExpectedTuple; final Tuple path2ExpectedTuple; try (FDBRecordContext context = database.openContext()) { List<Long> entries = resolveBatch(context, "production", "test", "com.mybiz.application1", "com.mybiz.application2", "S"); path1ExpectedTuple = Tuple.from(entries.get(0), 123456789L, entries.get(2), null); path2ExpectedTuple = Tuple.from(entries.get(1), 987654321L, entries.get(3), entries.get(4)); assertEquals(path1ExpectedTuple, path1Tuple); assertEquals(path2ExpectedTuple, path2Tuple); List<ResolvedKeySpacePath> revPath1 = root.resolveFromKey(context, path1ExpectedTuple).flatten(); assertEquals("production", revPath1.get(0).getDirectoryName()); assertEquals(entries.get(0), revPath1.get(0).getResolvedValue()); assertEquals("userid", revPath1.get(1).getDirectoryName()); assertEquals(123456789L, revPath1.get(1).getResolvedValue()); assertEquals("application", revPath1.get(2).getDirectoryName()); assertEquals(entries.get(2), revPath1.get(2).getResolvedValue()); assertEquals("dataStore", revPath1.get(3).getDirectoryName()); assertEquals(null, revPath1.get(3).getResolvedValue()); Tuple extendedPath2 = path2ExpectedTuple.add(10L); List<ResolvedKeySpacePath> revPath2 = root.resolveFromKey(context, extendedPath2).flatten(); assertEquals("test", revPath2.get(0).getDirectoryName()); assertEquals("userid", revPath2.get(1).getDirectoryName()); assertEquals("application", revPath2.get(2).getDirectoryName()); assertEquals("metadataStore", revPath2.get(3).getDirectoryName()); assertEquals(Tuple.from(10L), revPath2.get(3).getRemainder()); } }
@Nonnull public KeySpaceDirectory addSubdirectory(@Nonnull KeySpaceDirectory subdirectory) { for (KeySpaceDirectory existingSubdir : subdirsByName.values()) { if (existingSubdir.getName().equals(subdirectory.getName())) { throw new RecordCoreArgumentException("Subdirectory already exists", LogMessageKeys.PARENT_DIR, getName(), LogMessageKeys.DIR_NAME, subdirectory.getName()); } if (existingSubdir.getKeyType() == subdirectory.getKeyType()) { if (existingSubdir.getValue() == ANY_VALUE || subdirectory.getValue() == ANY_VALUE) { throw new RecordCoreArgumentException("Cannot add directory due to overlapping type", LogMessageKeys.PARENT_DIR, getName(), LogMessageKeys.DIR_NAME, existingSubdir.getName(), LogMessageKeys.DIR_TYPE, subdirectory.getKeyType()); } if (Objects.equals(subdirectory.getValue(), existingSubdir.getValue())) { throw new RecordCoreArgumentException("Cannot add directory due to overlapping constant value", LogMessageKeys.PARENT_DIR, getName(), LogMessageKeys.DIR_NAME, existingSubdir.getName(), LogMessageKeys.DIR_TYPE, existingSubdir.getKeyType(), LogMessageKeys.DIR_VALUE, existingSubdir.getValue()); } if (!existingSubdir.isCompatible(this, subdirectory) || !subdirectory.isCompatible(this, existingSubdir)) { throw new RecordCoreArgumentException("Cannot add directory due to incompatibility with existing subdirectory", LogMessageKeys.PARENT_DIR, getName(), LogMessageKeys.DIR_TYPE, existingSubdir.getKeyType(), LogMessageKeys.DIR_VALUE, existingSubdir.getValue()); } } } subdirectory.setParent(this); subdirsByName.put(subdirectory.getName(), subdirectory); subdirs.add(subdirectory); return this; }
KeySpaceDirectory { @Nonnull public KeySpaceDirectory addSubdirectory(@Nonnull KeySpaceDirectory subdirectory) { for (KeySpaceDirectory existingSubdir : subdirsByName.values()) { if (existingSubdir.getName().equals(subdirectory.getName())) { throw new RecordCoreArgumentException("Subdirectory already exists", LogMessageKeys.PARENT_DIR, getName(), LogMessageKeys.DIR_NAME, subdirectory.getName()); } if (existingSubdir.getKeyType() == subdirectory.getKeyType()) { if (existingSubdir.getValue() == ANY_VALUE || subdirectory.getValue() == ANY_VALUE) { throw new RecordCoreArgumentException("Cannot add directory due to overlapping type", LogMessageKeys.PARENT_DIR, getName(), LogMessageKeys.DIR_NAME, existingSubdir.getName(), LogMessageKeys.DIR_TYPE, subdirectory.getKeyType()); } if (Objects.equals(subdirectory.getValue(), existingSubdir.getValue())) { throw new RecordCoreArgumentException("Cannot add directory due to overlapping constant value", LogMessageKeys.PARENT_DIR, getName(), LogMessageKeys.DIR_NAME, existingSubdir.getName(), LogMessageKeys.DIR_TYPE, existingSubdir.getKeyType(), LogMessageKeys.DIR_VALUE, existingSubdir.getValue()); } if (!existingSubdir.isCompatible(this, subdirectory) || !subdirectory.isCompatible(this, existingSubdir)) { throw new RecordCoreArgumentException("Cannot add directory due to incompatibility with existing subdirectory", LogMessageKeys.PARENT_DIR, getName(), LogMessageKeys.DIR_TYPE, existingSubdir.getKeyType(), LogMessageKeys.DIR_VALUE, existingSubdir.getValue()); } } } subdirectory.setParent(this); subdirsByName.put(subdirectory.getName(), subdirectory); subdirs.add(subdirectory); return this; } }
KeySpaceDirectory { @Nonnull public KeySpaceDirectory addSubdirectory(@Nonnull KeySpaceDirectory subdirectory) { for (KeySpaceDirectory existingSubdir : subdirsByName.values()) { if (existingSubdir.getName().equals(subdirectory.getName())) { throw new RecordCoreArgumentException("Subdirectory already exists", LogMessageKeys.PARENT_DIR, getName(), LogMessageKeys.DIR_NAME, subdirectory.getName()); } if (existingSubdir.getKeyType() == subdirectory.getKeyType()) { if (existingSubdir.getValue() == ANY_VALUE || subdirectory.getValue() == ANY_VALUE) { throw new RecordCoreArgumentException("Cannot add directory due to overlapping type", LogMessageKeys.PARENT_DIR, getName(), LogMessageKeys.DIR_NAME, existingSubdir.getName(), LogMessageKeys.DIR_TYPE, subdirectory.getKeyType()); } if (Objects.equals(subdirectory.getValue(), existingSubdir.getValue())) { throw new RecordCoreArgumentException("Cannot add directory due to overlapping constant value", LogMessageKeys.PARENT_DIR, getName(), LogMessageKeys.DIR_NAME, existingSubdir.getName(), LogMessageKeys.DIR_TYPE, existingSubdir.getKeyType(), LogMessageKeys.DIR_VALUE, existingSubdir.getValue()); } if (!existingSubdir.isCompatible(this, subdirectory) || !subdirectory.isCompatible(this, existingSubdir)) { throw new RecordCoreArgumentException("Cannot add directory due to incompatibility with existing subdirectory", LogMessageKeys.PARENT_DIR, getName(), LogMessageKeys.DIR_TYPE, existingSubdir.getKeyType(), LogMessageKeys.DIR_VALUE, existingSubdir.getValue()); } } } subdirectory.setParent(this); subdirsByName.put(subdirectory.getName(), subdirectory); subdirs.add(subdirectory); return this; } KeySpaceDirectory(@Nonnull String name, @Nonnull KeyType keyType, @Nullable Object value,
                             @Nullable Function<KeySpacePath, KeySpacePath> wrapper);  KeySpaceDirectory(@Nonnull String name, @Nonnull KeyType keyType, @Nullable Function<KeySpacePath, KeySpacePath> wrapper);  KeySpaceDirectory(@Nonnull String name, @Nonnull KeyType keyType);  KeySpaceDirectory(@Nonnull String name, @Nonnull KeyType keyType, @Nullable Object value); }
KeySpaceDirectory { @Nonnull public KeySpaceDirectory addSubdirectory(@Nonnull KeySpaceDirectory subdirectory) { for (KeySpaceDirectory existingSubdir : subdirsByName.values()) { if (existingSubdir.getName().equals(subdirectory.getName())) { throw new RecordCoreArgumentException("Subdirectory already exists", LogMessageKeys.PARENT_DIR, getName(), LogMessageKeys.DIR_NAME, subdirectory.getName()); } if (existingSubdir.getKeyType() == subdirectory.getKeyType()) { if (existingSubdir.getValue() == ANY_VALUE || subdirectory.getValue() == ANY_VALUE) { throw new RecordCoreArgumentException("Cannot add directory due to overlapping type", LogMessageKeys.PARENT_DIR, getName(), LogMessageKeys.DIR_NAME, existingSubdir.getName(), LogMessageKeys.DIR_TYPE, subdirectory.getKeyType()); } if (Objects.equals(subdirectory.getValue(), existingSubdir.getValue())) { throw new RecordCoreArgumentException("Cannot add directory due to overlapping constant value", LogMessageKeys.PARENT_DIR, getName(), LogMessageKeys.DIR_NAME, existingSubdir.getName(), LogMessageKeys.DIR_TYPE, existingSubdir.getKeyType(), LogMessageKeys.DIR_VALUE, existingSubdir.getValue()); } if (!existingSubdir.isCompatible(this, subdirectory) || !subdirectory.isCompatible(this, existingSubdir)) { throw new RecordCoreArgumentException("Cannot add directory due to incompatibility with existing subdirectory", LogMessageKeys.PARENT_DIR, getName(), LogMessageKeys.DIR_TYPE, existingSubdir.getKeyType(), LogMessageKeys.DIR_VALUE, existingSubdir.getValue()); } } } subdirectory.setParent(this); subdirsByName.put(subdirectory.getName(), subdirectory); subdirs.add(subdirectory); return this; } KeySpaceDirectory(@Nonnull String name, @Nonnull KeyType keyType, @Nullable Object value,
                             @Nullable Function<KeySpacePath, KeySpacePath> wrapper);  KeySpaceDirectory(@Nonnull String name, @Nonnull KeyType keyType, @Nullable Function<KeySpacePath, KeySpacePath> wrapper);  KeySpaceDirectory(@Nonnull String name, @Nonnull KeyType keyType);  KeySpaceDirectory(@Nonnull String name, @Nonnull KeyType keyType, @Nullable Object value); int depth(); @Nonnull CompletableFuture<ResolvedKeySpacePath> findChildForValue(@Nonnull FDBRecordContext context,
                                                                     @Nullable ResolvedKeySpacePath parent,
                                                                     @Nullable Object value); @Nonnull KeySpaceDirectory addSubdirectory(@Nonnull KeySpaceDirectory subdirectory); boolean isLeaf(); @Nonnull KeySpaceDirectory getSubdirectory(@Nonnull String name); @Nonnull List<KeySpaceDirectory> getSubdirectories(); @Nullable KeySpaceDirectory getParent(); @Nonnull String getName(); @Nonnull String getNameInTree(); @Nonnull KeyType getKeyType(); @Nullable Object getValue(); String toPathString(); @Override String toString(); void toTree(Writer out); }
KeySpaceDirectory { @Nonnull public KeySpaceDirectory addSubdirectory(@Nonnull KeySpaceDirectory subdirectory) { for (KeySpaceDirectory existingSubdir : subdirsByName.values()) { if (existingSubdir.getName().equals(subdirectory.getName())) { throw new RecordCoreArgumentException("Subdirectory already exists", LogMessageKeys.PARENT_DIR, getName(), LogMessageKeys.DIR_NAME, subdirectory.getName()); } if (existingSubdir.getKeyType() == subdirectory.getKeyType()) { if (existingSubdir.getValue() == ANY_VALUE || subdirectory.getValue() == ANY_VALUE) { throw new RecordCoreArgumentException("Cannot add directory due to overlapping type", LogMessageKeys.PARENT_DIR, getName(), LogMessageKeys.DIR_NAME, existingSubdir.getName(), LogMessageKeys.DIR_TYPE, subdirectory.getKeyType()); } if (Objects.equals(subdirectory.getValue(), existingSubdir.getValue())) { throw new RecordCoreArgumentException("Cannot add directory due to overlapping constant value", LogMessageKeys.PARENT_DIR, getName(), LogMessageKeys.DIR_NAME, existingSubdir.getName(), LogMessageKeys.DIR_TYPE, existingSubdir.getKeyType(), LogMessageKeys.DIR_VALUE, existingSubdir.getValue()); } if (!existingSubdir.isCompatible(this, subdirectory) || !subdirectory.isCompatible(this, existingSubdir)) { throw new RecordCoreArgumentException("Cannot add directory due to incompatibility with existing subdirectory", LogMessageKeys.PARENT_DIR, getName(), LogMessageKeys.DIR_TYPE, existingSubdir.getKeyType(), LogMessageKeys.DIR_VALUE, existingSubdir.getValue()); } } } subdirectory.setParent(this); subdirsByName.put(subdirectory.getName(), subdirectory); subdirs.add(subdirectory); return this; } KeySpaceDirectory(@Nonnull String name, @Nonnull KeyType keyType, @Nullable Object value,
                             @Nullable Function<KeySpacePath, KeySpacePath> wrapper);  KeySpaceDirectory(@Nonnull String name, @Nonnull KeyType keyType, @Nullable Function<KeySpacePath, KeySpacePath> wrapper);  KeySpaceDirectory(@Nonnull String name, @Nonnull KeyType keyType);  KeySpaceDirectory(@Nonnull String name, @Nonnull KeyType keyType, @Nullable Object value); int depth(); @Nonnull CompletableFuture<ResolvedKeySpacePath> findChildForValue(@Nonnull FDBRecordContext context,
                                                                     @Nullable ResolvedKeySpacePath parent,
                                                                     @Nullable Object value); @Nonnull KeySpaceDirectory addSubdirectory(@Nonnull KeySpaceDirectory subdirectory); boolean isLeaf(); @Nonnull KeySpaceDirectory getSubdirectory(@Nonnull String name); @Nonnull List<KeySpaceDirectory> getSubdirectories(); @Nullable KeySpaceDirectory getParent(); @Nonnull String getName(); @Nonnull String getNameInTree(); @Nonnull KeyType getKeyType(); @Nullable Object getValue(); String toPathString(); @Override String toString(); void toTree(Writer out); static final Object ANY_VALUE; }