com.tinkerpop.blueprints.impls.tg.TinkerMetadataReader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of blueprints-core Show documentation
Show all versions of blueprints-core Show documentation
Core interfaces and utilities for Blueprints
The newest version!
package com.tinkerpop.blueprints.impls.tg;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Vertex;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* Reads TinkerGraph metadata from an InputStream.
*
* @author Victor Su
*/
class TinkerMetadataReader {
private final TinkerGraph graph;
/**
* @param graph the graph to populate with the TinkerGraph metadata
*/
public TinkerMetadataReader(TinkerGraph graph) {
this.graph = graph;
}
/**
* Read TinkerGraph metadata from a file.
*
* @param filename the name of the file to read the TinkerGraph metadata from
* @throws IOException thrown if there is an error reading the TinkerGraph metadata
*/
public void load(final String filename) throws IOException {
final FileInputStream fos = new FileInputStream(filename);
load(fos);
fos.close();
}
/**
* Read TinkerGraph metadata from an InputStream.
*
* @param inputStream the InStream to read the TinkerGraph metadata from
* @throws IOException thrown if there is an error reading the TinkerGraph metadata
*/
public void load(final InputStream inputStream) throws IOException {
DataInputStream reader = null;
try {
reader = new DataInputStream(inputStream);
this.graph.currentId = reader.readLong();
readIndices(reader, this.graph);
readVertexKeyIndices(reader, this.graph);
readEdgeKeyIndices(reader, this.graph);
} catch (IOException e) {
throw new RuntimeException("Could not read metadata file");
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
throw new RuntimeException("Could not read metadata file");
}
}
}
/**
* Read TinkerGraph metadata from an InputStream.
*
* @param graph the TinkerGraph to push the metadata to
* @param inputStream the InputStream to read the TinkerGraph metadata from
* @throws IOException thrown if there is an error reading the TinkerGraph metadata
*/
public static void load(final TinkerGraph graph, final InputStream inputStream) throws IOException {
TinkerMetadataReader reader = new TinkerMetadataReader(graph);
reader.load(inputStream);
}
/**
* Read TinkerGraph metadata from a file.
*
* @param graph the TinkerGraph to push the data to
* @param filename the name of the file to read the TinkerGraph metadata from
* @throws IOException thrown if there is an error reading the TinkerGraph metadata
*/
public static void load(final TinkerGraph graph, final String filename) throws IOException {
TinkerMetadataReader reader = new TinkerMetadataReader(graph);
reader.load(filename);
}
private void readIndices(final DataInputStream reader, final TinkerGraph graph) throws IOException {
// Read the number of indices
int indexCount = reader.readInt();
for (int i = 0; i < indexCount; i++) {
// Read the index name
String indexName = reader.readUTF();
// Read the index type
byte indexType = reader.readByte();
if (indexType != 1 && indexType != 2) {
throw new RuntimeException("Unknown index class type");
}
TinkerIndex tinkerIndex = new TinkerIndex(indexName, indexType == 1 ? Vertex.class : Edge.class);
// Read the number of items associated with this index name
int indexItemCount = reader.readInt();
for (int j = 0; j < indexItemCount; j++) {
// Read the item key
String indexItemKey = reader.readUTF();
// Read the number of sub-items associated with this item
int indexValueItemSetCount = reader.readInt();
for (int k = 0; k < indexValueItemSetCount; k++) {
// Read the number of vertices or edges in this sub-item
int setCount = reader.readInt();
for (int l = 0; l < setCount; l++) {
// Read the vertex or edge identifier
if (indexType == 1) {
Vertex v = graph.getVertex(readTypedData(reader));
if (v != null) {
tinkerIndex.put(indexItemKey, v.getProperty(indexItemKey), v);
}
} else if (indexType == 2) {
Edge e = graph.getEdge(readTypedData(reader));
if (e != null) {
tinkerIndex.put(indexItemKey, e.getProperty(indexItemKey), e);
}
}
}
}
}
graph.indices.put(indexName, tinkerIndex);
}
}
private void readVertexKeyIndices(final DataInputStream reader, final TinkerGraph graph) throws IOException {
// Read the number of vertex key indices
int indexCount = reader.readInt();
for (int i = 0; i < indexCount; i++) {
// Read the key index name
String indexName = reader.readUTF();
graph.vertexKeyIndex.createKeyIndex(indexName);
Map
© 2015 - 2024 Weber Informatics LLC | Privacy Policy