Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
podviaznikov committed Nov 13, 2011
1 parent 1055aed commit 35fcec2
Show file tree
Hide file tree
Showing 14 changed files with 334 additions and 17 deletions.
6 changes: 3 additions & 3 deletions lib/dao/dao.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Dao = exports.Dao = class Dao
get: (id, callback) =>
console.log "getting entity with id =", id, "from bucket =", @bucket if @log
@db.get @bucket, id, (err, attributes, meta) =>
if(err)
if err
callback err
else
callback undefined, @populateEntity(meta, attributes)
Expand All @@ -27,7 +27,7 @@ Dao = exports.Dao = class Dao
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)
if err
callback err
else
console.log "entity was saved in bucket", @bucket, "with id =", entity.id() if @log
Expand All @@ -39,7 +39,7 @@ Dao = exports.Dao = class Dao
# Remove all entities from `bucket`. TODO (anton) rename to removeAll
deleteAll: =>
@db.getAll @bucket, (err, objects) =>
if(!err)
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
Expand Down
83 changes: 83 additions & 0 deletions lib/dao/dao.coffee~
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]

4 changes: 1 addition & 3 deletions lib/dao/repos.dao.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ReposDao extends Dao
v.sort ( (a,b) -> return b['lastModifiedParsed'] - a['lastModifiedParsed'] )
return v
@db.add(@bucket).map(@_map).reduce(reduceDescending).run (err, docs) =>
if(err)
if err
callback err
else
console.log "newest repos", docs.length if @log
Expand All @@ -31,7 +31,6 @@ class ReposDao extends Dao
key_filters = [["matches", name]]
if(!name and author)
key_filters = [["starts_with", author]]
console.log "KF", key_filters
@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
Expand All @@ -42,4 +41,3 @@ class ReposDao extends Dao
callback undefined, repos

exports.newInstance = (log) -> new ReposDao(log)

43 changes: 43 additions & 0 deletions lib/dao/repos.dao.coffee~
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)
2 changes: 1 addition & 1 deletion lib/dao/trees.dao.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class TreesDao extends ObjectsDao

getBlobs: (treeId, callback) =>
@walk treeId, [[@bucket, "blob"]], (err, docs) =>
if(err)
if err
console.log "Cannot get blobs for tree", treeId, err if @log?
callback err
else
Expand Down
22 changes: 22 additions & 0 deletions lib/dao/trees.dao.coffee~
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)

6 changes: 3 additions & 3 deletions lib/dao/users.dao.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class UsersDao extends Dao
# return map:
fetchAllRepos: (user, callback) =>
@links user, [["repositories", type, 1],["objects", "commit", 1],["objects", "tree", 1],["objects", "blob", 1]], (err, docs) =>
if(err)
if err
callback err
else
repos = []
Expand All @@ -38,15 +38,15 @@ class UsersDao extends Dao

addRepo: (user, repoId, type, callback) =>
@get user, (err, user) =>
if(err)
if err
callback err
else
user.addLink "repositories", repoId, type
@save user, callback

removeRepo: (user, repoId, callback) =>
@get user, (err, user) =>
if(err)
if err
callback err
else
user.removeLink "repositories", repoId
Expand Down
66 changes: 66 additions & 0 deletions lib/dao/users.dao.coffee~
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)

3 changes: 0 additions & 3 deletions lib/objects/blob.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ Blob = exports.Blob = class Blob extends GitObject
# Dao related methods.
# ---------

# User can explicitly specify content type of the blob.
#contentType:@contentType

# Method for getting plain `attributes` of the GitObject.
attributes: => @data

23 changes: 23 additions & 0 deletions lib/objects/blob.coffee~
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

4 changes: 0 additions & 4 deletions lib/objects/git.entity.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ GitEntity = exports.GitEntity = class GitEntity
# 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())
Expand Down
41 changes: 41 additions & 0 deletions lib/objects/git.entity.coffee~
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())

1 change: 1 addition & 0 deletions lib/objects/repo.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Repo = exports.Repo = class Repo extends GitEntity

# Method for getting plain `attributes` of the GitObject.
attributes: =>
# TODO (Anton) why we have `attributes` key here
attributes =
name : @name
type : @type
Expand Down
Loading

0 comments on commit 35fcec2

Please sign in to comment.