/**
*
* @param dbmdJson
* @param repo
* @param tree
* @param conn
* @param clmnTree
* @return
* @throws IOException
* @throws sqlException
*/
public static ObjectId MetaTreeCommit(ObjectJson dbmdJson,Repository repo,TreeFormatter tree,Connection conn,Boolean clmnTree) throws IOException,sqlException {
DatabaseMetaData dbmd = conn.getMetaData();
// ObjectJson dbmdJson = new ObjectJson();
String mapString = MetaDbInfo(dbmd);
ObjectInserter objectInserter = repo.newObjectInserter();
ObjectId blobId = objectInserter.insert(Constants.OBJ_BLOB,mapString.getBytes());
objectInserter.flush();
tree.append(Consts.DATABASE,FileMode.REGULAR_FILE,blobId);
Utils.putTableMeta(repo,conn,dbmd,objectInserter,tree,clmnTree);
ObjectId treeId = objectInserter.insert(tree);
objectInserter.flush();
System.out.println("Tree ID: " + treeId.getName());
return treeId;
}
/**
*
* @param datarepo
* @param tree
* @param conn
* @param clmnTree
* @return
* @throws sqlException
* @throws IOException
*/
public static ObjectId dataTreeCommit(Repository datarepo,Boolean clmnTree) throws sqlException,IOException {
DatabaseMetaData dbmd = conn.getMetaData();
// ObjectJson dbmdJson = new ObjectJson();
String mapString = MetaDbInfo(dbmd);
// Build Db_Info object,general info about Database
ObjectInserter objectInserter = datarepo.newObjectInserter();
ObjectId blobId = objectInserter.insert(Constants.OBJ_BLOB,mapString.getBytes());
objectInserter.flush();
tree.append("DATABASE",blobId);
// Continue building Database Tree
Utils.putTableMeta(datarepo,clmnTree);
ObjectId treeId = objectInserter.insert(tree);
objectInserter.flush();
System.out.println("Tree ID: " + treeId.getName());
return treeId;
}
/**
*
* @param columns
* @param objectInserter
* @param tblTree
* @param walkTbl
* @return
* @throws sqlException
* @throws IOException
*/
public static ObjectId putColumnMetaAsObject(ResultSet columns,ObjectInserter objectInserter,TreeFormatter tblTree,RevWalk walkTbl) throws sqlException,IOException {
// Build object for column attributs
ObjectId objectId = buildMetaObject(columns,Consts.COLUMN_Meta_ATTRIBUTES,objectInserter);
tblTree.append(Consts.COLUMN_Meta,(AnyObjectId) objectId);
// Add empty tree for the column content
TreeFormatter clmnDataTree = new TreeFormatter();
ObjectId clmnDataTreeId = objectInserter.insert(clmnDataTree);
objectInserter.flush();
tblTree.append(Consts.COLUMN_DATA,walkTbl.parseTree(clmnDataTreeId));
return objectId;
}
/**
*
* @param columns
* @param objectInserter
* @param tblTree
* @param walkTbl
* @return
* @throws sqlException
* @throws IOException
*/
public static ObjectId putColumnMetaAsTree(ResultSet columns,IOException {
// Add tree for the column Metadata
TreeFormatter clmnMetaTree = new TreeFormatter();
// Build tree for column attributs
buildMetaTree(columns,clmnMetaTree);
ObjectId clmnMetaTreeId = objectInserter.insert(clmnMetaTree);
objectInserter.flush();
tblTree.append(Consts.COLUMN_Meta,walkTbl.parseTree(clmnMetaTreeId));
// Add empty tree for the column content
TreeFormatter clmnDataTree = new TreeFormatter();
ObjectId clmnDataTreeId = objectInserter.insert(clmnDataTree);
objectInserter.flush();
tblTree.append(Consts.COLUMN_DATA,walkTbl.parseTree(clmnDataTreeId));
return clmnMetaTreeId;
}
private static ObjectId buildMetaTree(ResultSet set,String[] attrs,ObjectInserter inserter,TreeFormatter tree) throws IOException,sqlException {
for (String attr : attrs) {
ObjectJson objectJson = new ObjectJson();
objectJson.addDoc(attr,set.getString(attr));
String attrjson = new Map2JsonString().convert(objectJson.getobjectAsMap());
ObjectId objectId = inserter.insert(Constants.OBJ_BLOB,attrjson.getBytes());
inserter.flush();
System.out.println("MetaObject: " + attrjson);
// commitJson.addDoc(Consts.OBJECT_ID,objectId);
tree.append(attr,objectId);
}
return tree.computeId(inserter);
}
/**
*
* @param repo
* @param conn
* @param dbmd
* @param objectInserter
* @param dbMetaTree
* @param clmnTree
* @return
*/
public static TreeFormatter putTableMeta(Repository repo,DatabaseMetaData dbmd,TreeFormatter dbMetaTree,Boolean clmnTree) {
String[] types = {"TABLE"};
try {
ResultSet tables = dbmd.getTables(null,null,types);
RevWalk walkTbl = new RevWalk(repo);
while (tables.next()) {
System.out.println("***************************************");
String tableName = tables.getString(Consts.TABLE_NAME);
TreeFormatter tblMetaTree = new TreeFormatter();
// Build tree for table attributs
buildMetaTree(tables,Consts.TABLE_Meta_ATTRIBUTES,tblMetaTree);
ResultSet columns = dbmd.getColumns(null,tableName,null);
if (clmnTree) {
addColumnsAsTrees(columns,tblMetaTree,walkTbl,dbMetaTree);
} else {
addColumnsAsObjects(columns,dbMetaTree);
}
TreeFormatter tblDataTree = new TreeFormatter();
ObjectId tblDataTreeId = objectInserter.insert(tblDataTree);
objectInserter.flush();
dbMetaTree.append(Consts.TABLE_DATA,walkTbl.parseTree(tblDataTreeId));
}
} catch (sqlException | IOException ex) {
System.out.println(ex.getMessage());
}
return dbMetaTree;
}
/**
*
* @param columns
* @param objectInserter
* @param tblMetaTree
* @param walkTbl
* @param dbMetaTree
* @throws IOException
* @throws sqlException
*/
public static void addColumnsAsObjects(ResultSet columns,TreeFormatter tblMetaTree,RevWalk walkTbl,TreeFormatter dbMetaTree) throws IOException,sqlException {
while (columns.next()) {
putColumnMetaAsObject(columns,walkTbl);
}
ObjectId tblMetaTreeId = objectInserter.insert(tblMetaTree);
objectInserter.flush();
dbMetaTree.append(Consts.TABLE_Meta,walkTbl.parseTree(tblMetaTreeId));
}
/**
*
* @param columns
* @param objectInserter
* @param tblMetaTree
* @param walkTbl
* @param dbMetaTree
* @throws sqlException
* @throws IOException
*/
public static void addColumnsAsTrees(ResultSet columns,TreeFormatter dbMetaTree) throws sqlException,IOException {
while (columns.next()) {
putColumnMetaAsTree(columns,walkTbl.parseTree(tblMetaTreeId));
}
@Nonnull
private GitFileEntry newDirectoryEntry(String... children) throws IOException {
TreeFormatter tf = new TreeFormatter();
for(String child : children) {
GitFileEntry childEntry = someFileEntry();
tf.append(child,childEntry.getMode(),childEntry.getId());
}
ObjectId treeId = TreeUtils.insertTree(tf,repo);
return newTreeEntry(treeId);
}
@Test
public void insertTreeIntoRepository_shouldBeAbletoRetrieveChildrenIdsByTreeIdAndFilename() throws IOException {
initRepository();
TreeFormatter tf = new TreeFormatter();
ObjectId nodeObject1 = someObjectId();
tf.append("file1.txt",REGULAR_FILE,nodeObject1);
ObjectId nodeObject2 = someObjectId();
tf.append("file2.txt",nodeObject2);
ObjectId tree = TreeUtils.insertTree(tf,repo);
assertEquals(nodeObject1,TreeUtils.getobjectId("file1.txt",repo));
assertEquals(nodeObject2,TreeUtils.getobjectId("file2.txt",repo));
}
@Test
public void insertTreeIntoRepository_shouldBeAbletoRetrieveChildrenFileModesByTreeIdAndFilename() throws IOException {
initRepository();
TreeFormatter tf = new TreeFormatter();
tf.append("file.txt",someObjectId());
tf.append("dir",TREE,someObjectId());
ObjectId tree = TreeUtils.insertTree(tf,repo);
assertEquals(REGULAR_FILE,TreeUtils.getFileMode("file.txt",repo));
assertEquals(TREE,TreeUtils.getFileMode("dir",repo));
}
项目:git-as-svn
文件:GitTreeUpdate.java
@NotNull ObjectId buildTree(@NotNull ObjectInserter inserter) throws IOException,SVNException {
final TreeFormatter treeBuilder = new TreeFormatter();
final List<GitTreeEntry> sortedEntries = new ArrayList<>(entries.values());
Collections.sort(sortedEntries);
for (GitTreeEntry entry : sortedEntries) {
treeBuilder.append(entry.getFileName(),entry.getFileMode(),entry.getobjectId().getobject());
}
new ObjectChecker().checkTree(treeBuilder.toByteArray());
return inserter.insert(treeBuilder);
}
public static void main(String[] args) throws IOException,GitAPIException {
Connection conn = null;
Repository datarepo = Commands.getRepo(Consts.DATA_REPO_PATH + ".git"); // (1)
ObjectId lastCommitId = datarepo.resolve(Constants.HEAD); // (2)
System.out.println("Last Commit: " + lastCommitId);
try {
conn = DriverManager.getConnection(URL);
TreeFormatter tree = new TreeFormatter(); // (3)
if (conn != null) {
ObjectJson dbmdJson = new ObjectJson();
ObjectId treeId = Utils.dataTreeCommit(datarepo,true); // (4)
ObjectId lastTreeId = Commands.getLastCommitTreeId(datarepo,lastCommitId); // (5)
ObjectId commitId = Commands.processCommit(lastCommitId,treeId,datarepo,lastTreeId); // (6)
if (commitId != null) {
List<DiffEntry> list = Diffs.listDiffs(datarepo,lastTreeId,treeId); // (7)
if (list != null) {
// Simply display the diff between the two commits
list.forEach((diff) -> {
// Todo
// Perform indexing of added to commit objects // (8)
// Print trace results
System.out.println(diff);
});
// Index the whole commit tree // (9)
System.out.print(dbmdJson.getobjectAsMap());
}
}
} else {
System.out.println("Metadata not supported");
}
} catch (sqlException ex1) {
System.err.println(ex1);
} finally {
if (conn != null) {
try {
conn.close();
} catch (sqlException ex) {
Logger.getLogger(DataMain.class.getName()).log(Level.SEVERE,ex);
}
}
}
}
public static void main(String[] args) throws IOException,GitAPIException {
Connection conn = null;
Repository repo = Commands.getRepo(Consts.Meta_REPO_PATH + ".git"); // (1)
ObjectId lastCommitId = repo.resolve(Constants.HEAD); // (2)
System.out.println("Last Commit: " + lastCommitId);
try {
conn = DriverManager.getConnection(URL);
TreeFormatter tree = new TreeFormatter(); // (3)
if (conn != null) {
ObjectJson dbmdJson = new ObjectJson();
ObjectId treeId = Utils.MetaTreeCommit(dbmdJson,repo,false); // (4)
ObjectId lastTreeId = Commands.getLastCommitTreeId(repo,lastTreeId); // (6)
if (commitId != null) {
List<DiffEntry> list = Diffs.listDiffs(repo,treeId); // (7)
if (list != null) {
// Simply display the diff between the two commits
list.forEach((diff) -> {
// Todo
// Perform indexing of added to commit objects
});
}
// Index the whole commit tree // (9)
DbIndex dbIndex = new DbIndex(Consts.Meta_REPO_PATH + ".git",esUrl,esPort);
dbIndex.indexCommitTree(repo);
}
} else {
System.out.println("Metadata not supported");
}
} catch (sqlException ex1) {
System.err.println(ex1);
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (sqlException ex2) {
System.out.println(ex2.getMessage());
}
}
}
项目:gerrit
文件:CreateChange.java
private static ObjectId emptyTreeId(ObjectInserter inserter) throws IOException {
return inserter.insert(new TreeFormatter());
}