2017 © Pedro Peláez
 

library ncr

Node Content Repository for php.

image

gdbots/ncr

Node Content Repository for php.

  • Wednesday, June 27, 2018
  • by gdbrown
  • Repository
  • 8 Watchers
  • 0 Stars
  • 4,659 Installations
  • PHP
  • 13 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 19 Versions
  • 79 % Grown

The README.md

ncr-php

Node Content Repository for php. Using this library assumes that you've already created and compiled your own pbj classes using the Pbjc and are making use of the "gdbots:ncr:mixin:*" mixins from gdbots/schemas., (*1)

If your project is using Symfony use the gdbots/ncr-bundle-php to simplify the integration., (*2)

Nodes and Edges

A node or vertex is a noun/entity in your system. An article, tweet, video, person, place, order, product, etc. The edges are the relationships between those things like "friends", "tagged to", "married to", "published by", etc., (*3)

This library doesn't provide you with a graph database implementation. It's concerned with persisting/retrieving nodes and edges. Graph traversal would still need to be provided by another library. It is recommended that data be replicated or projected out of the Ncr (or layered on top like GraphQL) into something suited for that purpose (e.g. Neo4j, Titan ElasticSearch)., (*4)

NodeRef

A NodeRef is a qualified identifier to a node/vertex. It is less verbose than a MessageRef as it is implied that node labels must be unique within a given vendor namespace and therefore can be represented in a more compact manner., (*5)

NodeRef Format: vendor:label:id The "vendor:label" portion is a SchemaQName, (*6)

Examples:, (*7)

acme:article:41e4532f-2f58-4b9d-afc8-e9c2cbcb4aba twitter:tweet:789234931599835136 youtube:video:EG0wQRsXLi4, (*8)

Nodes do not actually have a node_ref field, they have an _id field. The NodeRef is derived by taking the SchemaQName of the node's schema along with its _id. The NodeRef is an immutable value object which is used in various places without needing to actually have the node instance., (*9)

Ncr

The Ncr is the service responsible for node persistence. It is intentionally limited to basic key/value storage operations (e.g. put/get/find by index) to ensure the underlying implementation can be swapped out with little effort or decorated easily (caching layers for example)., (*10)

Available repository implementations:, (*11)

  • DynamoDb
  • Psr6 (gives you Redis, File, Memcached, Doctrine, etc.)
  • InMemory

Review the Gdbots\Ncr\Ncr interface for reference on the available methods., (*12)

Ncr::findNodeRefs

The Ncr is a simple key/value store which means querying is limited to the id of the item or a secondary index. An example of a secondary index would be the email or username of a user, the slug of an article or the isbn of a book., (*13)

An IndexQuery is used to findNodeRefs that match a query against a secondary index., (*14)

An example of using a IndexQuery:, (*15)

$query = IndexQueryBuilder::create(SchemaQName::fromString('acme:user'), 'email', 'homer@simpson.com')
    ->setCount(1)
    ->build();
$result = $this->ncr->findNodeRefs($query);
if (!$result->count()) {
    throw new NodeNotFound('Unable to find homer.');
}

$node = $this->ncr->getNode($result->getNodeRefs()[0]);

Not all storage engines can enforce uniqueness on a secondary index so the interface also cannot make that assumption. Because of this the findNodeRefs may return more than one value. It is up to your application logic to deal with that., (*16)

Ncr::pipeNodes

Getting data out of the Ncr should be dead simple, it's just json after all. Use the pipeNodes or pipeNodeRefs methods to export data. The gdbots/ncr-bundle-php provides console commands that make use of this to export and reindex nodes., (*17)

Exporting nodes using pipeNodes:, (*18)

foreach ($ncr->pipeNodes(SchemaQName::fromString('acme:article')) as $node) {
    echo json_encode($node) . PHP_EOL;
}

NcrCache

NcrCache is a first level cache which is ONLY seen and used by the current request. It is used to cache all nodes returned from get node request(s). This cache is used during Pbjx request processing or if the Ncr is running in the current process and is using the MemoizingNcr., (*19)

This cache should not be used when asking for a consistent result., (*20)

NcrCache is NOT an identity map and the Ncr is NOT an ORM. In some cases you may get the same exact object but it's not a guarantee so don't do something like this:, (*21)

$nodeRef = NodeRef::fromString('acme:article:123');
$cache->getNode($nodeRef) !== $cache->getNode($nodeRef);

If you need to check equality, use the message interface:, (*22)

$node1 = $cache->getNode($nodeRef);
$node2 = $cache->getNode($nodeRef);
$node->equals($node2); // returns true if their data is the same

NcrLazyLoader

NcrCache and other request interceptors make use of this service to batch load nodes only if they are requested. An example of this is when loading an article you may want to fetch the author or related items, but not always. Rather than force the logic to exist in the loading of an article, something else can manage that., (*23)

Lazy loading is generally application specific so this library provides some tools to make is easier., (*24)

Example lazy loading:, (*25)

public function onSearchNodesResponse(ResponseCreatedEvent $pbjxEvent): void
{
    $response = $pbjxEvent->getResponse();
    if (!$response->has('nodes')) {
        return;
    }

    // for all nodes in this search response, mark the creator
    // and updater for lazy load.  if they get requested at some point
    // in the current request, it will be batched for optimal performance
    $this->lazyLoader->addEmbeddedNodeRefs($response->get('nodes'), [
        'creator_ref' => 'acme:user',
        'updater_ref' => 'acme:user',
    ]);
}

NcrSearch

The Ncr provides the reliable storage and retrieval of Nodes. NcrSearch is in most cases a separate storage provider. For example, DynamoDb for Ncr and ElasticSearch for NcrSearch. In fact, the only implementation we have right now is ElasticSearch., (*26)

When using the gdbots/ncr-bundle-php you can enable the indexing with a simple configuration option. The bundle also provides a reindex console command., (*27)

Searching nodes is generally done in a request handler. Here is an example of searching nodes:, (*28)

public function handleRequest(Message $request, Pbjx $pbjx): Message
{
    $parsedQuery = ParsedQuery::fromArray(json_decode($request->get('parsed_query_json', '{}'), true));

    $response = SearchUsersResponseV1::create();
    $this->ncrSearch->searchNodes(
        $request,
        $parsedQuery,
        $response,
        [SchemaQName::fromString('acme:user')]
    );

    return $response;
}

The Versions

27/06 2018

dev-master

9999999-dev

Node Content Repository for php.

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

27/06 2018

v0.3.5

0.3.5.0

Node Content Repository for php.

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

30/05 2018

v0.3.4

0.3.4.0

Node Content Repository for php.

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

30/05 2018

dev-bug-in-memory-ncr

dev-bug-in-memory-ncr

Node Content Repository for php.

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

30/05 2018

v0.3.3

0.3.3.0

Node Content Repository for php.

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

08/05 2018

v0.3.2

0.3.2.0

Node Content Repository for php.

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

14/04 2018

v0.3.1

0.3.1.0

Node Content Repository for php.

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

05/04 2018

v0.3.0

0.3.0.0

Node Content Repository for php.

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

27/03 2018

v0.2.4

0.2.4.0

Node Content Repository for php.

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

27/03 2018

dev-abstracts

dev-abstracts

Node Content Repository for php.

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

15/02 2018

v0.2.3

0.2.3.0

Node Content Repository for php.

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

15/02 2018

dev-node-etag-enricher

dev-node-etag-enricher

Node Content Repository for php.

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

30/01 2018

v0.2.2

0.2.2.0

Node Content Repository for php.

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

30/01 2018

dev-normalizer

dev-normalizer

Node Content Repository for php.

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

09/01 2018

v0.2.1

0.2.1.0

Node Content Repository for php.

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

08/12 2017

v0.2.0

0.2.0.0

Node Content Repository for php.

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

26/11 2017

v0.1.2

0.1.2.0

Node Content Repository for php.

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

26/09 2017

v0.1.1

0.1.1.0

Node Content Repository for php.

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

27/03 2017

v0.1.0

0.1.0.0

Node Content Repository for php.

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires