public class StaticDataSource extends Object implements javax.inject.Provider<DataAccessObject>, Describable
Layout and builder for DAOs backed by static files. This is used to read CSV files and the like; it controls a composite DAO that reads from files, caches them in memory, and can compute some derived entities from others (e.g. extracting items from the item IDs in a rating data set).
Constructor and Description |
---|
StaticDataSource()
Construct a new data layout object.
|
StaticDataSource(String name)
Construct a new data layout object.
|
Modifier and Type | Method and Description |
---|---|
void |
addDerivedEntity(EntityType derived,
EntityType source,
TypedName<Long> attr)
Add a derived entity to the data source.
|
void |
addIndex(EntityType type,
TypedName<?> attr)
Index entities by an attribute.
|
void |
addSource(Collection<? extends Entity> data)
Add a collection data source.
|
void |
addSource(Collection<? extends Entity> data,
Map<String,Object> metadata)
Add a collection data source.
|
void |
addSource(EntitySource source)
Add an entity source.
|
static StaticDataSource |
csvRatingFile(Path file)
Create a static data source from a CSV rating file.
|
void |
describeTo(DescriptionWriter writer)
Write this class’s description to a sink.
|
static StaticDataSource |
fromJSON(com.fasterxml.jackson.databind.JsonNode object,
URI base)
Parse a JSON description of a data set.
|
static StaticDataSource |
fromJSON(String name,
com.fasterxml.jackson.databind.JsonNode object,
URI base)
Parse a JSON description of a data set.
|
static StaticDataSource |
fromList(Collection<? extends Entity> entities)
Create a static data source from a list/collection.
|
DataAccessObject |
get()
Get the data access object.
|
String |
getName()
Get the name of this source.
|
PreferenceDomain |
getPreferenceDomain() |
List<EntitySource> |
getSources()
Get the list of entity sources.
|
List<EntitySource> |
getSourcesForType(EntityType type)
Get the entity sources producing a particular type.
|
static StaticDataSource |
load(Path path)
Load a static file data set from a file.
|
static StaticDataSource |
load(Path path,
String name)
Load a static file data set from a file.
|
static StaticDataSource |
load(URI uri,
String name)
Load a static file data set from a URI.
|
String |
toString() |
public StaticDataSource()
Construct a new data layout object.
public StaticDataSource(String name)
Construct a new data layout object.
name
- The name of the data source.public String getName()
Get the name of this source.
public void addSource(Collection<? extends Entity> data)
Add a collection data source.
data
- The entities to add.public void addSource(Collection<? extends Entity> data, Map<String,Object> metadata)
Add a collection data source.
data
- The entities to add.metadata
- The entity source metadata.public void addSource(EntitySource source)
Add an entity source.
source
- The entity source to add.public void addIndex(EntityType type, TypedName<?> attr)
Index entities by an attribute.
type
- The entity type to index.attr
- The attribute to index.public void addDerivedEntity(EntityType derived, EntityType source, TypedName<Long> attr)
Add a derived entity to the data source. Derived entities are synthesized from IDs found in attributes of other entities (effectively foreign keys). This allows for things such as extracting the set of users or items from a file of ratings.
The derived entities will not overwrite entities from other sources with the same ID.
derived
- The derived entity type.source
- The type from which to derive it.attr
- The attribute to derive the entity type from.public List<EntitySource> getSources()
Get the list of entity sources.
@Nonnull public List<EntitySource> getSourcesForType(EntityType type)
Get the entity sources producing a particular type.
type
- The entity type to look for.type
.public DataAccessObject get()
Get the data access object. This method is thread-safe.
get
in interface javax.inject.Provider<DataAccessObject>
@Nullable public PreferenceDomain getPreferenceDomain()
public void describeTo(DescriptionWriter writer)
Describable
Write this class’s description to a sink. Anything relevant to this object’s identity should be digested; the idea is that, barring hash collisions, two objects with the same digest are equivalent. Used for things like deterministically generating cache file names.
describeTo
in interface Describable
writer
- The description writer to use.public static StaticDataSource fromJSON(com.fasterxml.jackson.databind.JsonNode object, URI base)
Parse a JSON description of a data set.
object
- The JSON object.public static StaticDataSource fromJSON(String name, com.fasterxml.jackson.databind.JsonNode object, URI base)
Parse a JSON description of a data set.
object
- The JSON object.public static StaticDataSource load(Path path) throws IOException
Load a static file data set from a file.
path
- The file to load.IOException
- If there is an error loading the data source.public static StaticDataSource load(Path path, String name) throws IOException
Load a static file data set from a file.
path
- The file to load.name
- The source name.IOException
- If there is an error loading the data source.public static StaticDataSource load(URI uri, String name) throws IOException
Load a static file data set from a URI.
uri
- The URI to load.name
- The source name.IOException
- If there is an error loading the data source.public static StaticDataSource csvRatingFile(Path file)
Create a static data source from a CSV rating file.
file
- The CSV rating file.public static StaticDataSource fromList(Collection<? extends Entity> entities)
Create a static data source from a list/collection.
entities
- The collection of entities.