JGit scalability limitation - java

I am using JGit for my project where i have to maintain the files uploaded by the users.
If the file already existing then it would create a new version of the same and associate the customer with his commited/uploaded set of files.
It works file locally i.e each file one uploads , commit it to repository i.e. on server machine and adds the same recored in the DB , to associate the customer and its respective file versioning lists i.e. commitIds.
But after few iterations of upload files (each upload may consists around 200 files) , JGit able to commit the files and generate the commitIds properly but not able to retreive the content of the files when showing the user back his commmitted files.
Unfortunately log does not show any errors while retreiving the files.
Hence i am lost ....and struggling to understand stand what is wrong here.
My questions are:
Does the JGIT have enough scalability ? i.e. fetch time would be fast enough as it grows?
what to do if i retrieve the files properly.
Below is the piece of code which i am using
FileUtils.copyFile(aFile,destFile);
AddCommand add = git.add();
add.addFilepattern(".").call();
CommitCommand commit = git.commit();
commit.setAuthor(user.getFirstName(), user.getUserId());
commit.setMessage("comments" ).call();
ObjectId lastCommitId = git.getRepository().resolve(org.eclipse.jgit.lib.Constants.HEAD);
Above destFile is GIT repository and aFile is the file name
Using the lastCommitId i am trying to retrive the content of the file but me getting the:
MissingObjectException: Missing unknown 0000000000000000000000000000000000000000
Code used to retreive the file is:
ObjectId lastCommitId = repo.resolve(lastCommitId);
RevTree tree = commit.getTree();
TreeWalk treeWalk = new TreeWalk(repo);
treeWalk.addTree(tree);
treeWalk.setRecursive(true);
treeWalk.setFilter(PathFilter.create("actial_File_Name")); //this is actual file name i used
boolean next = treeWalk.next();
if (next)
{
ObjectId objectId = treeWalk.getObjectId(0);
log.logError(" objectId :" + objectId );
try{
ObjectLoader loader = repo.open(objectId);
OutputStream out = new FileOutputStream(targetFile); ///targetFile is the actual file name i wanted to retreive the content i.e orginal name
loader.copyTo(out);
}
}

The 'missing unknown 0000' seems to suggest that one of your references is missing, and the null is being returned for the objectId. You should check to see if the objectId.equals(ObjectId.zeroId()) when you do the lookup.
You might also want to check to see if the repository needs garbage collection (either with JGit or a standalone Git client) - by default JGit doesn't do automatic garbage collection.
Note also that a commit doesn't get inserted into the database until you call the call() method and you get a commit back again. Maybe that's why you can't find it in your code?

Related

Groovy: Save gform input type file to the assets pipeline (or similar)

(Sorry if this is simple; this is my first post)
Is the groovy/grails asset pipeline modifiable at runtime?
Problem: I am creating an application where users create the objects. The objects are stored as text files so that only the necessary objects are built at runtime. Currently, the text file includes a string which represents the filename of the image. The plan was to have these images stored in assets/images/ as this works best for later displaying the object. However, now I am running into issues with saving files to assets/images/ at run time, and I can't even figure out if this is possible. *Displaying images already works in the way I require if I drag and drop the images into the desired folder, however I need a way for the controller to put the image there instead. The relevant section of controller code:
def folder = new File("languageDevelopment/grails-app/assets/images/")
//println folder
def f = request.getFile('keyImage');
if (f.empty)
{
flash.message = 'file cannot be empty'
render(view: 'create')
return
}
f.transferTo(folder)
The error I'm receiving is a fileNotFoundException
"/var/folders/9c/0brqct9j6pj4j85wnc5zljvc0000gn/T/languageDevelopment/grails-app/assets/images (No such file or directory)"
on f.transferTo(folder)
What is the section it is adding to the beginning of my "folder" object?
Thanks in advance. If you need more information or have a suggestion to a different route please let me know!
new File("languageDevelopment/grails-app/assets/images/")
This folder is present only in your sources
After deployment it will looks like "/PATH-TO-TOMCAT/webapps/ROOT/assets/" if you use tomcat.
Also asset/images, asset/font etc. will be merged to assets folder.
If you'd like to store temporary files you can create some directory under src/resources folder.
For example "src/resources/images"
And you can get access to this folder from classloader:
this.class.classLoader.getResource('images/someImage.png').path

Avoid download of previously downloaded files using Java FTP

I have a desktop application which downloads all files on server.When a new file is added I want to download only the newer file.
Well to know which one is the "new one" you have to create a map/or other datastructure and put pair of the name/metadata creationtime(or last modified time)which one suits you best , when you iterate over your files just see their metadata with
Path file = ...;
BasicFileAttributes attr = Files.readAttributes(file, BasicFileAttributes.class);
attr.creationTime(); //or attr.lastModifiedTime();
When you compare these times with one on server decide to download only the one with latest time.
Either way you have to keep track of at least the name/time modified(or created) at your previous download and compare these.
If this application on your desktop is not some kind of service that runs nonstop,find some way to persist that data on system,serialization or embed database h2/hsqldb within it.Use streams with conncurent iteration/ parralelStream to check these times and compare ,in case you use java8
edit- to get metadata from url, check this question Get the Last Modified date of an URL

How to get and edit Android download list?

Can I get and edit the list of files, downloaded with DownloadProvider?
What do I mean? We have an application called Downloads in Android that displays all the downloads made with DownloadProvider. The records it displays are stored in a database somewhere in /data and are not strongly connected with real files. E.g. if I delete a record in Downloads, the file is deleted too, but not vice versa.
So, I want to delete the file in filesystem and delete the record in Downloads application about this file.
Currently I have tried using something like:
DownloadManager dm = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterByStatus(DownloadManager.STATUS_PAUSED|
DownloadManager.STATUS_PENDING|
DownloadManager.STATUS_RUNNING|
DownloadManager.STATUS_SUCCESSFUL);
Cursor cur = dm.query(query);
and I don't see any ID's in query which I could pass to the DownloadManager.remove(long...IDs). Can I do what I want with Android API or the only way to achieve this is to edit the database itself?
Try call
mDownloadManager.remove(long... ids)

Want to delete existing document from folder and database values from table using transaction in hibernate query

I have an application with document uploading.After uploading the document,the document path is newly created and the document is saved within the file path.At the same time the document path and related values are saved with in the database table.In my application after uploading,there is a button to delete the unwanted document.Sometimes the document deletion operation is not properly work.So there is a wastage of memory will occur.I want to avoid the situation by using transaction statement.I don't know how to use the hibernate transaction to my work.Is this possible? Please help me to do the work successfully(I am using spring with hibernate integration and postgresql)
Thank you
In controller
int supDocId=1102;
String docPath=D:/;
String filePath=docPath+supDocId+".pdf";
File file=new File(filePath);
boolean isDelete = servicesService.deleteDocument(supDocId);
if(isDelete)
{
if(file.exists())
{
file.delete();
}
alertMsg = "The file is deleted sucessfully";
}
else{
alertMsg = "Deletion Failed.!!! File is under processing..";
}
In service class
public boolean deleteDocument(int supDocId){
return servicesDAO.deleteDocument(supDocId);
}
In servicesDAO class
public boolean deleteDocument(int supDocId){
int deleteStatus=0;
try {
String deleteQuery = "DELETE FROM tablename WHERE attch_doc_id='"+supDocId+"'";
Query deleteQ = session.createSQLQuery(deleteQuery);
deleteStatus = deleteQ.executeUpdate();
if(deleteStatus>0){
return deleteStatus;
}
} catch (Exception e) {
e.printStackTrace();
}
return deleteStatus;
}
I want to work the two operations(document and database value deletion) within the DAO class using transaction statement.
If DB transaction fails - you will have problem not only with deleted files, but also with uploaded new files.
In most cases file system does not supports transaction so I don't think bullet proof solution can be achieved using XA (distributed transactions using the JTA) or similar approach.
Quite strait forward solution I am using in some of my projects:
Files in file system are created before DB commit (but preferably after hibernate flush).
On file delete operation, reference to file is deleted from DB, but not from file system.
Asynchronous process periodically scans all files in file system and deletes files with no references from DB.
If you have big number of files some optimization can be necessary to prevent always scan all files. For example:
Put new files to folder with name "current date", so only part of files can be checked for unsuccessful transactions involving uploaded new files.
On file delete operation insert new record to table "deleted_files" with reference to file that should be deleted.
Create asynchronous process to periodically scans table "deleted_files", which deletes physical file and if deletion is successful (or file already is missing) removes record from table "deleted_files" and commits transaction.
You shouldn't use +supDocId+ Replace that with ? instead or you would be leaving yourself vulnerable to Sql Injection.
And deleteQ.Execute();is what you want to be doing not an update because you want to be checking boolean true or false if something is deleted.

Use box java sdk to download an item without knowing if it's a file or a folder

With the ID of an item stored on box, you can download it if it's a file, or download its contents if it's a folder. Either way it seems you need to know what sort of thing you're downloading in order to access it, doing either BoxFile file = new BoxFile(api, id); or BoxFolder folder = new BoxFolder(api, id); before handling the actual download.
I was hoping to be able to do something like
BoxItem boxItem = new BoxItem(api, id);
if (boxItem instanceof BoxFile) {
// download file
} else if (boxItem instanceof BoxFolder) {
// download all files in folder
}
sort of like the example in the docs of downloading a folder's contents. However, in that case the ID is that of a specific folder, whereas my ID is for either a folder or a file in the root folder, and I don't want to loop through all of the root folder's contents. And, anyway, BoxItem cannot be instantiated.
How can I tell ahead of time whether I'm downloading a file or a folder, with just the item's ID? If not, is there a way to download the item anyway?
I don't believe the API supports downloading an entire folder (and therefore the SDK doesn't either). The closest you can get is downloading all of the folder's files. That's why BoxFile has a download() method, but BoxFolder and BoxItem don't.
As for checking whether or not an ID corresponds to a file or folder - there isn't a way to tell without trying to make an API request. For example, you could try doing new BoxFile(api, id).getInfo() and seeing if it returns a 404.

Categories

Resources