-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1055aed
commit 35fcec2
Showing
14 changed files
with
334 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
riak = require "riak-js" | ||
utils = require "../objects/utils" | ||
|
||
# Dao | ||
# ----------- | ||
# Base class for all Dao classes. Some common methods should be implemented here. | ||
Dao = exports.Dao = class Dao | ||
|
||
constructor: (@bucket, @log = true) -> | ||
@db = riak.getClient({debug: @log}) | ||
|
||
# Get entity by `id`. Callback takes `error` and `entity` object. | ||
get: (id, callback) => | ||
console.log "getting entity with id =", id, "from bucket =", @bucket if @log | ||
@db.get @bucket, id, (err, attributes, meta) => | ||
if err | ||
callback err | ||
else | ||
callback undefined, @populateEntity(meta, attributes) | ||
|
||
# Save entity. | ||
save: (entity, callback) => | ||
console.log "saving entity with id =", entity.id(), "into bucket =", @bucket if @log | ||
meta = | ||
links: entity.links() | ||
index: entity.index() | ||
contentType: entity.contentType if entity.contentType? | ||
console.log "Content-type of the saved doc", meta.contentType | ||
@db.save @bucket, entity.id(), entity.attributes(), meta, (err, emptyEntity, meta) => | ||
if(err) | ||
callback err | ||
else | ||
console.log "entity was saved in bucket", @bucket, "with id =", entity.id() if @log | ||
callback undefined, entity | ||
|
||
# Remove entity by `id`. | ||
remove: (id, callback) => @db.remove @bucket, id, callback | ||
|
||
# Remove all entities from `bucket`. TODO (anton) rename to removeAll | ||
deleteAll: => | ||
@db.getAll @bucket, (err, objects) => | ||
if(!err) | ||
objects.forEach (object) => | ||
console.log "removing entity from bucket", @bucket, "with id =", object.meta.key if @log | ||
@db.remove @bucket, object.meta.key | ||
|
||
# Checks if such key exists in database. Callback takes 2 parameters: `err` and `exists` boolean parameter | ||
exists: (id, callback) => @db.exists @bucket, id, callback | ||
|
||
# Get all links from entity to some `bucket` under specified `tag`. | ||
walk: (id, spec, callback) => | ||
linkPhases = spec.map (unit) -> | ||
bucket: unit[0] or '_', tag: unit[1] or '_', keep: unit[2]? | ||
@db | ||
.add({bucket: @bucket, key_filters: [["eq", id]]}) | ||
.link(linkPhases) | ||
.map(@_map) | ||
.run(callback) | ||
|
||
links: (id, spec, callback) => @db.links @bucket, id, spec, callback | ||
|
||
# Method for building GitEntity. | ||
populateEntity: (meta, attributes) -> | ||
|
||
getLink: (links, tag) -> utils.getLink links, tag | ||
|
||
# Default map functions | ||
_map: (value) -> | ||
row = value.values[0] | ||
metadata = row.metadata | ||
userMeta = metadata["X-Riak-Meta"] | ||
linksArray = metadata["Links"] | ||
links = ({bucket: link[0], key: link[1], tag: link[2]} for link in linksArray) | ||
entity = | ||
meta: | ||
key: value.key | ||
links: links | ||
contentType: metadata["content-type"] | ||
lastModifiedParsed: Date.parse(metadata["X-Riak-Last-Modified"]) | ||
if entity.meta.contentType == "application/json" | ||
entity.attributes = JSON.parse(row.data) | ||
[entity] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
Dao = require("./dao").Dao | ||
Repo = require("../objects/repo").Repo | ||
|
||
class ReposDao extends Dao | ||
|
||
constructor: (log)-> super "repositories", log | ||
|
||
populateEntity: (meta, attributes) => | ||
author = @getLink meta.links, "author" | ||
forkedFrom = @getLink meta.links, "forked_from" | ||
commit = @getLink meta.links, "commit" | ||
new Repo(attributes.name, author, attributes.type, attributes.public, commit, forkedFrom) | ||
|
||
getNewestRepos: (callback) => | ||
reduceDescending = ( v , args ) -> | ||
v.sort ( (a,b) -> return b['lastModifiedParsed'] - a['lastModifiedParsed'] ) | ||
return v | ||
@db.add(@bucket).map(@_map).reduce(reduceDescending).run (err, docs) => | ||
if(err) | ||
callback err | ||
else | ||
console.log "newest repos", docs.length if @log | ||
repos = (@populateEntity doc.meta, doc.attributes for doc in docs when doc.meta?) | ||
callback undefined, repos | ||
|
||
search: (name, author, callback) => | ||
key_filters = [] | ||
if(name and author) | ||
key_filters = ["and", [["matches", name]], [["starts_with", author]]] | ||
if(name and !author) | ||
key_filters = [["matches", name]] | ||
if(!name and author) | ||
key_filters = [["starts_with", author]] | ||
@db.add({bucket: @bucket, key_filters: key_filters }).map(@_map).run (err, docs) => | ||
if(err) | ||
console.log "cannot find repos", err, "for filters", key_filters if @log | ||
callback undefined, [] | ||
else | ||
console.log "found repos", docs.length, docs if @log | ||
repos = (@populateEntity doc.meta, doc.attributes for doc in docs when doc.meta?) | ||
callback undefined, repos | ||
|
||
exports.newInstance = (log) -> new ReposDao(log) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Tree = require("../objects/tree").Tree | ||
blobsDao = require("./blobs.dao").newInstance() | ||
ObjectsDao = require("./objects.dao").ObjectsDao | ||
|
||
class TreesDao extends ObjectsDao | ||
|
||
constructor: (log)-> super log | ||
|
||
populateEntity: (meta, attributes) => | ||
new Tree(attributes.entries, @getRepository(meta.links), meta.key) | ||
|
||
getBlobs: (treeId, callback) => | ||
@walk treeId, [[@bucket, "blob"]], (err, docs) => | ||
if(err) | ||
console.log "Cannot get blobs for tree", treeId, err if @log? | ||
callback err | ||
else | ||
blobs = (blobsDao.populateEntity doc.meta, doc.attributes for doc in docs when doc? and doc.attributes?) | ||
callback undefined, blobs | ||
|
||
exports.newInstance = (log) -> new TreesDao(log) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
_ = require "underscore" | ||
Dao = require("./dao").Dao | ||
reposDao = require("./repos.dao").newInstance() | ||
blobsDao = require("./blobs.dao").newInstance() | ||
commitsDao = require("./commits.dao").newInstance() | ||
treesDao = require("./trees.dao").newInstance() | ||
User = require("../objects/user").User | ||
|
||
class UsersDao extends Dao | ||
|
||
constructor: (log) -> | ||
super "users", log | ||
|
||
populateEntity: (meta, attributes) => | ||
new User(meta.key, attributes.email, meta.links) | ||
|
||
# return map: | ||
fetchAllRepos: (user, callback) => | ||
@links user, [["repositories", type, 1],["objects", "commit", 1],["objects", "tree", 1],["objects", "blob", 1]], (err, docs) => | ||
if err | ||
callback err | ||
else | ||
repos = [] | ||
blobs = [] | ||
commits = [] | ||
trees = [] | ||
for doc in docs | ||
data = doc.data | ||
if doc.meta.bucket == "repositories" | ||
repos.push reposDao.populateEntity doc.meta, data | ||
else if doc.meta.bucket == "objects" | ||
switch data.type | ||
when "blob" then blobs.push blobsDao.populateEntity(doc.meta, data) | ||
when "tree" then trees.push treesDao.populateEntity(doc.meta, data) | ||
when "commit" then commits.push commitsDao.populateEntity(doc.meta, data) | ||
|
||
callback undefined, {repos: repos, commits: commits, trees: trees, blobs: blobs} | ||
|
||
addRepo: (user, repoId, type, callback) => | ||
@get user, (err, user) => | ||
if err | ||
callback err | ||
else | ||
user.addLink "repositories", repoId, type | ||
@save user, callback | ||
|
||
removeRepo: (user, repoId, callback) => | ||
@get user, (err, user) => | ||
if(err) | ||
callback err | ||
else | ||
user.removeLink "repositories", repoId | ||
@save user, callback | ||
|
||
|
||
watchRepo: (user, repoId, type, callback) => | ||
|
||
unwatchRepo: (user, repoId, callback) => | ||
|
||
|
||
followUser: (user, userToFollow, callback) => | ||
|
||
unfollowUser: (user, userToUnfollow, callback) => | ||
|
||
exports.newInstance = (log) -> new UsersDao(log) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
GitObject = require("./git.object").GitObject | ||
|
||
# Blob | ||
# ----------- | ||
# Class representing Git `blob`. Blob can store data of any type. | ||
# User can additionaly specify content type for the blob by `contentType` property. | ||
Blob = exports.Blob = class Blob extends GitObject | ||
|
||
# Constructor takes `data` and optionally `repo` id and blob's `id`. | ||
constructor: (@data, @repo = null, @_id = null, @contentType) -> | ||
super "blob", @repo, @_id | ||
|
||
content: => @data | ||
|
||
# Dao related methods. | ||
# --------- | ||
|
||
# User can explicitly specify content type of the blob. | ||
#contentType:@contentType | ||
|
||
# Method for getting plain `attributes` of the GitObject. | ||
attributes: => @data | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
_ = require "underscore" | ||
utils = require "./utils" | ||
|
||
# GitEntity | ||
# ----------- | ||
# Base abstract class for each git entity: user, repository, git objects (blobs, commits, trees, tags). | ||
# Each entity has `id`, `attributes` and `links` to other `entities`. | ||
GitEntity = exports.GitEntity = class GitEntity | ||
|
||
# Id of the entity. | ||
id: => @_id | ||
|
||
# Dao related methods. | ||
# --------- | ||
|
||
# Method for getting plain `attributes` of the GitObject. | ||
attributes: -> throw new Error("Should be implemented in every subclass!") | ||
|
||
# Method for getting `index`es of the GitObject. | ||
index: -> {} | ||
|
||
# Method for getting `links` that connect this GitEntity with git objects, users or repositories. | ||
links: -> [] | ||
|
||
# Method for finding appropriate link `key` by `tag` name. | ||
getLink: (tagName) => utils.getLink @links(), tagName | ||
|
||
# Method for finding all appropriate links `key` by `tag` name. | ||
getLinks: (tagName) => utils.getLinks @links(), tagName | ||
|
||
# Method for building link. | ||
buildLink: (bucket, key, tag) -> utils.buildLink bucket, key, tag | ||
|
||
# Property for getting content type for the GitEntity. By default this method has no implementation. | ||
# Should be overriden only in cases where we need explicitly specify content type. | ||
#contentType= null | ||
|
||
# Method that check whether two `GitEntities` are the same. | ||
equals: (gitEntity) => | ||
@id() == gitEntity.id() and _.isEqual(@links(), gitEntity.links()) and _.isEqual(@attributes(), gitEntity.attributes()) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.